mirror of
https://github.com/moodle/moodle.git
synced 2025-04-14 04:52:36 +02:00
MDL-63058 core_favourites: add existence checks to the service layer
This allows someone using the user_favourite_service to check whether an item is already marked as a favourite.
This commit is contained in:
parent
cc486e6125
commit
b81722e22f
@ -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
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user