MDL-55091 phpunit: Add support for findNodes, as they are now deprecated
[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.
451 */
452 public function test_get_subwiki_pages_invalid_instance() {
453 $this->setExpectedException('moodle_exception');
454 mod_wiki_external::get_subwiki_pages(0);
455 }
456
457 /**
458 * Test get_subwiki_pages using a user not enrolled in the course.
459 */
460 public function test_get_subwiki_pages_unenrolled_user() {
461 // Create and use the user.
462 $usernotenrolled = self::getDataGenerator()->create_user();
463 $this->setUser($usernotenrolled);
464
465 $this->setExpectedException('require_login_exception');
466 mod_wiki_external::get_subwiki_pages($this->wiki->id);
467 }
468
469 /**
470 * Test get_subwiki_pages using a hidden wiki as student.
471 */
472 public function test_get_subwiki_pages_hidden_wiki_as_student() {
473 // Create a hidden wiki and try to get the list of pages.
474 $hiddenwiki = $this->getDataGenerator()->create_module('wiki',
475 array('course' => $this->course->id, 'visible' => false));
476
477 $this->setUser($this->student);
478 $this->setExpectedException('require_login_exception');
479 mod_wiki_external::get_subwiki_pages($hiddenwiki->id);
480 }
481
482 /**
483 * Test get_subwiki_pages without the viewpage capability.
484 */
485 public function test_get_subwiki_pages_without_viewpage_capability() {
486 // Prohibit capability = mod/wiki:viewpage on the course for students.
487 $contextcourse = context_course::instance($this->course->id);
488 assign_capability('mod/wiki:viewpage', CAP_PROHIBIT, $this->studentrole->id, $contextcourse->id);
489 accesslib_clear_all_caches_for_unit_testing();
490
491 $this->setUser($this->student);
492 $this->setExpectedException('moodle_exception');
493 mod_wiki_external::get_subwiki_pages($this->wiki->id);
494 }
495
496 /**
497 * Test get_subwiki_pages using an invalid userid.
498 */
499 public function test_get_subwiki_pages_invalid_userid() {
500 // Create an individual wiki.
501 $indwiki = $this->getDataGenerator()->create_module('wiki',
502 array('course' => $this->course->id, 'wikimode' => 'individual'));
503
504 $this->setExpectedException('moodle_exception');
505 mod_wiki_external::get_subwiki_pages($indwiki->id, 0, -10);
506 }
507
508 /**
509 * Test get_subwiki_pages using an invalid groupid.
510 */
511 public function test_get_subwiki_pages_invalid_groupid() {
512 // Create testing data.
513 $this->create_collaborative_wikis_with_groups();
514
515 $this->setExpectedException('moodle_exception');
516 mod_wiki_external::get_subwiki_pages($this->wikisep->id, -111);
517 }
518
519 /**
520 * Test get_subwiki_pages, check that a student can't see another user pages in an individual wiki without groups.
521 */
522 public function test_get_subwiki_pages_individual_student_see_other_user() {
523 // Create an individual wiki.
524 $indwiki = $this->getDataGenerator()->create_module('wiki',
525 array('course' => $this->course->id, 'wikimode' => 'individual'));
526
527 $this->setUser($this->student);
528 $this->setExpectedException('moodle_exception');
529 mod_wiki_external::get_subwiki_pages($indwiki->id, 0, $this->teacher->id);
530 }
531
532 /**
533 * Test get_subwiki_pages, check that a student can't get the pages from another group in
534 * a collaborative wiki using separate groups.
535 */
536 public function test_get_subwiki_pages_collaborative_separate_groups_student_see_other_group() {
537 // Create testing data.
538 $this->create_collaborative_wikis_with_groups();
539
540 $this->setUser($this->student);
541 $this->setExpectedException('moodle_exception');
542 mod_wiki_external::get_subwiki_pages($this->wikisep->id, $this->group2->id);
543 }
544
545 /**
546 * Test get_subwiki_pages, check that a student can't get the pages from another group in
547 * an individual wiki using separate groups.
548 */
549 public function test_get_subwiki_pages_individual_separate_groups_student_see_other_group() {
550 // Create testing data.
551 $this->create_individual_wikis_with_groups();
552
553 $this->setUser($this->student);
554 $this->setExpectedException('moodle_exception');
555 mod_wiki_external::get_subwiki_pages($this->wikisepind->id, $this->group2->id, $this->teacher->id);
556 }
557
558 /**
559 * Test get_subwiki_pages, check that a student can't get the pages from all participants in
560 * a collaborative wiki using separate groups.
561 */
562 public function test_get_subwiki_pages_collaborative_separate_groups_student_see_all_participants() {
563 // Create testing data.
564 $this->create_collaborative_wikis_with_groups();
565
566 $this->setUser($this->student);
567 $this->setExpectedException('moodle_exception');
568 mod_wiki_external::get_subwiki_pages($this->wikisep->id, 0);
569 }
570
571 /**
572 * Test get_subwiki_pages, check that a student can't get the pages from all participants in
573 * an individual wiki using separate groups.
574 */
575 public function test_get_subwiki_pages_individual_separate_groups_student_see_all_participants() {
576 // Create testing data.
577 $this->create_individual_wikis_with_groups();
578
579 $this->setUser($this->student);
580 $this->setExpectedException('moodle_exception');
581 mod_wiki_external::get_subwiki_pages($this->wikisepind->id, 0, $this->teacher->id);
582 }
583
584 /**
585 * Test get_subwiki_pages without groups and collaborative wiki.
586 */
587 public function test_get_subwiki_pages_collaborative() {
588
589 // Test user with full capabilities.
590 $this->setUser($this->student);
591
592 // Set expected result: first page.
593 $expectedpages = array();
594 $expectedfirstpage = (array) $this->firstpage;
595 $expectedfirstpage['caneditpage'] = true; // No groups and students have 'mod/wiki:editpage' capability.
596 $expectedfirstpage['firstpage'] = true;
597 $expectedfirstpage['contentformat'] = 1;
598 $expectedpages[] = $expectedfirstpage;
599
600 $result = mod_wiki_external::get_subwiki_pages($this->wiki->id);
601 $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result);
602 $this->assertEquals($expectedpages, $result['pages']);
603
604 // Check that groupid param is ignored since the wiki isn't using groups.
605 $result = mod_wiki_external::get_subwiki_pages($this->wiki->id, 1234);
606 $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result);
607 $this->assertEquals($expectedpages, $result['pages']);
608
609 // Check that userid param is ignored since the wiki is collaborative.
610 $result = mod_wiki_external::get_subwiki_pages($this->wiki->id, 1234, 1234);
611 $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result);
612 $this->assertEquals($expectedpages, $result['pages']);
613
614 // 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.
615 $newpage = $this->getDataGenerator()->get_plugin_generator('mod_wiki')->create_page(
616 $this->wiki, array('title' => 'AAA'));
617
618 $expectednewpage = (array) $newpage;
619 $expectednewpage['caneditpage'] = true; // No groups and students have 'mod/wiki:editpage' capability.
620 $expectednewpage['firstpage'] = false;
621 $expectednewpage['contentformat'] = 1;
622 array_unshift($expectedpages, $expectednewpage); // Add page to the beginning since it orders by title by default.
623
624 $result = mod_wiki_external::get_subwiki_pages($this->wiki->id);
625 $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result);
626 $this->assertEquals($expectedpages, $result['pages']);
627
628 // Now we'll order by ID. Since first page was created first it'll have a lower ID.
629 $expectedpages = array($expectedfirstpage, $expectednewpage);
630 $result = mod_wiki_external::get_subwiki_pages($this->wiki->id, 0, 0, array('sortby' => 'id'));
631 $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result);
632 $this->assertEquals($expectedpages, $result['pages']);
633
31bfb008
DP
634 // Check that WS doesn't return page content if includecontent is false, it returns the size instead.
635 foreach ($expectedpages as $i => $expectedpage) {
636 if (function_exists('mb_strlen') && ((int)ini_get('mbstring.func_overload') & 2)) {
637 $expectedpages[$i]['contentsize'] = mb_strlen($expectedpages[$i]['cachedcontent'], '8bit');
638 } else {
639 $expectedpages[$i]['contentsize'] = strlen($expectedpages[$i]['cachedcontent']);
640 }
641 unset($expectedpages[$i]['cachedcontent']);
642 unset($expectedpages[$i]['contentformat']);
643 }
ed4fb2bf
DP
644 $result = mod_wiki_external::get_subwiki_pages($this->wiki->id, 0, 0, array('sortby' => 'id', 'includecontent' => 0));
645 $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result);
646 $this->assertEquals($expectedpages, $result['pages']);
647 }
648
649 /**
650 * Test get_subwiki_pages without groups.
651 */
652 public function test_get_subwiki_pages_individual() {
653
654 // Create an individual wiki to test userid param.
655 $indwiki = $this->getDataGenerator()->create_module('wiki',
656 array('course' => $this->course->id, 'wikimode' => 'individual'));
657
658 // Perform a request before creating any page to check that an empty array is returned if subwiki doesn't exist.
659 $result = mod_wiki_external::get_subwiki_pages($indwiki->id);
660 $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result);
661 $this->assertEquals(array(), $result['pages']);
662
663 // Create first pages as student and teacher.
664 $this->setUser($this->student);
665 $indfirstpagestudent = $this->getDataGenerator()->get_plugin_generator('mod_wiki')->create_first_page($indwiki);
666 $this->setUser($this->teacher);
667 $indfirstpageteacher = $this->getDataGenerator()->get_plugin_generator('mod_wiki')->create_first_page($indwiki);
668
669 // Check that teacher can get his pages.
670 $expectedteacherpage = (array) $indfirstpageteacher;
671 $expectedteacherpage['caneditpage'] = true;
672 $expectedteacherpage['firstpage'] = true;
673 $expectedteacherpage['contentformat'] = 1;
674 $expectedpages = array($expectedteacherpage);
675
676 $result = mod_wiki_external::get_subwiki_pages($indwiki->id, 0, $this->teacher->id);
677 $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result);
678 $this->assertEquals($expectedpages, $result['pages']);
679
680 // Check that the teacher can see the student's pages.
681 $expectedstudentpage = (array) $indfirstpagestudent;
682 $expectedstudentpage['caneditpage'] = true;
683 $expectedstudentpage['firstpage'] = true;
684 $expectedstudentpage['contentformat'] = 1;
685 $expectedpages = array($expectedstudentpage);
686
687 $result = mod_wiki_external::get_subwiki_pages($indwiki->id, 0, $this->student->id);
688 $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result);
689 $this->assertEquals($expectedpages, $result['pages']);
690
691 // Now check that student can get his pages.
692 $this->setUser($this->student);
693
694 $result = mod_wiki_external::get_subwiki_pages($indwiki->id, 0, $this->student->id);
695 $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result);
696 $this->assertEquals($expectedpages, $result['pages']);
697
698 // Check that not using userid uses current user.
699 $result = mod_wiki_external::get_subwiki_pages($indwiki->id, 0);
700 $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result);
701 $this->assertEquals($expectedpages, $result['pages']);
702 }
703
704 /**
705 * Test get_subwiki_pages with groups and collaborative wikis.
706 */
707 public function test_get_subwiki_pages_separate_groups_collaborative() {
708
709 // Create testing data.
710 $this->create_collaborative_wikis_with_groups();
711
712 $this->setUser($this->student);
713
714 // Try to get pages from a valid group in separate groups wiki.
715
716 $expectedpage = (array) $this->fpsepg1;
717 $expectedpage['caneditpage'] = true; // User belongs to group and has 'mod/wiki:editpage' capability.
718 $expectedpage['firstpage'] = true;
719 $expectedpage['contentformat'] = 1;
720 $expectedpages = array($expectedpage);
721
722 $result = mod_wiki_external::get_subwiki_pages($this->wikisep->id, $this->group1->id);
723 $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result);
724 $this->assertEquals($expectedpages, $result['pages']);
725
726 // Let's check that not using groupid returns the same result (current group).
727 $result = mod_wiki_external::get_subwiki_pages($this->wikisep->id);
728 $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result);
729 $this->assertEquals($expectedpages, $result['pages']);
730
731 // Check that teacher can view a group pages without belonging to it.
732 $this->setUser($this->teacher);
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 // Check that teacher can get the pages from all participants.
738 $expectedpage = (array) $this->fpsepall;
739 $expectedpage['caneditpage'] = true;
740 $expectedpage['firstpage'] = true;
741 $expectedpage['contentformat'] = 1;
742 $expectedpages = array($expectedpage);
743
744 $result = mod_wiki_external::get_subwiki_pages($this->wikisep->id, 0);
745 $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result);
746 $this->assertEquals($expectedpages, $result['pages']);
747 }
748
749 /**
750 * Test get_subwiki_pages with groups and collaborative wikis.
751 */
752 public function test_get_subwiki_pages_visible_groups_collaborative() {
753
754 // Create testing data.
755 $this->create_collaborative_wikis_with_groups();
756
757 $this->setUser($this->student);
758
759 // Try to get pages from a valid group in visible groups wiki.
760
761 $expectedpage = (array) $this->fpvisg1;
762 $expectedpage['caneditpage'] = true; // User belongs to group and has 'mod/wiki:editpage' capability.
763 $expectedpage['firstpage'] = true;
764 $expectedpage['contentformat'] = 1;
765 $expectedpages = array($expectedpage);
766
767 $result = mod_wiki_external::get_subwiki_pages($this->wikivis->id, $this->group1->id);
768 $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result);
769 $this->assertEquals($expectedpages, $result['pages']);
770
771 // Check that with visible groups a student can get the pages of groups he doesn't belong to.
772 $expectedpage = (array) $this->fpvisg2;
773 $expectedpage['caneditpage'] = false; // User doesn't belong to group so he can't edit the page.
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->group2->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 all participants.
783 $expectedpage = (array) $this->fpvisall;
784 $expectedpage['caneditpage'] = false;
785 $expectedpage['firstpage'] = true;
786 $expectedpage['contentformat'] = 1;
787 $expectedpages = array($expectedpage);
788
789 $result = mod_wiki_external::get_subwiki_pages($this->wikivis->id, 0);
790 $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result);
791 $this->assertEquals($expectedpages, $result['pages']);
792 }
793
794 /**
795 * Test get_subwiki_pages with groups and individual wikis.
796 */
797 public function test_get_subwiki_pages_separate_groups_individual() {
798
799 // Create testing data.
800 $this->create_individual_wikis_with_groups();
801
802 $this->setUser($this->student);
803
804 // Check that student can retrieve his pages from separate wiki.
805 $expectedpage = (array) $this->fpsepg1indstu;
806 $expectedpage['caneditpage'] = true;
807 $expectedpage['firstpage'] = true;
808 $expectedpage['contentformat'] = 1;
809 $expectedpages = array($expectedpage);
810
811 $result = mod_wiki_external::get_subwiki_pages($this->wikisepind->id, $this->group1->id, $this->student->id);
812 $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result);
813 $this->assertEquals($expectedpages, $result['pages']);
814
815 // Check that not using userid uses current user.
816 $result = mod_wiki_external::get_subwiki_pages($this->wikisepind->id, $this->group1->id);
817 $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result);
818 $this->assertEquals($expectedpages, $result['pages']);
819
820 // Check that the teacher can see the student pages.
821 $this->setUser($this->teacher);
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 a student can see pages from another user that belongs to his groups.
827 $this->setUser($this->student);
828 $expectedpage = (array) $this->fpsepg1indstu2;
829 $expectedpage['caneditpage'] = false;
830 $expectedpage['firstpage'] = true;
831 $expectedpage['contentformat'] = 1;
832 $expectedpages = array($expectedpage);
833
834 $result = mod_wiki_external::get_subwiki_pages($this->wikisepind->id, $this->group1->id, $this->student2->id);
835 $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result);
836 $this->assertEquals($expectedpages, $result['pages']);
837 }
838
839 /**
840 * Test get_subwiki_pages with groups and individual wikis.
841 */
842 public function test_get_subwiki_pages_visible_groups_individual() {
843
844 // Create testing data.
845 $this->create_individual_wikis_with_groups();
846
847 $this->setUser($this->student);
848
849 // Check that student can retrieve his pages from visible wiki.
850 $expectedpage = (array) $this->fpvisg1indstu;
851 $expectedpage['caneditpage'] = true;
852 $expectedpage['firstpage'] = true;
853 $expectedpage['contentformat'] = 1;
854 $expectedpages = array($expectedpage);
855
856 $result = mod_wiki_external::get_subwiki_pages($this->wikivisind->id, $this->group1->id, $this->student->id);
857 $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result);
858 $this->assertEquals($expectedpages, $result['pages']);
859
860 // Check that student can see teacher pages in visible groups, even if the user doesn't belong to the group.
861 $expectedpage = (array) $this->fpvisg2indt;
862 $expectedpage['caneditpage'] = false;
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->group2->id, $this->teacher->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 with visible groups a student can get the pages of all participants.
872 $expectedpage = (array) $this->fpvisallindt;
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, 0, $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
9a4c5222
DP
883 /**
884 * Test get_page_contents using an invalid pageid.
885 */
886 public function test_get_page_contents_invalid_pageid() {
887 $this->setExpectedException('moodle_exception');
888 mod_wiki_external::get_page_contents(0);
889 }
890
891 /**
892 * Test get_page_contents using a user not enrolled in the course.
893 */
894 public function test_get_page_contents_unenrolled_user() {
895 // Create and use the user.
896 $usernotenrolled = self::getDataGenerator()->create_user();
897 $this->setUser($usernotenrolled);
898
899 $this->setExpectedException('require_login_exception');
900 mod_wiki_external::get_page_contents($this->firstpage->id);
901 }
902
903 /**
904 * Test get_page_contents using a hidden wiki as student.
905 */
906 public function test_get_page_contents_hidden_wiki_as_student() {
907 // Create a hidden wiki and try to get a page contents.
908 $hiddenwiki = $this->getDataGenerator()->create_module('wiki',
909 array('course' => $this->course->id, 'visible' => false));
910 $hiddenpage = $this->getDataGenerator()->get_plugin_generator('mod_wiki')->create_page($hiddenwiki);
911
912 $this->setUser($this->student);
913 $this->setExpectedException('require_login_exception');
914 mod_wiki_external::get_page_contents($hiddenpage->id);
915 }
916
917 /**
918 * Test get_page_contents without the viewpage capability.
919 */
920 public function test_get_page_contents_without_viewpage_capability() {
921 // Prohibit capability = mod/wiki:viewpage on the course for students.
922 $contextcourse = context_course::instance($this->course->id);
923 assign_capability('mod/wiki:viewpage', CAP_PROHIBIT, $this->studentrole->id, $contextcourse->id);
924 accesslib_clear_all_caches_for_unit_testing();
925
926 $this->setUser($this->student);
927 $this->setExpectedException('moodle_exception');
928 mod_wiki_external::get_page_contents($this->firstpage->id);
929 }
930
931 /**
932 * Test get_page_contents, check that a student can't get a page from another group when
933 * using separate groups.
934 */
935 public function test_get_page_contents_separate_groups_student_see_other_group() {
936 // Create testing data.
937 $this->create_individual_wikis_with_groups();
938
939 $this->setUser($this->student);
940 $this->setExpectedException('moodle_exception');
941 mod_wiki_external::get_page_contents($this->fpsepg2indt->id);
942 }
943
944 /**
945 * Test get_page_contents without groups. We won't test all the possible cases because that's already
946 * done in the tests for get_subwiki_pages.
947 */
948 public function test_get_page_contents() {
949
950 // Test user with full capabilities.
951 $this->setUser($this->student);
952
953 // Set expected result: first page.
954 $expectedpage = array(
955 'id' => $this->firstpage->id,
956 'wikiid' => $this->wiki->id,
957 'subwikiid' => $this->firstpage->subwikiid,
958 'groupid' => 0, // No groups.
959 'userid' => 0, // Collaborative.
960 'title' => $this->firstpage->title,
961 'cachedcontent' => $this->firstpage->cachedcontent,
962 'contentformat' => 1,
963 'caneditpage' => true
964 );
965
966 $result = mod_wiki_external::get_page_contents($this->firstpage->id);
967 $result = external_api::clean_returnvalue(mod_wiki_external::get_page_contents_returns(), $result);
968 $this->assertEquals($expectedpage, $result['page']);
969
970 // Add a new page to the wiki and test with it.
971 $newpage = $this->getDataGenerator()->get_plugin_generator('mod_wiki')->create_page($this->wiki);
972
973 $expectedpage['id'] = $newpage->id;
974 $expectedpage['title'] = $newpage->title;
975 $expectedpage['cachedcontent'] = $newpage->cachedcontent;
976
977 $result = mod_wiki_external::get_page_contents($newpage->id);
978 $result = external_api::clean_returnvalue(mod_wiki_external::get_page_contents_returns(), $result);
979 $this->assertEquals($expectedpage, $result['page']);
980 }
981
982 /**
983 * Test get_page_contents with groups. We won't test all the possible cases because that's already
984 * done in the tests for get_subwiki_pages.
985 */
986 public function test_get_page_contents_with_groups() {
987
988 // Create testing data.
989 $this->create_individual_wikis_with_groups();
990
991 // Try to get page from a valid group in separate groups wiki.
992 $this->setUser($this->student);
993
994 $expectedfpsepg1indstu = array(
995 'id' => $this->fpsepg1indstu->id,
996 'wikiid' => $this->wikisepind->id,
997 'subwikiid' => $this->fpsepg1indstu->subwikiid,
998 'groupid' => $this->group1->id,
999 'userid' => $this->student->id,
1000 'title' => $this->fpsepg1indstu->title,
1001 'cachedcontent' => $this->fpsepg1indstu->cachedcontent,
1002 'contentformat' => 1,
1003 'caneditpage' => true
1004 );
1005
1006 $result = mod_wiki_external::get_page_contents($this->fpsepg1indstu->id);
1007 $result = external_api::clean_returnvalue(mod_wiki_external::get_page_contents_returns(), $result);
1008 $this->assertEquals($expectedfpsepg1indstu, $result['page']);
1009
1010 // Check that teacher can view a group pages without belonging to it.
1011 $this->setUser($this->teacher);
1012 $result = mod_wiki_external::get_page_contents($this->fpsepg1indstu->id);
1013 $result = external_api::clean_returnvalue(mod_wiki_external::get_page_contents_returns(), $result);
1014 $this->assertEquals($expectedfpsepg1indstu, $result['page']);
1015 }
1016
7ef81de8
DP
1017 /**
1018 * Test get_subwiki_files using a wiki without files.
1019 */
1020 public function test_get_subwiki_files_no_files() {
1021 $result = mod_wiki_external::get_subwiki_files($this->wiki->id);
1022 $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_files_returns(), $result);
1023 $this->assertCount(0, $result['files']);
1024 $this->assertCount(0, $result['warnings']);
1025 }
1026
1027 /**
1028 * Test get_subwiki_files, check that a student can't get files from another group's subwiki when
1029 * using separate groups.
1030 */
1031 public function test_get_subwiki_files_separate_groups_student_see_other_group() {
1032 // Create testing data.
1033 $this->create_collaborative_wikis_with_groups();
1034
1035 $this->setUser($this->student);
1036 $this->setExpectedException('moodle_exception');
1037 mod_wiki_external::get_subwiki_files($this->wikisep->id, $this->group2->id);
1038 }
1039
1040 /**
1041 * Test get_subwiki_files using a collaborative wiki without groups.
1042 */
1043 public function test_get_subwiki_files_collaborative_no_groups() {
1044 $this->setUser($this->student);
1045
1046 // Add a file as subwiki attachment.
1047 $fs = get_file_storage();
1048 $file = array('component' => 'mod_wiki', 'filearea' => 'attachments',
1049 'contextid' => $this->context->id, 'itemid' => $this->firstpage->subwikiid,
1050 'filename' => 'image.jpg', 'filepath' => '/', 'timemodified' => time());
1051 $content = 'IMAGE';
1052 $fs->create_file_from_string($file, $content);
1053
1054 $expectedfile = array(
1055 'filename' => $file['filename'],
1056 'filepath' => $file['filepath'],
1057 'mimetype' => 'image/jpeg',
1058 'filesize' => strlen($content),
1059 'timemodified' => $file['timemodified'],
1060 'fileurl' => moodle_url::make_webservice_pluginfile_url($file['contextid'], $file['component'],
1061 $file['filearea'], $file['itemid'], $file['filepath'], $file['filename']),
1062 );
1063
1064 // Call the WS and check that it returns this file.
1065 $result = mod_wiki_external::get_subwiki_files($this->wiki->id);
1066 $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_files_returns(), $result);
1067 $this->assertCount(1, $result['files']);
1068 $this->assertEquals($expectedfile, $result['files'][0]);
1069
1070 // Now add another file to the same subwiki.
1071 $file['filename'] = 'Another image.jpg';
1072 $file['timemodified'] = time();
1073 $content = 'ANOTHER IMAGE';
1074 $fs->create_file_from_string($file, $content);
1075
1076 $expectedfile['filename'] = $file['filename'];
1077 $expectedfile['timemodified'] = $file['timemodified'];
1078 $expectedfile['filesize'] = strlen($content);
1079 $expectedfile['fileurl'] = moodle_url::make_webservice_pluginfile_url($file['contextid'], $file['component'],
1080 $file['filearea'], $file['itemid'], $file['filepath'], $file['filename']);
1081
1082 // Call the WS and check that it returns both files file.
1083 $result = mod_wiki_external::get_subwiki_files($this->wiki->id);
1084 $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_files_returns(), $result);
1085 $this->assertCount(2, $result['files']);
1086 // The new file is returned first because they're returned in alphabetical order.
1087 $this->assertEquals($expectedfile, $result['files'][0]);
1088 }
1089
1090 /**
1091 * Test get_subwiki_files using an individual wiki with visible groups.
1092 */
1093 public function test_get_subwiki_files_visible_groups_individual() {
1094 // Create testing data.
1095 $this->create_individual_wikis_with_groups();
1096
1097 $this->setUser($this->student);
1098
1099 // Add a file as subwiki attachment in the student group 1 subwiki.
1100 $fs = get_file_storage();
1101 $contextwiki = context_module::instance($this->wikivisind->cmid);
1102 $file = array('component' => 'mod_wiki', 'filearea' => 'attachments',
1103 'contextid' => $contextwiki->id, 'itemid' => $this->fpvisg1indstu->subwikiid,
1104 'filename' => 'image.jpg', 'filepath' => '/', 'timemodified' => time());
1105 $content = 'IMAGE';
1106 $fs->create_file_from_string($file, $content);
1107
1108 $expectedfile = array(
1109 'filename' => $file['filename'],
1110 'filepath' => $file['filepath'],
1111 'mimetype' => 'image/jpeg',
1112 'filesize' => strlen($content),
1113 'timemodified' => $file['timemodified'],
1114 'fileurl' => moodle_url::make_webservice_pluginfile_url($file['contextid'], $file['component'],
1115 $file['filearea'], $file['itemid'], $file['filepath'], $file['filename']),
1116 );
1117
1118 // Call the WS and check that it returns this file.
1119 $result = mod_wiki_external::get_subwiki_files($this->wikivisind->id, $this->group1->id);
1120 $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_files_returns(), $result);
1121 $this->assertCount(1, $result['files']);
1122 $this->assertEquals($expectedfile, $result['files'][0]);
1123
1124 // Now check that a teacher can see it too.
1125 $this->setUser($this->teacher);
1126 $result = mod_wiki_external::get_subwiki_files($this->wikivisind->id, $this->group1->id, $this->student->id);
1127 $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_files_returns(), $result);
1128 $this->assertCount(1, $result['files']);
1129 $this->assertEquals($expectedfile, $result['files'][0]);
1130 }
1131
20dca621
PFO
1132
1133 /**
1134 * Test get_page_for_editing. We won't test all the possible cases because that's already
1135 * done in the tests for wiki_parser_proxy::get_section.
1136 */
1137 public function test_get_page_for_editing() {
1138
1139 $this->create_individual_wikis_with_groups();
1140
1141 $sectioncontent = '<h1>Title1</h1>Text inside section';
1142 $pagecontent = $sectioncontent.'<h1>Title2</h1>Text inside section';
1143 $newpage = $this->getDataGenerator()->get_plugin_generator('mod_wiki')->create_page(
1144 $this->wiki, array('content' => $pagecontent));
1145
1146 // Test user with full capabilities.
1147 $this->setUser($this->student);
1148
1149 // Set expected result: Full Page content.
1150 $expected = array(
1151 'content' => $pagecontent,
1152 'contentformat' => 'html',
1153 'version' => '1'
1154 );
1155
1156 $result = mod_wiki_external::get_page_for_editing($newpage->id);
1157 $result = external_api::clean_returnvalue(mod_wiki_external::get_page_for_editing_returns(), $result);
1158 $this->assertEquals($expected, $result['pagesection']);
1159
1160 // Set expected result: Section Page content.
1161 $expected = array(
1162 'content' => $sectioncontent,
1163 'contentformat' => 'html',
1164 'version' => '1'
1165 );
1166
1167 $result = mod_wiki_external::get_page_for_editing($newpage->id, 'Title1');
1168 $result = external_api::clean_returnvalue(mod_wiki_external::get_page_for_editing_returns(), $result);
1169 $this->assertEquals($expected, $result['pagesection']);
1170 }
1171
a6b6f0d2
PFO
1172 /**
1173 * Test new_page. We won't test all the possible cases because that's already
1174 * done in the tests for wiki_create_page.
1175 */
1176 public function test_new_page() {
1177
1178 $this->create_individual_wikis_with_groups();
1179
1180 $sectioncontent = '<h1>Title1</h1>Text inside section';
1181 $pagecontent = $sectioncontent.'<h1>Title2</h1>Text inside section';
1182 $pagetitle = 'Page Title';
1183
1184 // Test user with full capabilities.
1185 $this->setUser($this->student);
1186
1187 // Test on existing subwiki.
1188 $result = mod_wiki_external::new_page($pagetitle, $pagecontent, 'html', $this->fpsepg1indstu->subwikiid);
1189 $result = external_api::clean_returnvalue(mod_wiki_external::new_page_returns(), $result);
1190 $this->assertInternalType('int', $result['pageid']);
1191
1192 $version = wiki_get_current_version($result['pageid']);
1193 $this->assertEquals($pagecontent, $version->content);
1194 $this->assertEquals('html', $version->contentformat);
1195
1196 $page = wiki_get_page($result['pageid']);
1197 $this->assertEquals($pagetitle, $page->title);
1198
1199 // Test existing page creation.
1200 try {
1201 mod_wiki_external::new_page($pagetitle, $pagecontent, 'html', $this->fpsepg1indstu->subwikiid);
1202 $this->fail('Exception expected due to creation of an existing page.');
1203 } catch (moodle_exception $e) {
1204 $this->assertEquals('pageexists', $e->errorcode);
1205 }
1206
1207 // Test on non existing subwiki. Add student to group2 to have a new subwiki to be created.
1208 $this->getDataGenerator()->create_group_member(array('userid' => $this->student->id, 'groupid' => $this->group2->id));
1209 $result = mod_wiki_external::new_page($pagetitle, $pagecontent, 'html', null, $this->wikisepind->id, $this->student->id,
1210 $this->group2->id);
1211 $result = external_api::clean_returnvalue(mod_wiki_external::new_page_returns(), $result);
1212 $this->assertInternalType('int', $result['pageid']);
1213
1214 $version = wiki_get_current_version($result['pageid']);
1215 $this->assertEquals($pagecontent, $version->content);
1216 $this->assertEquals('html', $version->contentformat);
1217
1218 $page = wiki_get_page($result['pageid']);
1219 $this->assertEquals($pagetitle, $page->title);
1220
1221 $subwiki = wiki_get_subwiki($page->subwikiid);
1222 $expected = new StdClass();
1223 $expected->id = $subwiki->id;
1224 $expected->wikiid = $this->wikisepind->id;
1225 $expected->groupid = $this->group2->id;
1226 $expected->userid = $this->student->id;
1227 $this->assertEquals($expected, $subwiki);
1228
1229 // Check page creation for a user not in course.
1230 $this->studentnotincourse = self::getDataGenerator()->create_user();
1231 $this->anothercourse = $this->getDataGenerator()->create_course();
1232 $this->groupnotincourse = $this->getDataGenerator()->create_group(array('courseid' => $this->anothercourse->id));
1233
1234 try {
1235 mod_wiki_external::new_page($pagetitle, $pagecontent, 'html', null, $this->wikisepind->id,
1236 $this->studentnotincourse->id, $this->groupnotincourse->id);
1237 $this->fail('Exception expected due to creation of an invalid subwiki creation.');
1238 } catch (moodle_exception $e) {
1239 $this->assertEquals('cannoteditpage', $e->errorcode);
1240 }
1241
1242 }
1243
cf16f6ed
PFO
1244 /**
1245 * Test edit_page. We won't test all the possible cases because that's already
1246 * done in the tests for wiki_save_section / wiki_save_page.
1247 */
1248 public function test_edit_page() {
1249
1250 $this->create_individual_wikis_with_groups();
1251
1252 // Test user with full capabilities.
1253 $this->setUser($this->student);
1254
1255 $newpage = $this->getDataGenerator()->get_plugin_generator('mod_wiki')->create_page($this->wikisepind,
1256 array('group' => $this->group1->id, 'content' => 'Test'));
1257
1258 // Test edit whole page.
1259 $sectioncontent = '<h1>Title1</h1>Text inside section';
1260 $newpagecontent = $sectioncontent.'<h1>Title2</h1>Text inside section';
1261
1262 $result = mod_wiki_external::edit_page($newpage->id, $newpagecontent);
1263 $result = external_api::clean_returnvalue(mod_wiki_external::edit_page_returns(), $result);
1264 $this->assertInternalType('int', $result['pageid']);
1265
1266 $version = wiki_get_current_version($result['pageid']);
1267 $this->assertEquals($newpagecontent, $version->content);
1268
1269 // Test edit section.
1270 $newsectioncontent = '<h1>Title2</h1>New test2';
1271 $section = 'Title2';
1272
1273 $result = mod_wiki_external::edit_page($newpage->id, $newsectioncontent, $section);
1274 $result = external_api::clean_returnvalue(mod_wiki_external::edit_page_returns(), $result);
1275 $this->assertInternalType('int', $result['pageid']);
1276
1277 $expected = $sectioncontent . $newsectioncontent;
1278
1279 $version = wiki_get_current_version($result['pageid']);
1280 $this->assertEquals($expected, $version->content);
1281
1282 // Test locked section.
1283 $newsectioncontent = '<h1>Title2</h1>New test2';
1284 $section = 'Title2';
1285
1286 try {
1287 // Using user 1 to avoid other users to edit.
1288 wiki_set_lock($newpage->id, 1, $section, true);
1289 mod_wiki_external::edit_page($newpage->id, $newsectioncontent, $section);
1290 $this->fail('Exception expected due to locked section');
1291 } catch (moodle_exception $e) {
1292 $this->assertEquals('pageislocked', $e->errorcode);
1293 }
1294
1295 // Test edit non existing section.
1296 $newsectioncontent = '<h1>Title3</h1>New test3';
1297 $section = 'Title3';
1298
1299 try {
1300 mod_wiki_external::edit_page($newpage->id, $newsectioncontent, $section);
1301 $this->fail('Exception expected due to non existing section in the page.');
1302 } catch (moodle_exception $e) {
1303 $this->assertEquals('invalidsection', $e->errorcode);
1304 }
1305
1306 }
1307
4a5acec2 1308}