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(); | |
ed4fb2bf | 61 | $this->student2 = self::getDataGenerator()->create_user(); |
4a5acec2 DP |
62 | $this->teacher = self::getDataGenerator()->create_user(); |
63 | ||
64 | // Users enrolments. | |
65 | $this->studentrole = $DB->get_record('role', array('shortname' => 'student')); | |
66 | $this->teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher')); | |
67 | $this->getDataGenerator()->enrol_user($this->student->id, $this->course->id, $this->studentrole->id, 'manual'); | |
ed4fb2bf | 68 | $this->getDataGenerator()->enrol_user($this->student2->id, $this->course->id, $this->studentrole->id, 'manual'); |
4a5acec2 DP |
69 | $this->getDataGenerator()->enrol_user($this->teacher->id, $this->course->id, $this->teacherrole->id, 'manual'); |
70 | ||
ed4fb2bf | 71 | // Create first pages. |
4a5acec2 DP |
72 | $this->firstpage = $this->getDataGenerator()->get_plugin_generator('mod_wiki')->create_first_page($this->wiki); |
73 | } | |
74 | ||
ed4fb2bf DP |
75 | /** |
76 | * Create two collaborative wikis (separate/visible groups), 2 groups and a first page for each wiki and group. | |
77 | */ | |
78 | private function create_collaborative_wikis_with_groups() { | |
79 | // Create groups and add student to one of them. | |
80 | if (!isset($this->group1)) { | |
81 | $this->group1 = $this->getDataGenerator()->create_group(array('courseid' => $this->course->id)); | |
82 | $this->getDataGenerator()->create_group_member(array('userid' => $this->student->id, 'groupid' => $this->group1->id)); | |
83 | $this->getDataGenerator()->create_group_member(array('userid' => $this->student2->id, 'groupid' => $this->group1->id)); | |
84 | } | |
85 | if (!isset($this->group2)) { | |
86 | $this->group2 = $this->getDataGenerator()->create_group(array('courseid' => $this->course->id)); | |
87 | } | |
88 | ||
89 | // Create two collaborative wikis. | |
90 | $this->wikisep = $this->getDataGenerator()->create_module('wiki', | |
91 | array('course' => $this->course->id, 'groupmode' => SEPARATEGROUPS)); | |
92 | $this->wikivis = $this->getDataGenerator()->create_module('wiki', | |
93 | array('course' => $this->course->id, 'groupmode' => VISIBLEGROUPS)); | |
94 | ||
95 | // Create pages. | |
96 | $wikigenerator = $this->getDataGenerator()->get_plugin_generator('mod_wiki'); | |
97 | $this->fpsepg1 = $wikigenerator->create_first_page($this->wikisep, array('group' => $this->group1->id)); | |
98 | $this->fpsepg2 = $wikigenerator->create_first_page($this->wikisep, array('group' => $this->group2->id)); | |
99 | $this->fpsepall = $wikigenerator->create_first_page($this->wikisep, array('group' => 0)); // All participants. | |
100 | $this->fpvisg1 = $wikigenerator->create_first_page($this->wikivis, array('group' => $this->group1->id)); | |
101 | $this->fpvisg2 = $wikigenerator->create_first_page($this->wikivis, array('group' => $this->group2->id)); | |
102 | $this->fpvisall = $wikigenerator->create_first_page($this->wikivis, array('group' => 0)); // All participants. | |
103 | } | |
104 | ||
105 | /** | |
106 | * Create two individual wikis (separate/visible groups), 2 groups and a first page for each wiki and group. | |
107 | */ | |
108 | private function create_individual_wikis_with_groups() { | |
109 | // Create groups and add student to one of them. | |
110 | if (!isset($this->group1)) { | |
111 | $this->group1 = $this->getDataGenerator()->create_group(array('courseid' => $this->course->id)); | |
112 | $this->getDataGenerator()->create_group_member(array('userid' => $this->student->id, 'groupid' => $this->group1->id)); | |
113 | $this->getDataGenerator()->create_group_member(array('userid' => $this->student2->id, 'groupid' => $this->group1->id)); | |
114 | } | |
115 | if (!isset($this->group2)) { | |
116 | $this->group2 = $this->getDataGenerator()->create_group(array('courseid' => $this->course->id)); | |
117 | } | |
118 | ||
119 | // Create two individual wikis. | |
120 | $this->wikisepind = $this->getDataGenerator()->create_module('wiki', array('course' => $this->course->id, | |
121 | 'groupmode' => SEPARATEGROUPS, 'wikimode' => 'individual')); | |
122 | $this->wikivisind = $this->getDataGenerator()->create_module('wiki', array('course' => $this->course->id, | |
123 | 'groupmode' => VISIBLEGROUPS, 'wikimode' => 'individual')); | |
124 | ||
125 | // Create pages. Student can only create pages in his groups. | |
126 | $wikigenerator = $this->getDataGenerator()->get_plugin_generator('mod_wiki'); | |
127 | $this->setUser($this->teacher); | |
128 | $this->fpsepg1indt = $wikigenerator->create_first_page($this->wikisepind, array('group' => $this->group1->id)); | |
129 | $this->fpsepg2indt = $wikigenerator->create_first_page($this->wikisepind, array('group' => $this->group2->id)); | |
130 | $this->fpsepallindt = $wikigenerator->create_first_page($this->wikisepind, array('group' => 0)); // All participants. | |
131 | $this->fpvisg1indt = $wikigenerator->create_first_page($this->wikivisind, array('group' => $this->group1->id)); | |
132 | $this->fpvisg2indt = $wikigenerator->create_first_page($this->wikivisind, array('group' => $this->group2->id)); | |
133 | $this->fpvisallindt = $wikigenerator->create_first_page($this->wikivisind, array('group' => 0)); // All participants. | |
134 | ||
135 | $this->setUser($this->student); | |
136 | $this->fpsepg1indstu = $wikigenerator->create_first_page($this->wikisepind, array('group' => $this->group1->id)); | |
137 | $this->fpvisg1indstu = $wikigenerator->create_first_page($this->wikivisind, array('group' => $this->group1->id)); | |
138 | ||
139 | $this->setUser($this->student2); | |
140 | $this->fpsepg1indstu2 = $wikigenerator->create_first_page($this->wikisepind, array('group' => $this->group1->id)); | |
141 | $this->fpvisg1indstu2 = $wikigenerator->create_first_page($this->wikivisind, array('group' => $this->group1->id)); | |
142 | ||
143 | } | |
144 | ||
4a5acec2 DP |
145 | /* |
146 | * Test get wikis by courses | |
147 | */ | |
148 | public function test_mod_wiki_get_wikis_by_courses() { | |
149 | ||
150 | // Create additional course. | |
151 | $course2 = self::getDataGenerator()->create_course(); | |
152 | ||
153 | // Second wiki. | |
154 | $record = new stdClass(); | |
155 | $record->course = $course2->id; | |
156 | $wiki2 = self::getDataGenerator()->create_module('wiki', $record); | |
157 | ||
158 | // Execute real Moodle enrolment as we'll call unenrol() method on the instance later. | |
159 | $enrol = enrol_get_plugin('manual'); | |
160 | $enrolinstances = enrol_get_instances($course2->id, true); | |
161 | foreach ($enrolinstances as $courseenrolinstance) { | |
162 | if ($courseenrolinstance->enrol == "manual") { | |
163 | $instance2 = $courseenrolinstance; | |
164 | break; | |
165 | } | |
166 | } | |
167 | $enrol->enrol_user($instance2, $this->student->id, $this->studentrole->id); | |
168 | ||
169 | self::setUser($this->student); | |
170 | ||
171 | $returndescription = mod_wiki_external::get_wikis_by_courses_returns(); | |
172 | ||
173 | // Create what we expect to be returned when querying the two courses. | |
174 | // First for the student user. | |
175 | $expectedfields = array('id', 'coursemodule', 'course', 'name', 'intro', 'introformat', 'firstpagetitle', 'wikimode', | |
176 | 'defaultformat', 'forceformat', 'editbegin', 'editend', 'section', 'visible', 'groupmode', | |
177 | 'groupingid'); | |
178 | ||
179 | // Add expected coursemodule and data. | |
180 | $wiki1 = $this->wiki; | |
181 | $wiki1->coursemodule = $wiki1->cmid; | |
182 | $wiki1->introformat = 1; | |
183 | $wiki1->section = 0; | |
184 | $wiki1->visible = true; | |
185 | $wiki1->groupmode = 0; | |
186 | $wiki1->groupingid = 0; | |
187 | ||
188 | $wiki2->coursemodule = $wiki2->cmid; | |
189 | $wiki2->introformat = 1; | |
190 | $wiki2->section = 0; | |
191 | $wiki2->visible = true; | |
192 | $wiki2->groupmode = 0; | |
193 | $wiki2->groupingid = 0; | |
194 | ||
195 | foreach ($expectedfields as $field) { | |
196 | $expected1[$field] = $wiki1->{$field}; | |
197 | $expected2[$field] = $wiki2->{$field}; | |
198 | } | |
199 | // Users can create pages by default. | |
200 | $expected1['cancreatepages'] = true; | |
201 | $expected2['cancreatepages'] = true; | |
202 | ||
203 | $expectedwikis = array($expected2, $expected1); | |
204 | ||
205 | // Call the external function passing course ids. | |
206 | $result = mod_wiki_external::get_wikis_by_courses(array($course2->id, $this->course->id)); | |
207 | $result = external_api::clean_returnvalue($returndescription, $result); | |
208 | ||
209 | $this->assertEquals($expectedwikis, $result['wikis']); | |
210 | $this->assertCount(0, $result['warnings']); | |
211 | ||
212 | // Call the external function without passing course id. | |
213 | $result = mod_wiki_external::get_wikis_by_courses(); | |
214 | $result = external_api::clean_returnvalue($returndescription, $result); | |
215 | $this->assertEquals($expectedwikis, $result['wikis']); | |
216 | $this->assertCount(0, $result['warnings']); | |
217 | ||
218 | // Unenrol user from second course and alter expected wikis. | |
219 | $enrol->unenrol_user($instance2, $this->student->id); | |
220 | array_shift($expectedwikis); | |
221 | ||
222 | // Call the external function without passing course id. | |
223 | $result = mod_wiki_external::get_wikis_by_courses(); | |
224 | $result = external_api::clean_returnvalue($returndescription, $result); | |
225 | $this->assertEquals($expectedwikis, $result['wikis']); | |
226 | ||
227 | // Call for the second course we unenrolled the user from, expected warning. | |
228 | $result = mod_wiki_external::get_wikis_by_courses(array($course2->id)); | |
229 | $this->assertCount(1, $result['warnings']); | |
230 | $this->assertEquals('1', $result['warnings'][0]['warningcode']); | |
231 | $this->assertEquals($course2->id, $result['warnings'][0]['itemid']); | |
232 | ||
233 | // Now, try as a teacher for getting all the additional fields. | |
234 | self::setUser($this->teacher); | |
235 | ||
236 | $additionalfields = array('timecreated', 'timemodified'); | |
237 | ||
238 | foreach ($additionalfields as $field) { | |
239 | $expectedwikis[0][$field] = $wiki1->{$field}; | |
240 | } | |
241 | ||
242 | $result = mod_wiki_external::get_wikis_by_courses(); | |
243 | $result = external_api::clean_returnvalue($returndescription, $result); | |
244 | $this->assertEquals($expectedwikis, $result['wikis']); | |
245 | ||
246 | // Admin also should get all the information. | |
247 | self::setAdminUser(); | |
248 | ||
249 | $result = mod_wiki_external::get_wikis_by_courses(array($this->course->id)); | |
250 | $result = external_api::clean_returnvalue($returndescription, $result); | |
251 | $this->assertEquals($expectedwikis, $result['wikis']); | |
252 | ||
253 | // Now, prohibit capabilities. | |
254 | $this->setUser($this->student); | |
255 | $contextcourse1 = context_course::instance($this->course->id); | |
256 | // Prohibit capability = mod:wiki:viewpage on Course1 for students. | |
257 | assign_capability('mod/wiki:viewpage', CAP_PROHIBIT, $this->studentrole->id, $contextcourse1->id); | |
258 | accesslib_clear_all_caches_for_unit_testing(); | |
259 | ||
260 | $wikis = mod_wiki_external::get_wikis_by_courses(array($this->course->id)); | |
261 | $wikis = external_api::clean_returnvalue(mod_wiki_external::get_wikis_by_courses_returns(), $wikis); | |
262 | $this->assertFalse(isset($wikis['wikis'][0]['intro'])); | |
263 | ||
264 | // Prohibit capability = mod:wiki:createpage on Course1 for students. | |
265 | assign_capability('mod/wiki:createpage', CAP_PROHIBIT, $this->studentrole->id, $contextcourse1->id); | |
266 | accesslib_clear_all_caches_for_unit_testing(); | |
267 | ||
268 | $wikis = mod_wiki_external::get_wikis_by_courses(array($this->course->id)); | |
269 | $wikis = external_api::clean_returnvalue(mod_wiki_external::get_wikis_by_courses_returns(), $wikis); | |
270 | $this->assertFalse($wikis['wikis'][0]['cancreatepages']); | |
c1baf89e DP |
271 | |
272 | } | |
273 | ||
274 | /** | |
275 | * Test view_wiki. | |
276 | */ | |
277 | public function test_view_wiki() { | |
278 | ||
279 | // Test invalid instance id. | |
280 | try { | |
281 | mod_wiki_external::view_wiki(0); | |
282 | $this->fail('Exception expected due to invalid mod_wiki instance id.'); | |
283 | } catch (moodle_exception $e) { | |
284 | $this->assertEquals('incorrectwikiid', $e->errorcode); | |
285 | } | |
286 | ||
287 | // Test not-enrolled user. | |
288 | $usernotenrolled = self::getDataGenerator()->create_user(); | |
289 | $this->setUser($usernotenrolled); | |
290 | try { | |
291 | mod_wiki_external::view_wiki($this->wiki->id); | |
292 | $this->fail('Exception expected due to not enrolled user.'); | |
293 | } catch (moodle_exception $e) { | |
294 | $this->assertEquals('requireloginerror', $e->errorcode); | |
295 | } | |
296 | ||
297 | // Test user with full capabilities. | |
298 | $this->setUser($this->student); | |
299 | ||
300 | // Trigger and capture the event. | |
301 | $sink = $this->redirectEvents(); | |
302 | ||
303 | $result = mod_wiki_external::view_wiki($this->wiki->id); | |
304 | $result = external_api::clean_returnvalue(mod_wiki_external::view_wiki_returns(), $result); | |
305 | ||
306 | $events = $sink->get_events(); | |
307 | $this->assertCount(1, $events); | |
308 | $event = array_shift($events); | |
309 | ||
310 | // Checking that the event contains the expected values. | |
311 | $this->assertInstanceOf('\mod_wiki\event\course_module_viewed', $event); | |
312 | $this->assertEquals($this->context, $event->get_context()); | |
313 | $moodlewiki = new \moodle_url('/mod/wiki/view.php', array('id' => $this->cm->id)); | |
314 | $this->assertEquals($moodlewiki, $event->get_url()); | |
315 | $this->assertEventContextNotUsed($event); | |
316 | $this->assertNotEmpty($event->get_name()); | |
317 | ||
318 | // Test user with no capabilities. | |
319 | // We need a explicit prohibit since this capability is allowed for students by default. | |
320 | assign_capability('mod/wiki:viewpage', CAP_PROHIBIT, $this->studentrole->id, $this->context->id); | |
321 | accesslib_clear_all_caches_for_unit_testing(); | |
322 | ||
323 | try { | |
324 | mod_wiki_external::view_wiki($this->wiki->id); | |
325 | $this->fail('Exception expected due to missing capability.'); | |
326 | } catch (moodle_exception $e) { | |
327 | $this->assertEquals('cannotviewpage', $e->errorcode); | |
328 | } | |
329 | ||
330 | } | |
331 | ||
332 | /** | |
333 | * Test view_page. | |
334 | */ | |
335 | public function test_view_page() { | |
336 | ||
337 | // Test invalid page id. | |
338 | try { | |
339 | mod_wiki_external::view_page(0); | |
340 | $this->fail('Exception expected due to invalid view_page page id.'); | |
341 | } catch (moodle_exception $e) { | |
342 | $this->assertEquals('incorrectpageid', $e->errorcode); | |
343 | } | |
344 | ||
345 | // Test not-enrolled user. | |
346 | $usernotenrolled = self::getDataGenerator()->create_user(); | |
347 | $this->setUser($usernotenrolled); | |
348 | try { | |
349 | mod_wiki_external::view_page($this->firstpage->id); | |
350 | $this->fail('Exception expected due to not enrolled user.'); | |
351 | } catch (moodle_exception $e) { | |
352 | $this->assertEquals('requireloginerror', $e->errorcode); | |
353 | } | |
354 | ||
355 | // Test user with full capabilities. | |
356 | $this->setUser($this->student); | |
357 | ||
358 | // Trigger and capture the event. | |
359 | $sink = $this->redirectEvents(); | |
360 | ||
361 | $result = mod_wiki_external::view_page($this->firstpage->id); | |
362 | $result = external_api::clean_returnvalue(mod_wiki_external::view_page_returns(), $result); | |
363 | ||
364 | $events = $sink->get_events(); | |
365 | $this->assertCount(1, $events); | |
366 | $event = array_shift($events); | |
367 | ||
368 | // Checking that the event contains the expected values. | |
369 | $this->assertInstanceOf('\mod_wiki\event\page_viewed', $event); | |
370 | $this->assertEquals($this->context, $event->get_context()); | |
371 | $pageurl = new \moodle_url('/mod/wiki/view.php', array('pageid' => $this->firstpage->id)); | |
372 | $this->assertEquals($pageurl, $event->get_url()); | |
373 | $this->assertEventContextNotUsed($event); | |
374 | $this->assertNotEmpty($event->get_name()); | |
375 | ||
376 | // Test user with no capabilities. | |
377 | // We need a explicit prohibit since this capability is allowed for students by default. | |
378 | assign_capability('mod/wiki:viewpage', CAP_PROHIBIT, $this->studentrole->id, $this->context->id); | |
379 | accesslib_clear_all_caches_for_unit_testing(); | |
380 | ||
381 | try { | |
382 | mod_wiki_external::view_page($this->firstpage->id); | |
383 | $this->fail('Exception expected due to missing capability.'); | |
384 | } catch (moodle_exception $e) { | |
385 | $this->assertEquals('cannotviewpage', $e->errorcode); | |
386 | } | |
387 | ||
4a5acec2 DP |
388 | } |
389 | ||
5380c664 DP |
390 | /** |
391 | * Test get_subwikis. | |
392 | */ | |
393 | public function test_get_subwikis() { | |
394 | ||
395 | // Test invalid wiki id. | |
396 | try { | |
397 | mod_wiki_external::get_subwikis(0); | |
398 | $this->fail('Exception expected due to invalid get_subwikis wiki id.'); | |
399 | } catch (moodle_exception $e) { | |
400 | $this->assertEquals('incorrectwikiid', $e->errorcode); | |
401 | } | |
402 | ||
403 | // Test not-enrolled user. | |
404 | $usernotenrolled = self::getDataGenerator()->create_user(); | |
405 | $this->setUser($usernotenrolled); | |
406 | try { | |
407 | mod_wiki_external::get_subwikis($this->wiki->id); | |
408 | $this->fail('Exception expected due to not enrolled user.'); | |
409 | } catch (moodle_exception $e) { | |
410 | $this->assertEquals('requireloginerror', $e->errorcode); | |
411 | } | |
412 | ||
413 | // Test user with full capabilities. | |
414 | $this->setUser($this->student); | |
415 | ||
416 | // Create what we expect to be returned. We only test a basic case because deep testing is already done | |
417 | // in the tests for wiki_get_visible_subwikis. | |
418 | $expectedsubwikis = array(); | |
419 | $expectedsubwiki = array( | |
420 | 'id' => $this->firstpage->subwikiid, | |
421 | 'wikiid' => $this->wiki->id, | |
422 | 'groupid' => 0, | |
423 | 'userid' => 0, | |
424 | 'canedit' => true | |
425 | ); | |
426 | $expectedsubwikis[] = $expectedsubwiki; | |
427 | ||
428 | $result = mod_wiki_external::get_subwikis($this->wiki->id); | |
429 | $result = external_api::clean_returnvalue(mod_wiki_external::get_subwikis_returns(), $result); | |
430 | $this->assertEquals($expectedsubwikis, $result['subwikis']); | |
431 | $this->assertCount(0, $result['warnings']); | |
432 | ||
433 | // Test user with no capabilities. | |
434 | // We need a explicit prohibit since this capability is allowed for students by default. | |
435 | assign_capability('mod/wiki:viewpage', CAP_PROHIBIT, $this->studentrole->id, $this->context->id); | |
436 | accesslib_clear_all_caches_for_unit_testing(); | |
437 | ||
438 | try { | |
439 | mod_wiki_external::get_subwikis($this->wiki->id); | |
440 | $this->fail('Exception expected due to missing capability.'); | |
441 | } catch (moodle_exception $e) { | |
442 | $this->assertEquals('nopermissions', $e->errorcode); | |
443 | } | |
444 | ||
445 | } | |
446 | ||
ed4fb2bf DP |
447 | /** |
448 | * Test get_subwiki_pages using an invalid wiki instance. | |
449 | */ | |
450 | public function test_get_subwiki_pages_invalid_instance() { | |
451 | $this->setExpectedException('moodle_exception'); | |
452 | mod_wiki_external::get_subwiki_pages(0); | |
453 | } | |
454 | ||
455 | /** | |
456 | * Test get_subwiki_pages using a user not enrolled in the course. | |
457 | */ | |
458 | public function test_get_subwiki_pages_unenrolled_user() { | |
459 | // Create and use the user. | |
460 | $usernotenrolled = self::getDataGenerator()->create_user(); | |
461 | $this->setUser($usernotenrolled); | |
462 | ||
463 | $this->setExpectedException('require_login_exception'); | |
464 | mod_wiki_external::get_subwiki_pages($this->wiki->id); | |
465 | } | |
466 | ||
467 | /** | |
468 | * Test get_subwiki_pages using a hidden wiki as student. | |
469 | */ | |
470 | public function test_get_subwiki_pages_hidden_wiki_as_student() { | |
471 | // Create a hidden wiki and try to get the list of pages. | |
472 | $hiddenwiki = $this->getDataGenerator()->create_module('wiki', | |
473 | array('course' => $this->course->id, 'visible' => false)); | |
474 | ||
475 | $this->setUser($this->student); | |
476 | $this->setExpectedException('require_login_exception'); | |
477 | mod_wiki_external::get_subwiki_pages($hiddenwiki->id); | |
478 | } | |
479 | ||
480 | /** | |
481 | * Test get_subwiki_pages without the viewpage capability. | |
482 | */ | |
483 | public function test_get_subwiki_pages_without_viewpage_capability() { | |
484 | // Prohibit capability = mod/wiki:viewpage on the course for students. | |
485 | $contextcourse = context_course::instance($this->course->id); | |
486 | assign_capability('mod/wiki:viewpage', CAP_PROHIBIT, $this->studentrole->id, $contextcourse->id); | |
487 | accesslib_clear_all_caches_for_unit_testing(); | |
488 | ||
489 | $this->setUser($this->student); | |
490 | $this->setExpectedException('moodle_exception'); | |
491 | mod_wiki_external::get_subwiki_pages($this->wiki->id); | |
492 | } | |
493 | ||
494 | /** | |
495 | * Test get_subwiki_pages using an invalid userid. | |
496 | */ | |
497 | public function test_get_subwiki_pages_invalid_userid() { | |
498 | // Create an individual wiki. | |
499 | $indwiki = $this->getDataGenerator()->create_module('wiki', | |
500 | array('course' => $this->course->id, 'wikimode' => 'individual')); | |
501 | ||
502 | $this->setExpectedException('moodle_exception'); | |
503 | mod_wiki_external::get_subwiki_pages($indwiki->id, 0, -10); | |
504 | } | |
505 | ||
506 | /** | |
507 | * Test get_subwiki_pages using an invalid groupid. | |
508 | */ | |
509 | public function test_get_subwiki_pages_invalid_groupid() { | |
510 | // Create testing data. | |
511 | $this->create_collaborative_wikis_with_groups(); | |
512 | ||
513 | $this->setExpectedException('moodle_exception'); | |
514 | mod_wiki_external::get_subwiki_pages($this->wikisep->id, -111); | |
515 | } | |
516 | ||
517 | /** | |
518 | * Test get_subwiki_pages, check that a student can't see another user pages in an individual wiki without groups. | |
519 | */ | |
520 | public function test_get_subwiki_pages_individual_student_see_other_user() { | |
521 | // Create an individual wiki. | |
522 | $indwiki = $this->getDataGenerator()->create_module('wiki', | |
523 | array('course' => $this->course->id, 'wikimode' => 'individual')); | |
524 | ||
525 | $this->setUser($this->student); | |
526 | $this->setExpectedException('moodle_exception'); | |
527 | mod_wiki_external::get_subwiki_pages($indwiki->id, 0, $this->teacher->id); | |
528 | } | |
529 | ||
530 | /** | |
531 | * Test get_subwiki_pages, check that a student can't get the pages from another group in | |
532 | * a collaborative wiki using separate groups. | |
533 | */ | |
534 | public function test_get_subwiki_pages_collaborative_separate_groups_student_see_other_group() { | |
535 | // Create testing data. | |
536 | $this->create_collaborative_wikis_with_groups(); | |
537 | ||
538 | $this->setUser($this->student); | |
539 | $this->setExpectedException('moodle_exception'); | |
540 | mod_wiki_external::get_subwiki_pages($this->wikisep->id, $this->group2->id); | |
541 | } | |
542 | ||
543 | /** | |
544 | * Test get_subwiki_pages, check that a student can't get the pages from another group in | |
545 | * an individual wiki using separate groups. | |
546 | */ | |
547 | public function test_get_subwiki_pages_individual_separate_groups_student_see_other_group() { | |
548 | // Create testing data. | |
549 | $this->create_individual_wikis_with_groups(); | |
550 | ||
551 | $this->setUser($this->student); | |
552 | $this->setExpectedException('moodle_exception'); | |
553 | mod_wiki_external::get_subwiki_pages($this->wikisepind->id, $this->group2->id, $this->teacher->id); | |
554 | } | |
555 | ||
556 | /** | |
557 | * Test get_subwiki_pages, check that a student can't get the pages from all participants in | |
558 | * a collaborative wiki using separate groups. | |
559 | */ | |
560 | public function test_get_subwiki_pages_collaborative_separate_groups_student_see_all_participants() { | |
561 | // Create testing data. | |
562 | $this->create_collaborative_wikis_with_groups(); | |
563 | ||
564 | $this->setUser($this->student); | |
565 | $this->setExpectedException('moodle_exception'); | |
566 | mod_wiki_external::get_subwiki_pages($this->wikisep->id, 0); | |
567 | } | |
568 | ||
569 | /** | |
570 | * Test get_subwiki_pages, check that a student can't get the pages from all participants in | |
571 | * an individual wiki using separate groups. | |
572 | */ | |
573 | public function test_get_subwiki_pages_individual_separate_groups_student_see_all_participants() { | |
574 | // Create testing data. | |
575 | $this->create_individual_wikis_with_groups(); | |
576 | ||
577 | $this->setUser($this->student); | |
578 | $this->setExpectedException('moodle_exception'); | |
579 | mod_wiki_external::get_subwiki_pages($this->wikisepind->id, 0, $this->teacher->id); | |
580 | } | |
581 | ||
582 | /** | |
583 | * Test get_subwiki_pages without groups and collaborative wiki. | |
584 | */ | |
585 | public function test_get_subwiki_pages_collaborative() { | |
586 | ||
587 | // Test user with full capabilities. | |
588 | $this->setUser($this->student); | |
589 | ||
590 | // Set expected result: first page. | |
591 | $expectedpages = array(); | |
592 | $expectedfirstpage = (array) $this->firstpage; | |
593 | $expectedfirstpage['caneditpage'] = true; // No groups and students have 'mod/wiki:editpage' capability. | |
594 | $expectedfirstpage['firstpage'] = true; | |
595 | $expectedfirstpage['contentformat'] = 1; | |
596 | $expectedpages[] = $expectedfirstpage; | |
597 | ||
598 | $result = mod_wiki_external::get_subwiki_pages($this->wiki->id); | |
599 | $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result); | |
600 | $this->assertEquals($expectedpages, $result['pages']); | |
601 | ||
602 | // Check that groupid param is ignored since the wiki isn't using groups. | |
603 | $result = mod_wiki_external::get_subwiki_pages($this->wiki->id, 1234); | |
604 | $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result); | |
605 | $this->assertEquals($expectedpages, $result['pages']); | |
606 | ||
607 | // Check that userid param is ignored since the wiki is collaborative. | |
608 | $result = mod_wiki_external::get_subwiki_pages($this->wiki->id, 1234, 1234); | |
609 | $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result); | |
610 | $this->assertEquals($expectedpages, $result['pages']); | |
611 | ||
612 | // Add a new page to the wiki and test again. We'll use a custom title so it's returned first if sorted by title. | |
613 | $newpage = $this->getDataGenerator()->get_plugin_generator('mod_wiki')->create_page( | |
614 | $this->wiki, array('title' => 'AAA')); | |
615 | ||
616 | $expectednewpage = (array) $newpage; | |
617 | $expectednewpage['caneditpage'] = true; // No groups and students have 'mod/wiki:editpage' capability. | |
618 | $expectednewpage['firstpage'] = false; | |
619 | $expectednewpage['contentformat'] = 1; | |
620 | array_unshift($expectedpages, $expectednewpage); // Add page to the beginning since it orders by title by default. | |
621 | ||
622 | $result = mod_wiki_external::get_subwiki_pages($this->wiki->id); | |
623 | $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result); | |
624 | $this->assertEquals($expectedpages, $result['pages']); | |
625 | ||
626 | // Now we'll order by ID. Since first page was created first it'll have a lower ID. | |
627 | $expectedpages = array($expectedfirstpage, $expectednewpage); | |
628 | $result = mod_wiki_external::get_subwiki_pages($this->wiki->id, 0, 0, array('sortby' => 'id')); | |
629 | $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result); | |
630 | $this->assertEquals($expectedpages, $result['pages']); | |
631 | ||
31bfb008 DP |
632 | // Check that WS doesn't return page content if includecontent is false, it returns the size instead. |
633 | foreach ($expectedpages as $i => $expectedpage) { | |
634 | if (function_exists('mb_strlen') && ((int)ini_get('mbstring.func_overload') & 2)) { | |
635 | $expectedpages[$i]['contentsize'] = mb_strlen($expectedpages[$i]['cachedcontent'], '8bit'); | |
636 | } else { | |
637 | $expectedpages[$i]['contentsize'] = strlen($expectedpages[$i]['cachedcontent']); | |
638 | } | |
639 | unset($expectedpages[$i]['cachedcontent']); | |
640 | unset($expectedpages[$i]['contentformat']); | |
641 | } | |
ed4fb2bf DP |
642 | $result = mod_wiki_external::get_subwiki_pages($this->wiki->id, 0, 0, array('sortby' => 'id', 'includecontent' => 0)); |
643 | $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result); | |
644 | $this->assertEquals($expectedpages, $result['pages']); | |
645 | } | |
646 | ||
647 | /** | |
648 | * Test get_subwiki_pages without groups. | |
649 | */ | |
650 | public function test_get_subwiki_pages_individual() { | |
651 | ||
652 | // Create an individual wiki to test userid param. | |
653 | $indwiki = $this->getDataGenerator()->create_module('wiki', | |
654 | array('course' => $this->course->id, 'wikimode' => 'individual')); | |
655 | ||
656 | // Perform a request before creating any page to check that an empty array is returned if subwiki doesn't exist. | |
657 | $result = mod_wiki_external::get_subwiki_pages($indwiki->id); | |
658 | $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result); | |
659 | $this->assertEquals(array(), $result['pages']); | |
660 | ||
661 | // Create first pages as student and teacher. | |
662 | $this->setUser($this->student); | |
663 | $indfirstpagestudent = $this->getDataGenerator()->get_plugin_generator('mod_wiki')->create_first_page($indwiki); | |
664 | $this->setUser($this->teacher); | |
665 | $indfirstpageteacher = $this->getDataGenerator()->get_plugin_generator('mod_wiki')->create_first_page($indwiki); | |
666 | ||
667 | // Check that teacher can get his pages. | |
668 | $expectedteacherpage = (array) $indfirstpageteacher; | |
669 | $expectedteacherpage['caneditpage'] = true; | |
670 | $expectedteacherpage['firstpage'] = true; | |
671 | $expectedteacherpage['contentformat'] = 1; | |
672 | $expectedpages = array($expectedteacherpage); | |
673 | ||
674 | $result = mod_wiki_external::get_subwiki_pages($indwiki->id, 0, $this->teacher->id); | |
675 | $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result); | |
676 | $this->assertEquals($expectedpages, $result['pages']); | |
677 | ||
678 | // Check that the teacher can see the student's pages. | |
679 | $expectedstudentpage = (array) $indfirstpagestudent; | |
680 | $expectedstudentpage['caneditpage'] = true; | |
681 | $expectedstudentpage['firstpage'] = true; | |
682 | $expectedstudentpage['contentformat'] = 1; | |
683 | $expectedpages = array($expectedstudentpage); | |
684 | ||
685 | $result = mod_wiki_external::get_subwiki_pages($indwiki->id, 0, $this->student->id); | |
686 | $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result); | |
687 | $this->assertEquals($expectedpages, $result['pages']); | |
688 | ||
689 | // Now check that student can get his pages. | |
690 | $this->setUser($this->student); | |
691 | ||
692 | $result = mod_wiki_external::get_subwiki_pages($indwiki->id, 0, $this->student->id); | |
693 | $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result); | |
694 | $this->assertEquals($expectedpages, $result['pages']); | |
695 | ||
696 | // Check that not using userid uses current user. | |
697 | $result = mod_wiki_external::get_subwiki_pages($indwiki->id, 0); | |
698 | $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result); | |
699 | $this->assertEquals($expectedpages, $result['pages']); | |
700 | } | |
701 | ||
702 | /** | |
703 | * Test get_subwiki_pages with groups and collaborative wikis. | |
704 | */ | |
705 | public function test_get_subwiki_pages_separate_groups_collaborative() { | |
706 | ||
707 | // Create testing data. | |
708 | $this->create_collaborative_wikis_with_groups(); | |
709 | ||
710 | $this->setUser($this->student); | |
711 | ||
712 | // Try to get pages from a valid group in separate groups wiki. | |
713 | ||
714 | $expectedpage = (array) $this->fpsepg1; | |
715 | $expectedpage['caneditpage'] = true; // User belongs to group and has 'mod/wiki:editpage' capability. | |
716 | $expectedpage['firstpage'] = true; | |
717 | $expectedpage['contentformat'] = 1; | |
718 | $expectedpages = array($expectedpage); | |
719 | ||
720 | $result = mod_wiki_external::get_subwiki_pages($this->wikisep->id, $this->group1->id); | |
721 | $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result); | |
722 | $this->assertEquals($expectedpages, $result['pages']); | |
723 | ||
724 | // Let's check that not using groupid returns the same result (current group). | |
725 | $result = mod_wiki_external::get_subwiki_pages($this->wikisep->id); | |
726 | $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result); | |
727 | $this->assertEquals($expectedpages, $result['pages']); | |
728 | ||
729 | // Check that teacher can view a group pages without belonging to it. | |
730 | $this->setUser($this->teacher); | |
731 | $result = mod_wiki_external::get_subwiki_pages($this->wikisep->id, $this->group1->id); | |
732 | $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result); | |
733 | $this->assertEquals($expectedpages, $result['pages']); | |
734 | ||
735 | // Check that teacher can get the pages from all participants. | |
736 | $expectedpage = (array) $this->fpsepall; | |
737 | $expectedpage['caneditpage'] = true; | |
738 | $expectedpage['firstpage'] = true; | |
739 | $expectedpage['contentformat'] = 1; | |
740 | $expectedpages = array($expectedpage); | |
741 | ||
742 | $result = mod_wiki_external::get_subwiki_pages($this->wikisep->id, 0); | |
743 | $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result); | |
744 | $this->assertEquals($expectedpages, $result['pages']); | |
745 | } | |
746 | ||
747 | /** | |
748 | * Test get_subwiki_pages with groups and collaborative wikis. | |
749 | */ | |
750 | public function test_get_subwiki_pages_visible_groups_collaborative() { | |
751 | ||
752 | // Create testing data. | |
753 | $this->create_collaborative_wikis_with_groups(); | |
754 | ||
755 | $this->setUser($this->student); | |
756 | ||
757 | // Try to get pages from a valid group in visible groups wiki. | |
758 | ||
759 | $expectedpage = (array) $this->fpvisg1; | |
760 | $expectedpage['caneditpage'] = true; // User belongs to group and has 'mod/wiki:editpage' capability. | |
761 | $expectedpage['firstpage'] = true; | |
762 | $expectedpage['contentformat'] = 1; | |
763 | $expectedpages = array($expectedpage); | |
764 | ||
765 | $result = mod_wiki_external::get_subwiki_pages($this->wikivis->id, $this->group1->id); | |
766 | $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result); | |
767 | $this->assertEquals($expectedpages, $result['pages']); | |
768 | ||
769 | // Check that with visible groups a student can get the pages of groups he doesn't belong to. | |
770 | $expectedpage = (array) $this->fpvisg2; | |
771 | $expectedpage['caneditpage'] = false; // User doesn't belong to group so he can't edit the page. | |
772 | $expectedpage['firstpage'] = true; | |
773 | $expectedpage['contentformat'] = 1; | |
774 | $expectedpages = array($expectedpage); | |
775 | ||
776 | $result = mod_wiki_external::get_subwiki_pages($this->wikivis->id, $this->group2->id); | |
777 | $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result); | |
778 | $this->assertEquals($expectedpages, $result['pages']); | |
779 | ||
780 | // Check that with visible groups a student can get the pages of all participants. | |
781 | $expectedpage = (array) $this->fpvisall; | |
782 | $expectedpage['caneditpage'] = false; | |
783 | $expectedpage['firstpage'] = true; | |
784 | $expectedpage['contentformat'] = 1; | |
785 | $expectedpages = array($expectedpage); | |
786 | ||
787 | $result = mod_wiki_external::get_subwiki_pages($this->wikivis->id, 0); | |
788 | $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result); | |
789 | $this->assertEquals($expectedpages, $result['pages']); | |
790 | } | |
791 | ||
792 | /** | |
793 | * Test get_subwiki_pages with groups and individual wikis. | |
794 | */ | |
795 | public function test_get_subwiki_pages_separate_groups_individual() { | |
796 | ||
797 | // Create testing data. | |
798 | $this->create_individual_wikis_with_groups(); | |
799 | ||
800 | $this->setUser($this->student); | |
801 | ||
802 | // Check that student can retrieve his pages from separate wiki. | |
803 | $expectedpage = (array) $this->fpsepg1indstu; | |
804 | $expectedpage['caneditpage'] = true; | |
805 | $expectedpage['firstpage'] = true; | |
806 | $expectedpage['contentformat'] = 1; | |
807 | $expectedpages = array($expectedpage); | |
808 | ||
809 | $result = mod_wiki_external::get_subwiki_pages($this->wikisepind->id, $this->group1->id, $this->student->id); | |
810 | $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result); | |
811 | $this->assertEquals($expectedpages, $result['pages']); | |
812 | ||
813 | // Check that not using userid uses current user. | |
814 | $result = mod_wiki_external::get_subwiki_pages($this->wikisepind->id, $this->group1->id); | |
815 | $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result); | |
816 | $this->assertEquals($expectedpages, $result['pages']); | |
817 | ||
818 | // Check that the teacher can see the student pages. | |
819 | $this->setUser($this->teacher); | |
820 | $result = mod_wiki_external::get_subwiki_pages($this->wikisepind->id, $this->group1->id, $this->student->id); | |
821 | $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result); | |
822 | $this->assertEquals($expectedpages, $result['pages']); | |
823 | ||
824 | // Check that a student can see pages from another user that belongs to his groups. | |
825 | $this->setUser($this->student); | |
826 | $expectedpage = (array) $this->fpsepg1indstu2; | |
827 | $expectedpage['caneditpage'] = false; | |
828 | $expectedpage['firstpage'] = true; | |
829 | $expectedpage['contentformat'] = 1; | |
830 | $expectedpages = array($expectedpage); | |
831 | ||
832 | $result = mod_wiki_external::get_subwiki_pages($this->wikisepind->id, $this->group1->id, $this->student2->id); | |
833 | $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result); | |
834 | $this->assertEquals($expectedpages, $result['pages']); | |
835 | } | |
836 | ||
837 | /** | |
838 | * Test get_subwiki_pages with groups and individual wikis. | |
839 | */ | |
840 | public function test_get_subwiki_pages_visible_groups_individual() { | |
841 | ||
842 | // Create testing data. | |
843 | $this->create_individual_wikis_with_groups(); | |
844 | ||
845 | $this->setUser($this->student); | |
846 | ||
847 | // Check that student can retrieve his pages from visible wiki. | |
848 | $expectedpage = (array) $this->fpvisg1indstu; | |
849 | $expectedpage['caneditpage'] = true; | |
850 | $expectedpage['firstpage'] = true; | |
851 | $expectedpage['contentformat'] = 1; | |
852 | $expectedpages = array($expectedpage); | |
853 | ||
854 | $result = mod_wiki_external::get_subwiki_pages($this->wikivisind->id, $this->group1->id, $this->student->id); | |
855 | $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result); | |
856 | $this->assertEquals($expectedpages, $result['pages']); | |
857 | ||
858 | // Check that student can see teacher pages in visible groups, even if the user doesn't belong to the group. | |
859 | $expectedpage = (array) $this->fpvisg2indt; | |
860 | $expectedpage['caneditpage'] = false; | |
861 | $expectedpage['firstpage'] = true; | |
862 | $expectedpage['contentformat'] = 1; | |
863 | $expectedpages = array($expectedpage); | |
864 | ||
865 | $result = mod_wiki_external::get_subwiki_pages($this->wikivisind->id, $this->group2->id, $this->teacher->id); | |
866 | $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result); | |
867 | $this->assertEquals($expectedpages, $result['pages']); | |
868 | ||
869 | // Check that with visible groups a student can get the pages of all participants. | |
870 | $expectedpage = (array) $this->fpvisallindt; | |
871 | $expectedpage['caneditpage'] = false; | |
872 | $expectedpage['firstpage'] = true; | |
873 | $expectedpage['contentformat'] = 1; | |
874 | $expectedpages = array($expectedpage); | |
875 | ||
876 | $result = mod_wiki_external::get_subwiki_pages($this->wikivisind->id, 0, $this->teacher->id); | |
877 | $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result); | |
878 | $this->assertEquals($expectedpages, $result['pages']); | |
879 | } | |
880 | ||
9a4c5222 DP |
881 | /** |
882 | * Test get_page_contents using an invalid pageid. | |
883 | */ | |
884 | public function test_get_page_contents_invalid_pageid() { | |
885 | $this->setExpectedException('moodle_exception'); | |
886 | mod_wiki_external::get_page_contents(0); | |
887 | } | |
888 | ||
889 | /** | |
890 | * Test get_page_contents using a user not enrolled in the course. | |
891 | */ | |
892 | public function test_get_page_contents_unenrolled_user() { | |
893 | // Create and use the user. | |
894 | $usernotenrolled = self::getDataGenerator()->create_user(); | |
895 | $this->setUser($usernotenrolled); | |
896 | ||
897 | $this->setExpectedException('require_login_exception'); | |
898 | mod_wiki_external::get_page_contents($this->firstpage->id); | |
899 | } | |
900 | ||
901 | /** | |
902 | * Test get_page_contents using a hidden wiki as student. | |
903 | */ | |
904 | public function test_get_page_contents_hidden_wiki_as_student() { | |
905 | // Create a hidden wiki and try to get a page contents. | |
906 | $hiddenwiki = $this->getDataGenerator()->create_module('wiki', | |
907 | array('course' => $this->course->id, 'visible' => false)); | |
908 | $hiddenpage = $this->getDataGenerator()->get_plugin_generator('mod_wiki')->create_page($hiddenwiki); | |
909 | ||
910 | $this->setUser($this->student); | |
911 | $this->setExpectedException('require_login_exception'); | |
912 | mod_wiki_external::get_page_contents($hiddenpage->id); | |
913 | } | |
914 | ||
915 | /** | |
916 | * Test get_page_contents without the viewpage capability. | |
917 | */ | |
918 | public function test_get_page_contents_without_viewpage_capability() { | |
919 | // Prohibit capability = mod/wiki:viewpage on the course for students. | |
920 | $contextcourse = context_course::instance($this->course->id); | |
921 | assign_capability('mod/wiki:viewpage', CAP_PROHIBIT, $this->studentrole->id, $contextcourse->id); | |
922 | accesslib_clear_all_caches_for_unit_testing(); | |
923 | ||
924 | $this->setUser($this->student); | |
925 | $this->setExpectedException('moodle_exception'); | |
926 | mod_wiki_external::get_page_contents($this->firstpage->id); | |
927 | } | |
928 | ||
929 | /** | |
930 | * Test get_page_contents, check that a student can't get a page from another group when | |
931 | * using separate groups. | |
932 | */ | |
933 | public function test_get_page_contents_separate_groups_student_see_other_group() { | |
934 | // Create testing data. | |
935 | $this->create_individual_wikis_with_groups(); | |
936 | ||
937 | $this->setUser($this->student); | |
938 | $this->setExpectedException('moodle_exception'); | |
939 | mod_wiki_external::get_page_contents($this->fpsepg2indt->id); | |
940 | } | |
941 | ||
942 | /** | |
943 | * Test get_page_contents without groups. We won't test all the possible cases because that's already | |
944 | * done in the tests for get_subwiki_pages. | |
945 | */ | |
946 | public function test_get_page_contents() { | |
947 | ||
948 | // Test user with full capabilities. | |
949 | $this->setUser($this->student); | |
950 | ||
951 | // Set expected result: first page. | |
952 | $expectedpage = array( | |
953 | 'id' => $this->firstpage->id, | |
954 | 'wikiid' => $this->wiki->id, | |
955 | 'subwikiid' => $this->firstpage->subwikiid, | |
956 | 'groupid' => 0, // No groups. | |
957 | 'userid' => 0, // Collaborative. | |
958 | 'title' => $this->firstpage->title, | |
959 | 'cachedcontent' => $this->firstpage->cachedcontent, | |
960 | 'contentformat' => 1, | |
961 | 'caneditpage' => true | |
962 | ); | |
963 | ||
964 | $result = mod_wiki_external::get_page_contents($this->firstpage->id); | |
965 | $result = external_api::clean_returnvalue(mod_wiki_external::get_page_contents_returns(), $result); | |
966 | $this->assertEquals($expectedpage, $result['page']); | |
967 | ||
968 | // Add a new page to the wiki and test with it. | |
969 | $newpage = $this->getDataGenerator()->get_plugin_generator('mod_wiki')->create_page($this->wiki); | |
970 | ||
971 | $expectedpage['id'] = $newpage->id; | |
972 | $expectedpage['title'] = $newpage->title; | |
973 | $expectedpage['cachedcontent'] = $newpage->cachedcontent; | |
974 | ||
975 | $result = mod_wiki_external::get_page_contents($newpage->id); | |
976 | $result = external_api::clean_returnvalue(mod_wiki_external::get_page_contents_returns(), $result); | |
977 | $this->assertEquals($expectedpage, $result['page']); | |
978 | } | |
979 | ||
980 | /** | |
981 | * Test get_page_contents with groups. We won't test all the possible cases because that's already | |
982 | * done in the tests for get_subwiki_pages. | |
983 | */ | |
984 | public function test_get_page_contents_with_groups() { | |
985 | ||
986 | // Create testing data. | |
987 | $this->create_individual_wikis_with_groups(); | |
988 | ||
989 | // Try to get page from a valid group in separate groups wiki. | |
990 | $this->setUser($this->student); | |
991 | ||
992 | $expectedfpsepg1indstu = array( | |
993 | 'id' => $this->fpsepg1indstu->id, | |
994 | 'wikiid' => $this->wikisepind->id, | |
995 | 'subwikiid' => $this->fpsepg1indstu->subwikiid, | |
996 | 'groupid' => $this->group1->id, | |
997 | 'userid' => $this->student->id, | |
998 | 'title' => $this->fpsepg1indstu->title, | |
999 | 'cachedcontent' => $this->fpsepg1indstu->cachedcontent, | |
1000 | 'contentformat' => 1, | |
1001 | 'caneditpage' => true | |
1002 | ); | |
1003 | ||
1004 | $result = mod_wiki_external::get_page_contents($this->fpsepg1indstu->id); | |
1005 | $result = external_api::clean_returnvalue(mod_wiki_external::get_page_contents_returns(), $result); | |
1006 | $this->assertEquals($expectedfpsepg1indstu, $result['page']); | |
1007 | ||
1008 | // Check that teacher can view a group pages without belonging to it. | |
1009 | $this->setUser($this->teacher); | |
1010 | $result = mod_wiki_external::get_page_contents($this->fpsepg1indstu->id); | |
1011 | $result = external_api::clean_returnvalue(mod_wiki_external::get_page_contents_returns(), $result); | |
1012 | $this->assertEquals($expectedfpsepg1indstu, $result['page']); | |
1013 | } | |
1014 | ||
7ef81de8 DP |
1015 | /** |
1016 | * Test get_subwiki_files using a wiki without files. | |
1017 | */ | |
1018 | public function test_get_subwiki_files_no_files() { | |
1019 | $result = mod_wiki_external::get_subwiki_files($this->wiki->id); | |
1020 | $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_files_returns(), $result); | |
1021 | $this->assertCount(0, $result['files']); | |
1022 | $this->assertCount(0, $result['warnings']); | |
1023 | } | |
1024 | ||
1025 | /** | |
1026 | * Test get_subwiki_files, check that a student can't get files from another group's subwiki when | |
1027 | * using separate groups. | |
1028 | */ | |
1029 | public function test_get_subwiki_files_separate_groups_student_see_other_group() { | |
1030 | // Create testing data. | |
1031 | $this->create_collaborative_wikis_with_groups(); | |
1032 | ||
1033 | $this->setUser($this->student); | |
1034 | $this->setExpectedException('moodle_exception'); | |
1035 | mod_wiki_external::get_subwiki_files($this->wikisep->id, $this->group2->id); | |
1036 | } | |
1037 | ||
1038 | /** | |
1039 | * Test get_subwiki_files using a collaborative wiki without groups. | |
1040 | */ | |
1041 | public function test_get_subwiki_files_collaborative_no_groups() { | |
1042 | $this->setUser($this->student); | |
1043 | ||
1044 | // Add a file as subwiki attachment. | |
1045 | $fs = get_file_storage(); | |
1046 | $file = array('component' => 'mod_wiki', 'filearea' => 'attachments', | |
1047 | 'contextid' => $this->context->id, 'itemid' => $this->firstpage->subwikiid, | |
1048 | 'filename' => 'image.jpg', 'filepath' => '/', 'timemodified' => time()); | |
1049 | $content = 'IMAGE'; | |
1050 | $fs->create_file_from_string($file, $content); | |
1051 | ||
1052 | $expectedfile = array( | |
1053 | 'filename' => $file['filename'], | |
1054 | 'filepath' => $file['filepath'], | |
1055 | 'mimetype' => 'image/jpeg', | |
1056 | 'filesize' => strlen($content), | |
1057 | 'timemodified' => $file['timemodified'], | |
1058 | 'fileurl' => moodle_url::make_webservice_pluginfile_url($file['contextid'], $file['component'], | |
1059 | $file['filearea'], $file['itemid'], $file['filepath'], $file['filename']), | |
1060 | ); | |
1061 | ||
1062 | // Call the WS and check that it returns this file. | |
1063 | $result = mod_wiki_external::get_subwiki_files($this->wiki->id); | |
1064 | $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_files_returns(), $result); | |
1065 | $this->assertCount(1, $result['files']); | |
1066 | $this->assertEquals($expectedfile, $result['files'][0]); | |
1067 | ||
1068 | // Now add another file to the same subwiki. | |
1069 | $file['filename'] = 'Another image.jpg'; | |
1070 | $file['timemodified'] = time(); | |
1071 | $content = 'ANOTHER IMAGE'; | |
1072 | $fs->create_file_from_string($file, $content); | |
1073 | ||
1074 | $expectedfile['filename'] = $file['filename']; | |
1075 | $expectedfile['timemodified'] = $file['timemodified']; | |
1076 | $expectedfile['filesize'] = strlen($content); | |
1077 | $expectedfile['fileurl'] = moodle_url::make_webservice_pluginfile_url($file['contextid'], $file['component'], | |
1078 | $file['filearea'], $file['itemid'], $file['filepath'], $file['filename']); | |
1079 | ||
1080 | // Call the WS and check that it returns both files file. | |
1081 | $result = mod_wiki_external::get_subwiki_files($this->wiki->id); | |
1082 | $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_files_returns(), $result); | |
1083 | $this->assertCount(2, $result['files']); | |
1084 | // The new file is returned first because they're returned in alphabetical order. | |
1085 | $this->assertEquals($expectedfile, $result['files'][0]); | |
1086 | } | |
1087 | ||
1088 | /** | |
1089 | * Test get_subwiki_files using an individual wiki with visible groups. | |
1090 | */ | |
1091 | public function test_get_subwiki_files_visible_groups_individual() { | |
1092 | // Create testing data. | |
1093 | $this->create_individual_wikis_with_groups(); | |
1094 | ||
1095 | $this->setUser($this->student); | |
1096 | ||
1097 | // Add a file as subwiki attachment in the student group 1 subwiki. | |
1098 | $fs = get_file_storage(); | |
1099 | $contextwiki = context_module::instance($this->wikivisind->cmid); | |
1100 | $file = array('component' => 'mod_wiki', 'filearea' => 'attachments', | |
1101 | 'contextid' => $contextwiki->id, 'itemid' => $this->fpvisg1indstu->subwikiid, | |
1102 | 'filename' => 'image.jpg', 'filepath' => '/', 'timemodified' => time()); | |
1103 | $content = 'IMAGE'; | |
1104 | $fs->create_file_from_string($file, $content); | |
1105 | ||
1106 | $expectedfile = array( | |
1107 | 'filename' => $file['filename'], | |
1108 | 'filepath' => $file['filepath'], | |
1109 | 'mimetype' => 'image/jpeg', | |
1110 | 'filesize' => strlen($content), | |
1111 | 'timemodified' => $file['timemodified'], | |
1112 | 'fileurl' => moodle_url::make_webservice_pluginfile_url($file['contextid'], $file['component'], | |
1113 | $file['filearea'], $file['itemid'], $file['filepath'], $file['filename']), | |
1114 | ); | |
1115 | ||
1116 | // Call the WS and check that it returns this file. | |
1117 | $result = mod_wiki_external::get_subwiki_files($this->wikivisind->id, $this->group1->id); | |
1118 | $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_files_returns(), $result); | |
1119 | $this->assertCount(1, $result['files']); | |
1120 | $this->assertEquals($expectedfile, $result['files'][0]); | |
1121 | ||
1122 | // Now check that a teacher can see it too. | |
1123 | $this->setUser($this->teacher); | |
1124 | $result = mod_wiki_external::get_subwiki_files($this->wikivisind->id, $this->group1->id, $this->student->id); | |
1125 | $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_files_returns(), $result); | |
1126 | $this->assertCount(1, $result['files']); | |
1127 | $this->assertEquals($expectedfile, $result['files'][0]); | |
1128 | } | |
1129 | ||
20dca621 PFO |
1130 | |
1131 | /** | |
1132 | * Test get_page_for_editing. We won't test all the possible cases because that's already | |
1133 | * done in the tests for wiki_parser_proxy::get_section. | |
1134 | */ | |
1135 | public function test_get_page_for_editing() { | |
1136 | ||
1137 | $this->create_individual_wikis_with_groups(); | |
1138 | ||
1139 | $sectioncontent = '<h1>Title1</h1>Text inside section'; | |
1140 | $pagecontent = $sectioncontent.'<h1>Title2</h1>Text inside section'; | |
1141 | $newpage = $this->getDataGenerator()->get_plugin_generator('mod_wiki')->create_page( | |
1142 | $this->wiki, array('content' => $pagecontent)); | |
1143 | ||
1144 | // Test user with full capabilities. | |
1145 | $this->setUser($this->student); | |
1146 | ||
1147 | // Set expected result: Full Page content. | |
1148 | $expected = array( | |
1149 | 'content' => $pagecontent, | |
1150 | 'contentformat' => 'html', | |
1151 | 'version' => '1' | |
1152 | ); | |
1153 | ||
1154 | $result = mod_wiki_external::get_page_for_editing($newpage->id); | |
1155 | $result = external_api::clean_returnvalue(mod_wiki_external::get_page_for_editing_returns(), $result); | |
1156 | $this->assertEquals($expected, $result['pagesection']); | |
1157 | ||
1158 | // Set expected result: Section Page content. | |
1159 | $expected = array( | |
1160 | 'content' => $sectioncontent, | |
1161 | 'contentformat' => 'html', | |
1162 | 'version' => '1' | |
1163 | ); | |
1164 | ||
1165 | $result = mod_wiki_external::get_page_for_editing($newpage->id, 'Title1'); | |
1166 | $result = external_api::clean_returnvalue(mod_wiki_external::get_page_for_editing_returns(), $result); | |
1167 | $this->assertEquals($expected, $result['pagesection']); | |
1168 | } | |
1169 | ||
a6b6f0d2 PFO |
1170 | /** |
1171 | * Test new_page. We won't test all the possible cases because that's already | |
1172 | * done in the tests for wiki_create_page. | |
1173 | */ | |
1174 | public function test_new_page() { | |
1175 | ||
1176 | $this->create_individual_wikis_with_groups(); | |
1177 | ||
1178 | $sectioncontent = '<h1>Title1</h1>Text inside section'; | |
1179 | $pagecontent = $sectioncontent.'<h1>Title2</h1>Text inside section'; | |
1180 | $pagetitle = 'Page Title'; | |
1181 | ||
1182 | // Test user with full capabilities. | |
1183 | $this->setUser($this->student); | |
1184 | ||
1185 | // Test on existing subwiki. | |
1186 | $result = mod_wiki_external::new_page($pagetitle, $pagecontent, 'html', $this->fpsepg1indstu->subwikiid); | |
1187 | $result = external_api::clean_returnvalue(mod_wiki_external::new_page_returns(), $result); | |
1188 | $this->assertInternalType('int', $result['pageid']); | |
1189 | ||
1190 | $version = wiki_get_current_version($result['pageid']); | |
1191 | $this->assertEquals($pagecontent, $version->content); | |
1192 | $this->assertEquals('html', $version->contentformat); | |
1193 | ||
1194 | $page = wiki_get_page($result['pageid']); | |
1195 | $this->assertEquals($pagetitle, $page->title); | |
1196 | ||
1197 | // Test existing page creation. | |
1198 | try { | |
1199 | mod_wiki_external::new_page($pagetitle, $pagecontent, 'html', $this->fpsepg1indstu->subwikiid); | |
1200 | $this->fail('Exception expected due to creation of an existing page.'); | |
1201 | } catch (moodle_exception $e) { | |
1202 | $this->assertEquals('pageexists', $e->errorcode); | |
1203 | } | |
1204 | ||
1205 | // Test on non existing subwiki. Add student to group2 to have a new subwiki to be created. | |
1206 | $this->getDataGenerator()->create_group_member(array('userid' => $this->student->id, 'groupid' => $this->group2->id)); | |
1207 | $result = mod_wiki_external::new_page($pagetitle, $pagecontent, 'html', null, $this->wikisepind->id, $this->student->id, | |
1208 | $this->group2->id); | |
1209 | $result = external_api::clean_returnvalue(mod_wiki_external::new_page_returns(), $result); | |
1210 | $this->assertInternalType('int', $result['pageid']); | |
1211 | ||
1212 | $version = wiki_get_current_version($result['pageid']); | |
1213 | $this->assertEquals($pagecontent, $version->content); | |
1214 | $this->assertEquals('html', $version->contentformat); | |
1215 | ||
1216 | $page = wiki_get_page($result['pageid']); | |
1217 | $this->assertEquals($pagetitle, $page->title); | |
1218 | ||
1219 | $subwiki = wiki_get_subwiki($page->subwikiid); | |
1220 | $expected = new StdClass(); | |
1221 | $expected->id = $subwiki->id; | |
1222 | $expected->wikiid = $this->wikisepind->id; | |
1223 | $expected->groupid = $this->group2->id; | |
1224 | $expected->userid = $this->student->id; | |
1225 | $this->assertEquals($expected, $subwiki); | |
1226 | ||
1227 | // Check page creation for a user not in course. | |
1228 | $this->studentnotincourse = self::getDataGenerator()->create_user(); | |
1229 | $this->anothercourse = $this->getDataGenerator()->create_course(); | |
1230 | $this->groupnotincourse = $this->getDataGenerator()->create_group(array('courseid' => $this->anothercourse->id)); | |
1231 | ||
1232 | try { | |
1233 | mod_wiki_external::new_page($pagetitle, $pagecontent, 'html', null, $this->wikisepind->id, | |
1234 | $this->studentnotincourse->id, $this->groupnotincourse->id); | |
1235 | $this->fail('Exception expected due to creation of an invalid subwiki creation.'); | |
1236 | } catch (moodle_exception $e) { | |
1237 | $this->assertEquals('cannoteditpage', $e->errorcode); | |
1238 | } | |
1239 | ||
1240 | } | |
1241 | ||
4a5acec2 | 1242 | } |