MDL-63303 theme_bootstrapbase: add bs4 compat classes
[moodle.git] / favourites / tests / service_test.php
CommitLineData
77105132
JD
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * Testing the service layer within core_favourites.
19 *
20 * @package core_favourites
21 * @category test
22 * @copyright 2018 Jake Dallimore <jrhdallimore@gmail.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
cfaf86b0 25use \core_favourites\local\entity\favourite;
77105132
JD
26defined('MOODLE_INTERNAL') || die();
27
28/**
0551ed35 29 * Test class covering the user_favourite_service within the service layer of favourites.
77105132
JD
30 *
31 * @copyright 2018 Jake Dallimore <jrhdallimore@gmail.com>
32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33 */
0551ed35 34class user_favourite_service_testcase extends advanced_testcase {
77105132
JD
35
36 public function setUp() {
37 $this->resetAfterTest();
38 }
39
40 // Basic setup stuff to be reused in most tests.
41 protected function setup_users_and_courses() {
42 $user1 = self::getDataGenerator()->create_user();
43 $user1context = \context_user::instance($user1->id);
44 $user2 = self::getDataGenerator()->create_user();
45 $user2context = \context_user::instance($user2->id);
46 $course1 = self::getDataGenerator()->create_course();
47 $course2 = self::getDataGenerator()->create_course();
48 $course1context = context_course::instance($course1->id);
49 $course2context = context_course::instance($course2->id);
50 return [$user1context, $user2context, $course1context, $course2context];
51 }
52
53 /**
54 * Generates an in-memory repository for testing, using an array store for CRUD stuff.
55 *
56 * @param array $mockstore
57 * @return \PHPUnit\Framework\MockObject\MockObject
58 */
59 protected function get_mock_repository(array $mockstore) {
60 // This mock will just store data in an array.
b7a3ec6f 61 $mockrepo = $this->getMockBuilder(\core_favourites\local\repository\favourite_repository_interface::class)
77105132
JD
62 ->setMethods([])
63 ->getMock();
64 $mockrepo->expects($this->any())
65 ->method('add')
cfaf86b0 66 ->will($this->returnCallback(function(favourite $favourite) use (&$mockstore) {
77105132
JD
67 // Mock implementation of repository->add(), where an array is used instead of the DB.
68 // Duplicates are confirmed via the unique key, and exceptions thrown just like a real repo.
69 $key = $favourite->userid . $favourite->component . $favourite->itemtype . $favourite->itemid
70 . $favourite->contextid;
71
72 // Check the objects for the unique key.
73 foreach ($mockstore as $item) {
74 if ($item->uniquekey == $key) {
75 throw new \moodle_exception('Favourite already exists');
76 }
77 }
78 $index = count($mockstore); // Integer index.
79 $favourite->uniquekey = $key; // Simulate the unique key constraint.
80 $favourite->id = $index;
81 $mockstore[$index] = $favourite;
82 return $mockstore[$index];
83 })
84 );
85 $mockrepo->expects($this->any())
86 ->method('find_by')
8ffbe9c1 87 ->will($this->returnCallback(function(array $criteria, int $limitfrom = 0, int $limitnum = 0) use (&$mockstore) {
77105132
JD
88 // Check the mockstore for all objects with properties matching the key => val pairs in $criteria.
89 foreach ($mockstore as $index => $mockrow) {
90 $mockrowarr = (array)$mockrow;
91 if (array_diff($criteria, $mockrowarr) == []) {
92 $returns[$index] = $mockrow;
93 }
94 }
8ffbe9c1
JD
95 // Return a subset of the records, according to the paging options, if set.
96 if ($limitnum != 0) {
97 return array_slice($returns, $limitfrom, $limitnum);
98 }
99 // Otherwise, just return the full set.
77105132
JD
100 return $returns;
101 })
102 );
103 $mockrepo->expects($this->any())
104 ->method('find_favourite')
105 ->will($this->returnCallback(function(int $userid, string $comp, string $type, int $id, int $ctxid) use (&$mockstore) {
106 // Check the mockstore for all objects with properties matching the key => val pairs in $criteria.
107 $crit = ['userid' => $userid, 'component' => $comp, 'itemtype' => $type, 'itemid' => $id, 'contextid' => $ctxid];
108 foreach ($mockstore as $fakerow) {
109 $fakerowarr = (array)$fakerow;
110 if (array_diff($crit, $fakerowarr) == []) {
111 return $fakerow;
112 }
113 }
114 throw new \moodle_exception("Item not found");
115 })
116 );
117 $mockrepo->expects($this->any())
118 ->method('find')
119 ->will($this->returnCallback(function(int $id) use (&$mockstore) {
120 return $mockstore[$id];
121 })
122 );
123 $mockrepo->expects($this->any())
124 ->method('exists')
125 ->will($this->returnCallback(function(int $id) use (&$mockstore) {
126 return array_key_exists($id, $mockstore);
127 })
128 );
b81722e2
JD
129 $mockrepo->expects($this->any())
130 ->method('exists_by')
131 ->will($this->returnCallback(function(array $criteria) use (&$mockstore) {
132 // Check the mockstore for all objects with properties matching the key => val pairs in $criteria.
133 foreach ($mockstore as $index => $mockrow) {
134 $mockrowarr = (array)$mockrow;
135 if (array_diff($criteria, $mockrowarr) == []) {
136 return true;
137 }
138 }
139 return false;
140 })
141 );
77105132
JD
142 $mockrepo->expects($this->any())
143 ->method('delete')
144 ->will($this->returnCallback(function(int $id) use (&$mockstore) {
145 foreach ($mockstore as $mockrow) {
146 if ($mockrow->id == $id) {
147 unset($mockstore[$id]);
148 }
149 }
150 })
151 );
152 return $mockrepo;
153 }
154
155 /**
0551ed35 156 * Test getting a user_favourite_service from the static locator.
77105132
JD
157 */
158 public function test_get_service_for_user_context() {
159 list($user1context, $user2context, $course1context, $course2context) = $this->setup_users_and_courses();
0551ed35
JD
160 $userservice = \core_favourites\service_factory::get_service_for_user_context($user1context);
161 $this->assertInstanceOf(\core_favourites\local\service\user_favourite_service::class, $userservice);
77105132
JD
162 }
163
164 /**
165 * Test confirming an item can be favourited only once.
166 */
167 public function test_create_favourite_basic() {
168 list($user1context, $user2context, $course1context, $course2context) = $this->setup_users_and_courses();
169
0551ed35 170 // Get a user_favourite_service for a user.
77105132 171 $repo = $this->get_mock_repository([]); // Mock repository, using the array as a mock DB.
0551ed35 172 $user1service = new \core_favourites\local\service\user_favourite_service($user1context, $repo);
77105132
JD
173
174 // Favourite a course.
175 $favourite1 = $user1service->create_favourite('core_course', 'course', $course1context->instanceid, $course1context);
176 $this->assertObjectHasAttribute('id', $favourite1);
177
178 // Try to favourite the same course again.
179 $this->expectException('moodle_exception');
180 $user1service->create_favourite('core_course', 'course', $course1context->instanceid, $course1context);
181 }
182
183 /**
184 * Test confirming that an exception is thrown if trying to favourite an item for a non-existent component.
185 */
186 public function test_create_favourite_nonexistent_component() {
187 list($user1context, $user2context, $course1context, $course2context) = $this->setup_users_and_courses();
188
0551ed35 189 // Get a user_favourite_service for the user.
77105132 190 $repo = $this->get_mock_repository([]); // Mock repository, using the array as a mock DB.
0551ed35 191 $user1service = new \core_favourites\local\service\user_favourite_service($user1context, $repo);
77105132
JD
192
193 // Try to favourite something in a non-existent component.
194 $this->expectException('moodle_exception');
195 $user1service->create_favourite('core_cccourse', 'my_area', $course1context->instanceid, $course1context);
196 }
197
198 /**
199 * Test fetching favourites for single user, by area.
200 */
201 public function test_find_favourites_by_type_single_user() {
202 list($user1context, $user2context, $course1context, $course2context) = $this->setup_users_and_courses();
203
0551ed35 204 // Get a user_favourite_service for the user.
77105132 205 $repo = $this->get_mock_repository([]); // Mock repository, using the array as a mock DB.
0551ed35 206 $service = new \core_favourites\local\service\user_favourite_service($user1context, $repo);
77105132
JD
207
208 // Favourite 2 courses, in separate areas.
209 $fav1 = $service->create_favourite('core_course', 'course', $course1context->instanceid, $course1context);
210 $fav2 = $service->create_favourite('core_course', 'anothertype', $course2context->instanceid, $course2context);
211
212 // Verify we can get favourites by area.
213 $favourites = $service->find_favourites_by_type('core_course', 'course');
214 $this->assertInternalType('array', $favourites);
215 $this->assertCount(1, $favourites); // We only get favourites for the 'core_course/course' area.
216 $this->assertAttributeEquals($fav1->id, 'id', $favourites[$fav1->id]);
217
218 $favourites = $service->find_favourites_by_type('core_course', 'anothertype');
219 $this->assertInternalType('array', $favourites);
220 $this->assertCount(1, $favourites); // We only get favourites for the 'core_course/course' area.
221 $this->assertAttributeEquals($fav2->id, 'id', $favourites[$fav2->id]);
222 }
223
224 /**
225 * Make sure the find_favourites_by_type() method only returns favourites for the scoped user.
226 */
227 public function test_find_favourites_by_type_multiple_users() {
228 list($user1context, $user2context, $course1context, $course2context) = $this->setup_users_and_courses();
229
0551ed35 230 // Get a user_favourite_service for 2 users.
77105132 231 $repo = $this->get_mock_repository([]);
0551ed35
JD
232 $user1service = new \core_favourites\local\service\user_favourite_service($user1context, $repo);
233 $user2service = new \core_favourites\local\service\user_favourite_service($user2context, $repo);
77105132
JD
234
235 // Now, as each user, favourite the same course.
236 $fav1 = $user1service->create_favourite('core_course', 'course', $course1context->instanceid, $course1context);
237 $fav2 = $user2service->create_favourite('core_course', 'course', $course1context->instanceid, $course1context);
238
239 // Verify find_favourites_by_type only returns results for the user to which the service is scoped.
240 $user1favourites = $user1service->find_favourites_by_type('core_course', 'course');
241 $this->assertInternalType('array', $user1favourites);
242 $this->assertCount(1, $user1favourites); // We only get favourites for the 'core_course/course' area for $user1.
243 $this->assertAttributeEquals($fav1->id, 'id', $user1favourites[$fav1->id]);
244
245 $user2favourites = $user2service->find_favourites_by_type('core_course', 'course');
246 $this->assertInternalType('array', $user2favourites);
247 $this->assertCount(1, $user2favourites); // We only get favourites for the 'core_course/course' area for $user2.
248 $this->assertAttributeEquals($fav2->id, 'id', $user2favourites[$fav2->id]);
249 }
250
251 /**
252 * Test confirming that an exception is thrown if trying to get favourites for a non-existent component.
253 */
254 public function test_find_favourites_by_type_nonexistent_component() {
255 list($user1context, $user2context, $course1context, $course2context) = $this->setup_users_and_courses();
256
0551ed35 257 // Get a user_favourite_service for the user.
77105132 258 $repo = $this->get_mock_repository([]);
0551ed35 259 $service = new \core_favourites\local\service\user_favourite_service($user1context, $repo);
77105132
JD
260
261 // Verify we get an exception if we try to search for favourites in an invalid component.
262 $this->expectException('moodle_exception');
263 $service->find_favourites_by_type('cccore_notreal', 'something');
264 }
265
8ffbe9c1
JD
266 /**
267 * Test confirming the pagination support for the find_favourites_by_type() method.
268 */
269 public function test_find_favourites_by_type_pagination() {
270 list($user1context, $user2context, $course1context, $course2context) = $this->setup_users_and_courses();
271
0551ed35 272 // Get a user_favourite_service for the user.
8ffbe9c1 273 $repo = $this->get_mock_repository([]);
0551ed35 274 $service = new \core_favourites\local\service\user_favourite_service($user1context, $repo);
8ffbe9c1
JD
275
276 // Favourite 10 arbitrary items.
277 foreach (range(1, 10) as $i) {
278 $service->create_favourite('core_course', 'course', $i, $course1context);
279 }
280
281 // Verify we have 10 favourites.
282 $this->assertCount(10, $service->find_favourites_by_type('core_course', 'course'));
283
284 // Verify we get back 5 favourites for page 1.
285 $favourites = $service->find_favourites_by_type('core_course', 'course', 0, 5);
286 $this->assertCount(5, $favourites);
287
288 // Verify we get back 5 favourites for page 2.
289 $favourites = $service->find_favourites_by_type('core_course', 'course', 5, 5);
290 $this->assertCount(5, $favourites);
291
292 // Verify we get back an empty array if querying page 3.
293 $favourites = $service->find_favourites_by_type('core_course', 'course', 10, 5);
294 $this->assertCount(0, $favourites);
295 }
296
77105132
JD
297 /**
298 * Test confirming the basic deletion behaviour.
299 */
300 public function test_delete_favourite_basic() {
301 list($user1context, $user2context, $course1context, $course2context) = $this->setup_users_and_courses();
302
0551ed35 303 // Get a user_favourite_service for the user.
77105132 304 $repo = $this->get_mock_repository([]);
0551ed35 305 $service = new \core_favourites\local\service\user_favourite_service($user1context, $repo);
77105132
JD
306
307 // Favourite a course.
308 $fav1 = $service->create_favourite('core_course', 'course', $course1context->instanceid, $course1context);
309 $this->assertTrue($repo->exists($fav1->id));
310
311 // Delete the favourite.
312 $service->delete_favourite('core_course', 'course', $course1context->instanceid, $course1context);
313
314 // Verify the favourite doesn't exist.
315 $this->assertFalse($repo->exists($fav1->id));
316
317 // Try to delete a favourite which we know doesn't exist.
318 $this->expectException(\moodle_exception::class);
319 $service->delete_favourite('core_course', 'course', $course1context->instanceid, $course1context);
320 }
b81722e2
JD
321
322 /**
323 * Test confirming the behaviour of the favourite_exists() method.
324 */
325 public function test_favourite_exists() {
326 list($user1context, $user2context, $course1context, $course2context) = $this->setup_users_and_courses();
327
328 // Get a user_favourite_service for the user.
329 $repo = $this->get_mock_repository([]);
330 $service = new \core_favourites\local\service\user_favourite_service($user1context, $repo);
331
332 // Favourite a course.
333 $fav1 = $service->create_favourite('core_course', 'course', $course1context->instanceid, $course1context);
334
335 // Verify we can check existence of the favourite.
336 $this->assertTrue(
337 $service->favourite_exists(
338 'core_course',
339 'course',
340 $course1context->instanceid,
341 $course1context
342 )
343 );
344
345 // And one that we know doesn't exist.
346 $this->assertFalse(
347 $service->favourite_exists(
348 'core_course',
349 'someothertype',
350 $course1context->instanceid,
351 $course1context
352 )
353 );
354 }
77105132 355}