xref: /dokuwiki/lib/plugins/authpdo/_test/mysql.test.php (revision 964d95c6130dddb157480af3e30604388849f24f)
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    /**
87*964d95c6SAndreas Gohr     * Run general tests on all users
88*964d95c6SAndreas Gohr     *
89*964d95c6SAndreas Gohr     * @param auth_plugin_authpdo $auth
90*964d95c6SAndreas Gohr     * @param array $users
91*964d95c6SAndreas Gohr     */
92*964d95c6SAndreas Gohr    protected function runGeneralTests(auth_plugin_authpdo $auth, $users) {
93*964d95c6SAndreas Gohr        $this->assertTrue($auth->success, 'intialize auth');
94*964d95c6SAndreas Gohr
95*964d95c6SAndreas Gohr        if($auth->canDo('getUsers')) {
96*964d95c6SAndreas Gohr            $list = $auth->retrieveUsers();
97*964d95c6SAndreas Gohr            $this->assertGreaterThanOrEqual(count($users), count($list));
98*964d95c6SAndreas Gohr        }
99*964d95c6SAndreas Gohr
100*964d95c6SAndreas Gohr        if($auth->canDo('getUserCount')) {
101*964d95c6SAndreas Gohr            $count = $auth->getUserCount();
102*964d95c6SAndreas Gohr            $this->assertGreaterThanOrEqual(count($users), $count);
103*964d95c6SAndreas Gohr        }
104*964d95c6SAndreas Gohr    }
105*964d95c6SAndreas Gohr
106*964d95c6SAndreas Gohr    /**
1073f4d1534SAndreas Gohr     * run all the tests with the given user, depending on the capabilities
1083f4d1534SAndreas Gohr     *
1093f4d1534SAndreas Gohr     * @param auth_plugin_authpdo $auth
1103f4d1534SAndreas Gohr     * @param $user
1113f4d1534SAndreas Gohr     */
112*964d95c6SAndreas Gohr    protected function runUserTests(auth_plugin_authpdo $auth, $user) {
1133f4d1534SAndreas Gohr        global $conf;
1143f4d1534SAndreas Gohr        $info = 'testing ' . $user['user'];
1153f4d1534SAndreas Gohr
1163f4d1534SAndreas Gohr        // minimal setup
1173f4d1534SAndreas Gohr        $this->assertTrue($auth->checkPass($user['user'], $user['pass']), $info);
1183f4d1534SAndreas Gohr        $check = $auth->getUserData($user['user']);
1193f4d1534SAndreas Gohr        $this->assertEquals($user['user'], $check['user'], $info);
1203f4d1534SAndreas Gohr        $this->assertEquals($user['name'], $check['name'], $info);
1213f4d1534SAndreas Gohr        $this->assertEquals($user['mail'], $check['mail'], $info);
1223f4d1534SAndreas Gohr        $groups = array_merge($user['grps'], array($conf['defaultgroup']));
1233f4d1534SAndreas Gohr        $this->assertEquals($groups, $check['grps'], $info);
1243f4d1534SAndreas Gohr
125*964d95c6SAndreas Gohr        // getUsers
126*964d95c6SAndreas Gohr        if($auth->canDo('getUsers')) {
127*964d95c6SAndreas Gohr            $list = $auth->retrieveUsers(0, -1, array('user' => $user['user']));
128*964d95c6SAndreas Gohr            $this->assertGreaterThanOrEqual(1, count($list));
129*964d95c6SAndreas Gohr            $list = $auth->retrieveUsers(0, -1, array('name' => $user['name']));
130*964d95c6SAndreas Gohr            $this->assertGreaterThanOrEqual(1, count($list));
131*964d95c6SAndreas Gohr            $list = $auth->retrieveUsers(0, -1, array('mail' => $user['mail']));
132*964d95c6SAndreas Gohr            $this->assertGreaterThanOrEqual(1, count($list));
133*964d95c6SAndreas Gohr        }
134*964d95c6SAndreas Gohr
135*964d95c6SAndreas Gohr        // getUserCount
136*964d95c6SAndreas Gohr        if($auth->canDo('getUserCount')) {
137*964d95c6SAndreas Gohr            $count = $auth->getUserCount(array('user' => $user['user']));
138*964d95c6SAndreas Gohr            $this->assertGreaterThanOrEqual(1, $count);
139*964d95c6SAndreas Gohr            $count = $auth->getUserCount(array('name' => $user['name']));
140*964d95c6SAndreas Gohr            $this->assertGreaterThanOrEqual(1, $count);
141*964d95c6SAndreas Gohr            $count = $auth->getUserCount(array('mail' => $user['mail']));
142*964d95c6SAndreas Gohr            $this->assertGreaterThanOrEqual(1, $count);
143*964d95c6SAndreas Gohr        }
144*964d95c6SAndreas Gohr
1453f4d1534SAndreas Gohr        // modPass
1463f4d1534SAndreas Gohr        if($auth->canDo('modPass')) {
1473f4d1534SAndreas Gohr            $newpass = 'foobar';
1483f4d1534SAndreas Gohr            $ok = $auth->modifyUser($user['user'], array('pass' => $newpass));
1493f4d1534SAndreas Gohr            $this->assertTrue($ok, $info);
1503f4d1534SAndreas Gohr            $this->assertTrue($auth->checkPass($user['user'], $newpass), $info);
1513f4d1534SAndreas Gohr        }
1523f4d1534SAndreas Gohr
1533f4d1534SAndreas Gohr        // modMail
1543f4d1534SAndreas Gohr        if($auth->canDo('modMail')) {
1553f4d1534SAndreas Gohr            $newmail = 'foobar@example.com';
1563f4d1534SAndreas Gohr            $ok = $auth->modifyUser($user['user'], array('mail' => $newmail));
1573f4d1534SAndreas Gohr            $this->assertTrue($ok, $info);
1583f4d1534SAndreas Gohr            $check = $auth->getUserData($user['user']);
1593f4d1534SAndreas Gohr            $this->assertEquals($newmail, $check['mail'], $info);
1603f4d1534SAndreas Gohr        }
1613f4d1534SAndreas Gohr
1623f4d1534SAndreas Gohr        // modName
1633f4d1534SAndreas Gohr        if($auth->canDo('modName')) {
1643f4d1534SAndreas Gohr            $newname = 'FirstName Foobar';
1653f4d1534SAndreas Gohr            $ok = $auth->modifyUser($user['user'], array('name' => $newname));
1663f4d1534SAndreas Gohr            $this->assertTrue($ok, $info);
1673f4d1534SAndreas Gohr            $check = $auth->getUserData($user['user']);
1683f4d1534SAndreas Gohr            $this->assertEquals($newname, $check['name'], $info);
1693f4d1534SAndreas Gohr        }
1703f4d1534SAndreas Gohr
1713f4d1534SAndreas Gohr        // modLogin
1723f4d1534SAndreas Gohr        if($auth->canDo('modLogin')) {
1733f4d1534SAndreas Gohr            $newuser = 'foobar' . $user['user'];
1743f4d1534SAndreas Gohr            $ok = $auth->modifyUser($user['user'], array('user' => $newuser));
1753f4d1534SAndreas Gohr            $this->assertTrue($ok, $info);
1763f4d1534SAndreas Gohr            $check = $auth->getUserData($newuser);
1773f4d1534SAndreas Gohr            $this->assertEquals($newuser, $check['user'], $info);
1783f4d1534SAndreas Gohr        }
1793f4d1534SAndreas Gohr
1803f4d1534SAndreas Gohr    }
1813f4d1534SAndreas Gohr
1823f4d1534SAndreas Gohr    /**
1833f4d1534SAndreas Gohr     * This triggers all the tests based on the dumps and configurations
1843f4d1534SAndreas Gohr     *
1853f4d1534SAndreas Gohr     * @depends test_requirements
1863f4d1534SAndreas Gohr     */
1873f4d1534SAndreas Gohr    public function test_mysql() {
1883f4d1534SAndreas Gohr        global $conf;
1893f4d1534SAndreas Gohr
1903f4d1534SAndreas Gohr        $files = glob(__DIR__ . '/mysql/*.php');
1913f4d1534SAndreas Gohr        foreach($files as $file) {
1923f4d1534SAndreas Gohr            $dump = preg_replace('/\.php$/', '.sql', $file);
1933f4d1534SAndreas Gohr
1943f4d1534SAndreas Gohr            $this->createDatabase();
1953f4d1534SAndreas Gohr            $this->importDatabase($dump);
1963f4d1534SAndreas Gohr
1973f4d1534SAndreas Gohr            // Setup the configuration and initialize a new auth object
1983f4d1534SAndreas Gohr            /** @var $data array */
1993f4d1534SAndreas Gohr            include $file;
2003f4d1534SAndreas Gohr
2013f4d1534SAndreas Gohr            $conf['plugin']['authpdo'] = array();
2023f4d1534SAndreas Gohr            $conf['plugin']['authpdo'] = $data['conf'];
2033f4d1534SAndreas Gohr            $conf['plugin']['authpdo']['dsn'] = "mysql:dbname={$this->database};host={$this->host}";
2043f4d1534SAndreas Gohr            $conf['plugin']['authpdo']['user'] = $this->user;
2053f4d1534SAndreas Gohr            $conf['plugin']['authpdo']['pass'] = $this->pass;
2063f4d1534SAndreas Gohr            $conf['plugin']['authpdo']['debug'] = 1;
2073f4d1534SAndreas Gohr            if($data['passcrypt']) $conf['passcrypt'] = $data['passcrypt'];
2083f4d1534SAndreas Gohr            $auth = new auth_plugin_authpdo();
2093f4d1534SAndreas Gohr
210*964d95c6SAndreas Gohr            $this->runGeneralTests($auth, $data['users']);
2113f4d1534SAndreas Gohr            foreach($data['users'] as $user) {
212*964d95c6SAndreas Gohr                $this->runUserTests($auth, $user);
2133f4d1534SAndreas Gohr            }
2143f4d1534SAndreas Gohr
2153f4d1534SAndreas Gohr            $this->dropDatabase();
2163f4d1534SAndreas Gohr        }
2173f4d1534SAndreas Gohr    }
2183f4d1534SAndreas Gohr
2193f4d1534SAndreas Gohr}
220