Commit | Line | Data |
---|---|---|
4a5acec2 DP |
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 | * Wiki module external functions tests. | |
19 | * | |
20 | * @package mod_wiki | |
21 | * @category external | |
22 | * @copyright 2015 Dani Palou <dani@moodle.com> | |
23 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
24 | * @since Moodle 3.1 | |
25 | */ | |
26 | ||
27 | defined('MOODLE_INTERNAL') || die(); | |
28 | ||
29 | global $CFG; | |
30 | ||
31 | require_once($CFG->dirroot . '/webservice/tests/helpers.php'); | |
32 | require_once($CFG->dirroot . '/mod/wiki/lib.php'); | |
33 | ||
34 | /** | |
35 | * Wiki module external functions tests | |
36 | * | |
37 | * @package mod_wiki | |
38 | * @category external | |
39 | * @copyright 2015 Dani Palou <dani@moodle.com> | |
40 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
41 | * @since Moodle 3.1 | |
42 | */ | |
43 | class mod_wiki_external_testcase extends externallib_advanced_testcase { | |
44 | ||
45 | /** | |
46 | * Set up for every test | |
47 | */ | |
48 | public function setUp() { | |
49 | global $DB; | |
50 | $this->resetAfterTest(); | |
51 | $this->setAdminUser(); | |
52 | ||
53 | // Setup test data. | |
54 | $this->course = $this->getDataGenerator()->create_course(); | |
55 | $this->wiki = $this->getDataGenerator()->create_module('wiki', array('course' => $this->course->id)); | |
56 | $this->context = context_module::instance($this->wiki->cmid); | |
57 | $this->cm = get_coursemodule_from_instance('wiki', $this->wiki->id); | |
58 | ||
59 | // Create users. | |
60 | $this->student = self::getDataGenerator()->create_user(); | |
61 | $this->teacher = self::getDataGenerator()->create_user(); | |
62 | ||
63 | // Users enrolments. | |
64 | $this->studentrole = $DB->get_record('role', array('shortname' => 'student')); | |
65 | $this->teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher')); | |
66 | $this->getDataGenerator()->enrol_user($this->student->id, $this->course->id, $this->studentrole->id, 'manual'); | |
67 | $this->getDataGenerator()->enrol_user($this->teacher->id, $this->course->id, $this->teacherrole->id, 'manual'); | |
68 | ||
69 | // Create first page. | |
70 | $this->firstpage = $this->getDataGenerator()->get_plugin_generator('mod_wiki')->create_first_page($this->wiki); | |
71 | } | |
72 | ||
73 | /* | |
74 | * Test get wikis by courses | |
75 | */ | |
76 | public function test_mod_wiki_get_wikis_by_courses() { | |
77 | ||
78 | // Create additional course. | |
79 | $course2 = self::getDataGenerator()->create_course(); | |
80 | ||
81 | // Second wiki. | |
82 | $record = new stdClass(); | |
83 | $record->course = $course2->id; | |
84 | $wiki2 = self::getDataGenerator()->create_module('wiki', $record); | |
85 | ||
86 | // Execute real Moodle enrolment as we'll call unenrol() method on the instance later. | |
87 | $enrol = enrol_get_plugin('manual'); | |
88 | $enrolinstances = enrol_get_instances($course2->id, true); | |
89 | foreach ($enrolinstances as $courseenrolinstance) { | |
90 | if ($courseenrolinstance->enrol == "manual") { | |
91 | $instance2 = $courseenrolinstance; | |
92 | break; | |
93 | } | |
94 | } | |
95 | $enrol->enrol_user($instance2, $this->student->id, $this->studentrole->id); | |
96 | ||
97 | self::setUser($this->student); | |
98 | ||
99 | $returndescription = mod_wiki_external::get_wikis_by_courses_returns(); | |
100 | ||
101 | // Create what we expect to be returned when querying the two courses. | |
102 | // First for the student user. | |
103 | $expectedfields = array('id', 'coursemodule', 'course', 'name', 'intro', 'introformat', 'firstpagetitle', 'wikimode', | |
104 | 'defaultformat', 'forceformat', 'editbegin', 'editend', 'section', 'visible', 'groupmode', | |
105 | 'groupingid'); | |
106 | ||
107 | // Add expected coursemodule and data. | |
108 | $wiki1 = $this->wiki; | |
109 | $wiki1->coursemodule = $wiki1->cmid; | |
110 | $wiki1->introformat = 1; | |
111 | $wiki1->section = 0; | |
112 | $wiki1->visible = true; | |
113 | $wiki1->groupmode = 0; | |
114 | $wiki1->groupingid = 0; | |
115 | ||
116 | $wiki2->coursemodule = $wiki2->cmid; | |
117 | $wiki2->introformat = 1; | |
118 | $wiki2->section = 0; | |
119 | $wiki2->visible = true; | |
120 | $wiki2->groupmode = 0; | |
121 | $wiki2->groupingid = 0; | |
122 | ||
123 | foreach ($expectedfields as $field) { | |
124 | $expected1[$field] = $wiki1->{$field}; | |
125 | $expected2[$field] = $wiki2->{$field}; | |
126 | } | |
127 | // Users can create pages by default. | |
128 | $expected1['cancreatepages'] = true; | |
129 | $expected2['cancreatepages'] = true; | |
130 | ||
131 | $expectedwikis = array($expected2, $expected1); | |
132 | ||
133 | // Call the external function passing course ids. | |
134 | $result = mod_wiki_external::get_wikis_by_courses(array($course2->id, $this->course->id)); | |
135 | $result = external_api::clean_returnvalue($returndescription, $result); | |
136 | ||
137 | $this->assertEquals($expectedwikis, $result['wikis']); | |
138 | $this->assertCount(0, $result['warnings']); | |
139 | ||
140 | // Call the external function without passing course id. | |
141 | $result = mod_wiki_external::get_wikis_by_courses(); | |
142 | $result = external_api::clean_returnvalue($returndescription, $result); | |
143 | $this->assertEquals($expectedwikis, $result['wikis']); | |
144 | $this->assertCount(0, $result['warnings']); | |
145 | ||
146 | // Unenrol user from second course and alter expected wikis. | |
147 | $enrol->unenrol_user($instance2, $this->student->id); | |
148 | array_shift($expectedwikis); | |
149 | ||
150 | // Call the external function without passing course id. | |
151 | $result = mod_wiki_external::get_wikis_by_courses(); | |
152 | $result = external_api::clean_returnvalue($returndescription, $result); | |
153 | $this->assertEquals($expectedwikis, $result['wikis']); | |
154 | ||
155 | // Call for the second course we unenrolled the user from, expected warning. | |
156 | $result = mod_wiki_external::get_wikis_by_courses(array($course2->id)); | |
157 | $this->assertCount(1, $result['warnings']); | |
158 | $this->assertEquals('1', $result['warnings'][0]['warningcode']); | |
159 | $this->assertEquals($course2->id, $result['warnings'][0]['itemid']); | |
160 | ||
161 | // Now, try as a teacher for getting all the additional fields. | |
162 | self::setUser($this->teacher); | |
163 | ||
164 | $additionalfields = array('timecreated', 'timemodified'); | |
165 | ||
166 | foreach ($additionalfields as $field) { | |
167 | $expectedwikis[0][$field] = $wiki1->{$field}; | |
168 | } | |
169 | ||
170 | $result = mod_wiki_external::get_wikis_by_courses(); | |
171 | $result = external_api::clean_returnvalue($returndescription, $result); | |
172 | $this->assertEquals($expectedwikis, $result['wikis']); | |
173 | ||
174 | // Admin also should get all the information. | |
175 | self::setAdminUser(); | |
176 | ||
177 | $result = mod_wiki_external::get_wikis_by_courses(array($this->course->id)); | |
178 | $result = external_api::clean_returnvalue($returndescription, $result); | |
179 | $this->assertEquals($expectedwikis, $result['wikis']); | |
180 | ||
181 | // Now, prohibit capabilities. | |
182 | $this->setUser($this->student); | |
183 | $contextcourse1 = context_course::instance($this->course->id); | |
184 | // Prohibit capability = mod:wiki:viewpage on Course1 for students. | |
185 | assign_capability('mod/wiki:viewpage', CAP_PROHIBIT, $this->studentrole->id, $contextcourse1->id); | |
186 | accesslib_clear_all_caches_for_unit_testing(); | |
187 | ||
188 | $wikis = mod_wiki_external::get_wikis_by_courses(array($this->course->id)); | |
189 | $wikis = external_api::clean_returnvalue(mod_wiki_external::get_wikis_by_courses_returns(), $wikis); | |
190 | $this->assertFalse(isset($wikis['wikis'][0]['intro'])); | |
191 | ||
192 | // Prohibit capability = mod:wiki:createpage on Course1 for students. | |
193 | assign_capability('mod/wiki:createpage', CAP_PROHIBIT, $this->studentrole->id, $contextcourse1->id); | |
194 | accesslib_clear_all_caches_for_unit_testing(); | |
195 | ||
196 | $wikis = mod_wiki_external::get_wikis_by_courses(array($this->course->id)); | |
197 | $wikis = external_api::clean_returnvalue(mod_wiki_external::get_wikis_by_courses_returns(), $wikis); | |
198 | $this->assertFalse($wikis['wikis'][0]['cancreatepages']); | |
c1baf89e DP |
199 | |
200 | } | |
201 | ||
202 | /** | |
203 | * Test view_wiki. | |
204 | */ | |
205 | public function test_view_wiki() { | |
206 | ||
207 | // Test invalid instance id. | |
208 | try { | |
209 | mod_wiki_external::view_wiki(0); | |
210 | $this->fail('Exception expected due to invalid mod_wiki instance id.'); | |
211 | } catch (moodle_exception $e) { | |
212 | $this->assertEquals('incorrectwikiid', $e->errorcode); | |
213 | } | |
214 | ||
215 | // Test not-enrolled user. | |
216 | $usernotenrolled = self::getDataGenerator()->create_user(); | |
217 | $this->setUser($usernotenrolled); | |
218 | try { | |
219 | mod_wiki_external::view_wiki($this->wiki->id); | |
220 | $this->fail('Exception expected due to not enrolled user.'); | |
221 | } catch (moodle_exception $e) { | |
222 | $this->assertEquals('requireloginerror', $e->errorcode); | |
223 | } | |
224 | ||
225 | // Test user with full capabilities. | |
226 | $this->setUser($this->student); | |
227 | ||
228 | // Trigger and capture the event. | |
229 | $sink = $this->redirectEvents(); | |
230 | ||
231 | $result = mod_wiki_external::view_wiki($this->wiki->id); | |
232 | $result = external_api::clean_returnvalue(mod_wiki_external::view_wiki_returns(), $result); | |
233 | ||
234 | $events = $sink->get_events(); | |
235 | $this->assertCount(1, $events); | |
236 | $event = array_shift($events); | |
237 | ||
238 | // Checking that the event contains the expected values. | |
239 | $this->assertInstanceOf('\mod_wiki\event\course_module_viewed', $event); | |
240 | $this->assertEquals($this->context, $event->get_context()); | |
241 | $moodlewiki = new \moodle_url('/mod/wiki/view.php', array('id' => $this->cm->id)); | |
242 | $this->assertEquals($moodlewiki, $event->get_url()); | |
243 | $this->assertEventContextNotUsed($event); | |
244 | $this->assertNotEmpty($event->get_name()); | |
245 | ||
246 | // Test user with no capabilities. | |
247 | // We need a explicit prohibit since this capability is allowed for students by default. | |
248 | assign_capability('mod/wiki:viewpage', CAP_PROHIBIT, $this->studentrole->id, $this->context->id); | |
249 | accesslib_clear_all_caches_for_unit_testing(); | |
250 | ||
251 | try { | |
252 | mod_wiki_external::view_wiki($this->wiki->id); | |
253 | $this->fail('Exception expected due to missing capability.'); | |
254 | } catch (moodle_exception $e) { | |
255 | $this->assertEquals('cannotviewpage', $e->errorcode); | |
256 | } | |
257 | ||
258 | } | |
259 | ||
260 | /** | |
261 | * Test view_page. | |
262 | */ | |
263 | public function test_view_page() { | |
264 | ||
265 | // Test invalid page id. | |
266 | try { | |
267 | mod_wiki_external::view_page(0); | |
268 | $this->fail('Exception expected due to invalid view_page page id.'); | |
269 | } catch (moodle_exception $e) { | |
270 | $this->assertEquals('incorrectpageid', $e->errorcode); | |
271 | } | |
272 | ||
273 | // Test not-enrolled user. | |
274 | $usernotenrolled = self::getDataGenerator()->create_user(); | |
275 | $this->setUser($usernotenrolled); | |
276 | try { | |
277 | mod_wiki_external::view_page($this->firstpage->id); | |
278 | $this->fail('Exception expected due to not enrolled user.'); | |
279 | } catch (moodle_exception $e) { | |
280 | $this->assertEquals('requireloginerror', $e->errorcode); | |
281 | } | |
282 | ||
283 | // Test user with full capabilities. | |
284 | $this->setUser($this->student); | |
285 | ||
286 | // Trigger and capture the event. | |
287 | $sink = $this->redirectEvents(); | |
288 | ||
289 | $result = mod_wiki_external::view_page($this->firstpage->id); | |
290 | $result = external_api::clean_returnvalue(mod_wiki_external::view_page_returns(), $result); | |
291 | ||
292 | $events = $sink->get_events(); | |
293 | $this->assertCount(1, $events); | |
294 | $event = array_shift($events); | |
295 | ||
296 | // Checking that the event contains the expected values. | |
297 | $this->assertInstanceOf('\mod_wiki\event\page_viewed', $event); | |
298 | $this->assertEquals($this->context, $event->get_context()); | |
299 | $pageurl = new \moodle_url('/mod/wiki/view.php', array('pageid' => $this->firstpage->id)); | |
300 | $this->assertEquals($pageurl, $event->get_url()); | |
301 | $this->assertEventContextNotUsed($event); | |
302 | $this->assertNotEmpty($event->get_name()); | |
303 | ||
304 | // Test user with no capabilities. | |
305 | // We need a explicit prohibit since this capability is allowed for students by default. | |
306 | assign_capability('mod/wiki:viewpage', CAP_PROHIBIT, $this->studentrole->id, $this->context->id); | |
307 | accesslib_clear_all_caches_for_unit_testing(); | |
308 | ||
309 | try { | |
310 | mod_wiki_external::view_page($this->firstpage->id); | |
311 | $this->fail('Exception expected due to missing capability.'); | |
312 | } catch (moodle_exception $e) { | |
313 | $this->assertEquals('cannotviewpage', $e->errorcode); | |
314 | } | |
315 | ||
4a5acec2 DP |
316 | } |
317 | ||
318 | } |