diff --git a/auth/ldap/tests/plugin_test.php b/auth/ldap/tests/plugin_test.php index b468fde1554..296948b36d6 100644 --- a/auth/ldap/tests/plugin_test.php +++ b/auth/ldap/tests/plugin_test.php @@ -236,6 +236,73 @@ class auth_ldap_plugin_testcase extends advanced_testcase { ldap_close($connection); } + /** + * Test logging in via LDAP calls a user_loggedin event. + */ + public function test_ldap_user_loggedin_event() { + global $CFG, $DB, $USER; + + require_once($CFG->dirroot . '/auth/ldap/auth.php'); + + $this->resetAfterTest(); + + $this->setAdminUser(); + + $user = clone($USER); + + // The USER variable no longer stores the password hash, so set it here. + $user->password = 'password'; + + // Note: we are just going to trigger the function that calls the event, + // not actually perform a LDAP login, for the sake of sanity. + $ldap = new auth_plugin_ldap(); + + // Set the key for the cache flag we want to set which is used by LDAP. + set_cache_flag($ldap->pluginconfig . '/ntlmsess', sesskey(), $user->username, AUTH_NTLMTIMEOUT); + + // We are going to need to set the sesskey as the user's password in order for the LDAP log in to work. + update_internal_user_password($user, sesskey()); + + // The function ntlmsso_finish is responsible for triggering the event, so call it directly and catch the event. + $sink = $this->redirectEvents(); + // We need to supress this function call, or else we will get the message "session_regenerate_id(): Cannot + // regenerate session id - headers already sent" as the ntlmsso_finish function calls complete_user_login + @$ldap->ntlmsso_finish(); + $events = $sink->get_events(); + $sink->close(); + + // Unset the password now. + unset($user->password); + + // Get the user from the DB and set the expected variables. + $dbuser = $DB->get_record('user', array('id' => $user->id), '*', MUST_EXIST); + $user->firstaccess = (int) $dbuser->firstaccess; + $user->lastaccess = (int) $dbuser->lastaccess; + $user->currentlogin = (int) $dbuser->currentlogin; + $user->sesskey = sesskey(); + $user->lastcourseaccess = array(); + $user->currentcourseaccess = array(); + $user->groupmember = array(); + $user->profile = array(); + $user->preference = array( + '_lastloaded' => time() + ); + + // Check that the event is valid. + $this->assertCount(2, $events); + $event = $events[0]; + $this->assertInstanceOf('\core\event\user_updated', $event); + $event = $events[1]; + $this->assertInstanceOf('\core\event\user_loggedin', $event); + $this->assertEquals('user', $event->objecttable); + $this->assertEquals('2', $event->objectid); + $this->assertEquals(context_system::instance()->id, $event->contextid); + $this->assertEquals($user, $event->get_record_snapshot('user', 2)); + $expectedlog = array(SITEID, 'user', 'login', 'view.php?id=' . $USER->id . '&course=' . SITEID, $user->id, + 0, $user->id); + $this->assertEventLegacyLogData($expectedlog, $event); + } + protected function create_ldap_user($connection, $topdn, $i) { $o = array(); $o['objectClass'] = array('inetOrgPerson', 'organizationalPerson', 'person', 'posixAccount');