xref: /dokuwiki/_test/tests/inc/auth_loadacl.test.php (revision 0113757259202e06f0316ef4be0f938b134e6e9c)
11f6e92faSChristopher Smith<?php
21f6e92faSChristopher Smith/**
31f6e92faSChristopher Smith *  auth_loadACL carries out the user & group substitutions
41f6e92faSChristopher Smith *
5*01137572SChristopher Smith * @author     Chris Smith <chris@jalakai.co.uk>
61f6e92faSChristopher Smith */
71f6e92faSChristopher Smith
81f6e92faSChristopher Smithclass auth_loadacl_test extends DokuWikiTest {
91f6e92faSChristopher Smith
101f6e92faSChristopher Smith    function setUp() {
111f6e92faSChristopher Smith        global $USERINFO;
121f6e92faSChristopher Smith        parent::setUp();
131f6e92faSChristopher Smith        $_SERVER['REMOTE_USER'] = 'testuser';
141f6e92faSChristopher Smith        $USERINFO['grps'] = array('foo','bar');
151f6e92faSChristopher Smith    }
161f6e92faSChristopher Smith
171f6e92faSChristopher Smith    function tearDown() {
181f6e92faSChristopher Smith        parent::tearDown();
191f6e92faSChristopher Smith    }
201f6e92faSChristopher Smith
211f6e92faSChristopher Smith    function auth_loadACL_testwrapper($acls) {
221f6e92faSChristopher Smith        global $config_cascade;
231f6e92faSChristopher Smith        $acl_file = $config_cascade['acl']['default'];
241f6e92faSChristopher Smith
251f6e92faSChristopher Smith        $config_cascade['acl']['default'] .= '.test';
261f6e92faSChristopher Smith        file_put_contents($config_cascade['acl']['default'],$acls);
271f6e92faSChristopher Smith
281f6e92faSChristopher Smith        $result = auth_loadACL();
291f6e92faSChristopher Smith
301f6e92faSChristopher Smith        unlink($config_cascade['acl']['default']);
311f6e92faSChristopher Smith        $config_cascade['acl']['default'] = $acl_file;
321f6e92faSChristopher Smith
331f6e92faSChristopher Smith        return $result;
341f6e92faSChristopher Smith    }
351f6e92faSChristopher Smith
361f6e92faSChristopher Smith    function test_simple() {
371f6e92faSChristopher Smith        $acls = <<<ACL
381f6e92faSChristopher Smith* @ALL 2
391f6e92faSChristopher SmithACL;
401f6e92faSChristopher Smith        $expect = array("*\t@ALL 2");
411f6e92faSChristopher Smith        $this->assertEquals($expect, $this->auth_loadACL_testwrapper($acls));
421f6e92faSChristopher Smith    }
431f6e92faSChristopher Smith
441f6e92faSChristopher Smith    function test_user_substitution() {
451f6e92faSChristopher Smith        $acls = <<<ACL
461f6e92faSChristopher Smith%USER% %USER% 2
471f6e92faSChristopher SmithACL;
481f6e92faSChristopher Smith        $expect = array(
491f6e92faSChristopher Smith            "testuser\ttestuser 2",
501f6e92faSChristopher Smith        );
511f6e92faSChristopher Smith        $this->assertEquals($expect, $this->auth_loadACL_testwrapper($acls));
521f6e92faSChristopher Smith    }
531f6e92faSChristopher Smith
541f6e92faSChristopher Smith    function test_group_substitution() {
551f6e92faSChristopher Smith        $acls = <<<ACL
561f6e92faSChristopher Smith%GROUP% %GROUP% 2
571f6e92faSChristopher SmithACL;
581f6e92faSChristopher Smith        $expect = array(
591f6e92faSChristopher Smith            "foo\t@foo 2",
601f6e92faSChristopher Smith            "bar\t@bar 2",
611f6e92faSChristopher Smith        );
621f6e92faSChristopher Smith        $this->assertEquals($expect, $this->auth_loadACL_testwrapper($acls));
631f6e92faSChristopher Smith    }
641f6e92faSChristopher Smith
651f6e92faSChristopher Smith    function test_both_substitution() {
661f6e92faSChristopher Smith        $acls = <<<ACL
671f6e92faSChristopher Smith%GROUP%:%USER% %USER% 2
681f6e92faSChristopher Smith%GROUP%:%USER% %GROUP% 2
691f6e92faSChristopher SmithACL;
701f6e92faSChristopher Smith        $expect = array(
711f6e92faSChristopher Smith            "foo:testuser\ttestuser 2",
721f6e92faSChristopher Smith            "bar:testuser\ttestuser 2",
731f6e92faSChristopher Smith            "foo:testuser\t@foo 2",
741f6e92faSChristopher Smith            "bar:testuser\t@bar 2",
751f6e92faSChristopher Smith        );
761f6e92faSChristopher Smith        $this->assertEquals($expect, $this->auth_loadACL_testwrapper($acls));
771f6e92faSChristopher Smith    }
781f6e92faSChristopher Smith
791f6e92faSChristopher Smith    // put it all together - read the standard acl provided with the test suite
801f6e92faSChristopher Smith    function test_standardtestacls(){
811f6e92faSChristopher Smith        $expect = array(
821f6e92faSChristopher Smith            "*\t@ALL        8",
831f6e92faSChristopher Smith            "private:*\t@ALL        0",
841f6e92faSChristopher Smith            "users:*\t@ALL         1",
851f6e92faSChristopher Smith            "users:testuser:*\ttestuser       16",
861f6e92faSChristopher Smith            "groups:*\t@ALL         1",
871f6e92faSChristopher Smith            "groups:foo:*\t@foo      16",
881f6e92faSChristopher Smith            "groups:bar:*\t@bar      16",
891f6e92faSChristopher Smith        );
901f6e92faSChristopher Smith        $this->assertEquals($expect, auth_loadACL());
911f6e92faSChristopher Smith    }
921f6e92faSChristopher Smith
931f6e92faSChristopher Smith    // FS#2867, '\s' in php regular expressions may match non-space characters utf8 strings
941f6e92faSChristopher Smith    // this is due to locale setting on the server, which may match bytes '\xA0' and '\x85'
951f6e92faSChristopher Smith    // these two bytes are present in valid multi-byte UTF-8 characters.
961f6e92faSChristopher Smith    // this test will use one, 'ठ' (DEVANAGARI LETTER TTHA, e0 a4 a0).  There are many others.
971f6e92faSChristopher Smith    function test_FS2867() {
981f6e92faSChristopher Smith        global $USERINFO;
991f6e92faSChristopher Smith
1001f6e92faSChristopher Smith        setlocale(LC_ALL, "English_United States.1252");  // should only succeed on windows systems
1011f6e92faSChristopher Smith        setlocale(LC_ALL, "en_US.UTF-8");                 // should succeed on other systems
1021f6e92faSChristopher Smith
103*01137572SChristopher Smith        // no point continuing with this test if \s doesn't match A0
104*01137572SChristopher Smith        if (!preg_match('/\s/',"\xa0")) {
105*01137572SChristopher Smith            $this->markTestSkipped('Unable to change locale.');
106*01137572SChristopher Smith        }
107*01137572SChristopher Smith
1081f6e92faSChristopher Smith        $_SERVER['REMOTE_USER'] = 'utfठ8';
1091f6e92faSChristopher Smith        $USERINFO['grps'] = array('utfठ16','utfठa');
1101f6e92faSChristopher Smith
1111f6e92faSChristopher Smith        $acls = <<<ACL
1121f6e92faSChristopher Smith%GROUP%:%USER% %USER% 2
1131f6e92faSChristopher Smith%GROUP%:* %GROUP% 4
1141f6e92faSChristopher Smithdevangariठttha @ALL 2
1151f6e92faSChristopher SmithACL;
1161f6e92faSChristopher Smith        $expect = array(
1171f6e92faSChristopher Smith            "utfठ16:utfठ8\tutfठ8 2",
1181f6e92faSChristopher Smith            "utfठa:utfठ8\tutfठ8 2",
1191f6e92faSChristopher Smith            "utfठ16:*\t@utfठ16 4",
1201f6e92faSChristopher Smith            "utfठa:*\t@utfठa 4",
1211f6e92faSChristopher Smith            "devangariठttha\t@ALL 2",
1221f6e92faSChristopher Smith        );
1231f6e92faSChristopher Smith        $this->assertEquals($expect, $this->auth_loadACL_testwrapper($acls));
1241f6e92faSChristopher Smith    }
1251f6e92faSChristopher Smith}
1261f6e92faSChristopher Smith
1271f6e92faSChristopher Smith//Setup VIM: ex: et ts=4 :
128