Skip to content

Commit

Permalink
MDL-63058 core_favourites: add existence checks to the service layer
Browse files Browse the repository at this point in the history
This allows someone using the user_favourite_service to check whether
an item is already marked as a favourite.
  • Loading branch information
snake authored and Bas Brands committed Oct 20, 2018
1 parent cc486e6 commit b81722e
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
21 changes: 21 additions & 0 deletions favourites/classes/local/service/user_favourite_service.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,25 @@ public function delete_favourite(string $component, string $itemtype, int $itemi

$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
]
);
}
}
47 changes: 47 additions & 0 deletions favourites/tests/service_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,19 @@ protected function get_mock_repository(array $mockstore) {
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) {
Expand Down Expand Up @@ -305,4 +318,38 @@ public function test_delete_favourite_basic() {
$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
)
);
}
}

0 comments on commit b81722e

Please sign in to comment.