Merge branch 'w42_MDL-42224_m26_msgredir' of https://github.com/skodak/moodle
[moodle.git] / lib / tests / moodlelib_test.php
index 1a81bc9..48ec5fd 100644 (file)
@@ -2535,4 +2535,44 @@ class core_moodlelib_testcase extends advanced_testcase {
         email_to_user($user1, $user2, $subject, $messagetext);
         $this->assertDebuggingCalled('Unit tests must not send real emails! Use $this->redirectEmails()');
     }
+
+    /**
+     * Test user_updated event trigger by various apis.
+     */
+    public function test_user_updated_event() {
+        global $DB, $CFG;
+
+        $this->resetAfterTest();
+
+        $user = $this->getDataGenerator()->create_user();
+
+        // Set config to allow email_to_user() to be called.
+        $CFG->noemailever = false;
+
+        // Update user password.
+        $sink = $this->redirectEvents();
+        $sink2 = $this->redirectEmails(); // Make sure we are redirecting emails.
+        setnew_password_and_mail($user);
+        update_internal_user_password($user, 'randompass');
+        $events = $sink->get_events();
+        $sink->close();
+        $sink2->close();
+
+        // Test updated value.
+        $dbuser = $DB->get_record('user', array('id' => $user->id));
+        $this->assertSame($user->firstname, $dbuser->firstname);
+        $this->assertNotSame('M00dLe@T', $dbuser->password);
+
+        // Test event.
+        foreach ($events as $event) {
+            $this->assertInstanceOf('\core\event\user_updated', $event);
+            $this->assertSame($user->id, $event->objectid);
+            $this->assertSame('user_updated', $event->get_legacy_eventname());
+            $this->assertEventLegacyData($user, $event);
+            $this->assertEquals(context_user::instance($user->id), $event->get_context());
+            $expectedlogdata = array(SITEID, 'user', 'update', 'view.php?id='.$user->id, '');
+            $this->assertEventLegacyLogData($expectedlogdata, $event);
+        }
+    }
+
 }