Skip to content

Commit

Permalink
MDL-57268 auth_db: Unit tests for deletion from a large user set
Browse files Browse the repository at this point in the history
  • Loading branch information
John Okely authored and stronk7 committed Dec 20, 2016
1 parent 1148877 commit 25474b5
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions auth/db/tests/db_test.php
Expand Up @@ -31,6 +31,9 @@ class auth_db_testcase extends advanced_testcase {
/** @var string Original error log */
protected $oldlog;

/** @var int The amount of users to create for the large user set deletion test */
protected $largedeletionsetsize = 128;

protected function init_auth_database() {
global $DB, $CFG;
require_once("$CFG->dirroot/auth/db/auth.php");
Expand Down Expand Up @@ -444,4 +447,56 @@ public function test_clean_data() {

$this->cleanup_auth_database();
}

/**
* Testing the deletion of a user when there are many users in the external DB.
*/
public function test_deleting_with_many_users() {
global $DB;

$this->resetAfterTest(true);
$this->preventResetByRollback();
$this->init_auth_database();
$auth = get_auth_plugin('db');
$auth->db_init();

// Set to delete from moodle when missing from DB.
set_config('removeuser', AUTH_REMOVEUSER_FULLDELETE, 'auth/db');
$auth->config->removeuser = AUTH_REMOVEUSER_FULLDELETE;

// Create users.
$users = [];
for ($i = 0; $i < $this->largedeletionsetsize; $i++) {
$user = (object)array('username' => "u$i", 'name' => "u$i", 'pass' => 'heslo', 'email' => "u$i@example.com");
$user->id = $DB->insert_record('auth_db_users', $user);
$users[] = $user;
}

// Sync to moodle.
$trace = new null_progress_trace();
$auth->sync_users($trace, true);

// Check user is there.
$user = array_shift($users);
$moodleuser = $DB->get_record('user', array('email' => $user->email, 'auth' => 'db'));
$this->assertNotNull($moodleuser);
$this->assertEquals($user->username, $moodleuser->username);

// Delete a user.
$DB->delete_records('auth_db_users', array('id' => $user->id));

// Sync again.
$auth->sync_users($trace, true);

// Check user is no longer there.
$moodleuser = $DB->get_record('user', array('id' => $moodleuser->id));
$this->assertFalse($auth->user_login($user->username, 'heslo'));
$this->assertEquals(1, $moodleuser->deleted);

// Make sure it was the only user deleted.
$numberdeleted = $DB->count_records('user', array('deleted' => 1, 'auth' => 'db'));
$this->assertEquals(1, $numberdeleted);

$this->cleanup_auth_database();
}
}

0 comments on commit 25474b5

Please sign in to comment.