MDL-63712 core_message: Unit test improvements for fetching context/user
authorMichael Hawkins <michaelh@moodle.com>
Thu, 1 Nov 2018 05:20:11 +0000 (13:20 +0800)
committerMichael Hawkins <michaelh@moodle.com>
Thu, 1 Nov 2018 08:51:12 +0000 (16:51 +0800)
This issue is part of the MDL-62560 Epic.

message/tests/privacy_provider_test.php

index 608365d..101adbb 100644 (file)
@@ -211,6 +211,12 @@ class core_message_privacy_provider_testcase extends \core_privacy\tests\provide
         $user1 = $this->getDataGenerator()->create_user();
         $user2 = $this->getDataGenerator()->create_user();
 
+        // Test nothing is found before message is sent.
+        $contextlist = provider::get_contexts_for_userid($user1->id);
+        $this->assertCount(0, $contextlist);
+        $contextlist = provider::get_contexts_for_userid($user2->id);
+        $this->assertCount(0, $contextlist);
+
         $this->create_message($user1->id, $user2->id, time() - (9 * DAYSECS));
 
         // Test for the sender.
@@ -239,6 +245,12 @@ class core_message_privacy_provider_testcase extends \core_privacy\tests\provide
         $user1 = $this->getDataGenerator()->create_user();
         $user2 = $this->getDataGenerator()->create_user();
 
+        // Test nothing is found before notification is created.
+        $contextlist = provider::get_contexts_for_userid($user1->id);
+        $this->assertCount(0, $contextlist);
+        $contextlist = provider::get_contexts_for_userid($user2->id);
+        $this->assertCount(0, $contextlist);
+
         $this->create_notification($user1->id, $user2->id, time() - (9 * DAYSECS));
 
         // Test for the sender.
@@ -258,6 +270,108 @@ class core_message_privacy_provider_testcase extends \core_privacy\tests\provide
                 $contextforuser->id);
     }
 
+    /**
+     * Test for provider::get_contexts_for_userid() when a users has a contact.
+     */
+    public function test_get_contexts_for_userid_with_contact() {
+        $this->resetAfterTest();
+
+        $user1 = $this->getDataGenerator()->create_user();
+        $user2 = $this->getDataGenerator()->create_user();
+
+        // Test nothing is found before contact is created.
+        $contextlist = provider::get_contexts_for_userid($user1->id);
+        $this->assertCount(0, $contextlist);
+        $contextlist = provider::get_contexts_for_userid($user2->id);
+        $this->assertCount(0, $contextlist);
+
+        \core_message\api::add_contact($user1->id, $user2->id);
+
+        // Test for the user adding the contact.
+        $contextlist = provider::get_contexts_for_userid($user1->id);
+        $this->assertCount(1, $contextlist);
+        $contextforuser = $contextlist->current();
+        $this->assertEquals(
+                context_user::instance($user1->id)->id,
+                $contextforuser->id);
+
+        // Test for the user who is the contact.
+        $contextlist = provider::get_contexts_for_userid($user2->id);
+        $this->assertCount(1, $contextlist);
+        $contextforuser = $contextlist->current();
+        $this->assertEquals(
+                context_user::instance($user2->id)->id,
+                $contextforuser->id);
+    }
+
+    /**
+     * Test for provider::get_contexts_for_userid() when a user makes a contact request.
+     */
+    public function test_get_contexts_for_userid_with_contact_request() {
+        $this->resetAfterTest();
+
+        $user1 = $this->getDataGenerator()->create_user();
+        $user2 = $this->getDataGenerator()->create_user();
+
+        // Test nothing is found before request is created.
+        $contextlist = provider::get_contexts_for_userid($user1->id);
+        $this->assertCount(0, $contextlist);
+        $contextlist = provider::get_contexts_for_userid($user2->id);
+        $this->assertCount(0, $contextlist);
+
+        \core_message\api::create_contact_request($user1->id, $user2->id);
+
+        // Test for the user requesting the contact.
+        $contextlist = provider::get_contexts_for_userid($user1->id);
+        $this->assertCount(1, $contextlist);
+        $contextforuser = $contextlist->current();
+        $this->assertEquals(
+                context_user::instance($user1->id)->id,
+                $contextforuser->id);
+
+        // Test for the user receiving the contact request.
+        $contextlist = provider::get_contexts_for_userid($user2->id);
+        $this->assertCount(1, $contextlist);
+        $contextforuser = $contextlist->current();
+        $this->assertEquals(
+                context_user::instance($user2->id)->id,
+                $contextforuser->id);
+    }
+
+    /**
+     * Test for provider::get_contexts_for_userid() when a user is blocked.
+     */
+    public function test_get_contexts_for_userid_with_blocked_contact() {
+        $this->resetAfterTest();
+
+        $user1 = $this->getDataGenerator()->create_user();
+        $user2 = $this->getDataGenerator()->create_user();
+
+        // Test nothing is found before user is blocked.
+        $contextlist = provider::get_contexts_for_userid($user1->id);
+        $this->assertCount(0, $contextlist);
+        $contextlist = provider::get_contexts_for_userid($user2->id);
+        $this->assertCount(0, $contextlist);
+
+        \core_message\api::block_user($user1->id, $user2->id);
+
+        // Test for the blocking user.
+        $contextlist = provider::get_contexts_for_userid($user1->id);
+        $this->assertCount(1, $contextlist);
+        $contextforuser = $contextlist->current();
+        $this->assertEquals(
+                context_user::instance($user1->id)->id,
+                $contextforuser->id);
+
+        // Test for the user who is blocked.
+        $contextlist = provider::get_contexts_for_userid($user2->id);
+        $this->assertCount(1, $contextlist);
+        $contextforuser = $contextlist->current();
+        $this->assertEquals(
+                context_user::instance($user2->id)->id,
+                $contextforuser->id);
+    }
+
     /**
      * Test for provider::export_user_data().
      */
@@ -771,6 +885,14 @@ class core_message_privacy_provider_testcase extends \core_privacy\tests\provide
         $user1context = context_user::instance($user1->id);
         $user2context = context_user::instance($user2->id);
 
+        // Test nothing is found before message is sent.
+        $userlist = new \core_privacy\local\request\userlist($user1context, 'core_message');
+        \core_message\privacy\provider::get_users_in_context($userlist);
+        $this->assertCount(0, $userlist);
+        $userlist = new \core_privacy\local\request\userlist($user2context, 'core_message');
+        \core_message\privacy\provider::get_users_in_context($userlist);
+        $this->assertCount(0, $userlist);
+
         $this->create_message($user1->id, $user2->id, time() - (9 * DAYSECS));
 
         // Test for the sender.
@@ -800,6 +922,14 @@ class core_message_privacy_provider_testcase extends \core_privacy\tests\provide
         $user1context = context_user::instance($user1->id);
         $user2context = context_user::instance($user2->id);
 
+        // Test nothing is found before notification is created.
+        $userlist = new \core_privacy\local\request\userlist($user1context, 'core_message');
+        \core_message\privacy\provider::get_users_in_context($userlist);
+        $this->assertCount(0, $userlist);
+        $userlist = new \core_privacy\local\request\userlist($user2context, 'core_message');
+        \core_message\privacy\provider::get_users_in_context($userlist);
+        $this->assertCount(0, $userlist);
+
         $this->create_notification($user1->id, $user2->id, time() - (9 * DAYSECS));
 
         // Test for the sender.
@@ -817,6 +947,117 @@ class core_message_privacy_provider_testcase extends \core_privacy\tests\provide
         $this->assertEquals($user2->id, $userincontext->id);
     }
 
+    /**
+     * Test for provider::get_users_in_context() when a users has a contact.
+     */
+    public function test_get_users_in_context_with_contact() {
+        $this->resetAfterTest();
+
+        $user1 = $this->getDataGenerator()->create_user();
+        $user2 = $this->getDataGenerator()->create_user();
+
+        $user1context = context_user::instance($user1->id);
+        $user2context = context_user::instance($user2->id);
+
+        // Test nothing is found before contact is created.
+        $userlist = new \core_privacy\local\request\userlist($user1context, 'core_message');
+        \core_message\privacy\provider::get_users_in_context($userlist);
+        $this->assertCount(0, $userlist);
+        $userlist = new \core_privacy\local\request\userlist($user2context, 'core_message');
+        \core_message\privacy\provider::get_users_in_context($userlist);
+        $this->assertCount(0, $userlist);
+
+        \core_message\api::add_contact($user1->id, $user2->id);
+
+        // Test for the user adding the contact.
+        $userlist = new \core_privacy\local\request\userlist($user1context, 'core_message');
+        \core_message\privacy\provider::get_users_in_context($userlist);
+        $this->assertCount(1, $userlist);
+        $userincontext = $userlist->current();
+        $this->assertEquals($user1->id, $userincontext->id);
+
+        // Test for the user who is the contact.
+        $userlist = new \core_privacy\local\request\userlist($user2context, 'core_message');
+        \core_message\privacy\provider::get_users_in_context($userlist);
+        $this->assertCount(1, $userlist);
+        $userincontext = $userlist->current();
+        $this->assertEquals($user2->id, $userincontext->id);
+    }
+
+    /**
+     * Test for provider::get_users_in_context() when a user makes a contact request.
+     */
+    public function test_get_users_in_context_with_contact_request() {
+        $this->resetAfterTest();
+
+        $user1 = $this->getDataGenerator()->create_user();
+        $user2 = $this->getDataGenerator()->create_user();
+
+        $user1context = context_user::instance($user1->id);
+        $user2context = context_user::instance($user2->id);
+
+        // Test nothing is found before request is created.
+        $userlist = new \core_privacy\local\request\userlist($user1context, 'core_message');
+        \core_message\privacy\provider::get_users_in_context($userlist);
+        $this->assertCount(0, $userlist);
+        $userlist = new \core_privacy\local\request\userlist($user2context, 'core_message');
+        \core_message\privacy\provider::get_users_in_context($userlist);
+        $this->assertCount(0, $userlist);
+
+        \core_message\api::create_contact_request($user1->id, $user2->id);
+
+        // Test for the user requesting the contact.
+        $userlist = new \core_privacy\local\request\userlist($user1context, 'core_message');
+        \core_message\privacy\provider::get_users_in_context($userlist);
+        $this->assertCount(1, $userlist);
+        $userincontext = $userlist->current();
+        $this->assertEquals($user1->id, $userincontext->id);
+
+        // Test for the user receiving the contact request.
+        $userlist = new \core_privacy\local\request\userlist($user2context, 'core_message');
+        \core_message\privacy\provider::get_users_in_context($userlist);
+        $this->assertCount(1, $userlist);
+        $userincontext = $userlist->current();
+        $this->assertEquals($user2->id, $userincontext->id);
+    }
+
+    /**
+     * Test for provider::get_users_in_context() when a user is blocked.
+     */
+    public function test_get_users_in_context_with_blocked_contact() {
+        $this->resetAfterTest();
+
+        $user1 = $this->getDataGenerator()->create_user();
+        $user2 = $this->getDataGenerator()->create_user();
+
+        $user1context = context_user::instance($user1->id);
+        $user2context = context_user::instance($user2->id);
+
+        // Test nothing is found before user is blocked.
+        $userlist = new \core_privacy\local\request\userlist($user1context, 'core_message');
+        \core_message\privacy\provider::get_users_in_context($userlist);
+        $this->assertCount(0, $userlist);
+        $userlist = new \core_privacy\local\request\userlist($user2context, 'core_message');
+        \core_message\privacy\provider::get_users_in_context($userlist);
+        $this->assertCount(0, $userlist);
+
+        \core_message\api::block_user($user1->id, $user2->id);
+
+        // Test for the blocking user.
+        $userlist = new \core_privacy\local\request\userlist($user1context, 'core_message');
+        \core_message\privacy\provider::get_users_in_context($userlist);
+        $this->assertCount(1, $userlist);
+        $userincontext = $userlist->current();
+        $this->assertEquals($user1->id, $userincontext->id);
+
+        // Test for the user who is blocked.
+        $userlist = new \core_privacy\local\request\userlist($user2context, 'core_message');
+        \core_message\privacy\provider::get_users_in_context($userlist);
+        $this->assertCount(1, $userlist);
+        $userincontext = $userlist->current();
+        $this->assertEquals($user2->id, $userincontext->id);
+    }
+
     /**
      * Test for provider::delete_data_for_users().
      */