Proyectos de Subversion Moodle

Rev

Rev 11 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
namespace core_favourites;
18
 
19
use core_favourites\local\entity\favourite;
20
 
21
/**
22
 * Test class covering the component_favourite_service within the service layer of favourites.
23
 *
24
 * @package    core_favourites
25
 * @category   test
26
 * @copyright  2019 Jake Dallimore <jrhdallimore@gmail.com>
27
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28
 */
1441 ariadna 29
final class component_favourite_service_test extends \advanced_testcase {
1 efrain 30
 
31
    public function setUp(): void {
1441 ariadna 32
        parent::setUp();
1 efrain 33
        $this->resetAfterTest();
34
    }
35
 
36
    // Basic setup stuff to be reused in most tests.
37
    protected function setup_users_and_courses() {
38
        $user1 = self::getDataGenerator()->create_user();
39
        $user1context = \context_user::instance($user1->id);
40
        $user2 = self::getDataGenerator()->create_user();
41
        $user2context = \context_user::instance($user2->id);
42
        $course1 = self::getDataGenerator()->create_course();
43
        $course2 = self::getDataGenerator()->create_course();
44
        $course1context = \context_course::instance($course1->id);
45
        $course2context = \context_course::instance($course2->id);
46
        return [$user1context, $user2context, $course1context, $course2context];
47
    }
48
 
49
    /**
50
     * Generates an in-memory repository for testing, using an array store for CRUD stuff.
51
     *
52
     * @param array $mockstore
53
     * @return \PHPUnit\Framework\MockObject\MockObject
54
     */
55
    protected function get_mock_repository(array $mockstore) {
56
        // This mock will just store data in an array.
57
        $mockrepo = $this->getMockBuilder(\core_favourites\local\repository\favourite_repository_interface::class)
58
            ->onlyMethods([])
59
            ->getMock();
60
        $mockrepo->expects($this->any())
61
            ->method('add')
62
            ->will($this->returnCallback(function(favourite $favourite) use (&$mockstore) {
63
                // Mock implementation of repository->add(), where an array is used instead of the DB.
64
                // Duplicates are confirmed via the unique key, and exceptions thrown just like a real repo.
65
                $key = $favourite->userid . $favourite->component . $favourite->itemtype . $favourite->itemid
66
                    . $favourite->contextid;
67
 
68
                // Check the objects for the unique key.
69
                foreach ($mockstore as $item) {
70
                    if ($item->uniquekey == $key) {
71
                        throw new \moodle_exception('Favourite already exists');
72
                    }
73
                }
74
                $index = count($mockstore);     // Integer index.
75
                $favourite->uniquekey = $key;   // Simulate the unique key constraint.
76
                $favourite->id = $index;
77
                $mockstore[$index] = $favourite;
78
                return $mockstore[$index];
79
            })
80
        );
81
        $mockrepo->expects($this->any())
82
            ->method('find_by')
83
            ->will($this->returnCallback(function(array $criteria, int $limitfrom = 0, int $limitnum = 0) use (&$mockstore) {
84
                // Check the mockstore for all objects with properties matching the key => val pairs in $criteria.
85
                foreach ($mockstore as $index => $mockrow) {
86
                    $mockrowarr = (array)$mockrow;
87
                    if (array_diff_assoc($criteria, $mockrowarr) == []) {
88
                        $returns[$index] = $mockrow;
89
                    }
90
                }
91
                // Return a subset of the records, according to the paging options, if set.
92
                if ($limitnum != 0) {
93
                    return array_slice($returns, $limitfrom, $limitnum);
94
                }
95
                // Otherwise, just return the full set.
96
                return $returns;
97
            })
98
        );
99
        $mockrepo->expects($this->any())
100
            ->method('find_favourite')
101
            ->will($this->returnCallback(function(int $userid, string $comp, string $type, int $id, int $ctxid) use (&$mockstore) {
102
                // Check the mockstore for all objects with properties matching the key => val pairs in $criteria.
103
                $crit = ['userid' => $userid, 'component' => $comp, 'itemtype' => $type, 'itemid' => $id, 'contextid' => $ctxid];
104
                foreach ($mockstore as $fakerow) {
105
                    $fakerowarr = (array)$fakerow;
106
                    if (array_diff_assoc($crit, $fakerowarr) == []) {
107
                        return $fakerow;
108
                    }
109
                }
110
                throw new \dml_missing_record_exception("Item not found");
111
            })
112
        );
113
        $mockrepo->expects($this->any())
114
            ->method('find')
115
            ->will($this->returnCallback(function(int $id) use (&$mockstore) {
116
                return $mockstore[$id];
117
            })
118
        );
119
        $mockrepo->expects($this->any())
120
            ->method('exists')
121
            ->will($this->returnCallback(function(int $id) use (&$mockstore) {
122
                return array_key_exists($id, $mockstore);
123
            })
124
        );
125
        $mockrepo->expects($this->any())
126
            ->method('count_by')
127
            ->will($this->returnCallback(function(array $criteria) use (&$mockstore) {
128
                $count = 0;
129
                // Check the mockstore for all objects with properties matching the key => val pairs in $criteria.
130
                foreach ($mockstore as $index => $mockrow) {
131
                    $mockrowarr = (array)$mockrow;
132
                    if (array_diff_assoc($criteria, $mockrowarr) == []) {
133
                        $count++;
134
                    }
135
                }
136
                return $count;
137
            })
138
        );
139
        $mockrepo->expects($this->any())
140
            ->method('delete')
141
            ->will($this->returnCallback(function(int $id) use (&$mockstore) {
142
                foreach ($mockstore as $mockrow) {
143
                    if ($mockrow->id == $id) {
144
                        unset($mockstore[$id]);
145
                    }
146
                }
147
            })
148
        );
149
        $mockrepo->expects($this->any())
150
            ->method('delete_by')
151
            ->will($this->returnCallback(function(array $criteria) use (&$mockstore) {
152
                // Check the mockstore for all objects with properties matching the key => val pairs in $criteria.
153
                foreach ($mockstore as $index => $mockrow) {
154
                    $mockrowarr = (array)$mockrow;
155
                    if (array_diff_assoc($criteria, $mockrowarr) == []) {
156
                        unset($mockstore[$index]);
157
                    }
158
                }
159
            })
160
        );
161
        $mockrepo->expects($this->any())
162
            ->method('exists_by')
163
            ->will($this->returnCallback(function(array $criteria) use (&$mockstore) {
164
                // Check the mockstore for all objects with properties matching the key => val pairs in $criteria.
165
                foreach ($mockstore as $index => $mockrow) {
166
                    $mockrowarr = (array)$mockrow;
167
                    echo "Here";
168
                    if (array_diff_assoc($criteria, $mockrowarr) == []) {
169
                        return true;
170
                    }
171
                }
172
                return false;
173
            })
174
        );
175
        return $mockrepo;
176
    }
177
 
178
    /**
179
     * Test confirming the deletion of favourites by type and item, but with no optional context filter provided.
180
     */
11 efrain 181
    public function test_delete_favourites_by_type_and_item(): void {
1 efrain 182
        list($user1context, $user2context, $course1context, $course2context) = $this->setup_users_and_courses();
183
 
184
        // Get a user_favourite_service for each user.
185
        $repo = $this->get_mock_repository([]); // Mock repository, using the array as a mock DB.
186
        $user1service = new \core_favourites\local\service\user_favourite_service($user1context, $repo);
187
        $user2service = new \core_favourites\local\service\user_favourite_service($user2context, $repo);
188
 
189
        // Favourite both courses for both users.
190
        $fav1 = $user1service->create_favourite('core_course', 'course', $course1context->instanceid, $course1context);
191
        $fav2 = $user2service->create_favourite('core_course', 'course', $course1context->instanceid, $course1context);
192
        $fav3 = $user1service->create_favourite('core_course', 'course', $course2context->instanceid, $course2context);
193
        $fav4 = $user2service->create_favourite('core_course', 'course', $course2context->instanceid, $course2context);
194
        $this->assertTrue($repo->exists($fav1->id));
195
        $this->assertTrue($repo->exists($fav2->id));
196
        $this->assertTrue($repo->exists($fav3->id));
197
        $this->assertTrue($repo->exists($fav4->id));
198
 
199
        // Favourite something else arbitrarily.
200
        $fav5 = $user2service->create_favourite('core_user', 'course', $course2context->instanceid, $course2context);
201
        $fav6 = $user2service->create_favourite('core_course', 'whatnow', $course2context->instanceid, $course2context);
202
 
203
        // Get a component_favourite_service to perform the type based deletion.
204
        $service = new \core_favourites\local\service\component_favourite_service('core_course', $repo);
205
 
206
        // Delete all 'course' type favourites (for all users who have favourited course1).
207
        $service->delete_favourites_by_type_and_item('course', $course1context->instanceid);
208
 
209
        // Delete all 'course' type favourites (for all users who have favourited course2).
210
        $service->delete_favourites_by_type_and_item('course', $course2context->instanceid);
211
 
212
        // Verify the favourites don't exist.
213
        $this->assertFalse($repo->exists($fav1->id));
214
        $this->assertFalse($repo->exists($fav2->id));
215
        $this->assertFalse($repo->exists($fav3->id));
216
        $this->assertFalse($repo->exists($fav4->id));
217
 
218
        // Verify favourites of other types or for other components are not affected.
219
        $this->assertTrue($repo->exists($fav5->id));
220
        $this->assertTrue($repo->exists($fav6->id));
221
 
222
        // Try to delete favourites for a type which we know doesn't exist. Verify no exception.
223
        $this->assertNull($service->delete_favourites_by_type_and_item('course', $course1context->instanceid));
224
    }
225
 
226
    /**
227
     * Test confirming the deletion of favourites by type and item and with the optional context filter provided.
228
     */
11 efrain 229
    public function test_delete_favourites_by_type_and_item_with_context(): void {
1 efrain 230
        list($user1context, $user2context, $course1context, $course2context) = $this->setup_users_and_courses();
231
 
232
        // Get a user_favourite_service for each user.
233
        $repo = $this->get_mock_repository([]); // Mock repository, using the array as a mock DB.
234
        $user1service = new \core_favourites\local\service\user_favourite_service($user1context, $repo);
235
        $user2service = new \core_favourites\local\service\user_favourite_service($user2context, $repo);
236
 
237
        // Favourite both courses for both users.
238
        $fav1 = $user1service->create_favourite('core_course', 'course', $course1context->instanceid, $course1context);
239
        $fav2 = $user2service->create_favourite('core_course', 'course', $course1context->instanceid, $course1context);
240
        $fav3 = $user1service->create_favourite('core_course', 'course', $course2context->instanceid, $course2context);
241
        $fav4 = $user2service->create_favourite('core_course', 'course', $course2context->instanceid, $course2context);
242
        $this->assertTrue($repo->exists($fav1->id));
243
        $this->assertTrue($repo->exists($fav2->id));
244
        $this->assertTrue($repo->exists($fav3->id));
245
        $this->assertTrue($repo->exists($fav4->id));
246
 
247
        // Favourite something else arbitrarily.
248
        $fav5 = $user2service->create_favourite('core_user', 'course', $course1context->instanceid, $course1context);
249
        $fav6 = $user2service->create_favourite('core_course', 'whatnow', $course1context->instanceid, $course1context);
250
 
251
        // Favourite the courses again, but this time in another context.
252
        $fav7 = $user1service->create_favourite('core_course', 'course', $course1context->instanceid, \context_system::instance());
253
        $fav8 = $user2service->create_favourite('core_course', 'course', $course1context->instanceid, \context_system::instance());
254
        $fav9 = $user1service->create_favourite('core_course', 'course', $course2context->instanceid, \context_system::instance());
255
        $fav10 = $user2service->create_favourite('core_course', 'course', $course2context->instanceid, \context_system::instance());
256
 
257
        // Get a component_favourite_service to perform the type based deletion.
258
        $service = new \core_favourites\local\service\component_favourite_service('core_course', $repo);
259
 
260
        // Delete all 'course' type favourites (for all users at ONLY the course 1 context).
261
        $service->delete_favourites_by_type_and_item('course', $course1context->instanceid, $course1context);
262
 
263
        // Verify the favourites for course 1 context don't exist.
264
        $this->assertFalse($repo->exists($fav1->id));
265
        $this->assertFalse($repo->exists($fav2->id));
266
 
267
        // Verify the favourites for the same component and type, but NOT for the same contextid and unaffected.
268
        $this->assertTrue($repo->exists($fav3->id));
269
        $this->assertTrue($repo->exists($fav4->id));
270
 
271
        // Verify favourites of other types or for other components are not affected.
272
        $this->assertTrue($repo->exists($fav5->id));
273
        $this->assertTrue($repo->exists($fav6->id));
274
 
275
        // Verify the course favourite at the system context are unaffected.
276
        $this->assertTrue($repo->exists($fav7->id));
277
        $this->assertTrue($repo->exists($fav8->id));
278
        $this->assertTrue($repo->exists($fav9->id));
279
        $this->assertTrue($repo->exists($fav10->id));
280
 
281
        // Try to delete favourites for a type which we know doesn't exist. Verify no exception.
282
        $this->assertNull($service->delete_favourites_by_type_and_item('course', $course1context->instanceid, $course1context));
283
    }
284
}