Merge branch 'MDL-53166' of https://github.com/eugeneventer/moodle-fixes
[moodle.git] / mod / forum / tests / externallib_test.php
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/>.
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  */
26 defined('MOODLE_INTERNAL') || die();
28 global $CFG;
30 require_once($CFG->dirroot . '/webservice/tests/helpers.php');
32 class mod_forum_external_testcase extends externallib_advanced_testcase {
34     /**
35      * Tests set up
36      */
37     protected function setUp() {
38         global $CFG;
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();
44         require_once($CFG->dirroot . '/mod/forum/externallib.php');
45     }
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     }
53     /**
54      * Test get forums
55      */
56     public function test_mod_forum_get_forums_by_courses() {
57         global $USER, $CFG, $DB;
59         $this->resetAfterTest(true);
61         // Create a user.
62         $user = self::getDataGenerator()->create_user();
64         // Set to the user.
65         self::setUser($user);
67         // Create courses to add the modules.
68         $course1 = self::getDataGenerator()->create_course();
69         $course2 = self::getDataGenerator()->create_course();
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);
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);
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;
91         $forum1->cancreatediscussions = true;
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;
101         // Default limited role, no create discussion capability enabled.
102         $forum2->cancreatediscussions = false;
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)));
108         // Enrol the user in two courses.
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.
112         $enrol = enrol_get_plugin('manual');
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);
122         // Assign capabilities to view forums for forum 2.
123         $cm2 = get_coursemodule_from_id('forum', $forum2->cmid, 0, false, MUST_EXIST);
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);
128         // Create what we expect to be returned when querying the two courses.
129         unset($forum1->displaywordcount);
130         unset($forum2->displaywordcount);
132         $expectedforums = array();
133         $expectedforums[$forum1->id] = (array) $forum1;
134         $expectedforums[$forum2->id] = (array) $forum2;
136         // Call the external function passing course ids.
137         $forums = mod_forum_external::get_forums_by_courses(array($course1->id, $course2->id));
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         }
144         // Call the external function without passing course id.
145         $forums = mod_forum_external::get_forums_by_courses();
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         }
152         // Unenrol user from second course and alter expected forums.
153         $enrol->unenrol_user($instance2, $user->id);
154         unset($expectedforums[$forum2->id]);
156         // Call the external function without passing course id.
157         $forums = mod_forum_external::get_forums_by_courses();
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]);
161         $this->assertTrue($forums[0]['cancreatediscussions']);
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']);
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);
173     }
175     /**
176      * Test get forum discussions
177      */
178     public function test_mod_forum_get_forum_discussions() {
179         global $USER, $CFG, $DB;
181         $this->resetAfterTest(true);
183         // Set the CFG variable to allow track forums.
184         $CFG->forum_trackreadposts = true;
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();
195         // Set the first created user to the test user.
196         self::setUser($user1);
198         // Create courses to add the modules.
199         $course1 = self::getDataGenerator()->create_course();
200         $course2 = self::getDataGenerator()->create_course();
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);
208         // Second forum of type 'qanda' with tracking enabled.
209         $record = new stdClass();
210         $record->course = $course2->id;
211         $record->type = 'qanda';
212         $record->trackingtype = FORUM_TRACKING_FORCED;
213         $forum2 = self::getDataGenerator()->create_module('forum', $record);
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;
220         $discussion1 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
222         $record = new stdClass();
223         $record->course = $course2->id;
224         $record->userid = $user2->id;
225         $record->forum = $forum2->id;
226         $discussion2 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
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;
233         $discussion1reply1 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
235         $record->parent = $discussion1reply1->id;
236         $record->userid = $user3->id;
237         $discussion1reply2 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
239         $record->userid = $user4->id;
240         $discussion1reply3 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
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;
247         $discussion2reply1 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
249         $record->parent = $discussion2reply1->id;
250         $record->userid = $user3->id;
251         $discussion2reply2 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
253         // Check the forums were correctly created.
254         $this->assertEquals(2, $DB->count_records_select('forum', 'id = :forum1 OR id = :forum2',
255                 array('forum1' => $forum1->id, 'forum2' => $forum2->id)));
257         // Check the discussions were correctly created.
258         $this->assertEquals(2, $DB->count_records_select('forum_discussions', 'forum = :forum1 OR forum = :forum2',
259                                                             array('forum1' => $forum1->id, 'forum2' => $forum2->id)));
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)));
265         // Enrol the user in the first course.
266         $enrol = enrol_get_plugin('manual');
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);
271         // Now enrol into the second course.
272         // We don't use the dataGenerator as we need to get the $instance2 to unenrol later.
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);
282         // Assign capabilities to view discussions for forum 2.
283         $cm = get_coursemodule_from_id('forum', $forum2->cmid, 0, false, MUST_EXIST);
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);
288         // Create what we expect to be returned when querying the forums.
289         $expecteddiscussions = array();
290         $expecteddiscussions[] = array(
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             );
317         $expecteddiscussions[] = array(
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             );
345         // Call the external function passing forum ids.
346         $discussions = mod_forum_external::get_forum_discussions(array($forum1->id, $forum2->id));
347         $discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_returns(), $discussions);
348         $this->assertEquals($expecteddiscussions, $discussions);
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();
354         // Remove the users post from the qanda forum and ensure they can still see the discussion.
355         $DB->delete_records('forum_posts', array('id' => $discussion2reply1->id));
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);
358         $this->assertEquals(1, count($discussions));
360         // Call without required view discussion capability.
361         $this->unassignUserCapability('mod/forum:viewdiscussion', null, null, $course1->id);
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         }
368         $this->assertDebuggingCalled();
370         // Unenrol user from second course.
371         $enrol->unenrol_user($instance2, $user1->id);
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         }
380     }
382     /**
383      * Test get forum posts
384      */
385     public function test_mod_forum_get_forum_discussion_posts() {
386         global $CFG, $PAGE;
388         $this->resetAfterTest(true);
390         // Set the CFG variable to allow track forums.
391         $CFG->forum_trackreadposts = true;
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();
401         // Set the first created user to the test user.
402         self::setUser($user1);
404         // Create course to add the module.
405         $course1 = self::getDataGenerator()->create_course();
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);
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);
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);
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);
434         $record->parent = $discussion1reply1->id;
435         $record->userid = $user3->id;
436         $discussion1reply2 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
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);
445         // Delete one user, to test that we still receive posts by this user.
446         delete_user($user3);
448         // Create what we expect to be returned when querying the discussion.
449         $expectedposts = array(
450             'posts' => array(),
451             'warnings' => array(),
452         );
454         // User pictures are initially empty, we should get the links once the external function is called.
455         $expectedposts['posts'][] = array(
456             'id' => $discussion1reply2->id,
457             'discussion' => $discussion1reply2->discussion,
458             'parent' => $discussion1reply2->parent,
459             'userid' => (int) $discussion1reply2->userid,
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),
466             'messageformat' => 1,   // This value is usually changed by external_format_text() function.
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,
474             'userfullname' => fullname($user3),
475             'userpictureurl' => ''
476         );
478         $expectedposts['posts'][] = array(
479             'id' => $discussion1reply1->id,
480             'discussion' => $discussion1reply1->discussion,
481             'parent' => $discussion1reply1->parent,
482             'userid' => (int) $discussion1reply1->userid,
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),
489             'messageformat' => 1,   // This value is usually changed by external_format_text() function.
490             'messagetrust' => $discussion1reply1->messagetrust,
491             'attachment' => $discussion1reply1->attachment,
492             'totalscore' => $discussion1reply1->totalscore,
493             'mailnow' => $discussion1reply1->mailnow,
494             'children' => array($discussion1reply2->id),
495             'canreply' => true,
496             'postread' => false,
497             'userfullname' => fullname($user2),
498             'userpictureurl' => ''
499         );
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']));
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);
511         $userpicture = new user_picture($user2);
512         $userpicture->size = 1; // Size f1.
513         $expectedposts['posts'][1]['userpictureurl'] = $userpicture->get_url($PAGE)->out(false);
515         // Unset the initial discussion post.
516         array_pop($posts['posts']);
517         $this->assertEquals($expectedposts, $posts);
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']));
524     }
526     /**
527      * Test get forum posts (qanda forum)
528      */
529     public function test_mod_forum_get_forum_discussion_posts_qanda() {
530         global $CFG, $DB;
532         $this->resetAfterTest(true);
534         $record = new stdClass();
535         $user1 = self::getDataGenerator()->create_user($record);
536         $user2 = self::getDataGenerator()->create_user();
538         // Set the first created user to the test user.
539         self::setUser($user1);
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);
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);
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);
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);
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']));
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);
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']));
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);
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']));
590     }
592     /**
593      * Test get forum discussions paginated
594      */
595     public function test_mod_forum_get_forum_discussions_paginated() {
596         global $USER, $CFG, $DB, $PAGE;
598         $this->resetAfterTest(true);
600         // Set the CFG variable to allow track forums.
601         $CFG->forum_trackreadposts = true;
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();
612         // Set the first created user to the test user.
613         self::setUser($user1);
615         // Create courses to add the modules.
616         $course1 = self::getDataGenerator()->create_course();
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);
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);
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);
638         $record->parent = $discussion1reply1->id;
639         $record->userid = $user3->id;
640         $discussion1reply2 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
642         $record->userid = $user4->id;
643         $discussion1reply3 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
645         // Enrol the user in the first course.
646         $enrol = enrol_get_plugin('manual');
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);
658         // Delete one user.
659         delete_user($user4);
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);
667         // Create what we expect to be returned when querying the forums.
669         $post1 = $DB->get_record('forum_posts', array('id' => $discussion1->firstpost), '*', MUST_EXIST);
671         // User pictures are initially empty, we should get the links once the external function is called.
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),
695                 'userpictureurl' => '',
696                 'usermodifiedpictureurl' => '',
697                 'numreplies' => 3,
698                 'numunread' => 0,
699                 'pinned' => FORUM_DISCUSSION_UNPINNED
700             );
702         // Call the external function passing forum id.
703         $discussions = mod_forum_external::get_forum_discussions_paginated($forum1->id);
704         $discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_paginated_returns(), $discussions);
705         $expectedreturn = array(
706             'discussions' => array($expecteddiscussions),
707             'warnings' => array()
708         );
710         // Wait the theme to be loaded (the external_api call does that) to generate the user profiles.
711         $userpicture = new user_picture($user1);
712         $userpicture->size = 1; // Size f1.
713         $expectedreturn['discussions'][0]['userpictureurl'] = $userpicture->get_url($PAGE)->out(false);
715         $userpicture = new user_picture($user4);
716         $userpicture->size = 1; // Size f1.
717         $expectedreturn['discussions'][0]['usermodifiedpictureurl'] = $userpicture->get_url($PAGE)->out(false);
719         $this->assertEquals($expectedreturn, $discussions);
721         // Call without required view discussion capability.
722         $this->unassignUserCapability('mod/forum:viewdiscussion', $context->id, $newrole);
723         try {
724             mod_forum_external::get_forum_discussions_paginated($forum1->id);
725             $this->fail('Exception expected due to missing capability.');
726         } catch (moodle_exception $e) {
727             $this->assertEquals('noviewdiscussionspermission', $e->errorcode);
728         }
730         // Unenrol user from second course.
731         $enrol->unenrol_user($instance1, $user1->id);
733         // Call for the second course we unenrolled the user from, make sure exception thrown.
734         try {
735             mod_forum_external::get_forum_discussions_paginated($forum1->id);
736             $this->fail('Exception expected due to being unenrolled from the course.');
737         } catch (moodle_exception $e) {
738             $this->assertEquals('requireloginerror', $e->errorcode);
739         }
740     }
742     /**
743      * Test get forum discussions paginated (qanda forums)
744      */
745     public function test_mod_forum_get_forum_discussions_paginated_qanda() {
747         $this->resetAfterTest(true);
749         // Create courses to add the modules.
750         $course = self::getDataGenerator()->create_course();
752         $user1 = self::getDataGenerator()->create_user();
753         $user2 = self::getDataGenerator()->create_user();
755         // First forum with tracking off.
756         $record = new stdClass();
757         $record->course = $course->id;
758         $record->type = 'qanda';
759         $forum = self::getDataGenerator()->create_module('forum', $record);
761         // Add discussions to the forums.
762         $discussionrecord = new stdClass();
763         $discussionrecord->course = $course->id;
764         $discussionrecord->userid = $user2->id;
765         $discussionrecord->forum = $forum->id;
766         $discussion = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($discussionrecord);
768         self::setAdminUser();
769         $discussions = mod_forum_external::get_forum_discussions_paginated($forum->id);
770         $discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_paginated_returns(), $discussions);
772         $this->assertCount(1, $discussions['discussions']);
773         $this->assertCount(0, $discussions['warnings']);
775         self::setUser($user1);
776         $this->getDataGenerator()->enrol_user($user1->id, $course->id);
778         $discussions = mod_forum_external::get_forum_discussions_paginated($forum->id);
779         $discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_paginated_returns(), $discussions);
781         $this->assertCount(1, $discussions['discussions']);
782         $this->assertCount(0, $discussions['warnings']);
784     }
786     /**
787      * Test add_discussion_post
788      */
789     public function test_add_discussion_post() {
790         global $CFG;
792         $this->resetAfterTest(true);
794         $user = self::getDataGenerator()->create_user();
795         $otheruser = self::getDataGenerator()->create_user();
797         self::setAdminUser();
799         // Create course to add the module.
800         $course = self::getDataGenerator()->create_course(array('groupmode' => VISIBLEGROUPS, 'groupmodeforce' => 0));
802         // Forum with tracking off.
803         $record = new stdClass();
804         $record->course = $course->id;
805         $forum = self::getDataGenerator()->create_module('forum', $record);
806         $cm = get_coursemodule_from_id('forum', $forum->cmid, 0, false, MUST_EXIST);
807         $forumcontext = context_module::instance($forum->cmid);
809         // Add discussions to the forums.
810         $record = new stdClass();
811         $record->course = $course->id;
812         $record->userid = $user->id;
813         $record->forum = $forum->id;
814         $discussion = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
816         // Try to post (user not enrolled).
817         self::setUser($user);
818         try {
819             mod_forum_external::add_discussion_post($discussion->firstpost, 'some subject', 'some text here...');
820             $this->fail('Exception expected due to being unenrolled from the course.');
821         } catch (moodle_exception $e) {
822             $this->assertEquals('requireloginerror', $e->errorcode);
823         }
825         $this->getDataGenerator()->enrol_user($user->id, $course->id);
826         $this->getDataGenerator()->enrol_user($otheruser->id, $course->id);
828         $post = mod_forum_external::add_discussion_post($discussion->firstpost, 'some subject', 'some text here...');
829         $post = external_api::clean_returnvalue(mod_forum_external::add_discussion_post_returns(), $post);
831         $posts = mod_forum_external::get_forum_discussion_posts($discussion->id);
832         $posts = external_api::clean_returnvalue(mod_forum_external::get_forum_discussion_posts_returns(), $posts);
833         // We receive the discussion and the post.
834         $this->assertEquals(2, count($posts['posts']));
836         $tested = false;
837         foreach ($posts['posts'] as $postel) {
838             if ($post['postid'] == $postel['id']) {
839                 $this->assertEquals('some subject', $postel['subject']);
840                 $this->assertEquals('some text here...', $postel['message']);
841                 $tested = true;
842             }
843         }
844         $this->assertTrue($tested);
846         // Check not posting in groups the user is not member of.
847         $group = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
848         groups_add_member($group->id, $otheruser->id);
850         $forum = self::getDataGenerator()->create_module('forum', $record, array('groupmode' => SEPARATEGROUPS));
851         $record->forum = $forum->id;
852         $record->userid = $otheruser->id;
853         $record->groupid = $group->id;
854         $discussion = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
856         try {
857             mod_forum_external::add_discussion_post($discussion->firstpost, 'some subject', 'some text here...');
858             $this->fail('Exception expected due to invalid permissions for posting.');
859         } catch (moodle_exception $e) {
860             // Expect debugging since we are switching context, and this is something WS_SERVER mode don't like.
861             $this->assertDebuggingCalled();
862             $this->assertEquals('nopostforum', $e->errorcode);
863         }
865     }
867     /*
868      * Test add_discussion. A basic test since all the API functions are already covered by unit tests.
869      */
870     public function test_add_discussion() {
872         $this->resetAfterTest(true);
874         // Create courses to add the modules.
875         $course = self::getDataGenerator()->create_course();
877         $user1 = self::getDataGenerator()->create_user();
878         $user2 = self::getDataGenerator()->create_user();
880         // First forum with tracking off.
881         $record = new stdClass();
882         $record->course = $course->id;
883         $record->type = 'news';
884         $forum = self::getDataGenerator()->create_module('forum', $record);
886         self::setUser($user1);
887         $this->getDataGenerator()->enrol_user($user1->id, $course->id);
889         try {
890             mod_forum_external::add_discussion($forum->id, 'the subject', 'some text here...');
891             $this->fail('Exception expected due to invalid permissions.');
892         } catch (moodle_exception $e) {
893             $this->assertEquals('cannotcreatediscussion', $e->errorcode);
894         }
896         self::setAdminUser();
897         $discussion = mod_forum_external::add_discussion($forum->id, 'the subject', 'some text here...');
898         $discussion = external_api::clean_returnvalue(mod_forum_external::add_discussion_returns(), $discussion);
900         $discussions = mod_forum_external::get_forum_discussions_paginated($forum->id);
901         $discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_paginated_returns(), $discussions);
903         $this->assertCount(1, $discussions['discussions']);
904         $this->assertCount(0, $discussions['warnings']);
906         $this->assertEquals($discussion['discussionid'], $discussions['discussions'][0]['discussion']);
907         $this->assertEquals(-1, $discussions['discussions'][0]['groupid']);
908         $this->assertEquals('the subject', $discussions['discussions'][0]['subject']);
909         $this->assertEquals('some text here...', $discussions['discussions'][0]['message']);
911         $discussion2pinned = mod_forum_external::add_discussion($forum->id, 'the pinned subject', 'some 2 text here...', -1,
912                                                                 array('options' => array('name' => 'discussionpinned',
913                                                                                          'value' => true)));
914         $discussion3 = mod_forum_external::add_discussion($forum->id, 'the non pinnedsubject', 'some 3 text here...');
915         $discussions = mod_forum_external::get_forum_discussions_paginated($forum->id);
916         $discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_paginated_returns(), $discussions);
917         $this->assertCount(3, $discussions['discussions']);
918         $this->assertEquals($discussion2pinned['discussionid'], $discussions['discussions'][0]['discussion']);
919     }
921     /**
922      * Test adding discussions in a course with gorups
923      */
924     public function test_add_discussion_in_course_with_groups() {
925         global $CFG;
927         $this->resetAfterTest(true);
929         // Create course to add the module.
930         $course = self::getDataGenerator()->create_course(array('groupmode' => VISIBLEGROUPS, 'groupmodeforce' => 0));
931         $user = self::getDataGenerator()->create_user();
932         $this->getDataGenerator()->enrol_user($user->id, $course->id);
934         // Forum forcing separate gropus.
935         $record = new stdClass();
936         $record->course = $course->id;
937         $forum = self::getDataGenerator()->create_module('forum', $record, array('groupmode' => SEPARATEGROUPS));
939         // Try to post (user not enrolled).
940         self::setUser($user);
942         // The user is not enroled in any group, try to post in a forum with separate groups.
943         try {
944             mod_forum_external::add_discussion($forum->id, 'the subject', 'some text here...');
945             $this->fail('Exception expected due to invalid group permissions.');
946         } catch (moodle_exception $e) {
947             $this->assertEquals('cannotcreatediscussion', $e->errorcode);
948         }
950         try {
951             mod_forum_external::add_discussion($forum->id, 'the subject', 'some text here...', 0);
952             $this->fail('Exception expected due to invalid group permissions.');
953         } catch (moodle_exception $e) {
954             $this->assertEquals('cannotcreatediscussion', $e->errorcode);
955         }
957         // Create a group.
958         $group = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
960         // Try to post in a group the user is not enrolled.
961         try {
962             mod_forum_external::add_discussion($forum->id, 'the subject', 'some text here...', $group->id);
963             $this->fail('Exception expected due to invalid group permissions.');
964         } catch (moodle_exception $e) {
965             $this->assertEquals('cannotcreatediscussion', $e->errorcode);
966         }
968         // Add the user to a group.
969         groups_add_member($group->id, $user->id);
971         // Try to post in a group the user is not enrolled.
972         try {
973             mod_forum_external::add_discussion($forum->id, 'the subject', 'some text here...', $group->id + 1);
974             $this->fail('Exception expected due to invalid group.');
975         } catch (moodle_exception $e) {
976             $this->assertEquals('cannotcreatediscussion', $e->errorcode);
977         }
979         // Nost add the discussion using a valid group.
980         $discussion = mod_forum_external::add_discussion($forum->id, 'the subject', 'some text here...', $group->id);
981         $discussion = external_api::clean_returnvalue(mod_forum_external::add_discussion_returns(), $discussion);
983         $discussions = mod_forum_external::get_forum_discussions_paginated($forum->id);
984         $discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_paginated_returns(), $discussions);
986         $this->assertCount(1, $discussions['discussions']);
987         $this->assertCount(0, $discussions['warnings']);
988         $this->assertEquals($discussion['discussionid'], $discussions['discussions'][0]['discussion']);
989         $this->assertEquals($group->id, $discussions['discussions'][0]['groupid']);
991         // Now add a discussions without indicating a group. The function should guess the correct group.
992         $discussion = mod_forum_external::add_discussion($forum->id, 'the subject', 'some text here...');
993         $discussion = external_api::clean_returnvalue(mod_forum_external::add_discussion_returns(), $discussion);
995         $discussions = mod_forum_external::get_forum_discussions_paginated($forum->id);
996         $discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_paginated_returns(), $discussions);
998         $this->assertCount(2, $discussions['discussions']);
999         $this->assertCount(0, $discussions['warnings']);
1000         $this->assertEquals($group->id, $discussions['discussions'][0]['groupid']);
1001         $this->assertEquals($group->id, $discussions['discussions'][1]['groupid']);
1003         // Enrol the same user in other group.
1004         $group2 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
1005         groups_add_member($group2->id, $user->id);
1007         // Now add a discussions without indicating a group. The function should guess the correct group (the first one).
1008         $discussion = mod_forum_external::add_discussion($forum->id, 'the subject', 'some text here...');
1009         $discussion = external_api::clean_returnvalue(mod_forum_external::add_discussion_returns(), $discussion);
1011         $discussions = mod_forum_external::get_forum_discussions_paginated($forum->id);
1012         $discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_paginated_returns(), $discussions);
1014         $this->assertCount(3, $discussions['discussions']);
1015         $this->assertCount(0, $discussions['warnings']);
1016         $this->assertEquals($group->id, $discussions['discussions'][0]['groupid']);
1017         $this->assertEquals($group->id, $discussions['discussions'][1]['groupid']);
1018         $this->assertEquals($group->id, $discussions['discussions'][2]['groupid']);
1020     }
1022     /*
1023      * Test can_add_discussion. A basic test since all the API functions are already covered by unit tests.
1024      */
1025     public function test_can_add_discussion() {
1027         $this->resetAfterTest(true);
1029         // Create courses to add the modules.
1030         $course = self::getDataGenerator()->create_course();
1032         $user = self::getDataGenerator()->create_user();
1034         // First forum with tracking off.
1035         $record = new stdClass();
1036         $record->course = $course->id;
1037         $record->type = 'news';
1038         $forum = self::getDataGenerator()->create_module('forum', $record);
1040         // User with no permissions to add in a news forum.
1041         self::setUser($user);
1042         $this->getDataGenerator()->enrol_user($user->id, $course->id);
1044         $result = mod_forum_external::can_add_discussion($forum->id);
1045         $result = external_api::clean_returnvalue(mod_forum_external::can_add_discussion_returns(), $result);
1046         $this->assertFalse($result['status']);
1048         self::setAdminUser();
1049         $result = mod_forum_external::can_add_discussion($forum->id);
1050         $result = external_api::clean_returnvalue(mod_forum_external::can_add_discussion_returns(), $result);
1051         $this->assertTrue($result['status']);
1053     }