Commit | Line | Data |
---|---|---|
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 | */ | |
25 | ||
26 | defined('MOODLE_INTERNAL') || die(); | |
27 | ||
28 | /** | |
29 | * Test class covering the user_favourites_service within the service layer of favourites. | |
30 | * | |
31 | * @copyright 2018 Jake Dallimore <jrhdallimore@gmail.com> | |
32 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
33 | */ | |
34 | class user_favourites_service_testcase extends advanced_testcase { | |
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. | |
4a02aae5 | 61 | $mockrepo = $this->getMockBuilder(\core_favourites\local\repository\ifavourites_repository::class) |
77105132 JD |
62 | ->setMethods([]) |
63 | ->getMock(); | |
64 | $mockrepo->expects($this->any()) | |
65 | ->method('add') | |
66 | ->will($this->returnCallback(function(\stdclass $favourite) use (&$mockstore) { | |
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') | |
87 | ->will($this->returnCallback(function(array $criteria) use (&$mockstore) { | |
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 | } | |
95 | return $returns; | |
96 | }) | |
97 | ); | |
98 | $mockrepo->expects($this->any()) | |
99 | ->method('find_favourite') | |
100 | ->will($this->returnCallback(function(int $userid, string $comp, string $type, int $id, int $ctxid) use (&$mockstore) { | |
101 | // Check the mockstore for all objects with properties matching the key => val pairs in $criteria. | |
102 | $crit = ['userid' => $userid, 'component' => $comp, 'itemtype' => $type, 'itemid' => $id, 'contextid' => $ctxid]; | |
103 | foreach ($mockstore as $fakerow) { | |
104 | $fakerowarr = (array)$fakerow; | |
105 | if (array_diff($crit, $fakerowarr) == []) { | |
106 | return $fakerow; | |
107 | } | |
108 | } | |
109 | throw new \moodle_exception("Item not found"); | |
110 | }) | |
111 | ); | |
112 | $mockrepo->expects($this->any()) | |
113 | ->method('find') | |
114 | ->will($this->returnCallback(function(int $id) use (&$mockstore) { | |
115 | return $mockstore[$id]; | |
116 | }) | |
117 | ); | |
118 | $mockrepo->expects($this->any()) | |
119 | ->method('exists') | |
120 | ->will($this->returnCallback(function(int $id) use (&$mockstore) { | |
121 | return array_key_exists($id, $mockstore); | |
122 | }) | |
123 | ); | |
124 | $mockrepo->expects($this->any()) | |
125 | ->method('delete') | |
126 | ->will($this->returnCallback(function(int $id) use (&$mockstore) { | |
127 | foreach ($mockstore as $mockrow) { | |
128 | if ($mockrow->id == $id) { | |
129 | unset($mockstore[$id]); | |
130 | } | |
131 | } | |
132 | }) | |
133 | ); | |
134 | return $mockrepo; | |
135 | } | |
136 | ||
137 | /** | |
138 | * Test getting a user_favourites_service from the static locator. | |
139 | */ | |
140 | public function test_get_service_for_user_context() { | |
141 | list($user1context, $user2context, $course1context, $course2context) = $this->setup_users_and_courses(); | |
142 | $userservice = \core_favourites\services::get_service_for_user_context($user1context); | |
4a02aae5 | 143 | $this->assertInstanceOf(\core_favourites\local\service\user_favourites_service::class, $userservice); |
77105132 JD |
144 | } |
145 | ||
146 | /** | |
147 | * Test confirming an item can be favourited only once. | |
148 | */ | |
149 | public function test_create_favourite_basic() { | |
150 | list($user1context, $user2context, $course1context, $course2context) = $this->setup_users_and_courses(); | |
151 | ||
152 | // Get a user_favourites_service for a user. | |
153 | $repo = $this->get_mock_repository([]); // Mock repository, using the array as a mock DB. | |
4a02aae5 | 154 | $user1service = new \core_favourites\local\service\user_favourites_service($user1context, $repo); |
77105132 JD |
155 | |
156 | // Favourite a course. | |
157 | $favourite1 = $user1service->create_favourite('core_course', 'course', $course1context->instanceid, $course1context); | |
158 | $this->assertObjectHasAttribute('id', $favourite1); | |
159 | ||
160 | // Try to favourite the same course again. | |
161 | $this->expectException('moodle_exception'); | |
162 | $user1service->create_favourite('core_course', 'course', $course1context->instanceid, $course1context); | |
163 | } | |
164 | ||
165 | /** | |
166 | * Test confirming that an exception is thrown if trying to favourite an item for a non-existent component. | |
167 | */ | |
168 | public function test_create_favourite_nonexistent_component() { | |
169 | list($user1context, $user2context, $course1context, $course2context) = $this->setup_users_and_courses(); | |
170 | ||
171 | // Get a user_favourites_service for the user. | |
172 | $repo = $this->get_mock_repository([]); // Mock repository, using the array as a mock DB. | |
4a02aae5 | 173 | $user1service = new \core_favourites\local\service\user_favourites_service($user1context, $repo); |
77105132 JD |
174 | |
175 | // Try to favourite something in a non-existent component. | |
176 | $this->expectException('moodle_exception'); | |
177 | $user1service->create_favourite('core_cccourse', 'my_area', $course1context->instanceid, $course1context); | |
178 | } | |
179 | ||
180 | /** | |
181 | * Test fetching favourites for single user, by area. | |
182 | */ | |
183 | public function test_find_favourites_by_type_single_user() { | |
184 | list($user1context, $user2context, $course1context, $course2context) = $this->setup_users_and_courses(); | |
185 | ||
186 | // Get a user_favourites_service for the user. | |
187 | $repo = $this->get_mock_repository([]); // Mock repository, using the array as a mock DB. | |
4a02aae5 | 188 | $service = new \core_favourites\local\service\user_favourites_service($user1context, $repo); |
77105132 JD |
189 | |
190 | // Favourite 2 courses, in separate areas. | |
191 | $fav1 = $service->create_favourite('core_course', 'course', $course1context->instanceid, $course1context); | |
192 | $fav2 = $service->create_favourite('core_course', 'anothertype', $course2context->instanceid, $course2context); | |
193 | ||
194 | // Verify we can get favourites by area. | |
195 | $favourites = $service->find_favourites_by_type('core_course', 'course'); | |
196 | $this->assertInternalType('array', $favourites); | |
197 | $this->assertCount(1, $favourites); // We only get favourites for the 'core_course/course' area. | |
198 | $this->assertAttributeEquals($fav1->id, 'id', $favourites[$fav1->id]); | |
199 | ||
200 | $favourites = $service->find_favourites_by_type('core_course', 'anothertype'); | |
201 | $this->assertInternalType('array', $favourites); | |
202 | $this->assertCount(1, $favourites); // We only get favourites for the 'core_course/course' area. | |
203 | $this->assertAttributeEquals($fav2->id, 'id', $favourites[$fav2->id]); | |
204 | } | |
205 | ||
206 | /** | |
207 | * Make sure the find_favourites_by_type() method only returns favourites for the scoped user. | |
208 | */ | |
209 | public function test_find_favourites_by_type_multiple_users() { | |
210 | list($user1context, $user2context, $course1context, $course2context) = $this->setup_users_and_courses(); | |
211 | ||
212 | // Get a user_favourites_service for 2 users. | |
213 | $repo = $this->get_mock_repository([]); | |
4a02aae5 JD |
214 | $user1service = new \core_favourites\local\service\user_favourites_service($user1context, $repo); |
215 | $user2service = new \core_favourites\local\service\user_favourites_service($user2context, $repo); | |
77105132 JD |
216 | |
217 | // Now, as each user, favourite the same course. | |
218 | $fav1 = $user1service->create_favourite('core_course', 'course', $course1context->instanceid, $course1context); | |
219 | $fav2 = $user2service->create_favourite('core_course', 'course', $course1context->instanceid, $course1context); | |
220 | ||
221 | // Verify find_favourites_by_type only returns results for the user to which the service is scoped. | |
222 | $user1favourites = $user1service->find_favourites_by_type('core_course', 'course'); | |
223 | $this->assertInternalType('array', $user1favourites); | |
224 | $this->assertCount(1, $user1favourites); // We only get favourites for the 'core_course/course' area for $user1. | |
225 | $this->assertAttributeEquals($fav1->id, 'id', $user1favourites[$fav1->id]); | |
226 | ||
227 | $user2favourites = $user2service->find_favourites_by_type('core_course', 'course'); | |
228 | $this->assertInternalType('array', $user2favourites); | |
229 | $this->assertCount(1, $user2favourites); // We only get favourites for the 'core_course/course' area for $user2. | |
230 | $this->assertAttributeEquals($fav2->id, 'id', $user2favourites[$fav2->id]); | |
231 | } | |
232 | ||
233 | /** | |
234 | * Test confirming that an exception is thrown if trying to get favourites for a non-existent component. | |
235 | */ | |
236 | public function test_find_favourites_by_type_nonexistent_component() { | |
237 | list($user1context, $user2context, $course1context, $course2context) = $this->setup_users_and_courses(); | |
238 | ||
239 | // Get a user_favourites_service for the user. | |
240 | $repo = $this->get_mock_repository([]); | |
4a02aae5 | 241 | $service = new \core_favourites\local\service\user_favourites_service($user1context, $repo); |
77105132 JD |
242 | |
243 | // Verify we get an exception if we try to search for favourites in an invalid component. | |
244 | $this->expectException('moodle_exception'); | |
245 | $service->find_favourites_by_type('cccore_notreal', 'something'); | |
246 | } | |
247 | ||
248 | /** | |
249 | * Test confirming the basic deletion behaviour. | |
250 | */ | |
251 | public function test_delete_favourite_basic() { | |
252 | list($user1context, $user2context, $course1context, $course2context) = $this->setup_users_and_courses(); | |
253 | ||
254 | // Get a user_favourites_service for the user. | |
255 | $repo = $this->get_mock_repository([]); | |
4a02aae5 | 256 | $service = new \core_favourites\local\service\user_favourites_service($user1context, $repo); |
77105132 JD |
257 | |
258 | // Favourite a course. | |
259 | $fav1 = $service->create_favourite('core_course', 'course', $course1context->instanceid, $course1context); | |
260 | $this->assertTrue($repo->exists($fav1->id)); | |
261 | ||
262 | // Delete the favourite. | |
263 | $service->delete_favourite('core_course', 'course', $course1context->instanceid, $course1context); | |
264 | ||
265 | // Verify the favourite doesn't exist. | |
266 | $this->assertFalse($repo->exists($fav1->id)); | |
267 | ||
268 | // Try to delete a favourite which we know doesn't exist. | |
269 | $this->expectException(\moodle_exception::class); | |
270 | $service->delete_favourite('core_course', 'course', $course1context->instanceid, $course1context); | |
271 | } | |
272 | } |