xref: /dokuwiki/lib/plugins/authpdo/_test/mysql.test.php (revision 1c33cec37215d0c964cf961bdbc49ae7db3657e6)
13f4d1534SAndreas Gohr<?php
23f4d1534SAndreas Gohr
33f4d1534SAndreas Gohr/**
432a211c7SAndreas 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
1132a211c7SAndreas Gohr    protected $driver = 'mysql';
123f4d1534SAndreas Gohr    protected $host = '';
133f4d1534SAndreas Gohr    protected $database = 'authpdo_testing';
143f4d1534SAndreas Gohr    protected $user = '';
153f4d1534SAndreas Gohr    protected $pass = '';
1632a211c7SAndreas Gohr    protected $port = '';
173f4d1534SAndreas Gohr
18*1c33cec3SAndreas Gohr    public function setUp() : void {
193f4d1534SAndreas Gohr        parent::setUp();
2032a211c7SAndreas Gohr        $configuration = DOKU_UNITTEST . "{$this->driver}.conf.php";
213f4d1534SAndreas Gohr        if(!file_exists($configuration)) {
223f4d1534SAndreas Gohr            return;
233f4d1534SAndreas Gohr        }
243f4d1534SAndreas Gohr        /** @var $conf array */
253f4d1534SAndreas Gohr        include $configuration;
263f4d1534SAndreas Gohr        $this->host = $conf['host'];
273f4d1534SAndreas Gohr        $this->user = $conf['user'];
283f4d1534SAndreas Gohr        $this->pass = $conf['pass'];
2932a211c7SAndreas Gohr        $this->port = $conf['port'];
303f4d1534SAndreas Gohr    }
313f4d1534SAndreas Gohr
323f4d1534SAndreas Gohr    /**
3332a211c7SAndreas Gohr     * try to remove the last set up database
3432a211c7SAndreas Gohr     *
3532a211c7SAndreas Gohr     * it might still be there if something went wrong
3632a211c7SAndreas Gohr     */
37*1c33cec3SAndreas Gohr    public function tearDown() : void {
3832a211c7SAndreas Gohr        parent::tearDown();
3932a211c7SAndreas Gohr        $this->dropDatabase();
4032a211c7SAndreas Gohr    }
4132a211c7SAndreas Gohr
4232a211c7SAndreas Gohr    /**
4332a211c7SAndreas Gohr     * Check if database credentials and extensions exist
443f4d1534SAndreas Gohr     */
453f4d1534SAndreas Gohr    public function test_requirements() {
463f4d1534SAndreas Gohr        if(!$this->host || !$this->user) {
4732a211c7SAndreas Gohr            $this->markTestSkipped("Skipped {$this->driver} tests. Missing configuration");
4832a211c7SAndreas Gohr        }
4932a211c7SAndreas Gohr        if(!class_exists('PDO')) {
5032a211c7SAndreas Gohr            $this->markTestSkipped("Skipped {$this->driver} tests. Missing PDO extension");
5132a211c7SAndreas Gohr        }
5232a211c7SAndreas Gohr        if(!in_array($this->driver, pdo_drivers())) {
5332a211c7SAndreas Gohr            $this->markTestSkipped("Skipped {$this->driver} tests. Missing pdo_{$this->driver} extension");
543f4d1534SAndreas Gohr        }
551bff2abaSAndreas Gohr        $this->assertTrue(true); // avoid being marked as risky for having no assertion
563f4d1534SAndreas Gohr    }
573f4d1534SAndreas Gohr
583f4d1534SAndreas Gohr    /**
593f4d1534SAndreas Gohr     * create the database for testing
603f4d1534SAndreas Gohr     */
613f4d1534SAndreas Gohr    protected function createDatabase() {
623f4d1534SAndreas Gohr        $pdo = new PDO(
6332a211c7SAndreas Gohr            "{$this->driver}:host={$this->host};port={$this->port}", $this->user, $this->pass,
643f4d1534SAndreas Gohr            array(
653f4d1534SAndreas Gohr                PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, // we want exceptions, not error codes
663f4d1534SAndreas Gohr            )
673f4d1534SAndreas Gohr        );
683f4d1534SAndreas Gohr        $pdo->exec("DROP DATABASE IF EXISTS {$this->database}");
693f4d1534SAndreas Gohr        $pdo->exec("CREATE DATABASE {$this->database}");
703f4d1534SAndreas Gohr        $pdo = null;
713f4d1534SAndreas Gohr    }
723f4d1534SAndreas Gohr
733f4d1534SAndreas Gohr    /**
743f4d1534SAndreas Gohr     * remove the database
753f4d1534SAndreas Gohr     */
763f4d1534SAndreas Gohr    protected function dropDatabase() {
773f4d1534SAndreas Gohr        $pdo = new PDO(
7832a211c7SAndreas Gohr            "{$this->driver}:host={$this->host};port={$this->port}", $this->user, $this->pass,
793f4d1534SAndreas Gohr            array(
803f4d1534SAndreas Gohr                PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, // we want exceptions, not error codes
813f4d1534SAndreas Gohr            )
823f4d1534SAndreas Gohr        );
8332a211c7SAndreas Gohr        try {
843f4d1534SAndreas Gohr            $pdo->exec("DROP DATABASE IF EXISTS {$this->database}");
8532a211c7SAndreas Gohr        } catch (PDOException $e) {
8632a211c7SAndreas Gohr            // ignore - sometimes this fails even though the database was deleted
8732a211c7SAndreas Gohr        }
883f4d1534SAndreas Gohr        $pdo = null;
893f4d1534SAndreas Gohr    }
903f4d1534SAndreas Gohr
913f4d1534SAndreas Gohr    /**
923f4d1534SAndreas Gohr     * imports a database dump
933f4d1534SAndreas Gohr     *
943f4d1534SAndreas Gohr     * @param $file
953f4d1534SAndreas Gohr     */
963f4d1534SAndreas Gohr    protected function importDatabase($file) {
973f4d1534SAndreas Gohr        // connect to database and import dump
983f4d1534SAndreas Gohr        $pdo = null;
993f4d1534SAndreas Gohr        $pdo = new PDO(
10032a211c7SAndreas Gohr            "{$this->driver}:dbname={$this->database};host={$this->host};port={$this->port}", $this->user, $this->pass,
1013f4d1534SAndreas Gohr            array(
1023f4d1534SAndreas Gohr                PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, // we want exceptions, not error codes
1033f4d1534SAndreas Gohr            )
1043f4d1534SAndreas Gohr        );
1053f4d1534SAndreas Gohr        $sql = file_get_contents($file);
1063f4d1534SAndreas Gohr        $pdo->exec($sql);
1073f4d1534SAndreas Gohr        $pdo = null;
1083f4d1534SAndreas Gohr    }
1093f4d1534SAndreas Gohr
1103f4d1534SAndreas Gohr    /**
111964d95c6SAndreas Gohr     * Run general tests on all users
112964d95c6SAndreas Gohr     *
113964d95c6SAndreas Gohr     * @param auth_plugin_authpdo $auth
114964d95c6SAndreas Gohr     * @param array $users
115964d95c6SAndreas Gohr     */
116964d95c6SAndreas Gohr    protected function runGeneralTests(auth_plugin_authpdo $auth, $users) {
117df01249eSAndreas Gohr        global $conf;
118df01249eSAndreas Gohr        $info = 'DSN: ' . $auth->getConf('dsn');
119df01249eSAndreas Gohr        $this->assertTrue($auth->success, $info);
120964d95c6SAndreas Gohr
121964d95c6SAndreas Gohr        if($auth->canDo('getUsers')) {
122964d95c6SAndreas Gohr            $list = $auth->retrieveUsers();
123df01249eSAndreas Gohr            $this->assertGreaterThanOrEqual(count($users), count($list), $info);
124df01249eSAndreas Gohr        }
125df01249eSAndreas Gohr
126df01249eSAndreas Gohr        if($auth->canDo('getGroups')) {
127df01249eSAndreas Gohr            $list = $auth->retrieveGroups();
128df01249eSAndreas Gohr            $this->assertGreaterThanOrEqual(1, $list, $info);
129964d95c6SAndreas Gohr        }
130964d95c6SAndreas Gohr
131964d95c6SAndreas Gohr        if($auth->canDo('getUserCount')) {
132964d95c6SAndreas Gohr            $count = $auth->getUserCount();
133964d95c6SAndreas Gohr            $this->assertGreaterThanOrEqual(count($users), $count);
134964d95c6SAndreas Gohr        }
135df01249eSAndreas Gohr
136df01249eSAndreas Gohr        if($auth->canDo('addUser')) {
137df01249eSAndreas Gohr            $newuser = array(
138df01249eSAndreas Gohr                'user' => 'newuserfoobar',
139df01249eSAndreas Gohr                'name' => 'First LastFoobar',
140df01249eSAndreas Gohr                'pass' => 'password',
141df01249eSAndreas Gohr                'mail' => 'newuserfoobar@example.com',
142df01249eSAndreas Gohr                'grps' => array('acompletelynewgroup')
143df01249eSAndreas Gohr            );
144df01249eSAndreas Gohr            $ok = $auth->createUser(
145df01249eSAndreas Gohr                $newuser['user'],
146df01249eSAndreas Gohr                $newuser['pass'],
147df01249eSAndreas Gohr                $newuser['name'],
148df01249eSAndreas Gohr                $newuser['mail'],
149df01249eSAndreas Gohr                $newuser['grps']
150df01249eSAndreas Gohr            );
151df01249eSAndreas Gohr            $this->assertTrue($ok, $info);
152df01249eSAndreas Gohr            $check = $auth->getUserData($newuser['user']);
153df01249eSAndreas Gohr            $this->assertEquals($newuser['user'], $check['user'], $info);
154df01249eSAndreas Gohr            $this->assertEquals($newuser['mail'], $check['mail'], $info);
155df01249eSAndreas Gohr            $groups = array_merge($newuser['grps'], array($conf['defaultgroup']));
156df01249eSAndreas Gohr            $this->assertEquals($groups, $check['grps'], $info);
157df01249eSAndreas Gohr        }
158964d95c6SAndreas Gohr    }
159964d95c6SAndreas Gohr
160964d95c6SAndreas Gohr    /**
1613f4d1534SAndreas Gohr     * run all the tests with the given user, depending on the capabilities
1623f4d1534SAndreas Gohr     *
1633f4d1534SAndreas Gohr     * @param auth_plugin_authpdo $auth
1643f4d1534SAndreas Gohr     * @param $user
1653f4d1534SAndreas Gohr     */
166964d95c6SAndreas Gohr    protected function runUserTests(auth_plugin_authpdo $auth, $user) {
1673f4d1534SAndreas Gohr        global $conf;
168df01249eSAndreas Gohr        $info = 'DSN: ' . $auth->getConf('dsn') . ' User:' . $user['user'];
1693f4d1534SAndreas Gohr
1703f4d1534SAndreas Gohr        // minimal setup
1713f4d1534SAndreas Gohr        $this->assertTrue($auth->checkPass($user['user'], $user['pass']), $info);
1723f4d1534SAndreas Gohr        $check = $auth->getUserData($user['user']);
1733f4d1534SAndreas Gohr        $this->assertEquals($user['user'], $check['user'], $info);
1743f4d1534SAndreas Gohr        $this->assertEquals($user['name'], $check['name'], $info);
1753f4d1534SAndreas Gohr        $this->assertEquals($user['mail'], $check['mail'], $info);
1763f4d1534SAndreas Gohr        $groups = array_merge($user['grps'], array($conf['defaultgroup']));
1773f4d1534SAndreas Gohr        $this->assertEquals($groups, $check['grps'], $info);
1783f4d1534SAndreas Gohr
179964d95c6SAndreas Gohr        // getUsers
180964d95c6SAndreas Gohr        if($auth->canDo('getUsers')) {
181964d95c6SAndreas Gohr            $list = $auth->retrieveUsers(0, -1, array('user' => $user['user']));
182964d95c6SAndreas Gohr            $this->assertGreaterThanOrEqual(1, count($list));
183964d95c6SAndreas Gohr            $list = $auth->retrieveUsers(0, -1, array('name' => $user['name']));
184964d95c6SAndreas Gohr            $this->assertGreaterThanOrEqual(1, count($list));
185964d95c6SAndreas Gohr            $list = $auth->retrieveUsers(0, -1, array('mail' => $user['mail']));
186964d95c6SAndreas Gohr            $this->assertGreaterThanOrEqual(1, count($list));
187964d95c6SAndreas Gohr        }
188964d95c6SAndreas Gohr
189964d95c6SAndreas Gohr        // getUserCount
190964d95c6SAndreas Gohr        if($auth->canDo('getUserCount')) {
191964d95c6SAndreas Gohr            $count = $auth->getUserCount(array('user' => $user['user']));
192964d95c6SAndreas Gohr            $this->assertGreaterThanOrEqual(1, $count);
193964d95c6SAndreas Gohr            $count = $auth->getUserCount(array('name' => $user['name']));
194964d95c6SAndreas Gohr            $this->assertGreaterThanOrEqual(1, $count);
195964d95c6SAndreas Gohr            $count = $auth->getUserCount(array('mail' => $user['mail']));
196964d95c6SAndreas Gohr            $this->assertGreaterThanOrEqual(1, $count);
197964d95c6SAndreas Gohr        }
198964d95c6SAndreas Gohr
199df01249eSAndreas Gohr        // modGroups
200df01249eSAndreas Gohr        if($auth->canDo('modGroups')) {
201df01249eSAndreas Gohr            $newgroup = 'foobar';
202df01249eSAndreas Gohr            $ok = $auth->modifyUser($user['user'], array('grps' => array($newgroup)));
203df01249eSAndreas Gohr            $this->assertTrue($ok, $info);
204df01249eSAndreas Gohr            $check = $auth->getUserData($user['user']);
205df01249eSAndreas Gohr            $this->assertTrue(in_array($newgroup, $check['grps']), $info);
206df01249eSAndreas Gohr        }
207df01249eSAndreas Gohr
2083f4d1534SAndreas Gohr        // modPass
2093f4d1534SAndreas Gohr        if($auth->canDo('modPass')) {
2103f4d1534SAndreas Gohr            $newpass = 'foobar';
2113f4d1534SAndreas Gohr            $ok = $auth->modifyUser($user['user'], array('pass' => $newpass));
2123f4d1534SAndreas Gohr            $this->assertTrue($ok, $info);
2133f4d1534SAndreas Gohr            $this->assertTrue($auth->checkPass($user['user'], $newpass), $info);
2143f4d1534SAndreas Gohr        }
2153f4d1534SAndreas Gohr
2163f4d1534SAndreas Gohr        // modMail
2173f4d1534SAndreas Gohr        if($auth->canDo('modMail')) {
2183f4d1534SAndreas Gohr            $newmail = 'foobar@example.com';
2193f4d1534SAndreas Gohr            $ok = $auth->modifyUser($user['user'], array('mail' => $newmail));
2203f4d1534SAndreas Gohr            $this->assertTrue($ok, $info);
2213f4d1534SAndreas Gohr            $check = $auth->getUserData($user['user']);
2223f4d1534SAndreas Gohr            $this->assertEquals($newmail, $check['mail'], $info);
2233f4d1534SAndreas Gohr        }
2243f4d1534SAndreas Gohr
2253f4d1534SAndreas Gohr        // modName
2263f4d1534SAndreas Gohr        if($auth->canDo('modName')) {
2273f4d1534SAndreas Gohr            $newname = 'FirstName Foobar';
2283f4d1534SAndreas Gohr            $ok = $auth->modifyUser($user['user'], array('name' => $newname));
2293f4d1534SAndreas Gohr            $this->assertTrue($ok, $info);
2303f4d1534SAndreas Gohr            $check = $auth->getUserData($user['user']);
2313f4d1534SAndreas Gohr            $this->assertEquals($newname, $check['name'], $info);
2323f4d1534SAndreas Gohr        }
2333f4d1534SAndreas Gohr
2343f4d1534SAndreas Gohr        // modLogin
2353f4d1534SAndreas Gohr        if($auth->canDo('modLogin')) {
2363f4d1534SAndreas Gohr            $newuser = 'foobar' . $user['user'];
2373f4d1534SAndreas Gohr            $ok = $auth->modifyUser($user['user'], array('user' => $newuser));
2383f4d1534SAndreas Gohr            $this->assertTrue($ok, $info);
2393f4d1534SAndreas Gohr            $check = $auth->getUserData($newuser);
2403f4d1534SAndreas Gohr            $this->assertEquals($newuser, $check['user'], $info);
241df01249eSAndreas Gohr            // rename back
242df01249eSAndreas Gohr            $ok = $auth->modifyUser($newuser, array('user' => $user['user']));
243df01249eSAndreas Gohr            $this->assertTrue($ok, $info);
2443f4d1534SAndreas Gohr        }
2453f4d1534SAndreas Gohr
246df01249eSAndreas Gohr        // delUser
247df01249eSAndreas Gohr        if($auth->canDo('delUser')) {
248df01249eSAndreas Gohr            $num = $auth->deleteUsers(array($user['user']));
249df01249eSAndreas Gohr            $this->assertEquals(1, $num, $info);
250df01249eSAndreas Gohr            $this->assertFalse($auth->getUserData($user['user']), $info);
251df01249eSAndreas Gohr        }
2523f4d1534SAndreas Gohr    }
2533f4d1534SAndreas Gohr
2543f4d1534SAndreas Gohr    /**
255c3f4c777SAndreas Gohr     * prepares the individual configurations for testing
2563f4d1534SAndreas Gohr     *
257c3f4c777SAndreas Gohr     * @return array
2583f4d1534SAndreas Gohr     */
259c3f4c777SAndreas Gohr    public function data_provider() {
260c3f4c777SAndreas Gohr        $testdata = array();
2613f4d1534SAndreas Gohr
26232a211c7SAndreas Gohr        $files = glob(__DIR__ . "/{$this->driver}/*.php");
2633f4d1534SAndreas Gohr        foreach($files as $file) {
2643f4d1534SAndreas Gohr            $dump = preg_replace('/\.php$/', '.sql', $file);
265c3f4c777SAndreas Gohr            $dbname = 'authpdo_testing_' . basename($file, '.php');
266c3f4c777SAndreas Gohr
267c3f4c777SAndreas Gohr            /** @var $data array */
268c3f4c777SAndreas Gohr            include $file;
269c3f4c777SAndreas Gohr
270c3f4c777SAndreas Gohr            $testdata[] = array($dbname, $dump, $data);
271c3f4c777SAndreas Gohr        }
272c3f4c777SAndreas Gohr
273c3f4c777SAndreas Gohr        return $testdata;
274c3f4c777SAndreas Gohr    }
275c3f4c777SAndreas Gohr
276c3f4c777SAndreas Gohr    /**
277c3f4c777SAndreas Gohr     * This triggers all the tests based on the dumps and configurations
278c3f4c777SAndreas Gohr     *
279c3f4c777SAndreas Gohr     * @dataProvider data_provider
280c3f4c777SAndreas Gohr     * @depends      test_requirements
281c3f4c777SAndreas Gohr     * @param string $dbname Name of the database to use
282c3f4c777SAndreas Gohr     * @param string $dump The path to the dump file to import
283c3f4c777SAndreas Gohr     * @param array|string $data config and test user setup. When a string is passed, test is skipped with that msg
284c3f4c777SAndreas Gohr     */
285c3f4c777SAndreas Gohr    public function test_database($dbname, $dump, $data){
286c3f4c777SAndreas Gohr        global $conf;
287c3f4c777SAndreas Gohr
288c3f4c777SAndreas Gohr        if(!is_array($data)) {
289c3f4c777SAndreas Gohr            $this->markTestSkipped($data);
290c3f4c777SAndreas Gohr            return;
291c3f4c777SAndreas Gohr        }
292c3f4c777SAndreas Gohr
293c3f4c777SAndreas Gohr        $this->database = $dbname;
2943f4d1534SAndreas Gohr
2953f4d1534SAndreas Gohr        $this->createDatabase();
2963f4d1534SAndreas Gohr        $this->importDatabase($dump);
2973f4d1534SAndreas Gohr
2983f4d1534SAndreas Gohr        // Setup the configuration and initialize a new auth object
2993f4d1534SAndreas Gohr        $conf['plugin']['authpdo'] = array();
3003f4d1534SAndreas Gohr        $conf['plugin']['authpdo'] = $data['conf'];
30132a211c7SAndreas Gohr        $conf['plugin']['authpdo']['dsn'] = "{$this->driver}:dbname={$this->database};host={$this->host};port={$this->port}";
3023f4d1534SAndreas Gohr        $conf['plugin']['authpdo']['user'] = $this->user;
3033f4d1534SAndreas Gohr        $conf['plugin']['authpdo']['pass'] = $this->pass;
3043f4d1534SAndreas Gohr        $conf['plugin']['authpdo']['debug'] = 1;
3053f4d1534SAndreas Gohr        if($data['passcrypt']) $conf['passcrypt'] = $data['passcrypt'];
3063f4d1534SAndreas Gohr        $auth = new auth_plugin_authpdo();
3073f4d1534SAndreas Gohr
308964d95c6SAndreas Gohr        $this->runGeneralTests($auth, $data['users']);
3093f4d1534SAndreas Gohr        foreach($data['users'] as $user) {
310964d95c6SAndreas Gohr            $this->runUserTests($auth, $user);
3113f4d1534SAndreas Gohr        }
3123f4d1534SAndreas Gohr
3133f4d1534SAndreas Gohr        $this->dropDatabase();
3143f4d1534SAndreas Gohr    }
3153f4d1534SAndreas Gohr
3163f4d1534SAndreas Gohr}
317