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