MDL-64656 core_tag: Add existing WebServices to the mobile app service
[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.
7ef49bd3
JL
175 $expectedfields = array('id', 'coursemodule', 'course', 'name', 'intro', 'introformat', 'introfiles', 'firstpagetitle',
176 'wikimode', 'defaultformat', 'forceformat', 'editbegin', 'editend', 'section', 'visible',
177 'groupmode', 'groupingid');
4a5acec2
DP
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;
7ef49bd3 187 $wiki1->introfiles = [];
4a5acec2
DP
188
189 $wiki2->coursemodule = $wiki2->cmid;
190 $wiki2->introformat = 1;
191 $wiki2->section = 0;
192 $wiki2->visible = true;
193 $wiki2->groupmode = 0;
194 $wiki2->groupingid = 0;
7ef49bd3 195 $wiki2->introfiles = [];
4a5acec2
DP
196
197 foreach ($expectedfields as $field) {
198 $expected1[$field] = $wiki1->{$field};
199 $expected2[$field] = $wiki2->{$field};
200 }
201 // Users can create pages by default.
202 $expected1['cancreatepages'] = true;
203 $expected2['cancreatepages'] = true;
204
205 $expectedwikis = array($expected2, $expected1);
206
207 // Call the external function passing course ids.
208 $result = mod_wiki_external::get_wikis_by_courses(array($course2->id, $this->course->id));
209 $result = external_api::clean_returnvalue($returndescription, $result);
210
211 $this->assertEquals($expectedwikis, $result['wikis']);
212 $this->assertCount(0, $result['warnings']);
213
214 // Call the external function without passing course id.
215 $result = mod_wiki_external::get_wikis_by_courses();
216 $result = external_api::clean_returnvalue($returndescription, $result);
217 $this->assertEquals($expectedwikis, $result['wikis']);
218 $this->assertCount(0, $result['warnings']);
219
220 // Unenrol user from second course and alter expected wikis.
221 $enrol->unenrol_user($instance2, $this->student->id);
222 array_shift($expectedwikis);
223
224 // Call the external function without passing course id.
225 $result = mod_wiki_external::get_wikis_by_courses();
226 $result = external_api::clean_returnvalue($returndescription, $result);
227 $this->assertEquals($expectedwikis, $result['wikis']);
228
229 // Call for the second course we unenrolled the user from, expected warning.
230 $result = mod_wiki_external::get_wikis_by_courses(array($course2->id));
231 $this->assertCount(1, $result['warnings']);
232 $this->assertEquals('1', $result['warnings'][0]['warningcode']);
233 $this->assertEquals($course2->id, $result['warnings'][0]['itemid']);
234
235 // Now, try as a teacher for getting all the additional fields.
236 self::setUser($this->teacher);
237
238 $additionalfields = array('timecreated', 'timemodified');
239
240 foreach ($additionalfields as $field) {
241 $expectedwikis[0][$field] = $wiki1->{$field};
242 }
243
244 $result = mod_wiki_external::get_wikis_by_courses();
245 $result = external_api::clean_returnvalue($returndescription, $result);
246 $this->assertEquals($expectedwikis, $result['wikis']);
247
248 // Admin also should get all the information.
249 self::setAdminUser();
250
251 $result = mod_wiki_external::get_wikis_by_courses(array($this->course->id));
252 $result = external_api::clean_returnvalue($returndescription, $result);
253 $this->assertEquals($expectedwikis, $result['wikis']);
254
255 // Now, prohibit capabilities.
256 $this->setUser($this->student);
257 $contextcourse1 = context_course::instance($this->course->id);
3a93df8b
MG
258
259 // Default student role allows to view wiki and create pages.
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->assertEquals('Test wiki 1', $wikis['wikis'][0]['intro']);
263 $this->assertEquals(1, $wikis['wikis'][0]['cancreatepages']);
264
4a5acec2 265 // Prohibit capability = mod:wiki:viewpage on Course1 for students.
3a93df8b 266 assign_capability('mod/wiki:viewpage', CAP_PROHIBIT, $this->studentrole->id, $contextcourse1->id, true);
4a5acec2 267 accesslib_clear_all_caches_for_unit_testing();
3a93df8b 268 course_modinfo::clear_instance_cache(null);
4a5acec2
DP
269
270 $wikis = mod_wiki_external::get_wikis_by_courses(array($this->course->id));
271 $wikis = external_api::clean_returnvalue(mod_wiki_external::get_wikis_by_courses_returns(), $wikis);
3a93df8b 272 $this->assertEquals(0, count($wikis['wikis']));
4a5acec2
DP
273
274 // Prohibit capability = mod:wiki:createpage on Course1 for students.
3a93df8b 275 assign_capability('mod/wiki:viewpage', CAP_ALLOW, $this->studentrole->id, $contextcourse1->id, true);
4a5acec2
DP
276 assign_capability('mod/wiki:createpage', CAP_PROHIBIT, $this->studentrole->id, $contextcourse1->id);
277 accesslib_clear_all_caches_for_unit_testing();
3a93df8b 278 course_modinfo::clear_instance_cache(null);
4a5acec2
DP
279
280 $wikis = mod_wiki_external::get_wikis_by_courses(array($this->course->id));
281 $wikis = external_api::clean_returnvalue(mod_wiki_external::get_wikis_by_courses_returns(), $wikis);
282 $this->assertFalse($wikis['wikis'][0]['cancreatepages']);
c1baf89e
DP
283
284 }
285
286 /**
287 * Test view_wiki.
288 */
289 public function test_view_wiki() {
290
291 // Test invalid instance id.
292 try {
293 mod_wiki_external::view_wiki(0);
294 $this->fail('Exception expected due to invalid mod_wiki instance id.');
295 } catch (moodle_exception $e) {
296 $this->assertEquals('incorrectwikiid', $e->errorcode);
297 }
298
299 // Test not-enrolled user.
300 $usernotenrolled = self::getDataGenerator()->create_user();
301 $this->setUser($usernotenrolled);
302 try {
303 mod_wiki_external::view_wiki($this->wiki->id);
304 $this->fail('Exception expected due to not enrolled user.');
305 } catch (moodle_exception $e) {
306 $this->assertEquals('requireloginerror', $e->errorcode);
307 }
308
309 // Test user with full capabilities.
310 $this->setUser($this->student);
311
312 // Trigger and capture the event.
313 $sink = $this->redirectEvents();
314
315 $result = mod_wiki_external::view_wiki($this->wiki->id);
316 $result = external_api::clean_returnvalue(mod_wiki_external::view_wiki_returns(), $result);
317
318 $events = $sink->get_events();
319 $this->assertCount(1, $events);
320 $event = array_shift($events);
321
322 // Checking that the event contains the expected values.
323 $this->assertInstanceOf('\mod_wiki\event\course_module_viewed', $event);
324 $this->assertEquals($this->context, $event->get_context());
325 $moodlewiki = new \moodle_url('/mod/wiki/view.php', array('id' => $this->cm->id));
326 $this->assertEquals($moodlewiki, $event->get_url());
327 $this->assertEventContextNotUsed($event);
328 $this->assertNotEmpty($event->get_name());
329
330 // Test user with no capabilities.
331 // We need a explicit prohibit since this capability is allowed for students by default.
332 assign_capability('mod/wiki:viewpage', CAP_PROHIBIT, $this->studentrole->id, $this->context->id);
333 accesslib_clear_all_caches_for_unit_testing();
334
335 try {
336 mod_wiki_external::view_wiki($this->wiki->id);
337 $this->fail('Exception expected due to missing capability.');
338 } catch (moodle_exception $e) {
339 $this->assertEquals('cannotviewpage', $e->errorcode);
340 }
341
342 }
343
344 /**
345 * Test view_page.
346 */
347 public function test_view_page() {
348
349 // Test invalid page id.
350 try {
351 mod_wiki_external::view_page(0);
352 $this->fail('Exception expected due to invalid view_page page id.');
353 } catch (moodle_exception $e) {
354 $this->assertEquals('incorrectpageid', $e->errorcode);
355 }
356
357 // Test not-enrolled user.
358 $usernotenrolled = self::getDataGenerator()->create_user();
359 $this->setUser($usernotenrolled);
360 try {
361 mod_wiki_external::view_page($this->firstpage->id);
362 $this->fail('Exception expected due to not enrolled user.');
363 } catch (moodle_exception $e) {
364 $this->assertEquals('requireloginerror', $e->errorcode);
365 }
366
367 // Test user with full capabilities.
368 $this->setUser($this->student);
369
370 // Trigger and capture the event.
371 $sink = $this->redirectEvents();
372
373 $result = mod_wiki_external::view_page($this->firstpage->id);
374 $result = external_api::clean_returnvalue(mod_wiki_external::view_page_returns(), $result);
375
376 $events = $sink->get_events();
377 $this->assertCount(1, $events);
378 $event = array_shift($events);
379
380 // Checking that the event contains the expected values.
381 $this->assertInstanceOf('\mod_wiki\event\page_viewed', $event);
382 $this->assertEquals($this->context, $event->get_context());
383 $pageurl = new \moodle_url('/mod/wiki/view.php', array('pageid' => $this->firstpage->id));
384 $this->assertEquals($pageurl, $event->get_url());
385 $this->assertEventContextNotUsed($event);
386 $this->assertNotEmpty($event->get_name());
387
388 // Test user with no capabilities.
389 // We need a explicit prohibit since this capability is allowed for students by default.
390 assign_capability('mod/wiki:viewpage', CAP_PROHIBIT, $this->studentrole->id, $this->context->id);
391 accesslib_clear_all_caches_for_unit_testing();
392
393 try {
394 mod_wiki_external::view_page($this->firstpage->id);
395 $this->fail('Exception expected due to missing capability.');
396 } catch (moodle_exception $e) {
397 $this->assertEquals('cannotviewpage', $e->errorcode);
398 }
399
4a5acec2
DP
400 }
401
5380c664
DP
402 /**
403 * Test get_subwikis.
404 */
405 public function test_get_subwikis() {
406
407 // Test invalid wiki id.
408 try {
409 mod_wiki_external::get_subwikis(0);
410 $this->fail('Exception expected due to invalid get_subwikis wiki id.');
411 } catch (moodle_exception $e) {
412 $this->assertEquals('incorrectwikiid', $e->errorcode);
413 }
414
415 // Test not-enrolled user.
416 $usernotenrolled = self::getDataGenerator()->create_user();
417 $this->setUser($usernotenrolled);
418 try {
419 mod_wiki_external::get_subwikis($this->wiki->id);
420 $this->fail('Exception expected due to not enrolled user.');
421 } catch (moodle_exception $e) {
422 $this->assertEquals('requireloginerror', $e->errorcode);
423 }
424
425 // Test user with full capabilities.
426 $this->setUser($this->student);
427
428 // Create what we expect to be returned. We only test a basic case because deep testing is already done
429 // in the tests for wiki_get_visible_subwikis.
430 $expectedsubwikis = array();
431 $expectedsubwiki = array(
432 'id' => $this->firstpage->subwikiid,
433 'wikiid' => $this->wiki->id,
434 'groupid' => 0,
435 'userid' => 0,
436 'canedit' => true
437 );
438 $expectedsubwikis[] = $expectedsubwiki;
439
440 $result = mod_wiki_external::get_subwikis($this->wiki->id);
441 $result = external_api::clean_returnvalue(mod_wiki_external::get_subwikis_returns(), $result);
442 $this->assertEquals($expectedsubwikis, $result['subwikis']);
443 $this->assertCount(0, $result['warnings']);
444
445 // Test user with no capabilities.
446 // We need a explicit prohibit since this capability is allowed for students by default.
447 assign_capability('mod/wiki:viewpage', CAP_PROHIBIT, $this->studentrole->id, $this->context->id);
448 accesslib_clear_all_caches_for_unit_testing();
449
450 try {
451 mod_wiki_external::get_subwikis($this->wiki->id);
452 $this->fail('Exception expected due to missing capability.');
453 } catch (moodle_exception $e) {
454 $this->assertEquals('nopermissions', $e->errorcode);
455 }
456
457 }
458
ed4fb2bf
DP
459 /**
460 * Test get_subwiki_pages using an invalid wiki instance.
52f3e060
RT
461 *
462 * @expectedException moodle_exception
ed4fb2bf
DP
463 */
464 public function test_get_subwiki_pages_invalid_instance() {
ed4fb2bf
DP
465 mod_wiki_external::get_subwiki_pages(0);
466 }
467
468 /**
469 * Test get_subwiki_pages using a user not enrolled in the course.
52f3e060
RT
470 *
471 * @expectedException require_login_exception
ed4fb2bf
DP
472 */
473 public function test_get_subwiki_pages_unenrolled_user() {
474 // Create and use the user.
475 $usernotenrolled = self::getDataGenerator()->create_user();
476 $this->setUser($usernotenrolled);
477
ed4fb2bf
DP
478 mod_wiki_external::get_subwiki_pages($this->wiki->id);
479 }
480
481 /**
482 * Test get_subwiki_pages using a hidden wiki as student.
52f3e060
RT
483 *
484 * @expectedException require_login_exception
ed4fb2bf
DP
485 */
486 public function test_get_subwiki_pages_hidden_wiki_as_student() {
487 // Create a hidden wiki and try to get the list of pages.
488 $hiddenwiki = $this->getDataGenerator()->create_module('wiki',
489 array('course' => $this->course->id, 'visible' => false));
490
491 $this->setUser($this->student);
ed4fb2bf
DP
492 mod_wiki_external::get_subwiki_pages($hiddenwiki->id);
493 }
494
495 /**
496 * Test get_subwiki_pages without the viewpage capability.
52f3e060
RT
497 *
498 * @expectedException moodle_exception
ed4fb2bf
DP
499 */
500 public function test_get_subwiki_pages_without_viewpage_capability() {
501 // Prohibit capability = mod/wiki:viewpage on the course for students.
502 $contextcourse = context_course::instance($this->course->id);
503 assign_capability('mod/wiki:viewpage', CAP_PROHIBIT, $this->studentrole->id, $contextcourse->id);
504 accesslib_clear_all_caches_for_unit_testing();
505
506 $this->setUser($this->student);
ed4fb2bf
DP
507 mod_wiki_external::get_subwiki_pages($this->wiki->id);
508 }
509
510 /**
511 * Test get_subwiki_pages using an invalid userid.
52f3e060
RT
512 *
513 * @expectedException moodle_exception
ed4fb2bf
DP
514 */
515 public function test_get_subwiki_pages_invalid_userid() {
516 // Create an individual wiki.
517 $indwiki = $this->getDataGenerator()->create_module('wiki',
518 array('course' => $this->course->id, 'wikimode' => 'individual'));
519
ed4fb2bf
DP
520 mod_wiki_external::get_subwiki_pages($indwiki->id, 0, -10);
521 }
522
523 /**
524 * Test get_subwiki_pages using an invalid groupid.
52f3e060
RT
525 *
526 * @expectedException moodle_exception
ed4fb2bf
DP
527 */
528 public function test_get_subwiki_pages_invalid_groupid() {
529 // Create testing data.
530 $this->create_collaborative_wikis_with_groups();
531
ed4fb2bf
DP
532 mod_wiki_external::get_subwiki_pages($this->wikisep->id, -111);
533 }
534
535 /**
536 * Test get_subwiki_pages, check that a student can't see another user pages in an individual wiki without groups.
52f3e060
RT
537 *
538 * @expectedException moodle_exception
ed4fb2bf
DP
539 */
540 public function test_get_subwiki_pages_individual_student_see_other_user() {
541 // Create an individual wiki.
542 $indwiki = $this->getDataGenerator()->create_module('wiki',
543 array('course' => $this->course->id, 'wikimode' => 'individual'));
544
545 $this->setUser($this->student);
ed4fb2bf
DP
546 mod_wiki_external::get_subwiki_pages($indwiki->id, 0, $this->teacher->id);
547 }
548
549 /**
550 * Test get_subwiki_pages, check that a student can't get the pages from another group in
551 * a collaborative wiki using separate groups.
52f3e060
RT
552 *
553 * @expectedException moodle_exception
ed4fb2bf
DP
554 */
555 public function test_get_subwiki_pages_collaborative_separate_groups_student_see_other_group() {
556 // Create testing data.
557 $this->create_collaborative_wikis_with_groups();
558
559 $this->setUser($this->student);
ed4fb2bf
DP
560 mod_wiki_external::get_subwiki_pages($this->wikisep->id, $this->group2->id);
561 }
562
563 /**
564 * Test get_subwiki_pages, check that a student can't get the pages from another group in
565 * an individual wiki using separate groups.
52f3e060
RT
566 *
567 * @expectedException moodle_exception
ed4fb2bf
DP
568 */
569 public function test_get_subwiki_pages_individual_separate_groups_student_see_other_group() {
570 // Create testing data.
571 $this->create_individual_wikis_with_groups();
572
573 $this->setUser($this->student);
ed4fb2bf
DP
574 mod_wiki_external::get_subwiki_pages($this->wikisepind->id, $this->group2->id, $this->teacher->id);
575 }
576
577 /**
578 * Test get_subwiki_pages, check that a student can't get the pages from all participants in
579 * a collaborative wiki using separate groups.
52f3e060
RT
580 *
581 * @expectedException moodle_exception
ed4fb2bf
DP
582 */
583 public function test_get_subwiki_pages_collaborative_separate_groups_student_see_all_participants() {
584 // Create testing data.
585 $this->create_collaborative_wikis_with_groups();
586
587 $this->setUser($this->student);
ed4fb2bf
DP
588 mod_wiki_external::get_subwiki_pages($this->wikisep->id, 0);
589 }
590
591 /**
592 * Test get_subwiki_pages, check that a student can't get the pages from all participants in
593 * an individual wiki using separate groups.
52f3e060
RT
594 *
595 * @expectedException moodle_exception
ed4fb2bf
DP
596 */
597 public function test_get_subwiki_pages_individual_separate_groups_student_see_all_participants() {
598 // Create testing data.
599 $this->create_individual_wikis_with_groups();
600
601 $this->setUser($this->student);
ed4fb2bf
DP
602 mod_wiki_external::get_subwiki_pages($this->wikisepind->id, 0, $this->teacher->id);
603 }
604
605 /**
606 * Test get_subwiki_pages without groups and collaborative wiki.
607 */
608 public function test_get_subwiki_pages_collaborative() {
609
610 // Test user with full capabilities.
611 $this->setUser($this->student);
612
613 // Set expected result: first page.
614 $expectedpages = array();
615 $expectedfirstpage = (array) $this->firstpage;
616 $expectedfirstpage['caneditpage'] = true; // No groups and students have 'mod/wiki:editpage' capability.
617 $expectedfirstpage['firstpage'] = true;
618 $expectedfirstpage['contentformat'] = 1;
619 $expectedpages[] = $expectedfirstpage;
620
621 $result = mod_wiki_external::get_subwiki_pages($this->wiki->id);
622 $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result);
623 $this->assertEquals($expectedpages, $result['pages']);
624
625 // Check that groupid param is ignored since the wiki isn't using groups.
626 $result = mod_wiki_external::get_subwiki_pages($this->wiki->id, 1234);
627 $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result);
628 $this->assertEquals($expectedpages, $result['pages']);
629
630 // Check that userid param is ignored since the wiki is collaborative.
631 $result = mod_wiki_external::get_subwiki_pages($this->wiki->id, 1234, 1234);
632 $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result);
633 $this->assertEquals($expectedpages, $result['pages']);
634
635 // 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.
636 $newpage = $this->getDataGenerator()->get_plugin_generator('mod_wiki')->create_page(
637 $this->wiki, array('title' => 'AAA'));
638
639 $expectednewpage = (array) $newpage;
640 $expectednewpage['caneditpage'] = true; // No groups and students have 'mod/wiki:editpage' capability.
641 $expectednewpage['firstpage'] = false;
642 $expectednewpage['contentformat'] = 1;
643 array_unshift($expectedpages, $expectednewpage); // Add page to the beginning since it orders by title by default.
644
645 $result = mod_wiki_external::get_subwiki_pages($this->wiki->id);
646 $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result);
647 $this->assertEquals($expectedpages, $result['pages']);
648
649 // Now we'll order by ID. Since first page was created first it'll have a lower ID.
650 $expectedpages = array($expectedfirstpage, $expectednewpage);
651 $result = mod_wiki_external::get_subwiki_pages($this->wiki->id, 0, 0, array('sortby' => 'id'));
652 $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result);
653 $this->assertEquals($expectedpages, $result['pages']);
654
31bfb008
DP
655 // Check that WS doesn't return page content if includecontent is false, it returns the size instead.
656 foreach ($expectedpages as $i => $expectedpage) {
657 if (function_exists('mb_strlen') && ((int)ini_get('mbstring.func_overload') & 2)) {
658 $expectedpages[$i]['contentsize'] = mb_strlen($expectedpages[$i]['cachedcontent'], '8bit');
659 } else {
660 $expectedpages[$i]['contentsize'] = strlen($expectedpages[$i]['cachedcontent']);
661 }
662 unset($expectedpages[$i]['cachedcontent']);
663 unset($expectedpages[$i]['contentformat']);
664 }
ed4fb2bf
DP
665 $result = mod_wiki_external::get_subwiki_pages($this->wiki->id, 0, 0, array('sortby' => 'id', 'includecontent' => 0));
666 $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result);
667 $this->assertEquals($expectedpages, $result['pages']);
668 }
669
670 /**
671 * Test get_subwiki_pages without groups.
672 */
673 public function test_get_subwiki_pages_individual() {
674
675 // Create an individual wiki to test userid param.
676 $indwiki = $this->getDataGenerator()->create_module('wiki',
677 array('course' => $this->course->id, 'wikimode' => 'individual'));
678
679 // Perform a request before creating any page to check that an empty array is returned if subwiki doesn't exist.
680 $result = mod_wiki_external::get_subwiki_pages($indwiki->id);
681 $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result);
682 $this->assertEquals(array(), $result['pages']);
683
684 // Create first pages as student and teacher.
685 $this->setUser($this->student);
686 $indfirstpagestudent = $this->getDataGenerator()->get_plugin_generator('mod_wiki')->create_first_page($indwiki);
687 $this->setUser($this->teacher);
688 $indfirstpageteacher = $this->getDataGenerator()->get_plugin_generator('mod_wiki')->create_first_page($indwiki);
689
690 // Check that teacher can get his pages.
691 $expectedteacherpage = (array) $indfirstpageteacher;
692 $expectedteacherpage['caneditpage'] = true;
693 $expectedteacherpage['firstpage'] = true;
694 $expectedteacherpage['contentformat'] = 1;
695 $expectedpages = array($expectedteacherpage);
696
697 $result = mod_wiki_external::get_subwiki_pages($indwiki->id, 0, $this->teacher->id);
698 $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result);
699 $this->assertEquals($expectedpages, $result['pages']);
700
701 // Check that the teacher can see the student's pages.
702 $expectedstudentpage = (array) $indfirstpagestudent;
703 $expectedstudentpage['caneditpage'] = true;
704 $expectedstudentpage['firstpage'] = true;
705 $expectedstudentpage['contentformat'] = 1;
706 $expectedpages = array($expectedstudentpage);
707
708 $result = mod_wiki_external::get_subwiki_pages($indwiki->id, 0, $this->student->id);
709 $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result);
710 $this->assertEquals($expectedpages, $result['pages']);
711
712 // Now check that student can get his pages.
713 $this->setUser($this->student);
714
715 $result = mod_wiki_external::get_subwiki_pages($indwiki->id, 0, $this->student->id);
716 $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result);
717 $this->assertEquals($expectedpages, $result['pages']);
718
719 // Check that not using userid uses current user.
720 $result = mod_wiki_external::get_subwiki_pages($indwiki->id, 0);
721 $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result);
722 $this->assertEquals($expectedpages, $result['pages']);
723 }
724
725 /**
726 * Test get_subwiki_pages with groups and collaborative wikis.
727 */
728 public function test_get_subwiki_pages_separate_groups_collaborative() {
729
730 // Create testing data.
731 $this->create_collaborative_wikis_with_groups();
732
733 $this->setUser($this->student);
734
735 // Try to get pages from a valid group in separate groups wiki.
736
737 $expectedpage = (array) $this->fpsepg1;
738 $expectedpage['caneditpage'] = true; // User belongs to group and has 'mod/wiki:editpage' capability.
739 $expectedpage['firstpage'] = true;
740 $expectedpage['contentformat'] = 1;
741 $expectedpages = array($expectedpage);
742
743 $result = mod_wiki_external::get_subwiki_pages($this->wikisep->id, $this->group1->id);
744 $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result);
745 $this->assertEquals($expectedpages, $result['pages']);
746
747 // Let's check that not using groupid returns the same result (current group).
748 $result = mod_wiki_external::get_subwiki_pages($this->wikisep->id);
749 $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result);
750 $this->assertEquals($expectedpages, $result['pages']);
751
752 // Check that teacher can view a group pages without belonging to it.
753 $this->setUser($this->teacher);
754 $result = mod_wiki_external::get_subwiki_pages($this->wikisep->id, $this->group1->id);
755 $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result);
756 $this->assertEquals($expectedpages, $result['pages']);
757
758 // Check that teacher can get the pages from all participants.
759 $expectedpage = (array) $this->fpsepall;
760 $expectedpage['caneditpage'] = true;
761 $expectedpage['firstpage'] = true;
762 $expectedpage['contentformat'] = 1;
763 $expectedpages = array($expectedpage);
764
765 $result = mod_wiki_external::get_subwiki_pages($this->wikisep->id, 0);
766 $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result);
767 $this->assertEquals($expectedpages, $result['pages']);
768 }
769
770 /**
771 * Test get_subwiki_pages with groups and collaborative wikis.
772 */
773 public function test_get_subwiki_pages_visible_groups_collaborative() {
774
775 // Create testing data.
776 $this->create_collaborative_wikis_with_groups();
777
778 $this->setUser($this->student);
779
780 // Try to get pages from a valid group in visible groups wiki.
781
782 $expectedpage = (array) $this->fpvisg1;
783 $expectedpage['caneditpage'] = true; // User belongs to group and has 'mod/wiki:editpage' capability.
784 $expectedpage['firstpage'] = true;
785 $expectedpage['contentformat'] = 1;
786 $expectedpages = array($expectedpage);
787
788 $result = mod_wiki_external::get_subwiki_pages($this->wikivis->id, $this->group1->id);
789 $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result);
790 $this->assertEquals($expectedpages, $result['pages']);
791
792 // Check that with visible groups a student can get the pages of groups he doesn't belong to.
793 $expectedpage = (array) $this->fpvisg2;
794 $expectedpage['caneditpage'] = false; // User doesn't belong to group so he can't edit the page.
795 $expectedpage['firstpage'] = true;
796 $expectedpage['contentformat'] = 1;
797 $expectedpages = array($expectedpage);
798
799 $result = mod_wiki_external::get_subwiki_pages($this->wikivis->id, $this->group2->id);
800 $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result);
801 $this->assertEquals($expectedpages, $result['pages']);
802
803 // Check that with visible groups a student can get the pages of all participants.
804 $expectedpage = (array) $this->fpvisall;
805 $expectedpage['caneditpage'] = false;
806 $expectedpage['firstpage'] = true;
807 $expectedpage['contentformat'] = 1;
808 $expectedpages = array($expectedpage);
809
810 $result = mod_wiki_external::get_subwiki_pages($this->wikivis->id, 0);
811 $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result);
812 $this->assertEquals($expectedpages, $result['pages']);
813 }
814
815 /**
816 * Test get_subwiki_pages with groups and individual wikis.
817 */
818 public function test_get_subwiki_pages_separate_groups_individual() {
819
820 // Create testing data.
821 $this->create_individual_wikis_with_groups();
822
823 $this->setUser($this->student);
824
825 // Check that student can retrieve his pages from separate wiki.
826 $expectedpage = (array) $this->fpsepg1indstu;
827 $expectedpage['caneditpage'] = true;
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->student->id);
833 $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result);
834 $this->assertEquals($expectedpages, $result['pages']);
835
836 // Check that not using userid uses current user.
837 $result = mod_wiki_external::get_subwiki_pages($this->wikisepind->id, $this->group1->id);
838 $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result);
839 $this->assertEquals($expectedpages, $result['pages']);
840
841 // Check that the teacher can see the student pages.
842 $this->setUser($this->teacher);
843 $result = mod_wiki_external::get_subwiki_pages($this->wikisepind->id, $this->group1->id, $this->student->id);
844 $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result);
845 $this->assertEquals($expectedpages, $result['pages']);
846
847 // Check that a student can see pages from another user that belongs to his groups.
848 $this->setUser($this->student);
849 $expectedpage = (array) $this->fpsepg1indstu2;
850 $expectedpage['caneditpage'] = false;
851 $expectedpage['firstpage'] = true;
852 $expectedpage['contentformat'] = 1;
853 $expectedpages = array($expectedpage);
854
855 $result = mod_wiki_external::get_subwiki_pages($this->wikisepind->id, $this->group1->id, $this->student2->id);
856 $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result);
857 $this->assertEquals($expectedpages, $result['pages']);
858 }
859
860 /**
861 * Test get_subwiki_pages with groups and individual wikis.
862 */
863 public function test_get_subwiki_pages_visible_groups_individual() {
864
865 // Create testing data.
866 $this->create_individual_wikis_with_groups();
867
868 $this->setUser($this->student);
869
870 // Check that student can retrieve his pages from visible wiki.
871 $expectedpage = (array) $this->fpvisg1indstu;
872 $expectedpage['caneditpage'] = true;
873 $expectedpage['firstpage'] = true;
874 $expectedpage['contentformat'] = 1;
875 $expectedpages = array($expectedpage);
876
877 $result = mod_wiki_external::get_subwiki_pages($this->wikivisind->id, $this->group1->id, $this->student->id);
878 $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result);
879 $this->assertEquals($expectedpages, $result['pages']);
880
881 // Check that student can see teacher pages in visible groups, even if the user doesn't belong to the group.
882 $expectedpage = (array) $this->fpvisg2indt;
883 $expectedpage['caneditpage'] = false;
884 $expectedpage['firstpage'] = true;
885 $expectedpage['contentformat'] = 1;
886 $expectedpages = array($expectedpage);
887
888 $result = mod_wiki_external::get_subwiki_pages($this->wikivisind->id, $this->group2->id, $this->teacher->id);
889 $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result);
890 $this->assertEquals($expectedpages, $result['pages']);
891
892 // Check that with visible groups a student can get the pages of all participants.
893 $expectedpage = (array) $this->fpvisallindt;
894 $expectedpage['caneditpage'] = false;
895 $expectedpage['firstpage'] = true;
896 $expectedpage['contentformat'] = 1;
897 $expectedpages = array($expectedpage);
898
899 $result = mod_wiki_external::get_subwiki_pages($this->wikivisind->id, 0, $this->teacher->id);
900 $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result);
901 $this->assertEquals($expectedpages, $result['pages']);
902 }
903
9a4c5222
DP
904 /**
905 * Test get_page_contents using an invalid pageid.
52f3e060
RT
906 *
907 * @expectedException moodle_exception
9a4c5222
DP
908 */
909 public function test_get_page_contents_invalid_pageid() {
9a4c5222
DP
910 mod_wiki_external::get_page_contents(0);
911 }
912
913 /**
914 * Test get_page_contents using a user not enrolled in the course.
52f3e060
RT
915 *
916 * @expectedException require_login_exception
9a4c5222
DP
917 */
918 public function test_get_page_contents_unenrolled_user() {
919 // Create and use the user.
920 $usernotenrolled = self::getDataGenerator()->create_user();
921 $this->setUser($usernotenrolled);
922
9a4c5222
DP
923 mod_wiki_external::get_page_contents($this->firstpage->id);
924 }
925
926 /**
927 * Test get_page_contents using a hidden wiki as student.
52f3e060
RT
928 *
929 * @expectedException require_login_exception
9a4c5222
DP
930 */
931 public function test_get_page_contents_hidden_wiki_as_student() {
932 // Create a hidden wiki and try to get a page contents.
933 $hiddenwiki = $this->getDataGenerator()->create_module('wiki',
934 array('course' => $this->course->id, 'visible' => false));
935 $hiddenpage = $this->getDataGenerator()->get_plugin_generator('mod_wiki')->create_page($hiddenwiki);
936
937 $this->setUser($this->student);
9a4c5222
DP
938 mod_wiki_external::get_page_contents($hiddenpage->id);
939 }
940
941 /**
942 * Test get_page_contents without the viewpage capability.
52f3e060
RT
943 *
944 * @expectedException moodle_exception
9a4c5222
DP
945 */
946 public function test_get_page_contents_without_viewpage_capability() {
947 // Prohibit capability = mod/wiki:viewpage on the course for students.
948 $contextcourse = context_course::instance($this->course->id);
949 assign_capability('mod/wiki:viewpage', CAP_PROHIBIT, $this->studentrole->id, $contextcourse->id);
950 accesslib_clear_all_caches_for_unit_testing();
951
952 $this->setUser($this->student);
9a4c5222
DP
953 mod_wiki_external::get_page_contents($this->firstpage->id);
954 }
955
956 /**
957 * Test get_page_contents, check that a student can't get a page from another group when
958 * using separate groups.
52f3e060
RT
959 *
960 * @expectedException moodle_exception
9a4c5222
DP
961 */
962 public function test_get_page_contents_separate_groups_student_see_other_group() {
963 // Create testing data.
964 $this->create_individual_wikis_with_groups();
965
966 $this->setUser($this->student);
9a4c5222
DP
967 mod_wiki_external::get_page_contents($this->fpsepg2indt->id);
968 }
969
970 /**
971 * Test get_page_contents without groups. We won't test all the possible cases because that's already
972 * done in the tests for get_subwiki_pages.
973 */
974 public function test_get_page_contents() {
975
976 // Test user with full capabilities.
977 $this->setUser($this->student);
978
979 // Set expected result: first page.
980 $expectedpage = array(
981 'id' => $this->firstpage->id,
982 'wikiid' => $this->wiki->id,
983 'subwikiid' => $this->firstpage->subwikiid,
984 'groupid' => 0, // No groups.
985 'userid' => 0, // Collaborative.
986 'title' => $this->firstpage->title,
987 'cachedcontent' => $this->firstpage->cachedcontent,
988 'contentformat' => 1,
7f98de7d
DP
989 'caneditpage' => true,
990 'version' => 1
9a4c5222
DP
991 );
992
993 $result = mod_wiki_external::get_page_contents($this->firstpage->id);
994 $result = external_api::clean_returnvalue(mod_wiki_external::get_page_contents_returns(), $result);
995 $this->assertEquals($expectedpage, $result['page']);
996
997 // Add a new page to the wiki and test with it.
998 $newpage = $this->getDataGenerator()->get_plugin_generator('mod_wiki')->create_page($this->wiki);
999
1000 $expectedpage['id'] = $newpage->id;
1001 $expectedpage['title'] = $newpage->title;
1002 $expectedpage['cachedcontent'] = $newpage->cachedcontent;
1003
1004 $result = mod_wiki_external::get_page_contents($newpage->id);
1005 $result = external_api::clean_returnvalue(mod_wiki_external::get_page_contents_returns(), $result);
1006 $this->assertEquals($expectedpage, $result['page']);
1007 }
1008
1009 /**
1010 * Test get_page_contents with groups. We won't test all the possible cases because that's already
1011 * done in the tests for get_subwiki_pages.
1012 */
1013 public function test_get_page_contents_with_groups() {
1014
1015 // Create testing data.
1016 $this->create_individual_wikis_with_groups();
1017
1018 // Try to get page from a valid group in separate groups wiki.
1019 $this->setUser($this->student);
1020
1021 $expectedfpsepg1indstu = array(
1022 'id' => $this->fpsepg1indstu->id,
1023 'wikiid' => $this->wikisepind->id,
1024 'subwikiid' => $this->fpsepg1indstu->subwikiid,
1025 'groupid' => $this->group1->id,
1026 'userid' => $this->student->id,
1027 'title' => $this->fpsepg1indstu->title,
1028 'cachedcontent' => $this->fpsepg1indstu->cachedcontent,
1029 'contentformat' => 1,
7f98de7d
DP
1030 'caneditpage' => true,
1031 'version' => 1
9a4c5222
DP
1032 );
1033
1034 $result = mod_wiki_external::get_page_contents($this->fpsepg1indstu->id);
1035 $result = external_api::clean_returnvalue(mod_wiki_external::get_page_contents_returns(), $result);
1036 $this->assertEquals($expectedfpsepg1indstu, $result['page']);
1037
1038 // Check that teacher can view a group pages without belonging to it.
1039 $this->setUser($this->teacher);
1040 $result = mod_wiki_external::get_page_contents($this->fpsepg1indstu->id);
1041 $result = external_api::clean_returnvalue(mod_wiki_external::get_page_contents_returns(), $result);
1042 $this->assertEquals($expectedfpsepg1indstu, $result['page']);
1043 }
1044
7ef81de8
DP
1045 /**
1046 * Test get_subwiki_files using a wiki without files.
1047 */
1048 public function test_get_subwiki_files_no_files() {
1049 $result = mod_wiki_external::get_subwiki_files($this->wiki->id);
1050 $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_files_returns(), $result);
1051 $this->assertCount(0, $result['files']);
1052 $this->assertCount(0, $result['warnings']);
1053 }
1054
1055 /**
1056 * Test get_subwiki_files, check that a student can't get files from another group's subwiki when
1057 * using separate groups.
52f3e060
RT
1058 *
1059 * @expectedException moodle_exception
7ef81de8
DP
1060 */
1061 public function test_get_subwiki_files_separate_groups_student_see_other_group() {
1062 // Create testing data.
1063 $this->create_collaborative_wikis_with_groups();
1064
1065 $this->setUser($this->student);
7ef81de8
DP
1066 mod_wiki_external::get_subwiki_files($this->wikisep->id, $this->group2->id);
1067 }
1068
1069 /**
1070 * Test get_subwiki_files using a collaborative wiki without groups.
1071 */
1072 public function test_get_subwiki_files_collaborative_no_groups() {
1073 $this->setUser($this->student);
1074
1075 // Add a file as subwiki attachment.
1076 $fs = get_file_storage();
1077 $file = array('component' => 'mod_wiki', 'filearea' => 'attachments',
1078 'contextid' => $this->context->id, 'itemid' => $this->firstpage->subwikiid,
1079 'filename' => 'image.jpg', 'filepath' => '/', 'timemodified' => time());
1080 $content = 'IMAGE';
1081 $fs->create_file_from_string($file, $content);
1082
1083 $expectedfile = array(
1084 'filename' => $file['filename'],
1085 'filepath' => $file['filepath'],
1086 'mimetype' => 'image/jpeg',
1104a9fa 1087 'isexternalfile' => false,
7ef81de8
DP
1088 'filesize' => strlen($content),
1089 'timemodified' => $file['timemodified'],
1090 'fileurl' => moodle_url::make_webservice_pluginfile_url($file['contextid'], $file['component'],
1091 $file['filearea'], $file['itemid'], $file['filepath'], $file['filename']),
1092 );
1093
1094 // Call the WS and check that it returns this file.
1095 $result = mod_wiki_external::get_subwiki_files($this->wiki->id);
1096 $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_files_returns(), $result);
1097 $this->assertCount(1, $result['files']);
1098 $this->assertEquals($expectedfile, $result['files'][0]);
1099
1100 // Now add another file to the same subwiki.
1101 $file['filename'] = 'Another image.jpg';
1102 $file['timemodified'] = time();
1103 $content = 'ANOTHER IMAGE';
1104 $fs->create_file_from_string($file, $content);
1105
1106 $expectedfile['filename'] = $file['filename'];
1107 $expectedfile['timemodified'] = $file['timemodified'];
1108 $expectedfile['filesize'] = strlen($content);
1109 $expectedfile['fileurl'] = moodle_url::make_webservice_pluginfile_url($file['contextid'], $file['component'],
1110 $file['filearea'], $file['itemid'], $file['filepath'], $file['filename']);
1111
1112 // Call the WS and check that it returns both files file.
1113 $result = mod_wiki_external::get_subwiki_files($this->wiki->id);
1114 $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_files_returns(), $result);
1115 $this->assertCount(2, $result['files']);
1116 // The new file is returned first because they're returned in alphabetical order.
1117 $this->assertEquals($expectedfile, $result['files'][0]);
1118 }
1119
1120 /**
1121 * Test get_subwiki_files using an individual wiki with visible groups.
1122 */
1123 public function test_get_subwiki_files_visible_groups_individual() {
1124 // Create testing data.
1125 $this->create_individual_wikis_with_groups();
1126
1127 $this->setUser($this->student);
1128
1129 // Add a file as subwiki attachment in the student group 1 subwiki.
1130 $fs = get_file_storage();
1131 $contextwiki = context_module::instance($this->wikivisind->cmid);
1132 $file = array('component' => 'mod_wiki', 'filearea' => 'attachments',
1133 'contextid' => $contextwiki->id, 'itemid' => $this->fpvisg1indstu->subwikiid,
1134 'filename' => 'image.jpg', 'filepath' => '/', 'timemodified' => time());
1135 $content = 'IMAGE';
1136 $fs->create_file_from_string($file, $content);
1137
1138 $expectedfile = array(
1139 'filename' => $file['filename'],
1140 'filepath' => $file['filepath'],
1141 'mimetype' => 'image/jpeg',
1104a9fa 1142 'isexternalfile' => false,
7ef81de8
DP
1143 'filesize' => strlen($content),
1144 'timemodified' => $file['timemodified'],
1145 'fileurl' => moodle_url::make_webservice_pluginfile_url($file['contextid'], $file['component'],
1146 $file['filearea'], $file['itemid'], $file['filepath'], $file['filename']),
1147 );
1148
1149 // Call the WS and check that it returns this file.
1150 $result = mod_wiki_external::get_subwiki_files($this->wikivisind->id, $this->group1->id);
1151 $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_files_returns(), $result);
1152 $this->assertCount(1, $result['files']);
1153 $this->assertEquals($expectedfile, $result['files'][0]);
1154
1155 // Now check that a teacher can see it too.
1156 $this->setUser($this->teacher);
1157 $result = mod_wiki_external::get_subwiki_files($this->wikivisind->id, $this->group1->id, $this->student->id);
1158 $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_files_returns(), $result);
1159 $this->assertCount(1, $result['files']);
1160 $this->assertEquals($expectedfile, $result['files'][0]);
1161 }
1162
20dca621
PFO
1163
1164 /**
1165 * Test get_page_for_editing. We won't test all the possible cases because that's already
1166 * done in the tests for wiki_parser_proxy::get_section.
1167 */
1168 public function test_get_page_for_editing() {
1169
1170 $this->create_individual_wikis_with_groups();
1171
3b40103c
DP
1172 // We add a <span> in the first title to verify the WS works sending HTML in section.
1173 $sectioncontent = '<h1><span>Title1</span></h1>Text inside section';
20dca621
PFO
1174 $pagecontent = $sectioncontent.'<h1>Title2</h1>Text inside section';
1175 $newpage = $this->getDataGenerator()->get_plugin_generator('mod_wiki')->create_page(
1176 $this->wiki, array('content' => $pagecontent));
1177
1178 // Test user with full capabilities.
1179 $this->setUser($this->student);
1180
1181 // Set expected result: Full Page content.
1182 $expected = array(
1183 'content' => $pagecontent,
1184 'contentformat' => 'html',
1185 'version' => '1'
1186 );
1187
1188 $result = mod_wiki_external::get_page_for_editing($newpage->id);
1189 $result = external_api::clean_returnvalue(mod_wiki_external::get_page_for_editing_returns(), $result);
1190 $this->assertEquals($expected, $result['pagesection']);
1191
1192 // Set expected result: Section Page content.
1193 $expected = array(
1194 'content' => $sectioncontent,
1195 'contentformat' => 'html',
1196 'version' => '1'
1197 );
1198
3b40103c 1199 $result = mod_wiki_external::get_page_for_editing($newpage->id, '<span>Title1</span>');
20dca621
PFO
1200 $result = external_api::clean_returnvalue(mod_wiki_external::get_page_for_editing_returns(), $result);
1201 $this->assertEquals($expected, $result['pagesection']);
1202 }
1203
a6b6f0d2 1204 /**
c13c21b9
PFO
1205 * Test test_get_page_locking.
1206 */
1207 public function test_get_page_locking() {
1208
1209 $this->create_individual_wikis_with_groups();
1210
1211 $pagecontent = '<h1>Title1</h1>Text inside section<h1>Title2</h1>Text inside section';
1212 $newpage = $this->getDataGenerator()->get_plugin_generator('mod_wiki')->create_page(
1213 $this->wiki, array('content' => $pagecontent));
1214
1215 // Test user with full capabilities.
1216 $this->setUser($this->student);
1217
1218 // Test Section locking.
1219 $expected = array(
1220 'version' => '1'
1221 );
1222
1223 $result = mod_wiki_external::get_page_for_editing($newpage->id, 'Title1', true);
1224 $result = external_api::clean_returnvalue(mod_wiki_external::get_page_for_editing_returns(), $result);
1225 $this->assertEquals($expected, $result['pagesection']);
1226
1227 // Test the section is locked.
1228 $this->setUser($this->student2);
1229 try {
1230 mod_wiki_external::get_page_for_editing($newpage->id, 'Title1', true);
1231 $this->fail('Exception expected due to not page locking.');
1232 } catch (moodle_exception $e) {
1233 $this->assertEquals('pageislocked', $e->errorcode);
1234 }
1235
1236 // Test the page is locked.
1237 try {
1238 mod_wiki_external::get_page_for_editing($newpage->id, null, true);
1239 $this->fail('Exception expected due to not page locking.');
1240 } catch (moodle_exception $e) {
1241 $this->assertEquals('pageislocked', $e->errorcode);
1242 }
1243
1244 // Test the other section is not locked.
1245 $result = mod_wiki_external::get_page_for_editing($newpage->id, 'Title2', true);
1246 $result = external_api::clean_returnvalue(mod_wiki_external::get_page_for_editing_returns(), $result);
1247 $this->assertEquals($expected, $result['pagesection']);
1248
1249 // Back to the original user to test version change when editing.
1250 $this->setUser($this->student);
1251 $newsectioncontent = '<h1>Title2</h1>New test2';
1252 $result = mod_wiki_external::edit_page($newpage->id, $newsectioncontent, 'Title1');
1253
1254 $expected = array(
1255 'version' => '2'
1256 );
1257 $result = mod_wiki_external::get_page_for_editing($newpage->id, 'Title1', true);
1258 $result = external_api::clean_returnvalue(mod_wiki_external::get_page_for_editing_returns(), $result);
1259 $this->assertEquals($expected, $result['pagesection']);
1260 }
1261
1262 /**
a6b6f0d2
PFO
1263 * Test new_page. We won't test all the possible cases because that's already
1264 * done in the tests for wiki_create_page.
1265 */
1266 public function test_new_page() {
1267
1268 $this->create_individual_wikis_with_groups();
1269
1270 $sectioncontent = '<h1>Title1</h1>Text inside section';
1271 $pagecontent = $sectioncontent.'<h1>Title2</h1>Text inside section';
1272 $pagetitle = 'Page Title';
1273
1274 // Test user with full capabilities.
1275 $this->setUser($this->student);
1276
1277 // Test on existing subwiki.
1278 $result = mod_wiki_external::new_page($pagetitle, $pagecontent, 'html', $this->fpsepg1indstu->subwikiid);
1279 $result = external_api::clean_returnvalue(mod_wiki_external::new_page_returns(), $result);
1280 $this->assertInternalType('int', $result['pageid']);
1281
1282 $version = wiki_get_current_version($result['pageid']);
1283 $this->assertEquals($pagecontent, $version->content);
1284 $this->assertEquals('html', $version->contentformat);
1285
1286 $page = wiki_get_page($result['pageid']);
1287 $this->assertEquals($pagetitle, $page->title);
1288
1289 // Test existing page creation.
1290 try {
1291 mod_wiki_external::new_page($pagetitle, $pagecontent, 'html', $this->fpsepg1indstu->subwikiid);
1292 $this->fail('Exception expected due to creation of an existing page.');
1293 } catch (moodle_exception $e) {
1294 $this->assertEquals('pageexists', $e->errorcode);
1295 }
1296
1297 // Test on non existing subwiki. Add student to group2 to have a new subwiki to be created.
1298 $this->getDataGenerator()->create_group_member(array('userid' => $this->student->id, 'groupid' => $this->group2->id));
1299 $result = mod_wiki_external::new_page($pagetitle, $pagecontent, 'html', null, $this->wikisepind->id, $this->student->id,
1300 $this->group2->id);
1301 $result = external_api::clean_returnvalue(mod_wiki_external::new_page_returns(), $result);
1302 $this->assertInternalType('int', $result['pageid']);
1303
1304 $version = wiki_get_current_version($result['pageid']);
1305 $this->assertEquals($pagecontent, $version->content);
1306 $this->assertEquals('html', $version->contentformat);
1307
1308 $page = wiki_get_page($result['pageid']);
1309 $this->assertEquals($pagetitle, $page->title);
1310
1311 $subwiki = wiki_get_subwiki($page->subwikiid);
1312 $expected = new StdClass();
1313 $expected->id = $subwiki->id;
1314 $expected->wikiid = $this->wikisepind->id;
1315 $expected->groupid = $this->group2->id;
1316 $expected->userid = $this->student->id;
1317 $this->assertEquals($expected, $subwiki);
1318
1319 // Check page creation for a user not in course.
1320 $this->studentnotincourse = self::getDataGenerator()->create_user();
1321 $this->anothercourse = $this->getDataGenerator()->create_course();
1322 $this->groupnotincourse = $this->getDataGenerator()->create_group(array('courseid' => $this->anothercourse->id));
1323
1324 try {
1325 mod_wiki_external::new_page($pagetitle, $pagecontent, 'html', null, $this->wikisepind->id,
1326 $this->studentnotincourse->id, $this->groupnotincourse->id);
1327 $this->fail('Exception expected due to creation of an invalid subwiki creation.');
1328 } catch (moodle_exception $e) {
1329 $this->assertEquals('cannoteditpage', $e->errorcode);
1330 }
1331
1332 }
1333
cf16f6ed
PFO
1334 /**
1335 * Test edit_page. We won't test all the possible cases because that's already
1336 * done in the tests for wiki_save_section / wiki_save_page.
1337 */
1338 public function test_edit_page() {
1339
1340 $this->create_individual_wikis_with_groups();
1341
1342 // Test user with full capabilities.
1343 $this->setUser($this->student);
1344
1345 $newpage = $this->getDataGenerator()->get_plugin_generator('mod_wiki')->create_page($this->wikisepind,
1346 array('group' => $this->group1->id, 'content' => 'Test'));
1347
1348 // Test edit whole page.
3b40103c
DP
1349 // We add <span> in the titles to verify the WS works sending HTML in section.
1350 $sectioncontent = '<h1><span>Title1</span></h1>Text inside section';
1351 $newpagecontent = $sectioncontent.'<h1><span>Title2</span></h1>Text inside section';
cf16f6ed
PFO
1352
1353 $result = mod_wiki_external::edit_page($newpage->id, $newpagecontent);
1354 $result = external_api::clean_returnvalue(mod_wiki_external::edit_page_returns(), $result);
1355 $this->assertInternalType('int', $result['pageid']);
1356
1357 $version = wiki_get_current_version($result['pageid']);
1358 $this->assertEquals($newpagecontent, $version->content);
1359
1360 // Test edit section.
3b40103c
DP
1361 $newsectioncontent = '<h1><span>Title2</span></h1>New test2';
1362 $section = '<span>Title2</span>';
cf16f6ed
PFO
1363
1364 $result = mod_wiki_external::edit_page($newpage->id, $newsectioncontent, $section);
1365 $result = external_api::clean_returnvalue(mod_wiki_external::edit_page_returns(), $result);
1366 $this->assertInternalType('int', $result['pageid']);
1367
1368 $expected = $sectioncontent . $newsectioncontent;
1369
1370 $version = wiki_get_current_version($result['pageid']);
1371 $this->assertEquals($expected, $version->content);
1372
1373 // Test locked section.
3b40103c
DP
1374 $newsectioncontent = '<h1><span>Title2</span></h1>New test2';
1375 $section = '<span>Title2</span>';
cf16f6ed
PFO
1376
1377 try {
1378 // Using user 1 to avoid other users to edit.
1379 wiki_set_lock($newpage->id, 1, $section, true);
1380 mod_wiki_external::edit_page($newpage->id, $newsectioncontent, $section);
1381 $this->fail('Exception expected due to locked section');
1382 } catch (moodle_exception $e) {
1383 $this->assertEquals('pageislocked', $e->errorcode);
1384 }
1385
1386 // Test edit non existing section.
1387 $newsectioncontent = '<h1>Title3</h1>New test3';
1388 $section = 'Title3';
1389
1390 try {
1391 mod_wiki_external::edit_page($newpage->id, $newsectioncontent, $section);
1392 $this->fail('Exception expected due to non existing section in the page.');
1393 } catch (moodle_exception $e) {
1394 $this->assertEquals('invalidsection', $e->errorcode);
1395 }
1396
1397 }
1398
4a5acec2 1399}