xref: /dokuwiki/lib/plugins/authpdo/_test/mysql.test.php (revision df01249e8b85052013de1deba4557864723e13a5)
13f4d1534SAndreas Gohr<?php
23f4d1534SAndreas Gohr
33f4d1534SAndreas Gohr/**
43f4d1534SAndreas Gohr * MySQL tests for the authpdo plugin
53f4d1534SAndreas Gohr *
63f4d1534SAndreas Gohr * @group plugin_authpdo
73f4d1534SAndreas Gohr * @group plugins
83f4d1534SAndreas Gohr */
93f4d1534SAndreas Gohrclass mysql_plugin_authpdo_test extends DokuWikiTest {
103f4d1534SAndreas Gohr
113f4d1534SAndreas Gohr    protected $host = '';
123f4d1534SAndreas Gohr    protected $database = 'authpdo_testing';
133f4d1534SAndreas Gohr    protected $user = '';
143f4d1534SAndreas Gohr    protected $pass = '';
153f4d1534SAndreas Gohr
163f4d1534SAndreas Gohr    public function setUp() {
173f4d1534SAndreas Gohr        parent::setUp();
183f4d1534SAndreas Gohr        $configuration = DOKU_UNITTEST . 'mysql.conf.php';
193f4d1534SAndreas Gohr        if(!file_exists($configuration)) {
203f4d1534SAndreas Gohr            return;
213f4d1534SAndreas Gohr        }
223f4d1534SAndreas Gohr        /** @var $conf array */
233f4d1534SAndreas Gohr        include $configuration;
243f4d1534SAndreas Gohr        $this->host = $conf['host'];
253f4d1534SAndreas Gohr        $this->user = $conf['user'];
263f4d1534SAndreas Gohr        $this->pass = $conf['pass'];
273f4d1534SAndreas Gohr    }
283f4d1534SAndreas Gohr
293f4d1534SAndreas Gohr    /**
303f4d1534SAndreas Gohr     * Check if database credentials exist
313f4d1534SAndreas Gohr     */
323f4d1534SAndreas Gohr    public function test_requirements() {
333f4d1534SAndreas Gohr        if(!$this->host || !$this->user) {
343f4d1534SAndreas Gohr            $this->markTestSkipped("Skipped mysql tests. Missing configuration");
353f4d1534SAndreas Gohr        }
363f4d1534SAndreas Gohr    }
373f4d1534SAndreas Gohr
383f4d1534SAndreas Gohr    /**
393f4d1534SAndreas Gohr     * create the database for testing
403f4d1534SAndreas Gohr     */
413f4d1534SAndreas Gohr    protected function createDatabase() {
423f4d1534SAndreas Gohr        $pdo = new PDO(
433f4d1534SAndreas Gohr            "mysql:dbname=;host={$this->host}", $this->user, $this->pass,
443f4d1534SAndreas Gohr            array(
453f4d1534SAndreas Gohr                PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, // we want exceptions, not error codes
463f4d1534SAndreas Gohr            )
473f4d1534SAndreas Gohr        );
483f4d1534SAndreas Gohr        $pdo->exec("DROP DATABASE IF EXISTS {$this->database}");
493f4d1534SAndreas Gohr        $pdo->exec("CREATE DATABASE {$this->database}");
503f4d1534SAndreas Gohr        $pdo = null;
513f4d1534SAndreas Gohr    }
523f4d1534SAndreas Gohr
533f4d1534SAndreas Gohr    /**
543f4d1534SAndreas Gohr     * remove the database
553f4d1534SAndreas Gohr     */
563f4d1534SAndreas Gohr    protected function dropDatabase() {
573f4d1534SAndreas Gohr        $pdo = new PDO(
583f4d1534SAndreas Gohr            "mysql:dbname={$this->database};host={$this->host}", $this->user, $this->pass,
593f4d1534SAndreas Gohr            array(
603f4d1534SAndreas Gohr                PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, // we want exceptions, not error codes
613f4d1534SAndreas Gohr            )
623f4d1534SAndreas Gohr        );
633f4d1534SAndreas Gohr        $pdo->exec("DROP DATABASE IF EXISTS {$this->database}");
643f4d1534SAndreas Gohr        $pdo = null;
653f4d1534SAndreas Gohr    }
663f4d1534SAndreas Gohr
673f4d1534SAndreas Gohr    /**
683f4d1534SAndreas Gohr     * imports a database dump
693f4d1534SAndreas Gohr     *
703f4d1534SAndreas Gohr     * @param $file
713f4d1534SAndreas Gohr     */
723f4d1534SAndreas Gohr    protected function importDatabase($file) {
733f4d1534SAndreas Gohr        // connect to database and import dump
743f4d1534SAndreas Gohr        $pdo = null;
753f4d1534SAndreas Gohr        $pdo = new PDO(
763f4d1534SAndreas Gohr            "mysql:dbname={$this->database};host={$this->host}", $this->user, $this->pass,
773f4d1534SAndreas Gohr            array(
783f4d1534SAndreas Gohr                PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, // we want exceptions, not error codes
793f4d1534SAndreas Gohr            )
803f4d1534SAndreas Gohr        );
813f4d1534SAndreas Gohr        $sql = file_get_contents($file);
823f4d1534SAndreas Gohr        $pdo->exec($sql);
833f4d1534SAndreas Gohr        $pdo = null;
843f4d1534SAndreas Gohr    }
853f4d1534SAndreas Gohr
863f4d1534SAndreas Gohr    /**
87964d95c6SAndreas Gohr     * Run general tests on all users
88964d95c6SAndreas Gohr     *
89964d95c6SAndreas Gohr     * @param auth_plugin_authpdo $auth
90964d95c6SAndreas Gohr     * @param array $users
91964d95c6SAndreas Gohr     */
92964d95c6SAndreas Gohr    protected function runGeneralTests(auth_plugin_authpdo $auth, $users) {
93*df01249eSAndreas Gohr        global $conf;
94*df01249eSAndreas Gohr        $info = 'DSN: ' . $auth->getConf('dsn');
95*df01249eSAndreas Gohr        $this->assertTrue($auth->success, $info);
96964d95c6SAndreas Gohr
97964d95c6SAndreas Gohr        if($auth->canDo('getUsers')) {
98964d95c6SAndreas Gohr            $list = $auth->retrieveUsers();
99*df01249eSAndreas Gohr            $this->assertGreaterThanOrEqual(count($users), count($list), $info);
100*df01249eSAndreas Gohr        }
101*df01249eSAndreas Gohr
102*df01249eSAndreas Gohr        if($auth->canDo('getGroups')) {
103*df01249eSAndreas Gohr            $list = $auth->retrieveGroups();
104*df01249eSAndreas Gohr            $this->assertGreaterThanOrEqual(1, $list, $info);
105964d95c6SAndreas Gohr        }
106964d95c6SAndreas Gohr
107964d95c6SAndreas Gohr        if($auth->canDo('getUserCount')) {
108964d95c6SAndreas Gohr            $count = $auth->getUserCount();
109964d95c6SAndreas Gohr            $this->assertGreaterThanOrEqual(count($users), $count);
110964d95c6SAndreas Gohr        }
111*df01249eSAndreas Gohr
112*df01249eSAndreas Gohr        if($auth->canDo('addUser')) {
113*df01249eSAndreas Gohr            $newuser = array(
114*df01249eSAndreas Gohr                'user' => 'newuserfoobar',
115*df01249eSAndreas Gohr                'name' => 'First LastFoobar',
116*df01249eSAndreas Gohr                'pass' => 'password',
117*df01249eSAndreas Gohr                'mail' => 'newuserfoobar@example.com',
118*df01249eSAndreas Gohr                'grps' => array('acompletelynewgroup')
119*df01249eSAndreas Gohr            );
120*df01249eSAndreas Gohr            $ok = $auth->createUser(
121*df01249eSAndreas Gohr                $newuser['user'],
122*df01249eSAndreas Gohr                $newuser['pass'],
123*df01249eSAndreas Gohr                $newuser['name'],
124*df01249eSAndreas Gohr                $newuser['mail'],
125*df01249eSAndreas Gohr                $newuser['grps']
126*df01249eSAndreas Gohr            );
127*df01249eSAndreas Gohr            $this->assertTrue($ok, $info);
128*df01249eSAndreas Gohr            $check = $auth->getUserData($newuser['user']);
129*df01249eSAndreas Gohr            $this->assertEquals($newuser['user'], $check['user'], $info);
130*df01249eSAndreas Gohr            $this->assertEquals($newuser['mail'], $check['mail'], $info);
131*df01249eSAndreas Gohr            $groups = array_merge($newuser['grps'], array($conf['defaultgroup']));
132*df01249eSAndreas Gohr            $this->assertEquals($groups, $check['grps'], $info);
133*df01249eSAndreas Gohr        }
134964d95c6SAndreas Gohr    }
135964d95c6SAndreas Gohr
136964d95c6SAndreas Gohr    /**
1373f4d1534SAndreas Gohr     * run all the tests with the given user, depending on the capabilities
1383f4d1534SAndreas Gohr     *
1393f4d1534SAndreas Gohr     * @param auth_plugin_authpdo $auth
1403f4d1534SAndreas Gohr     * @param $user
1413f4d1534SAndreas Gohr     */
142964d95c6SAndreas Gohr    protected function runUserTests(auth_plugin_authpdo $auth, $user) {
1433f4d1534SAndreas Gohr        global $conf;
144*df01249eSAndreas Gohr        $info = 'DSN: ' . $auth->getConf('dsn') . ' User:' . $user['user'];
1453f4d1534SAndreas Gohr
1463f4d1534SAndreas Gohr        // minimal setup
1473f4d1534SAndreas Gohr        $this->assertTrue($auth->checkPass($user['user'], $user['pass']), $info);
1483f4d1534SAndreas Gohr        $check = $auth->getUserData($user['user']);
1493f4d1534SAndreas Gohr        $this->assertEquals($user['user'], $check['user'], $info);
1503f4d1534SAndreas Gohr        $this->assertEquals($user['name'], $check['name'], $info);
1513f4d1534SAndreas Gohr        $this->assertEquals($user['mail'], $check['mail'], $info);
1523f4d1534SAndreas Gohr        $groups = array_merge($user['grps'], array($conf['defaultgroup']));
1533f4d1534SAndreas Gohr        $this->assertEquals($groups, $check['grps'], $info);
1543f4d1534SAndreas Gohr
155964d95c6SAndreas Gohr        // getUsers
156964d95c6SAndreas Gohr        if($auth->canDo('getUsers')) {
157964d95c6SAndreas Gohr            $list = $auth->retrieveUsers(0, -1, array('user' => $user['user']));
158964d95c6SAndreas Gohr            $this->assertGreaterThanOrEqual(1, count($list));
159964d95c6SAndreas Gohr            $list = $auth->retrieveUsers(0, -1, array('name' => $user['name']));
160964d95c6SAndreas Gohr            $this->assertGreaterThanOrEqual(1, count($list));
161964d95c6SAndreas Gohr            $list = $auth->retrieveUsers(0, -1, array('mail' => $user['mail']));
162964d95c6SAndreas Gohr            $this->assertGreaterThanOrEqual(1, count($list));
163964d95c6SAndreas Gohr        }
164964d95c6SAndreas Gohr
165964d95c6SAndreas Gohr        // getUserCount
166964d95c6SAndreas Gohr        if($auth->canDo('getUserCount')) {
167964d95c6SAndreas Gohr            $count = $auth->getUserCount(array('user' => $user['user']));
168964d95c6SAndreas Gohr            $this->assertGreaterThanOrEqual(1, $count);
169964d95c6SAndreas Gohr            $count = $auth->getUserCount(array('name' => $user['name']));
170964d95c6SAndreas Gohr            $this->assertGreaterThanOrEqual(1, $count);
171964d95c6SAndreas Gohr            $count = $auth->getUserCount(array('mail' => $user['mail']));
172964d95c6SAndreas Gohr            $this->assertGreaterThanOrEqual(1, $count);
173964d95c6SAndreas Gohr        }
174964d95c6SAndreas Gohr
175*df01249eSAndreas Gohr        // modGroups
176*df01249eSAndreas Gohr        if($auth->canDo('modGroups')) {
177*df01249eSAndreas Gohr            $newgroup = 'foobar';
178*df01249eSAndreas Gohr            $ok = $auth->modifyUser($user['user'], array('grps' => array($newgroup)));
179*df01249eSAndreas Gohr            $this->assertTrue($ok, $info);
180*df01249eSAndreas Gohr            $check = $auth->getUserData($user['user']);
181*df01249eSAndreas Gohr            $this->assertTrue(in_array($newgroup, $check['grps']), $info);
182*df01249eSAndreas Gohr        }
183*df01249eSAndreas Gohr
1843f4d1534SAndreas Gohr        // modPass
1853f4d1534SAndreas Gohr        if($auth->canDo('modPass')) {
1863f4d1534SAndreas Gohr            $newpass = 'foobar';
1873f4d1534SAndreas Gohr            $ok = $auth->modifyUser($user['user'], array('pass' => $newpass));
1883f4d1534SAndreas Gohr            $this->assertTrue($ok, $info);
1893f4d1534SAndreas Gohr            $this->assertTrue($auth->checkPass($user['user'], $newpass), $info);
1903f4d1534SAndreas Gohr        }
1913f4d1534SAndreas Gohr
1923f4d1534SAndreas Gohr        // modMail
1933f4d1534SAndreas Gohr        if($auth->canDo('modMail')) {
1943f4d1534SAndreas Gohr            $newmail = 'foobar@example.com';
1953f4d1534SAndreas Gohr            $ok = $auth->modifyUser($user['user'], array('mail' => $newmail));
1963f4d1534SAndreas Gohr            $this->assertTrue($ok, $info);
1973f4d1534SAndreas Gohr            $check = $auth->getUserData($user['user']);
1983f4d1534SAndreas Gohr            $this->assertEquals($newmail, $check['mail'], $info);
1993f4d1534SAndreas Gohr        }
2003f4d1534SAndreas Gohr
2013f4d1534SAndreas Gohr        // modName
2023f4d1534SAndreas Gohr        if($auth->canDo('modName')) {
2033f4d1534SAndreas Gohr            $newname = 'FirstName Foobar';
2043f4d1534SAndreas Gohr            $ok = $auth->modifyUser($user['user'], array('name' => $newname));
2053f4d1534SAndreas Gohr            $this->assertTrue($ok, $info);
2063f4d1534SAndreas Gohr            $check = $auth->getUserData($user['user']);
2073f4d1534SAndreas Gohr            $this->assertEquals($newname, $check['name'], $info);
2083f4d1534SAndreas Gohr        }
2093f4d1534SAndreas Gohr
2103f4d1534SAndreas Gohr        // modLogin
2113f4d1534SAndreas Gohr        if($auth->canDo('modLogin')) {
2123f4d1534SAndreas Gohr            $newuser = 'foobar' . $user['user'];
2133f4d1534SAndreas Gohr            $ok = $auth->modifyUser($user['user'], array('user' => $newuser));
2143f4d1534SAndreas Gohr            $this->assertTrue($ok, $info);
2153f4d1534SAndreas Gohr            $check = $auth->getUserData($newuser);
2163f4d1534SAndreas Gohr            $this->assertEquals($newuser, $check['user'], $info);
217*df01249eSAndreas Gohr            // rename back
218*df01249eSAndreas Gohr            $ok = $auth->modifyUser($newuser, array('user' => $user['user']));
219*df01249eSAndreas Gohr            $this->assertTrue($ok, $info);
2203f4d1534SAndreas Gohr        }
2213f4d1534SAndreas Gohr
222*df01249eSAndreas Gohr        // delUser
223*df01249eSAndreas Gohr        if($auth->canDo('delUser')) {
224*df01249eSAndreas Gohr            $num = $auth->deleteUsers(array($user['user']));
225*df01249eSAndreas Gohr            $this->assertEquals(1, $num, $info);
226*df01249eSAndreas Gohr            $this->assertFalse($auth->getUserData($user['user']), $info);
227*df01249eSAndreas Gohr        }
2283f4d1534SAndreas Gohr    }
2293f4d1534SAndreas Gohr
2303f4d1534SAndreas Gohr    /**
2313f4d1534SAndreas Gohr     * This triggers all the tests based on the dumps and configurations
2323f4d1534SAndreas Gohr     *
2333f4d1534SAndreas Gohr     * @depends test_requirements
2343f4d1534SAndreas Gohr     */
2353f4d1534SAndreas Gohr    public function test_mysql() {
2363f4d1534SAndreas Gohr        global $conf;
2373f4d1534SAndreas Gohr
2383f4d1534SAndreas Gohr        $files = glob(__DIR__ . '/mysql/*.php');
2393f4d1534SAndreas Gohr        foreach($files as $file) {
2403f4d1534SAndreas Gohr            $dump = preg_replace('/\.php$/', '.sql', $file);
241*df01249eSAndreas Gohr            $this->database = 'authpdo_testing_' . basename($file, '.php');
2423f4d1534SAndreas Gohr
2433f4d1534SAndreas Gohr            $this->createDatabase();
2443f4d1534SAndreas Gohr            $this->importDatabase($dump);
2453f4d1534SAndreas Gohr
2463f4d1534SAndreas Gohr            // Setup the configuration and initialize a new auth object
2473f4d1534SAndreas Gohr            /** @var $data array */
2483f4d1534SAndreas Gohr            include $file;
2493f4d1534SAndreas Gohr
2503f4d1534SAndreas Gohr            $conf['plugin']['authpdo'] = array();
2513f4d1534SAndreas Gohr            $conf['plugin']['authpdo'] = $data['conf'];
2523f4d1534SAndreas Gohr            $conf['plugin']['authpdo']['dsn'] = "mysql:dbname={$this->database};host={$this->host}";
2533f4d1534SAndreas Gohr            $conf['plugin']['authpdo']['user'] = $this->user;
2543f4d1534SAndreas Gohr            $conf['plugin']['authpdo']['pass'] = $this->pass;
2553f4d1534SAndreas Gohr            $conf['plugin']['authpdo']['debug'] = 1;
2563f4d1534SAndreas Gohr            if($data['passcrypt']) $conf['passcrypt'] = $data['passcrypt'];
2573f4d1534SAndreas Gohr            $auth = new auth_plugin_authpdo();
2583f4d1534SAndreas Gohr
259964d95c6SAndreas Gohr            $this->runGeneralTests($auth, $data['users']);
2603f4d1534SAndreas Gohr            foreach($data['users'] as $user) {
261964d95c6SAndreas Gohr                $this->runUserTests($auth, $user);
2623f4d1534SAndreas Gohr            }
2633f4d1534SAndreas Gohr
2643f4d1534SAndreas Gohr            $this->dropDatabase();
2653f4d1534SAndreas Gohr        }
2663f4d1534SAndreas Gohr    }
2673f4d1534SAndreas Gohr
2683f4d1534SAndreas Gohr}
269