ef2f40e3ef9fc0635068426bf338afbfbe9b1664
[moodle.git] / mod / forum / tests / externallib_test.php
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
18 /**
19  * The module forums external functions unit tests
20  *
21  * @package    mod_forum
22  * @category   external
23  * @copyright  2012 Mark Nelson <markn@moodle.com>
24  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25  */
27 defined('MOODLE_INTERNAL') || die();
29 global $CFG;
31 require_once($CFG->dirroot . '/webservice/tests/helpers.php');
33 class mod_forum_external_testcase extends externallib_advanced_testcase {
35     /**
36      * Tests set up
37      */
38     protected function setUp() {
39         global $CFG;
41         // We must clear the subscription caches. This has to be done both before each test, and after in case of other
42         // tests using these functions.
43         \mod_forum\subscriptions::reset_forum_cache();
45         require_once($CFG->dirroot . '/mod/forum/externallib.php');
46     }
48     public function tearDown() {
49         // We must clear the subscription caches. This has to be done both before each test, and after in case of other
50         // tests using these functions.
51         \mod_forum\subscriptions::reset_forum_cache();
52     }
54     /**
55      * Test get forums
56      */
57     public function test_mod_forum_get_forums_by_courses() {
58         global $USER, $CFG, $DB;
60         $this->resetAfterTest(true);
62         // Create a user.
63         $user = self::getDataGenerator()->create_user();
65         // Set to the user.
66         self::setUser($user);
68         // Create courses to add the modules.
69         $course1 = self::getDataGenerator()->create_course();
70         $course2 = self::getDataGenerator()->create_course();
72         // First forum.
73         $record = new stdClass();
74         $record->introformat = FORMAT_HTML;
75         $record->course = $course1->id;
76         $forum1 = self::getDataGenerator()->create_module('forum', $record);
78         // Second forum.
79         $record = new stdClass();
80         $record->introformat = FORMAT_HTML;
81         $record->course = $course2->id;
82         $forum2 = self::getDataGenerator()->create_module('forum', $record);
84         // Add discussions to the forums.
85         $record = new stdClass();
86         $record->course = $course1->id;
87         $record->userid = $user->id;
88         $record->forum = $forum1->id;
89         $discussion1 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
90         // Expect one discussion.
91         $forum1->numdiscussions = 1;
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;
102         // Check the forum was correctly created.
103         $this->assertEquals(2, $DB->count_records_select('forum', 'id = :forum1 OR id = :forum2',
104                 array('forum1' => $forum1->id, 'forum2' => $forum2->id)));
106         // Enrol the user in two courses.
107         // DataGenerator->enrol_user automatically sets a role for the user with the permission mod/form:viewdiscussion.
108         $this->getDataGenerator()->enrol_user($user->id, $course1->id, null, 'manual');
109         // Execute real Moodle enrolment as we'll call unenrol() method on the instance later.
110         $enrol = enrol_get_plugin('manual');
111         $enrolinstances = enrol_get_instances($course2->id, true);
112         foreach ($enrolinstances as $courseenrolinstance) {
113             if ($courseenrolinstance->enrol == "manual") {
114                 $instance2 = $courseenrolinstance;
115                 break;
116             }
117         }
118         $enrol->enrol_user($instance2, $user->id);
120         // Assign capabilities to view forums for forum 2.
121         $cm2 = get_coursemodule_from_id('forum', $forum2->cmid, 0, false, MUST_EXIST);
122         $context2 = context_module::instance($cm2->id);
123         $newrole = create_role('Role 2', 'role2', 'Role 2 description');
124         $roleid2 = $this->assignUserCapability('mod/forum:viewdiscussion', $context2->id, $newrole);
126         // Create what we expect to be returned when querying the two courses.
127         $expectedforums = array();
128         $expectedforums[$forum1->id] = (array) $forum1;
129         $expectedforums[$forum2->id] = (array) $forum2;
131         // Call the external function passing course ids.
132         $forums = mod_forum_external::get_forums_by_courses(array($course1->id, $course2->id));
133         external_api::clean_returnvalue(mod_forum_external::get_forums_by_courses_returns(), $forums);
134         $this->assertEquals($expectedforums, $forums);
136         // Call the external function without passing course id.
137         $forums = mod_forum_external::get_forums_by_courses();
138         external_api::clean_returnvalue(mod_forum_external::get_forums_by_courses_returns(), $forums);
139         $this->assertEquals($expectedforums, $forums);
141         // Unenrol user from second course and alter expected forums.
142         $enrol->unenrol_user($instance2, $user->id);
143         unset($expectedforums[$forum2->id]);
145         // Call the external function without passing course id.
146         $forums = mod_forum_external::get_forums_by_courses();
147         external_api::clean_returnvalue(mod_forum_external::get_forums_by_courses_returns(), $forums);
148         $this->assertEquals($expectedforums, $forums);
150         // Call for the second course we unenrolled the user from, ensure exception thrown.
151         try {
152             mod_forum_external::get_forums_by_courses(array($course2->id));
153             $this->fail('Exception expected due to being unenrolled from the course.');
154         } catch (moodle_exception $e) {
155             $this->assertEquals('requireloginerror', $e->errorcode);
156         }
158         // Call without required capability, ensure exception thrown.
159         $this->unassignUserCapability('mod/forum:viewdiscussion', null, null, $course1->id);
160         try {
161             $forums = mod_forum_external::get_forums_by_courses(array($course1->id));
162             $this->fail('Exception expected due to missing capability.');
163         } catch (moodle_exception $e) {
164             $this->assertEquals('nopermissions', $e->errorcode);
165         }
166     }
168     /**
169      * Test get forum discussions
170      */
171     public function test_mod_forum_get_forum_discussions() {
172         global $USER, $CFG, $DB;
174         $this->resetAfterTest(true);
176         // Set the CFG variable to allow track forums.
177         $CFG->forum_trackreadposts = true;
179         // Create a user who can track forums.
180         $record = new stdClass();
181         $record->trackforums = true;
182         $user1 = self::getDataGenerator()->create_user($record);
183         // Create a bunch of other users to post.
184         $user2 = self::getDataGenerator()->create_user();
185         $user3 = self::getDataGenerator()->create_user();
186         $user4 = self::getDataGenerator()->create_user();
188         // Set the first created user to the test user.
189         self::setUser($user1);
191         // Create courses to add the modules.
192         $course1 = self::getDataGenerator()->create_course();
193         $course2 = self::getDataGenerator()->create_course();
195         // First forum with tracking off.
196         $record = new stdClass();
197         $record->course = $course1->id;
198         $record->trackingtype = FORUM_TRACKING_OFF;
199         $forum1 = self::getDataGenerator()->create_module('forum', $record);
201         // Second forum of type 'qanda' with tracking enabled.
202         $record = new stdClass();
203         $record->course = $course2->id;
204         $record->type = 'qanda';
205         $record->trackingtype = FORUM_TRACKING_FORCED;
206         $forum2 = self::getDataGenerator()->create_module('forum', $record);
208         // Add discussions to the forums.
209         $record = new stdClass();
210         $record->course = $course1->id;
211         $record->userid = $user1->id;
212         $record->forum = $forum1->id;
213         $discussion1 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
215         $record = new stdClass();
216         $record->course = $course2->id;
217         $record->userid = $user2->id;
218         $record->forum = $forum2->id;
219         $discussion2 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
221         // Add three replies to the discussion 1 from different users.
222         $record = new stdClass();
223         $record->discussion = $discussion1->id;
224         $record->parent = $discussion1->firstpost;
225         $record->userid = $user2->id;
226         $discussion1reply1 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
228         $record->parent = $discussion1reply1->id;
229         $record->userid = $user3->id;
230         $discussion1reply2 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
232         $record->userid = $user4->id;
233         $discussion1reply3 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
235         // Add two replies to discussion 2 from different users.
236         $record = new stdClass();
237         $record->discussion = $discussion2->id;
238         $record->parent = $discussion2->firstpost;
239         $record->userid = $user1->id;
240         $discussion2reply1 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
242         $record->parent = $discussion2reply1->id;
243         $record->userid = $user3->id;
244         $discussion2reply2 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
246         // Check the forums were correctly created.
247         $this->assertEquals(2, $DB->count_records_select('forum', 'id = :forum1 OR id = :forum2',
248                 array('forum1' => $forum1->id, 'forum2' => $forum2->id)));
250         // Check the discussions were correctly created.
251         $this->assertEquals(2, $DB->count_records_select('forum_discussions', 'forum = :forum1 OR forum = :forum2',
252                                                             array('forum1' => $forum1->id, 'forum2' => $forum2->id)));
254         // Check the posts were correctly created, don't forget each discussion created also creates a post.
255         $this->assertEquals(7, $DB->count_records_select('forum_posts', 'discussion = :discussion1 OR discussion = :discussion2',
256                 array('discussion1' => $discussion1->id, 'discussion2' => $discussion2->id)));
258         // Enrol the user in the first course.
259         $enrol = enrol_get_plugin('manual');
260         // Following line enrol and assign default role id to the user.
261         // So the user automatically gets mod/forum:viewdiscussion on all forums of the course.
262         $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
264         // Now enrol into the second course.
265         // We don't use the dataGenerator as we need to get the $instance2 to unenrol later.
266         $enrolinstances = enrol_get_instances($course2->id, true);
267         foreach ($enrolinstances as $courseenrolinstance) {
268             if ($courseenrolinstance->enrol == "manual") {
269                 $instance2 = $courseenrolinstance;
270                 break;
271             }
272         }
273         $enrol->enrol_user($instance2, $user1->id);
275         // Assign capabilities to view discussions for forum 2.
276         $cm = get_coursemodule_from_id('forum', $forum2->cmid, 0, false, MUST_EXIST);
277         $context = context_module::instance($cm->id);
278         $newrole = create_role('Role 2', 'role2', 'Role 2 description');
279         $this->assignUserCapability('mod/forum:viewdiscussion', $context->id, $newrole);
281         // Create what we expect to be returned when querying the forums.
282         $expecteddiscussions = array();
283         $expecteddiscussions[] = array(
284                 'id' => $discussion1->id,
285                 'course' => $discussion1->course,
286                 'forum' => $discussion1->forum,
287                 'name' => $discussion1->name,
288                 'firstpost' => $discussion1->firstpost,
289                 'userid' => $discussion1->userid,
290                 'groupid' => $discussion1->groupid,
291                 'assessed' => $discussion1->assessed,
292                 'timemodified' => $discussion1reply3->created,
293                 'usermodified' => $discussion1reply3->userid,
294                 'timestart' => $discussion1->timestart,
295                 'timeend' => $discussion1->timeend,
296                 'firstuserfullname' => fullname($user1),
297                 'firstuserimagealt' => $user1->imagealt,
298                 'firstuserpicture' => $user1->picture,
299                 'firstuseremail' => $user1->email,
300                 'subject' => $discussion1->name,
301                 'numreplies' => 3,
302                 'numunread' => '',
303                 'lastpost' => $discussion1reply3->id,
304                 'lastuserid' => $user4->id,
305                 'lastuserfullname' => fullname($user4),
306                 'lastuserimagealt' => $user4->imagealt,
307                 'lastuserpicture' => $user4->picture,
308                 'lastuseremail' => $user4->email
309             );
310         $expecteddiscussions[] = array(
311                 'id' => $discussion2->id,
312                 'course' => $discussion2->course,
313                 'forum' => $discussion2->forum,
314                 'name' => $discussion2->name,
315                 'firstpost' => $discussion2->firstpost,
316                 'userid' => $discussion2->userid,
317                 'groupid' => $discussion2->groupid,
318                 'assessed' => $discussion2->assessed,
319                 'timemodified' => $discussion2reply2->created,
320                 'usermodified' => $discussion2reply2->userid,
321                 'timestart' => $discussion2->timestart,
322                 'timeend' => $discussion2->timeend,
323                 'firstuserfullname' => fullname($user2),
324                 'firstuserimagealt' => $user2->imagealt,
325                 'firstuserpicture' => $user2->picture,
326                 'firstuseremail' => $user2->email,
327                 'subject' => $discussion2->name,
328                 'numreplies' => 2,
329                 'numunread' => 3,
330                 'lastpost' => $discussion2reply2->id,
331                 'lastuserid' => $user3->id,
332                 'lastuserfullname' => fullname($user3),
333                 'lastuserimagealt' => $user3->imagealt,
334                 'lastuserpicture' => $user3->picture,
335                 'lastuseremail' => $user3->email
336             );
338         // Call the external function passing forum ids.
339         $discussions = mod_forum_external::get_forum_discussions(array($forum1->id, $forum2->id));
340         $discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_returns(), $discussions);
341         $this->assertEquals($expecteddiscussions, $discussions);
342         // Some debugging is going to be produced, this is because we switch PAGE contexts in the get_forum_discussions function,
343         // the switch happens when the validate_context function is called inside a foreach loop.
344         // See MDL-41746 for more information.
345         $this->assertDebuggingCalled();
347         // Remove the users post from the qanda forum and ensure they can not return the discussion.
348         $DB->delete_records('forum_posts', array('id' => $discussion2reply1->id));
349         $discussions = mod_forum_external::get_forum_discussions(array($forum2->id));
350         $discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_returns(), $discussions);
351         $this->assertEquals(0, count($discussions));
353         // Call without required view discussion capability.
354         $this->unassignUserCapability('mod/forum:viewdiscussion', null, null, $course1->id);
355         try {
356             mod_forum_external::get_forum_discussions(array($forum1->id));
357             $this->fail('Exception expected due to missing capability.');
358         } catch (moodle_exception $e) {
359             $this->assertEquals('nopermissions', $e->errorcode);
360         }
361         $this->assertDebuggingCalled();
363         // Unenrol user from second course.
364         $enrol->unenrol_user($instance2, $user1->id);
366         // Call for the second course we unenrolled the user from, make sure exception thrown.
367         try {
368             mod_forum_external::get_forum_discussions(array($forum2->id));
369             $this->fail('Exception expected due to being unenrolled from the course.');
370         } catch (moodle_exception $e) {
371             $this->assertEquals('requireloginerror', $e->errorcode);
372         }
373     }
375     /**
376      * Test get forum posts
377      */
378     public function test_mod_forum_get_forum_discussion_posts() {
379         global $CFG;
381         $this->resetAfterTest(true);
383         // Set the CFG variable to allow track forums.
384         $CFG->forum_trackreadposts = true;
386         // Create a user who can track forums.
387         $record = new stdClass();
388         $record->trackforums = true;
389         $user1 = self::getDataGenerator()->create_user($record);
390         // Create a bunch of other users to post.
391         $user2 = self::getDataGenerator()->create_user();
392         $user3 = self::getDataGenerator()->create_user();
394         // Set the first created user to the test user.
395         self::setUser($user1);
397         // Create course to add the module.
398         $course1 = self::getDataGenerator()->create_course();
400         // Forum with tracking off.
401         $record = new stdClass();
402         $record->course = $course1->id;
403         $record->trackingtype = FORUM_TRACKING_OFF;
404         $forum1 = self::getDataGenerator()->create_module('forum', $record);
405         $forum1context = context_module::instance($forum1->cmid);
407         // Add discussions to the forums.
408         $record = new stdClass();
409         $record->course = $course1->id;
410         $record->userid = $user1->id;
411         $record->forum = $forum1->id;
412         $discussion1 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
414         $record = new stdClass();
415         $record->course = $course1->id;
416         $record->userid = $user2->id;
417         $record->forum = $forum1->id;
418         $discussion2 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
420         // Add 2 replies to the discussion 1 from different users.
421         $record = new stdClass();
422         $record->discussion = $discussion1->id;
423         $record->parent = $discussion1->firstpost;
424         $record->userid = $user2->id;
425         $discussion1reply1 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
427         $record->parent = $discussion1reply1->id;
428         $record->userid = $user3->id;
429         $discussion1reply2 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
431         // Enrol the user in the  course.
432         $enrol = enrol_get_plugin('manual');
433         // Following line enrol and assign default role id to the user.
434         // So the user automatically gets mod/forum:viewdiscussion on all forums of the course.
435         $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
436         $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
438         // Delete one user, to test that we still receive posts by this user.
439         delete_user($user3);
441         // Create what we expect to be returned when querying the discussion.
442         $expectedposts = array(
443             'posts' => array(),
444             'warnings' => array(),
445         );
447         // Empty picture since it's a user deleted (user3).
448         $userpictureurl = '';
450         $expectedposts['posts'][] = array(
451             'id' => $discussion1reply2->id,
452             'discussion' => $discussion1reply2->discussion,
453             'parent' => $discussion1reply2->parent,
454             'userid' => (int) $discussion1reply2->userid,
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),
461             'messageformat' => 1,   // This value is usually changed by external_format_text() function.
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,
469             'userfullname' => fullname($user3),
470             'userpictureurl' => $userpictureurl
471         );
473         $userpictureurl = moodle_url::make_webservice_pluginfile_url(
474             context_user::instance($discussion1reply1->userid)->id, 'user', 'icon', null, '/', 'f1')->out(false);
476         $expectedposts['posts'][] = array(
477             'id' => $discussion1reply1->id,
478             'discussion' => $discussion1reply1->discussion,
479             'parent' => $discussion1reply1->parent,
480             'userid' => (int) $discussion1reply1->userid,
481             'created' => $discussion1reply1->created,
482             'modified' => $discussion1reply1->modified,
483             'mailed' => $discussion1reply1->mailed,
484             'subject' => $discussion1reply1->subject,
485             'message' => file_rewrite_pluginfile_urls($discussion1reply1->message, 'pluginfile.php',
486                     $forum1context->id, 'mod_forum', 'post', $discussion1reply1->id),
487             'messageformat' => 1,   // This value is usually changed by external_format_text() function.
488             'messagetrust' => $discussion1reply1->messagetrust,
489             'attachment' => $discussion1reply1->attachment,
490             'totalscore' => $discussion1reply1->totalscore,
491             'mailnow' => $discussion1reply1->mailnow,
492             'children' => array($discussion1reply2->id),
493             'canreply' => true,
494             'postread' => false,
495             'userfullname' => fullname($user2),
496             'userpictureurl' => $userpictureurl
497         );
499         // Test a discussion with two additional posts (total 3 posts).
500         $posts = mod_forum_external::get_forum_discussion_posts($discussion1->id, 'modified', 'DESC');
501         $posts = external_api::clean_returnvalue(mod_forum_external::get_forum_discussion_posts_returns(), $posts);
502         $this->assertEquals(3, count($posts['posts']));
504         // Unset the initial discussion post.
505         array_pop($posts['posts']);
506         $this->assertEquals($expectedposts, $posts);
508         // Test discussion without additional posts. There should be only one post (the one created by the discussion).
509         $posts = mod_forum_external::get_forum_discussion_posts($discussion2->id, 'modified', 'DESC');
510         $posts = external_api::clean_returnvalue(mod_forum_external::get_forum_discussion_posts_returns(), $posts);
511         $this->assertEquals(1, count($posts['posts']));
513     }
515     /**
516      * Test get forum discussions paginated
517      */
518     public function test_mod_forum_get_forum_discussions_paginated() {
519         global $USER, $CFG, $DB;
521         $this->resetAfterTest(true);
523         // Set the CFG variable to allow track forums.
524         $CFG->forum_trackreadposts = true;
526         // Create a user who can track forums.
527         $record = new stdClass();
528         $record->trackforums = true;
529         $user1 = self::getDataGenerator()->create_user($record);
530         // Create a bunch of other users to post.
531         $user2 = self::getDataGenerator()->create_user();
532         $user3 = self::getDataGenerator()->create_user();
533         $user4 = self::getDataGenerator()->create_user();
535         // Set the first created user to the test user.
536         self::setUser($user1);
538         // Create courses to add the modules.
539         $course1 = self::getDataGenerator()->create_course();
541         // First forum with tracking off.
542         $record = new stdClass();
543         $record->course = $course1->id;
544         $record->trackingtype = FORUM_TRACKING_OFF;
545         $forum1 = self::getDataGenerator()->create_module('forum', $record);
547         // Add discussions to the forums.
548         $record = new stdClass();
549         $record->course = $course1->id;
550         $record->userid = $user1->id;
551         $record->forum = $forum1->id;
552         $discussion1 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
554         // Add three replies to the discussion 1 from different users.
555         $record = new stdClass();
556         $record->discussion = $discussion1->id;
557         $record->parent = $discussion1->firstpost;
558         $record->userid = $user2->id;
559         $discussion1reply1 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
561         $record->parent = $discussion1reply1->id;
562         $record->userid = $user3->id;
563         $discussion1reply2 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
565         $record->userid = $user4->id;
566         $discussion1reply3 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
568         // Enrol the user in the first course.
569         $enrol = enrol_get_plugin('manual');
571         // We don't use the dataGenerator as we need to get the $instance2 to unenrol later.
572         $enrolinstances = enrol_get_instances($course1->id, true);
573         foreach ($enrolinstances as $courseenrolinstance) {
574             if ($courseenrolinstance->enrol == "manual") {
575                 $instance1 = $courseenrolinstance;
576                 break;
577             }
578         }
579         $enrol->enrol_user($instance1, $user1->id);
581         // Delete one user.
582         delete_user($user4);
584         // Assign capabilities to view discussions for forum 1.
585         $cm = get_coursemodule_from_id('forum', $forum1->cmid, 0, false, MUST_EXIST);
586         $context = context_module::instance($cm->id);
587         $newrole = create_role('Role 2', 'role2', 'Role 2 description');
588         $this->assignUserCapability('mod/forum:viewdiscussion', $context->id, $newrole);
590         // Create what we expect to be returned when querying the forums.
592         $post1 = $DB->get_record('forum_posts', array('id' => $discussion1->firstpost), '*', MUST_EXIST);
593         $userpictureurl = moodle_url::make_webservice_pluginfile_url(
594                     context_user::instance($user1->id)->id, 'user', 'icon', null, '/', 'f1');
596         // We expect an empty URL since we deleted the user4.
597         $usermodifiedpictureurl = '';
599         $expecteddiscussions = array(
600                 'id' => $discussion1->firstpost,
601                 'name' => $discussion1->name,
602                 'groupid' => $discussion1->groupid,
603                 'timemodified' => $discussion1reply3->created,
604                 'usermodified' => $discussion1reply3->userid,
605                 'timestart' => $discussion1->timestart,
606                 'timeend' => $discussion1->timeend,
607                 'discussion' => $discussion1->id,
608                 'parent' => 0,
609                 'userid' => $discussion1->userid,
610                 'created' => $post1->created,
611                 'modified' => $post1->modified,
612                 'mailed' => $post1->mailed,
613                 'subject' => $post1->subject,
614                 'message' => $post1->message,
615                 'messageformat' => $post1->messageformat,
616                 'messagetrust' => $post1->messagetrust,
617                 'attachment' => $post1->attachment,
618                 'totalscore' => $post1->totalscore,
619                 'mailnow' => $post1->mailnow,
620                 'userfullname' => fullname($user1),
621                 'usermodifiedfullname' => fullname($user4),
622                 'userpictureurl' => $userpictureurl,
623                 'usermodifiedpictureurl' => $usermodifiedpictureurl,
624                 'numreplies' => 3,
625                 'numunread' => 0
626             );
628         // Call the external function passing forum id.
629         $discussions = mod_forum_external::get_forum_discussions_paginated($forum1->id);
630         $discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_paginated_returns(), $discussions);
631         $expectedreturn = array(
632             'discussions' => array($expecteddiscussions),
633             'warnings' => array()
634         );
635         $this->assertEquals($expectedreturn, $discussions);
637         // Call without required view discussion capability.
638         $this->unassignUserCapability('mod/forum:viewdiscussion', $context->id, $newrole);
639         try {
640             mod_forum_external::get_forum_discussions_paginated($forum1->id);
641             $this->fail('Exception expected due to missing capability.');
642         } catch (moodle_exception $e) {
643             $this->assertEquals('noviewdiscussionspermission', $e->errorcode);
644         }
646         // Unenrol user from second course.
647         $enrol->unenrol_user($instance1, $user1->id);
649         // Call for the second course we unenrolled the user from, make sure exception thrown.
650         try {
651             mod_forum_external::get_forum_discussions_paginated($forum1->id);
652             $this->fail('Exception expected due to being unenrolled from the course.');
653         } catch (moodle_exception $e) {
654             $this->assertEquals('requireloginerror', $e->errorcode);
655         }
656     }