MDL-53703 wiki: New WS mod_wiki_get_subwiki_files
[moodle.git] / mod / wiki / tests / externallib_test.php
CommitLineData
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
27defined('MOODLE_INTERNAL') || die();
28
29global $CFG;
30
31require_once($CFG->dirroot . '/webservice/tests/helpers.php');
32require_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 */
43class 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
4a5acec2 1015}