Skip to content

Commit

Permalink
MDL-49022 auth_ldap: Updated unit test, checking sync_user events
Browse files Browse the repository at this point in the history
  • Loading branch information
Rajesh Taneja committed Jun 23, 2015
1 parent 7b9643b commit 5ee54f8
Showing 1 changed file with 66 additions and 0 deletions.
66 changes: 66 additions & 0 deletions auth/ldap/tests/plugin_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,25 @@ public function test_auth_ldap() {
$auth = get_auth_plugin('ldap');

ob_start();
$sink = $this->redirectEvents();
$auth->sync_users(true);
$events = $sink->get_events();
$sink->close();
ob_end_clean();

// Check events, 5 users created with 2 users having roles.
$this->assertCount(7, $events);
foreach ($events as $index => $event) {
$usercreatedindex = array(0, 2, 4, 5, 6);
$roleassignedindex = array (1, 3);
if (in_array($index, $usercreatedindex)) {
$this->assertInstanceOf('\core\event\user_created', $event);
}
if (in_array($index, $roleassignedindex)) {
$this->assertInstanceOf('\core\event\role_assigned', $event);
}
}

$this->assertEquals(5, $DB->count_records('user', array('auth'=>'ldap')));
$this->assertEquals(2, $DB->count_records('role_assignments'));
$this->assertEquals(2, $DB->count_records('role_assignments', array('roleid'=>$creatorrole->id)));
Expand All @@ -150,9 +166,15 @@ public function test_auth_ldap() {
$this->delete_ldap_user($connection, $topdn, 1);

ob_start();
$sink = $this->redirectEvents();
$auth->sync_users(true);
$events = $sink->get_events();
$sink->close();
ob_end_clean();

// Check events, no new event.
$this->assertCount(0, $events);

$this->assertEquals(5, $DB->count_records('user', array('auth'=>'ldap')));
$this->assertEquals(0, $DB->count_records('user', array('suspended'=>1)));
$this->assertEquals(0, $DB->count_records('user', array('deleted'=>1)));
Expand All @@ -166,9 +188,17 @@ public function test_auth_ldap() {
$auth = get_auth_plugin('ldap');

ob_start();
$sink = $this->redirectEvents();
$auth->sync_users(true);
$events = $sink->get_events();
$sink->close();
ob_end_clean();

// Check events, 1 user got updated.
$this->assertCount(1, $events);
$event = reset($events);
$this->assertInstanceOf('\core\event\user_updated', $event);

$this->assertEquals(5, $DB->count_records('user', array('auth'=>'ldap')));
$this->assertEquals(0, $DB->count_records('user', array('auth'=>'nologin', 'username'=>'username1')));
$this->assertEquals(1, $DB->count_records('user', array('auth'=>'ldap', 'suspended'=>'1', 'username'=>'username1')));
Expand All @@ -179,9 +209,17 @@ public function test_auth_ldap() {
$this->create_ldap_user($connection, $topdn, 1);

ob_start();
$sink = $this->redirectEvents();
$auth->sync_users(true);
$events = $sink->get_events();
$sink->close();
ob_end_clean();

// Check events, 1 user got updated.
$this->assertCount(1, $events);
$event = reset($events);
$this->assertInstanceOf('\core\event\user_updated', $event);

$this->assertEquals(5, $DB->count_records('user', array('auth'=>'ldap')));
$this->assertEquals(0, $DB->count_records('user', array('suspended'=>1)));
$this->assertEquals(0, $DB->count_records('user', array('deleted'=>1)));
Expand All @@ -191,9 +229,17 @@ public function test_auth_ldap() {
$DB->set_field('user', 'auth', 'nologin', array('username'=>'username1'));

ob_start();
$sink = $this->redirectEvents();
$auth->sync_users(true);
$events = $sink->get_events();
$sink->close();
ob_end_clean();

// Check events, 1 user got updated.
$this->assertCount(1, $events);
$event = reset($events);
$this->assertInstanceOf('\core\event\user_updated', $event);

$this->assertEquals(5, $DB->count_records('user', array('auth'=>'ldap')));
$this->assertEquals(0, $DB->count_records('user', array('suspended'=>1)));
$this->assertEquals(0, $DB->count_records('user', array('deleted'=>1)));
Expand All @@ -208,9 +254,19 @@ public function test_auth_ldap() {
$this->delete_ldap_user($connection, $topdn, 1);

ob_start();
$sink = $this->redirectEvents();
$auth->sync_users(true);
$events = $sink->get_events();
$sink->close();
ob_end_clean();

// Check events, 2 events role_unassigned and user_deleted.
$this->assertCount(2, $events);
$event = array_pop($events);
$this->assertInstanceOf('\core\event\user_deleted', $event);
$event = array_pop($events);
$this->assertInstanceOf('\core\event\role_unassigned', $event);

$this->assertEquals(5, $DB->count_records('user', array('auth'=>'ldap')));
$this->assertEquals(0, $DB->count_records('user', array('username'=>'username1')));
$this->assertEquals(0, $DB->count_records('user', array('suspended'=>1)));
Expand All @@ -221,9 +277,19 @@ public function test_auth_ldap() {
$this->create_ldap_user($connection, $topdn, 1);

ob_start();
$sink = $this->redirectEvents();
$auth->sync_users(true);
$events = $sink->get_events();
$sink->close();
ob_end_clean();

// Check events, 2 events role_assigned and user_created.
$this->assertCount(2, $events);
$event = array_pop($events);
$this->assertInstanceOf('\core\event\role_assigned', $event);
$event = array_pop($events);
$this->assertInstanceOf('\core\event\user_created', $event);

$this->assertEquals(6, $DB->count_records('user', array('auth'=>'ldap')));
$this->assertEquals(1, $DB->count_records('user', array('username'=>'username1')));
$this->assertEquals(0, $DB->count_records('user', array('suspended'=>1)));
Expand Down

0 comments on commit 5ee54f8

Please sign in to comment.