MDL-52809 wiki: New WS mod_wiki_get_subwikis
[moodle.git] / mod / wiki / tests / lib_test.php
CommitLineData
c1baf89e
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 * Unit tests for mod_wiki lib
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;
30require_once($CFG->dirroot . '/mod/wiki/lib.php');
31require_once($CFG->libdir . '/completionlib.php');
32
33/**
34 * Unit tests for mod_wiki lib
35 *
36 * @package mod_wiki
37 * @category external
38 * @copyright 2015 Dani Palou <juan@moodle.com>
39 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40 * @since Moodle 3.1
41 */
42class mod_wiki_lib_testcase extends advanced_testcase {
43
44 /**
45 * Test wiki_view.
46 *
47 * @return void
48 */
49 public function test_wiki_view() {
50 global $CFG;
51
52 $CFG->enablecompletion = COMPLETION_ENABLED;
53 $this->resetAfterTest();
54
55 $this->setAdminUser();
56 // Setup test data.
57 $course = $this->getDataGenerator()->create_course(array('enablecompletion' => COMPLETION_ENABLED));
58 $options = array('completion' => COMPLETION_TRACKING_AUTOMATIC, 'completionview' => COMPLETION_VIEW_REQUIRED);
59 $wiki = $this->getDataGenerator()->create_module('wiki', array('course' => $course->id), $options);
60 $context = context_module::instance($wiki->cmid);
61 $cm = get_coursemodule_from_instance('wiki', $wiki->id);
62
63 // Trigger and capture the event.
64 $sink = $this->redirectEvents();
65
66 wiki_view($wiki, $course, $cm, $context);
67
68 $events = $sink->get_events();
69 // 2 additional events thanks to completion.
70 $this->assertCount(3, $events);
71 $event = array_shift($events);
72
73 // Checking that the event contains the expected values.
74 $this->assertInstanceOf('\mod_wiki\event\course_module_viewed', $event);
75 $this->assertEquals($context, $event->get_context());
76 $moodleurl = new \moodle_url('/mod/wiki/view.php', array('id' => $cm->id));
77 $this->assertEquals($moodleurl, $event->get_url());
78 $this->assertEventContextNotUsed($event);
79 $this->assertNotEmpty($event->get_name());
80
81 // Check completion status.
82 $completion = new completion_info($course);
83 $completiondata = $completion->get_data($cm);
84 $this->assertEquals(1, $completiondata->completionstate);
85
86 }
87
88 /**
89 * Test wiki_page_view.
90 *
91 * @return void
92 */
93 public function test_wiki_page_view() {
94 global $CFG;
95
96 $CFG->enablecompletion = COMPLETION_ENABLED;
97 $this->resetAfterTest();
98
99 $this->setAdminUser();
100 // Setup test data.
101 $course = $this->getDataGenerator()->create_course(array('enablecompletion' => COMPLETION_ENABLED));
102 $options = array('completion' => COMPLETION_TRACKING_AUTOMATIC, 'completionview' => COMPLETION_VIEW_REQUIRED);
103 $wiki = $this->getDataGenerator()->create_module('wiki', array('course' => $course->id), $options);
104 $context = context_module::instance($wiki->cmid);
105 $cm = get_coursemodule_from_instance('wiki', $wiki->id);
106 $firstpage = $this->getDataGenerator()->get_plugin_generator('mod_wiki')->create_first_page($wiki);
107
108 // Trigger and capture the event.
109 $sink = $this->redirectEvents();
110
111 wiki_page_view($wiki, $firstpage, $course, $cm, $context);
112
113 $events = $sink->get_events();
114 // 2 additional events thanks to completion.
115 $this->assertCount(3, $events);
116 $event = array_shift($events);
117
118 // Checking that the event contains the expected values.
119 $this->assertInstanceOf('\mod_wiki\event\page_viewed', $event);
120 $this->assertEquals($context, $event->get_context());
121 $pageurl = new \moodle_url('/mod/wiki/view.php', array('pageid' => $firstpage->id));
122 $this->assertEquals($pageurl, $event->get_url());
123 $this->assertEventContextNotUsed($event);
124 $this->assertNotEmpty($event->get_name());
125
126 // Check completion status.
127 $completion = new completion_info($course);
128 $completiondata = $completion->get_data($cm);
129 $this->assertEquals(1, $completiondata->completionstate);
130
131 }
85894eda
DP
132
133 /**
134 * Test wiki_user_can_edit without groups.
135 *
136 * @return void
137 */
138 public function test_wiki_user_can_edit() {
139 global $DB;
140
141 $this->resetAfterTest();
142 $this->setAdminUser();
143
144 // Setup test data.
145 $course = $this->getDataGenerator()->create_course();
146 $indwiki = $this->getDataGenerator()->create_module('wiki', array('course' => $course->id, 'wikimode' => 'individual'));
147 $colwiki = $this->getDataGenerator()->create_module('wiki', array('course' => $course->id, 'wikimode' => 'collaborative'));
148
149 // Create users.
150 $student = self::getDataGenerator()->create_user();
151 $teacher = self::getDataGenerator()->create_user();
152
153 // Users enrolments.
154 $studentrole = $DB->get_record('role', array('shortname' => 'student'));
155 $teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'));
156 $this->getDataGenerator()->enrol_user($student->id, $course->id, $studentrole->id, 'manual');
157 $this->getDataGenerator()->enrol_user($teacher->id, $course->id, $teacherrole->id, 'manual');
158
159 // Simulate collaborative subwiki.
160 $swcol = new stdClass();
161 $swcol->id = -1;
162 $swcol->wikiid = $colwiki->id;
163 $swcol->groupid = 0;
164 $swcol->userid = 0;
165
166 // Simulate individual subwikis (1 per user).
167 $swindstudent = clone($swcol);
168 $swindstudent->wikiid = $indwiki->id;
169 $swindstudent->userid = $student->id;
170
171 $swindteacher = clone($swindstudent);
172 $swindteacher->userid = $teacher->id;
173
174 $this->setUser($student);
175
176 // Check that the student can edit the collaborative subwiki.
177 $this->assertTrue(wiki_user_can_edit($swcol));
178
179 // Check that the student can edit his individual subwiki.
180 $this->assertTrue(wiki_user_can_edit($swindstudent));
181
182 // Check that the student cannot edit teacher's individual subwiki.
183 $this->assertFalse(wiki_user_can_edit($swindteacher));
184
185 // Now test as a teacher.
186 $this->setUser($teacher);
187
188 // Check that the teacher can edit the collaborative subwiki.
189 $this->assertTrue(wiki_user_can_edit($swcol));
190
191 // Check that the teacher can edit his individual subwiki.
192 $this->assertTrue(wiki_user_can_edit($swindteacher));
193
194 // Check that the teacher can edit student's individual subwiki.
195 $this->assertTrue(wiki_user_can_edit($swindstudent));
196
197 }
198
199 /**
200 * Test wiki_user_can_edit using collaborative wikis with groups.
201 *
202 * @return void
203 */
204 public function test_wiki_user_can_edit_with_groups_collaborative() {
205 global $DB;
206
207 $this->resetAfterTest();
208 $this->setAdminUser();
209
210 // Setup test data.
211 $course = $this->getDataGenerator()->create_course();
212 $wikisepcol = $this->getDataGenerator()->create_module('wiki', array('course' => $course->id,
213 'groupmode' => SEPARATEGROUPS, 'wikimode' => 'collaborative'));
214 $wikiviscol = $this->getDataGenerator()->create_module('wiki', array('course' => $course->id,
215 'groupmode' => VISIBLEGROUPS, 'wikimode' => 'collaborative'));
216
217 // Create users.
218 $student = self::getDataGenerator()->create_user();
219 $student2 = self::getDataGenerator()->create_user();
220 $teacher = self::getDataGenerator()->create_user();
221
222 // Users enrolments.
223 $studentrole = $DB->get_record('role', array('shortname' => 'student'));
224 $teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'));
225 $this->getDataGenerator()->enrol_user($student->id, $course->id, $studentrole->id, 'manual');
226 $this->getDataGenerator()->enrol_user($student2->id, $course->id, $studentrole->id, 'manual');
227 $this->getDataGenerator()->enrol_user($teacher->id, $course->id, $teacherrole->id, 'manual');
228
229 // Create groups.
230 $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
231 $this->getDataGenerator()->create_group_member(array('userid' => $student->id, 'groupid' => $group1->id));
232 $this->getDataGenerator()->create_group_member(array('userid' => $student2->id, 'groupid' => $group1->id));
233 $group2 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
234 $this->getDataGenerator()->create_group_member(array('userid' => $student2->id, 'groupid' => $group2->id));
235
236 // Simulate all the possible subwikis.
237 // Subwikis in collaborative wikis: 1 subwiki per group + 1 subwiki for all participants.
238 $swsepcolg1 = new stdClass();
239 $swsepcolg1->id = -1;
240 $swsepcolg1->wikiid = $wikisepcol->id;
241 $swsepcolg1->groupid = $group1->id;
242 $swsepcolg1->userid = 0;
243
244 $swsepcolg2 = clone($swsepcolg1);
245 $swsepcolg2->groupid = $group2->id;
246
247 $swsepcolallparts = clone($swsepcolg1); // All participants.
248 $swsepcolallparts->groupid = 0;
249
250 $swviscolg1 = clone($swsepcolg1);
251 $swviscolg1->wikiid = $wikiviscol->id;
252
253 $swviscolg2 = clone($swviscolg1);
254 $swviscolg2->groupid = $group2->id;
255
256 $swviscolallparts = clone($swviscolg1); // All participants.
257 $swviscolallparts->groupid = 0;
258
259 $this->setUser($student);
260
261 // Check that the student can edit his group's subwiki both in separate and visible groups.
262 $this->assertTrue(wiki_user_can_edit($swsepcolg1));
263 $this->assertTrue(wiki_user_can_edit($swviscolg1));
264
265 // Check that the student cannot edit subwiki from group 2 both in separate and visible groups.
266 $this->assertFalse(wiki_user_can_edit($swsepcolg2));
267 $this->assertFalse(wiki_user_can_edit($swviscolg2));
268
269 // Now test as student 2.
270 $this->setUser($student2);
271
272 // Check that the student 2 can edit subwikis from both groups both in separate and visible groups.
273 $this->assertTrue(wiki_user_can_edit($swsepcolg1));
274 $this->assertTrue(wiki_user_can_edit($swviscolg1));
275 $this->assertTrue(wiki_user_can_edit($swsepcolg2));
276 $this->assertTrue(wiki_user_can_edit($swviscolg2));
277
278 // Check that the student 2 cannot edit subwikis from all participants.
279 $this->assertFalse(wiki_user_can_edit($swsepcolallparts));
280 $this->assertFalse(wiki_user_can_edit($swviscolallparts));
281
282 // Now test it as a teacher.
283 $this->setUser($teacher);
284
285 // Check that teacher can edit all subwikis.
286 $this->assertTrue(wiki_user_can_edit($swsepcolg1));
287 $this->assertTrue(wiki_user_can_edit($swviscolg1));
288 $this->assertTrue(wiki_user_can_edit($swsepcolg2));
289 $this->assertTrue(wiki_user_can_edit($swviscolg2));
290 $this->assertTrue(wiki_user_can_edit($swsepcolallparts));
291 $this->assertTrue(wiki_user_can_edit($swviscolallparts));
292 }
293
294 /**
295 * Test wiki_user_can_edit using individual wikis with groups.
296 *
297 * @return void
298 */
299 public function test_wiki_user_can_edit_with_groups_individual() {
300 global $DB;
301
302 $this->resetAfterTest();
303 $this->setAdminUser();
304
305 // Setup test data.
306 $course = $this->getDataGenerator()->create_course();
307 $wikisepind = $this->getDataGenerator()->create_module('wiki', array('course' => $course->id,
308 'groupmode' => SEPARATEGROUPS, 'wikimode' => 'individual'));
309 $wikivisind = $this->getDataGenerator()->create_module('wiki', array('course' => $course->id,
310 'groupmode' => VISIBLEGROUPS, 'wikimode' => 'individual'));
311
312 // Create users.
313 $student = self::getDataGenerator()->create_user();
314 $student2 = self::getDataGenerator()->create_user();
315 $teacher = self::getDataGenerator()->create_user();
316
317 // Users enrolments.
318 $studentrole = $DB->get_record('role', array('shortname' => 'student'));
319 $teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'));
320 $this->getDataGenerator()->enrol_user($student->id, $course->id, $studentrole->id, 'manual');
321 $this->getDataGenerator()->enrol_user($student2->id, $course->id, $studentrole->id, 'manual');
322 $this->getDataGenerator()->enrol_user($teacher->id, $course->id, $teacherrole->id, 'manual');
323
324 // Create groups.
325 $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
326 $this->getDataGenerator()->create_group_member(array('userid' => $student->id, 'groupid' => $group1->id));
327 $this->getDataGenerator()->create_group_member(array('userid' => $student2->id, 'groupid' => $group1->id));
328 $group2 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
329 $this->getDataGenerator()->create_group_member(array('userid' => $student2->id, 'groupid' => $group2->id));
330
331 // Simulate all the possible subwikis.
332 // Subwikis in collaborative wikis: 1 subwiki per group + 1 subwiki for all participants.
333 $swsepindg1s1 = new stdClass();
334 $swsepindg1s1->id = -1;
335 $swsepindg1s1->wikiid = $wikisepind->id;
336 $swsepindg1s1->groupid = $group1->id;
337 $swsepindg1s1->userid = $student->id;
338
339 $swsepindg1s2 = clone($swsepindg1s1);
340 $swsepindg1s2->userid = $student2->id;
341
342 $swsepindg2s2 = clone($swsepindg1s2);
343 $swsepindg2s2->groupid = $group2->id;
344
345 $swsepindteacher = clone($swsepindg1s1);
346 $swsepindteacher->userid = $teacher->id;
347 $swsepindteacher->groupid = 0;
348
349 $swvisindg1s1 = clone($swsepindg1s1);
350 $swvisindg1s1->wikiid = $wikivisind->id;
351
352 $swvisindg1s2 = clone($swvisindg1s1);
353 $swvisindg1s2->userid = $student2->id;
354
355 $swvisindg2s2 = clone($swvisindg1s2);
356 $swvisindg2s2->groupid = $group2->id;
357
358 $swvisindteacher = clone($swvisindg1s1);
359 $swvisindteacher->userid = $teacher->id;
360 $swvisindteacher->groupid = 0;
361
362 $this->setUser($student);
363
364 // Check that the student can edit his subwiki both in separate and visible groups.
365 $this->assertTrue(wiki_user_can_edit($swsepindg1s1));
366 $this->assertTrue(wiki_user_can_edit($swvisindg1s1));
367
368 // Check that the student cannot edit subwikis from another user even if he belongs to his group.
369 $this->assertFalse(wiki_user_can_edit($swsepindg1s2));
370 $this->assertFalse(wiki_user_can_edit($swvisindg1s2));
371
372 // Now test as student 2.
373 $this->setUser($student2);
374
375 // Check that the student 2 can edit his subwikis from both groups both in separate and visible groups.
376 $this->assertTrue(wiki_user_can_edit($swsepindg1s2));
377 $this->assertTrue(wiki_user_can_edit($swvisindg1s2));
378 $this->assertTrue(wiki_user_can_edit($swsepindg2s2));
379 $this->assertTrue(wiki_user_can_edit($swvisindg2s2));
380
381 // Now test it as a teacher.
382 $this->setUser($teacher);
383
384 // Check that teacher can edit all subwikis.
385 $this->assertTrue(wiki_user_can_edit($swsepindg1s1));
386 $this->assertTrue(wiki_user_can_edit($swsepindg1s2));
387 $this->assertTrue(wiki_user_can_edit($swsepindg2s2));
388 $this->assertTrue(wiki_user_can_edit($swsepindteacher));
389 $this->assertTrue(wiki_user_can_edit($swvisindg1s1));
390 $this->assertTrue(wiki_user_can_edit($swvisindg1s2));
391 $this->assertTrue(wiki_user_can_edit($swvisindg2s2));
392 $this->assertTrue(wiki_user_can_edit($swvisindteacher));
393 }
c1baf89e 394}