MDL-47162 core_message: debug whenever courseid is missing
[moodle.git] / message / tests / events_test.php
index a4081a9..4006738 100644 (file)
@@ -202,7 +202,7 @@ class core_message_events_testcase extends advanced_testcase {
             'relateduserid' => 2,
             'other' => array(
                 'messageid' => 3,
-                'courseid' => 1
+                'courseid' => 4
             )
         ));
 
@@ -219,8 +219,66 @@ 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());
+        $this->assertEquals(3, $event->other['messageid']);
+        $this->assertEquals(4, $event->other['courseid']);
     }
 
+    public function test_mesage_sent_without_other_courseid() {
+
+        // Creating a message_sent event without other[courseid] leads to exception.
+        $this->expectException('coding_exception');
+        $this->expectExceptionMessage('The \'courseid\' value must be set in other');
+
+        $event = \core\event\message_sent::create(array(
+            'userid' => 1,
+            'context'  => context_system::instance(),
+            'relateduserid' => 2,
+            'other' => array(
+                'messageid' => 3,
+            )
+        ));
+    }
+
+    public function test_mesage_sent_via_create_from_ids() {
+        // Containing courseid.
+        $event = \core\event\message_sent::create_from_ids(1, 2, 3, 4);
+
+        // Trigger and capturing the event.
+        $sink = $this->redirectEvents();
+        $event->trigger();
+        $events = $sink->get_events();
+        $event = reset($events);
+
+        // Check that the event data is valid.
+        $this->assertInstanceOf('\core\event\message_sent', $event);
+        $this->assertEquals(context_system::instance(), $event->get_context());
+        $expected = array(SITEID, 'message', 'write', 'index.php?user=1&id=2&history=1#m3', 1);
+        $this->assertEventLegacyLogData($expected, $event);
+        $url = new moodle_url('/message/index.php', array('user1' => $event->userid, 'user2' => $event->relateduserid));
+        $this->assertEquals($url, $event->get_url());
+        $this->assertEquals(3, $event->other['messageid']);
+        $this->assertEquals(4, $event->other['courseid']);
+    }
+
+    public function test_mesage_sent_via_create_from_ids_without_other_courseid() {
+
+        // Creating a message_sent event without courseid leads to debugging + SITEID.
+        // TODO: MDL-55449 Ensure this leads to exception instead of debugging in Moodle 3.6.
+        $event = \core\event\message_sent::create_from_ids(1, 2, 3);
+
+        // Trigger and capturing the event.
+        $sink = $this->redirectEvents();
+        $event->trigger();
+        $events = $sink->get_events();
+        $event = reset($events);
+
+        $this->assertDebuggingCalled();
+        $this->assertEquals(SITEID, $event->other['courseid']);
+    }
+
+
+
+
     /**
      * Test the message viewed event.
      */