Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403]

   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  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  
  30  class auth_db_testcase extends advanced_testcase {
  31      /** @var string Original error log */
  32      protected $oldlog;
  33  
  34      /** @var int The amount of users to create for the large user set deletion test  */
  35      protected $largedeletionsetsize = 128;
  36  
  37      protected function init_auth_database() {
  38          global $DB, $CFG;
  39          require_once("$CFG->dirroot/auth/db/auth.php");
  40  
  41          // Discard error logs from AdoDB.
  42          $this->oldlog = ini_get('error_log');
  43          ini_set('error_log', "$CFG->dataroot/testlog.log");
  44  
  45          $dbman = $DB->get_manager();
  46  
  47          set_config('extencoding', 'utf-8', 'auth_db');
  48  
  49          set_config('host', $CFG->dbhost, 'auth_db');
  50          set_config('user', $CFG->dbuser, 'auth_db');
  51          set_config('pass', $CFG->dbpass, 'auth_db');
  52          set_config('name', $CFG->dbname, 'auth_db');
  53  
  54          if (!empty($CFG->dboptions['dbport'])) {
  55              set_config('host', $CFG->dbhost.':'.$CFG->dboptions['dbport'], 'auth_db');
  56          }
  57  
  58          switch ($DB->get_dbfamily()) {
  59  
  60              case 'mysql':
  61                  set_config('type', 'mysqli', 'auth_db');
  62                  set_config('setupsql', "SET NAMES 'UTF-8'", 'auth_db');
  63                  set_config('sybasequoting', '0', 'auth_db');
  64                  if (!empty($CFG->dboptions['dbsocket'])) {
  65                      $dbsocket = $CFG->dboptions['dbsocket'];
  66                      if ((strpos($dbsocket, '/') === false and strpos($dbsocket, '\\') === false)) {
  67                          $dbsocket = ini_get('mysqli.default_socket');
  68                      }
  69                      set_config('type', 'mysqli://'.rawurlencode($CFG->dbuser).':'.rawurlencode($CFG->dbpass).'@'.rawurlencode($CFG->dbhost).'/'.rawurlencode($CFG->dbname).'?socket='.rawurlencode($dbsocket), 'auth_db');
  70                  }
  71                  break;
  72  
  73              case 'oracle':
  74                  set_config('type', 'oci8po', 'auth_db');
  75                  set_config('sybasequoting', '1', 'auth_db');
  76                  break;
  77  
  78              case 'postgres':
  79                  set_config('type', 'postgres7', 'auth_db');
  80                  $setupsql = "SET NAMES 'UTF-8'";
  81                  if (!empty($CFG->dboptions['dbschema'])) {
  82                      $setupsql .= "; SET search_path = '".$CFG->dboptions['dbschema']."'";
  83                  }
  84                  set_config('setupsql', $setupsql, 'auth_db');
  85                  set_config('sybasequoting', '0', 'auth_db');
  86                  if (!empty($CFG->dboptions['dbsocket']) and ($CFG->dbhost === 'localhost' or $CFG->dbhost === '127.0.0.1')) {
  87                      if (strpos($CFG->dboptions['dbsocket'], '/') !== false) {
  88                          $socket = $CFG->dboptions['dbsocket'];
  89                          if (!empty($CFG->dboptions['dbport'])) {
  90                              $socket .= ':' . $CFG->dboptions['dbport'];
  91                          }
  92                          set_config('host', $socket, 'auth_db');
  93                      } else {
  94                          set_config('host', '', 'auth_db');
  95                      }
  96                  }
  97                  break;
  98  
  99              case 'mssql':
 100                  set_config('type', 'mssqlnative', 'auth_db');
 101                  set_config('sybasequoting', '1', 'auth_db');
 102  
 103                  // The native sqlsrv driver uses a comma as separator between host and port.
 104                  $dbhost = $CFG->dbhost;
 105                  if (!empty($dboptions['dbport'])) {
 106                      $dbhost .= ',' . $dboptions['dbport'];
 107                  }
 108                  set_config('host', $dbhost, 'auth_db');
 109                  break;
 110  
 111              default:
 112                  throw new exception('Unknown database family ' . $DB->get_dbfamily());
 113          }
 114  
 115          $table = new xmldb_table('auth_db_users');
 116          $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
 117          $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, null, null);
 118          $table->add_field('pass', XMLDB_TYPE_CHAR, '255', null, null, null);
 119          $table->add_field('email', XMLDB_TYPE_CHAR, '255', null, null, null);
 120          $table->add_field('firstname', XMLDB_TYPE_CHAR, '255', null, null, null);
 121          $table->add_field('lastname', XMLDB_TYPE_CHAR, '255', null, null, null);
 122          $table->add_field('animal', XMLDB_TYPE_CHAR, '255', null, null, null);
 123          $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
 124          if ($dbman->table_exists($table)) {
 125              $dbman->drop_table($table);
 126          }
 127          $dbman->create_table($table);
 128          set_config('table', $CFG->prefix.'auth_db_users', 'auth_db');
 129          set_config('fielduser', 'name', 'auth_db');
 130          set_config('fieldpass', 'pass', 'auth_db');
 131          set_config('field_map_lastname', 'lastname', 'auth_db');
 132          set_config('field_updatelocal_lastname', 'oncreate', 'auth_db');
 133          set_config('field_lock_lastname', 'unlocked', 'auth_db');
 134          // Setu up field mappings.
 135  
 136          set_config('field_map_email', 'email', 'auth_db');
 137          set_config('field_updatelocal_email', 'oncreate', 'auth_db');
 138          set_config('field_updateremote_email', '0', 'auth_db');
 139          set_config('field_lock_email', 'unlocked', 'auth_db');
 140  
 141          // Create a user profile field and add mapping to it.
 142          $DB->insert_record('user_info_field', ['shortname' => 'pet', 'name' => 'Pet', 'required' => 0,
 143              'visible' => 1, 'locked' => 0, 'categoryid' => 1, 'datatype' => 'text']);
 144  
 145          set_config('field_map_profile_field_pet', 'animal', 'auth_db');
 146          set_config('field_updatelocal_profile_field_pet', 'oncreate', 'auth_db');
 147          set_config('field_updateremote_profile_field_pet', '0', 'auth_db');
 148          set_config('field_lock_profile_field_pet', 'unlocked', 'auth_db');
 149  
 150          // Init the rest of settings.
 151          set_config('passtype', 'plaintext', 'auth_db');
 152          set_config('changepasswordurl', '', 'auth_db');
 153          set_config('debugauthdb', 0, 'auth_db');
 154          set_config('removeuser', AUTH_REMOVEUSER_KEEP, 'auth_db');
 155      }
 156  
 157      protected function cleanup_auth_database() {
 158          global $DB;
 159  
 160          $dbman = $DB->get_manager();
 161          $table = new xmldb_table('auth_db_users');
 162          $dbman->drop_table($table);
 163  
 164          ini_set('error_log', $this->oldlog);
 165      }
 166  
 167      public function test_plugin() {
 168          global $DB, $CFG;
 169          require_once($CFG->dirroot . '/user/profile/lib.php');
 170  
 171          $this->resetAfterTest(true);
 172  
 173          // NOTE: It is strongly discouraged to create new tables in advanced_testcase classes,
 174          //       but there is no other simple way to test ext database enrol sync, so let's
 175          //       disable transactions are try to cleanup after the tests.
 176  
 177          $this->preventResetByRollback();
 178  
 179          $this->init_auth_database();
 180  
 181          /** @var auth_plugin_db $auth */
 182          $auth = get_auth_plugin('db');
 183  
 184          $authdb = $auth->db_init();
 185  
 186  
 187          // Test adodb may access the table.
 188  
 189          $user1 = (object)array('name'=>'u1', 'pass'=>'heslo', 'email'=>'u1@example.com');
 190          $user1->id = $DB->insert_record('auth_db_users', $user1);
 191  
 192  
 193          $sql = "SELECT * FROM {$auth->config->table}";
 194          $rs = $authdb->Execute($sql);
 195          $this->assertInstanceOf('ADORecordSet', $rs);
 196          $this->assertFalse($rs->EOF);
 197          $fields = $rs->FetchRow();
 198          $this->assertTrue(is_array($fields));
 199          $this->assertTrue($rs->EOF);
 200          $rs->Close();
 201  
 202          $authdb->Close();
 203  
 204  
 205          // Test bulk user account creation.
 206  
 207          $user2 = (object)['name' => 'u2', 'pass' => 'heslo', 'email' => 'u2@example.com', 'animal' => 'cat'];
 208          $user2->id = $DB->insert_record('auth_db_users', $user2);
 209  
 210          $user3 = (object)array('name'=>'admin', 'pass'=>'heslo', 'email'=>'admin@example.com'); // Should be skipped.
 211          $user3->id = $DB->insert_record('auth_db_users', $user3);
 212  
 213          $this->assertCount(2, $DB->get_records('user'));
 214  
 215          $trace = new null_progress_trace();
 216  
 217          // Sync users and make sure that two events user_created werer triggered.
 218          $sink = $this->redirectEvents();
 219          $auth->sync_users($trace, false);
 220          $events = $sink->get_events();
 221          $sink->close();
 222          $this->assertCount(2, $events);
 223          $this->assertTrue($events[0] instanceof  \core\event\user_created);
 224          $this->assertTrue($events[1] instanceof  \core\event\user_created);
 225  
 226          // Assert the two users were created.
 227          $this->assertEquals(4, $DB->count_records('user'));
 228          $u1 = $DB->get_record('user', array('username'=>$user1->name, 'auth'=>'db'));
 229          $this->assertSame($user1->email, $u1->email);
 230          $this->assertEmpty(profile_user_record($u1->id)->pet);
 231          $u2 = $DB->get_record('user', array('username'=>$user2->name, 'auth'=>'db'));
 232          $this->assertSame($user2->email, $u2->email);
 233          $this->assertSame($user2->animal, profile_user_record($u2->id)->pet);
 234          $admin = $DB->get_record('user', array('username'=>'admin', 'auth'=>'manual'));
 235          $this->assertNotEmpty($admin);
 236  
 237  
 238          // Test sync updates.
 239  
 240          $user2b = clone($user2);
 241          $user2b->email = 'u2b@example.com';
 242          $user2b->animal = 'dog';
 243          $DB->update_record('auth_db_users', $user2b);
 244  
 245          $auth->sync_users($trace, false);
 246          $this->assertEquals(4, $DB->count_records('user'));
 247          $u2 = $DB->get_record('user', array('username'=>$user2->name));
 248          $this->assertSame($user2->email, $u2->email);
 249          $this->assertSame($user2->animal, profile_user_record($u2->id)->pet);
 250  
 251          $auth->sync_users($trace, true);
 252          $this->assertEquals(4, $DB->count_records('user'));
 253          $u2 = $DB->get_record('user', array('username'=>$user2->name));
 254          $this->assertSame($user2->email, $u2->email);
 255  
 256          set_config('field_updatelocal_email', 'onlogin', 'auth_db');
 257          $auth->config->field_updatelocal_email = 'onlogin';
 258          set_config('field_updatelocal_profile_field_pet', 'onlogin', 'auth_db');
 259          $auth->config->field_updatelocal_profile_field_pet = 'onlogin';
 260  
 261          $auth->sync_users($trace, false);
 262          $this->assertEquals(4, $DB->count_records('user'));
 263          $u2 = $DB->get_record('user', array('username'=>$user2->name));
 264          $this->assertSame($user2->email, $u2->email);
 265  
 266          $auth->sync_users($trace, true);
 267          $this->assertEquals(4, $DB->count_records('user'));
 268          $u2 = $DB->get_record('user', array('username'=>$user2->name));
 269          $this->assertSame($user2b->email, $u2->email);
 270          $this->assertSame($user2b->animal, profile_user_record($u2->id)->pet);
 271  
 272  
 273          // Test sync deletes and suspends.
 274  
 275          $DB->delete_records('auth_db_users', array('id'=>$user2->id));
 276          $this->assertCount(2, $DB->get_records('auth_db_users'));
 277          unset($user2);
 278          unset($user2b);
 279  
 280          $auth->sync_users($trace, false);
 281          $this->assertEquals(4, $DB->count_records('user'));
 282          $this->assertEquals(0, $DB->count_records('user', array('deleted'=>1)));
 283          $this->assertEquals(0, $DB->count_records('user', array('suspended'=>1)));
 284  
 285          set_config('removeuser', AUTH_REMOVEUSER_SUSPEND, 'auth_db');
 286          $auth->config->removeuser = AUTH_REMOVEUSER_SUSPEND;
 287  
 288          $auth->sync_users($trace, false);
 289          $this->assertEquals(4, $DB->count_records('user'));
 290          $this->assertEquals(0, $DB->count_records('user', array('deleted'=>1)));
 291          $this->assertEquals(1, $DB->count_records('user', array('suspended'=>1)));
 292  
 293          $user2 = (object)array('name'=>'u2', 'pass'=>'heslo', 'email'=>'u2@example.com');
 294          $user2->id = $DB->insert_record('auth_db_users', $user2);
 295  
 296          $auth->sync_users($trace, false);
 297          $this->assertEquals(4, $DB->count_records('user'));
 298          $this->assertEquals(0, $DB->count_records('user', array('deleted'=>1)));
 299          $this->assertEquals(0, $DB->count_records('user', array('suspended'=>1)));
 300  
 301          $DB->delete_records('auth_db_users', array('id'=>$user2->id));
 302  
 303          set_config('removeuser', AUTH_REMOVEUSER_FULLDELETE, 'auth_db');
 304          $auth->config->removeuser = AUTH_REMOVEUSER_FULLDELETE;
 305  
 306          $auth->sync_users($trace, false);
 307          $this->assertEquals(4, $DB->count_records('user'));
 308          $this->assertEquals(1, $DB->count_records('user', array('deleted'=>1)));
 309          $this->assertEquals(0, $DB->count_records('user', array('suspended'=>1)));
 310  
 311          $user2 = (object)array('name'=>'u2', 'pass'=>'heslo', 'email'=>'u2@example.com');
 312          $user2->id = $DB->insert_record('auth_db_users', $user2);
 313  
 314          $auth->sync_users($trace, false);
 315          $this->assertEquals(5, $DB->count_records('user'));
 316          $this->assertEquals(1, $DB->count_records('user', array('deleted'=>1)));
 317          $this->assertEquals(0, $DB->count_records('user', array('suspended'=>1)));
 318  
 319  
 320          // Test user_login().
 321  
 322          $user3 = (object)array('name'=>'u3', 'pass'=>'heslo', 'email'=>'u3@example.com');
 323          $user3->id = $DB->insert_record('auth_db_users', $user3);
 324  
 325          $this->assertFalse($auth->user_login('u4', 'heslo'));
 326          $this->assertTrue($auth->user_login('u1', 'heslo'));
 327  
 328          $this->assertFalse($DB->record_exists('user', array('username'=>'u3', 'auth'=>'db')));
 329          $this->assertTrue($auth->user_login('u3', 'heslo'));
 330          $this->assertFalse($DB->record_exists('user', array('username'=>'u3', 'auth'=>'db')));
 331  
 332          set_config('passtype', 'md5', 'auth_db');
 333          $auth->config->passtype = 'md5';
 334          $user3->pass = md5('heslo');
 335          $DB->update_record('auth_db_users', $user3);
 336          $this->assertTrue($auth->user_login('u3', 'heslo'));
 337  
 338          // Test user created to see if the checking happens strictly.
 339          $usermd5 = (object)['name' => 'usermd5', 'pass' => '0e462097431906509019562988736854'];
 340          $usermd5->id = $DB->insert_record('auth_db_users', $usermd5);
 341  
 342          // md5('240610708') === '0e462097431906509019562988736854'.
 343          $this->assertTrue($auth->user_login('usermd5', '240610708'));
 344          $this->assertFalse($auth->user_login('usermd5', 'QNKCDZO'));
 345  
 346          set_config('passtype', 'sh1', 'auth_db');
 347          $auth->config->passtype = 'sha1';
 348          $user3->pass = sha1('heslo');
 349          $DB->update_record('auth_db_users', $user3);
 350          $this->assertTrue($auth->user_login('u3', 'heslo'));
 351  
 352          // Test user created to see if the checking happens strictly.
 353          $usersha1 = (object)['name' => 'usersha1', 'pass' => '0e66507019969427134894567494305185566735'];
 354          $usersha1->id = $DB->insert_record('auth_db_users', $usersha1);
 355  
 356          // sha1('aaroZmOk') === '0e66507019969427134894567494305185566735'.
 357          $this->assertTrue($auth->user_login('usersha1', 'aaroZmOk'));
 358          $this->assertFalse($auth->user_login('usersha1', 'aaK1STfY'));
 359  
 360          set_config('passtype', 'saltedcrypt', 'auth_db');
 361          $auth->config->passtype = 'saltedcrypt';
 362          $user3->pass = password_hash('heslo', PASSWORD_BCRYPT);
 363          $DB->update_record('auth_db_users', $user3);
 364          $this->assertTrue($auth->user_login('u3', 'heslo'));
 365  
 366          set_config('passtype', 'internal', 'auth_db');
 367          $auth->config->passtype = 'internal';
 368          create_user_record('u3', 'heslo', 'db');
 369          $this->assertTrue($auth->user_login('u3', 'heslo'));
 370  
 371  
 372          $DB->delete_records('auth_db_users', array('id'=>$user3->id));
 373  
 374          set_config('removeuser', AUTH_REMOVEUSER_KEEP, 'auth_db');
 375          $auth->config->removeuser = AUTH_REMOVEUSER_KEEP;
 376          $this->assertTrue($auth->user_login('u3', 'heslo'));
 377  
 378          set_config('removeuser', AUTH_REMOVEUSER_SUSPEND, 'auth_db');
 379          $auth->config->removeuser = AUTH_REMOVEUSER_SUSPEND;
 380          $this->assertFalse($auth->user_login('u3', 'heslo'));
 381  
 382          set_config('removeuser', AUTH_REMOVEUSER_FULLDELETE, 'auth_db');
 383          $auth->config->removeuser = AUTH_REMOVEUSER_FULLDELETE;
 384          $this->assertFalse($auth->user_login('u3', 'heslo'));
 385  
 386          set_config('passtype', 'sh1', 'auth_db');
 387          $auth->config->passtype = 'sha1';
 388          $this->assertFalse($auth->user_login('u3', 'heslo'));
 389  
 390  
 391          // Test login create and update.
 392  
 393          $user4 = (object)array('name'=>'u4', 'pass'=>'heslo', 'email'=>'u4@example.com');
 394          $user4->id = $DB->insert_record('auth_db_users', $user4);
 395  
 396          set_config('passtype', 'plaintext', 'auth_db');
 397          $auth->config->passtype = 'plaintext';
 398  
 399          $iuser4 = create_user_record('u4', 'heslo', 'db');
 400          $this->assertNotEmpty($iuser4);
 401          $this->assertSame($user4->name, $iuser4->username);
 402          $this->assertSame($user4->email, $iuser4->email);
 403          $this->assertSame('db', $iuser4->auth);
 404          $this->assertSame($CFG->mnet_localhost_id, $iuser4->mnethostid);
 405  
 406          $user4b = clone($user4);
 407          $user4b->email = 'u4b@example.com';
 408          $DB->update_record('auth_db_users', $user4b);
 409  
 410          set_config('field_updatelocal_email', 'oncreate', 'auth_db');
 411          $auth->config->field_updatelocal_email = 'oncreate';
 412  
 413          update_user_record('u4');
 414          $iuser4 = $DB->get_record('user', array('id'=>$iuser4->id));
 415          $this->assertSame($user4->email, $iuser4->email);
 416  
 417          set_config('field_updatelocal_email', 'onlogin', 'auth_db');
 418          $auth->config->field_updatelocal_email = 'onlogin';
 419  
 420          update_user_record('u4');
 421          $iuser4 = $DB->get_record('user', array('id'=>$iuser4->id));
 422          $this->assertSame($user4b->email, $iuser4->email);
 423  
 424  
 425          // Test user_exists()
 426  
 427          $this->assertTrue($auth->user_exists('u1'));
 428          $this->assertTrue($auth->user_exists('admin'));
 429          $this->assertFalse($auth->user_exists('u3'));
 430          $this->assertTrue($auth->user_exists('u4'));
 431  
 432          $this->cleanup_auth_database();
 433      }
 434  
 435      /**
 436       * Testing the function _colonscope() from ADOdb.
 437       */
 438      public function test_adodb_colonscope() {
 439          global $CFG;
 440          require_once($CFG->libdir.'/adodb/adodb.inc.php');
 441          require_once($CFG->libdir.'/adodb/drivers/adodb-odbc.inc.php');
 442          require_once($CFG->libdir.'/adodb/drivers/adodb-db2ora.inc.php');
 443  
 444          $this->resetAfterTest(false);
 445  
 446          $sql = "select * from table WHERE column=:1 AND anothercolumn > :0";
 447          $arr = array('b', 1);
 448          list($sqlout, $arrout) = _colonscope($sql,$arr);
 449          $this->assertEquals("select * from table WHERE column=? AND anothercolumn > ?", $sqlout);
 450          $this->assertEquals(array(1, 'b'), $arrout);
 451      }
 452  
 453      /**
 454       * Testing the clean_data() method.
 455       */
 456      public function test_clean_data() {
 457          global $DB;
 458  
 459          $this->resetAfterTest(false);
 460          $this->preventResetByRollback();
 461          $this->init_auth_database();
 462          $auth = get_auth_plugin('db');
 463          $auth->db_init();
 464  
 465          // Create users on external table.
 466          $extdbuser1 = (object)array('name'=>'u1', 'pass'=>'heslo', 'email'=>'u1@example.com');
 467          $extdbuser1->id = $DB->insert_record('auth_db_users', $extdbuser1);
 468  
 469          // User with malicious data on the name (won't be imported).
 470          $extdbuser2 = (object)array('name'=>'user<script>alert(1);</script>xss', 'pass'=>'heslo', 'email'=>'xssuser@example.com');
 471          $extdbuser2->id = $DB->insert_record('auth_db_users', $extdbuser2);
 472  
 473          $extdbuser3 = (object)array('name'=>'u3', 'pass'=>'heslo', 'email'=>'u3@example.com',
 474                  'lastname' => 'user<script>alert(1);</script>xss');
 475          $extdbuser3->id = $DB->insert_record('auth_db_users', $extdbuser3);
 476          $trace = new null_progress_trace();
 477  
 478          // Let's test user sync make sure still works as expected..
 479          $auth->sync_users($trace, true);
 480          $this->assertDebuggingCalled("The property 'lastname' has invalid data and has been cleaned.");
 481          // User with correct data, should be equal to external db.
 482          $user1 = $DB->get_record('user', array('email'=> $extdbuser1->email, 'auth'=>'db'));
 483          $this->assertEquals($extdbuser1->name, $user1->username);
 484          $this->assertEquals($extdbuser1->email, $user1->email);
 485  
 486          // Get the user on moodle user table.
 487          $user2 = $DB->get_record('user', array('email'=> $extdbuser2->email, 'auth'=>'db'));
 488          $user3 = $DB->get_record('user', array('email'=> $extdbuser3->email, 'auth'=>'db'));
 489  
 490          $this->assertEmpty($user2);
 491          $this->assertEquals($extdbuser3->name, $user3->username);
 492          $this->assertEquals('useralert(1);xss', $user3->lastname);
 493  
 494          $this->cleanup_auth_database();
 495      }
 496  
 497      /**
 498       * Testing the deletion of a user when there are many users in the external DB.
 499       */
 500      public function test_deleting_with_many_users() {
 501          global $DB;
 502  
 503          $this->resetAfterTest(true);
 504          $this->preventResetByRollback();
 505          $this->init_auth_database();
 506          $auth = get_auth_plugin('db');
 507          $auth->db_init();
 508  
 509          // Set to delete from moodle when missing from DB.
 510          set_config('removeuser', AUTH_REMOVEUSER_FULLDELETE, 'auth_db');
 511          $auth->config->removeuser = AUTH_REMOVEUSER_FULLDELETE;
 512  
 513          // Create users.
 514          $users = [];
 515          for ($i = 0; $i < $this->largedeletionsetsize; $i++) {
 516              $user = (object)array('username' => "u$i", 'name' => "u$i", 'pass' => 'heslo', 'email' => "u$i@example.com");
 517              $user->id  = $DB->insert_record('auth_db_users', $user);
 518              $users[] = $user;
 519          }
 520  
 521          // Sync to moodle.
 522          $trace = new null_progress_trace();
 523          $auth->sync_users($trace, true);
 524  
 525          // Check user is there.
 526          $user = array_shift($users);
 527          $moodleuser = $DB->get_record('user', array('email' => $user->email, 'auth' => 'db'));
 528          $this->assertNotNull($moodleuser);
 529          $this->assertEquals($user->username, $moodleuser->username);
 530  
 531          // Delete a user.
 532          $DB->delete_records('auth_db_users', array('id' => $user->id));
 533  
 534          // Sync again.
 535          $auth->sync_users($trace, true);
 536  
 537          // Check user is no longer there.
 538          $moodleuser = $DB->get_record('user', array('id' => $moodleuser->id));
 539          $this->assertFalse($auth->user_login($user->username, 'heslo'));
 540          $this->assertEquals(1, $moodleuser->deleted);
 541  
 542          // Make sure it was the only user deleted.
 543          $numberdeleted = $DB->count_records('user', array('deleted' => 1, 'auth' => 'db'));
 544          $this->assertEquals(1, $numberdeleted);
 545  
 546          $this->cleanup_auth_database();
 547      }
 548  }