weekly on-sync release 3.1dev
[moodle.git] / mod / forum / tests / externallib_test.php
CommitLineData
2b9fe87d 1<?php
2b9fe87d
MN
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 * The module forums external functions unit tests
19 *
20 * @package mod_forum
21 * @category external
22 * @copyright 2012 Mark Nelson <markn@moodle.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26defined('MOODLE_INTERNAL') || die();
27
28global $CFG;
29
30require_once($CFG->dirroot . '/webservice/tests/helpers.php');
31
32class mod_forum_external_testcase extends externallib_advanced_testcase {
33
34 /**
35 * Tests set up
36 */
37 protected function setUp() {
38 global $CFG;
39
59075a43
AN
40 // We must clear the subscription caches. This has to be done both before each test, and after in case of other
41 // tests using these functions.
42 \mod_forum\subscriptions::reset_forum_cache();
43
2b9fe87d
MN
44 require_once($CFG->dirroot . '/mod/forum/externallib.php');
45 }
46
59075a43
AN
47 public function tearDown() {
48 // We must clear the subscription caches. This has to be done both before each test, and after in case of other
49 // tests using these functions.
50 \mod_forum\subscriptions::reset_forum_cache();
51 }
52
2b9fe87d
MN
53 /**
54 * Test get forums
55 */
56 public function test_mod_forum_get_forums_by_courses() {
57 global $USER, $CFG, $DB;
58
59 $this->resetAfterTest(true);
60
61 // Create a user.
62 $user = self::getDataGenerator()->create_user();
63
64 // Set to the user.
65 self::setUser($user);
66
67 // Create courses to add the modules.
68 $course1 = self::getDataGenerator()->create_course();
69 $course2 = self::getDataGenerator()->create_course();
70
71 // First forum.
72 $record = new stdClass();
73 $record->introformat = FORMAT_HTML;
74 $record->course = $course1->id;
75 $forum1 = self::getDataGenerator()->create_module('forum', $record);
76
77 // Second forum.
78 $record = new stdClass();
79 $record->introformat = FORMAT_HTML;
80 $record->course = $course2->id;
81 $forum2 = self::getDataGenerator()->create_module('forum', $record);
82
7ea6ada3
JL
83 // Add discussions to the forums.
84 $record = new stdClass();
85 $record->course = $course1->id;
86 $record->userid = $user->id;
87 $record->forum = $forum1->id;
88 $discussion1 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
89 // Expect one discussion.
90 $forum1->numdiscussions = 1;
ea5b910b 91 $forum1->cancreatediscussions = true;
7ea6ada3
JL
92
93 $record = new stdClass();
94 $record->course = $course2->id;
95 $record->userid = $user->id;
96 $record->forum = $forum2->id;
97 $discussion2 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
98 $discussion3 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
99 // Expect two discussions.
100 $forum2->numdiscussions = 2;
ea5b910b
JL
101 // Default limited role, no create discussion capability enabled.
102 $forum2->cancreatediscussions = false;
7ea6ada3 103
2b9fe87d
MN
104 // Check the forum was correctly created.
105 $this->assertEquals(2, $DB->count_records_select('forum', 'id = :forum1 OR id = :forum2',
106 array('forum1' => $forum1->id, 'forum2' => $forum2->id)));
107
108 // Enrol the user in two courses.
909f27ac
JM
109 // DataGenerator->enrol_user automatically sets a role for the user with the permission mod/form:viewdiscussion.
110 $this->getDataGenerator()->enrol_user($user->id, $course1->id, null, 'manual');
111 // Execute real Moodle enrolment as we'll call unenrol() method on the instance later.
2b9fe87d 112 $enrol = enrol_get_plugin('manual');
2b9fe87d
MN
113 $enrolinstances = enrol_get_instances($course2->id, true);
114 foreach ($enrolinstances as $courseenrolinstance) {
115 if ($courseenrolinstance->enrol == "manual") {
116 $instance2 = $courseenrolinstance;
117 break;
118 }
119 }
120 $enrol->enrol_user($instance2, $user->id);
121
2b9fe87d 122 // Assign capabilities to view forums for forum 2.
74b63eae 123 $cm2 = get_coursemodule_from_id('forum', $forum2->cmid, 0, false, MUST_EXIST);
2b9fe87d
MN
124 $context2 = context_module::instance($cm2->id);
125 $newrole = create_role('Role 2', 'role2', 'Role 2 description');
126 $roleid2 = $this->assignUserCapability('mod/forum:viewdiscussion', $context2->id, $newrole);
127
128 // Create what we expect to be returned when querying the two courses.
c8f1d8a0
JL
129 unset($forum1->displaywordcount);
130 unset($forum2->displaywordcount);
131
2b9fe87d
MN
132 $expectedforums = array();
133 $expectedforums[$forum1->id] = (array) $forum1;
134 $expectedforums[$forum2->id] = (array) $forum2;
135
136 // Call the external function passing course ids.
137 $forums = mod_forum_external::get_forums_by_courses(array($course1->id, $course2->id));
c8f1d8a0
JL
138 $forums = external_api::clean_returnvalue(mod_forum_external::get_forums_by_courses_returns(), $forums);
139 $this->assertCount(2, $forums);
140 foreach ($forums as $forum) {
141 $this->assertEquals($expectedforums[$forum['id']], $forum);
142 }
2b9fe87d
MN
143
144 // Call the external function without passing course id.
145 $forums = mod_forum_external::get_forums_by_courses();
c8f1d8a0
JL
146 $forums = external_api::clean_returnvalue(mod_forum_external::get_forums_by_courses_returns(), $forums);
147 $this->assertCount(2, $forums);
148 foreach ($forums as $forum) {
149 $this->assertEquals($expectedforums[$forum['id']], $forum);
150 }
2b9fe87d
MN
151
152 // Unenrol user from second course and alter expected forums.
153 $enrol->unenrol_user($instance2, $user->id);
154 unset($expectedforums[$forum2->id]);
155
156 // Call the external function without passing course id.
157 $forums = mod_forum_external::get_forums_by_courses();
c8f1d8a0
JL
158 $forums = external_api::clean_returnvalue(mod_forum_external::get_forums_by_courses_returns(), $forums);
159 $this->assertCount(1, $forums);
160 $this->assertEquals($expectedforums[$forum1->id], $forums[0]);
ea5b910b
JL
161 $this->assertTrue($forums[0]['cancreatediscussions']);
162
163 // Change the type of the forum, the user shouldn't be able to add discussions.
164 $DB->set_field('forum', 'type', 'news', array('id' => $forum1->id));
165 $forums = mod_forum_external::get_forums_by_courses();
166 $forums = external_api::clean_returnvalue(mod_forum_external::get_forums_by_courses_returns(), $forums);
167 $this->assertFalse($forums[0]['cancreatediscussions']);
c8f1d8a0
JL
168
169 // Call for the second course we unenrolled the user from.
170 $forums = mod_forum_external::get_forums_by_courses(array($course2->id));
171 $forums = external_api::clean_returnvalue(mod_forum_external::get_forums_by_courses_returns(), $forums);
172 $this->assertCount(0, $forums);
a9a0cb69
MN
173 }
174
175 /**
176 * Test get forum discussions
177 */
178 public function test_mod_forum_get_forum_discussions() {
179 global $USER, $CFG, $DB;
180
181 $this->resetAfterTest(true);
182
183 // Set the CFG variable to allow track forums.
184 $CFG->forum_trackreadposts = true;
185
186 // Create a user who can track forums.
187 $record = new stdClass();
188 $record->trackforums = true;
189 $user1 = self::getDataGenerator()->create_user($record);
190 // Create a bunch of other users to post.
191 $user2 = self::getDataGenerator()->create_user();
192 $user3 = self::getDataGenerator()->create_user();
193 $user4 = self::getDataGenerator()->create_user();
194
195 // Set the first created user to the test user.
196 self::setUser($user1);
197
198 // Create courses to add the modules.
199 $course1 = self::getDataGenerator()->create_course();
200 $course2 = self::getDataGenerator()->create_course();
201
202 // First forum with tracking off.
203 $record = new stdClass();
204 $record->course = $course1->id;
205 $record->trackingtype = FORUM_TRACKING_OFF;
206 $forum1 = self::getDataGenerator()->create_module('forum', $record);
207
208 // Second forum of type 'qanda' with tracking enabled.
209 $record = new stdClass();
210 $record->course = $course2->id;
211 $record->type = 'qanda';
bd8f5d45 212 $record->trackingtype = FORUM_TRACKING_FORCED;
a9a0cb69
MN
213 $forum2 = self::getDataGenerator()->create_module('forum', $record);
214
a9a0cb69
MN
215 // Add discussions to the forums.
216 $record = new stdClass();
217 $record->course = $course1->id;
218 $record->userid = $user1->id;
219 $record->forum = $forum1->id;
c3f31a3d 220 $discussion1 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
a9a0cb69
MN
221
222 $record = new stdClass();
223 $record->course = $course2->id;
224 $record->userid = $user2->id;
225 $record->forum = $forum2->id;
c3f31a3d 226 $discussion2 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
a9a0cb69 227
a9a0cb69
MN
228 // Add three replies to the discussion 1 from different users.
229 $record = new stdClass();
230 $record->discussion = $discussion1->id;
231 $record->parent = $discussion1->firstpost;
232 $record->userid = $user2->id;
c3f31a3d 233 $discussion1reply1 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
2b9fe87d 234
a9a0cb69
MN
235 $record->parent = $discussion1reply1->id;
236 $record->userid = $user3->id;
c3f31a3d 237 $discussion1reply2 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
a9a0cb69
MN
238
239 $record->userid = $user4->id;
c3f31a3d 240 $discussion1reply3 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
a9a0cb69
MN
241
242 // Add two replies to discussion 2 from different users.
243 $record = new stdClass();
244 $record->discussion = $discussion2->id;
245 $record->parent = $discussion2->firstpost;
246 $record->userid = $user1->id;
c3f31a3d 247 $discussion2reply1 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
a9a0cb69
MN
248
249 $record->parent = $discussion2reply1->id;
250 $record->userid = $user3->id;
c3f31a3d 251 $discussion2reply2 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
a9a0cb69
MN
252
253 // Check the forums were correctly created.
40afeb40
JL
254 $this->assertEquals(2, $DB->count_records_select('forum', 'id = :forum1 OR id = :forum2',
255 array('forum1' => $forum1->id, 'forum2' => $forum2->id)));
a9a0cb69
MN
256
257 // Check the discussions were correctly created.
40afeb40
JL
258 $this->assertEquals(2, $DB->count_records_select('forum_discussions', 'forum = :forum1 OR forum = :forum2',
259 array('forum1' => $forum1->id, 'forum2' => $forum2->id)));
a9a0cb69
MN
260
261 // Check the posts were correctly created, don't forget each discussion created also creates a post.
262 $this->assertEquals(7, $DB->count_records_select('forum_posts', 'discussion = :discussion1 OR discussion = :discussion2',
263 array('discussion1' => $discussion1->id, 'discussion2' => $discussion2->id)));
264
265 // Enrol the user in the first course.
266 $enrol = enrol_get_plugin('manual');
909f27ac
JM
267 // Following line enrol and assign default role id to the user.
268 // So the user automatically gets mod/forum:viewdiscussion on all forums of the course.
269 $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
a9a0cb69
MN
270
271 // Now enrol into the second course.
909f27ac 272 // We don't use the dataGenerator as we need to get the $instance2 to unenrol later.
a9a0cb69
MN
273 $enrolinstances = enrol_get_instances($course2->id, true);
274 foreach ($enrolinstances as $courseenrolinstance) {
275 if ($courseenrolinstance->enrol == "manual") {
276 $instance2 = $courseenrolinstance;
277 break;
278 }
279 }
280 $enrol->enrol_user($instance2, $user1->id);
281
a9a0cb69 282 // Assign capabilities to view discussions for forum 2.
74b63eae 283 $cm = get_coursemodule_from_id('forum', $forum2->cmid, 0, false, MUST_EXIST);
a9a0cb69
MN
284 $context = context_module::instance($cm->id);
285 $newrole = create_role('Role 2', 'role2', 'Role 2 description');
286 $this->assignUserCapability('mod/forum:viewdiscussion', $context->id, $newrole);
287
a9a0cb69
MN
288 // Create what we expect to be returned when querying the forums.
289 $expecteddiscussions = array();
9a3f7a48 290 $expecteddiscussions[] = array(
a9a0cb69
MN
291 'id' => $discussion1->id,
292 'course' => $discussion1->course,
293 'forum' => $discussion1->forum,
294 'name' => $discussion1->name,
295 'firstpost' => $discussion1->firstpost,
296 'userid' => $discussion1->userid,
297 'groupid' => $discussion1->groupid,
298 'assessed' => $discussion1->assessed,
299 'timemodified' => $discussion1reply3->created,
300 'usermodified' => $discussion1reply3->userid,
301 'timestart' => $discussion1->timestart,
302 'timeend' => $discussion1->timeend,
303 'firstuserfullname' => fullname($user1),
304 'firstuserimagealt' => $user1->imagealt,
305 'firstuserpicture' => $user1->picture,
306 'firstuseremail' => $user1->email,
307 'subject' => $discussion1->name,
308 'numreplies' => 3,
309 'numunread' => '',
310 'lastpost' => $discussion1reply3->id,
311 'lastuserid' => $user4->id,
312 'lastuserfullname' => fullname($user4),
313 'lastuserimagealt' => $user4->imagealt,
314 'lastuserpicture' => $user4->picture,
315 'lastuseremail' => $user4->email
316 );
9a3f7a48 317 $expecteddiscussions[] = array(
a9a0cb69
MN
318 'id' => $discussion2->id,
319 'course' => $discussion2->course,
320 'forum' => $discussion2->forum,
321 'name' => $discussion2->name,
322 'firstpost' => $discussion2->firstpost,
323 'userid' => $discussion2->userid,
324 'groupid' => $discussion2->groupid,
325 'assessed' => $discussion2->assessed,
326 'timemodified' => $discussion2reply2->created,
327 'usermodified' => $discussion2reply2->userid,
328 'timestart' => $discussion2->timestart,
329 'timeend' => $discussion2->timeend,
330 'firstuserfullname' => fullname($user2),
331 'firstuserimagealt' => $user2->imagealt,
332 'firstuserpicture' => $user2->picture,
333 'firstuseremail' => $user2->email,
334 'subject' => $discussion2->name,
335 'numreplies' => 2,
336 'numunread' => 3,
337 'lastpost' => $discussion2reply2->id,
338 'lastuserid' => $user3->id,
339 'lastuserfullname' => fullname($user3),
340 'lastuserimagealt' => $user3->imagealt,
341 'lastuserpicture' => $user3->picture,
342 'lastuseremail' => $user3->email
343 );
a9a0cb69
MN
344
345 // Call the external function passing forum ids.
40afeb40 346 $discussions = mod_forum_external::get_forum_discussions(array($forum1->id, $forum2->id));
9a3f7a48 347 $discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_returns(), $discussions);
a9a0cb69 348 $this->assertEquals($expecteddiscussions, $discussions);
40afeb40
JL
349 // Some debugging is going to be produced, this is because we switch PAGE contexts in the get_forum_discussions function,
350 // the switch happens when the validate_context function is called inside a foreach loop.
351 // See MDL-41746 for more information.
352 $this->assertDebuggingCalled();
a9a0cb69 353
039c81f0 354 // Remove the users post from the qanda forum and ensure they can still see the discussion.
a9a0cb69 355 $DB->delete_records('forum_posts', array('id' => $discussion2reply1->id));
40afeb40
JL
356 $discussions = mod_forum_external::get_forum_discussions(array($forum2->id));
357 $discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_returns(), $discussions);
039c81f0 358 $this->assertEquals(1, count($discussions));
a9a0cb69
MN
359
360 // Call without required view discussion capability.
909f27ac 361 $this->unassignUserCapability('mod/forum:viewdiscussion', null, null, $course1->id);
a9a0cb69
MN
362 try {
363 mod_forum_external::get_forum_discussions(array($forum1->id));
364 $this->fail('Exception expected due to missing capability.');
365 } catch (moodle_exception $e) {
366 $this->assertEquals('nopermissions', $e->errorcode);
367 }
40afeb40 368 $this->assertDebuggingCalled();
a9a0cb69
MN
369
370 // Unenrol user from second course.
371 $enrol->unenrol_user($instance2, $user1->id);
372
373 // Call for the second course we unenrolled the user from, make sure exception thrown.
374 try {
375 mod_forum_external::get_forum_discussions(array($forum2->id));
376 $this->fail('Exception expected due to being unenrolled from the course.');
377 } catch (moodle_exception $e) {
378 $this->assertEquals('requireloginerror', $e->errorcode);
379 }
2b9fe87d 380 }
e2ede426
JL
381
382 /**
383 * Test get forum posts
384 */
385 public function test_mod_forum_get_forum_discussion_posts() {
d85bedf7 386 global $CFG, $PAGE;
e2ede426
JL
387
388 $this->resetAfterTest(true);
389
390 // Set the CFG variable to allow track forums.
391 $CFG->forum_trackreadposts = true;
392
393 // Create a user who can track forums.
394 $record = new stdClass();
395 $record->trackforums = true;
396 $user1 = self::getDataGenerator()->create_user($record);
397 // Create a bunch of other users to post.
398 $user2 = self::getDataGenerator()->create_user();
399 $user3 = self::getDataGenerator()->create_user();
400
401 // Set the first created user to the test user.
402 self::setUser($user1);
403
404 // Create course to add the module.
405 $course1 = self::getDataGenerator()->create_course();
406
407 // Forum with tracking off.
408 $record = new stdClass();
409 $record->course = $course1->id;
410 $record->trackingtype = FORUM_TRACKING_OFF;
411 $forum1 = self::getDataGenerator()->create_module('forum', $record);
412 $forum1context = context_module::instance($forum1->cmid);
413
414 // Add discussions to the forums.
415 $record = new stdClass();
416 $record->course = $course1->id;
417 $record->userid = $user1->id;
418 $record->forum = $forum1->id;
419 $discussion1 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
420
421 $record = new stdClass();
422 $record->course = $course1->id;
423 $record->userid = $user2->id;
424 $record->forum = $forum1->id;
425 $discussion2 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
426
427 // Add 2 replies to the discussion 1 from different users.
428 $record = new stdClass();
429 $record->discussion = $discussion1->id;
430 $record->parent = $discussion1->firstpost;
431 $record->userid = $user2->id;
432 $discussion1reply1 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
433
434 $record->parent = $discussion1reply1->id;
435 $record->userid = $user3->id;
436 $discussion1reply2 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
437
438 // Enrol the user in the course.
439 $enrol = enrol_get_plugin('manual');
440 // Following line enrol and assign default role id to the user.
441 // So the user automatically gets mod/forum:viewdiscussion on all forums of the course.
442 $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
443 $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
81f810dc
JL
444
445 // Delete one user, to test that we still receive posts by this user.
446 delete_user($user3);
e2ede426
JL
447
448 // Create what we expect to be returned when querying the discussion.
449 $expectedposts = array(
450 'posts' => array(),
451 'warnings' => array(),
452 );
694bf0c7 453
d85bedf7 454 // User pictures are initially empty, we should get the links once the external function is called.
e2ede426
JL
455 $expectedposts['posts'][] = array(
456 'id' => $discussion1reply2->id,
457 'discussion' => $discussion1reply2->discussion,
458 'parent' => $discussion1reply2->parent,
48fb0250 459 'userid' => (int) $discussion1reply2->userid,
e2ede426
JL
460 'created' => $discussion1reply2->created,
461 'modified' => $discussion1reply2->modified,
462 'mailed' => $discussion1reply2->mailed,
463 'subject' => $discussion1reply2->subject,
464 'message' => file_rewrite_pluginfile_urls($discussion1reply2->message, 'pluginfile.php',
465 $forum1context->id, 'mod_forum', 'post', $discussion1reply2->id),
48fb0250 466 'messageformat' => 1, // This value is usually changed by external_format_text() function.
e2ede426
JL
467 'messagetrust' => $discussion1reply2->messagetrust,
468 'attachment' => $discussion1reply2->attachment,
469 'totalscore' => $discussion1reply2->totalscore,
470 'mailnow' => $discussion1reply2->mailnow,
471 'children' => array(),
472 'canreply' => true,
473 'postread' => false,
694bf0c7 474 'userfullname' => fullname($user3),
d85bedf7 475 'userpictureurl' => ''
e2ede426 476 );
694bf0c7 477
e2ede426
JL
478 $expectedposts['posts'][] = array(
479 'id' => $discussion1reply1->id,
480 'discussion' => $discussion1reply1->discussion,
481 'parent' => $discussion1reply1->parent,
48fb0250 482 'userid' => (int) $discussion1reply1->userid,
e2ede426
JL
483 'created' => $discussion1reply1->created,
484 'modified' => $discussion1reply1->modified,
485 'mailed' => $discussion1reply1->mailed,
486 'subject' => $discussion1reply1->subject,
487 'message' => file_rewrite_pluginfile_urls($discussion1reply1->message, 'pluginfile.php',
488 $forum1context->id, 'mod_forum', 'post', $discussion1reply1->id),
48fb0250 489 'messageformat' => 1, // This value is usually changed by external_format_text() function.
e2ede426
JL
490 'messagetrust' => $discussion1reply1->messagetrust,
491 'attachment' => $discussion1reply1->attachment,
492 'totalscore' => $discussion1reply1->totalscore,
493 'mailnow' => $discussion1reply1->mailnow,
d2c58b95 494 'children' => array($discussion1reply2->id),
e2ede426
JL
495 'canreply' => true,
496 'postread' => false,
694bf0c7 497 'userfullname' => fullname($user2),
d85bedf7 498 'userpictureurl' => ''
e2ede426
JL
499 );
500
501 // Test a discussion with two additional posts (total 3 posts).
502 $posts = mod_forum_external::get_forum_discussion_posts($discussion1->id, 'modified', 'DESC');
503 $posts = external_api::clean_returnvalue(mod_forum_external::get_forum_discussion_posts_returns(), $posts);
504 $this->assertEquals(3, count($posts['posts']));
505
d85bedf7
JL
506 // Generate here the pictures because we need to wait to the external function to init the theme.
507 $userpicture = new user_picture($user3);
508 $userpicture->size = 1; // Size f1.
509 $expectedposts['posts'][0]['userpictureurl'] = $userpicture->get_url($PAGE)->out(false);
510
511 $userpicture = new user_picture($user2);
512 $userpicture->size = 1; // Size f1.
513 $expectedposts['posts'][1]['userpictureurl'] = $userpicture->get_url($PAGE)->out(false);
514
e2ede426
JL
515 // Unset the initial discussion post.
516 array_pop($posts['posts']);
517 $this->assertEquals($expectedposts, $posts);
518
519 // Test discussion without additional posts. There should be only one post (the one created by the discussion).
520 $posts = mod_forum_external::get_forum_discussion_posts($discussion2->id, 'modified', 'DESC');
521 $posts = external_api::clean_returnvalue(mod_forum_external::get_forum_discussion_posts_returns(), $posts);
522 $this->assertEquals(1, count($posts['posts']));
523
524 }
c2586672 525
b1aa7dfa
JL
526 /**
527 * Test get forum posts (qanda forum)
528 */
529 public function test_mod_forum_get_forum_discussion_posts_qanda() {
530 global $CFG, $DB;
531
532 $this->resetAfterTest(true);
533
534 $record = new stdClass();
535 $user1 = self::getDataGenerator()->create_user($record);
536 $user2 = self::getDataGenerator()->create_user();
537
538 // Set the first created user to the test user.
539 self::setUser($user1);
540
541 // Create course to add the module.
542 $course1 = self::getDataGenerator()->create_course();
543 $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
544 $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
545
546 // Forum with tracking off.
547 $record = new stdClass();
548 $record->course = $course1->id;
549 $record->type = 'qanda';
550 $forum1 = self::getDataGenerator()->create_module('forum', $record);
551 $forum1context = context_module::instance($forum1->cmid);
552
553 // Add discussions to the forums.
554 $record = new stdClass();
555 $record->course = $course1->id;
556 $record->userid = $user2->id;
557 $record->forum = $forum1->id;
558 $discussion1 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
559
560 // Add 1 reply (not the actual user).
561 $record = new stdClass();
562 $record->discussion = $discussion1->id;
563 $record->parent = $discussion1->firstpost;
564 $record->userid = $user2->id;
565 $discussion1reply1 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
566
567 // We still see only the original post.
568 $posts = mod_forum_external::get_forum_discussion_posts($discussion1->id, 'modified', 'DESC');
569 $posts = external_api::clean_returnvalue(mod_forum_external::get_forum_discussion_posts_returns(), $posts);
570 $this->assertEquals(1, count($posts['posts']));
571
572 // Add a new reply, the user is going to be able to see only the original post and their new post.
573 $record = new stdClass();
574 $record->discussion = $discussion1->id;
575 $record->parent = $discussion1->firstpost;
576 $record->userid = $user1->id;
577 $discussion1reply2 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
578
579 $posts = mod_forum_external::get_forum_discussion_posts($discussion1->id, 'modified', 'DESC');
580 $posts = external_api::clean_returnvalue(mod_forum_external::get_forum_discussion_posts_returns(), $posts);
581 $this->assertEquals(2, count($posts['posts']));
582
583 // Now, we can fake the time of the user post, so he can se the rest of the discussion posts.
584 $discussion1reply2->created -= $CFG->maxeditingtime * 2;
585 $DB->update_record('forum_posts', $discussion1reply2);
586
587 $posts = mod_forum_external::get_forum_discussion_posts($discussion1->id, 'modified', 'DESC');
588 $posts = external_api::clean_returnvalue(mod_forum_external::get_forum_discussion_posts_returns(), $posts);
589 $this->assertEquals(3, count($posts['posts']));
b1aa7dfa
JL
590 }
591
c2586672
JL
592 /**
593 * Test get forum discussions paginated
594 */
595 public function test_mod_forum_get_forum_discussions_paginated() {
d85bedf7 596 global $USER, $CFG, $DB, $PAGE;
c2586672
JL
597
598 $this->resetAfterTest(true);
599
600 // Set the CFG variable to allow track forums.
601 $CFG->forum_trackreadposts = true;
602
603 // Create a user who can track forums.
604 $record = new stdClass();
605 $record->trackforums = true;
606 $user1 = self::getDataGenerator()->create_user($record);
607 // Create a bunch of other users to post.
608 $user2 = self::getDataGenerator()->create_user();
609 $user3 = self::getDataGenerator()->create_user();
610 $user4 = self::getDataGenerator()->create_user();
611
612 // Set the first created user to the test user.
613 self::setUser($user1);
614
615 // Create courses to add the modules.
616 $course1 = self::getDataGenerator()->create_course();
617
618 // First forum with tracking off.
619 $record = new stdClass();
620 $record->course = $course1->id;
621 $record->trackingtype = FORUM_TRACKING_OFF;
622 $forum1 = self::getDataGenerator()->create_module('forum', $record);
623
624 // Add discussions to the forums.
625 $record = new stdClass();
626 $record->course = $course1->id;
627 $record->userid = $user1->id;
628 $record->forum = $forum1->id;
629 $discussion1 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
630
631 // Add three replies to the discussion 1 from different users.
632 $record = new stdClass();
633 $record->discussion = $discussion1->id;
634 $record->parent = $discussion1->firstpost;
635 $record->userid = $user2->id;
636 $discussion1reply1 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
637
638 $record->parent = $discussion1reply1->id;
639 $record->userid = $user3->id;
640 $discussion1reply2 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
641
642 $record->userid = $user4->id;
643 $discussion1reply3 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
644
645 // Enrol the user in the first course.
646 $enrol = enrol_get_plugin('manual');
647
648 // We don't use the dataGenerator as we need to get the $instance2 to unenrol later.
649 $enrolinstances = enrol_get_instances($course1->id, true);
650 foreach ($enrolinstances as $courseenrolinstance) {
651 if ($courseenrolinstance->enrol == "manual") {
652 $instance1 = $courseenrolinstance;
653 break;
654 }
655 }
656 $enrol->enrol_user($instance1, $user1->id);
657
81f810dc
JL
658 // Delete one user.
659 delete_user($user4);
660
c2586672
JL
661 // Assign capabilities to view discussions for forum 1.
662 $cm = get_coursemodule_from_id('forum', $forum1->cmid, 0, false, MUST_EXIST);
663 $context = context_module::instance($cm->id);
664 $newrole = create_role('Role 2', 'role2', 'Role 2 description');
665 $this->assignUserCapability('mod/forum:viewdiscussion', $context->id, $newrole);
666
667 // Create what we expect to be returned when querying the forums.
668
669 $post1 = $DB->get_record('forum_posts', array('id' => $discussion1->firstpost), '*', MUST_EXIST);
c2586672 670
d85bedf7 671 // User pictures are initially empty, we should get the links once the external function is called.
c2586672
JL
672 $expecteddiscussions = array(
673 'id' => $discussion1->firstpost,
674 'name' => $discussion1->name,
675 'groupid' => $discussion1->groupid,
676 'timemodified' => $discussion1reply3->created,
677 'usermodified' => $discussion1reply3->userid,
678 'timestart' => $discussion1->timestart,
679 'timeend' => $discussion1->timeend,
680 'discussion' => $discussion1->id,
681 'parent' => 0,
682 'userid' => $discussion1->userid,
683 'created' => $post1->created,
684 'modified' => $post1->modified,
685 'mailed' => $post1->mailed,
686 'subject' => $post1->subject,
687 'message' => $post1->message,
688 'messageformat' => $post1->messageformat,
689 'messagetrust' => $post1->messagetrust,
690 'attachment' => $post1->attachment,
691 'totalscore' => $post1->totalscore,
692 'mailnow' => $post1->mailnow,
693 'userfullname' => fullname($user1),
694 'usermodifiedfullname' => fullname($user4),
d85bedf7
JL
695 'userpictureurl' => '',
696 'usermodifiedpictureurl' => '',
c2586672
JL
697 'numreplies' => 3,
698 'numunread' => 0
699 );
700
701 // Call the external function passing forum id.
702 $discussions = mod_forum_external::get_forum_discussions_paginated($forum1->id);
703 $discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_paginated_returns(), $discussions);
704 $expectedreturn = array(
705 'discussions' => array($expecteddiscussions),
706 'warnings' => array()
707 );
d85bedf7
JL
708
709 // Wait the theme to be loaded (the external_api call does that) to generate the user profiles.
710 $userpicture = new user_picture($user1);
711 $userpicture->size = 1; // Size f1.
712 $expectedreturn['discussions'][0]['userpictureurl'] = $userpicture->get_url($PAGE)->out(false);
713
714 $userpicture = new user_picture($user4);
715 $userpicture->size = 1; // Size f1.
716 $expectedreturn['discussions'][0]['usermodifiedpictureurl'] = $userpicture->get_url($PAGE)->out(false);
717
c2586672
JL
718 $this->assertEquals($expectedreturn, $discussions);
719
720 // Call without required view discussion capability.
721 $this->unassignUserCapability('mod/forum:viewdiscussion', $context->id, $newrole);
722 try {
723 mod_forum_external::get_forum_discussions_paginated($forum1->id);
724 $this->fail('Exception expected due to missing capability.');
725 } catch (moodle_exception $e) {
726 $this->assertEquals('noviewdiscussionspermission', $e->errorcode);
727 }
728
729 // Unenrol user from second course.
730 $enrol->unenrol_user($instance1, $user1->id);
731
732 // Call for the second course we unenrolled the user from, make sure exception thrown.
733 try {
734 mod_forum_external::get_forum_discussions_paginated($forum1->id);
735 $this->fail('Exception expected due to being unenrolled from the course.');
736 } catch (moodle_exception $e) {
737 $this->assertEquals('requireloginerror', $e->errorcode);
738 }
739 }
039c81f0
JL
740
741 /**
742 * Test get forum discussions paginated (qanda forums)
743 */
744 public function test_mod_forum_get_forum_discussions_paginated_qanda() {
745
746 $this->resetAfterTest(true);
747
748 // Create courses to add the modules.
749 $course = self::getDataGenerator()->create_course();
750
751 $user1 = self::getDataGenerator()->create_user();
752 $user2 = self::getDataGenerator()->create_user();
753
754 // First forum with tracking off.
755 $record = new stdClass();
756 $record->course = $course->id;
757 $record->type = 'qanda';
758 $forum = self::getDataGenerator()->create_module('forum', $record);
759
760 // Add discussions to the forums.
761 $discussionrecord = new stdClass();
762 $discussionrecord->course = $course->id;
763 $discussionrecord->userid = $user2->id;
764 $discussionrecord->forum = $forum->id;
765 $discussion = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($discussionrecord);
766
767 self::setAdminUser();
768 $discussions = mod_forum_external::get_forum_discussions_paginated($forum->id);
769 $discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_paginated_returns(), $discussions);
770
771 $this->assertCount(1, $discussions['discussions']);
772 $this->assertCount(0, $discussions['warnings']);
773
774 self::setUser($user1);
775 $this->getDataGenerator()->enrol_user($user1->id, $course->id);
776
777 $discussions = mod_forum_external::get_forum_discussions_paginated($forum->id);
778 $discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_paginated_returns(), $discussions);
779
780 $this->assertCount(1, $discussions['discussions']);
781 $this->assertCount(0, $discussions['warnings']);
782
783 }
50a20317
JL
784
785 /**
786 * Test add_discussion_post
787 */
788 public function test_add_discussion_post() {
789 global $CFG;
790
791 $this->resetAfterTest(true);
792
793 $user = self::getDataGenerator()->create_user();
794 $otheruser = self::getDataGenerator()->create_user();
795
796 self::setAdminUser();
797
798 // Create course to add the module.
799 $course = self::getDataGenerator()->create_course(array('groupmode' => VISIBLEGROUPS, 'groupmodeforce' => 0));
800
801 // Forum with tracking off.
802 $record = new stdClass();
803 $record->course = $course->id;
804 $forum = self::getDataGenerator()->create_module('forum', $record);
805 $cm = get_coursemodule_from_id('forum', $forum->cmid, 0, false, MUST_EXIST);
806 $forumcontext = context_module::instance($forum->cmid);
807
808 // Add discussions to the forums.
809 $record = new stdClass();
810 $record->course = $course->id;
811 $record->userid = $user->id;
812 $record->forum = $forum->id;
813 $discussion = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
814
815 // Try to post (user not enrolled).
816 self::setUser($user);
817 try {
818 mod_forum_external::add_discussion_post($discussion->firstpost, 'some subject', 'some text here...');
819 $this->fail('Exception expected due to being unenrolled from the course.');
820 } catch (moodle_exception $e) {
821 $this->assertEquals('requireloginerror', $e->errorcode);
822 }
823
824 $this->getDataGenerator()->enrol_user($user->id, $course->id);
825 $this->getDataGenerator()->enrol_user($otheruser->id, $course->id);
826
827 $post = mod_forum_external::add_discussion_post($discussion->firstpost, 'some subject', 'some text here...');
828 $post = external_api::clean_returnvalue(mod_forum_external::add_discussion_post_returns(), $post);
829
830 $posts = mod_forum_external::get_forum_discussion_posts($discussion->id);
831 $posts = external_api::clean_returnvalue(mod_forum_external::get_forum_discussion_posts_returns(), $posts);
832 // We receive the discussion and the post.
833 $this->assertEquals(2, count($posts['posts']));
43ff833d
JL
834
835 $tested = false;
836 foreach ($posts['posts'] as $postel) {
837 if ($post['postid'] == $postel['id']) {
838 $this->assertEquals('some subject', $postel['subject']);
839 $this->assertEquals('some text here...', $postel['message']);
840 $tested = true;
841 }
842 }
843 $this->assertTrue($tested);
50a20317
JL
844
845 // Check not posting in groups the user is not member of.
846 $group = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
847 groups_add_member($group->id, $otheruser->id);
848
849 $forum = self::getDataGenerator()->create_module('forum', $record, array('groupmode' => SEPARATEGROUPS));
850 $record->forum = $forum->id;
851 $record->userid = $otheruser->id;
852 $record->groupid = $group->id;
853 $discussion = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
854
855 try {
856 mod_forum_external::add_discussion_post($discussion->firstpost, 'some subject', 'some text here...');
857 $this->fail('Exception expected due to invalid permissions for posting.');
858 } catch (moodle_exception $e) {
859 // Expect debugging since we are switching context, and this is something WS_SERVER mode don't like.
860 $this->assertDebuggingCalled();
861 $this->assertEquals('nopostforum', $e->errorcode);
862 }
863
864 }
7ab43ac8
JL
865
866 /*
867 * Test add_discussion. A basic test since all the API functions are already covered by unit tests.
868 */
869 public function test_add_discussion() {
870
871 $this->resetAfterTest(true);
872
873 // Create courses to add the modules.
874 $course = self::getDataGenerator()->create_course();
875
876 $user1 = self::getDataGenerator()->create_user();
877 $user2 = self::getDataGenerator()->create_user();
878
879 // First forum with tracking off.
880 $record = new stdClass();
881 $record->course = $course->id;
882 $record->type = 'news';
883 $forum = self::getDataGenerator()->create_module('forum', $record);
884
885 self::setUser($user1);
886 $this->getDataGenerator()->enrol_user($user1->id, $course->id);
887
888 try {
889 mod_forum_external::add_discussion($forum->id, 'the subject', 'some text here...');
890 $this->fail('Exception expected due to invalid permissions.');
891 } catch (moodle_exception $e) {
892 $this->assertEquals('cannotcreatediscussion', $e->errorcode);
893 }
894
895 self::setAdminUser();
896 $discussion = mod_forum_external::add_discussion($forum->id, 'the subject', 'some text here...');
897 $discussion = external_api::clean_returnvalue(mod_forum_external::add_discussion_returns(), $discussion);
898
899 $discussions = mod_forum_external::get_forum_discussions_paginated($forum->id);
900 $discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_paginated_returns(), $discussions);
901
902 $this->assertCount(1, $discussions['discussions']);
903 $this->assertCount(0, $discussions['warnings']);
904
905 $this->assertEquals($discussion['discussionid'], $discussions['discussions'][0]['discussion']);
906 $this->assertEquals(-1, $discussions['discussions'][0]['groupid']);
907 $this->assertEquals('the subject', $discussions['discussions'][0]['subject']);
908 $this->assertEquals('some text here...', $discussions['discussions'][0]['message']);
909
910 }
911
912 /**
913 * Test adding discussions in a course with gorups
914 */
915 public function test_add_discussion_in_course_with_groups() {
916 global $CFG;
917
918 $this->resetAfterTest(true);
919
920 // Create course to add the module.
921 $course = self::getDataGenerator()->create_course(array('groupmode' => VISIBLEGROUPS, 'groupmodeforce' => 0));
922 $user = self::getDataGenerator()->create_user();
923 $this->getDataGenerator()->enrol_user($user->id, $course->id);
924
925 // Forum forcing separate gropus.
926 $record = new stdClass();
927 $record->course = $course->id;
928 $forum = self::getDataGenerator()->create_module('forum', $record, array('groupmode' => SEPARATEGROUPS));
929
930 // Try to post (user not enrolled).
931 self::setUser($user);
932
933 // The user is not enroled in any group, try to post in a forum with separate groups.
934 try {
935 mod_forum_external::add_discussion($forum->id, 'the subject', 'some text here...');
936 $this->fail('Exception expected due to invalid group permissions.');
937 } catch (moodle_exception $e) {
938 $this->assertEquals('cannotcreatediscussion', $e->errorcode);
939 }
940
941 try {
942 mod_forum_external::add_discussion($forum->id, 'the subject', 'some text here...', 0);
943 $this->fail('Exception expected due to invalid group permissions.');
944 } catch (moodle_exception $e) {
945 $this->assertEquals('cannotcreatediscussion', $e->errorcode);
946 }
947
948 // Create a group.
949 $group = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
950
951 // Try to post in a group the user is not enrolled.
952 try {
953 mod_forum_external::add_discussion($forum->id, 'the subject', 'some text here...', $group->id);
954 $this->fail('Exception expected due to invalid group permissions.');
955 } catch (moodle_exception $e) {
956 $this->assertEquals('cannotcreatediscussion', $e->errorcode);
957 }
958
959 // Add the user to a group.
960 groups_add_member($group->id, $user->id);
961
962 // Try to post in a group the user is not enrolled.
963 try {
964 mod_forum_external::add_discussion($forum->id, 'the subject', 'some text here...', $group->id + 1);
965 $this->fail('Exception expected due to invalid group.');
966 } catch (moodle_exception $e) {
967 $this->assertEquals('cannotcreatediscussion', $e->errorcode);
968 }
969
970 // Nost add the discussion using a valid group.
971 $discussion = mod_forum_external::add_discussion($forum->id, 'the subject', 'some text here...', $group->id);
972 $discussion = external_api::clean_returnvalue(mod_forum_external::add_discussion_returns(), $discussion);
973
974 $discussions = mod_forum_external::get_forum_discussions_paginated($forum->id);
975 $discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_paginated_returns(), $discussions);
976
977 $this->assertCount(1, $discussions['discussions']);
978 $this->assertCount(0, $discussions['warnings']);
979 $this->assertEquals($discussion['discussionid'], $discussions['discussions'][0]['discussion']);
980 $this->assertEquals($group->id, $discussions['discussions'][0]['groupid']);
981
982 // Now add a discussions without indicating a group. The function should guess the correct group.
983 $discussion = mod_forum_external::add_discussion($forum->id, 'the subject', 'some text here...');
984 $discussion = external_api::clean_returnvalue(mod_forum_external::add_discussion_returns(), $discussion);
985
986 $discussions = mod_forum_external::get_forum_discussions_paginated($forum->id);
987 $discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_paginated_returns(), $discussions);
988
989 $this->assertCount(2, $discussions['discussions']);
990 $this->assertCount(0, $discussions['warnings']);
991 $this->assertEquals($group->id, $discussions['discussions'][0]['groupid']);
992 $this->assertEquals($group->id, $discussions['discussions'][1]['groupid']);
993
994 // Enrol the same user in other group.
995 $group2 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
996 groups_add_member($group2->id, $user->id);
997
998 // Now add a discussions without indicating a group. The function should guess the correct group (the first one).
999 $discussion = mod_forum_external::add_discussion($forum->id, 'the subject', 'some text here...');
1000 $discussion = external_api::clean_returnvalue(mod_forum_external::add_discussion_returns(), $discussion);
1001
1002 $discussions = mod_forum_external::get_forum_discussions_paginated($forum->id);
1003 $discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_paginated_returns(), $discussions);
1004
1005 $this->assertCount(3, $discussions['discussions']);
1006 $this->assertCount(0, $discussions['warnings']);
1007 $this->assertEquals($group->id, $discussions['discussions'][0]['groupid']);
1008 $this->assertEquals($group->id, $discussions['discussions'][1]['groupid']);
1009 $this->assertEquals($group->id, $discussions['discussions'][2]['groupid']);
1010
1011 }
1012
2b9fe87d 1013}