MDL-43450 behat: Checking the ->behat_* vars contents before showing the steps list
[moodle.git] / auth / db / tests / db_test.php
CommitLineData
e7aeaa65
PS
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * External database auth sync tests, this also tests adodb drivers
19 * that are matching our four supported Moodle database drivers.
20 *
21 * @package auth_db
22 * @category phpunit
23 * @copyright 2012 Petr Skoda {@link http://skodak.org}
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 */
26
27defined('MOODLE_INTERNAL') || die();
28
29
30class auth_db_testcase extends advanced_testcase {
31
32 protected function init_auth_database() {
33 global $DB, $CFG;
34 require_once("$CFG->dirroot/auth/db/auth.php");
35
36 $dbman = $DB->get_manager();
37
38 set_config('extencoding', 'utf-8', 'auth/db');
39
40 set_config('host', $CFG->dbhost, 'auth/db');
41 set_config('user', $CFG->dbuser, 'auth/db');
42 set_config('pass', $CFG->dbpass, 'auth/db');
43 set_config('name', $CFG->dbname, 'auth/db');
44
45 if (!empty($CFG->dboptions['dbport'])) {
46 set_config('host', $CFG->dbhost.':'.$CFG->dboptions['dbport'], 'auth/db');
47 }
48
49 switch (get_class($DB)) {
50 case 'mssql_native_moodle_database':
51 set_config('type', 'mssql_n', 'auth/db');
52 set_config('sybasequoting', '1', 'auth/db');
53 break;
54
17601a7e 55 case 'mariadb_native_moodle_database':
e7aeaa65
PS
56 case 'mysqli_native_moodle_database':
57 set_config('type', 'mysqli', 'auth/db');
58 set_config('setupsql', "SET NAMES 'UTF-8'", 'auth/db');
59 set_config('sybasequoting', '0', 'auth/db');
60 if (!empty($CFG->dboptions['dbsocket'])) {
61 $dbsocket = $CFG->dboptions['dbsocket'];
62 if ((strpos($dbsocket, '/') === false and strpos($dbsocket, '\\') === false)) {
63 $dbsocket = ini_get('mysqli.default_socket');
64 }
65 set_config('type', 'mysqli://'.rawurlencode($CFG->dbuser).':'.rawurlencode($CFG->dbpass).'@'.rawurlencode($CFG->dbhost).'/'.rawurlencode($CFG->dbname).'?socket='.rawurlencode($dbsocket), 'auth/db');
66 }
67 break;
68
69 case 'oci_native_moodle_database':
70 set_config('type', 'oci8po', 'auth/db');
71 set_config('sybasequoting', '1', 'auth/db');
72 break;
73
74 case 'pgsql_native_moodle_database':
75 set_config('type', 'postgres7', 'auth/db');
92b00c32
PS
76 $setupsql = "SET NAMES 'UTF-8'";
77 if (!empty($CFG->dboptions['dbschema'])) {
78 $setupsql .= "; SET search_path = '".$CFG->dboptions['dbschema']."'";
79 }
80 set_config('setupsql', $setupsql, 'auth/db');
e7aeaa65
PS
81 set_config('sybasequoting', '0', 'auth/db');
82 if (!empty($CFG->dboptions['dbsocket']) and ($CFG->dbhost === 'localhost' or $CFG->dbhost === '127.0.0.1')) {
83 if (strpos($CFG->dboptions['dbsocket'], '/') !== false) {
84 set_config('host', $CFG->dboptions['dbsocket'], 'auth/db');
85 } else {
86 set_config('host', '', 'auth/db');
87 }
88 }
89 break;
90
91 case 'sqlsrv_native_moodle_database':
92 set_config('type', 'mssqlnative', 'auth/db');
93 set_config('sybasequoting', '1', 'auth/db');
94 break;
95
96 default:
97 throw new exception('Unknown database driver '.get_class($DB));
98 }
99
100 $table = new xmldb_table('auth_db_users');
101 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
102 $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, null, null);
103 $table->add_field('pass', XMLDB_TYPE_CHAR, '255', null, null, null);
104 $table->add_field('email', XMLDB_TYPE_CHAR, '255', null, null, null);
105 $table->add_field('firstname', XMLDB_TYPE_CHAR, '255', null, null, null);
106 $table->add_field('lastname', XMLDB_TYPE_CHAR, '255', null, null, null);
107 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
108 if ($dbman->table_exists($table)) {
109 $dbman->drop_table($table);
110 }
111 $dbman->create_table($table);
92b00c32 112 set_config('table', $CFG->prefix.'auth_db_users', 'auth/db');
e7aeaa65
PS
113 set_config('fielduser', 'name', 'auth/db');
114 set_config('fieldpass', 'pass', 'auth/db');
115
116 // Setu up field mappings.
117
118 set_config('field_map_email', 'email', 'auth/db');
119 set_config('field_updatelocal_email', 'oncreate', 'auth/db');
120 set_config('field_updateremote_email', '0', 'auth/db');
121 set_config('field_lock_email', 'unlocked', 'auth/db');
122
123 // Init the rest of settings.
124 set_config('passtype', 'plaintext', 'auth/db');
125 set_config('changepasswordurl', '', 'auth/db');
126 set_config('debugauthdb', 0, 'auth/db');
127 set_config('removeuser', AUTH_REMOVEUSER_KEEP, 'auth/db');
128 }
129
130 protected function cleanup_auth_database() {
131 global $DB;
132
133 $dbman = $DB->get_manager();
134 $table = new xmldb_table('auth_db_users');
135 $dbman->drop_table($table);
136 }
137
138 public function test_plugin() {
139 global $DB, $CFG;
140
141 $this->resetAfterTest(false);
142
143 // NOTE: It is strongly discouraged to create new tables in advanced_testcase classes,
144 // but there is no other simple way to test ext database enrol sync, so let's
145 // disable transactions are try to cleanup after the tests.
146
147 $this->preventResetByRollback();
148
149 $this->init_auth_database();
150
151 /** @var auth_plugin_db $auth */
152 $auth = get_auth_plugin('db');
153
154 $authdb = $auth->db_init();
155
156
157 // Test adodb may access the table.
158
159 $user1 = (object)array('name'=>'u1', 'pass'=>'heslo', 'email'=>'u1@example.com');
160 $user1->id = $DB->insert_record('auth_db_users', $user1);
161
162
163 $sql = "SELECT * FROM {$auth->config->table}";
164 $rs = $authdb->Execute($sql);
165 $this->assertInstanceOf('ADORecordSet', $rs);
166 $this->assertFalse($rs->EOF);
167 $fields = $rs->FetchRow();
168 $this->assertTrue(is_array($fields));
169 $this->assertTrue($rs->EOF);
170 $rs->Close();
171
172 $authdb->Close();
173
174
175 // Test bulk user account creation.
176
177 $user2 = (object)array('name'=>'u2', 'pass'=>'heslo', 'email'=>'u2@example.com');
178 $user2->id = $DB->insert_record('auth_db_users', $user2);
179
180 $user3 = (object)array('name'=>'admin', 'pass'=>'heslo', 'email'=>'admin@example.com'); // Should be skipped.
181 $user3->id = $DB->insert_record('auth_db_users', $user3);
182
183 $this->assertCount(2, $DB->get_records('user'));
184
185 $trace = new null_progress_trace();
186 $auth->sync_users($trace, false);
187
188 $this->assertEquals(4, $DB->count_records('user'));
189 $u1 = $DB->get_record('user', array('username'=>$user1->name, 'auth'=>'db'));
190 $this->assertSame($user1->email, $u1->email);
191 $u2 = $DB->get_record('user', array('username'=>$user2->name, 'auth'=>'db'));
192 $this->assertSame($user2->email, $u2->email);
193 $admin = $DB->get_record('user', array('username'=>'admin', 'auth'=>'manual'));
194 $this->assertNotEmpty($admin);
195
196
197 // Test sync updates.
198
199 $user2b = clone($user2);
200 $user2b->email = 'u2b@example.com';
201 $DB->update_record('auth_db_users', $user2b);
202
203 $auth->sync_users($trace, false);
204 $this->assertEquals(4, $DB->count_records('user'));
205 $u2 = $DB->get_record('user', array('username'=>$user2->name));
206 $this->assertSame($user2->email, $u2->email);
207
208 $auth->sync_users($trace, true);
209 $this->assertEquals(4, $DB->count_records('user'));
210 $u2 = $DB->get_record('user', array('username'=>$user2->name));
211 $this->assertSame($user2->email, $u2->email);
212
213 set_config('field_updatelocal_email', 'onlogin', 'auth/db');
214 $auth->config->field_updatelocal_email = 'onlogin';
215
216 $auth->sync_users($trace, false);
217 $this->assertEquals(4, $DB->count_records('user'));
218 $u2 = $DB->get_record('user', array('username'=>$user2->name));
219 $this->assertSame($user2->email, $u2->email);
220
221 $auth->sync_users($trace, true);
222 $this->assertEquals(4, $DB->count_records('user'));
223 $u2 = $DB->get_record('user', array('username'=>$user2->name));
224 $this->assertSame($user2b->email, $u2->email);
225
226
227 // Test sync deletes and suspends.
228
229 $DB->delete_records('auth_db_users', array('id'=>$user2->id));
230 $this->assertCount(2, $DB->get_records('auth_db_users'));
231 unset($user2);
232 unset($user2b);
233
234 $auth->sync_users($trace, false);
235 $this->assertEquals(4, $DB->count_records('user'));
236 $this->assertEquals(0, $DB->count_records('user', array('deleted'=>1)));
237 $this->assertEquals(0, $DB->count_records('user', array('suspended'=>1)));
238
239 set_config('removeuser', AUTH_REMOVEUSER_SUSPEND, 'auth/db');
240 $auth->config->removeuser = AUTH_REMOVEUSER_SUSPEND;
241
242 $auth->sync_users($trace, false);
243 $this->assertEquals(4, $DB->count_records('user'));
244 $this->assertEquals(0, $DB->count_records('user', array('deleted'=>1)));
245 $this->assertEquals(1, $DB->count_records('user', array('suspended'=>1)));
246
247 $user2 = (object)array('name'=>'u2', 'pass'=>'heslo', 'email'=>'u2@example.com');
248 $user2->id = $DB->insert_record('auth_db_users', $user2);
249
250 $auth->sync_users($trace, false);
251 $this->assertEquals(4, $DB->count_records('user'));
252 $this->assertEquals(0, $DB->count_records('user', array('deleted'=>1)));
253 $this->assertEquals(0, $DB->count_records('user', array('suspended'=>1)));
254
255 $DB->delete_records('auth_db_users', array('id'=>$user2->id));
256
257 set_config('removeuser', AUTH_REMOVEUSER_FULLDELETE, 'auth/db');
258 $auth->config->removeuser = AUTH_REMOVEUSER_FULLDELETE;
259
260 $auth->sync_users($trace, false);
261 $this->assertEquals(4, $DB->count_records('user'));
262 $this->assertEquals(1, $DB->count_records('user', array('deleted'=>1)));
263 $this->assertEquals(0, $DB->count_records('user', array('suspended'=>1)));
264
265 $user2 = (object)array('name'=>'u2', 'pass'=>'heslo', 'email'=>'u2@example.com');
266 $user2->id = $DB->insert_record('auth_db_users', $user2);
267
268 $auth->sync_users($trace, false);
269 $this->assertEquals(5, $DB->count_records('user'));
270 $this->assertEquals(1, $DB->count_records('user', array('deleted'=>1)));
271 $this->assertEquals(0, $DB->count_records('user', array('suspended'=>1)));
272
273
274 // Test user_login().
275
276 $user3 = (object)array('name'=>'u3', 'pass'=>'heslo', 'email'=>'u3@example.com');
277 $user3->id = $DB->insert_record('auth_db_users', $user3);
278
279 $this->assertFalse($auth->user_login('u4', 'heslo'));
280 $this->assertTrue($auth->user_login('u1', 'heslo'));
281
282 $this->assertFalse($DB->record_exists('user', array('username'=>'u3', 'auth'=>'db')));
283 $this->assertTrue($auth->user_login('u3', 'heslo'));
284 $this->assertFalse($DB->record_exists('user', array('username'=>'u3', 'auth'=>'db')));
285
286 set_config('passtype', 'md5', 'auth/db');
287 $auth->config->passtype = 'md5';
288 $user3->pass = md5('heslo');
289 $DB->update_record('auth_db_users', $user3);
290 $this->assertTrue($auth->user_login('u3', 'heslo'));
291
292 set_config('passtype', 'sh1', 'auth/db');
293 $auth->config->passtype = 'sha1';
294 $user3->pass = sha1('heslo');
295 $DB->update_record('auth_db_users', $user3);
296 $this->assertTrue($auth->user_login('u3', 'heslo'));
297
298 set_config('passtype', 'internal', 'auth/db');
299 $auth->config->passtype = 'internal';
300 create_user_record('u3', 'heslo', 'db');
301 $this->assertTrue($auth->user_login('u3', 'heslo'));
302
303
304 $DB->delete_records('auth_db_users', array('id'=>$user3->id));
305
306 set_config('removeuser', AUTH_REMOVEUSER_KEEP, 'auth/db');
307 $auth->config->removeuser = AUTH_REMOVEUSER_KEEP;
308 $this->assertTrue($auth->user_login('u3', 'heslo'));
309
310 set_config('removeuser', AUTH_REMOVEUSER_SUSPEND, 'auth/db');
311 $auth->config->removeuser = AUTH_REMOVEUSER_SUSPEND;
312 $this->assertFalse($auth->user_login('u3', 'heslo'));
313
314 set_config('removeuser', AUTH_REMOVEUSER_FULLDELETE, 'auth/db');
315 $auth->config->removeuser = AUTH_REMOVEUSER_FULLDELETE;
316 $this->assertFalse($auth->user_login('u3', 'heslo'));
317
318 set_config('passtype', 'sh1', 'auth/db');
319 $auth->config->passtype = 'sha1';
320 $this->assertFalse($auth->user_login('u3', 'heslo'));
321
322
323 // Test login create and update.
324
325 $user4 = (object)array('name'=>'u4', 'pass'=>'heslo', 'email'=>'u4@example.com');
326 $user4->id = $DB->insert_record('auth_db_users', $user4);
327
328 set_config('passtype', 'plaintext', 'auth/db');
329 $auth->config->passtype = 'plaintext';
330
331 $iuser4 = create_user_record('u4', 'heslo', 'db');
332 $this->assertNotEmpty($iuser4);
333 $this->assertSame($user4->name, $iuser4->username);
334 $this->assertSame($user4->email, $iuser4->email);
335 $this->assertSame('db', $iuser4->auth);
336 $this->assertSame($CFG->mnet_localhost_id, $iuser4->mnethostid);
337
338 $user4b = clone($user4);
339 $user4b->email = 'u4b@example.com';
340 $DB->update_record('auth_db_users', $user4b);
341
342 set_config('field_updatelocal_email', 'oncreate', 'auth/db');
343 $auth->config->field_updatelocal_email = 'oncreate';
344
345 update_user_record('u4');
346 $iuser4 = $DB->get_record('user', array('id'=>$iuser4->id));
347 $this->assertSame($user4->email, $iuser4->email);
348
349 set_config('field_updatelocal_email', 'onlogin', 'auth/db');
350 $auth->config->field_updatelocal_email = 'onlogin';
351
352 update_user_record('u4');
353 $iuser4 = $DB->get_record('user', array('id'=>$iuser4->id));
354 $this->assertSame($user4b->email, $iuser4->email);
355
356
357 // Test user_exists()
358
359 $this->assertTrue($auth->user_exists('u1'));
360 $this->assertTrue($auth->user_exists('admin'));
361 $this->assertFalse($auth->user_exists('u3'));
362 $this->assertTrue($auth->user_exists('u4'));
363
364 $this->cleanup_auth_database();
365 }
366}