From 631d9f661fea9a47bb01a412d79b088bb9ef682e Mon Sep 17 00:00:00 2001 From: Dan Poltawski Date: Wed, 14 Aug 2013 17:29:18 +0800 Subject: [PATCH] MDL-41191 enrol: unit tests for enrol_get_shared_courses --- enrol/tests/enrollib_test.php | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/enrol/tests/enrollib_test.php b/enrol/tests/enrollib_test.php index 250fcd8ba92..cba40a8c338 100644 --- a/enrol/tests/enrollib_test.php +++ b/enrol/tests/enrollib_test.php @@ -250,4 +250,33 @@ class core_enrollib_testcase extends advanced_testcase { $this->assertTrue(enrol_user_sees_own_courses()); $this->assertEquals($reads, $DB->perf_get_reads()); } + + public function test_enrol_get_shared_courses() { + $this->resetAfterTest(); + + $user1 = $this->getDataGenerator()->create_user(); + $user2 = $this->getDataGenerator()->create_user(); + $user3 = $this->getDataGenerator()->create_user(); + + $course1 = $this->getDataGenerator()->create_course(); + $this->getDataGenerator()->enrol_user($user1->id, $course1->id); + $this->getDataGenerator()->enrol_user($user2->id, $course1->id); + + $course2 = $this->getDataGenerator()->create_course(); + $this->getDataGenerator()->enrol_user($user1->id, $course2->id); + + // Test that user1 and user2 have courses in common. + $this->assertTrue(enrol_get_shared_courses($user1, $user2, false, true)); + // Test that user1 and user3 have no courses in common. + $this->assertFalse(enrol_get_shared_courses($user1, $user3, false, true)); + + // Test retrieving the courses in common. + $sharedcourses = enrol_get_shared_courses($user1, $user2, true); + + // Only should be one shared course. + $this->assertCount(1, $sharedcourses); + $sharedcourse = array_shift($sharedcourses); + // It should be course 1. + $this->assertEquals($sharedcourse->id, $course1->id); + } }