1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* MySQL tests for the authpdo plugin |
5
|
|
|
* |
6
|
|
|
* @group plugin_authpdo |
7
|
|
|
* @group plugins |
8
|
|
|
*/ |
9
|
|
|
class 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 general tests on all users |
88
|
|
|
* |
89
|
|
|
* @param auth_plugin_authpdo $auth |
90
|
|
|
* @param array $users |
91
|
|
|
*/ |
92
|
|
|
protected function runGeneralTests(auth_plugin_authpdo $auth, $users) { |
93
|
|
|
global $conf; |
94
|
|
|
$info = 'DSN: ' . $auth->getConf('dsn'); |
95
|
|
|
$this->assertTrue($auth->success, $info); |
96
|
|
|
|
97
|
|
|
if($auth->canDo('getUsers')) { |
98
|
|
|
$list = $auth->retrieveUsers(); |
99
|
|
|
$this->assertGreaterThanOrEqual(count($users), count($list), $info); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
if($auth->canDo('getGroups')) { |
103
|
|
|
$list = $auth->retrieveGroups(); |
104
|
|
|
$this->assertGreaterThanOrEqual(1, $list, $info); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
if($auth->canDo('getUserCount')) { |
108
|
|
|
$count = $auth->getUserCount(); |
109
|
|
|
$this->assertGreaterThanOrEqual(count($users), $count); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
if($auth->canDo('addUser')) { |
113
|
|
|
$newuser = array( |
114
|
|
|
'user' => 'newuserfoobar', |
115
|
|
|
'name' => 'First LastFoobar', |
116
|
|
|
'pass' => 'password', |
117
|
|
|
'mail' => '[email protected]', |
118
|
|
|
'grps' => array('acompletelynewgroup') |
119
|
|
|
); |
120
|
|
|
$ok = $auth->createUser( |
121
|
|
|
$newuser['user'], |
122
|
|
|
$newuser['pass'], |
123
|
|
|
$newuser['name'], |
124
|
|
|
$newuser['mail'], |
125
|
|
|
$newuser['grps'] |
126
|
|
|
); |
127
|
|
|
$this->assertTrue($ok, $info); |
128
|
|
|
$check = $auth->getUserData($newuser['user']); |
129
|
|
|
$this->assertEquals($newuser['user'], $check['user'], $info); |
130
|
|
|
$this->assertEquals($newuser['mail'], $check['mail'], $info); |
131
|
|
|
$groups = array_merge($newuser['grps'], array($conf['defaultgroup'])); |
132
|
|
|
$this->assertEquals($groups, $check['grps'], $info); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* run all the tests with the given user, depending on the capabilities |
138
|
|
|
* |
139
|
|
|
* @param auth_plugin_authpdo $auth |
140
|
|
|
* @param $user |
141
|
|
|
*/ |
142
|
|
|
protected function runUserTests(auth_plugin_authpdo $auth, $user) { |
143
|
|
|
global $conf; |
144
|
|
|
$info = 'DSN: ' . $auth->getConf('dsn') . ' User:' . $user['user']; |
145
|
|
|
|
146
|
|
|
// minimal setup |
147
|
|
|
$this->assertTrue($auth->checkPass($user['user'], $user['pass']), $info); |
148
|
|
|
$check = $auth->getUserData($user['user']); |
149
|
|
|
$this->assertEquals($user['user'], $check['user'], $info); |
150
|
|
|
$this->assertEquals($user['name'], $check['name'], $info); |
151
|
|
|
$this->assertEquals($user['mail'], $check['mail'], $info); |
152
|
|
|
$groups = array_merge($user['grps'], array($conf['defaultgroup'])); |
153
|
|
|
$this->assertEquals($groups, $check['grps'], $info); |
154
|
|
|
|
155
|
|
|
// getUsers |
156
|
|
|
if($auth->canDo('getUsers')) { |
157
|
|
|
$list = $auth->retrieveUsers(0, -1, array('user' => $user['user'])); |
158
|
|
|
$this->assertGreaterThanOrEqual(1, count($list)); |
159
|
|
|
$list = $auth->retrieveUsers(0, -1, array('name' => $user['name'])); |
160
|
|
|
$this->assertGreaterThanOrEqual(1, count($list)); |
161
|
|
|
$list = $auth->retrieveUsers(0, -1, array('mail' => $user['mail'])); |
162
|
|
|
$this->assertGreaterThanOrEqual(1, count($list)); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
// getUserCount |
166
|
|
|
if($auth->canDo('getUserCount')) { |
167
|
|
|
$count = $auth->getUserCount(array('user' => $user['user'])); |
168
|
|
|
$this->assertGreaterThanOrEqual(1, $count); |
169
|
|
|
$count = $auth->getUserCount(array('name' => $user['name'])); |
170
|
|
|
$this->assertGreaterThanOrEqual(1, $count); |
171
|
|
|
$count = $auth->getUserCount(array('mail' => $user['mail'])); |
172
|
|
|
$this->assertGreaterThanOrEqual(1, $count); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
// modGroups |
176
|
|
|
if($auth->canDo('modGroups')) { |
177
|
|
|
$newgroup = 'foobar'; |
178
|
|
|
$ok = $auth->modifyUser($user['user'], array('grps' => array($newgroup))); |
179
|
|
|
$this->assertTrue($ok, $info); |
180
|
|
|
$check = $auth->getUserData($user['user']); |
181
|
|
|
$this->assertTrue(in_array($newgroup, $check['grps']), $info); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
// modPass |
185
|
|
|
if($auth->canDo('modPass')) { |
186
|
|
|
$newpass = 'foobar'; |
187
|
|
|
$ok = $auth->modifyUser($user['user'], array('pass' => $newpass)); |
188
|
|
|
$this->assertTrue($ok, $info); |
189
|
|
|
$this->assertTrue($auth->checkPass($user['user'], $newpass), $info); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
// modMail |
193
|
|
|
if($auth->canDo('modMail')) { |
194
|
|
|
$newmail = '[email protected]'; |
195
|
|
|
$ok = $auth->modifyUser($user['user'], array('mail' => $newmail)); |
196
|
|
|
$this->assertTrue($ok, $info); |
197
|
|
|
$check = $auth->getUserData($user['user']); |
198
|
|
|
$this->assertEquals($newmail, $check['mail'], $info); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
// modName |
202
|
|
|
if($auth->canDo('modName')) { |
203
|
|
|
$newname = 'FirstName Foobar'; |
204
|
|
|
$ok = $auth->modifyUser($user['user'], array('name' => $newname)); |
205
|
|
|
$this->assertTrue($ok, $info); |
206
|
|
|
$check = $auth->getUserData($user['user']); |
207
|
|
|
$this->assertEquals($newname, $check['name'], $info); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
// modLogin |
211
|
|
|
if($auth->canDo('modLogin')) { |
212
|
|
|
$newuser = 'foobar' . $user['user']; |
213
|
|
|
$ok = $auth->modifyUser($user['user'], array('user' => $newuser)); |
214
|
|
|
$this->assertTrue($ok, $info); |
215
|
|
|
$check = $auth->getUserData($newuser); |
216
|
|
|
$this->assertEquals($newuser, $check['user'], $info); |
217
|
|
|
// rename back |
218
|
|
|
$ok = $auth->modifyUser($newuser, array('user' => $user['user'])); |
219
|
|
|
$this->assertTrue($ok, $info); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
// delUser |
223
|
|
|
if($auth->canDo('delUser')) { |
224
|
|
|
$num = $auth->deleteUsers(array($user['user'])); |
225
|
|
|
$this->assertEquals(1, $num, $info); |
226
|
|
|
$this->assertFalse($auth->getUserData($user['user']), $info); |
227
|
|
|
} |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* This triggers all the tests based on the dumps and configurations |
232
|
|
|
* |
233
|
|
|
* @depends test_requirements |
234
|
|
|
*/ |
235
|
|
|
public function test_mysql() { |
236
|
|
|
global $conf; |
237
|
|
|
|
238
|
|
|
$files = glob(__DIR__ . '/mysql/*.php'); |
239
|
|
|
foreach($files as $file) { |
240
|
|
|
$dump = preg_replace('/\.php$/', '.sql', $file); |
241
|
|
|
$this->database = 'authpdo_testing_' . basename($file, '.php'); |
242
|
|
|
|
243
|
|
|
$this->createDatabase(); |
244
|
|
|
$this->importDatabase($dump); |
245
|
|
|
|
246
|
|
|
// Setup the configuration and initialize a new auth object |
247
|
|
|
/** @var $data array */ |
248
|
|
|
include $file; |
249
|
|
|
|
250
|
|
|
$conf['plugin']['authpdo'] = array(); |
251
|
|
|
$conf['plugin']['authpdo'] = $data['conf']; |
|
|
|
|
252
|
|
|
$conf['plugin']['authpdo']['dsn'] = "mysql:dbname={$this->database};host={$this->host}"; |
253
|
|
|
$conf['plugin']['authpdo']['user'] = $this->user; |
254
|
|
|
$conf['plugin']['authpdo']['pass'] = $this->pass; |
255
|
|
|
$conf['plugin']['authpdo']['debug'] = 1; |
256
|
|
|
if($data['passcrypt']) $conf['passcrypt'] = $data['passcrypt']; |
257
|
|
|
$auth = new auth_plugin_authpdo(); |
258
|
|
|
|
259
|
|
|
$this->runGeneralTests($auth, $data['users']); |
260
|
|
|
foreach($data['users'] as $user) { |
261
|
|
|
$this->runUserTests($auth, $user); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
$this->dropDatabase(); |
265
|
|
|
} |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
} |
269
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.