MDL-68019 favourites: Unit test
authorPeter <peterrolanddias@gmail.com>
Thu, 27 Feb 2020 00:30:19 +0000 (08:30 +0800)
committerPeter Dias <peter@moodle.com>
Tue, 3 Mar 2020 05:19:46 +0000 (13:19 +0800)
favourites/tests/repository_test.php
favourites/tests/user_favourite_service_test.php

index 7a4fa27..b95451a 100644 (file)
@@ -302,6 +302,16 @@ class favourite_repository_testcase extends advanced_testcase {
         );
         $favouritesrepo->add($favourite);
 
+        // Add another favourite.
+        $favourite = new favourite(
+            'core_course',
+            'course_item',
+            $course1context->instanceid,
+            $course1context->id,
+            $user1context->instanceid
+        );
+        $favouritesrepo->add($favourite);
+
         // From the repo, get the list of favourites for the 'core_course/course' area.
         $userfavourites = $favouritesrepo->find_by(['component' => 'core_course', 'itemtype' => 'course']);
         $this->assertIsArray($userfavourites);
@@ -311,6 +321,16 @@ class favourite_repository_testcase extends advanced_testcase {
         $userfavourites = $favouritesrepo->find_by(['component' => 'core_cannibalism', 'itemtype' => 'course']);
         $this->assertIsArray($userfavourites);
         $this->assertCount(0, $userfavourites);
+
+        // From the repo, get the list of favourites for the 'core_course/course' area when passed as an array.
+        $userfavourites = $favouritesrepo->find_by(['component' => 'core_course', 'itemtype' => ['course']]);
+        $this->assertIsArray($userfavourites);
+        $this->assertCount(1, $userfavourites);
+
+        // From the repo, get the list of favourites for the 'core_course' area given multiple item_types.
+        $userfavourites = $favouritesrepo->find_by(['component' => 'core_course', 'itemtype' => ['course', 'course_item']]);
+        $this->assertIsArray($userfavourites);
+        $this->assertCount(2, $userfavourites);
     }
 
     /**
index bc172af..be372f1 100644 (file)
@@ -85,11 +85,29 @@ class user_favourite_service_testcase extends advanced_testcase {
         $mockrepo->expects($this->any())
             ->method('find_by')
             ->will($this->returnCallback(function(array $criteria, int $limitfrom = 0, int $limitnum = 0) use (&$mockstore) {
+                // Check for single value key pair vs multiple.
+                $multipleconditions = [];
+                foreach ($criteria as $key => $value) {
+                    if (is_array($value)) {
+                        $multipleconditions[$key] = $value;
+                        unset($criteria[$key]);
+                    }
+                }
+
                 // Check the mockstore for all objects with properties matching the key => val pairs in $criteria.
                 foreach ($mockstore as $index => $mockrow) {
                     $mockrowarr = (array)$mockrow;
                     if (array_diff_assoc($criteria, $mockrowarr) == []) {
-                        $returns[$index] = $mockrow;
+                        $found = true;
+                        foreach ($multipleconditions as $key => $value) {
+                            if (!in_array($mockrowarr[$key], $value)) {
+                                $found = false;
+                                break;
+                            }
+                        }
+                        if ($found) {
+                            $returns[$index] = $mockrow;
+                        }
                     }
                 }
                 // Return a subset of the records, according to the paging options, if set.
@@ -235,6 +253,42 @@ class user_favourite_service_testcase extends advanced_testcase {
         $this->assertEquals($fav2->id, $favourites[$fav2->id]->id);
     }
 
+    /**
+     * Test fetching favourites for single user, by area.
+     */
+    public function test_find_all_favourites() {
+        list($user1context, $user2context, $course1context, $course2context) = $this->setup_users_and_courses();
+
+        // Get a user_favourite_service for the user.
+        $repo = $this->get_mock_repository([]); // Mock repository, using the array as a mock DB.
+        $service = new \core_favourites\local\service\user_favourite_service($user1context, $repo);
+
+        // Favourite 2 courses, in separate areas.
+        $fav1 = $service->create_favourite('core_course', 'course', $course1context->instanceid, $course1context);
+        $fav2 = $service->create_favourite('core_course', 'anothertype', $course2context->instanceid, $course2context);
+        $fav3 = $service->create_favourite('core_course', 'yetanothertype', $course2context->instanceid, $course2context);
+
+        // Verify we can get favourites by area.
+        $favourites = $service->find_all_favourites('core_course', ['course']);
+        $this->assertIsArray($favourites);
+        $this->assertCount(1, $favourites); // We only get favourites for the 'core_course/course' area.
+        $this->assertEquals($fav1->id, $favourites[$fav1->id]->id);
+
+        $favourites = $service->find_all_favourites('core_course', ['course', 'anothertype']);
+        $this->assertIsArray($favourites);
+        // We only get favourites for the 'core_course/course' and 'core_course/anothertype area.
+        $this->assertCount(2, $favourites);
+        $this->assertEquals($fav1->id, $favourites[$fav1->id]->id);
+        $this->assertEquals($fav2->id, $favourites[$fav2->id]->id);
+
+        $favourites = $service->find_all_favourites('core_course');
+        $this->assertIsArray($favourites);
+        $this->assertCount(3, $favourites); // We only get favourites for the 'core_cours' area.
+        $this->assertEquals($fav2->id, $favourites[$fav2->id]->id);
+        $this->assertEquals($fav1->id, $favourites[$fav1->id]->id);
+        $this->assertEquals($fav3->id, $favourites[$fav3->id]->id);
+    }
+
     /**
      * Make sure the find_favourites_by_type() method only returns favourites for the scoped user.
      */