xref: /plugin/aclinfo/_test/AclInfoTest.php (revision ef67e929675a5055a440496af227b50903f73f69)
1*ef67e929SAndreas Gohr<?php
2*ef67e929SAndreas Gohr
3*ef67e929SAndreas Gohrnamespace dokuwiki\plugin\aclinfo\test;
4*ef67e929SAndreas Gohr
5*ef67e929SAndreas Gohruse DokuWikiTest;
6*ef67e929SAndreas Gohr
7*ef67e929SAndreas Gohr/**
8*ef67e929SAndreas Gohr * Tests for the aclinfo plugin
9*ef67e929SAndreas Gohr *
10*ef67e929SAndreas Gohr * @group plugin_aclinfo
11*ef67e929SAndreas Gohr * @group plugins
12*ef67e929SAndreas Gohr */
13*ef67e929SAndreas Gohrclass AclInfoTest extends DokuWikiTest
14*ef67e929SAndreas Gohr{
15*ef67e929SAndreas Gohr
16*ef67e929SAndreas Gohr    /**
17*ef67e929SAndreas Gohr     * @return array
18*ef67e929SAndreas Gohr     * @see testProcessAclMatches
19*ef67e929SAndreas Gohr     */
20*ef67e929SAndreas Gohr    public function provideProcessAclMatches()
21*ef67e929SAndreas Gohr    {
22*ef67e929SAndreas Gohr        return [
23*ef67e929SAndreas Gohr            'empty array' => [
24*ef67e929SAndreas Gohr                [],
25*ef67e929SAndreas Gohr                []
26*ef67e929SAndreas Gohr            ],
27*ef67e929SAndreas Gohr            'single user permission' => [
28*ef67e929SAndreas Gohr                ['wiki:page  user  1'],
29*ef67e929SAndreas Gohr                ['user' => 1]
30*ef67e929SAndreas Gohr            ],
31*ef67e929SAndreas Gohr            'multiple user permissions' => [
32*ef67e929SAndreas Gohr                ['wiki:page  user1  1', 'wiki:page  user2  2'],
33*ef67e929SAndreas Gohr                ['user1' => 1, 'user2' => 2]
34*ef67e929SAndreas Gohr            ],
35*ef67e929SAndreas Gohr            'comment removal' => [
36*ef67e929SAndreas Gohr                ['wiki:page  user  1  # this is a comment'],
37*ef67e929SAndreas Gohr                ['user' => 1]
38*ef67e929SAndreas Gohr            ],
39*ef67e929SAndreas Gohr            'admin capped to delete' => [
40*ef67e929SAndreas Gohr                ['wiki:page  admin  255'],
41*ef67e929SAndreas Gohr                ['admin' => 16] // AUTH_DELETE = 16
42*ef67e929SAndreas Gohr            ],
43*ef67e929SAndreas Gohr            'first permission wins for duplicate user' => [
44*ef67e929SAndreas Gohr                ['wiki:page  user  1', 'wiki:page  user  8'],
45*ef67e929SAndreas Gohr                ['user' => 1]
46*ef67e929SAndreas Gohr            ],
47*ef67e929SAndreas Gohr        ];
48*ef67e929SAndreas Gohr    }
49*ef67e929SAndreas Gohr
50*ef67e929SAndreas Gohr    /**
51*ef67e929SAndreas Gohr     * @dataProvider provideProcessAclMatches
52*ef67e929SAndreas Gohr     * @param array $matches
53*ef67e929SAndreas Gohr     * @param array $expected
54*ef67e929SAndreas Gohr     */
55*ef67e929SAndreas Gohr    public function testProcessAclMatches(array $matches, array $expected)
56*ef67e929SAndreas Gohr    {
57*ef67e929SAndreas Gohr        $plugin = new \syntax_plugin_aclinfo();
58*ef67e929SAndreas Gohr        $result = $this->callInaccessibleMethod($plugin, 'processAclMatches', [$matches]);
59*ef67e929SAndreas Gohr        $this->assertEquals($expected, $result);
60*ef67e929SAndreas Gohr    }
61*ef67e929SAndreas Gohr
62*ef67e929SAndreas Gohr    /**
63*ef67e929SAndreas Gohr     * @return array
64*ef67e929SAndreas Gohr     * @see testAclCheck
65*ef67e929SAndreas Gohr     */
66*ef67e929SAndreas Gohr    public function provideAclCheck()
67*ef67e929SAndreas Gohr    {
68*ef67e929SAndreas Gohr        return [
69*ef67e929SAndreas Gohr            'exact page match' => [
70*ef67e929SAndreas Gohr                'wiki:page',
71*ef67e929SAndreas Gohr                [
72*ef67e929SAndreas Gohr                    'wiki:page  @ALL     1',
73*ef67e929SAndreas Gohr                    'wiki:page  user1    2',
74*ef67e929SAndreas Gohr                ],
75*ef67e929SAndreas Gohr                ['@ALL' => 1, 'user1' => 2]
76*ef67e929SAndreas Gohr            ],
77*ef67e929SAndreas Gohr            'namespace match' => [
78*ef67e929SAndreas Gohr                'namespace:page',
79*ef67e929SAndreas Gohr                [
80*ef67e929SAndreas Gohr                    'namespace:*  @ALL  1',
81*ef67e929SAndreas Gohr                ],
82*ef67e929SAndreas Gohr                ['@ALL' => 1]
83*ef67e929SAndreas Gohr            ],
84*ef67e929SAndreas Gohr            'exact match takes precedence over namespace' => [
85*ef67e929SAndreas Gohr                'namespace:page',
86*ef67e929SAndreas Gohr                [
87*ef67e929SAndreas Gohr                    'namespace:page  user1  8',
88*ef67e929SAndreas Gohr                    'namespace:*    @ALL  1',
89*ef67e929SAndreas Gohr                ],
90*ef67e929SAndreas Gohr                ['user1' => 8, '@ALL' => 1]
91*ef67e929SAndreas Gohr            ],
92*ef67e929SAndreas Gohr            'root page namespace' => [
93*ef67e929SAndreas Gohr                ':page',
94*ef67e929SAndreas Gohr                [
95*ef67e929SAndreas Gohr                    '*  @ALL  1',
96*ef67e929SAndreas Gohr                ],
97*ef67e929SAndreas Gohr                ['@ALL' => 1]
98*ef67e929SAndreas Gohr            ],
99*ef67e929SAndreas Gohr            'empty namespace climbs to root' => [
100*ef67e929SAndreas Gohr                'namespace:sub:page',
101*ef67e929SAndreas Gohr                [
102*ef67e929SAndreas Gohr                    '*           @ALL  1',
103*ef67e929SAndreas Gohr                ],
104*ef67e929SAndreas Gohr                ['@ALL' => 1]
105*ef67e929SAndreas Gohr            ],
106*ef67e929SAndreas Gohr            'multiple namespace levels' => [
107*ef67e929SAndreas Gohr                'a:b:c:page',
108*ef67e929SAndreas Gohr                [
109*ef67e929SAndreas Gohr                    'a:b:c:page  user1  2',
110*ef67e929SAndreas Gohr                    'a:b:*       user2  4',
111*ef67e929SAndreas Gohr                    'a:*         user3  8',
112*ef67e929SAndreas Gohr                ],
113*ef67e929SAndreas Gohr                ['user1' => 2, 'user2' => 4, 'user3' => 8]
114*ef67e929SAndreas Gohr            ],
115*ef67e929SAndreas Gohr        ];
116*ef67e929SAndreas Gohr    }
117*ef67e929SAndreas Gohr
118*ef67e929SAndreas Gohr    /**
119*ef67e929SAndreas Gohr     * @dataProvider provideAclCheck
120*ef67e929SAndreas Gohr     * @param string $id
121*ef67e929SAndreas Gohr     * @param array $authAcl
122*ef67e929SAndreas Gohr     * @param array $expected
123*ef67e929SAndreas Gohr     */
124*ef67e929SAndreas Gohr    public function testAclCheck(string $id, array $authAcl, array $expected)
125*ef67e929SAndreas Gohr    {
126*ef67e929SAndreas Gohr        global $AUTH_ACL;
127*ef67e929SAndreas Gohr        $AUTH_ACL = $authAcl;
128*ef67e929SAndreas Gohr
129*ef67e929SAndreas Gohr        $plugin = new \syntax_plugin_aclinfo();
130*ef67e929SAndreas Gohr        $result = $this->callInaccessibleMethod($plugin, 'aclCheck', [$id]);
131*ef67e929SAndreas Gohr
132*ef67e929SAndreas Gohr        $this->assertEquals($expected, $result);
133*ef67e929SAndreas Gohr    }
134*ef67e929SAndreas Gohr
135*ef67e929SAndreas Gohr    public function testAclCheckNoMatch()
136*ef67e929SAndreas Gohr    {
137*ef67e929SAndreas Gohr        global $AUTH_ACL;
138*ef67e929SAndreas Gohr        $AUTH_ACL = [];
139*ef67e929SAndreas Gohr
140*ef67e929SAndreas Gohr        $plugin = new \syntax_plugin_aclinfo();
141*ef67e929SAndreas Gohr        $result = $this->callInaccessibleMethod($plugin, 'aclCheck', ['nonexistent:page']);
142*ef67e929SAndreas Gohr
143*ef67e929SAndreas Gohr        $this->assertEquals([], $result);
144*ef67e929SAndreas Gohr    }
145*ef67e929SAndreas Gohr}
146