xref: /dokuwiki/lib/plugins/authpdo/_test/mysql.test.php (revision 3f4d15349d786af7992b9e7377fcd5298eb6eb4b)
1<?php
2
3/**
4 * MySQL tests for the authpdo plugin
5 *
6 * @group plugin_authpdo
7 * @group plugins
8 */
9class mysql_plugin_authpdo_test extends DokuWikiTest {
10
11    protected $host = '';
12    protected $database = 'authpdo_testing';
13    protected $user = '';
14    protected $pass = '';
15
16    public function setUp() {
17        parent::setUp();
18        $configuration = DOKU_UNITTEST . 'mysql.conf.php';
19        if(!file_exists($configuration)) {
20            return;
21        }
22        /** @var $conf array */
23        include $configuration;
24        $this->host = $conf['host'];
25        $this->user = $conf['user'];
26        $this->pass = $conf['pass'];
27    }
28
29    /**
30     * Check if database credentials exist
31     */
32    public function test_requirements() {
33        if(!$this->host || !$this->user) {
34            $this->markTestSkipped("Skipped mysql tests. Missing configuration");
35        }
36    }
37
38    /**
39     * create the database for testing
40     */
41    protected function createDatabase() {
42        $pdo = new PDO(
43            "mysql:dbname=;host={$this->host}", $this->user, $this->pass,
44            array(
45                PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, // we want exceptions, not error codes
46            )
47        );
48        $pdo->exec("DROP DATABASE IF EXISTS {$this->database}");
49        $pdo->exec("CREATE DATABASE {$this->database}");
50        $pdo = null;
51    }
52
53    /**
54     * remove the database
55     */
56    protected function dropDatabase() {
57        $pdo = new PDO(
58            "mysql:dbname={$this->database};host={$this->host}", $this->user, $this->pass,
59            array(
60                PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, // we want exceptions, not error codes
61            )
62        );
63        $pdo->exec("DROP DATABASE IF EXISTS {$this->database}");
64        $pdo = null;
65    }
66
67    /**
68     * imports a database dump
69     *
70     * @param $file
71     */
72    protected function importDatabase($file) {
73        // connect to database and import dump
74        $pdo = null;
75        $pdo = new PDO(
76            "mysql:dbname={$this->database};host={$this->host}", $this->user, $this->pass,
77            array(
78                PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, // we want exceptions, not error codes
79            )
80        );
81        $sql = file_get_contents($file);
82        $pdo->exec($sql);
83        $pdo = null;
84    }
85
86    /**
87     * run all the tests with the given user, depending on the capabilities
88     *
89     * @param auth_plugin_authpdo $auth
90     * @param $user
91     */
92    protected function runTests(auth_plugin_authpdo $auth, $user) {
93        global $conf;
94        $info = 'testing ' . $user['user'];
95        $this->assertTrue($auth->success, $info);
96
97        // minimal setup
98        $this->assertTrue($auth->checkPass($user['user'], $user['pass']), $info);
99        $check = $auth->getUserData($user['user']);
100        $this->assertEquals($user['user'], $check['user'], $info);
101        $this->assertEquals($user['name'], $check['name'], $info);
102        $this->assertEquals($user['mail'], $check['mail'], $info);
103        $groups = array_merge($user['grps'], array($conf['defaultgroup']));
104        $this->assertEquals($groups, $check['grps'], $info);
105
106        // modPass
107        if($auth->canDo('modPass')) {
108            $newpass = 'foobar';
109            $ok = $auth->modifyUser($user['user'], array('pass' => $newpass));
110            $this->assertTrue($ok, $info);
111            $this->assertTrue($auth->checkPass($user['user'], $newpass), $info);
112        }
113
114        // modMail
115        if($auth->canDo('modMail')) {
116            $newmail = 'foobar@example.com';
117            $ok = $auth->modifyUser($user['user'], array('mail' => $newmail));
118            $this->assertTrue($ok, $info);
119            $check = $auth->getUserData($user['user']);
120            $this->assertEquals($newmail, $check['mail'], $info);
121        }
122
123        // modName
124        if($auth->canDo('modName')) {
125            $newname = 'FirstName Foobar';
126            $ok = $auth->modifyUser($user['user'], array('name' => $newname));
127            $this->assertTrue($ok, $info);
128            $check = $auth->getUserData($user['user']);
129            $this->assertEquals($newname, $check['name'], $info);
130        }
131
132        // modLogin
133        if($auth->canDo('modLogin')) {
134            $newuser = 'foobar'.$user['user'];
135            $ok = $auth->modifyUser($user['user'], array('user' => $newuser));
136            $this->assertTrue($ok, $info);
137            $check = $auth->getUserData($newuser);
138            $this->assertEquals($newuser, $check['user'], $info);
139        }
140
141    }
142
143    /**
144     * This triggers all the tests based on the dumps and configurations
145     *
146     * @depends test_requirements
147     */
148    public function test_mysql() {
149        global $conf;
150
151        $files = glob(__DIR__ . '/mysql/*.php');
152        foreach($files as $file) {
153            $dump = preg_replace('/\.php$/', '.sql', $file);
154
155            $this->createDatabase();
156            $this->importDatabase($dump);
157
158            // Setup the configuration and initialize a new auth object
159            /** @var $data array */
160            include $file;
161
162            $conf['plugin']['authpdo'] = array();
163            $conf['plugin']['authpdo'] = $data['conf'];
164            $conf['plugin']['authpdo']['dsn'] = "mysql:dbname={$this->database};host={$this->host}";
165            $conf['plugin']['authpdo']['user'] = $this->user;
166            $conf['plugin']['authpdo']['pass'] = $this->pass;
167            $conf['plugin']['authpdo']['debug'] = 1;
168            if($data['passcrypt']) $conf['passcrypt'] = $data['passcrypt'];
169            $auth = new auth_plugin_authpdo();
170
171            foreach($data['users'] as $user) {
172                $this->runTests($auth, $user);
173            }
174
175            $this->dropDatabase();
176        }
177    }
178
179}
180