This repository has been archived by the owner on Dec 13, 2024. It is now read-only.
forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservices_content_item_service_test.php
228 lines (187 loc) · 10.4 KB
/
services_content_item_service_test.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
<?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 tests for the content_item_service class.
*
* @package core
* @subpackage course
* @copyright 2020 Jake Dallimore <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace tests\course;
defined('MOODLE_INTERNAL') || die();
use \core_course\local\service\content_item_service;
use \core_course\local\repository\content_item_readonly_repository;
/**
* The tests for the content_item_service class.
*
* @copyright 2020 Jake Dallimore <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class services_content_item_service_testcase extends \advanced_testcase {
/**
* Test confirming that content items are returned by the service.
*/
public function test_get_content_items_for_user_in_course_basic() {
$this->resetAfterTest();
// Create a user in a course.
$course = $this->getDataGenerator()->create_course();
$user = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher');
$cis = new content_item_service(new content_item_readonly_repository());
$contentitems = $cis->get_content_items_for_user_in_course($user, $course);
foreach ($contentitems as $key => $contentitem) {
$this->assertObjectHasAttribute('id', $contentitem);
$this->assertObjectHasAttribute('name', $contentitem);
$this->assertObjectHasAttribute('title', $contentitem);
$this->assertObjectHasAttribute('link', $contentitem);
$this->assertObjectHasAttribute('icon', $contentitem);
$this->assertObjectHasAttribute('help', $contentitem);
$this->assertObjectHasAttribute('archetype', $contentitem);
$this->assertObjectHasAttribute('componentname', $contentitem);
}
}
/**
* Test confirming that access control is performed when asking the service to return content items for a user in a course.
*/
public function test_get_content_items_for_user_in_course_permissions() {
$this->resetAfterTest();
global $DB;
// Create a user in a course.
$course = $this->getDataGenerator()->create_course();
$user = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher');
// No cap override, so assign should be returned.
$cis = new content_item_service(new content_item_readonly_repository());
$contentitems = $cis->get_content_items_for_user_in_course($user, $course);
$this->assertContains('assign', array_column($contentitems, 'name'));
// Override the capability 'mod/assign:addinstance' for the 'editing teacher' role.
$teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'));
assign_capability('mod/assign:addinstance', CAP_PROHIBIT, $teacherrole->id, \context_course::instance($course->id));
$contentitems = $cis->get_content_items_for_user_in_course($user, $course);
$this->assertArrayNotHasKey('assign', $contentitems);
}
/**
* Test confirming that params can be added to the content item's link.
*/
public function test_get_content_item_for_user_in_course_link_params() {
$this->resetAfterTest();
// Create a user in a course.
$course = $this->getDataGenerator()->create_course();
$user = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher');
$cis = new content_item_service(new content_item_readonly_repository());
$contentitems = $cis->get_content_items_for_user_in_course($user, $course, ['sr' => 7]);
foreach ($contentitems as $item) {
$this->assertStringContainsString('sr=7', $item->link);
}
}
/**
* Test confirming that all content items can be fetched irrespective of permissions.
*/
public function test_get_all_content_items() {
$this->resetAfterTest();
global $DB;
// Create a user in a course.
$course = $this->getDataGenerator()->create_course();
$user = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher');
$cis = new content_item_service(new content_item_readonly_repository());
$allcontentitems = $cis->get_all_content_items($user);
$coursecontentitems = $cis->get_content_items_for_user_in_course($user, $course);
// The call to get_all_content_items() should return the same items as for the course,
// given the user in an editing teacher and can add manual lti instances.
$this->assertEquals(array_column($allcontentitems, 'name'), array_column($coursecontentitems, 'name'));
// Now removing the cap 'mod/lti:addinstance'. This will restrict those items returned by the course-specific method.
$teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'));
assign_capability('mod/lti:addinstance', CAP_PROHIBIT, $teacherrole->id, \context_course::instance($course->id));
// Verify that all items, including lti, are still returned by the get_all_content_items() call.
$allcontentitems = $cis->get_all_content_items($user);
$coursecontentitems = $cis->get_content_items_for_user_in_course($user, $course);
$this->assertNotContains('lti', array_column($coursecontentitems, 'name'));
$this->assertContains('lti', array_column($allcontentitems, 'name'));
}
/**
* Test confirming that a content item can be added to a user's favourites.
*/
public function test_add_to_user_favourites() {
$this->resetAfterTest();
// Create a user in a course.
$course = $this->getDataGenerator()->create_course();
$user = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher');
$cis = new content_item_service(new content_item_readonly_repository());
// Grab a the assign content item, which we'll favourite for the user.
$items = $cis->get_all_content_items($user);
$assign = $items[array_search('assign', array_column($items, 'name'))];
$contentitem = $cis->add_to_user_favourites($user, 'mod_assign', $assign->id);
// Verify the exported result is marked as a favourite.
$this->assertEquals('assign', $contentitem->name);
$this->assertTrue($contentitem->favourite);
// Verify the item is marked as a favourite when returned from the other service methods.
$allitems = $cis->get_all_content_items($user);
$allitemsassign = $allitems[array_search('assign', array_column($allitems, 'name'))];
$courseitems = $cis->get_content_items_for_user_in_course($user, $course);
$courseitemsassign = $courseitems[array_search('assign', array_column($courseitems, 'name'))];
$this->assertTrue($allitemsassign->favourite);
$this->assertTrue($courseitemsassign->favourite);
}
/**
* Test verifying that content items can be removed from a user's favourites.
*/
public function test_remove_from_user_favourites() {
$this->resetAfterTest();
// Create a user in a course.
$course = $this->getDataGenerator()->create_course();
$user = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher');
$cis = new content_item_service(new content_item_readonly_repository());
// Grab a the assign content item, which we'll favourite for the user.
$items = $cis->get_all_content_items($user);
$assign = $items[array_search('assign', array_column($items, 'name'))];
$cis->add_to_user_favourites($user, 'mod_assign', $assign->id);
// Now, remove the favourite, and verify it.
$contentitem = $cis->remove_from_user_favourites($user, 'mod_assign', $assign->id);
// Verify the exported result is not marked as a favourite.
$this->assertEquals('assign', $contentitem->name);
$this->assertFalse($contentitem->favourite);
// Verify the item is not marked as a favourite when returned from the other service methods.
$allitems = $cis->get_all_content_items($user);
$allitemsassign = $allitems[array_search('assign', array_column($allitems, 'name'))];
$courseitems = $cis->get_content_items_for_user_in_course($user, $course);
$courseitemsassign = $courseitems[array_search('assign', array_column($courseitems, 'name'))];
$this->assertFalse($allitemsassign->favourite);
$this->assertFalse($courseitemsassign->favourite);
}
/**
* Test that toggling a recommendation works as anticipated.
*/
public function test_toggle_recommendation() {
$this->resetAfterTest();
// Create a user in a course.
$course = $this->getDataGenerator()->create_course();
$user = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher');
$cis = new content_item_service(new content_item_readonly_repository());
// Grab a the assign content item, which we'll recommend for the user.
$items = $cis->get_all_content_items($user);
$assign = $items[array_search('assign', array_column($items, 'name'))];
$result = $cis->toggle_recommendation($assign->componentname, $assign->id);
$this->assertTrue($result);
$courseitems = $cis->get_all_content_items($user);
$courseitemsassign = $courseitems[array_search('assign', array_column($courseitems, 'name'))];
$this->assertTrue($courseitemsassign->recommended);
// Let's toggle the recommendation off.
$result = $cis->toggle_recommendation($assign->componentname, $assign->id);
$this->assertFalse($result);
$courseitems = $cis->get_all_content_items($user);
$courseitemsassign = $courseitems[array_search('assign', array_column($courseitems, 'name'))];
$this->assertFalse($courseitemsassign->recommended);
}
}