2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Wiki module external functions tests.
22 * @copyright 2015 Dani Palou <dani@moodle.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 defined('MOODLE_INTERNAL') || die();
31 require_once($CFG->dirroot . '/webservice/tests/helpers.php');
32 require_once($CFG->dirroot . '/mod/wiki/lib.php');
35 * Wiki module external functions tests
39 * @copyright 2015 Dani Palou <dani@moodle.com>
40 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
43 class mod_wiki_external_testcase extends externallib_advanced_testcase {
46 * Set up for every test
48 public function setUp() {
50 $this->resetAfterTest();
51 $this->setAdminUser();
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);
60 $this->student = self::getDataGenerator()->create_user();
61 $this->student2 = self::getDataGenerator()->create_user();
62 $this->teacher = self::getDataGenerator()->create_user();
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');
68 $this->getDataGenerator()->enrol_user($this->student2->id, $this->course->id, $this->studentrole->id, 'manual');
69 $this->getDataGenerator()->enrol_user($this->teacher->id, $this->course->id, $this->teacherrole->id, 'manual');
71 // Create first pages.
72 $this->firstpage = $this->getDataGenerator()->get_plugin_generator('mod_wiki')->create_first_page($this->wiki);
76 * Create two collaborative wikis (separate/visible groups), 2 groups and a first page for each wiki and group.
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));
85 if (!isset($this->group2)) {
86 $this->group2 = $this->getDataGenerator()->create_group(array('courseid' => $this->course->id));
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));
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.
106 * Create two individual wikis (separate/visible groups), 2 groups and a first page for each wiki and group.
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));
115 if (!isset($this->group2)) {
116 $this->group2 = $this->getDataGenerator()->create_group(array('courseid' => $this->course->id));
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'));
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.
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));
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));
146 * Test get wikis by courses
148 public function test_mod_wiki_get_wikis_by_courses() {
150 // Create additional course.
151 $course2 = self::getDataGenerator()->create_course();
154 $record = new stdClass();
155 $record->course = $course2->id;
156 $wiki2 = self::getDataGenerator()->create_module('wiki', $record);
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;
167 $enrol->enrol_user($instance2, $this->student->id, $this->studentrole->id);
169 self::setUser($this->student);
171 $returndescription = mod_wiki_external::get_wikis_by_courses_returns();
173 // Create what we expect to be returned when querying the two courses.
174 // First for the student user.
175 $expectedfields = array('id', 'coursemodule', 'course', 'name', 'intro', 'introformat', 'introfiles', 'firstpagetitle',
176 'wikimode', 'defaultformat', 'forceformat', 'editbegin', 'editend', 'section', 'visible',
177 'groupmode', 'groupingid');
179 // Add expected coursemodule and data.
180 $wiki1 = $this->wiki;
181 $wiki1->coursemodule = $wiki1->cmid;
182 $wiki1->introformat = 1;
184 $wiki1->visible = true;
185 $wiki1->groupmode = 0;
186 $wiki1->groupingid = 0;
187 $wiki1->introfiles = [];
189 $wiki2->coursemodule = $wiki2->cmid;
190 $wiki2->introformat = 1;
192 $wiki2->visible = true;
193 $wiki2->groupmode = 0;
194 $wiki2->groupingid = 0;
195 $wiki2->introfiles = [];
197 foreach ($expectedfields as $field) {
198 $expected1[$field] = $wiki1->{$field};
199 $expected2[$field] = $wiki2->{$field};
201 // Users can create pages by default.
202 $expected1['cancreatepages'] = true;
203 $expected2['cancreatepages'] = true;
205 $expectedwikis = array($expected2, $expected1);
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);
211 $this->assertEquals($expectedwikis, $result['wikis']);
212 $this->assertCount(0, $result['warnings']);
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']);
220 // Unenrol user from second course and alter expected wikis.
221 $enrol->unenrol_user($instance2, $this->student->id);
222 array_shift($expectedwikis);
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']);
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']);
235 // Now, try as a teacher for getting all the additional fields.
236 self::setUser($this->teacher);
238 $additionalfields = array('timecreated', 'timemodified');
240 foreach ($additionalfields as $field) {
241 $expectedwikis[0][$field] = $wiki1->{$field};
244 $result = mod_wiki_external::get_wikis_by_courses();
245 $result = external_api::clean_returnvalue($returndescription, $result);
246 $this->assertEquals($expectedwikis, $result['wikis']);
248 // Admin also should get all the information.
249 self::setAdminUser();
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']);
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();
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']));
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();
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']);
279 public function test_view_wiki() {
281 // Test invalid instance id.
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);
289 // Test not-enrolled user.
290 $usernotenrolled = self::getDataGenerator()->create_user();
291 $this->setUser($usernotenrolled);
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);
299 // Test user with full capabilities.
300 $this->setUser($this->student);
302 // Trigger and capture the event.
303 $sink = $this->redirectEvents();
305 $result = mod_wiki_external::view_wiki($this->wiki->id);
306 $result = external_api::clean_returnvalue(mod_wiki_external::view_wiki_returns(), $result);
308 $events = $sink->get_events();
309 $this->assertCount(1, $events);
310 $event = array_shift($events);
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());
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();
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);
337 public function test_view_page() {
339 // Test invalid page id.
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);
347 // Test not-enrolled user.
348 $usernotenrolled = self::getDataGenerator()->create_user();
349 $this->setUser($usernotenrolled);
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);
357 // Test user with full capabilities.
358 $this->setUser($this->student);
360 // Trigger and capture the event.
361 $sink = $this->redirectEvents();
363 $result = mod_wiki_external::view_page($this->firstpage->id);
364 $result = external_api::clean_returnvalue(mod_wiki_external::view_page_returns(), $result);
366 $events = $sink->get_events();
367 $this->assertCount(1, $events);
368 $event = array_shift($events);
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());
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();
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);
395 public function test_get_subwikis() {
397 // Test invalid wiki id.
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);
405 // Test not-enrolled user.
406 $usernotenrolled = self::getDataGenerator()->create_user();
407 $this->setUser($usernotenrolled);
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);
415 // Test user with full capabilities.
416 $this->setUser($this->student);
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,
428 $expectedsubwikis[] = $expectedsubwiki;
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']);
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();
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);
450 * Test get_subwiki_pages using an invalid wiki instance.
452 * @expectedException moodle_exception
454 public function test_get_subwiki_pages_invalid_instance() {
455 mod_wiki_external::get_subwiki_pages(0);
459 * Test get_subwiki_pages using a user not enrolled in the course.
461 * @expectedException require_login_exception
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);
468 mod_wiki_external::get_subwiki_pages($this->wiki->id);
472 * Test get_subwiki_pages using a hidden wiki as student.
474 * @expectedException require_login_exception
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));
481 $this->setUser($this->student);
482 mod_wiki_external::get_subwiki_pages($hiddenwiki->id);
486 * Test get_subwiki_pages without the viewpage capability.
488 * @expectedException moodle_exception
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();
496 $this->setUser($this->student);
497 mod_wiki_external::get_subwiki_pages($this->wiki->id);
501 * Test get_subwiki_pages using an invalid userid.
503 * @expectedException moodle_exception
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'));
510 mod_wiki_external::get_subwiki_pages($indwiki->id, 0, -10);
514 * Test get_subwiki_pages using an invalid groupid.
516 * @expectedException moodle_exception
518 public function test_get_subwiki_pages_invalid_groupid() {
519 // Create testing data.
520 $this->create_collaborative_wikis_with_groups();
522 mod_wiki_external::get_subwiki_pages($this->wikisep->id, -111);
526 * Test get_subwiki_pages, check that a student can't see another user pages in an individual wiki without groups.
528 * @expectedException moodle_exception
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'));
535 $this->setUser($this->student);
536 mod_wiki_external::get_subwiki_pages($indwiki->id, 0, $this->teacher->id);
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.
543 * @expectedException moodle_exception
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();
549 $this->setUser($this->student);
550 mod_wiki_external::get_subwiki_pages($this->wikisep->id, $this->group2->id);
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.
557 * @expectedException moodle_exception
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();
563 $this->setUser($this->student);
564 mod_wiki_external::get_subwiki_pages($this->wikisepind->id, $this->group2->id, $this->teacher->id);
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.
571 * @expectedException moodle_exception
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();
577 $this->setUser($this->student);
578 mod_wiki_external::get_subwiki_pages($this->wikisep->id, 0);
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.
585 * @expectedException moodle_exception
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();
591 $this->setUser($this->student);
592 mod_wiki_external::get_subwiki_pages($this->wikisepind->id, 0, $this->teacher->id);
596 * Test get_subwiki_pages without groups and collaborative wiki.
598 public function test_get_subwiki_pages_collaborative() {
600 // Test user with full capabilities.
601 $this->setUser($this->student);
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;
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']);
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']);
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']);
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'));
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.
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']);
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']);
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');
650 $expectedpages[$i]['contentsize'] = strlen($expectedpages[$i]['cachedcontent']);
652 unset($expectedpages[$i]['cachedcontent']);
653 unset($expectedpages[$i]['contentformat']);
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']);
661 * Test get_subwiki_pages without groups.
663 public function test_get_subwiki_pages_individual() {
665 // Create an individual wiki to test userid param.
666 $indwiki = $this->getDataGenerator()->create_module('wiki',
667 array('course' => $this->course->id, 'wikimode' => 'individual'));
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']);
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);
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);
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']);
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);
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']);
702 // Now check that student can get his pages.
703 $this->setUser($this->student);
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']);
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']);
716 * Test get_subwiki_pages with groups and collaborative wikis.
718 public function test_get_subwiki_pages_separate_groups_collaborative() {
720 // Create testing data.
721 $this->create_collaborative_wikis_with_groups();
723 $this->setUser($this->student);
725 // Try to get pages from a valid group in separate groups wiki.
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);
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']);
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']);
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']);
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);
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']);
761 * Test get_subwiki_pages with groups and collaborative wikis.
763 public function test_get_subwiki_pages_visible_groups_collaborative() {
765 // Create testing data.
766 $this->create_collaborative_wikis_with_groups();
768 $this->setUser($this->student);
770 // Try to get pages from a valid group in visible groups wiki.
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);
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']);
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);
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']);
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);
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']);
806 * Test get_subwiki_pages with groups and individual wikis.
808 public function test_get_subwiki_pages_separate_groups_individual() {
810 // Create testing data.
811 $this->create_individual_wikis_with_groups();
813 $this->setUser($this->student);
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);
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']);
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']);
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']);
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);
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']);
851 * Test get_subwiki_pages with groups and individual wikis.
853 public function test_get_subwiki_pages_visible_groups_individual() {
855 // Create testing data.
856 $this->create_individual_wikis_with_groups();
858 $this->setUser($this->student);
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);
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']);
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);
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']);
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);
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']);
895 * Test get_page_contents using an invalid pageid.
897 * @expectedException moodle_exception
899 public function test_get_page_contents_invalid_pageid() {
900 mod_wiki_external::get_page_contents(0);
904 * Test get_page_contents using a user not enrolled in the course.
906 * @expectedException require_login_exception
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);
913 mod_wiki_external::get_page_contents($this->firstpage->id);
917 * Test get_page_contents using a hidden wiki as student.
919 * @expectedException require_login_exception
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);
927 $this->setUser($this->student);
928 mod_wiki_external::get_page_contents($hiddenpage->id);
932 * Test get_page_contents without the viewpage capability.
934 * @expectedException moodle_exception
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();
942 $this->setUser($this->student);
943 mod_wiki_external::get_page_contents($this->firstpage->id);
947 * Test get_page_contents, check that a student can't get a page from another group when
948 * using separate groups.
950 * @expectedException moodle_exception
952 public function test_get_page_contents_separate_groups_student_see_other_group() {
953 // Create testing data.
954 $this->create_individual_wikis_with_groups();
956 $this->setUser($this->student);
957 mod_wiki_external::get_page_contents($this->fpsepg2indt->id);
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.
964 public function test_get_page_contents() {
966 // Test user with full capabilities.
967 $this->setUser($this->student);
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,
979 'caneditpage' => true,
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']);
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);
990 $expectedpage['id'] = $newpage->id;
991 $expectedpage['title'] = $newpage->title;
992 $expectedpage['cachedcontent'] = $newpage->cachedcontent;
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']);
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.
1003 public function test_get_page_contents_with_groups() {
1005 // Create testing data.
1006 $this->create_individual_wikis_with_groups();
1008 // Try to get page from a valid group in separate groups wiki.
1009 $this->setUser($this->student);
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,
1020 'caneditpage' => true,
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']);
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']);
1036 * Test get_subwiki_files using a wiki without files.
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']);
1046 * Test get_subwiki_files, check that a student can't get files from another group's subwiki when
1047 * using separate groups.
1049 * @expectedException moodle_exception
1051 public function test_get_subwiki_files_separate_groups_student_see_other_group() {
1052 // Create testing data.
1053 $this->create_collaborative_wikis_with_groups();
1055 $this->setUser($this->student);
1056 mod_wiki_external::get_subwiki_files($this->wikisep->id, $this->group2->id);
1060 * Test get_subwiki_files using a collaborative wiki without groups.
1062 public function test_get_subwiki_files_collaborative_no_groups() {
1063 $this->setUser($this->student);
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());
1071 $fs->create_file_from_string($file, $content);
1073 $expectedfile = array(
1074 'filename' => $file['filename'],
1075 'filepath' => $file['filepath'],
1076 'mimetype' => 'image/jpeg',
1077 'filesize' => strlen($content),
1078 'timemodified' => $file['timemodified'],
1079 'fileurl' => moodle_url::make_webservice_pluginfile_url($file['contextid'], $file['component'],
1080 $file['filearea'], $file['itemid'], $file['filepath'], $file['filename']),
1083 // Call the WS and check that it returns this file.
1084 $result = mod_wiki_external::get_subwiki_files($this->wiki->id);
1085 $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_files_returns(), $result);
1086 $this->assertCount(1, $result['files']);
1087 $this->assertEquals($expectedfile, $result['files'][0]);
1089 // Now add another file to the same subwiki.
1090 $file['filename'] = 'Another image.jpg';
1091 $file['timemodified'] = time();
1092 $content = 'ANOTHER IMAGE';
1093 $fs->create_file_from_string($file, $content);
1095 $expectedfile['filename'] = $file['filename'];
1096 $expectedfile['timemodified'] = $file['timemodified'];
1097 $expectedfile['filesize'] = strlen($content);
1098 $expectedfile['fileurl'] = moodle_url::make_webservice_pluginfile_url($file['contextid'], $file['component'],
1099 $file['filearea'], $file['itemid'], $file['filepath'], $file['filename']);
1101 // Call the WS and check that it returns both files file.
1102 $result = mod_wiki_external::get_subwiki_files($this->wiki->id);
1103 $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_files_returns(), $result);
1104 $this->assertCount(2, $result['files']);
1105 // The new file is returned first because they're returned in alphabetical order.
1106 $this->assertEquals($expectedfile, $result['files'][0]);
1110 * Test get_subwiki_files using an individual wiki with visible groups.
1112 public function test_get_subwiki_files_visible_groups_individual() {
1113 // Create testing data.
1114 $this->create_individual_wikis_with_groups();
1116 $this->setUser($this->student);
1118 // Add a file as subwiki attachment in the student group 1 subwiki.
1119 $fs = get_file_storage();
1120 $contextwiki = context_module::instance($this->wikivisind->cmid);
1121 $file = array('component' => 'mod_wiki', 'filearea' => 'attachments',
1122 'contextid' => $contextwiki->id, 'itemid' => $this->fpvisg1indstu->subwikiid,
1123 'filename' => 'image.jpg', 'filepath' => '/', 'timemodified' => time());
1125 $fs->create_file_from_string($file, $content);
1127 $expectedfile = array(
1128 'filename' => $file['filename'],
1129 'filepath' => $file['filepath'],
1130 'mimetype' => 'image/jpeg',
1131 'filesize' => strlen($content),
1132 'timemodified' => $file['timemodified'],
1133 'fileurl' => moodle_url::make_webservice_pluginfile_url($file['contextid'], $file['component'],
1134 $file['filearea'], $file['itemid'], $file['filepath'], $file['filename']),
1137 // Call the WS and check that it returns this file.
1138 $result = mod_wiki_external::get_subwiki_files($this->wikivisind->id, $this->group1->id);
1139 $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_files_returns(), $result);
1140 $this->assertCount(1, $result['files']);
1141 $this->assertEquals($expectedfile, $result['files'][0]);
1143 // Now check that a teacher can see it too.
1144 $this->setUser($this->teacher);
1145 $result = mod_wiki_external::get_subwiki_files($this->wikivisind->id, $this->group1->id, $this->student->id);
1146 $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_files_returns(), $result);
1147 $this->assertCount(1, $result['files']);
1148 $this->assertEquals($expectedfile, $result['files'][0]);
1153 * Test get_page_for_editing. We won't test all the possible cases because that's already
1154 * done in the tests for wiki_parser_proxy::get_section.
1156 public function test_get_page_for_editing() {
1158 $this->create_individual_wikis_with_groups();
1160 // We add a <span> in the first title to verify the WS works sending HTML in section.
1161 $sectioncontent = '<h1><span>Title1</span></h1>Text inside section';
1162 $pagecontent = $sectioncontent.'<h1>Title2</h1>Text inside section';
1163 $newpage = $this->getDataGenerator()->get_plugin_generator('mod_wiki')->create_page(
1164 $this->wiki, array('content' => $pagecontent));
1166 // Test user with full capabilities.
1167 $this->setUser($this->student);
1169 // Set expected result: Full Page content.
1171 'content' => $pagecontent,
1172 'contentformat' => 'html',
1176 $result = mod_wiki_external::get_page_for_editing($newpage->id);
1177 $result = external_api::clean_returnvalue(mod_wiki_external::get_page_for_editing_returns(), $result);
1178 $this->assertEquals($expected, $result['pagesection']);
1180 // Set expected result: Section Page content.
1182 'content' => $sectioncontent,
1183 'contentformat' => 'html',
1187 $result = mod_wiki_external::get_page_for_editing($newpage->id, '<span>Title1</span>');
1188 $result = external_api::clean_returnvalue(mod_wiki_external::get_page_for_editing_returns(), $result);
1189 $this->assertEquals($expected, $result['pagesection']);
1193 * Test test_get_page_locking.
1195 public function test_get_page_locking() {
1197 $this->create_individual_wikis_with_groups();
1199 $pagecontent = '<h1>Title1</h1>Text inside section<h1>Title2</h1>Text inside section';
1200 $newpage = $this->getDataGenerator()->get_plugin_generator('mod_wiki')->create_page(
1201 $this->wiki, array('content' => $pagecontent));
1203 // Test user with full capabilities.
1204 $this->setUser($this->student);
1206 // Test Section locking.
1211 $result = mod_wiki_external::get_page_for_editing($newpage->id, 'Title1', true);
1212 $result = external_api::clean_returnvalue(mod_wiki_external::get_page_for_editing_returns(), $result);
1213 $this->assertEquals($expected, $result['pagesection']);
1215 // Test the section is locked.
1216 $this->setUser($this->student2);
1218 mod_wiki_external::get_page_for_editing($newpage->id, 'Title1', true);
1219 $this->fail('Exception expected due to not page locking.');
1220 } catch (moodle_exception $e) {
1221 $this->assertEquals('pageislocked', $e->errorcode);
1224 // Test the page is locked.
1226 mod_wiki_external::get_page_for_editing($newpage->id, null, true);
1227 $this->fail('Exception expected due to not page locking.');
1228 } catch (moodle_exception $e) {
1229 $this->assertEquals('pageislocked', $e->errorcode);
1232 // Test the other section is not locked.
1233 $result = mod_wiki_external::get_page_for_editing($newpage->id, 'Title2', true);
1234 $result = external_api::clean_returnvalue(mod_wiki_external::get_page_for_editing_returns(), $result);
1235 $this->assertEquals($expected, $result['pagesection']);
1237 // Back to the original user to test version change when editing.
1238 $this->setUser($this->student);
1239 $newsectioncontent = '<h1>Title2</h1>New test2';
1240 $result = mod_wiki_external::edit_page($newpage->id, $newsectioncontent, 'Title1');
1245 $result = mod_wiki_external::get_page_for_editing($newpage->id, 'Title1', true);
1246 $result = external_api::clean_returnvalue(mod_wiki_external::get_page_for_editing_returns(), $result);
1247 $this->assertEquals($expected, $result['pagesection']);
1251 * Test new_page. We won't test all the possible cases because that's already
1252 * done in the tests for wiki_create_page.
1254 public function test_new_page() {
1256 $this->create_individual_wikis_with_groups();
1258 $sectioncontent = '<h1>Title1</h1>Text inside section';
1259 $pagecontent = $sectioncontent.'<h1>Title2</h1>Text inside section';
1260 $pagetitle = 'Page Title';
1262 // Test user with full capabilities.
1263 $this->setUser($this->student);
1265 // Test on existing subwiki.
1266 $result = mod_wiki_external::new_page($pagetitle, $pagecontent, 'html', $this->fpsepg1indstu->subwikiid);
1267 $result = external_api::clean_returnvalue(mod_wiki_external::new_page_returns(), $result);
1268 $this->assertInternalType('int', $result['pageid']);
1270 $version = wiki_get_current_version($result['pageid']);
1271 $this->assertEquals($pagecontent, $version->content);
1272 $this->assertEquals('html', $version->contentformat);
1274 $page = wiki_get_page($result['pageid']);
1275 $this->assertEquals($pagetitle, $page->title);
1277 // Test existing page creation.
1279 mod_wiki_external::new_page($pagetitle, $pagecontent, 'html', $this->fpsepg1indstu->subwikiid);
1280 $this->fail('Exception expected due to creation of an existing page.');
1281 } catch (moodle_exception $e) {
1282 $this->assertEquals('pageexists', $e->errorcode);
1285 // Test on non existing subwiki. Add student to group2 to have a new subwiki to be created.
1286 $this->getDataGenerator()->create_group_member(array('userid' => $this->student->id, 'groupid' => $this->group2->id));
1287 $result = mod_wiki_external::new_page($pagetitle, $pagecontent, 'html', null, $this->wikisepind->id, $this->student->id,
1289 $result = external_api::clean_returnvalue(mod_wiki_external::new_page_returns(), $result);
1290 $this->assertInternalType('int', $result['pageid']);
1292 $version = wiki_get_current_version($result['pageid']);
1293 $this->assertEquals($pagecontent, $version->content);
1294 $this->assertEquals('html', $version->contentformat);
1296 $page = wiki_get_page($result['pageid']);
1297 $this->assertEquals($pagetitle, $page->title);
1299 $subwiki = wiki_get_subwiki($page->subwikiid);
1300 $expected = new StdClass();
1301 $expected->id = $subwiki->id;
1302 $expected->wikiid = $this->wikisepind->id;
1303 $expected->groupid = $this->group2->id;
1304 $expected->userid = $this->student->id;
1305 $this->assertEquals($expected, $subwiki);
1307 // Check page creation for a user not in course.
1308 $this->studentnotincourse = self::getDataGenerator()->create_user();
1309 $this->anothercourse = $this->getDataGenerator()->create_course();
1310 $this->groupnotincourse = $this->getDataGenerator()->create_group(array('courseid' => $this->anothercourse->id));
1313 mod_wiki_external::new_page($pagetitle, $pagecontent, 'html', null, $this->wikisepind->id,
1314 $this->studentnotincourse->id, $this->groupnotincourse->id);
1315 $this->fail('Exception expected due to creation of an invalid subwiki creation.');
1316 } catch (moodle_exception $e) {
1317 $this->assertEquals('cannoteditpage', $e->errorcode);
1323 * Test edit_page. We won't test all the possible cases because that's already
1324 * done in the tests for wiki_save_section / wiki_save_page.
1326 public function test_edit_page() {
1328 $this->create_individual_wikis_with_groups();
1330 // Test user with full capabilities.
1331 $this->setUser($this->student);
1333 $newpage = $this->getDataGenerator()->get_plugin_generator('mod_wiki')->create_page($this->wikisepind,
1334 array('group' => $this->group1->id, 'content' => 'Test'));
1336 // Test edit whole page.
1337 // We add <span> in the titles to verify the WS works sending HTML in section.
1338 $sectioncontent = '<h1><span>Title1</span></h1>Text inside section';
1339 $newpagecontent = $sectioncontent.'<h1><span>Title2</span></h1>Text inside section';
1341 $result = mod_wiki_external::edit_page($newpage->id, $newpagecontent);
1342 $result = external_api::clean_returnvalue(mod_wiki_external::edit_page_returns(), $result);
1343 $this->assertInternalType('int', $result['pageid']);
1345 $version = wiki_get_current_version($result['pageid']);
1346 $this->assertEquals($newpagecontent, $version->content);
1348 // Test edit section.
1349 $newsectioncontent = '<h1><span>Title2</span></h1>New test2';
1350 $section = '<span>Title2</span>';
1352 $result = mod_wiki_external::edit_page($newpage->id, $newsectioncontent, $section);
1353 $result = external_api::clean_returnvalue(mod_wiki_external::edit_page_returns(), $result);
1354 $this->assertInternalType('int', $result['pageid']);
1356 $expected = $sectioncontent . $newsectioncontent;
1358 $version = wiki_get_current_version($result['pageid']);
1359 $this->assertEquals($expected, $version->content);
1361 // Test locked section.
1362 $newsectioncontent = '<h1><span>Title2</span></h1>New test2';
1363 $section = '<span>Title2</span>';
1366 // Using user 1 to avoid other users to edit.
1367 wiki_set_lock($newpage->id, 1, $section, true);
1368 mod_wiki_external::edit_page($newpage->id, $newsectioncontent, $section);
1369 $this->fail('Exception expected due to locked section');
1370 } catch (moodle_exception $e) {
1371 $this->assertEquals('pageislocked', $e->errorcode);
1374 // Test edit non existing section.
1375 $newsectioncontent = '<h1>Title3</h1>New test3';
1376 $section = 'Title3';
1379 mod_wiki_external::edit_page($newpage->id, $newsectioncontent, $section);
1380 $this->fail('Exception expected due to non existing section in the page.');
1381 } catch (moodle_exception $e) {
1382 $this->assertEquals('invalidsection', $e->errorcode);