MDL-63712 core_message: Unit test improvements for fetching context/user

This issue is part of the MDL-62560 Epic.
This commit is contained in:
Michael Hawkins 2018-11-01 13:20:11 +08:00
parent ba81c26c79
commit b8bbef0a9b

View File

@ -211,6 +211,12 @@ class core_message_privacy_provider_testcase extends \core_privacy\tests\provide
$user1 = $this->getDataGenerator()->create_user(); $user1 = $this->getDataGenerator()->create_user();
$user2 = $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)); $this->create_message($user1->id, $user2->id, time() - (9 * DAYSECS));
// Test for the sender. // Test for the sender.
@ -239,6 +245,12 @@ class core_message_privacy_provider_testcase extends \core_privacy\tests\provide
$user1 = $this->getDataGenerator()->create_user(); $user1 = $this->getDataGenerator()->create_user();
$user2 = $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)); $this->create_notification($user1->id, $user2->id, time() - (9 * DAYSECS));
// Test for the sender. // Test for the sender.
@ -258,6 +270,108 @@ class core_message_privacy_provider_testcase extends \core_privacy\tests\provide
$contextforuser->id); $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(). * 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); $user1context = context_user::instance($user1->id);
$user2context = context_user::instance($user2->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)); $this->create_message($user1->id, $user2->id, time() - (9 * DAYSECS));
// Test for the sender. // 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); $user1context = context_user::instance($user1->id);
$user2context = context_user::instance($user2->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)); $this->create_notification($user1->id, $user2->id, time() - (9 * DAYSECS));
// Test for the sender. // 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); $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(). * Test for provider::delete_data_for_users().
*/ */