MDL-63658 core_favourites: make favourite a first class object
This patch adds several things: - favourite entity class under local/entity - refactoring of repository and service to use the new class instead of stdClass. - update repository unit tests to use the object instead of stdClass. - update service_test so that the mock repo requires the object for the add operation. - remove unnecessary constructor from favourites_repository class.
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
<?php
|
||||
// This file is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Moodle is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* Contains the favourite class, each instance being a representation of a DB row for the 'favourite' table.
|
||||
*
|
||||
* @package core_favourites
|
||||
* @copyright 2018 Jake Dallimore <[email protected]>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
namespace core_favourites\local\entity;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
/**
|
||||
* Contains the favourite class, each instance being a representation of a DB row for the 'favourite' table.
|
||||
*
|
||||
* @copyright 2018 Jake Dallimore <[email protected]>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class favourite {
|
||||
/** @var int $id the id of the favourite.*/
|
||||
public $id;
|
||||
|
||||
/** @var string $component the frankenstyle name of the component containing the favourited item. E.g. 'core_course'.*/
|
||||
public $component;
|
||||
|
||||
/** @var string $itemtype the type of the item being marked as a favourite. E.g. 'course', 'conversation', etc.*/
|
||||
public $itemtype;
|
||||
|
||||
/** @var int $itemid the id of the item that is being marked as a favourite. e.g course->id, conversation->id, etc.*/
|
||||
public $itemid;
|
||||
|
||||
/** @var int $contextid the id of the context in which this favourite was created.*/
|
||||
public $contextid;
|
||||
|
||||
/** @var int $userid the id of user who owns this favourite.*/
|
||||
public $userid;
|
||||
|
||||
/** @var int $ordering the ordering of the favourite within it's favourite area.*/
|
||||
public $ordering;
|
||||
|
||||
/** @var int $timecreated the time at which the favourite was created.*/
|
||||
public $timecreated;
|
||||
|
||||
/** @var int $timemodified the time at which the last modification of the favourite took place.*/
|
||||
public $timemodified;
|
||||
|
||||
/**
|
||||
* Favourite constructor.
|
||||
* @param string $component the frankenstyle name of the component containing the favourited item. E.g. 'core_course'.
|
||||
* @param string $itemtype the type of the item being marked as a favourite. E.g. 'course', 'conversation', etc.
|
||||
* @param int $itemid the id of the item that is being marked as a favourite. e.g course->id, conversation->id, etc.
|
||||
* @param int $contextid the id of the context in which this favourite was created.
|
||||
* @param int $userid the id of user who owns this favourite.
|
||||
*/
|
||||
public function __construct(string $component, string $itemtype, int $itemid, int $contextid, int $userid) {
|
||||
$this->component = $component;
|
||||
$this->itemtype = $itemtype;
|
||||
$this->itemid = $itemid;
|
||||
$this->contextid = $contextid;
|
||||
$this->userid = $userid;
|
||||
}
|
||||
}
|
||||
@@ -21,6 +21,7 @@
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
namespace core_favourites\local\repository;
|
||||
use \core_favourites\local\entity\favourite;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
@@ -31,10 +32,10 @@ interface crud_repository {
|
||||
/**
|
||||
* Add one item to this repository.
|
||||
*
|
||||
* @param object $item the item to add.
|
||||
* @return object the item which was added.
|
||||
* @param favourite $item the item to add.
|
||||
* @return favourite the item which was added.
|
||||
*/
|
||||
public function add($item);
|
||||
public function add(favourite $item) : favourite;
|
||||
|
||||
/**
|
||||
* Add all the items in the list to this repository.
|
||||
@@ -48,9 +49,9 @@ interface crud_repository {
|
||||
* Find an item in this repository based on its id.
|
||||
*
|
||||
* @param int $id the id of the item.
|
||||
* @return object the item.
|
||||
* @return favourite the item.
|
||||
*/
|
||||
public function find(int $id);
|
||||
public function find(int $id) : favourite;
|
||||
|
||||
/**
|
||||
* Find all items in this repository.
|
||||
@@ -89,10 +90,10 @@ interface crud_repository {
|
||||
/**
|
||||
* Update an item within this repository.
|
||||
*
|
||||
* @param object $item the item to update.
|
||||
* @return object the updated item.
|
||||
* @param favourite $item the item to update.
|
||||
* @return favourite the updated item.
|
||||
*/
|
||||
public function update($item);
|
||||
public function update(favourite $item) : favourite;
|
||||
|
||||
/**
|
||||
* Delete an item by id.
|
||||
|
||||
@@ -14,13 +14,14 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
/**
|
||||
* Contains the user_favourites_repository class, responsible for CRUD operations for user favourites.
|
||||
* Contains the favourites_repository class, responsible for CRUD operations for favourites.
|
||||
*
|
||||
* @package core_favourites
|
||||
* @copyright 2018 Jake Dallimore <[email protected]>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
namespace core_favourites\local\repository;
|
||||
use \core_favourites\local\entity\favourite;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
@@ -40,20 +41,48 @@ class favourites_repository implements ifavourites_repository {
|
||||
protected $favouritetable = 'favourite';
|
||||
|
||||
/**
|
||||
* The favourites_repository constructor.
|
||||
* Get a favourite object, based on a full record.
|
||||
* @param \stdClass $record the record we wish to hydrate.
|
||||
* @return favourite the favourite record.
|
||||
*/
|
||||
public function __construct() {
|
||||
protected function get_favourite_from_record(\stdClass $record) : favourite {
|
||||
$favourite = new favourite(
|
||||
$record->component,
|
||||
$record->itemtype,
|
||||
$record->itemid,
|
||||
$record->contextid,
|
||||
$record->userid
|
||||
);
|
||||
$favourite->id = $record->id;
|
||||
$favourite->ordering = $record->ordering ?? null;
|
||||
$favourite->timecreated = $record->timecreated ?? null;
|
||||
$favourite->timemodified = $record->timemodified ?? null;
|
||||
|
||||
return $favourite;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of favourite objects, based on a list of records.
|
||||
* @param array $records the record we wish to hydrate.
|
||||
* @return array the list of favourites.
|
||||
*/
|
||||
protected function get_list_of_favourites_from_records(array $records) {
|
||||
$list = [];
|
||||
foreach ($records as $index => $record) {
|
||||
$list[$index] = $this->get_favourite_from_record($record);
|
||||
}
|
||||
return $list;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a favourite to the repository.
|
||||
*
|
||||
* @param \stdClass $favourite the favourite to add.
|
||||
* @return \stdClass the favourite which has been stored.
|
||||
* @param favourite $favourite the favourite to add.
|
||||
* @return favourite the favourite which has been stored.
|
||||
* @throws \dml_exception if any database errors are encountered.
|
||||
* @throws \moodle_exception if the favourite has missing or invalid properties.
|
||||
*/
|
||||
public function add($favourite) : \stdClass {
|
||||
public function add(favourite $favourite) : favourite {
|
||||
global $DB;
|
||||
$this->validate($favourite);
|
||||
$favourite = (array)$favourite;
|
||||
@@ -83,19 +112,21 @@ class favourites_repository implements ifavourites_repository {
|
||||
$ids[] = $DB->insert_record($this->favouritetable, $favourite);
|
||||
}
|
||||
list($insql, $params) = $DB->get_in_or_equal($ids);
|
||||
return $DB->get_records_select($this->favouritetable, "id $insql", $params);
|
||||
$records = $DB->get_records_select($this->favouritetable, "id $insql", $params);
|
||||
return $this->get_list_of_favourites_from_records($records);
|
||||
}
|
||||
|
||||
/**
|
||||
* Find a favourite by id.
|
||||
*
|
||||
* @param int $id the id of the favourite.
|
||||
* @return \stdClass the favourite.
|
||||
* @return favourite the favourite.
|
||||
* @throws \dml_exception if any database errors are encountered.
|
||||
*/
|
||||
public function find(int $id) : \stdClass {
|
||||
public function find(int $id) : favourite {
|
||||
global $DB;
|
||||
return $DB->get_record($this->favouritetable, ['id' => $id], '*', MUST_EXIST);
|
||||
$record = $DB->get_record($this->favouritetable, ['id' => $id], '*', MUST_EXIST);
|
||||
return $this->get_favourite_from_record($record);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -109,7 +140,8 @@ class favourites_repository implements ifavourites_repository {
|
||||
*/
|
||||
public function find_by(array $criteria, int $limitfrom = 0, int $limitnum = 0) : array {
|
||||
global $DB;
|
||||
return $DB->get_records($this->favouritetable, $criteria, '', '*', $limitfrom, $limitnum);
|
||||
$records = $DB->get_records($this->favouritetable, $criteria, '', '*', $limitfrom, $limitnum);
|
||||
return $this->get_list_of_favourites_from_records($records);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -122,7 +154,8 @@ class favourites_repository implements ifavourites_repository {
|
||||
*/
|
||||
public function find_all(int $limitfrom = 0, int $limitnum = 0) : array {
|
||||
global $DB;
|
||||
return $DB->get_records($this->favouritetable, null, '', '*', $limitfrom, $limitnum);
|
||||
$records = $DB->get_records($this->favouritetable, null, '', '*', $limitfrom, $limitnum);
|
||||
return $this->get_list_of_favourites_from_records($records);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -135,19 +168,20 @@ class favourites_repository implements ifavourites_repository {
|
||||
* @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 int $contextid the contextid of the item which was favourited.
|
||||
* @return \stdClass the favourite.
|
||||
* @return favourite the favourite.
|
||||
* @throws \dml_exception if any database errors are encountered or if the record could not be found.
|
||||
*/
|
||||
public function find_favourite(int $userid, string $component, string $itemtype, int $itemid, int $contextid) : \stdClass {
|
||||
public function find_favourite(int $userid, string $component, string $itemtype, int $itemid, int $contextid) : favourite {
|
||||
global $DB;
|
||||
// Favourites model: We know that only one favourite can exist based on these properties.
|
||||
return $DB->get_record($this->favouritetable, [
|
||||
$record = $DB->get_record($this->favouritetable, [
|
||||
'userid' => $userid,
|
||||
'component' => $component,
|
||||
'itemtype' => $itemtype,
|
||||
'itemid' => $itemid,
|
||||
'contextid' => $contextid
|
||||
], '*', MUST_EXIST);
|
||||
return $this->get_favourite_from_record($record);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -165,11 +199,11 @@ class favourites_repository implements ifavourites_repository {
|
||||
/**
|
||||
* Update a favourite.
|
||||
*
|
||||
* @param \stdClass $favourite the favourite to update.
|
||||
* @return \stdClass the updated favourite.
|
||||
* @param favourite $favourite the favourite to update.
|
||||
* @return favourite the updated favourite.
|
||||
* @throws \dml_exception if any database errors are encountered.
|
||||
*/
|
||||
public function update($favourite) : \stdClass {
|
||||
public function update(favourite $favourite) : favourite {
|
||||
global $DB;
|
||||
$time = time();
|
||||
$favourite->timemodified = $time;
|
||||
@@ -260,22 +294,25 @@ class favourites_repository implements ifavourites_repository {
|
||||
/**
|
||||
* Basic validation, confirming we have the minimum field set needed to save a record to the store.
|
||||
*
|
||||
* @param \stdClass $favourite the favourite record to validate.
|
||||
* @param favourite $favourite the favourite record to validate.
|
||||
* @throws \moodle_exception if the supplied favourite has missing or unsupported fields.
|
||||
*/
|
||||
protected function validate(\stdClass $favourite) {
|
||||
protected function validate(favourite $favourite) {
|
||||
|
||||
$favourite = (array)$favourite;
|
||||
|
||||
// The allowed fields, and whether or not each is required.
|
||||
// The timecreated field is generated during create/update, and cannot be specified either.
|
||||
// The allowed fields, and whether or not each is required to create a record.
|
||||
// The timecreated, timemodified and id fields are generated during create/update.
|
||||
$allowedfields = [
|
||||
'userid' => true,
|
||||
'component' => true,
|
||||
'itemtype' => true,
|
||||
'itemid' => true,
|
||||
'contextid' => true,
|
||||
'ordering' => false
|
||||
'ordering' => false,
|
||||
'timecreated' => false,
|
||||
'timemodified' => false,
|
||||
'id' => false
|
||||
];
|
||||
|
||||
$requiredfields = array_filter($allowedfields, function($field) {
|
||||
|
||||
@@ -21,11 +21,12 @@
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
namespace core_favourites\local\repository;
|
||||
use \core_favourites\local\entity\favourite;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
/**
|
||||
* The favourites_repository interface, defining additional operations useful to favourites type repositories.
|
||||
* The favourites_repository interface, defining additional operations useful to favourite type repositories.
|
||||
*
|
||||
* @copyright 2018 Jake Dallimore <[email protected]>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
@@ -39,7 +40,7 @@ interface ifavourites_repository extends crud_repository {
|
||||
* @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 int $contextid the contextid of the item which was favourited.
|
||||
* @return \stdClass the favourite.
|
||||
* @return favourite the favourite.
|
||||
*/
|
||||
public function find_favourite(int $userid, string $component, string $itemtype, int $itemid, int $contextid) : \stdClass;
|
||||
public function find_favourite(int $userid, string $component, string $itemtype, int $itemid, int $contextid) : favourite;
|
||||
}
|
||||
|
||||
@@ -22,6 +22,7 @@
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
namespace core_favourites\local\service;
|
||||
use \core_favourites\local\entity\favourite;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
@@ -74,11 +75,11 @@ class user_favourites_service {
|
||||
* @param int $itemid the id of the item which is to be favourited.
|
||||
* @param \context $context the context in which the item is to be favourited.
|
||||
* @param int|null $ordering optional ordering integer used for sorting the favourites in an area.
|
||||
* @return \stdClass the favourite, once created.
|
||||
* @return favourite the favourite, once created.
|
||||
* @throws \moodle_exception if the component name is invalid, or if the repository encounters any errors.
|
||||
*/
|
||||
public function create_favourite(string $component, string $itemtype, int $itemid, \context $context,
|
||||
int $ordering = null) : \stdClass {
|
||||
int $ordering = null) : favourite {
|
||||
// Access: Any component can ask to favourite something, we can't verify access to that 'something' here though.
|
||||
|
||||
// Validate the component name.
|
||||
@@ -86,14 +87,8 @@ class user_favourites_service {
|
||||
throw new \moodle_exception("Invalid component name '$component'");
|
||||
}
|
||||
|
||||
$favourite = (object) [
|
||||
'userid' => $this->userid,
|
||||
'component' => $component,
|
||||
'itemtype' => $itemtype,
|
||||
'itemid' => $itemid,
|
||||
'contextid' => $context->id,
|
||||
'ordering' => $ordering > 0 ? $ordering : null
|
||||
];
|
||||
$favourite = new favourite($component, $itemtype, $itemid, $context->id, $this->userid);
|
||||
$favourite->ordering = $ordering > 0 ? $ordering : null;
|
||||
return $this->repo->add($favourite);
|
||||
}
|
||||
|
||||
|
||||
@@ -40,7 +40,7 @@ class services {
|
||||
* Returns a basic service object providing operations for user favourites.
|
||||
*
|
||||
* @param \context_user $context the context of the user to which the service should be scoped.
|
||||
* @return user_favourites_service the service object.
|
||||
* @return \core_favourites\local\service\user_favourites_service the service object.
|
||||
*/
|
||||
public static function get_service_for_user_context(\context_user $context) : local\service\user_favourites_service {
|
||||
return new local\service\user_favourites_service($context, new local\repository\favourites_repository());
|
||||
|
||||
@@ -26,6 +26,7 @@
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
use \core_favourites\local\repository\favourites_repository;
|
||||
use \core_favourites\local\entity\favourite;
|
||||
|
||||
/**
|
||||
* Test class covering the favourites_repository.
|
||||
@@ -61,18 +62,19 @@ class favourites_repository_testcase extends advanced_testcase {
|
||||
// Create a favourites repository and favourite a course.
|
||||
$favouritesrepo = new favourites_repository($user1context);
|
||||
|
||||
$favcourse = (object)[
|
||||
'userid' => $user1context->instanceid,
|
||||
'component' => 'core_course',
|
||||
'itemtype' => 'course',
|
||||
'itemid' => $course1context->instanceid,
|
||||
'contextid' => $course1context->id,
|
||||
];
|
||||
$favcourse = new favourite(
|
||||
'core_course',
|
||||
'course',
|
||||
$course1context->instanceid,
|
||||
$course1context->id,
|
||||
$user1context->instanceid
|
||||
);
|
||||
$timenow = time(); // Reference only, to check that the created item has a time equal to or greater than this.
|
||||
$favourite = $favouritesrepo->add($favcourse);
|
||||
|
||||
// Verify we get the record back.
|
||||
$this->assertInstanceOf(\stdClass::class, $favourite);
|
||||
$this->assertInstanceOf(favourite::class, $favourite);
|
||||
$this->assertObjectHasAttribute('id', $favourite);
|
||||
$this->assertEquals('core_course', $favourite->component);
|
||||
$this->assertEquals('course', $favourite->itemtype);
|
||||
|
||||
@@ -95,14 +97,14 @@ class favourites_repository_testcase extends advanced_testcase {
|
||||
// Create a favourites repository and favourite a course.
|
||||
$favouritesrepo = new favourites_repository($user1context);
|
||||
|
||||
$favcourse = (object)[
|
||||
'userid' => $user1context->instanceid,
|
||||
'component' => 'core_course',
|
||||
'itemtype' => 'course',
|
||||
'itemid' => $course1context->instanceid,
|
||||
'contextid' => $course1context->id,
|
||||
'anotherfield' => 'cat'
|
||||
];
|
||||
$favcourse = new favourite(
|
||||
'core_course',
|
||||
'course',
|
||||
$course1context->instanceid,
|
||||
$course1context->id,
|
||||
$user1context->instanceid
|
||||
);
|
||||
$favcourse->something = 'something';
|
||||
|
||||
$this->expectException('moodle_exception');
|
||||
$favouritesrepo->add($favcourse);
|
||||
@@ -114,14 +116,17 @@ class favourites_repository_testcase extends advanced_testcase {
|
||||
public function test_add_incomplete_favourite() {
|
||||
list($user1context, $user2context, $course1context, $course2context) = $this->setup_users_and_courses();
|
||||
|
||||
// Create a favourites repository and favourite a course.
|
||||
// Create a favourites repository and try to favourite a course.
|
||||
$favouritesrepo = new favourites_repository($user1context);
|
||||
|
||||
$favcourse = (object)[
|
||||
'component' => 'core_course',
|
||||
'itemtype' => 'course',
|
||||
'itemid' => $course1context->instanceid
|
||||
];
|
||||
$favcourse = new favourite(
|
||||
'core_course',
|
||||
'course',
|
||||
$course1context->instanceid,
|
||||
$course1context->id,
|
||||
$user1context->instanceid
|
||||
);
|
||||
unset($favcourse->userid);
|
||||
|
||||
$this->expectException('moodle_exception');
|
||||
$favouritesrepo->add($favcourse);
|
||||
@@ -134,27 +139,29 @@ class favourites_repository_testcase extends advanced_testcase {
|
||||
$favouritesrepo = new favourites_repository($user1context);
|
||||
$favcourses = [];
|
||||
|
||||
$favcourses[] = (object)[
|
||||
'userid' => $user1context->instanceid,
|
||||
'component' => 'core_course',
|
||||
'itemtype' => 'course',
|
||||
'itemid' => $course1context->instanceid,
|
||||
'contextid' => $course1context->id,
|
||||
];
|
||||
$favcourses[] = (object)[
|
||||
'userid' => $user1context->instanceid,
|
||||
'component' => 'core_course',
|
||||
'itemtype' => 'course',
|
||||
'itemid' => $course2context->instanceid,
|
||||
'contextid' => $course2context->id,
|
||||
];
|
||||
$favcourses[] = new favourite(
|
||||
'core_course',
|
||||
'course',
|
||||
$course1context->instanceid,
|
||||
$course1context->id,
|
||||
$user1context->instanceid
|
||||
);
|
||||
$favcourses[] = new favourite(
|
||||
'core_course',
|
||||
'course',
|
||||
$course2context->instanceid,
|
||||
$course2context->id,
|
||||
$user1context->instanceid
|
||||
);
|
||||
|
||||
$timenow = time(); // Reference only, to check that the created item has a time equal to or greater than this.
|
||||
$favourites = $favouritesrepo->add_all($favcourses);
|
||||
|
||||
$this->assertInternalType('array', $favourites);
|
||||
$this->assertCount(2, $favourites);
|
||||
foreach ($favourites as $favourite) {
|
||||
// Verify we get the record back.
|
||||
// Verify we get the favourite back.
|
||||
$this->assertInstanceOf(favourite::class, $favourite);
|
||||
$this->assertEquals('core_course', $favourite->component);
|
||||
$this->assertEquals('course', $favourite->itemtype);
|
||||
|
||||
@@ -177,18 +184,18 @@ class favourites_repository_testcase extends advanced_testcase {
|
||||
|
||||
// Create a favourites repository and favourite a course.
|
||||
$favouritesrepo = new favourites_repository($user1context);
|
||||
$favourite = (object) [
|
||||
'userid' => $user1context->instanceid,
|
||||
'component' => 'core_course',
|
||||
'itemtype' => 'course',
|
||||
'itemid' => $course1context->instanceid,
|
||||
'contextid' => $course1context->id
|
||||
];
|
||||
$favourite = new favourite(
|
||||
'core_course',
|
||||
'course',
|
||||
$course1context->instanceid,
|
||||
$course1context->id,
|
||||
$user1context->instanceid
|
||||
);
|
||||
$favourite = $favouritesrepo->add($favourite);
|
||||
|
||||
// Now, from the repo, get the single favourite we just created, by id.
|
||||
$userfavourite = $favouritesrepo->find($favourite->id);
|
||||
$this->assertInstanceOf(\stdClass::class, $userfavourite);
|
||||
$this->assertInstanceOf(favourite::class, $userfavourite);
|
||||
$this->assertObjectHasAttribute('timecreated', $userfavourite);
|
||||
|
||||
// Try to get a favourite we know doesn't exist.
|
||||
@@ -209,20 +216,20 @@ class favourites_repository_testcase extends advanced_testcase {
|
||||
$this->assertEquals([], $favouritesrepo->find_all());
|
||||
|
||||
// Save a favourite for 2 courses, in different areas.
|
||||
$favourite = (object) [
|
||||
'userid' => $user1context->instanceid,
|
||||
'component' => 'core_course',
|
||||
'itemtype' => 'course',
|
||||
'itemid' => $course1context->instanceid,
|
||||
'contextid' => $course1context->id
|
||||
];
|
||||
$favourite2 = (object) [
|
||||
'userid' => $user1context->instanceid,
|
||||
'component' => 'core_course',
|
||||
'itemtype' => 'anothertype',
|
||||
'itemid' => $course2context->instanceid,
|
||||
'contextid' => $course2context->id
|
||||
];
|
||||
$favourite = new favourite(
|
||||
'core_course',
|
||||
'course',
|
||||
$course1context->instanceid,
|
||||
$course1context->id,
|
||||
$user1context->instanceid
|
||||
);
|
||||
$favourite2 = new favourite(
|
||||
'core_course',
|
||||
'course',
|
||||
$course2context->instanceid,
|
||||
$course2context->id,
|
||||
$user1context->instanceid
|
||||
);
|
||||
$favouritesrepo->add($favourite);
|
||||
$favouritesrepo->add($favourite2);
|
||||
|
||||
@@ -230,6 +237,7 @@ class favourites_repository_testcase extends advanced_testcase {
|
||||
$favourites = $favouritesrepo->find_all();
|
||||
$this->assertCount(2, $favourites);
|
||||
foreach ($favourites as $fav) {
|
||||
$this->assertInstanceOf(favourite::class, $fav);
|
||||
$this->assertObjectHasAttribute('id', $fav);
|
||||
$this->assertObjectHasAttribute('timecreated', $fav);
|
||||
}
|
||||
@@ -251,13 +259,13 @@ class favourites_repository_testcase extends advanced_testcase {
|
||||
|
||||
// Save 10 arbitrary favourites to the repo.
|
||||
foreach (range(1, 10) as $i) {
|
||||
$favourite = (object) [
|
||||
'userid' => $user1context->instanceid,
|
||||
'component' => 'core_course',
|
||||
'itemtype' => 'course',
|
||||
'itemid' => $i,
|
||||
'contextid' => $course1context->id
|
||||
];
|
||||
$favourite = new favourite(
|
||||
'core_course',
|
||||
'course',
|
||||
$i,
|
||||
$course1context->id,
|
||||
$user1context->instanceid
|
||||
);
|
||||
$favouritesrepo->add($favourite);
|
||||
}
|
||||
|
||||
@@ -285,13 +293,13 @@ class favourites_repository_testcase extends advanced_testcase {
|
||||
|
||||
// Create a favourites repository and favourite a course.
|
||||
$favouritesrepo = new favourites_repository($user1context);
|
||||
$favourite = (object) [
|
||||
'userid' => $user1context->instanceid,
|
||||
'component' => 'core_course',
|
||||
'itemtype' => 'course',
|
||||
'itemid' => $course1context->instanceid,
|
||||
'contextid' => $course1context->id
|
||||
];
|
||||
$favourite = new favourite(
|
||||
'core_course',
|
||||
'course',
|
||||
$course1context->instanceid,
|
||||
$course1context->id,
|
||||
$user1context->instanceid
|
||||
);
|
||||
$favouritesrepo->add($favourite);
|
||||
|
||||
// From the repo, get the list of favourites for the 'core_course/course' area.
|
||||
@@ -321,13 +329,13 @@ class favourites_repository_testcase extends advanced_testcase {
|
||||
|
||||
// Save 10 arbitrary favourites to the repo.
|
||||
foreach (range(1, 10) as $i) {
|
||||
$favourite = (object) [
|
||||
'userid' => $user1context->instanceid,
|
||||
'component' => 'core_course',
|
||||
'itemtype' => 'course',
|
||||
'itemid' => $i,
|
||||
'contextid' => $course1context->id
|
||||
];
|
||||
$favourite = new favourite(
|
||||
'core_course',
|
||||
'course',
|
||||
$i,
|
||||
$course1context->id,
|
||||
$user1context->instanceid
|
||||
);
|
||||
$favouritesrepo->add($favourite);
|
||||
}
|
||||
|
||||
@@ -359,20 +367,20 @@ class favourites_repository_testcase extends advanced_testcase {
|
||||
|
||||
// Create a favourites repository and add 2 favourites in different areas.
|
||||
$favouritesrepo = new favourites_repository($user1context);
|
||||
$favourite = (object) [
|
||||
'userid' => $user1context->instanceid,
|
||||
'component' => 'core_course',
|
||||
'itemtype' => 'course',
|
||||
'itemid' => $course1context->instanceid,
|
||||
'contextid' => $course1context->id
|
||||
];
|
||||
$favourite2 = (object) [
|
||||
'userid' => $user1context->instanceid,
|
||||
'component' => 'core_course',
|
||||
'itemtype' => 'anothertype',
|
||||
'itemid' => $course2context->instanceid,
|
||||
'contextid' => $course2context->id
|
||||
];
|
||||
$favourite = new favourite(
|
||||
'core_course',
|
||||
'course',
|
||||
$course1context->instanceid,
|
||||
$course1context->id,
|
||||
$user1context->instanceid
|
||||
);
|
||||
$favourite2 = new favourite(
|
||||
'core_course',
|
||||
'anothertype',
|
||||
$course2context->instanceid,
|
||||
$course2context->id,
|
||||
$user1context->instanceid
|
||||
);
|
||||
$favouritesrepo->add($favourite);
|
||||
$favouritesrepo->add($favourite2);
|
||||
|
||||
@@ -390,13 +398,13 @@ class favourites_repository_testcase extends advanced_testcase {
|
||||
|
||||
// Create a favourites repository and favourite a course.
|
||||
$favouritesrepo = new favourites_repository($user1context);
|
||||
$favourite = (object) [
|
||||
'userid' => $user1context->instanceid,
|
||||
'component' => 'core_course',
|
||||
'itemtype' => 'course',
|
||||
'itemid' => $course1context->instanceid,
|
||||
'contextid' => $course1context->id
|
||||
];
|
||||
$favourite = new favourite(
|
||||
'core_course',
|
||||
'course',
|
||||
$course1context->instanceid,
|
||||
$course1context->id,
|
||||
$user1context->instanceid
|
||||
);
|
||||
$createdfavourite = $favouritesrepo->add($favourite);
|
||||
|
||||
// Verify the existence of the favourite in the repo.
|
||||
@@ -411,20 +419,20 @@ class favourites_repository_testcase extends advanced_testcase {
|
||||
|
||||
// Create a favourites repository and favourite two courses, in different areas.
|
||||
$favouritesrepo = new favourites_repository($user1context);
|
||||
$favourite = (object) [
|
||||
'userid' => $user1context->instanceid,
|
||||
'component' => 'core_course',
|
||||
'itemtype' => 'course',
|
||||
'itemid' => $course1context->instanceid,
|
||||
'contextid' => $course1context->id
|
||||
];
|
||||
$favourite2 = (object) [
|
||||
'userid' => $user1context->instanceid,
|
||||
'component' => 'core_course',
|
||||
'itemtype' => 'anothertype',
|
||||
'itemid' => $course2context->instanceid,
|
||||
'contextid' => $course2context->id
|
||||
];
|
||||
$favourite = new favourite(
|
||||
'core_course',
|
||||
'course',
|
||||
$course1context->instanceid,
|
||||
$course1context->id,
|
||||
$user1context->instanceid
|
||||
);
|
||||
$favourite2 = new favourite(
|
||||
'core_course',
|
||||
'anothertype',
|
||||
$course2context->instanceid,
|
||||
$course2context->id,
|
||||
$user1context->instanceid
|
||||
);
|
||||
$favourite1 = $favouritesrepo->add($favourite);
|
||||
$favourite2 = $favouritesrepo->add($favourite2);
|
||||
|
||||
@@ -447,19 +455,20 @@ class favourites_repository_testcase extends advanced_testcase {
|
||||
|
||||
// Create a favourites repository and favourite a course.
|
||||
$favouritesrepo = new favourites_repository($user1context);
|
||||
$favourite = (object) [
|
||||
'userid' => $user1context->instanceid,
|
||||
'component' => 'core_course',
|
||||
'itemtype' => 'course',
|
||||
'itemid' => $course1context->instanceid,
|
||||
'contextid' => $course1context->id
|
||||
];
|
||||
$favourite = new favourite(
|
||||
'core_course',
|
||||
'course',
|
||||
$course1context->instanceid,
|
||||
$course1context->id,
|
||||
$user1context->instanceid
|
||||
);
|
||||
$favourite1 = $favouritesrepo->add($favourite);
|
||||
$this->assertNull($favourite1->ordering);
|
||||
|
||||
// Verify we can update the ordering for 2 favourites.
|
||||
$favourite1->ordering = 1;
|
||||
$favourite1 = $favouritesrepo->update($favourite1);
|
||||
$this->assertInstanceOf(stdClass::class, $favourite1);
|
||||
$this->assertInstanceOf(favourite::class, $favourite1);
|
||||
$this->assertAttributeEquals('1', 'ordering', $favourite1);
|
||||
}
|
||||
|
||||
@@ -468,13 +477,13 @@ class favourites_repository_testcase extends advanced_testcase {
|
||||
|
||||
// Create a favourites repository and favourite a course.
|
||||
$favouritesrepo = new favourites_repository($user1context);
|
||||
$favourite = (object) [
|
||||
'userid' => $user1context->instanceid,
|
||||
'component' => 'core_course',
|
||||
'itemtype' => 'course',
|
||||
'itemid' => $course1context->instanceid,
|
||||
'contextid' => $course1context->id
|
||||
];
|
||||
$favourite = new favourite(
|
||||
'core_course',
|
||||
'course',
|
||||
$course1context->instanceid,
|
||||
$course1context->id,
|
||||
$user1context->instanceid
|
||||
);
|
||||
$favourite = $favouritesrepo->add($favourite);
|
||||
|
||||
// Verify the existence of the favourite in the repo.
|
||||
@@ -490,20 +499,20 @@ class favourites_repository_testcase extends advanced_testcase {
|
||||
|
||||
// Create a favourites repository and favourite two courses, in different areas.
|
||||
$favouritesrepo = new favourites_repository($user1context);
|
||||
$favourite = (object) [
|
||||
'userid' => $user1context->instanceid,
|
||||
'component' => 'core_course',
|
||||
'itemtype' => 'course',
|
||||
'itemid' => $course1context->instanceid,
|
||||
'contextid' => $course1context->id
|
||||
];
|
||||
$favourite2 = (object) [
|
||||
'userid' => $user1context->instanceid,
|
||||
'component' => 'core_course',
|
||||
'itemtype' => 'anothertype',
|
||||
'itemid' => $course2context->instanceid,
|
||||
'contextid' => $course2context->id
|
||||
];
|
||||
$favourite = new favourite(
|
||||
'core_course',
|
||||
'course',
|
||||
$course1context->instanceid,
|
||||
$course1context->id,
|
||||
$user1context->instanceid
|
||||
);
|
||||
$favourite2 = new favourite(
|
||||
'core_course',
|
||||
'anothertype',
|
||||
$course1context->instanceid,
|
||||
$course1context->id,
|
||||
$user1context->instanceid
|
||||
);
|
||||
$favourite1 = $favouritesrepo->add($favourite);
|
||||
$favourite2 = $favouritesrepo->add($favourite2);
|
||||
|
||||
|
||||
@@ -22,7 +22,7 @@
|
||||
* @copyright 2018 Jake Dallimore <[email protected]>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
use \core_favourites\local\entity\favourite;
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
/**
|
||||
@@ -63,7 +63,7 @@ class user_favourites_service_testcase extends advanced_testcase {
|
||||
->getMock();
|
||||
$mockrepo->expects($this->any())
|
||||
->method('add')
|
||||
->will($this->returnCallback(function(\stdclass $favourite) use (&$mockstore) {
|
||||
->will($this->returnCallback(function(favourite $favourite) use (&$mockstore) {
|
||||
// Mock implementation of repository->add(), where an array is used instead of the DB.
|
||||
// Duplicates are confirmed via the unique key, and exceptions thrown just like a real repo.
|
||||
$key = $favourite->userid . $favourite->component . $favourite->itemtype . $favourite->itemid
|
||||
|
||||
Reference in New Issue
Block a user