MDL-64047 core_message: test get_contact_requests() with blocked users
authorJake Dallimore <jake@moodle.com>
Tue, 27 Nov 2018 00:51:40 +0000 (08:51 +0800)
committerJake Dallimore <jake@moodle.com>
Tue, 27 Nov 2018 00:55:35 +0000 (08:55 +0800)
message/tests/api_test.php
message/tests/externallib_test.php

index 703fb60..f3fe0c4 100644 (file)
@@ -4729,6 +4729,24 @@ class core_message_api_testcase extends core_message_messagelib_testcase {
         $this->assertObjectHasAttribute('iscontact', $request);
     }
 
+    /**
+     * Test the get_contact_requests() function when the user has blocked the sender of the request.
+     */
+    public function test_get_contact_requests_blocked_sender() {
+        $user1 = self::getDataGenerator()->create_user();
+        $user2 = self::getDataGenerator()->create_user();
+
+        // User1 blocks User2.
+        \core_message\api::block_user($user1->id, $user2->id);
+
+        // User2 tries to add User1 as a contact.
+        \core_message\api::create_contact_request($user2->id, $user1->id);
+
+        // Verify we don't see the contact request from the blocked user User2 in the requests for User1.
+        $requests = \core_message\api::get_contact_requests($user1->id);
+        $this->assertEmpty($requests);
+    }
+
     /**
      * Test getting contact requests when there are none.
      */
index 938541c..2cc5436 100644 (file)
@@ -587,6 +587,28 @@ class core_message_externallib_testcase extends externallib_advanced_testcase {
         $this->assertArrayHasKey('iscontact', $request);
     }
 
+    /**
+     * Test the get_contact_requests() function when the user has blocked the sender of the request.
+     */
+    public function test_get_contact_requests_blocked_sender() {
+        $this->resetAfterTest();
+        $user1 = self::getDataGenerator()->create_user();
+        $user2 = self::getDataGenerator()->create_user();
+
+        // User1 blocks User2.
+        \core_message\api::block_user($user1->id, $user2->id);
+
+        // User2 tries to add User1 as a contact.
+        \core_message\api::create_contact_request($user2->id, $user1->id);
+
+        // Verify we don't see the contact request from the blocked user User2 in the requests for User1.
+        $this->setUser($user1);
+        $requests = core_message_external::get_contact_requests($user1->id);
+        $requests = external_api::clean_returnvalue(core_message_external::get_contact_requests_returns(), $requests);
+
+        $this->assertCount(0, $requests);
+    }
+
     /**
      * Test getting contact requests when there are none.
      */