MDL-45566 message: Send blocking events when user is not in contact list
authorFrederic Massart <fred@moodle.com>
Thu, 15 May 2014 08:08:46 +0000 (16:08 +0800)
committerDan Poltawski <dan@moodle.com>
Tue, 20 May 2014 05:17:58 +0000 (13:17 +0800)
message/lib.php
message/tests/events_test.php

index 9d8b974..6954ece 100644 (file)
@@ -1043,13 +1043,19 @@ function message_add_contact($contactid, $blocked=0) {
         $contact->blocked = $blocked;
         $contact->id = $DB->insert_record('message_contacts', $contact);
 
-        // Trigger event for adding a contact.
-        $event = \core\event\message_contact_added::create(array(
+        $eventparams = array(
             'objectid' => $contact->id,
             'userid' => $contact->userid,
             'relateduserid' => $contact->contactid,
             'context'  => context_user::instance($contact->userid)
-        ));
+        );
+
+        if ($blocked) {
+            $event = \core\event\message_contact_blocked::create($eventparams);
+        } else {
+            $event = \core\event\message_contact_added::create($eventparams);
+        }
+        // Trigger event.
         $event->trigger();
 
         return true;
index 2486b03..0a8699e 100644 (file)
@@ -100,6 +100,7 @@ class core_message_events_testcase extends advanced_testcase {
 
         // Create a user to add to the admin's contact list.
         $user = $this->getDataGenerator()->create_user();
+        $user2 = $this->getDataGenerator()->create_user();
 
         // Add the user to the admin's contact list.
         message_add_contact($user->id);
@@ -117,6 +118,20 @@ class core_message_events_testcase extends advanced_testcase {
         $this->assertEventLegacyLogData($expected, $event);
         $url = new moodle_url('/message/index.php', array('user1' => $event->userid, 'user2' => $event->relateduserid));
         $this->assertEquals($url, $event->get_url());
+
+        // Now blocking a user that is not a contact.
+        $sink->clear();
+        message_block_contact($user2->id);
+        $events = $sink->get_events();
+        $event = reset($events);
+
+        // Check that the event data is valid.
+        $this->assertInstanceOf('\core\event\message_contact_blocked', $event);
+        $this->assertEquals(context_user::instance(2), $event->get_context());
+        $expected = array(SITEID, 'message', 'block contact', 'index.php?user1=' . $user2->id . '&amp;user2=2', $user2->id);
+        $this->assertEventLegacyLogData($expected, $event);
+        $url = new moodle_url('/message/index.php', array('user1' => $event->userid, 'user2' => $event->relateduserid));
+        $this->assertEquals($url, $event->get_url());
     }
 
     /**