MDL-51886 wiki: New WS mod_wiki_view_wiki and mod_wiki_view_page
[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();
61 $this->teacher = self::getDataGenerator()->create_user();
62
63 // Users enrolments.
64 $this->studentrole = $DB->get_record('role', array('shortname' => 'student'));
65 $this->teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'));
66 $this->getDataGenerator()->enrol_user($this->student->id, $this->course->id, $this->studentrole->id, 'manual');
67 $this->getDataGenerator()->enrol_user($this->teacher->id, $this->course->id, $this->teacherrole->id, 'manual');
68
69 // Create first page.
70 $this->firstpage = $this->getDataGenerator()->get_plugin_generator('mod_wiki')->create_first_page($this->wiki);
71 }
72
73 /*
74 * Test get wikis by courses
75 */
76 public function test_mod_wiki_get_wikis_by_courses() {
77
78 // Create additional course.
79 $course2 = self::getDataGenerator()->create_course();
80
81 // Second wiki.
82 $record = new stdClass();
83 $record->course = $course2->id;
84 $wiki2 = self::getDataGenerator()->create_module('wiki', $record);
85
86 // Execute real Moodle enrolment as we'll call unenrol() method on the instance later.
87 $enrol = enrol_get_plugin('manual');
88 $enrolinstances = enrol_get_instances($course2->id, true);
89 foreach ($enrolinstances as $courseenrolinstance) {
90 if ($courseenrolinstance->enrol == "manual") {
91 $instance2 = $courseenrolinstance;
92 break;
93 }
94 }
95 $enrol->enrol_user($instance2, $this->student->id, $this->studentrole->id);
96
97 self::setUser($this->student);
98
99 $returndescription = mod_wiki_external::get_wikis_by_courses_returns();
100
101 // Create what we expect to be returned when querying the two courses.
102 // First for the student user.
103 $expectedfields = array('id', 'coursemodule', 'course', 'name', 'intro', 'introformat', 'firstpagetitle', 'wikimode',
104 'defaultformat', 'forceformat', 'editbegin', 'editend', 'section', 'visible', 'groupmode',
105 'groupingid');
106
107 // Add expected coursemodule and data.
108 $wiki1 = $this->wiki;
109 $wiki1->coursemodule = $wiki1->cmid;
110 $wiki1->introformat = 1;
111 $wiki1->section = 0;
112 $wiki1->visible = true;
113 $wiki1->groupmode = 0;
114 $wiki1->groupingid = 0;
115
116 $wiki2->coursemodule = $wiki2->cmid;
117 $wiki2->introformat = 1;
118 $wiki2->section = 0;
119 $wiki2->visible = true;
120 $wiki2->groupmode = 0;
121 $wiki2->groupingid = 0;
122
123 foreach ($expectedfields as $field) {
124 $expected1[$field] = $wiki1->{$field};
125 $expected2[$field] = $wiki2->{$field};
126 }
127 // Users can create pages by default.
128 $expected1['cancreatepages'] = true;
129 $expected2['cancreatepages'] = true;
130
131 $expectedwikis = array($expected2, $expected1);
132
133 // Call the external function passing course ids.
134 $result = mod_wiki_external::get_wikis_by_courses(array($course2->id, $this->course->id));
135 $result = external_api::clean_returnvalue($returndescription, $result);
136
137 $this->assertEquals($expectedwikis, $result['wikis']);
138 $this->assertCount(0, $result['warnings']);
139
140 // Call the external function without passing course id.
141 $result = mod_wiki_external::get_wikis_by_courses();
142 $result = external_api::clean_returnvalue($returndescription, $result);
143 $this->assertEquals($expectedwikis, $result['wikis']);
144 $this->assertCount(0, $result['warnings']);
145
146 // Unenrol user from second course and alter expected wikis.
147 $enrol->unenrol_user($instance2, $this->student->id);
148 array_shift($expectedwikis);
149
150 // Call the external function without passing course id.
151 $result = mod_wiki_external::get_wikis_by_courses();
152 $result = external_api::clean_returnvalue($returndescription, $result);
153 $this->assertEquals($expectedwikis, $result['wikis']);
154
155 // Call for the second course we unenrolled the user from, expected warning.
156 $result = mod_wiki_external::get_wikis_by_courses(array($course2->id));
157 $this->assertCount(1, $result['warnings']);
158 $this->assertEquals('1', $result['warnings'][0]['warningcode']);
159 $this->assertEquals($course2->id, $result['warnings'][0]['itemid']);
160
161 // Now, try as a teacher for getting all the additional fields.
162 self::setUser($this->teacher);
163
164 $additionalfields = array('timecreated', 'timemodified');
165
166 foreach ($additionalfields as $field) {
167 $expectedwikis[0][$field] = $wiki1->{$field};
168 }
169
170 $result = mod_wiki_external::get_wikis_by_courses();
171 $result = external_api::clean_returnvalue($returndescription, $result);
172 $this->assertEquals($expectedwikis, $result['wikis']);
173
174 // Admin also should get all the information.
175 self::setAdminUser();
176
177 $result = mod_wiki_external::get_wikis_by_courses(array($this->course->id));
178 $result = external_api::clean_returnvalue($returndescription, $result);
179 $this->assertEquals($expectedwikis, $result['wikis']);
180
181 // Now, prohibit capabilities.
182 $this->setUser($this->student);
183 $contextcourse1 = context_course::instance($this->course->id);
184 // Prohibit capability = mod:wiki:viewpage on Course1 for students.
185 assign_capability('mod/wiki:viewpage', CAP_PROHIBIT, $this->studentrole->id, $contextcourse1->id);
186 accesslib_clear_all_caches_for_unit_testing();
187
188 $wikis = mod_wiki_external::get_wikis_by_courses(array($this->course->id));
189 $wikis = external_api::clean_returnvalue(mod_wiki_external::get_wikis_by_courses_returns(), $wikis);
190 $this->assertFalse(isset($wikis['wikis'][0]['intro']));
191
192 // Prohibit capability = mod:wiki:createpage on Course1 for students.
193 assign_capability('mod/wiki:createpage', CAP_PROHIBIT, $this->studentrole->id, $contextcourse1->id);
194 accesslib_clear_all_caches_for_unit_testing();
195
196 $wikis = mod_wiki_external::get_wikis_by_courses(array($this->course->id));
197 $wikis = external_api::clean_returnvalue(mod_wiki_external::get_wikis_by_courses_returns(), $wikis);
198 $this->assertFalse($wikis['wikis'][0]['cancreatepages']);
199 }
200
201}