From 69ddeb42e2358f8dbe7b9772da66cfe82bd69676 Mon Sep 17 00:00:00 2001 From: Mark Nelson Date: Sun, 29 Jun 2014 17:14:10 -0700 Subject: [PATCH 1/2] MDL-41389 auth_db: added user_updated/created event triggers --- auth/db/auth.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/auth/db/auth.php b/auth/db/auth.php index 3b80e2676a7..e35b94df5fc 100644 --- a/auth/db/auth.php +++ b/auth/db/auth.php @@ -384,6 +384,10 @@ class auth_plugin_db extends auth_plugin_base { if ($old_user = $DB->get_record('user', array('username'=>$username, 'deleted'=>0, 'suspended'=>1, 'mnethostid'=>$CFG->mnet_localhost_id, 'auth'=>$this->authtype))) { $DB->set_field('user', 'suspended', 0, array('id'=>$old_user->id)); $trace->output(get_string('auth_dbreviveduser', 'auth_db', array('name'=>$username, 'id'=>$old_user->id)), 1); + + // Trigger user_updated event. + \core\event\user_updated::create_from_userid($old_user->id)->trigger(); + continue; } } @@ -410,6 +414,10 @@ class auth_plugin_db extends auth_plugin_base { } try { $id = $DB->insert_record ('user', $user); // it is truly a new user + + // Trigger user_created event. + \core\event\user_created::create_from_userid($id)->trigger(); + $trace->output(get_string('auth_dbinsertuser', 'auth_db', array('name'=>$user->username, 'id'=>$id)), 1); } catch (moodle_exception $e) { $trace->output(get_string('auth_dbinsertusererror', 'auth_db', $user->username), 1); @@ -547,6 +555,9 @@ class auth_plugin_db extends auth_plugin_base { } if ($updated) { $DB->set_field('user', 'timemodified', time(), array('id'=>$userid)); + + // Trigger user_updated event. + \core\event\user_updated::create_from_userid($userid)->trigger(); } return $DB->get_record('user', array('id'=>$userid, 'deleted'=>0)); } From a93f8344ae16cd5ba6bc28921126266b33163b9e Mon Sep 17 00:00:00 2001 From: Mark Nelson Date: Thu, 3 Jul 2014 14:52:16 -0700 Subject: [PATCH 2/2] MDL-41389 auth_db: added unit tests for events --- auth/db/tests/db_test.php | 2 +- auth/db/tests/events_test.php | 150 ++++++++++++++++++++++++++++++++++ 2 files changed, 151 insertions(+), 1 deletion(-) create mode 100644 auth/db/tests/events_test.php diff --git a/auth/db/tests/db_test.php b/auth/db/tests/db_test.php index 64f34f30de1..f365f139edd 100644 --- a/auth/db/tests/db_test.php +++ b/auth/db/tests/db_test.php @@ -31,7 +31,7 @@ class auth_db_testcase extends advanced_testcase { /** @var string Original error log */ protected $oldlog; - protected function init_auth_database() { + public function init_auth_database() { global $DB, $CFG; require_once("$CFG->dirroot/auth/db/auth.php"); diff --git a/auth/db/tests/events_test.php b/auth/db/tests/events_test.php new file mode 100644 index 00000000000..46e4408edb1 --- /dev/null +++ b/auth/db/tests/events_test.php @@ -0,0 +1,150 @@ +. + +/** + * Events tests. + * + * @package auth_db + * @category test + * @copyright 2014 Mark Nelson + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +global $CFG; + +require_once($CFG->dirroot . '/auth/db/tests/db_test.php'); + +class auth_db_events_testcase extends advanced_testcase { + + /** + * Test set up. + */ + public function setUp() { + $this->resetAfterTest(true); + } + + /** + * Tests that the locations in the auth_db API that create a user trigger the user_created event. + */ + public function test_user_created() { + global $DB; + + $this->preventResetByRollback(); + + // Initialise the database. + $authdbtestcase = new auth_db_testcase(); + $authdbtestcase->init_auth_database(); + + $auth = get_auth_plugin('db'); + $auth->db_init(); + + // Add a user to the auth_db_users table - we will then call sync_users to + // deal with the record here. In this case it will create the user. + $user = new stdClass(); + $user->name = 'mark'; + $user->pass = 'password123'; + $user->email = 'what@legend.com'; + $user->id = $DB->insert_record('auth_db_users', $user); + + // Run sync_users and capture the user_created event. + $sink = $this->redirectEvents(); + $trace = new null_progress_trace(); + $auth->sync_users($trace, false); + $events = $sink->get_events(); + $sink->close(); + + // Check that there is only one event. + $this->assertEquals(1, count($events)); + + // Get the event. + $event = array_pop($events); + + // Test that the user created event was triggered - no need to test the other + // details of the event as that is done extensively in other unit tests. + $this->assertInstanceOf('\core\event\user_created', $event); + } + + /** + * Tests that the locations in the auth_db API that update a user trigger the user_updated event. + */ + public function test_user_updated() { + global $CFG, $DB; + + $this->preventResetByRollback(); + + // Initialise the database. + $authdbtestcase = new auth_db_testcase(); + $authdbtestcase->init_auth_database(); + + $auth = get_auth_plugin('db'); + $auth->db_init(); + + // Add a suspended user. + $user = array(); + $user['username'] = 'mark'; + $user['suspended'] = '1'; + $user['mnethostid'] = $CFG->mnet_localhost_id; + $user['auth'] = 'db'; + $this->getDataGenerator()->create_user($user); + + // Add a user to the auth_db_users table - we will then call sync_users to + // deal with the record here. In this case it will un-suspend the user. + $user = new stdClass(); + $user->name = 'mark'; + $user->pass = 'password123'; + $user->email = 'what@legend.com'; + $user->id = $DB->insert_record('auth_db_users', $user); + + // Set the config to remove the suspension on the user. + set_config('removeuser', AUTH_REMOVEUSER_SUSPEND, 'auth/db'); + $auth->config->removeuser = AUTH_REMOVEUSER_SUSPEND; + + // Run sync_users and capture the user_updated event. + $sink = $this->redirectEvents(); + $trace = new null_progress_trace(); + $auth->sync_users($trace, false); + $events = $sink->get_events(); + $sink->close(); + + // Check that there is only one event. + $this->assertEquals(1, count($events)); + + // Get the event. + $event = array_pop($events); + + // Test that the user updated event was triggered - no need to test the other + // details of the event as that is done extensively in other unit tests. + $this->assertInstanceOf('\core\event\user_updated', $event); + + // Run sync_users and capture the user_updated event. + $sink = $this->redirectEvents(); + $auth->update_user_record('mark'); + $events = $sink->get_events(); + $sink->close(); + + // Check that there is only one event. + $this->assertEquals(1, count($events)); + + // Get the event. + $event = array_pop($events); + + // Test that the user updated event was triggered - no need to test the other + // details of the event as that is done extensively in other unit tests. + $this->assertInstanceOf('\core\event\user_updated', $event); + } +}