1<?php
2
3/**
4 * Helper tests for the elasticsearch plugin
5 *
6 * @group plugin_elasticsearch
7 * @group plugins
8 */
9class helper_plugin_elasticsearch_test extends DokuWikiTest
10{
11    protected $pluginsEnabled = ['elasticsearch'];
12
13    protected $acl = [
14        '*	@user	8',
15        '*	@ALL	0',
16        'macro:*	@user	0',
17        'macro:*	@mino	8',
18        'mini:*	@user	0',
19        'mini:micro:*	mirco	0',
20        'mini:micro:*	@user	8',
21        'mini:micro:nano:*	@micra	8',
22        'mini:micro:nano:*	@nana	8',
23        'mini:micro:nano:*	@ALL	0',
24        'mini:micro:nano:stop	spots	8',
25        'mini:micro:nano:stop	@ALL	0',
26        'super:*	@ALL	8',
27        'super:mini:*	@ALL	0',
28        'super:mini:stop	@nana	8',
29    ];
30
31    /**
32     * Expected permissions
33     *
34     * @return array
35     */
36    public function dataSanity()
37    {
38        return [
39            ['mini:micro:nano:stop', 'user', 'group', 0],
40            ['mini:micro:nano:stop', 'spots', 'group', 8],
41            ['mini:micro:nano:start', 'user', 'micra', 8],
42            ['mini:micro:nano:start', 'user', 'nana', 8],
43            ['mini:micro:start', 'user', 'user', 8],
44            ['mini:micro:start', 'mirco', 'user', 8],
45            ['mini:micro:start', 'mirco', 'group', 0],
46            ['super:start', 'user', 'ALL', 8],
47            ['super:start', 'user', 'group', 8],
48            ['super:mini:start', 'user', 'nana', 0],
49            ['super:mini:stop', 'user', 'nana', 8],
50        ];
51    }
52
53    /**
54     * Provide the test data for the ACL check
55     *
56     * @return array
57     */
58    public function dataACL()
59    {
60        return [
61            [
62                'mini:micro:nano:stop',
63                [
64                    'groups_include' => [],
65                    'groups_exclude' => [],
66                    'users_include' => ['spots'],
67                    'users_exclude' => [],
68                ]
69            ],
70            [
71                'mini:micro:nano:start',
72                [
73                    'groups_include' => ['micra', 'nana'],
74                    'groups_exclude' => [],
75                    'users_include' => [],
76                    'users_exclude' => [],
77                ]
78            ],
79            [
80                'mini:micro:start',
81                [
82                    'groups_include' => ['user'],
83                    'groups_exclude' => [],
84                    'users_include' => [],
85                    'users_exclude' => ['mirco'],
86                ]
87            ],
88            [
89                'super:start',
90                [
91                    'groups_include' => ['ALL'],
92                    'groups_exclude' => [],
93                    'users_include' => [],
94                    'users_exclude' => [],
95                ]
96            ],
97            [
98                'super:mini:start',
99                [
100                    'groups_include' => [],
101                    'groups_exclude' => [],
102                    'users_include' => [],
103                    'users_exclude' => [],
104                ]
105            ],
106            [
107                'super:mini:stop',
108                [
109                    'groups_include' => ['nana'],
110                    'groups_exclude' => [],
111                    'users_include' => [],
112                    'users_exclude' => [],
113                ]
114            ],
115        ];
116    }
117
118    /**
119     * Run "normal" ACL checks to make sure we do expect the correct thing to happen
120     *
121     * This is mainly to sanity check the expectations we have for the results in the ACL checks
122     *
123     * @dataProvider dataSanity
124     * @param string $page
125     * @param string $user
126     * @param string $group
127     * @param int $expected
128     */
129    public function testSanity($page, $user, $group, $expected)
130    {
131        global $AUTH_ACL;
132        $AUTH_ACL = $this->acl;
133
134        $this->assertSame($expected, auth_aclcheck($page, $user, [$group]));
135    }
136
137    /**
138     * Check parsed ACLs for a given page
139     *
140     * @dataProvider dataACL
141     * @param string $page
142     * @param array $expected
143     */
144    public function testACL($page, $expected)
145    {
146        global $AUTH_ACL;
147        $AUTH_ACL = $this->acl;
148
149        /** @var helper_plugin_elasticsearch_acl $helper */
150        $helper = plugin_load('helper', 'elasticsearch_acl');
151
152        $raw = $helper->getPageACL($page);
153        $actual = $helper->splitRules($raw);
154
155        $this->assertEquals($expected, $actual);
156    }
157}
158