diff --git a/favourites/classes/local/service/user_favourite_service.php b/favourites/classes/local/service/user_favourite_service.php index d3b6f6d99b9..16718b82f00 100644 --- a/favourites/classes/local/service/user_favourite_service.php +++ b/favourites/classes/local/service/user_favourite_service.php @@ -135,4 +135,25 @@ class user_favourite_service { $this->repo->delete($favourite->id); } + + /** + * Check whether an item has been marked as a favourite in the respective area. + * + * @param string $component the frankenstyle component name. + * @param string $itemtype the type of the favourited item. + * @param int $itemid the id of the item which was favourited (not the favourite's id). + * @param \context $context the context of the item which was favourited. + * @return bool true if the item is favourited, false otherwise. + */ + public function favourite_exists(string $component, string $itemtype, int $itemid, \context $context) : bool { + return $this->repo->exists_by( + [ + 'userid' => $this->userid, + 'component' => $component, + 'itemtype' => $itemtype, + 'itemid' => $itemid, + 'contextid' => $context->id + ] + ); + } } diff --git a/favourites/tests/service_test.php b/favourites/tests/service_test.php index 6600c2dccc6..939e5446629 100644 --- a/favourites/tests/service_test.php +++ b/favourites/tests/service_test.php @@ -126,6 +126,19 @@ class user_favourite_service_testcase extends advanced_testcase { return array_key_exists($id, $mockstore); }) ); + $mockrepo->expects($this->any()) + ->method('exists_by') + ->will($this->returnCallback(function(array $criteria) use (&$mockstore) { + // 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($criteria, $mockrowarr) == []) { + return true; + } + } + return false; + }) + ); $mockrepo->expects($this->any()) ->method('delete') ->will($this->returnCallback(function(int $id) use (&$mockstore) { @@ -305,4 +318,38 @@ class user_favourite_service_testcase extends advanced_testcase { $this->expectException(\moodle_exception::class); $service->delete_favourite('core_course', 'course', $course1context->instanceid, $course1context); } + + /** + * Test confirming the behaviour of the favourite_exists() method. + */ + public function test_favourite_exists() { + list($user1context, $user2context, $course1context, $course2context) = $this->setup_users_and_courses(); + + // Get a user_favourite_service for the user. + $repo = $this->get_mock_repository([]); + $service = new \core_favourites\local\service\user_favourite_service($user1context, $repo); + + // Favourite a course. + $fav1 = $service->create_favourite('core_course', 'course', $course1context->instanceid, $course1context); + + // Verify we can check existence of the favourite. + $this->assertTrue( + $service->favourite_exists( + 'core_course', + 'course', + $course1context->instanceid, + $course1context + ) + ); + + // And one that we know doesn't exist. + $this->assertFalse( + $service->favourite_exists( + 'core_course', + 'someothertype', + $course1context->instanceid, + $course1context + ) + ); + } }