1<?php
2/**
3 *  auth_loadACL carries out the user & group substitutions
4 *
5 * @author     Chris Smith <chris@jalakai.co.uk>
6 */
7
8class auth_loadacl_test extends DokuWikiTest {
9
10    function setUp() : void {
11        global $USERINFO;
12        parent::setUp();
13        $_SERVER['REMOTE_USER'] = 'testuser';
14        $USERINFO['grps'] = array('foo','bar');
15    }
16
17    function tearDown() : void {
18        parent::tearDown();
19    }
20
21    function auth_loadACL_testwrapper($acls) {
22        global $config_cascade;
23        $acl_file = $config_cascade['acl']['default'];
24
25        $config_cascade['acl']['default'] .= '.test';
26        file_put_contents($config_cascade['acl']['default'],$acls);
27
28        $result = auth_loadACL();
29
30        unlink($config_cascade['acl']['default']);
31        $config_cascade['acl']['default'] = $acl_file;
32
33        return $result;
34    }
35
36    function test_simple() {
37        $acls = <<<ACL
38* @ALL 2
39ACL;
40        $expect = array("*\t@ALL 2");
41        $this->assertEquals($expect, $this->auth_loadACL_testwrapper($acls));
42    }
43
44    function test_user_substitution() {
45        $acls = <<<ACL
46%USER% %USER% 2
47ACL;
48        $expect = array(
49            "testuser\ttestuser 2",
50        );
51        $this->assertEquals($expect, $this->auth_loadACL_testwrapper($acls));
52    }
53
54    function test_group_substitution() {
55        $acls = <<<ACL
56%GROUP% %GROUP% 2
57ACL;
58        $expect = array(
59            "foo\t@foo 2",
60            "bar\t@bar 2",
61        );
62        $this->assertEquals($expect, $this->auth_loadACL_testwrapper($acls));
63    }
64
65    function test_both_substitution() {
66        $acls = <<<ACL
67%GROUP%:%USER% %USER% 2
68%GROUP%:%USER% %GROUP% 2
69ACL;
70        $expect = array(
71            "foo:testuser\ttestuser 2",
72            "bar:testuser\ttestuser 2",
73            "foo:testuser\t@foo 2",
74            "bar:testuser\t@bar 2",
75        );
76        $this->assertEquals($expect, $this->auth_loadACL_testwrapper($acls));
77    }
78
79    // put it all together - read the standard acl provided with the test suite
80    function test_standardtestacls(){
81        $expect = array(
82            "*\t@ALL        8",
83            "private:*\t@ALL        0",
84            "users:*\t@ALL         1",
85            "users:testuser:*\ttestuser       16",
86            "groups:*\t@ALL         1",
87            "groups:foo:*\t@foo      16",
88            "groups:bar:*\t@bar      16",
89        );
90        $this->assertEquals($expect, auth_loadACL());
91    }
92
93    // FS#2867, '\s' in php regular expressions may match non-space characters utf8 strings
94    // this is due to locale setting on the server, which may match bytes '\xA0' and '\x85'
95    // these two bytes are present in valid multi-byte UTF-8 characters.
96    // this test will use one, 'ठ' (DEVANAGARI LETTER TTHA, e0 a4 a0).  There are many others.
97    function test_FS2867() {
98        global $USERINFO;
99
100        $old_locale = setlocale(LC_ALL, '0');
101        setlocale(LC_ALL, "English_United States.1252");  // should only succeed on windows systems
102        setlocale(LC_ALL, "en_US.UTF-8");                 // should succeed on other systems
103
104        // no point continuing with this test if \s doesn't match A0
105        if (!preg_match('/\s/',"\xa0")) {
106            setlocale(LC_ALL, $old_locale);
107            $this->markTestSkipped('Unable to change locale.');
108        }
109
110        $_SERVER['REMOTE_USER'] = 'utfठ8';
111        $USERINFO['grps'] = array('utfठ16','utfठa');
112
113        $acls = <<<ACL
114%GROUP%:%USER% %USER% 2
115%GROUP%:* %GROUP% 4
116devangariठttha @ALL 2
117ACL;
118        $expect = array(
119            "utfठ16:utfठ8\tutfठ8 2",
120            "utfठa:utfठ8\tutfठ8 2",
121            "utfठ16:*\t@utfठ16 4",
122            "utfठa:*\t@utfठa 4",
123            "devangariठttha\t@ALL 2",
124        );
125        $this->assertEquals($expect, $this->auth_loadACL_testwrapper($acls));
126        setlocale(LC_ALL, $old_locale);
127    }
128}
129
130//Setup VIM: ex: et ts=4 :
131