weekly 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
MN
348 $this->assertEquals($expecteddiscussions, $discussions);
349
039c81f0 350 // Remove the users post from the qanda forum and ensure they can still see the discussion.
a9a0cb69 351 $DB->delete_records('forum_posts', array('id' => $discussion2reply1->id));
40afeb40
JL
352 $discussions = mod_forum_external::get_forum_discussions(array($forum2->id));
353 $discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_returns(), $discussions);
039c81f0 354 $this->assertEquals(1, count($discussions));
a9a0cb69
MN
355
356 // Call without required view discussion capability.
909f27ac 357 $this->unassignUserCapability('mod/forum:viewdiscussion', null, null, $course1->id);
a9a0cb69
MN
358 try {
359 mod_forum_external::get_forum_discussions(array($forum1->id));
360 $this->fail('Exception expected due to missing capability.');
361 } catch (moodle_exception $e) {
362 $this->assertEquals('nopermissions', $e->errorcode);
363 }
364
365 // Unenrol user from second course.
366 $enrol->unenrol_user($instance2, $user1->id);
367
368 // Call for the second course we unenrolled the user from, make sure exception thrown.
369 try {
370 mod_forum_external::get_forum_discussions(array($forum2->id));
371 $this->fail('Exception expected due to being unenrolled from the course.');
372 } catch (moodle_exception $e) {
373 $this->assertEquals('requireloginerror', $e->errorcode);
374 }
2b9fe87d 375 }
e2ede426
JL
376
377 /**
378 * Test get forum posts
379 */
380 public function test_mod_forum_get_forum_discussion_posts() {
d85bedf7 381 global $CFG, $PAGE;
e2ede426
JL
382
383 $this->resetAfterTest(true);
384
385 // Set the CFG variable to allow track forums.
386 $CFG->forum_trackreadposts = true;
387
388 // Create a user who can track forums.
389 $record = new stdClass();
390 $record->trackforums = true;
391 $user1 = self::getDataGenerator()->create_user($record);
392 // Create a bunch of other users to post.
393 $user2 = self::getDataGenerator()->create_user();
394 $user3 = self::getDataGenerator()->create_user();
395
396 // Set the first created user to the test user.
397 self::setUser($user1);
398
399 // Create course to add the module.
400 $course1 = self::getDataGenerator()->create_course();
401
402 // Forum with tracking off.
403 $record = new stdClass();
404 $record->course = $course1->id;
405 $record->trackingtype = FORUM_TRACKING_OFF;
406 $forum1 = self::getDataGenerator()->create_module('forum', $record);
407 $forum1context = context_module::instance($forum1->cmid);
408
409 // Add discussions to the forums.
410 $record = new stdClass();
411 $record->course = $course1->id;
412 $record->userid = $user1->id;
413 $record->forum = $forum1->id;
414 $discussion1 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
415
416 $record = new stdClass();
417 $record->course = $course1->id;
418 $record->userid = $user2->id;
419 $record->forum = $forum1->id;
420 $discussion2 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
421
422 // Add 2 replies to the discussion 1 from different users.
423 $record = new stdClass();
424 $record->discussion = $discussion1->id;
425 $record->parent = $discussion1->firstpost;
426 $record->userid = $user2->id;
427 $discussion1reply1 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
428
429 $record->parent = $discussion1reply1->id;
430 $record->userid = $user3->id;
431 $discussion1reply2 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
432
433 // Enrol the user in the course.
434 $enrol = enrol_get_plugin('manual');
435 // Following line enrol and assign default role id to the user.
436 // So the user automatically gets mod/forum:viewdiscussion on all forums of the course.
437 $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
438 $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
81f810dc
JL
439
440 // Delete one user, to test that we still receive posts by this user.
441 delete_user($user3);
e2ede426
JL
442
443 // Create what we expect to be returned when querying the discussion.
444 $expectedposts = array(
445 'posts' => array(),
446 'warnings' => array(),
447 );
694bf0c7 448
d85bedf7 449 // User pictures are initially empty, we should get the links once the external function is called.
e2ede426
JL
450 $expectedposts['posts'][] = array(
451 'id' => $discussion1reply2->id,
452 'discussion' => $discussion1reply2->discussion,
453 'parent' => $discussion1reply2->parent,
48fb0250 454 'userid' => (int) $discussion1reply2->userid,
e2ede426
JL
455 'created' => $discussion1reply2->created,
456 'modified' => $discussion1reply2->modified,
457 'mailed' => $discussion1reply2->mailed,
458 'subject' => $discussion1reply2->subject,
459 'message' => file_rewrite_pluginfile_urls($discussion1reply2->message, 'pluginfile.php',
460 $forum1context->id, 'mod_forum', 'post', $discussion1reply2->id),
48fb0250 461 'messageformat' => 1, // This value is usually changed by external_format_text() function.
e2ede426
JL
462 'messagetrust' => $discussion1reply2->messagetrust,
463 'attachment' => $discussion1reply2->attachment,
464 'totalscore' => $discussion1reply2->totalscore,
465 'mailnow' => $discussion1reply2->mailnow,
466 'children' => array(),
467 'canreply' => true,
468 'postread' => false,
694bf0c7 469 'userfullname' => fullname($user3),
d85bedf7 470 'userpictureurl' => ''
e2ede426 471 );
694bf0c7 472
e2ede426
JL
473 $expectedposts['posts'][] = array(
474 'id' => $discussion1reply1->id,
475 'discussion' => $discussion1reply1->discussion,
476 'parent' => $discussion1reply1->parent,
48fb0250 477 'userid' => (int) $discussion1reply1->userid,
e2ede426
JL
478 'created' => $discussion1reply1->created,
479 'modified' => $discussion1reply1->modified,
480 'mailed' => $discussion1reply1->mailed,
481 'subject' => $discussion1reply1->subject,
482 'message' => file_rewrite_pluginfile_urls($discussion1reply1->message, 'pluginfile.php',
483 $forum1context->id, 'mod_forum', 'post', $discussion1reply1->id),
48fb0250 484 'messageformat' => 1, // This value is usually changed by external_format_text() function.
e2ede426
JL
485 'messagetrust' => $discussion1reply1->messagetrust,
486 'attachment' => $discussion1reply1->attachment,
487 'totalscore' => $discussion1reply1->totalscore,
488 'mailnow' => $discussion1reply1->mailnow,
d2c58b95 489 'children' => array($discussion1reply2->id),
e2ede426
JL
490 'canreply' => true,
491 'postread' => false,
694bf0c7 492 'userfullname' => fullname($user2),
d85bedf7 493 'userpictureurl' => ''
e2ede426
JL
494 );
495
496 // Test a discussion with two additional posts (total 3 posts).
497 $posts = mod_forum_external::get_forum_discussion_posts($discussion1->id, 'modified', 'DESC');
498 $posts = external_api::clean_returnvalue(mod_forum_external::get_forum_discussion_posts_returns(), $posts);
499 $this->assertEquals(3, count($posts['posts']));
500
d85bedf7
JL
501 // Generate here the pictures because we need to wait to the external function to init the theme.
502 $userpicture = new user_picture($user3);
503 $userpicture->size = 1; // Size f1.
504 $expectedposts['posts'][0]['userpictureurl'] = $userpicture->get_url($PAGE)->out(false);
505
506 $userpicture = new user_picture($user2);
507 $userpicture->size = 1; // Size f1.
508 $expectedposts['posts'][1]['userpictureurl'] = $userpicture->get_url($PAGE)->out(false);
509
e2ede426
JL
510 // Unset the initial discussion post.
511 array_pop($posts['posts']);
512 $this->assertEquals($expectedposts, $posts);
513
514 // Test discussion without additional posts. There should be only one post (the one created by the discussion).
515 $posts = mod_forum_external::get_forum_discussion_posts($discussion2->id, 'modified', 'DESC');
516 $posts = external_api::clean_returnvalue(mod_forum_external::get_forum_discussion_posts_returns(), $posts);
517 $this->assertEquals(1, count($posts['posts']));
518
519 }
c2586672 520
b1aa7dfa
JL
521 /**
522 * Test get forum posts (qanda forum)
523 */
524 public function test_mod_forum_get_forum_discussion_posts_qanda() {
525 global $CFG, $DB;
526
527 $this->resetAfterTest(true);
528
529 $record = new stdClass();
530 $user1 = self::getDataGenerator()->create_user($record);
531 $user2 = self::getDataGenerator()->create_user();
532
533 // Set the first created user to the test user.
534 self::setUser($user1);
535
536 // Create course to add the module.
537 $course1 = self::getDataGenerator()->create_course();
538 $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
539 $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
540
541 // Forum with tracking off.
542 $record = new stdClass();
543 $record->course = $course1->id;
544 $record->type = 'qanda';
545 $forum1 = self::getDataGenerator()->create_module('forum', $record);
546 $forum1context = context_module::instance($forum1->cmid);
547
548 // Add discussions to the forums.
549 $record = new stdClass();
550 $record->course = $course1->id;
551 $record->userid = $user2->id;
552 $record->forum = $forum1->id;
553 $discussion1 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
554
555 // Add 1 reply (not the actual user).
556 $record = new stdClass();
557 $record->discussion = $discussion1->id;
558 $record->parent = $discussion1->firstpost;
559 $record->userid = $user2->id;
560 $discussion1reply1 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
561
562 // We still see only the original post.
563 $posts = mod_forum_external::get_forum_discussion_posts($discussion1->id, 'modified', 'DESC');
564 $posts = external_api::clean_returnvalue(mod_forum_external::get_forum_discussion_posts_returns(), $posts);
565 $this->assertEquals(1, count($posts['posts']));
566
567 // Add a new reply, the user is going to be able to see only the original post and their new post.
568 $record = new stdClass();
569 $record->discussion = $discussion1->id;
570 $record->parent = $discussion1->firstpost;
571 $record->userid = $user1->id;
572 $discussion1reply2 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
573
574 $posts = mod_forum_external::get_forum_discussion_posts($discussion1->id, 'modified', 'DESC');
575 $posts = external_api::clean_returnvalue(mod_forum_external::get_forum_discussion_posts_returns(), $posts);
576 $this->assertEquals(2, count($posts['posts']));
577
578 // Now, we can fake the time of the user post, so he can se the rest of the discussion posts.
579 $discussion1reply2->created -= $CFG->maxeditingtime * 2;
580 $DB->update_record('forum_posts', $discussion1reply2);
581
582 $posts = mod_forum_external::get_forum_discussion_posts($discussion1->id, 'modified', 'DESC');
583 $posts = external_api::clean_returnvalue(mod_forum_external::get_forum_discussion_posts_returns(), $posts);
584 $this->assertEquals(3, count($posts['posts']));
b1aa7dfa
JL
585 }
586
c2586672
JL
587 /**
588 * Test get forum discussions paginated
589 */
590 public function test_mod_forum_get_forum_discussions_paginated() {
d85bedf7 591 global $USER, $CFG, $DB, $PAGE;
c2586672
JL
592
593 $this->resetAfterTest(true);
594
595 // Set the CFG variable to allow track forums.
596 $CFG->forum_trackreadposts = true;
597
598 // Create a user who can track forums.
599 $record = new stdClass();
600 $record->trackforums = true;
601 $user1 = self::getDataGenerator()->create_user($record);
602 // Create a bunch of other users to post.
603 $user2 = self::getDataGenerator()->create_user();
604 $user3 = self::getDataGenerator()->create_user();
605 $user4 = self::getDataGenerator()->create_user();
606
607 // Set the first created user to the test user.
608 self::setUser($user1);
609
610 // Create courses to add the modules.
611 $course1 = self::getDataGenerator()->create_course();
612
613 // First forum with tracking off.
614 $record = new stdClass();
615 $record->course = $course1->id;
616 $record->trackingtype = FORUM_TRACKING_OFF;
617 $forum1 = self::getDataGenerator()->create_module('forum', $record);
618
619 // Add discussions to the forums.
620 $record = new stdClass();
621 $record->course = $course1->id;
622 $record->userid = $user1->id;
623 $record->forum = $forum1->id;
624 $discussion1 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
625
626 // Add three replies to the discussion 1 from different users.
627 $record = new stdClass();
628 $record->discussion = $discussion1->id;
629 $record->parent = $discussion1->firstpost;
630 $record->userid = $user2->id;
631 $discussion1reply1 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
632
633 $record->parent = $discussion1reply1->id;
634 $record->userid = $user3->id;
635 $discussion1reply2 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
636
637 $record->userid = $user4->id;
638 $discussion1reply3 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
639
640 // Enrol the user in the first course.
641 $enrol = enrol_get_plugin('manual');
642
643 // We don't use the dataGenerator as we need to get the $instance2 to unenrol later.
644 $enrolinstances = enrol_get_instances($course1->id, true);
645 foreach ($enrolinstances as $courseenrolinstance) {
646 if ($courseenrolinstance->enrol == "manual") {
647 $instance1 = $courseenrolinstance;
648 break;
649 }
650 }
651 $enrol->enrol_user($instance1, $user1->id);
652
81f810dc
JL
653 // Delete one user.
654 delete_user($user4);
655
c2586672
JL
656 // Assign capabilities to view discussions for forum 1.
657 $cm = get_coursemodule_from_id('forum', $forum1->cmid, 0, false, MUST_EXIST);
658 $context = context_module::instance($cm->id);
659 $newrole = create_role('Role 2', 'role2', 'Role 2 description');
660 $this->assignUserCapability('mod/forum:viewdiscussion', $context->id, $newrole);
661
662 // Create what we expect to be returned when querying the forums.
663
664 $post1 = $DB->get_record('forum_posts', array('id' => $discussion1->firstpost), '*', MUST_EXIST);
c2586672 665
d85bedf7 666 // User pictures are initially empty, we should get the links once the external function is called.
c2586672
JL
667 $expecteddiscussions = array(
668 'id' => $discussion1->firstpost,
669 'name' => $discussion1->name,
670 'groupid' => $discussion1->groupid,
671 'timemodified' => $discussion1reply3->created,
672 'usermodified' => $discussion1reply3->userid,
673 'timestart' => $discussion1->timestart,
674 'timeend' => $discussion1->timeend,
675 'discussion' => $discussion1->id,
676 'parent' => 0,
677 'userid' => $discussion1->userid,
678 'created' => $post1->created,
679 'modified' => $post1->modified,
680 'mailed' => $post1->mailed,
681 'subject' => $post1->subject,
682 'message' => $post1->message,
683 'messageformat' => $post1->messageformat,
684 'messagetrust' => $post1->messagetrust,
685 'attachment' => $post1->attachment,
686 'totalscore' => $post1->totalscore,
687 'mailnow' => $post1->mailnow,
688 'userfullname' => fullname($user1),
689 'usermodifiedfullname' => fullname($user4),
d85bedf7
JL
690 'userpictureurl' => '',
691 'usermodifiedpictureurl' => '',
c2586672 692 'numreplies' => 3,
5f219cf1 693 'numunread' => 0,
f9621c56 694 'pinned' => FORUM_DISCUSSION_UNPINNED
c2586672
JL
695 );
696
697 // Call the external function passing forum id.
698 $discussions = mod_forum_external::get_forum_discussions_paginated($forum1->id);
699 $discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_paginated_returns(), $discussions);
700 $expectedreturn = array(
701 'discussions' => array($expecteddiscussions),
702 'warnings' => array()
703 );
d85bedf7
JL
704
705 // Wait the theme to be loaded (the external_api call does that) to generate the user profiles.
706 $userpicture = new user_picture($user1);
707 $userpicture->size = 1; // Size f1.
708 $expectedreturn['discussions'][0]['userpictureurl'] = $userpicture->get_url($PAGE)->out(false);
709
710 $userpicture = new user_picture($user4);
711 $userpicture->size = 1; // Size f1.
712 $expectedreturn['discussions'][0]['usermodifiedpictureurl'] = $userpicture->get_url($PAGE)->out(false);
713
c2586672
JL
714 $this->assertEquals($expectedreturn, $discussions);
715
716 // Call without required view discussion capability.
717 $this->unassignUserCapability('mod/forum:viewdiscussion', $context->id, $newrole);
718 try {
719 mod_forum_external::get_forum_discussions_paginated($forum1->id);
720 $this->fail('Exception expected due to missing capability.');
721 } catch (moodle_exception $e) {
722 $this->assertEquals('noviewdiscussionspermission', $e->errorcode);
723 }
724
725 // Unenrol user from second course.
726 $enrol->unenrol_user($instance1, $user1->id);
727
728 // Call for the second course we unenrolled the user from, make sure exception thrown.
729 try {
730 mod_forum_external::get_forum_discussions_paginated($forum1->id);
731 $this->fail('Exception expected due to being unenrolled from the course.');
732 } catch (moodle_exception $e) {
733 $this->assertEquals('requireloginerror', $e->errorcode);
734 }
735 }
039c81f0
JL
736
737 /**
738 * Test get forum discussions paginated (qanda forums)
739 */
740 public function test_mod_forum_get_forum_discussions_paginated_qanda() {
741
742 $this->resetAfterTest(true);
743
744 // Create courses to add the modules.
745 $course = self::getDataGenerator()->create_course();
746
747 $user1 = self::getDataGenerator()->create_user();
748 $user2 = self::getDataGenerator()->create_user();
749
750 // First forum with tracking off.
751 $record = new stdClass();
752 $record->course = $course->id;
753 $record->type = 'qanda';
754 $forum = self::getDataGenerator()->create_module('forum', $record);
755
756 // Add discussions to the forums.
757 $discussionrecord = new stdClass();
758 $discussionrecord->course = $course->id;
759 $discussionrecord->userid = $user2->id;
760 $discussionrecord->forum = $forum->id;
761 $discussion = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($discussionrecord);
762
763 self::setAdminUser();
764 $discussions = mod_forum_external::get_forum_discussions_paginated($forum->id);
765 $discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_paginated_returns(), $discussions);
766
767 $this->assertCount(1, $discussions['discussions']);
768 $this->assertCount(0, $discussions['warnings']);
769
770 self::setUser($user1);
771 $this->getDataGenerator()->enrol_user($user1->id, $course->id);
772
773 $discussions = mod_forum_external::get_forum_discussions_paginated($forum->id);
774 $discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_paginated_returns(), $discussions);
775
776 $this->assertCount(1, $discussions['discussions']);
777 $this->assertCount(0, $discussions['warnings']);
778
779 }
50a20317
JL
780
781 /**
782 * Test add_discussion_post
783 */
784 public function test_add_discussion_post() {
785 global $CFG;
786
787 $this->resetAfterTest(true);
788
789 $user = self::getDataGenerator()->create_user();
790 $otheruser = self::getDataGenerator()->create_user();
791
792 self::setAdminUser();
793
794 // Create course to add the module.
795 $course = self::getDataGenerator()->create_course(array('groupmode' => VISIBLEGROUPS, 'groupmodeforce' => 0));
796
797 // Forum with tracking off.
798 $record = new stdClass();
799 $record->course = $course->id;
800 $forum = self::getDataGenerator()->create_module('forum', $record);
801 $cm = get_coursemodule_from_id('forum', $forum->cmid, 0, false, MUST_EXIST);
802 $forumcontext = context_module::instance($forum->cmid);
803
804 // Add discussions to the forums.
805 $record = new stdClass();
806 $record->course = $course->id;
807 $record->userid = $user->id;
808 $record->forum = $forum->id;
809 $discussion = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
810
811 // Try to post (user not enrolled).
812 self::setUser($user);
813 try {
814 mod_forum_external::add_discussion_post($discussion->firstpost, 'some subject', 'some text here...');
815 $this->fail('Exception expected due to being unenrolled from the course.');
816 } catch (moodle_exception $e) {
817 $this->assertEquals('requireloginerror', $e->errorcode);
818 }
819
820 $this->getDataGenerator()->enrol_user($user->id, $course->id);
821 $this->getDataGenerator()->enrol_user($otheruser->id, $course->id);
822
823 $post = mod_forum_external::add_discussion_post($discussion->firstpost, 'some subject', 'some text here...');
824 $post = external_api::clean_returnvalue(mod_forum_external::add_discussion_post_returns(), $post);
825
826 $posts = mod_forum_external::get_forum_discussion_posts($discussion->id);
827 $posts = external_api::clean_returnvalue(mod_forum_external::get_forum_discussion_posts_returns(), $posts);
828 // We receive the discussion and the post.
829 $this->assertEquals(2, count($posts['posts']));
43ff833d
JL
830
831 $tested = false;
832 foreach ($posts['posts'] as $postel) {
833 if ($post['postid'] == $postel['id']) {
834 $this->assertEquals('some subject', $postel['subject']);
835 $this->assertEquals('some text here...', $postel['message']);
836 $tested = true;
837 }
838 }
839 $this->assertTrue($tested);
50a20317
JL
840
841 // Check not posting in groups the user is not member of.
842 $group = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
843 groups_add_member($group->id, $otheruser->id);
844
845 $forum = self::getDataGenerator()->create_module('forum', $record, array('groupmode' => SEPARATEGROUPS));
846 $record->forum = $forum->id;
847 $record->userid = $otheruser->id;
848 $record->groupid = $group->id;
849 $discussion = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
850
851 try {
852 mod_forum_external::add_discussion_post($discussion->firstpost, 'some subject', 'some text here...');
853 $this->fail('Exception expected due to invalid permissions for posting.');
854 } catch (moodle_exception $e) {
50a20317
JL
855 $this->assertEquals('nopostforum', $e->errorcode);
856 }
857
858 }
7ab43ac8
JL
859
860 /*
861 * Test add_discussion. A basic test since all the API functions are already covered by unit tests.
862 */
863 public function test_add_discussion() {
864
865 $this->resetAfterTest(true);
866
867 // Create courses to add the modules.
868 $course = self::getDataGenerator()->create_course();
869
870 $user1 = self::getDataGenerator()->create_user();
871 $user2 = self::getDataGenerator()->create_user();
872
873 // First forum with tracking off.
874 $record = new stdClass();
875 $record->course = $course->id;
876 $record->type = 'news';
877 $forum = self::getDataGenerator()->create_module('forum', $record);
878
879 self::setUser($user1);
880 $this->getDataGenerator()->enrol_user($user1->id, $course->id);
881
882 try {
883 mod_forum_external::add_discussion($forum->id, 'the subject', 'some text here...');
884 $this->fail('Exception expected due to invalid permissions.');
885 } catch (moodle_exception $e) {
886 $this->assertEquals('cannotcreatediscussion', $e->errorcode);
887 }
888
889 self::setAdminUser();
890 $discussion = mod_forum_external::add_discussion($forum->id, 'the subject', 'some text here...');
891 $discussion = external_api::clean_returnvalue(mod_forum_external::add_discussion_returns(), $discussion);
892
893 $discussions = mod_forum_external::get_forum_discussions_paginated($forum->id);
894 $discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_paginated_returns(), $discussions);
895
896 $this->assertCount(1, $discussions['discussions']);
897 $this->assertCount(0, $discussions['warnings']);
898
899 $this->assertEquals($discussion['discussionid'], $discussions['discussions'][0]['discussion']);
900 $this->assertEquals(-1, $discussions['discussions'][0]['groupid']);
901 $this->assertEquals('the subject', $discussions['discussions'][0]['subject']);
902 $this->assertEquals('some text here...', $discussions['discussions'][0]['message']);
903
5f219cf1
BK
904 $discussion2pinned = mod_forum_external::add_discussion($forum->id, 'the pinned subject', 'some 2 text here...', -1,
905 array('options' => array('name' => 'discussionpinned',
906 'value' => true)));
907 $discussion3 = mod_forum_external::add_discussion($forum->id, 'the non pinnedsubject', 'some 3 text here...');
908 $discussions = mod_forum_external::get_forum_discussions_paginated($forum->id);
909 $discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_paginated_returns(), $discussions);
910 $this->assertCount(3, $discussions['discussions']);
911 $this->assertEquals($discussion2pinned['discussionid'], $discussions['discussions'][0]['discussion']);
7ab43ac8
JL
912 }
913
914 /**
915 * Test adding discussions in a course with gorups
916 */
917 public function test_add_discussion_in_course_with_groups() {
918 global $CFG;
919
920 $this->resetAfterTest(true);
921
922 // Create course to add the module.
923 $course = self::getDataGenerator()->create_course(array('groupmode' => VISIBLEGROUPS, 'groupmodeforce' => 0));
924 $user = self::getDataGenerator()->create_user();
925 $this->getDataGenerator()->enrol_user($user->id, $course->id);
926
927 // Forum forcing separate gropus.
928 $record = new stdClass();
929 $record->course = $course->id;
930 $forum = self::getDataGenerator()->create_module('forum', $record, array('groupmode' => SEPARATEGROUPS));
931
932 // Try to post (user not enrolled).
933 self::setUser($user);
934
935 // The user is not enroled in any group, try to post in a forum with separate groups.
936 try {
937 mod_forum_external::add_discussion($forum->id, 'the subject', 'some text here...');
938 $this->fail('Exception expected due to invalid group permissions.');
939 } catch (moodle_exception $e) {
940 $this->assertEquals('cannotcreatediscussion', $e->errorcode);
941 }
942
943 try {
944 mod_forum_external::add_discussion($forum->id, 'the subject', 'some text here...', 0);
945 $this->fail('Exception expected due to invalid group permissions.');
946 } catch (moodle_exception $e) {
947 $this->assertEquals('cannotcreatediscussion', $e->errorcode);
948 }
949
950 // Create a group.
951 $group = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
952
953 // Try to post in a group the user is not enrolled.
954 try {
955 mod_forum_external::add_discussion($forum->id, 'the subject', 'some text here...', $group->id);
956 $this->fail('Exception expected due to invalid group permissions.');
957 } catch (moodle_exception $e) {
958 $this->assertEquals('cannotcreatediscussion', $e->errorcode);
959 }
960
961 // Add the user to a group.
962 groups_add_member($group->id, $user->id);
963
964 // Try to post in a group the user is not enrolled.
965 try {
966 mod_forum_external::add_discussion($forum->id, 'the subject', 'some text here...', $group->id + 1);
967 $this->fail('Exception expected due to invalid group.');
968 } catch (moodle_exception $e) {
969 $this->assertEquals('cannotcreatediscussion', $e->errorcode);
970 }
971
972 // Nost add the discussion using a valid group.
973 $discussion = mod_forum_external::add_discussion($forum->id, 'the subject', 'some text here...', $group->id);
974 $discussion = external_api::clean_returnvalue(mod_forum_external::add_discussion_returns(), $discussion);
975
976 $discussions = mod_forum_external::get_forum_discussions_paginated($forum->id);
977 $discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_paginated_returns(), $discussions);
978
979 $this->assertCount(1, $discussions['discussions']);
980 $this->assertCount(0, $discussions['warnings']);
981 $this->assertEquals($discussion['discussionid'], $discussions['discussions'][0]['discussion']);
982 $this->assertEquals($group->id, $discussions['discussions'][0]['groupid']);
983
984 // Now add a discussions without indicating a group. The function should guess the correct group.
985 $discussion = mod_forum_external::add_discussion($forum->id, 'the subject', 'some text here...');
986 $discussion = external_api::clean_returnvalue(mod_forum_external::add_discussion_returns(), $discussion);
987
988 $discussions = mod_forum_external::get_forum_discussions_paginated($forum->id);
989 $discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_paginated_returns(), $discussions);
990
991 $this->assertCount(2, $discussions['discussions']);
992 $this->assertCount(0, $discussions['warnings']);
993 $this->assertEquals($group->id, $discussions['discussions'][0]['groupid']);
994 $this->assertEquals($group->id, $discussions['discussions'][1]['groupid']);
995
996 // Enrol the same user in other group.
997 $group2 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
998 groups_add_member($group2->id, $user->id);
999
1000 // Now add a discussions without indicating a group. The function should guess the correct group (the first one).
1001 $discussion = mod_forum_external::add_discussion($forum->id, 'the subject', 'some text here...');
1002 $discussion = external_api::clean_returnvalue(mod_forum_external::add_discussion_returns(), $discussion);
1003
1004 $discussions = mod_forum_external::get_forum_discussions_paginated($forum->id);
1005 $discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_paginated_returns(), $discussions);
1006
1007 $this->assertCount(3, $discussions['discussions']);
1008 $this->assertCount(0, $discussions['warnings']);
1009 $this->assertEquals($group->id, $discussions['discussions'][0]['groupid']);
1010 $this->assertEquals($group->id, $discussions['discussions'][1]['groupid']);
1011 $this->assertEquals($group->id, $discussions['discussions'][2]['groupid']);
1012
1013 }
1014
04cd8ae3
JL
1015 /*
1016 * Test can_add_discussion. A basic test since all the API functions are already covered by unit tests.
1017 */
1018 public function test_can_add_discussion() {
1019
1020 $this->resetAfterTest(true);
1021
1022 // Create courses to add the modules.
1023 $course = self::getDataGenerator()->create_course();
1024
1025 $user = self::getDataGenerator()->create_user();
1026
1027 // First forum with tracking off.
1028 $record = new stdClass();
1029 $record->course = $course->id;
1030 $record->type = 'news';
1031 $forum = self::getDataGenerator()->create_module('forum', $record);
1032
1033 // User with no permissions to add in a news forum.
1034 self::setUser($user);
1035 $this->getDataGenerator()->enrol_user($user->id, $course->id);
1036
1037 $result = mod_forum_external::can_add_discussion($forum->id);
1038 $result = external_api::clean_returnvalue(mod_forum_external::can_add_discussion_returns(), $result);
1039 $this->assertFalse($result['status']);
1040
1041 self::setAdminUser();
1042 $result = mod_forum_external::can_add_discussion($forum->id);
1043 $result = external_api::clean_returnvalue(mod_forum_external::can_add_discussion_returns(), $result);
1044 $this->assertTrue($result['status']);
1045
1046 }
1047
2b9fe87d 1048}