aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/lib/plugins/authpdo/_test/mysql.test.php
blob: 35a76baf541bdc66f6f9e99405164af9c7b9beba (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
<?php

/**
 * mysql tests for the authpdo plugin
 *
 * @group plugin_authpdo
 * @group plugins
 */
class mysql_plugin_authpdo_test extends DokuWikiTest {

    protected $driver = 'mysql';
    protected $host = '';
    protected $database = 'authpdo_testing';
    protected $user = '';
    protected $pass = '';
    protected $port = '';

    public function setUp() : void {
        parent::setUp();
        $configuration = DOKU_UNITTEST . "{$this->driver}.conf.php";
        if(!file_exists($configuration)) {
            return;
        }
        /** @var $conf array */
        include $configuration;
        $this->host = $conf['host'];
        $this->user = $conf['user'];
        $this->pass = $conf['pass'];
        $this->port = $conf['port'];
    }

    /**
     * try to remove the last set up database
     *
     * it might still be there if something went wrong
     */
    public function tearDown() : void {
        parent::tearDown();
        $this->dropDatabase();
    }

    /**
     * Check if database credentials and extensions exist
     */
    public function test_requirements() {
        if(!$this->host || !$this->user) {
            $this->markTestSkipped("Skipped {$this->driver} tests. Missing configuration");
        }
        if(!class_exists('PDO')) {
            $this->markTestSkipped("Skipped {$this->driver} tests. Missing PDO extension");
        }
        if(!in_array($this->driver, pdo_drivers())) {
            $this->markTestSkipped("Skipped {$this->driver} tests. Missing pdo_{$this->driver} extension");
        }
        $this->assertTrue(true); // avoid being marked as risky for having no assertion
    }

    /**
     * create the database for testing
     */
    protected function createDatabase() {
        $pdo = new PDO(
            "{$this->driver}:host={$this->host};port={$this->port}", $this->user, $this->pass,
            array(
                PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, // we want exceptions, not error codes
            )
        );
        $pdo->exec("DROP DATABASE IF EXISTS {$this->database}");
        $pdo->exec("CREATE DATABASE {$this->database}");
        $pdo = null;
    }

    /**
     * remove the database
     */
    protected function dropDatabase() {
        $pdo = new PDO(
            "{$this->driver}:host={$this->host};port={$this->port}", $this->user, $this->pass,
            array(
                PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, // we want exceptions, not error codes
            )
        );
        try {
            $pdo->exec("DROP DATABASE IF EXISTS {$this->database}");
        } catch (PDOException $e) {
            // ignore - sometimes this fails even though the database was deleted
        }
        $pdo = null;
    }

    /**
     * imports a database dump
     *
     * @param $file
     */
    protected function importDatabase($file) {
        // connect to database and import dump
        $pdo = null;
        $pdo = new PDO(
            "{$this->driver}:dbname={$this->database};host={$this->host};port={$this->port}", $this->user, $this->pass,
            array(
                PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, // we want exceptions, not error codes
            )
        );
        $sql = file_get_contents($file);
        $pdo->exec($sql);
        $pdo = null;
    }

    /**
     * Run general tests on all users
     *
     * @param auth_plugin_authpdo $auth
     * @param array $users
     */
    protected function runGeneralTests(auth_plugin_authpdo $auth, $users) {
        global $conf;
        $info = 'DSN: ' . $auth->getConf('dsn');
        $this->assertTrue($auth->success, $info);

        if($auth->canDo('getUsers')) {
            $list = $auth->retrieveUsers();
            $this->assertGreaterThanOrEqual(count($users), count($list), $info);
        }

        if($auth->canDo('getGroups')) {
            $list = $auth->retrieveGroups();
            $this->assertGreaterThanOrEqual(1, $list, $info);
        }

        if($auth->canDo('getUserCount')) {
            $count = $auth->getUserCount();
            $this->assertGreaterThanOrEqual(count($users), $count);
        }

        if($auth->canDo('addUser')) {
            $newuser = array(
                'user' => 'newuserfoobar',
                'name' => 'First LastFoobar',
                'pass' => 'password',
                'mail' => 'newuserfoobar@example.com',
                'grps' => array('acompletelynewgroup')
            );
            $ok = $auth->createUser(
                $newuser['user'],
                $newuser['pass'],
                $newuser['name'],
                $newuser['mail'],
                $newuser['grps']
            );
            $this->assertTrue($ok, $info);
            $check = $auth->getUserData($newuser['user']);
            $this->assertEquals($newuser['user'], $check['user'], $info);
            $this->assertEquals($newuser['mail'], $check['mail'], $info);
            $groups = array_merge($newuser['grps'], array($conf['defaultgroup']));
            $this->assertEquals($groups, $check['grps'], $info);
        }
    }

    /**
     * run all the tests with the given user, depending on the capabilities
     *
     * @param auth_plugin_authpdo $auth
     * @param $user
     */
    protected function runUserTests(auth_plugin_authpdo $auth, $user) {
        global $conf;
        $info = 'DSN: ' . $auth->getConf('dsn') . ' User:' . $user['user'];

        // minimal setup
        $this->assertTrue($auth->checkPass($user['user'], $user['pass']), $info);
        $check = $auth->getUserData($user['user']);
        $this->assertEquals($user['user'], $check['user'], $info);
        $this->assertEquals($user['name'], $check['name'], $info);
        $this->assertEquals($user['mail'], $check['mail'], $info);
        $groups = array_merge($user['grps'], array($conf['defaultgroup']));
        $this->assertEquals($groups, $check['grps'], $info);

        // getUsers
        if($auth->canDo('getUsers')) {
            $list = $auth->retrieveUsers(0, -1, array('user' => $user['user']));
            $this->assertGreaterThanOrEqual(1, count($list));
            $list = $auth->retrieveUsers(0, -1, array('name' => $user['name']));
            $this->assertGreaterThanOrEqual(1, count($list));
            $list = $auth->retrieveUsers(0, -1, array('mail' => $user['mail']));
            $this->assertGreaterThanOrEqual(1, count($list));
        }

        // getUserCount
        if($auth->canDo('getUserCount')) {
            $count = $auth->getUserCount(array('user' => $user['user']));
            $this->assertGreaterThanOrEqual(1, $count);
            $count = $auth->getUserCount(array('name' => $user['name']));
            $this->assertGreaterThanOrEqual(1, $count);
            $count = $auth->getUserCount(array('mail' => $user['mail']));
            $this->assertGreaterThanOrEqual(1, $count);
        }

        // modGroups
        if($auth->canDo('modGroups')) {
            $newgroup = 'foobar';
            $ok = $auth->modifyUser($user['user'], array('grps' => array($newgroup)));
            $this->assertTrue($ok, $info);
            $check = $auth->getUserData($user['user']);
            $this->assertTrue(in_array($newgroup, $check['grps']), $info);
        }

        // modPass
        if($auth->canDo('modPass')) {
            $newpass = 'foobar';
            $ok = $auth->modifyUser($user['user'], array('pass' => $newpass));
            $this->assertTrue($ok, $info);
            $this->assertTrue($auth->checkPass($user['user'], $newpass), $info);
        }

        // modMail
        if($auth->canDo('modMail')) {
            $newmail = 'foobar@example.com';
            $ok = $auth->modifyUser($user['user'], array('mail' => $newmail));
            $this->assertTrue($ok, $info);
            $check = $auth->getUserData($user['user']);
            $this->assertEquals($newmail, $check['mail'], $info);
        }

        // modName
        if($auth->canDo('modName')) {
            $newname = 'FirstName Foobar';
            $ok = $auth->modifyUser($user['user'], array('name' => $newname));
            $this->assertTrue($ok, $info);
            $check = $auth->getUserData($user['user']);
            $this->assertEquals($newname, $check['name'], $info);
        }

        // modLogin
        if($auth->canDo('modLogin')) {
            $newuser = 'foobar' . $user['user'];
            $ok = $auth->modifyUser($user['user'], array('user' => $newuser));
            $this->assertTrue($ok, $info);
            $check = $auth->getUserData($newuser);
            $this->assertEquals($newuser, $check['user'], $info);
            // rename back
            $ok = $auth->modifyUser($newuser, array('user' => $user['user']));
            $this->assertTrue($ok, $info);
        }

        // delUser
        if($auth->canDo('delUser')) {
            $num = $auth->deleteUsers(array($user['user']));
            $this->assertEquals(1, $num, $info);
            $this->assertFalse($auth->getUserData($user['user']), $info);
        }
    }

    /**
     * prepares the individual configurations for testing
     *
     * @return array
     */
    public function data_provider() {
        $testdata = array();

        $files = glob(__DIR__ . "/{$this->driver}/*.php");
        foreach($files as $file) {
            $dump = preg_replace('/\.php$/', '.sql', $file);
            $dbname = 'authpdo_testing_' . basename($file, '.php');

            /** @var $data array */
            include $file;

            $testdata[] = array($dbname, $dump, $data);
        }

        return $testdata;
    }

    /**
     * This triggers all the tests based on the dumps and configurations
     *
     * @dataProvider data_provider
     * @depends      test_requirements
     * @param string $dbname Name of the database to use
     * @param string $dump The path to the dump file to import
     * @param array|string $data config and test user setup. When a string is passed, test is skipped with that msg
     */
    public function test_database($dbname, $dump, $data){
        global $conf;

        if(!is_array($data)) {
            $this->markTestSkipped($data);
            return;
        }

        $this->database = $dbname;

        $this->createDatabase();
        $this->importDatabase($dump);

        // Setup the configuration and initialize a new auth object
        $conf['plugin']['authpdo'] = array();
        $conf['plugin']['authpdo'] = $data['conf'];
        $conf['plugin']['authpdo']['dsn'] = "{$this->driver}:dbname={$this->database};host={$this->host};port={$this->port}";
        $conf['plugin']['authpdo']['user'] = $this->user;
        $conf['plugin']['authpdo']['pass'] = $this->pass;
        $conf['plugin']['authpdo']['debug'] = 1;
        if($data['passcrypt']) $conf['passcrypt'] = $data['passcrypt'];
        $auth = new auth_plugin_authpdo();

        $this->runGeneralTests($auth, $data['users']);
        foreach($data['users'] as $user) {
            $this->runUserTests($auth, $user);
        }

        $this->dropDatabase();
    }

}