MDL-66625 forumreport_summary: Adding behat
[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');
31 require_once($CFG->dirroot . '/mod/forum/lib.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(array('trackforums' => 1));
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         $record->trackingtype = FORUM_TRACKING_FORCED;
77         $forum1 = self::getDataGenerator()->create_module('forum', $record);
79         // Second forum.
80         $record = new stdClass();
81         $record->introformat = FORMAT_HTML;
82         $record->course = $course2->id;
83         $record->trackingtype = FORUM_TRACKING_OFF;
84         $forum2 = self::getDataGenerator()->create_module('forum', $record);
85         $forum2->introfiles = [];
87         // Add discussions to the forums.
88         $record = new stdClass();
89         $record->course = $course1->id;
90         $record->userid = $user->id;
91         $record->forum = $forum1->id;
92         $discussion1 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
93         // Expect one discussion.
94         $forum1->numdiscussions = 1;
95         $forum1->cancreatediscussions = true;
96         $forum1->istracked = true;
97         $forum1->unreadpostscount = 0;
98         $forum1->introfiles = [];
100         $record = new stdClass();
101         $record->course = $course2->id;
102         $record->userid = $user->id;
103         $record->forum = $forum2->id;
104         $discussion2 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
105         $discussion3 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
106         // Expect two discussions.
107         $forum2->numdiscussions = 2;
108         // Default limited role, no create discussion capability enabled.
109         $forum2->cancreatediscussions = false;
110         $forum2->istracked = false;
112         // Check the forum was correctly created.
113         $this->assertEquals(2, $DB->count_records_select('forum', 'id = :forum1 OR id = :forum2',
114                 array('forum1' => $forum1->id, 'forum2' => $forum2->id)));
116         // Enrol the user in two courses.
117         // DataGenerator->enrol_user automatically sets a role for the user with the permission mod/form:viewdiscussion.
118         $this->getDataGenerator()->enrol_user($user->id, $course1->id, null, 'manual');
119         // Execute real Moodle enrolment as we'll call unenrol() method on the instance later.
120         $enrol = enrol_get_plugin('manual');
121         $enrolinstances = enrol_get_instances($course2->id, true);
122         foreach ($enrolinstances as $courseenrolinstance) {
123             if ($courseenrolinstance->enrol == "manual") {
124                 $instance2 = $courseenrolinstance;
125                 break;
126             }
127         }
128         $enrol->enrol_user($instance2, $user->id);
130         // Assign capabilities to view forums for forum 2.
131         $cm2 = get_coursemodule_from_id('forum', $forum2->cmid, 0, false, MUST_EXIST);
132         $context2 = context_module::instance($cm2->id);
133         $newrole = create_role('Role 2', 'role2', 'Role 2 description');
134         $roleid2 = $this->assignUserCapability('mod/forum:viewdiscussion', $context2->id, $newrole);
136         // Create what we expect to be returned when querying the two courses.
137         unset($forum1->displaywordcount);
138         unset($forum2->displaywordcount);
140         $expectedforums = array();
141         $expectedforums[$forum1->id] = (array) $forum1;
142         $expectedforums[$forum2->id] = (array) $forum2;
144         // Call the external function passing course ids.
145         $forums = mod_forum_external::get_forums_by_courses(array($course1->id, $course2->id));
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         // Call the external function without passing course id.
153         $forums = mod_forum_external::get_forums_by_courses();
154         $forums = external_api::clean_returnvalue(mod_forum_external::get_forums_by_courses_returns(), $forums);
155         $this->assertCount(2, $forums);
156         foreach ($forums as $forum) {
157             $this->assertEquals($expectedforums[$forum['id']], $forum);
158         }
160         // Unenrol user from second course and alter expected forums.
161         $enrol->unenrol_user($instance2, $user->id);
162         unset($expectedforums[$forum2->id]);
164         // Call the external function without passing course 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->assertCount(1, $forums);
168         $this->assertEquals($expectedforums[$forum1->id], $forums[0]);
169         $this->assertTrue($forums[0]['cancreatediscussions']);
171         // Change the type of the forum, the user shouldn't be able to add discussions.
172         $DB->set_field('forum', 'type', 'news', array('id' => $forum1->id));
173         $forums = mod_forum_external::get_forums_by_courses();
174         $forums = external_api::clean_returnvalue(mod_forum_external::get_forums_by_courses_returns(), $forums);
175         $this->assertFalse($forums[0]['cancreatediscussions']);
177         // Call for the second course we unenrolled the user from.
178         $forums = mod_forum_external::get_forums_by_courses(array($course2->id));
179         $forums = external_api::clean_returnvalue(mod_forum_external::get_forums_by_courses_returns(), $forums);
180         $this->assertCount(0, $forums);
181     }
183     /**
184      * Test the toggle favourite state
185      */
186     public function test_mod_forum_toggle_favourite_state() {
187         global $USER, $CFG, $DB;
189         $this->resetAfterTest(true);
191         // Create a user.
192         $user = self::getDataGenerator()->create_user(array('trackforums' => 1));
194         // Set to the user.
195         self::setUser($user);
197         // Create courses to add the modules.
198         $course1 = self::getDataGenerator()->create_course();
199         $this->getDataGenerator()->enrol_user($user->id, $course1->id);
201         $record = new stdClass();
202         $record->introformat = FORMAT_HTML;
203         $record->course = $course1->id;
204         $record->trackingtype = FORUM_TRACKING_OFF;
205         $forum1 = self::getDataGenerator()->create_module('forum', $record);
206         $forum1->introfiles = [];
208         // Add discussions to the forums.
209         $record = new stdClass();
210         $record->course = $course1->id;
211         $record->userid = $user->id;
212         $record->forum = $forum1->id;
213         $discussion1 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
215         $response = mod_forum_external::toggle_favourite_state($discussion1->id, 1);
216         $response = external_api::clean_returnvalue(mod_forum_external::toggle_favourite_state_returns(), $response);
217         $this->assertTrue($response['userstate']['favourited']);
219         $response = mod_forum_external::toggle_favourite_state($discussion1->id, 0);
220         $response = external_api::clean_returnvalue(mod_forum_external::toggle_favourite_state_returns(), $response);
221         $this->assertFalse($response['userstate']['favourited']);
223         $this->setUser(0);
224         try {
225             $response = mod_forum_external::toggle_favourite_state($discussion1->id, 0);
226         } catch (moodle_exception $e) {
227             $this->assertEquals('requireloginerror', $e->errorcode);
228         }
229     }
231     /**
232      * Test the toggle pin state
233      */
234     public function test_mod_forum_set_pin_state() {
235         $this->resetAfterTest(true);
237         // Create a user.
238         $user = self::getDataGenerator()->create_user(array('trackforums' => 1));
240         // Set to the user.
241         self::setUser($user);
243         // Create courses to add the modules.
244         $course1 = self::getDataGenerator()->create_course();
245         $this->getDataGenerator()->enrol_user($user->id, $course1->id);
247         $record = new stdClass();
248         $record->introformat = FORMAT_HTML;
249         $record->course = $course1->id;
250         $record->trackingtype = FORUM_TRACKING_OFF;
251         $forum1 = self::getDataGenerator()->create_module('forum', $record);
252         $forum1->introfiles = [];
254         // Add discussions to the forums.
255         $record = new stdClass();
256         $record->course = $course1->id;
257         $record->userid = $user->id;
258         $record->forum = $forum1->id;
259         $discussion1 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
261         try {
262             $response = mod_forum_external::set_pin_state($discussion1->id, 1);
263         } catch (Exception $e) {
264             $this->assertEquals('cannotpindiscussions', $e->errorcode);
265         }
267         self::setAdminUser();
268         $response = mod_forum_external::set_pin_state($discussion1->id, 1);
269         $response = external_api::clean_returnvalue(mod_forum_external::set_pin_state_returns(), $response);
270         $this->assertTrue($response['pinned']);
272         $response = mod_forum_external::set_pin_state($discussion1->id, 0);
273         $response = external_api::clean_returnvalue(mod_forum_external::set_pin_state_returns(), $response);
274         $this->assertFalse($response['pinned']);
275     }
277     /**
278      * Test get forum posts
279      */
280     public function test_mod_forum_get_forum_discussion_posts() {
281         global $CFG, $PAGE;
283         $this->resetAfterTest(true);
285         // Set the CFG variable to allow track forums.
286         $CFG->forum_trackreadposts = true;
288         // Create a user who can track forums.
289         $record = new stdClass();
290         $record->trackforums = true;
291         $user1 = self::getDataGenerator()->create_user($record);
292         // Create a bunch of other users to post.
293         $user2 = self::getDataGenerator()->create_user();
294         $user3 = self::getDataGenerator()->create_user();
296         // Set the first created user to the test user.
297         self::setUser($user1);
299         // Create course to add the module.
300         $course1 = self::getDataGenerator()->create_course();
302         // Forum with tracking off.
303         $record = new stdClass();
304         $record->course = $course1->id;
305         $record->trackingtype = FORUM_TRACKING_OFF;
306         $forum1 = self::getDataGenerator()->create_module('forum', $record);
307         $forum1context = context_module::instance($forum1->cmid);
309         // Forum with tracking enabled.
310         $record = new stdClass();
311         $record->course = $course1->id;
312         $forum2 = self::getDataGenerator()->create_module('forum', $record);
313         $forum2cm = get_coursemodule_from_id('forum', $forum2->cmid);
314         $forum2context = context_module::instance($forum2->cmid);
316         // Add discussions to the forums.
317         $record = new stdClass();
318         $record->course = $course1->id;
319         $record->userid = $user1->id;
320         $record->forum = $forum1->id;
321         $discussion1 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
323         $record = new stdClass();
324         $record->course = $course1->id;
325         $record->userid = $user2->id;
326         $record->forum = $forum1->id;
327         $discussion2 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
329         $record = new stdClass();
330         $record->course = $course1->id;
331         $record->userid = $user2->id;
332         $record->forum = $forum2->id;
333         $discussion3 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
335         // Add 2 replies to the discussion 1 from different users.
336         $record = new stdClass();
337         $record->discussion = $discussion1->id;
338         $record->parent = $discussion1->firstpost;
339         $record->userid = $user2->id;
340         $discussion1reply1 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
341         $filename = 'shouldbeanimage.jpg';
342         // Add a fake inline image to the post.
343         $filerecordinline = array(
344             'contextid' => $forum1context->id,
345             'component' => 'mod_forum',
346             'filearea'  => 'post',
347             'itemid'    => $discussion1reply1->id,
348             'filepath'  => '/',
349             'filename'  => $filename,
350         );
351         $fs = get_file_storage();
352         $timepost = time();
353         $fs->create_file_from_string($filerecordinline, 'image contents (not really)');
355         $record->parent = $discussion1reply1->id;
356         $record->userid = $user3->id;
357         $record->tags = array('Cats', 'Dogs');
358         $discussion1reply2 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
360         // Enrol the user in the  course.
361         $enrol = enrol_get_plugin('manual');
362         // Following line enrol and assign default role id to the user.
363         // So the user automatically gets mod/forum:viewdiscussion on all forums of the course.
364         $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
365         $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
367         // Delete one user, to test that we still receive posts by this user.
368         delete_user($user3);
370         // Create what we expect to be returned when querying the discussion.
371         $expectedposts = array(
372             'posts' => array(),
373             'ratinginfo' => array(
374                 'contextid' => $forum1context->id,
375                 'component' => 'mod_forum',
376                 'ratingarea' => 'post',
377                 'canviewall' => null,
378                 'canviewany' => null,
379                 'scales' => array(),
380                 'ratings' => array(),
381             ),
382             'warnings' => array(),
383         );
385         // User pictures are initially empty, we should get the links once the external function is called.
386         $expectedposts['posts'][] = array(
387             'id' => $discussion1reply2->id,
388             'discussion' => $discussion1reply2->discussion,
389             'parent' => $discussion1reply2->parent,
390             'userid' => (int) $discussion1reply2->userid,
391             'created' => $discussion1reply2->created,
392             'modified' => $discussion1reply2->modified,
393             'mailed' => $discussion1reply2->mailed,
394             'subject' => $discussion1reply2->subject,
395             'message' => file_rewrite_pluginfile_urls($discussion1reply2->message, 'pluginfile.php',
396                     $forum1context->id, 'mod_forum', 'post', $discussion1reply2->id),
397             'messageformat' => 1,   // This value is usually changed by external_format_text() function.
398             'messagetrust' => $discussion1reply2->messagetrust,
399             'attachment' => $discussion1reply2->attachment,
400             'totalscore' => $discussion1reply2->totalscore,
401             'mailnow' => $discussion1reply2->mailnow,
402             'children' => array(),
403             'canreply' => true,
404             'postread' => false,
405             'userfullname' => fullname($user3),
406             'userpictureurl' => '',
407             'deleted' => false,
408             'isprivatereply' => false,
409             'tags' => \core_tag\external\util::get_item_tags('mod_forum', 'forum_posts', $discussion1reply2->id),
410         );
411         // Cast to expected.
412         $this->assertCount(2, $expectedposts['posts'][0]['tags']);
413         $expectedposts['posts'][0]['tags'][0]['isstandard'] = (bool) $expectedposts['posts'][0]['tags'][0]['isstandard'];
414         $expectedposts['posts'][0]['tags'][1]['isstandard'] = (bool) $expectedposts['posts'][0]['tags'][1]['isstandard'];
416         $expectedposts['posts'][] = array(
417             'id' => $discussion1reply1->id,
418             'discussion' => $discussion1reply1->discussion,
419             'parent' => $discussion1reply1->parent,
420             'userid' => (int) $discussion1reply1->userid,
421             'created' => $discussion1reply1->created,
422             'modified' => $discussion1reply1->modified,
423             'mailed' => $discussion1reply1->mailed,
424             'subject' => $discussion1reply1->subject,
425             'message' => file_rewrite_pluginfile_urls($discussion1reply1->message, 'pluginfile.php',
426                     $forum1context->id, 'mod_forum', 'post', $discussion1reply1->id),
427             'messageformat' => 1,   // This value is usually changed by external_format_text() function.
428             'messagetrust' => $discussion1reply1->messagetrust,
429             'attachment' => $discussion1reply1->attachment,
430             'messageinlinefiles' => array(
431                 array(
432                     'filename' => $filename,
433                     'filepath' => '/',
434                     'filesize' => '27',
435                     'fileurl' => moodle_url::make_webservice_pluginfile_url($forum1context->id, 'mod_forum', 'post',
436                                     $discussion1reply1->id, '/', $filename),
437                     'timemodified' => $timepost,
438                     'mimetype' => 'image/jpeg',
439                     'isexternalfile' => false,
440                 )
441             ),
442             'totalscore' => $discussion1reply1->totalscore,
443             'mailnow' => $discussion1reply1->mailnow,
444             'children' => array($discussion1reply2->id),
445             'canreply' => true,
446             'postread' => false,
447             'userfullname' => fullname($user2),
448             'userpictureurl' => '',
449             'deleted' => false,
450             'isprivatereply' => false,
451             'tags' => array(),
452         );
454         // Test a discussion with two additional posts (total 3 posts).
455         $posts = mod_forum_external::get_forum_discussion_posts($discussion1->id, 'modified', 'DESC');
456         $posts = external_api::clean_returnvalue(mod_forum_external::get_forum_discussion_posts_returns(), $posts);
457         $this->assertEquals(3, count($posts['posts']));
459         // Generate here the pictures because we need to wait to the external function to init the theme.
460         $userpicture = new user_picture($user3);
461         $userpicture->size = 1; // Size f1.
462         $expectedposts['posts'][0]['userpictureurl'] = $userpicture->get_url($PAGE)->out(false);
464         $userpicture = new user_picture($user2);
465         $userpicture->size = 1; // Size f1.
466         $expectedposts['posts'][1]['userpictureurl'] = $userpicture->get_url($PAGE)->out(false);
468         // Unset the initial discussion post.
469         array_pop($posts['posts']);
470         $this->assertEquals($expectedposts, $posts);
472         // Check we receive the unread count correctly on tracked forum.
473         forum_tp_count_forum_unread_posts($forum2cm, $course1, true);    // Reset static cache.
474         $result = mod_forum_external::get_forums_by_courses(array($course1->id));
475         $result = external_api::clean_returnvalue(mod_forum_external::get_forums_by_courses_returns(), $result);
476         foreach ($result as $f) {
477             if ($f['id'] == $forum2->id) {
478                 $this->assertEquals(1, $f['unreadpostscount']);
479             }
480         }
482         // Test discussion without additional posts. There should be only one post (the one created by the discussion).
483         $posts = mod_forum_external::get_forum_discussion_posts($discussion2->id, 'modified', 'DESC');
484         $posts = external_api::clean_returnvalue(mod_forum_external::get_forum_discussion_posts_returns(), $posts);
485         $this->assertEquals(1, count($posts['posts']));
487         // Test discussion tracking on not tracked forum.
488         $result = mod_forum_external::view_forum_discussion($discussion1->id);
489         $result = external_api::clean_returnvalue(mod_forum_external::view_forum_discussion_returns(), $result);
490         $this->assertTrue($result['status']);
491         $this->assertEmpty($result['warnings']);
493         // Test posts have not been marked as read.
494         $posts = mod_forum_external::get_forum_discussion_posts($discussion1->id, 'modified', 'DESC');
495         $posts = external_api::clean_returnvalue(mod_forum_external::get_forum_discussion_posts_returns(), $posts);
496         foreach ($posts['posts'] as $post) {
497             $this->assertFalse($post['postread']);
498         }
500         // Test discussion tracking on tracked forum.
501         $result = mod_forum_external::view_forum_discussion($discussion3->id);
502         $result = external_api::clean_returnvalue(mod_forum_external::view_forum_discussion_returns(), $result);
503         $this->assertTrue($result['status']);
504         $this->assertEmpty($result['warnings']);
506         // Test posts have been marked as read.
507         $posts = mod_forum_external::get_forum_discussion_posts($discussion3->id, 'modified', 'DESC');
508         $posts = external_api::clean_returnvalue(mod_forum_external::get_forum_discussion_posts_returns(), $posts);
509         foreach ($posts['posts'] as $post) {
510             $this->assertTrue($post['postread']);
511         }
513         // Check we receive 0 unread posts.
514         forum_tp_count_forum_unread_posts($forum2cm, $course1, true);    // Reset static cache.
515         $result = mod_forum_external::get_forums_by_courses(array($course1->id));
516         $result = external_api::clean_returnvalue(mod_forum_external::get_forums_by_courses_returns(), $result);
517         foreach ($result as $f) {
518             if ($f['id'] == $forum2->id) {
519                 $this->assertEquals(0, $f['unreadpostscount']);
520             }
521         }
522     }
524     /**
525      * Test get forum posts
526      *
527      * Tests is similar to the get_forum_discussion_posts only utilizing the new return structure and entities
528      */
529     public function test_mod_forum_get_discussion_posts() {
530         global $CFG, $PAGE;
532         $this->resetAfterTest(true);
534         // Set the CFG variable to allow track forums.
535         $CFG->forum_trackreadposts = true;
537         $urlfactory = mod_forum\local\container::get_url_factory();
538         $legacyfactory = mod_forum\local\container::get_legacy_data_mapper_factory();
539         $entityfactory = mod_forum\local\container::get_entity_factory();
541         // Create a user who can track forums.
542         $record = new stdClass();
543         $record->trackforums = true;
544         $user1 = self::getDataGenerator()->create_user($record);
545         // Create a bunch of other users to post.
546         $user2 = self::getDataGenerator()->create_user();
547         $user2entity = $entityfactory->get_author_from_stdclass($user2);
548         $exporteduser2 = [
549             'id' => (int) $user2->id,
550             'fullname' => fullname($user2),
551             'isdeleted' => false,
552             'groups' => [],
553             'urls' => [
554                 'profile' => $urlfactory->get_author_profile_url($user2entity),
555                 'profileimage' => $urlfactory->get_author_profile_image_url($user2entity),
556             ]
557         ];
558         $user2->fullname = $exporteduser2['fullname'];
560         $user3 = self::getDataGenerator()->create_user(['fullname' => "Mr Pants 1"]);
561         $user3entity = $entityfactory->get_author_from_stdclass($user3);
562         $exporteduser3 = [
563             'id' => (int) $user3->id,
564             'fullname' => fullname($user3),
565             'groups' => [],
566             'isdeleted' => false,
567             'urls' => [
568                 'profile' => $urlfactory->get_author_profile_url($user3entity),
569                 'profileimage' => $urlfactory->get_author_profile_image_url($user3entity),
570             ]
571         ];
572         $user3->fullname = $exporteduser3['fullname'];
573         $forumgenerator = self::getDataGenerator()->get_plugin_generator('mod_forum');
575         // Set the first created user to the test user.
576         self::setUser($user1);
578         // Create course to add the module.
579         $course1 = self::getDataGenerator()->create_course();
581         // Forum with tracking off.
582         $record = new stdClass();
583         $record->course = $course1->id;
584         $record->trackingtype = FORUM_TRACKING_OFF;
585         $forum1 = self::getDataGenerator()->create_module('forum', $record);
586         $forum1context = context_module::instance($forum1->cmid);
588         // Forum with tracking enabled.
589         $record = new stdClass();
590         $record->course = $course1->id;
591         $forum2 = self::getDataGenerator()->create_module('forum', $record);
592         $forum2cm = get_coursemodule_from_id('forum', $forum2->cmid);
593         $forum2context = context_module::instance($forum2->cmid);
595         // Add discussions to the forums.
596         $record = new stdClass();
597         $record->course = $course1->id;
598         $record->userid = $user1->id;
599         $record->forum = $forum1->id;
600         $discussion1 = $forumgenerator->create_discussion($record);
602         $record = new stdClass();
603         $record->course = $course1->id;
604         $record->userid = $user2->id;
605         $record->forum = $forum1->id;
606         $discussion2 = $forumgenerator->create_discussion($record);
608         $record = new stdClass();
609         $record->course = $course1->id;
610         $record->userid = $user2->id;
611         $record->forum = $forum2->id;
612         $discussion3 = $forumgenerator->create_discussion($record);
614         // Add 2 replies to the discussion 1 from different users.
615         $record = new stdClass();
616         $record->discussion = $discussion1->id;
617         $record->parent = $discussion1->firstpost;
618         $record->userid = $user2->id;
619         $discussion1reply1 = $forumgenerator->create_post($record);
620         $filename = 'shouldbeanimage.jpg';
621         // Add a fake inline image to the post.
622         $filerecordinline = array(
623             'contextid' => $forum1context->id,
624             'component' => 'mod_forum',
625             'filearea'  => 'post',
626             'itemid'    => $discussion1reply1->id,
627             'filepath'  => '/',
628             'filename'  => $filename,
629         );
630         $fs = get_file_storage();
631         $timepost = time();
632         $fs->create_file_from_string($filerecordinline, 'image contents (not really)');
634         $record->parent = $discussion1reply1->id;
635         $record->userid = $user3->id;
636         $discussion1reply2 = $forumgenerator->create_post($record);
638         // Enrol the user in the  course.
639         $enrol = enrol_get_plugin('manual');
640         // Following line enrol and assign default role id to the user.
641         // So the user automatically gets mod/forum:viewdiscussion on all forums of the course.
642         $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
643         $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
645         // Delete one user, to test that we still receive posts by this user.
646         delete_user($user3);
647         $exporteduser3 = [
648             'id' => (int) $user3->id,
649             'fullname' => get_string('deleteduser', 'mod_forum'),
650             'groups' => [],
651             'isdeleted' => true,
652             'urls' => [
653                 'profile' => $urlfactory->get_author_profile_url($user3entity),
654                 'profileimage' => $urlfactory->get_author_profile_image_url($user3entity),
655             ]
656         ];
658         // Create what we expect to be returned when querying the discussion.
659         $expectedposts = array(
660             'posts' => array(),
661             'ratinginfo' => array(
662                 'contextid' => $forum1context->id,
663                 'component' => 'mod_forum',
664                 'ratingarea' => 'post',
665                 'canviewall' => null,
666                 'canviewany' => null,
667                 'scales' => array(),
668                 'ratings' => array(),
669             ),
670             'warnings' => array(),
671         );
673         // User pictures are initially empty, we should get the links once the external function is called.
674         $isolatedurl = $urlfactory->get_discussion_view_url_from_discussion_id($discussion1reply2->discussion);
675         $isolatedurl->params(['parent' => $discussion1reply2->id]);
676         $expectedposts['posts'][] = array(
677             'id' => $discussion1reply2->id,
678             'discussionid' => $discussion1reply2->discussion,
679             'parentid' => $discussion1reply2->parent,
680             'hasparent' => true,
681             'timecreated' => $discussion1reply2->created,
682             'subject' => $discussion1reply2->subject,
683             'replysubject' => get_string('re', 'mod_forum') . " {$discussion1reply2->subject}",
684             'message' => file_rewrite_pluginfile_urls($discussion1reply2->message, 'pluginfile.php',
685                     $forum1context->id, 'mod_forum', 'post', $discussion1reply2->id),
686             'messageformat' => 1,   // This value is usually changed by external_format_text() function.
687             'unread' => null,
688             'isdeleted' => false,
689             'isprivatereply' => false,
690             'haswordcount' => false,
691             'wordcount' => null,
692             'author'=> $exporteduser3,
693             'attachments' => [],
694             'tags' => [],
695             'html' => [
696                 'rating' => null,
697                 'taglist' => null,
698                 'authorsubheading' => $forumgenerator->get_author_subheading_html((object)$exporteduser3, $discussion1reply2->created)
699             ],
700             'capabilities' => [
701                 'view' => 1,
702                 'edit' => 0,
703                 'delete' => 0,
704                 'split' => 0,
705                 'reply' => 1,
706                 'export' => 0,
707                 'controlreadstatus' => 0,
708                 'canreplyprivately' => 0,
709                 'selfenrol' => 0
710             ],
711             'urls' => [
712                 'view' => $urlfactory->get_view_post_url_from_post_id($discussion1reply2->discussion, $discussion1reply2->id),
713                 'viewisolated' => $isolatedurl->out(false),
714                 'viewparent' => $urlfactory->get_view_post_url_from_post_id($discussion1reply2->discussion, $discussion1reply2->parent),
715                 'edit' => null,
716                 'delete' =>null,
717                 'split' => null,
718                 'reply' => (new moodle_url('/mod/forum/post.php#mformforum', [
719                     'reply' => $discussion1reply2->id
720                 ]))->out(false),
721                 'export' => null,
722                 'markasread' => null,
723                 'markasunread' => null,
724                 'discuss' => $urlfactory->get_discussion_view_url_from_discussion_id($discussion1reply2->discussion),
725             ],
726         );
729         $isolatedurl = $urlfactory->get_discussion_view_url_from_discussion_id($discussion1reply1->discussion);
730         $isolatedurl->params(['parent' => $discussion1reply1->id]);
731         $expectedposts['posts'][] = array(
732             'id' => $discussion1reply1->id,
733             'discussionid' => $discussion1reply1->discussion,
734             'parentid' => $discussion1reply1->parent,
735             'hasparent' => true,
736             'timecreated' => $discussion1reply1->created,
737             'subject' => $discussion1reply1->subject,
738             'replysubject' => get_string('re', 'mod_forum') . " {$discussion1reply1->subject}",
739             'message' => file_rewrite_pluginfile_urls($discussion1reply1->message, 'pluginfile.php',
740                     $forum1context->id, 'mod_forum', 'post', $discussion1reply1->id),
741             'messageformat' => 1,   // This value is usually changed by external_format_text() function.
742             'unread' => null,
743             'isdeleted' => false,
744             'isprivatereply' => false,
745             'haswordcount' => false,
746             'wordcount' => null,
747             'author'=> $exporteduser2,
748             'attachments' => [],
749             'tags' => [],
750             'html' => [
751                 'rating' => null,
752                 'taglist' => null,
753                 'authorsubheading' => $forumgenerator->get_author_subheading_html((object)$exporteduser2, $discussion1reply1->created)
754             ],
755             'capabilities' => [
756                 'view' => 1,
757                 'edit' => 0,
758                 'delete' => 0,
759                 'split' => 0,
760                 'reply' => 1,
761                 'export' => 0,
762                 'controlreadstatus' => 0,
763                 'canreplyprivately' => 0,
764                 'selfenrol' => 0
765             ],
766             'urls' => [
767                 'view' => $urlfactory->get_view_post_url_from_post_id($discussion1reply1->discussion, $discussion1reply1->id),
768                 'viewisolated' => $isolatedurl->out(false),
769                 'viewparent' => $urlfactory->get_view_post_url_from_post_id($discussion1reply1->discussion, $discussion1reply1->parent),
770                 'edit' => null,
771                 'delete' =>null,
772                 'split' => null,
773                 'reply' => (new moodle_url('/mod/forum/post.php#mformforum', [
774                     'reply' => $discussion1reply1->id
775                 ]))->out(false),
776                 'export' => null,
777                 'markasread' => null,
778                 'markasunread' => null,
779                 'discuss' => $urlfactory->get_discussion_view_url_from_discussion_id($discussion1reply1->discussion),
780             ],
781         );
783         // Test a discussion with two additional posts (total 3 posts).
784         $posts = mod_forum_external::get_discussion_posts($discussion1->id, 'modified', 'DESC');
785         $posts = external_api::clean_returnvalue(mod_forum_external::get_discussion_posts_returns(), $posts);
786         $this->assertEquals(3, count($posts['posts']));
788         // Unset the initial discussion post.
789         array_pop($posts['posts']);
790         $this->assertEquals($expectedposts, $posts);
792         // Check we receive the unread count correctly on tracked forum.
793         forum_tp_count_forum_unread_posts($forum2cm, $course1, true);    // Reset static cache.
794         $result = mod_forum_external::get_forums_by_courses(array($course1->id));
795         $result = external_api::clean_returnvalue(mod_forum_external::get_forums_by_courses_returns(), $result);
796         foreach ($result as $f) {
797             if ($f['id'] == $forum2->id) {
798                 $this->assertEquals(1, $f['unreadpostscount']);
799             }
800         }
802         // Test discussion without additional posts. There should be only one post (the one created by the discussion).
803         $posts = mod_forum_external::get_discussion_posts($discussion2->id, 'modified', 'DESC');
804         $posts = external_api::clean_returnvalue(mod_forum_external::get_discussion_posts_returns(), $posts);
805         $this->assertEquals(1, count($posts['posts']));
807         // Test discussion tracking on not tracked forum.
808         $result = mod_forum_external::view_forum_discussion($discussion1->id);
809         $result = external_api::clean_returnvalue(mod_forum_external::view_forum_discussion_returns(), $result);
810         $this->assertTrue($result['status']);
811         $this->assertEmpty($result['warnings']);
813         // Test posts have not been marked as read.
814         $posts = mod_forum_external::get_discussion_posts($discussion1->id, 'modified', 'DESC');
815         $posts = external_api::clean_returnvalue(mod_forum_external::get_discussion_posts_returns(), $posts);
816         foreach ($posts['posts'] as $post) {
817             $this->assertNull($post['unread']);
818         }
820         // Test discussion tracking on tracked forum.
821         $result = mod_forum_external::view_forum_discussion($discussion3->id);
822         $result = external_api::clean_returnvalue(mod_forum_external::view_forum_discussion_returns(), $result);
823         $this->assertTrue($result['status']);
824         $this->assertEmpty($result['warnings']);
826         // Test posts have been marked as read.
827         $posts = mod_forum_external::get_discussion_posts($discussion3->id, 'modified', 'DESC');
828         $posts = external_api::clean_returnvalue(mod_forum_external::get_discussion_posts_returns(), $posts);
829         foreach ($posts['posts'] as $post) {
830             $this->assertFalse($post['unread']);
831         }
833         // Check we receive 0 unread posts.
834         forum_tp_count_forum_unread_posts($forum2cm, $course1, true);    // Reset static cache.
835         $result = mod_forum_external::get_forums_by_courses(array($course1->id));
836         $result = external_api::clean_returnvalue(mod_forum_external::get_forums_by_courses_returns(), $result);
837         foreach ($result as $f) {
838             if ($f['id'] == $forum2->id) {
839                 $this->assertEquals(0, $f['unreadpostscount']);
840             }
841         }
842     }
844     /**
845      * Test get forum posts
846      */
847     public function test_mod_forum_get_forum_discussion_posts_deleted() {
848         global $CFG, $PAGE;
850         $this->resetAfterTest(true);
851         $generator = self::getDataGenerator()->get_plugin_generator('mod_forum');
853         // Create a course and enrol some users in it.
854         $course1 = self::getDataGenerator()->create_course();
856         // Create users.
857         $user1 = self::getDataGenerator()->create_user();
858         $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
859         $user2 = self::getDataGenerator()->create_user();
860         $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
862         // Set the first created user to the test user.
863         self::setUser($user1);
865         // Create test data.
866         $forum1 = self::getDataGenerator()->create_module('forum', (object) [
867                 'course' => $course1->id,
868             ]);
869         $forum1context = context_module::instance($forum1->cmid);
871         // Add discussions to the forum.
872         $discussion = $generator->create_discussion((object) [
873                 'course' => $course1->id,
874                 'userid' => $user1->id,
875                 'forum' => $forum1->id,
876             ]);
878         $discussion2 = $generator->create_discussion((object) [
879                 'course' => $course1->id,
880                 'userid' => $user2->id,
881                 'forum' => $forum1->id,
882             ]);
884         // Add replies to the discussion.
885         $discussionreply1 = $generator->create_post((object) [
886                 'discussion' => $discussion->id,
887                 'parent' => $discussion->firstpost,
888                 'userid' => $user2->id,
889             ]);
890         $discussionreply2 = $generator->create_post((object) [
891                 'discussion' => $discussion->id,
892                 'parent' => $discussionreply1->id,
893                 'userid' => $user2->id,
894                 'subject' => '',
895                 'message' => '',
896                 'messageformat' => FORMAT_PLAIN,
897                 'deleted' => 1,
898             ]);
899         $discussionreply3 = $generator->create_post((object) [
900                 'discussion' => $discussion->id,
901                 'parent' => $discussion->firstpost,
902                 'userid' => $user2->id,
903             ]);
905         // Test where some posts have been marked as deleted.
906         $posts = mod_forum_external::get_forum_discussion_posts($discussion->id, 'modified', 'DESC');
907         $posts = external_api::clean_returnvalue(mod_forum_external::get_forum_discussion_posts_returns(), $posts);
908         $deletedsubject = get_string('privacy:request:delete:post:subject', 'mod_forum');
909         $deletedmessage = get_string('privacy:request:delete:post:message', 'mod_forum');
911         foreach ($posts['posts'] as $post) {
912             if ($post['id'] == $discussionreply2->id) {
913                 $this->assertTrue($post['deleted']);
914                 $this->assertEquals($deletedsubject, $post['subject']);
915                 $this->assertEquals($deletedmessage, $post['message']);
916             } else {
917                 $this->assertFalse($post['deleted']);
918                 $this->assertNotEquals($deletedsubject, $post['subject']);
919                 $this->assertNotEquals($deletedmessage, $post['message']);
920             }
921         }
922     }
924     /**
925      * Test get forum posts (qanda forum)
926      */
927     public function test_mod_forum_get_forum_discussion_posts_qanda() {
928         global $CFG, $DB;
930         $this->resetAfterTest(true);
932         $record = new stdClass();
933         $user1 = self::getDataGenerator()->create_user($record);
934         $user2 = self::getDataGenerator()->create_user();
936         // Set the first created user to the test user.
937         self::setUser($user1);
939         // Create course to add the module.
940         $course1 = self::getDataGenerator()->create_course();
941         $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
942         $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
944         // Forum with tracking off.
945         $record = new stdClass();
946         $record->course = $course1->id;
947         $record->type = 'qanda';
948         $forum1 = self::getDataGenerator()->create_module('forum', $record);
949         $forum1context = context_module::instance($forum1->cmid);
951         // Add discussions to the forums.
952         $record = new stdClass();
953         $record->course = $course1->id;
954         $record->userid = $user2->id;
955         $record->forum = $forum1->id;
956         $discussion1 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
958         // Add 1 reply (not the actual user).
959         $record = new stdClass();
960         $record->discussion = $discussion1->id;
961         $record->parent = $discussion1->firstpost;
962         $record->userid = $user2->id;
963         $discussion1reply1 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
965         // We still see only the original post.
966         $posts = mod_forum_external::get_forum_discussion_posts($discussion1->id, 'modified', 'DESC');
967         $posts = external_api::clean_returnvalue(mod_forum_external::get_forum_discussion_posts_returns(), $posts);
968         $this->assertEquals(1, count($posts['posts']));
970         // Add a new reply, the user is going to be able to see only the original post and their new post.
971         $record = new stdClass();
972         $record->discussion = $discussion1->id;
973         $record->parent = $discussion1->firstpost;
974         $record->userid = $user1->id;
975         $discussion1reply2 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
977         $posts = mod_forum_external::get_forum_discussion_posts($discussion1->id, 'modified', 'DESC');
978         $posts = external_api::clean_returnvalue(mod_forum_external::get_forum_discussion_posts_returns(), $posts);
979         $this->assertEquals(2, count($posts['posts']));
981         // Now, we can fake the time of the user post, so he can se the rest of the discussion posts.
982         $discussion1reply2->created -= $CFG->maxeditingtime * 2;
983         $DB->update_record('forum_posts', $discussion1reply2);
985         $posts = mod_forum_external::get_forum_discussion_posts($discussion1->id, 'modified', 'DESC');
986         $posts = external_api::clean_returnvalue(mod_forum_external::get_forum_discussion_posts_returns(), $posts);
987         $this->assertEquals(3, count($posts['posts']));
988     }
990     /**
991      * Test get forum discussions paginated
992      */
993     public function test_mod_forum_get_forum_discussions_paginated() {
994         global $USER, $CFG, $DB, $PAGE;
996         $this->resetAfterTest(true);
998         // Set the CFG variable to allow track forums.
999         $CFG->forum_trackreadposts = true;
1001         // Create a user who can track forums.
1002         $record = new stdClass();
1003         $record->trackforums = true;
1004         $user1 = self::getDataGenerator()->create_user($record);
1005         // Create a bunch of other users to post.
1006         $user2 = self::getDataGenerator()->create_user();
1007         $user3 = self::getDataGenerator()->create_user();
1008         $user4 = self::getDataGenerator()->create_user();
1010         // Set the first created user to the test user.
1011         self::setUser($user1);
1013         // Create courses to add the modules.
1014         $course1 = self::getDataGenerator()->create_course();
1016         // First forum with tracking off.
1017         $record = new stdClass();
1018         $record->course = $course1->id;
1019         $record->trackingtype = FORUM_TRACKING_OFF;
1020         $forum1 = self::getDataGenerator()->create_module('forum', $record);
1022         // Add discussions to the forums.
1023         $record = new stdClass();
1024         $record->course = $course1->id;
1025         $record->userid = $user1->id;
1026         $record->forum = $forum1->id;
1027         $discussion1 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1029         // Add three replies to the discussion 1 from different users.
1030         $record = new stdClass();
1031         $record->discussion = $discussion1->id;
1032         $record->parent = $discussion1->firstpost;
1033         $record->userid = $user2->id;
1034         $discussion1reply1 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1036         $record->parent = $discussion1reply1->id;
1037         $record->userid = $user3->id;
1038         $discussion1reply2 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1040         $record->userid = $user4->id;
1041         $discussion1reply3 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1043         // Enrol the user in the first course.
1044         $enrol = enrol_get_plugin('manual');
1046         // We don't use the dataGenerator as we need to get the $instance2 to unenrol later.
1047         $enrolinstances = enrol_get_instances($course1->id, true);
1048         foreach ($enrolinstances as $courseenrolinstance) {
1049             if ($courseenrolinstance->enrol == "manual") {
1050                 $instance1 = $courseenrolinstance;
1051                 break;
1052             }
1053         }
1054         $enrol->enrol_user($instance1, $user1->id);
1056         // Delete one user.
1057         delete_user($user4);
1059         // Assign capabilities to view discussions for forum 1.
1060         $cm = get_coursemodule_from_id('forum', $forum1->cmid, 0, false, MUST_EXIST);
1061         $context = context_module::instance($cm->id);
1062         $newrole = create_role('Role 2', 'role2', 'Role 2 description');
1063         $this->assignUserCapability('mod/forum:viewdiscussion', $context->id, $newrole);
1065         // Create what we expect to be returned when querying the forums.
1067         $post1 = $DB->get_record('forum_posts', array('id' => $discussion1->firstpost), '*', MUST_EXIST);
1069         // User pictures are initially empty, we should get the links once the external function is called.
1070         $expecteddiscussions = array(
1071                 'id' => $discussion1->firstpost,
1072                 'name' => $discussion1->name,
1073                 'groupid' => (int) $discussion1->groupid,
1074                 'timemodified' => $discussion1reply3->created,
1075                 'usermodified' => (int) $discussion1reply3->userid,
1076                 'timestart' => (int) $discussion1->timestart,
1077                 'timeend' => (int) $discussion1->timeend,
1078                 'discussion' => $discussion1->id,
1079                 'parent' => 0,
1080                 'userid' => (int) $discussion1->userid,
1081                 'created' => (int) $post1->created,
1082                 'modified' => (int) $post1->modified,
1083                 'mailed' => (int) $post1->mailed,
1084                 'subject' => $post1->subject,
1085                 'message' => $post1->message,
1086                 'messageformat' => (int) $post1->messageformat,
1087                 'messagetrust' => (int) $post1->messagetrust,
1088                 'attachment' => $post1->attachment,
1089                 'totalscore' => (int) $post1->totalscore,
1090                 'mailnow' => (int) $post1->mailnow,
1091                 'userfullname' => fullname($user1),
1092                 'usermodifiedfullname' => fullname($user4),
1093                 'userpictureurl' => '',
1094                 'usermodifiedpictureurl' => '',
1095                 'numreplies' => 3,
1096                 'numunread' => 0,
1097                 'pinned' => (bool) FORUM_DISCUSSION_UNPINNED,
1098                 'locked' => false,
1099                 'canreply' => false,
1100                 'canlock' => false
1101             );
1103         // Call the external function passing forum id.
1104         $discussions = mod_forum_external::get_forum_discussions_paginated($forum1->id);
1105         $discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_paginated_returns(), $discussions);
1106         $expectedreturn = array(
1107             'discussions' => array($expecteddiscussions),
1108             'warnings' => array()
1109         );
1111         // Wait the theme to be loaded (the external_api call does that) to generate the user profiles.
1112         $userpicture = new user_picture($user1);
1113         $userpicture->size = 1; // Size f1.
1114         $expectedreturn['discussions'][0]['userpictureurl'] = $userpicture->get_url($PAGE)->out(false);
1116         $userpicture = new user_picture($user4);
1117         $userpicture->size = 1; // Size f1.
1118         $expectedreturn['discussions'][0]['usermodifiedpictureurl'] = $userpicture->get_url($PAGE)->out(false);
1120         $this->assertEquals($expectedreturn, $discussions);
1122         // Call without required view discussion capability.
1123         $this->unassignUserCapability('mod/forum:viewdiscussion', $context->id, $newrole);
1124         try {
1125             mod_forum_external::get_forum_discussions_paginated($forum1->id);
1126             $this->fail('Exception expected due to missing capability.');
1127         } catch (moodle_exception $e) {
1128             $this->assertEquals('noviewdiscussionspermission', $e->errorcode);
1129         }
1131         // Unenrol user from second course.
1132         $enrol->unenrol_user($instance1, $user1->id);
1134         // Call for the second course we unenrolled the user from, make sure exception thrown.
1135         try {
1136             mod_forum_external::get_forum_discussions_paginated($forum1->id);
1137             $this->fail('Exception expected due to being unenrolled from the course.');
1138         } catch (moodle_exception $e) {
1139             $this->assertEquals('requireloginerror', $e->errorcode);
1140         }
1142         $this->setAdminUser();
1143         $discussions = mod_forum_external::get_forum_discussions_paginated($forum1->id);
1144         $discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_paginated_returns(), $discussions);
1145         $this->assertTrue($discussions['discussions'][0]['canlock']);
1146     }
1148     /**
1149      * Test get forum discussions paginated (qanda forums)
1150      */
1151     public function test_mod_forum_get_forum_discussions_paginated_qanda() {
1153         $this->resetAfterTest(true);
1155         // Create courses to add the modules.
1156         $course = self::getDataGenerator()->create_course();
1158         $user1 = self::getDataGenerator()->create_user();
1159         $user2 = self::getDataGenerator()->create_user();
1161         // First forum with tracking off.
1162         $record = new stdClass();
1163         $record->course = $course->id;
1164         $record->type = 'qanda';
1165         $forum = self::getDataGenerator()->create_module('forum', $record);
1167         // Add discussions to the forums.
1168         $discussionrecord = new stdClass();
1169         $discussionrecord->course = $course->id;
1170         $discussionrecord->userid = $user2->id;
1171         $discussionrecord->forum = $forum->id;
1172         $discussion = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($discussionrecord);
1174         self::setAdminUser();
1175         $discussions = mod_forum_external::get_forum_discussions_paginated($forum->id);
1176         $discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_paginated_returns(), $discussions);
1178         $this->assertCount(1, $discussions['discussions']);
1179         $this->assertCount(0, $discussions['warnings']);
1181         self::setUser($user1);
1182         $this->getDataGenerator()->enrol_user($user1->id, $course->id);
1184         $discussions = mod_forum_external::get_forum_discussions_paginated($forum->id);
1185         $discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_paginated_returns(), $discussions);
1187         $this->assertCount(1, $discussions['discussions']);
1188         $this->assertCount(0, $discussions['warnings']);
1190     }
1192     /**
1193      * Test get forum discussions
1194      */
1195     public function test_mod_forum_get_forum_discussions() {
1196         global $CFG, $DB, $PAGE;
1198         $this->resetAfterTest(true);
1200         // Set the CFG variable to allow track forums.
1201         $CFG->forum_trackreadposts = true;
1203         // Create a user who can track forums.
1204         $record = new stdClass();
1205         $record->trackforums = true;
1206         $user1 = self::getDataGenerator()->create_user($record);
1207         // Create a bunch of other users to post.
1208         $user2 = self::getDataGenerator()->create_user();
1209         $user3 = self::getDataGenerator()->create_user();
1210         $user4 = self::getDataGenerator()->create_user();
1212         // Set the first created user to the test user.
1213         self::setUser($user1);
1215         // Create courses to add the modules.
1216         $course1 = self::getDataGenerator()->create_course();
1218         // First forum with tracking off.
1219         $record = new stdClass();
1220         $record->course = $course1->id;
1221         $record->trackingtype = FORUM_TRACKING_OFF;
1222         $forum1 = self::getDataGenerator()->create_module('forum', $record);
1224         // Add discussions to the forums.
1225         $record = new stdClass();
1226         $record->course = $course1->id;
1227         $record->userid = $user1->id;
1228         $record->forum = $forum1->id;
1229         $discussion1 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1231         // Add three replies to the discussion 1 from different users.
1232         $record = new stdClass();
1233         $record->discussion = $discussion1->id;
1234         $record->parent = $discussion1->firstpost;
1235         $record->userid = $user2->id;
1236         $discussion1reply1 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1238         $record->parent = $discussion1reply1->id;
1239         $record->userid = $user3->id;
1240         $discussion1reply2 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1242         $record->userid = $user4->id;
1243         $discussion1reply3 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1245         // Enrol the user in the first course.
1246         $enrol = enrol_get_plugin('manual');
1248         // We don't use the dataGenerator as we need to get the $instance2 to unenrol later.
1249         $enrolinstances = enrol_get_instances($course1->id, true);
1250         foreach ($enrolinstances as $courseenrolinstance) {
1251             if ($courseenrolinstance->enrol == "manual") {
1252                 $instance1 = $courseenrolinstance;
1253                 break;
1254             }
1255         }
1256         $enrol->enrol_user($instance1, $user1->id);
1258         // Delete one user.
1259         delete_user($user4);
1261         // Assign capabilities to view discussions for forum 1.
1262         $cm = get_coursemodule_from_id('forum', $forum1->cmid, 0, false, MUST_EXIST);
1263         $context = context_module::instance($cm->id);
1264         $newrole = create_role('Role 2', 'role2', 'Role 2 description');
1265         $this->assignUserCapability('mod/forum:viewdiscussion', $context->id, $newrole);
1267         // Create what we expect to be returned when querying the forums.
1269         $post1 = $DB->get_record('forum_posts', array('id' => $discussion1->firstpost), '*', MUST_EXIST);
1271         // User pictures are initially empty, we should get the links once the external function is called.
1272         $expecteddiscussions = array(
1273             'id' => $discussion1->firstpost,
1274             'name' => $discussion1->name,
1275             'groupid' => (int) $discussion1->groupid,
1276             'timemodified' => (int) $discussion1reply3->created,
1277             'usermodified' => (int) $discussion1reply3->userid,
1278             'timestart' => (int) $discussion1->timestart,
1279             'timeend' => (int) $discussion1->timeend,
1280             'discussion' => (int) $discussion1->id,
1281             'parent' => 0,
1282             'userid' => (int) $discussion1->userid,
1283             'created' => (int) $post1->created,
1284             'modified' => (int) $post1->modified,
1285             'mailed' => (int) $post1->mailed,
1286             'subject' => $post1->subject,
1287             'message' => $post1->message,
1288             'messageformat' => (int) $post1->messageformat,
1289             'messagetrust' => (int) $post1->messagetrust,
1290             'attachment' => $post1->attachment,
1291             'totalscore' => (int) $post1->totalscore,
1292             'mailnow' => (int) $post1->mailnow,
1293             'userfullname' => fullname($user1),
1294             'usermodifiedfullname' => fullname($user4),
1295             'userpictureurl' => '',
1296             'usermodifiedpictureurl' => '',
1297             'numreplies' => 3,
1298             'numunread' => 0,
1299             'pinned' => (bool) FORUM_DISCUSSION_UNPINNED,
1300             'locked' => false,
1301             'canreply' => false,
1302             'canlock' => false,
1303             'starred' => false,
1304             'canfavourite' => true
1305         );
1307         // Call the external function passing forum id.
1308         $discussions = mod_forum_external::get_forum_discussions($forum1->id);
1309         $discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_returns(), $discussions);
1310         $expectedreturn = array(
1311             'discussions' => array($expecteddiscussions),
1312             'warnings' => array()
1313         );
1315         // Wait the theme to be loaded (the external_api call does that) to generate the user profiles.
1316         $userpicture = new user_picture($user1);
1317         $userpicture->size = 2; // Size f2.
1318         $expectedreturn['discussions'][0]['userpictureurl'] = $userpicture->get_url($PAGE)->out(false);
1320         $userpicture = new user_picture($user4);
1321         $userpicture->size = 2; // Size f2.
1322         $expectedreturn['discussions'][0]['usermodifiedpictureurl'] = $userpicture->get_url($PAGE)->out(false);
1324         $this->assertEquals($expectedreturn, $discussions);
1326         // Test the starring functionality return.
1327         $t = mod_forum_external::toggle_favourite_state($discussion1->id, 1);
1328         $expectedreturn['discussions'][0]['starred'] = true;
1329         $discussions = mod_forum_external::get_forum_discussions($forum1->id);
1330         $discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_returns(), $discussions);
1331         $this->assertEquals($expectedreturn, $discussions);
1333         // Call without required view discussion capability.
1334         $this->unassignUserCapability('mod/forum:viewdiscussion', $context->id, $newrole);
1335         try {
1336             mod_forum_external::get_forum_discussions($forum1->id);
1337             $this->fail('Exception expected due to missing capability.');
1338         } catch (moodle_exception $e) {
1339             $this->assertEquals('noviewdiscussionspermission', $e->errorcode);
1340         }
1342         // Unenrol user from second course.
1343         $enrol->unenrol_user($instance1, $user1->id);
1345         // Call for the second course we unenrolled the user from, make sure exception thrown.
1346         try {
1347             mod_forum_external::get_forum_discussions($forum1->id);
1348             $this->fail('Exception expected due to being unenrolled from the course.');
1349         } catch (moodle_exception $e) {
1350             $this->assertEquals('requireloginerror', $e->errorcode);
1351         }
1353         $this->setAdminUser();
1354         $discussions = mod_forum_external::get_forum_discussions($forum1->id);
1355         $discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_returns(), $discussions);
1356         $this->assertTrue($discussions['discussions'][0]['canlock']);
1357     }
1359     /**
1360      * Test the sorting in get forum discussions
1361      */
1362     public function test_mod_forum_get_forum_discussions_sorting() {
1363         global $CFG, $DB, $PAGE;
1365         $this->resetAfterTest(true);
1367         // Set the CFG variable to allow track forums.
1368         $CFG->forum_trackreadposts = true;
1370         // Create a user who can track forums.
1371         $record = new stdClass();
1372         $record->trackforums = true;
1373         $user1 = self::getDataGenerator()->create_user($record);
1374         // Create a bunch of other users to post.
1375         $user2 = self::getDataGenerator()->create_user();
1376         $user3 = self::getDataGenerator()->create_user();
1377         $user4 = self::getDataGenerator()->create_user();
1379         // Set the first created user to the test user.
1380         self::setUser($user1);
1382         // Create courses to add the modules.
1383         $course1 = self::getDataGenerator()->create_course();
1385         // Enrol the user in the first course.
1386         $enrol = enrol_get_plugin('manual');
1388         // We don't use the dataGenerator as we need to get the $instance2 to unenrol later.
1389         $enrolinstances = enrol_get_instances($course1->id, true);
1390         foreach ($enrolinstances as $courseenrolinstance) {
1391             if ($courseenrolinstance->enrol == "manual") {
1392                 $instance1 = $courseenrolinstance;
1393                 break;
1394             }
1395         }
1396         $enrol->enrol_user($instance1, $user1->id);
1398         // First forum with tracking off.
1399         $record = new stdClass();
1400         $record->course = $course1->id;
1401         $record->trackingtype = FORUM_TRACKING_OFF;
1402         $forum1 = self::getDataGenerator()->create_module('forum', $record);
1404         // Assign capabilities to view discussions for forum 1.
1405         $cm = get_coursemodule_from_id('forum', $forum1->cmid, 0, false, MUST_EXIST);
1406         $context = context_module::instance($cm->id);
1407         $newrole = create_role('Role 2', 'role2', 'Role 2 description');
1408         $this->assignUserCapability('mod/forum:viewdiscussion', $context->id, $newrole);
1410         // Add discussions to the forums.
1411         $record = new stdClass();
1412         $record->course = $course1->id;
1413         $record->userid = $user1->id;
1414         $record->forum = $forum1->id;
1415         $discussion1 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1416         sleep(1);
1418         // Add three replies to the discussion 1 from different users.
1419         $record = new stdClass();
1420         $record->discussion = $discussion1->id;
1421         $record->parent = $discussion1->firstpost;
1422         $record->userid = $user2->id;
1423         $discussion1reply1 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1424         sleep(1);
1426         $record->parent = $discussion1reply1->id;
1427         $record->userid = $user3->id;
1428         $discussion1reply2 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1429         sleep(1);
1431         $record->userid = $user4->id;
1432         $discussion1reply3 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1433         sleep(1);
1435         // Create discussion2.
1436         $record2 = new stdClass();
1437         $record2->course = $course1->id;
1438         $record2->userid = $user1->id;
1439         $record2->forum = $forum1->id;
1440         $discussion2 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record2);
1441         sleep(1);
1443         // Add one reply to the discussion 2.
1444         $record2 = new stdClass();
1445         $record2->discussion = $discussion2->id;
1446         $record2->parent = $discussion2->firstpost;
1447         $record2->userid = $user2->id;
1448         $discussion2reply1 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record2);
1449         sleep(1);
1451         // Create discussion 3.
1452         $record3 = new stdClass();
1453         $record3->course = $course1->id;
1454         $record3->userid = $user1->id;
1455         $record3->forum = $forum1->id;
1456         $discussion3 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record3);
1457         sleep(1);
1459         // Add two replies to the discussion 3.
1460         $record3 = new stdClass();
1461         $record3->discussion = $discussion3->id;
1462         $record3->parent = $discussion3->firstpost;
1463         $record3->userid = $user2->id;
1464         $discussion3reply1 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record3);
1465         sleep(1);
1467         $record3->parent = $discussion3reply1->id;
1468         $record3->userid = $user3->id;
1469         $discussion3reply2 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record3);
1471         // Call the external function passing forum id.
1472         $discussions = mod_forum_external::get_forum_discussions($forum1->id);
1473         $discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_returns(), $discussions);
1474         // Discussions should be ordered by last post date in descending order by default.
1475         $this->assertEquals($discussions['discussions'][0]['discussion'], $discussion3->id);
1476         $this->assertEquals($discussions['discussions'][1]['discussion'], $discussion2->id);
1477         $this->assertEquals($discussions['discussions'][2]['discussion'], $discussion1->id);
1479         $vaultfactory = \mod_forum\local\container::get_vault_factory();
1480         $discussionlistvault = $vaultfactory->get_discussions_in_forum_vault();
1482         // Call the external function passing forum id and sort order parameter.
1483         $discussions = mod_forum_external::get_forum_discussions($forum1->id, $discussionlistvault::SORTORDER_LASTPOST_ASC);
1484         $discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_returns(), $discussions);
1485         // Discussions should be ordered by last post date in ascending order.
1486         $this->assertEquals($discussions['discussions'][0]['discussion'], $discussion1->id);
1487         $this->assertEquals($discussions['discussions'][1]['discussion'], $discussion2->id);
1488         $this->assertEquals($discussions['discussions'][2]['discussion'], $discussion3->id);
1490         // Call the external function passing forum id and sort order parameter.
1491         $discussions = mod_forum_external::get_forum_discussions($forum1->id, $discussionlistvault::SORTORDER_CREATED_DESC);
1492         $discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_returns(), $discussions);
1493         // Discussions should be ordered by discussion creation date in descending order.
1494         $this->assertEquals($discussions['discussions'][0]['discussion'], $discussion3->id);
1495         $this->assertEquals($discussions['discussions'][1]['discussion'], $discussion2->id);
1496         $this->assertEquals($discussions['discussions'][2]['discussion'], $discussion1->id);
1498         // Call the external function passing forum id and sort order parameter.
1499         $discussions = mod_forum_external::get_forum_discussions($forum1->id, $discussionlistvault::SORTORDER_CREATED_ASC);
1500         $discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_returns(), $discussions);
1501         // Discussions should be ordered by discussion creation date in ascending order.
1502         $this->assertEquals($discussions['discussions'][0]['discussion'], $discussion1->id);
1503         $this->assertEquals($discussions['discussions'][1]['discussion'], $discussion2->id);
1504         $this->assertEquals($discussions['discussions'][2]['discussion'], $discussion3->id);
1506         // Call the external function passing forum id and sort order parameter.
1507         $discussions = mod_forum_external::get_forum_discussions($forum1->id, $discussionlistvault::SORTORDER_REPLIES_DESC);
1508         $discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_returns(), $discussions);
1509         // Discussions should be ordered by the number of replies in descending order.
1510         $this->assertEquals($discussions['discussions'][0]['discussion'], $discussion1->id);
1511         $this->assertEquals($discussions['discussions'][1]['discussion'], $discussion3->id);
1512         $this->assertEquals($discussions['discussions'][2]['discussion'], $discussion2->id);
1514         // Call the external function passing forum id and sort order parameter.
1515         $discussions = mod_forum_external::get_forum_discussions($forum1->id, $discussionlistvault::SORTORDER_REPLIES_ASC);
1516         $discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_returns(), $discussions);
1517         // Discussions should be ordered by the number of replies in ascending order.
1518         $this->assertEquals($discussions['discussions'][0]['discussion'], $discussion2->id);
1519         $this->assertEquals($discussions['discussions'][1]['discussion'], $discussion3->id);
1520         $this->assertEquals($discussions['discussions'][2]['discussion'], $discussion1->id);
1522         // Pin discussion2.
1523         $DB->update_record('forum_discussions',
1524             (object) array('id' => $discussion2->id, 'pinned' => FORUM_DISCUSSION_PINNED));
1526         // Call the external function passing forum id.
1527         $discussions = mod_forum_external::get_forum_discussions($forum1->id);
1528         $discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_returns(), $discussions);
1529         // Discussions should be ordered by last post date in descending order by default.
1530         // Pinned discussions should be at the top of the list.
1531         $this->assertEquals($discussions['discussions'][0]['discussion'], $discussion2->id);
1532         $this->assertEquals($discussions['discussions'][1]['discussion'], $discussion3->id);
1533         $this->assertEquals($discussions['discussions'][2]['discussion'], $discussion1->id);
1535         // Call the external function passing forum id and sort order parameter.
1536         $discussions = mod_forum_external::get_forum_discussions($forum1->id, $discussionlistvault::SORTORDER_LASTPOST_ASC);
1537         $discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_returns(), $discussions);
1538         // Discussions should be ordered by last post date in ascending order.
1539         // Pinned discussions should be at the top of the list.
1540         $this->assertEquals($discussions['discussions'][0]['discussion'], $discussion2->id);
1541         $this->assertEquals($discussions['discussions'][1]['discussion'], $discussion1->id);
1542         $this->assertEquals($discussions['discussions'][2]['discussion'], $discussion3->id);
1543     }
1545     /**
1546      * Test add_discussion_post
1547      */
1548     public function test_add_discussion_post() {
1549         global $CFG;
1551         $this->resetAfterTest(true);
1553         $user = self::getDataGenerator()->create_user();
1554         $otheruser = self::getDataGenerator()->create_user();
1556         self::setAdminUser();
1558         // Create course to add the module.
1559         $course = self::getDataGenerator()->create_course(array('groupmode' => VISIBLEGROUPS, 'groupmodeforce' => 0));
1561         // Forum with tracking off.
1562         $record = new stdClass();
1563         $record->course = $course->id;
1564         $forum = self::getDataGenerator()->create_module('forum', $record);
1565         $cm = get_coursemodule_from_id('forum', $forum->cmid, 0, false, MUST_EXIST);
1566         $forumcontext = context_module::instance($forum->cmid);
1568         // Add discussions to the forums.
1569         $record = new stdClass();
1570         $record->course = $course->id;
1571         $record->userid = $user->id;
1572         $record->forum = $forum->id;
1573         $discussion = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1575         // Try to post (user not enrolled).
1576         self::setUser($user);
1577         try {
1578             mod_forum_external::add_discussion_post($discussion->firstpost, 'some subject', 'some text here...');
1579             $this->fail('Exception expected due to being unenrolled from the course.');
1580         } catch (moodle_exception $e) {
1581             $this->assertEquals('requireloginerror', $e->errorcode);
1582         }
1584         $this->getDataGenerator()->enrol_user($user->id, $course->id);
1585         $this->getDataGenerator()->enrol_user($otheruser->id, $course->id);
1587         $createdpost = mod_forum_external::add_discussion_post($discussion->firstpost, 'some subject', 'some text here...');
1588         $createdpost = external_api::clean_returnvalue(mod_forum_external::add_discussion_post_returns(), $createdpost);
1590         $posts = mod_forum_external::get_forum_discussion_posts($discussion->id);
1591         $posts = external_api::clean_returnvalue(mod_forum_external::get_forum_discussion_posts_returns(), $posts);
1592         // We receive the discussion and the post.
1593         $this->assertEquals(2, count($posts['posts']));
1595         $tested = false;
1596         foreach ($posts['posts'] as $thispost) {
1597             if ($createdpost['postid'] == $thispost['id']) {
1598                 $this->assertEquals('some subject', $thispost['subject']);
1599                 $this->assertEquals('some text here...', $thispost['message']);
1600                 $this->assertEquals(FORMAT_HTML, $thispost['messageformat']); // This is the default if format was not specified.
1601                 $tested = true;
1602             }
1603         }
1604         $this->assertTrue($tested);
1606         // Let's simulate a call with any other format, it should be stored that way.
1607         global $DB; // Yes, we are going to use DB facilities too, because cannot rely on other functions for checking
1608                     // the format. They eat it completely (going back to FORMAT_HTML. So we only can trust DB for further
1609                     // processing.
1610         $formats = [FORMAT_PLAIN, FORMAT_MOODLE, FORMAT_MARKDOWN, FORMAT_HTML];
1611         $options = [];
1612         foreach ($formats as $format) {
1613             $createdpost = mod_forum_external::add_discussion_post($discussion->firstpost,
1614                 'with some format', 'some formatted here...', $options, $format);
1615             $createdpost = external_api::clean_returnvalue(mod_forum_external::add_discussion_post_returns(), $createdpost);
1616             $dbformat = $DB->get_field('forum_posts', 'messageformat', ['id' => $createdpost['postid']]);
1617             $this->assertEquals($format, $dbformat);
1618         }
1620         // Now let's try the 'topreferredformat' option. That should end with the content
1621         // transformed and the format being FORMAT_HTML (when, like in this case,  user preferred
1622         // format is HTML, inferred from editor in preferences).
1623         $options = [['name' => 'topreferredformat', 'value' => true]];
1624         $createdpost = mod_forum_external::add_discussion_post($discussion->firstpost,
1625             'interesting subject', 'with some https://example.com link', $options, FORMAT_MOODLE);
1626         $createdpost = external_api::clean_returnvalue(mod_forum_external::add_discussion_post_returns(), $createdpost);
1627         $dbpost = $DB->get_record('forum_posts', ['id' => $createdpost['postid']]);
1628         // Format HTML and content converted, we should get.
1629         $this->assertEquals(FORMAT_HTML, $dbpost->messageformat);
1630         $this->assertEquals('<div class="text_to_html">with some https://example.com link</div>', $dbpost->message);
1632         // Test inline and regular attachment in post
1633         // Create a file in a draft area for inline attachments.
1634         $draftidinlineattach = file_get_unused_draft_itemid();
1635         $draftidattach = file_get_unused_draft_itemid();
1636         self::setUser($user);
1637         $usercontext = context_user::instance($user->id);
1638         $filepath = '/';
1639         $filearea = 'draft';
1640         $component = 'user';
1641         $filenameimg = 'shouldbeanimage.txt';
1642         $filerecordinline = array(
1643             'contextid' => $usercontext->id,
1644             'component' => $component,
1645             'filearea'  => $filearea,
1646             'itemid'    => $draftidinlineattach,
1647             'filepath'  => $filepath,
1648             'filename'  => $filenameimg,
1649         );
1650         $fs = get_file_storage();
1652         // Create a file in a draft area for regular attachments.
1653         $filerecordattach = $filerecordinline;
1654         $attachfilename = 'attachment.txt';
1655         $filerecordattach['filename'] = $attachfilename;
1656         $filerecordattach['itemid'] = $draftidattach;
1657         $fs->create_file_from_string($filerecordinline, 'image contents (not really)');
1658         $fs->create_file_from_string($filerecordattach, 'simple text attachment');
1660         $options = array(array('name' => 'inlineattachmentsid', 'value' => $draftidinlineattach),
1661                          array('name' => 'attachmentsid', 'value' => $draftidattach));
1662         $dummytext = 'Here is an inline image: <img src="' . $CFG->wwwroot
1663                      . "/draftfile.php/{$usercontext->id}/user/draft/{$draftidinlineattach}/{$filenameimg}"
1664                      . '" alt="inlineimage">.';
1665         $createdpost = mod_forum_external::add_discussion_post($discussion->firstpost, 'new post inline attachment',
1666                                                                $dummytext, $options);
1667         $createdpost = external_api::clean_returnvalue(mod_forum_external::add_discussion_post_returns(), $createdpost);
1669         $posts = mod_forum_external::get_forum_discussion_posts($discussion->id);
1670         $posts = external_api::clean_returnvalue(mod_forum_external::get_forum_discussion_posts_returns(), $posts);
1671         // We receive the discussion and the post.
1672         // Can't guarantee order of posts during tests.
1673         $postfound = false;
1674         foreach ($posts['posts'] as $thispost) {
1675             if ($createdpost['postid'] == $thispost['id']) {
1676                 $this->assertEquals($createdpost['postid'], $thispost['id']);
1677                 $this->assertEquals($thispost['attachment'], 1, "There should be a non-inline attachment");
1678                 $this->assertCount(1, $thispost['attachments'], "There should be 1 attachment");
1679                 $this->assertEquals($thispost['attachments'][0]['filename'], $attachfilename, "There should be 1 attachment");
1680                 $this->assertContains('pluginfile.php', $thispost['message']);
1681                 $postfound = true;
1682                 break;
1683             }
1684         }
1686         $this->assertTrue($postfound);
1688         // Check not posting in groups the user is not member of.
1689         $group = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
1690         groups_add_member($group->id, $otheruser->id);
1692         $forum = self::getDataGenerator()->create_module('forum', $record, array('groupmode' => SEPARATEGROUPS));
1693         $record->forum = $forum->id;
1694         $record->userid = $otheruser->id;
1695         $record->groupid = $group->id;
1696         $discussion = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1698         try {
1699             mod_forum_external::add_discussion_post($discussion->firstpost, 'some subject', 'some text here...');
1700             $this->fail('Exception expected due to invalid permissions for posting.');
1701         } catch (moodle_exception $e) {
1702             $this->assertEquals('nopostforum', $e->errorcode);
1703         }
1704     }
1706     /**
1707      * Test add_discussion_post and auto subscription to a discussion.
1708      */
1709     public function test_add_discussion_post_subscribe_discussion() {
1710         global $USER;
1712         $this->resetAfterTest(true);
1714         self::setAdminUser();
1716         $user = self::getDataGenerator()->create_user();
1717         $admin = get_admin();
1718         // Create course to add the module.
1719         $course = self::getDataGenerator()->create_course(array('groupmode' => VISIBLEGROUPS, 'groupmodeforce' => 0));
1721         $this->getDataGenerator()->enrol_user($user->id, $course->id);
1723         // Forum with tracking off.
1724         $record = new stdClass();
1725         $record->course = $course->id;
1726         $forum = self::getDataGenerator()->create_module('forum', $record);
1727         $cm = get_coursemodule_from_id('forum', $forum->cmid, 0, false, MUST_EXIST);
1729         // Add discussions to the forums.
1730         $record = new stdClass();
1731         $record->course = $course->id;
1732         $record->userid = $admin->id;
1733         $record->forum = $forum->id;
1734         $discussion1 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1735         $discussion2 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1737         // Try to post as user.
1738         self::setUser($user);
1739         // Enable auto subscribe discussion.
1740         $USER->autosubscribe = true;
1741         // Add a discussion post in a forum discussion where the user is not subscribed (auto-subscribe preference enabled).
1742         mod_forum_external::add_discussion_post($discussion1->firstpost, 'some subject', 'some text here...');
1744         $posts = mod_forum_external::get_forum_discussion_posts($discussion1->id);
1745         $posts = external_api::clean_returnvalue(mod_forum_external::get_forum_discussion_posts_returns(), $posts);
1746         // We receive the discussion and the post.
1747         $this->assertEquals(2, count($posts['posts']));
1748         // The user should be subscribed to the discussion after adding a discussion post.
1749         $this->assertTrue(\mod_forum\subscriptions::is_subscribed($user->id, $forum, $discussion1->id, $cm));
1751         // Disable auto subscribe discussion.
1752         $USER->autosubscribe = false;
1753         $this->assertTrue(\mod_forum\subscriptions::is_subscribed($user->id, $forum, $discussion1->id, $cm));
1754         // Add a discussion post in a forum discussion where the user is subscribed (auto-subscribe preference disabled).
1755         mod_forum_external::add_discussion_post($discussion1->firstpost, 'some subject 1', 'some text here 1...');
1757         $posts = mod_forum_external::get_forum_discussion_posts($discussion1->id);
1758         $posts = external_api::clean_returnvalue(mod_forum_external::get_forum_discussion_posts_returns(), $posts);
1759         // We receive the discussion and the post.
1760         $this->assertEquals(3, count($posts['posts']));
1761         // The user should still be subscribed to the discussion after adding a discussion post.
1762         $this->assertTrue(\mod_forum\subscriptions::is_subscribed($user->id, $forum, $discussion1->id, $cm));
1764         $this->assertFalse(\mod_forum\subscriptions::is_subscribed($user->id, $forum, $discussion2->id, $cm));
1765         // Add a discussion post in a forum discussion where the user is not subscribed (auto-subscribe preference disabled).
1766         mod_forum_external::add_discussion_post($discussion2->firstpost, 'some subject 2', 'some text here 2...');
1768         $posts = mod_forum_external::get_forum_discussion_posts($discussion2->id);
1769         $posts = external_api::clean_returnvalue(mod_forum_external::get_forum_discussion_posts_returns(), $posts);
1770         // We receive the discussion and the post.
1771         $this->assertEquals(2, count($posts['posts']));
1772         // The user should still not be subscribed to the discussion after adding a discussion post.
1773         $this->assertFalse(\mod_forum\subscriptions::is_subscribed($user->id, $forum, $discussion2->id, $cm));
1775         // Passing a value for the discussionsubscribe option parameter.
1776         $this->assertFalse(\mod_forum\subscriptions::is_subscribed($user->id, $forum, $discussion2->id, $cm));
1777         // Add a discussion post in a forum discussion where the user is not subscribed (auto-subscribe preference disabled),
1778         // and the option parameter 'discussionsubscribe' => true in the webservice.
1779         $option = array('name' => 'discussionsubscribe', 'value' => true);
1780         $options[] = $option;
1781         mod_forum_external::add_discussion_post($discussion2->firstpost, 'some subject 2', 'some text here 2...',
1782             $options);
1784         $posts = mod_forum_external::get_forum_discussion_posts($discussion2->id);
1785         $posts = external_api::clean_returnvalue(mod_forum_external::get_forum_discussion_posts_returns(), $posts);
1786         // We receive the discussion and the post.
1787         $this->assertEquals(3, count($posts['posts']));
1788         // The user should now be subscribed to the discussion after adding a discussion post.
1789         $this->assertTrue(\mod_forum\subscriptions::is_subscribed($user->id, $forum, $discussion2->id, $cm));
1790     }
1792     /*
1793      * Test add_discussion. A basic test since all the API functions are already covered by unit tests.
1794      */
1795     public function test_add_discussion() {
1796         global $CFG, $USER;
1797         $this->resetAfterTest(true);
1799         // Create courses to add the modules.
1800         $course = self::getDataGenerator()->create_course();
1802         $user1 = self::getDataGenerator()->create_user();
1803         $user2 = self::getDataGenerator()->create_user();
1805         // First forum with tracking off.
1806         $record = new stdClass();
1807         $record->course = $course->id;
1808         $record->type = 'news';
1809         $forum = self::getDataGenerator()->create_module('forum', $record);
1811         self::setUser($user1);
1812         $this->getDataGenerator()->enrol_user($user1->id, $course->id);
1814         try {
1815             mod_forum_external::add_discussion($forum->id, 'the subject', 'some text here...');
1816             $this->fail('Exception expected due to invalid permissions.');
1817         } catch (moodle_exception $e) {
1818             $this->assertEquals('cannotcreatediscussion', $e->errorcode);
1819         }
1821         self::setAdminUser();
1822         $createddiscussion = mod_forum_external::add_discussion($forum->id, 'the subject', 'some text here...');
1823         $createddiscussion = external_api::clean_returnvalue(mod_forum_external::add_discussion_returns(), $createddiscussion);
1825         $discussions = mod_forum_external::get_forum_discussions_paginated($forum->id);
1826         $discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_paginated_returns(), $discussions);
1828         $this->assertCount(1, $discussions['discussions']);
1829         $this->assertCount(0, $discussions['warnings']);
1831         $this->assertEquals($createddiscussion['discussionid'], $discussions['discussions'][0]['discussion']);
1832         $this->assertEquals(-1, $discussions['discussions'][0]['groupid']);
1833         $this->assertEquals('the subject', $discussions['discussions'][0]['subject']);
1834         $this->assertEquals('some text here...', $discussions['discussions'][0]['message']);
1836         $discussion2pinned = mod_forum_external::add_discussion($forum->id, 'the pinned subject', 'some 2 text here...', -1,
1837                                                                 array('options' => array('name' => 'discussionpinned',
1838                                                                                          'value' => true)));
1839         $discussion3 = mod_forum_external::add_discussion($forum->id, 'the non pinnedsubject', 'some 3 text here...');
1840         $discussions = mod_forum_external::get_forum_discussions_paginated($forum->id);
1841         $discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_paginated_returns(), $discussions);
1842         $this->assertCount(3, $discussions['discussions']);
1843         $this->assertEquals($discussion2pinned['discussionid'], $discussions['discussions'][0]['discussion']);
1845         // Test inline and regular attachment in new discussion
1846         // Create a file in a draft area for inline attachments.
1848         $fs = get_file_storage();
1850         $draftidinlineattach = file_get_unused_draft_itemid();
1851         $draftidattach = file_get_unused_draft_itemid();
1853         $usercontext = context_user::instance($USER->id);
1854         $filepath = '/';
1855         $filearea = 'draft';
1856         $component = 'user';
1857         $filenameimg = 'shouldbeanimage.txt';
1858         $filerecord = array(
1859             'contextid' => $usercontext->id,
1860             'component' => $component,
1861             'filearea'  => $filearea,
1862             'itemid'    => $draftidinlineattach,
1863             'filepath'  => $filepath,
1864             'filename'  => $filenameimg,
1865         );
1867         // Create a file in a draft area for regular attachments.
1868         $filerecordattach = $filerecord;
1869         $attachfilename = 'attachment.txt';
1870         $filerecordattach['filename'] = $attachfilename;
1871         $filerecordattach['itemid'] = $draftidattach;
1872         $fs->create_file_from_string($filerecord, 'image contents (not really)');
1873         $fs->create_file_from_string($filerecordattach, 'simple text attachment');
1875         $dummytext = 'Here is an inline image: <img src="' . $CFG->wwwroot .
1876                     "/draftfile.php/{$usercontext->id}/user/draft/{$draftidinlineattach}/{$filenameimg}" .
1877                     '" alt="inlineimage">.';
1879         $options = array(array('name' => 'inlineattachmentsid', 'value' => $draftidinlineattach),
1880                          array('name' => 'attachmentsid', 'value' => $draftidattach));
1881         $createddiscussion = mod_forum_external::add_discussion($forum->id, 'the inline attachment subject',
1882                                                                 $dummytext, -1, $options);
1883         $createddiscussion = external_api::clean_returnvalue(mod_forum_external::add_discussion_returns(), $createddiscussion);
1885         $discussions = mod_forum_external::get_forum_discussions_paginated($forum->id);
1886         $discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_paginated_returns(), $discussions);
1888         $this->assertCount(4, $discussions['discussions']);
1889         $this->assertCount(0, $createddiscussion['warnings']);
1890         // Can't guarantee order of posts during tests.
1891         $postfound = false;
1892         foreach ($discussions['discussions'] as $thisdiscussion) {
1893             if ($createddiscussion['discussionid'] == $thisdiscussion['discussion']) {
1894                 $this->assertEquals($thisdiscussion['attachment'], 1, "There should be a non-inline attachment");
1895                 $this->assertCount(1, $thisdiscussion['attachments'], "There should be 1 attachment");
1896                 $this->assertEquals($thisdiscussion['attachments'][0]['filename'], $attachfilename, "There should be 1 attachment");
1897                 $this->assertNotContains('draftfile.php', $thisdiscussion['message']);
1898                 $this->assertContains('pluginfile.php', $thisdiscussion['message']);
1899                 $postfound = true;
1900                 break;
1901             }
1902         }
1904         $this->assertTrue($postfound);
1905     }
1907     /**
1908      * Test adding discussions in a course with gorups
1909      */
1910     public function test_add_discussion_in_course_with_groups() {
1911         global $CFG;
1913         $this->resetAfterTest(true);
1915         // Create course to add the module.
1916         $course = self::getDataGenerator()->create_course(array('groupmode' => VISIBLEGROUPS, 'groupmodeforce' => 0));
1917         $user = self::getDataGenerator()->create_user();
1918         $this->getDataGenerator()->enrol_user($user->id, $course->id);
1920         // Forum forcing separate gropus.
1921         $record = new stdClass();
1922         $record->course = $course->id;
1923         $forum = self::getDataGenerator()->create_module('forum', $record, array('groupmode' => SEPARATEGROUPS));
1925         // Try to post (user not enrolled).
1926         self::setUser($user);
1928         // The user is not enroled in any group, try to post in a forum with separate groups.
1929         try {
1930             mod_forum_external::add_discussion($forum->id, 'the subject', 'some text here...');
1931             $this->fail('Exception expected due to invalid group permissions.');
1932         } catch (moodle_exception $e) {
1933             $this->assertEquals('cannotcreatediscussion', $e->errorcode);
1934         }
1936         try {
1937             mod_forum_external::add_discussion($forum->id, 'the subject', 'some text here...', 0);
1938             $this->fail('Exception expected due to invalid group permissions.');
1939         } catch (moodle_exception $e) {
1940             $this->assertEquals('cannotcreatediscussion', $e->errorcode);
1941         }
1943         // Create a group.
1944         $group = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
1946         // Try to post in a group the user is not enrolled.
1947         try {
1948             mod_forum_external::add_discussion($forum->id, 'the subject', 'some text here...', $group->id);
1949             $this->fail('Exception expected due to invalid group permissions.');
1950         } catch (moodle_exception $e) {
1951             $this->assertEquals('cannotcreatediscussion', $e->errorcode);
1952         }
1954         // Add the user to a group.
1955         groups_add_member($group->id, $user->id);
1957         // Try to post in a group the user is not enrolled.
1958         try {
1959             mod_forum_external::add_discussion($forum->id, 'the subject', 'some text here...', $group->id + 1);
1960             $this->fail('Exception expected due to invalid group.');
1961         } catch (moodle_exception $e) {
1962             $this->assertEquals('cannotcreatediscussion', $e->errorcode);
1963         }
1965         // Nost add the discussion using a valid group.
1966         $discussion = mod_forum_external::add_discussion($forum->id, 'the subject', 'some text here...', $group->id);
1967         $discussion = external_api::clean_returnvalue(mod_forum_external::add_discussion_returns(), $discussion);
1969         $discussions = mod_forum_external::get_forum_discussions_paginated($forum->id);
1970         $discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_paginated_returns(), $discussions);
1972         $this->assertCount(1, $discussions['discussions']);
1973         $this->assertCount(0, $discussions['warnings']);
1974         $this->assertEquals($discussion['discussionid'], $discussions['discussions'][0]['discussion']);
1975         $this->assertEquals($group->id, $discussions['discussions'][0]['groupid']);
1977         // Now add a discussions without indicating a group. The function should guess the correct group.
1978         $discussion = mod_forum_external::add_discussion($forum->id, 'the subject', 'some text here...');
1979         $discussion = external_api::clean_returnvalue(mod_forum_external::add_discussion_returns(), $discussion);
1981         $discussions = mod_forum_external::get_forum_discussions_paginated($forum->id);
1982         $discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_paginated_returns(), $discussions);
1984         $this->assertCount(2, $discussions['discussions']);
1985         $this->assertCount(0, $discussions['warnings']);
1986         $this->assertEquals($group->id, $discussions['discussions'][0]['groupid']);
1987         $this->assertEquals($group->id, $discussions['discussions'][1]['groupid']);
1989         // Enrol the same user in other group.
1990         $group2 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
1991         groups_add_member($group2->id, $user->id);
1993         // Now add a discussions without indicating a group. The function should guess the correct group (the first one).
1994         $discussion = mod_forum_external::add_discussion($forum->id, 'the subject', 'some text here...');
1995         $discussion = external_api::clean_returnvalue(mod_forum_external::add_discussion_returns(), $discussion);
1997         $discussions = mod_forum_external::get_forum_discussions_paginated($forum->id);
1998         $discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_paginated_returns(), $discussions);
2000         $this->assertCount(3, $discussions['discussions']);
2001         $this->assertCount(0, $discussions['warnings']);
2002         $this->assertEquals($group->id, $discussions['discussions'][0]['groupid']);
2003         $this->assertEquals($group->id, $discussions['discussions'][1]['groupid']);
2004         $this->assertEquals($group->id, $discussions['discussions'][2]['groupid']);
2006     }
2008     /*
2009      * Test set_lock_state.
2010      */
2011     public function test_set_lock_state() {
2012         global $DB;
2013         $this->resetAfterTest(true);
2015         // Create courses to add the modules.
2016         $course = self::getDataGenerator()->create_course();
2017         $user = self::getDataGenerator()->create_user();
2018         $studentrole = $DB->get_record('role', array('shortname' => 'student'));
2020         // First forum with tracking off.
2021         $record = new stdClass();
2022         $record->course = $course->id;
2023         $record->type = 'news';
2024         $forum = self::getDataGenerator()->create_module('forum', $record);
2026         $record = new stdClass();
2027         $record->course = $course->id;
2028         $record->userid = $user->id;
2029         $record->forum = $forum->id;
2030         $discussion = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
2032         // User who is a student.
2033         self::setUser($user);
2034         $this->getDataGenerator()->enrol_user($user->id, $course->id, $studentrole->id, 'manual');
2036         // Only a teacher should be able to lock a discussion.
2037         try {
2038             $result = mod_forum_external::set_lock_state($forum->id, $discussion->id, 0);
2039             $this->fail('Exception expected due to missing capability.');
2040         } catch (moodle_exception $e) {
2041             $this->assertEquals('errorcannotlock', $e->errorcode);
2042         }
2044         // Set the lock.
2045         self::setAdminUser();
2046         $result = mod_forum_external::set_lock_state($forum->id, $discussion->id, 0);
2047         $result = external_api::clean_returnvalue(mod_forum_external::set_lock_state_returns(), $result);
2048         $this->assertTrue($result['locked']);
2049         $this->assertNotEquals(0, $result['times']['locked']);
2051         // Unset the lock.
2052         $result = mod_forum_external::set_lock_state($forum->id, $discussion->id, time());
2053         $result = external_api::clean_returnvalue(mod_forum_external::set_lock_state_returns(), $result);
2054         $this->assertFalse($result['locked']);
2055         $this->assertEquals('0', $result['times']['locked']);
2056     }
2058     /*
2059      * Test can_add_discussion. A basic test since all the API functions are already covered by unit tests.
2060      */
2061     public function test_can_add_discussion() {
2062         global $DB;
2063         $this->resetAfterTest(true);
2065         // Create courses to add the modules.
2066         $course = self::getDataGenerator()->create_course();
2068         $user = self::getDataGenerator()->create_user();
2070         // First forum with tracking off.
2071         $record = new stdClass();
2072         $record->course = $course->id;
2073         $record->type = 'news';
2074         $forum = self::getDataGenerator()->create_module('forum', $record);
2076         // User with no permissions to add in a news forum.
2077         self::setUser($user);
2078         $this->getDataGenerator()->enrol_user($user->id, $course->id);
2080         $result = mod_forum_external::can_add_discussion($forum->id);
2081         $result = external_api::clean_returnvalue(mod_forum_external::can_add_discussion_returns(), $result);
2082         $this->assertFalse($result['status']);
2083         $this->assertFalse($result['canpindiscussions']);
2084         $this->assertTrue($result['cancreateattachment']);
2086         // Disable attachments.
2087         $DB->set_field('forum', 'maxattachments', 0, array('id' => $forum->id));
2088         $result = mod_forum_external::can_add_discussion($forum->id);
2089         $result = external_api::clean_returnvalue(mod_forum_external::can_add_discussion_returns(), $result);
2090         $this->assertFalse($result['status']);
2091         $this->assertFalse($result['canpindiscussions']);
2092         $this->assertFalse($result['cancreateattachment']);
2093         $DB->set_field('forum', 'maxattachments', 1, array('id' => $forum->id));    // Enable attachments again.
2095         self::setAdminUser();
2096         $result = mod_forum_external::can_add_discussion($forum->id);
2097         $result = external_api::clean_returnvalue(mod_forum_external::can_add_discussion_returns(), $result);
2098         $this->assertTrue($result['status']);
2099         $this->assertTrue($result['canpindiscussions']);
2100         $this->assertTrue($result['cancreateattachment']);
2101     }
2103     /*
2104      * A basic test to make sure users cannot post to forum after the cutoff date.
2105      */
2106     public function test_can_add_discussion_after_cutoff() {
2107         $this->resetAfterTest(true);
2109         // Create courses to add the modules.
2110         $course = self::getDataGenerator()->create_course();
2112         $user = self::getDataGenerator()->create_user();
2114         // Create a forum with cutoff date set to a past date.
2115         $forum = self::getDataGenerator()->create_module('forum', ['course' => $course->id, 'cutoffdate' => time() - 1]);
2117         // User with no mod/forum:canoverridecutoff capability.
2118         self::setUser($user);
2119         $this->getDataGenerator()->enrol_user($user->id, $course->id);
2121         $result = mod_forum_external::can_add_discussion($forum->id);
2122         $result = external_api::clean_returnvalue(mod_forum_external::can_add_discussion_returns(), $result);
2123         $this->assertFalse($result['status']);
2125         self::setAdminUser();
2126         $result = mod_forum_external::can_add_discussion($forum->id);
2127         $result = external_api::clean_returnvalue(mod_forum_external::can_add_discussion_returns(), $result);
2128         $this->assertTrue($result['status']);
2129     }
2131     /**
2132      * Test get forum posts discussions including rating information.
2133      */
2134     public function test_mod_forum_get_forum_discussion_rating_information() {
2135         global $DB, $CFG;
2136         require_once($CFG->dirroot . '/rating/lib.php');
2138         $this->resetAfterTest(true);
2140         $user1 = self::getDataGenerator()->create_user();
2141         $user2 = self::getDataGenerator()->create_user();
2142         $user3 = self::getDataGenerator()->create_user();
2143         $teacher = self::getDataGenerator()->create_user();
2145         // Create course to add the module.
2146         $course = self::getDataGenerator()->create_course();
2148         $studentrole = $DB->get_record('role', array('shortname' => 'student'));
2149         $teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'));
2150         $this->getDataGenerator()->enrol_user($user1->id, $course->id, $studentrole->id, 'manual');
2151         $this->getDataGenerator()->enrol_user($user2->id, $course->id, $studentrole->id, 'manual');
2152         $this->getDataGenerator()->enrol_user($user3->id, $course->id, $studentrole->id, 'manual');
2153         $this->getDataGenerator()->enrol_user($teacher->id, $course->id, $teacherrole->id, 'manual');
2155         // Create the forum.
2156         $record = new stdClass();
2157         $record->course = $course->id;
2158         // Set Aggregate type = Average of ratings.
2159         $record->assessed = RATING_AGGREGATE_AVERAGE;
2160         $record->scale = 100;
2161         $forum = self::getDataGenerator()->create_module('forum', $record);
2162         $context = context_module::instance($forum->cmid);
2164         // Add discussion to the forum.
2165         $record = new stdClass();
2166         $record->course = $course->id;
2167         $record->userid = $user1->id;
2168         $record->forum = $forum->id;
2169         $discussion = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
2171         // Retrieve the first post.
2172         $post = $DB->get_record('forum_posts', array('discussion' => $discussion->id));
2174         // Rate the discussion as user2.
2175         $rating1 = new stdClass();
2176         $rating1->contextid = $context->id;
2177         $rating1->component = 'mod_forum';
2178         $rating1->ratingarea = 'post';
2179         $rating1->itemid = $post->id;
2180         $rating1->rating = 50;
2181         $rating1->scaleid = 100;
2182         $rating1->userid = $user2->id;
2183         $rating1->timecreated = time();
2184         $rating1->timemodified = time();
2185         $rating1->id = $DB->insert_record('rating', $rating1);
2187         // Rate the discussion as user3.
2188         $rating2 = new stdClass();
2189         $rating2->contextid = $context->id;
2190         $rating2->component = 'mod_forum';
2191         $rating2->ratingarea = 'post';
2192         $rating2->itemid = $post->id;
2193         $rating2->rating = 100;
2194         $rating2->scaleid = 100;
2195         $rating2->userid = $user3->id;
2196         $rating2->timecreated = time() + 1;
2197         $rating2->timemodified = time() + 1;
2198         $rating2->id = $DB->insert_record('rating', $rating2);
2200         // Retrieve the rating for the post as student.
2201         $this->setUser($user1);
2202         $posts = mod_forum_external::get_forum_discussion_posts($discussion->id);
2203         $posts = external_api::clean_returnvalue(mod_forum_external::get_forum_discussion_posts_returns(), $posts);
2204         $this->assertCount(1, $posts['ratinginfo']['ratings']);
2205         $this->assertTrue($posts['ratinginfo']['ratings'][0]['canviewaggregate']);
2206         $this->assertFalse($posts['ratinginfo']['canviewall']);
2207         $this->assertFalse($posts['ratinginfo']['ratings'][0]['canrate']);
2208         $this->assertEquals(2, $posts['ratinginfo']['ratings'][0]['count']);
2209         $this->assertEquals(($rating1->rating + $rating2->rating) / 2, $posts['ratinginfo']['ratings'][0]['aggregate']);
2211         // Retrieve the rating for the post as teacher.
2212         $this->setUser($teacher);
2213         $posts = mod_forum_external::get_forum_discussion_posts($discussion->id);
2214         $posts = external_api::clean_returnvalue(mod_forum_external::get_forum_discussion_posts_returns(), $posts);
2215         $this->assertCount(1, $posts['ratinginfo']['ratings']);
2216         $this->assertTrue($posts['ratinginfo']['ratings'][0]['canviewaggregate']);
2217         $this->assertTrue($posts['ratinginfo']['canviewall']);
2218         $this->assertTrue($posts['ratinginfo']['ratings'][0]['canrate']);
2219         $this->assertEquals(2, $posts['ratinginfo']['ratings'][0]['count']);
2220         $this->assertEquals(($rating1->rating + $rating2->rating) / 2, $posts['ratinginfo']['ratings'][0]['aggregate']);
2221     }
2223     /**
2224      * Test mod_forum_get_forum_access_information.
2225      */
2226     public function test_mod_forum_get_forum_access_information() {
2227         global $DB;
2229         $this->resetAfterTest(true);
2231         $student = self::getDataGenerator()->create_user();
2232         $course = self::getDataGenerator()->create_course();
2233         // Create the forum.
2234         $record = new stdClass();
2235         $record->course = $course->id;
2236         $forum = self::getDataGenerator()->create_module('forum', $record);
2238         $studentrole = $DB->get_record('role', array('shortname' => 'student'));
2239         $this->getDataGenerator()->enrol_user($student->id, $course->id, $studentrole->id, 'manual');
2241         self::setUser($student);
2242         $result = mod_forum_external::get_forum_access_information($forum->id);
2243         $result = external_api::clean_returnvalue(mod_forum_external::get_forum_access_information_returns(), $result);
2245         // Check default values for capabilities.
2246         $enabledcaps = array('canviewdiscussion', 'canstartdiscussion', 'canreplypost', 'canviewrating', 'cancreateattachment',
2247             'canexportownpost', 'cancantogglefavourite', 'candeleteownpost', 'canallowforcesubscribe');
2249         unset($result['warnings']);
2250         foreach ($result as $capname => $capvalue) {
2251             if (in_array($capname, $enabledcaps)) {
2252                 $this->assertTrue($capvalue);
2253             } else {
2254                 $this->assertFalse($capvalue);
2255             }
2256         }
2257         // Now, unassign some capabilities.
2258         unassign_capability('mod/forum:deleteownpost', $studentrole->id);
2259         unassign_capability('mod/forum:allowforcesubscribe', $studentrole->id);
2260         array_pop($enabledcaps);
2261         array_pop($enabledcaps);
2262         accesslib_clear_all_caches_for_unit_testing();
2264         $result = mod_forum_external::get_forum_access_information($forum->id);
2265         $result = external_api::clean_returnvalue(mod_forum_external::get_forum_access_information_returns(), $result);
2266         unset($result['warnings']);
2267         foreach ($result as $capname => $capvalue) {
2268             if (in_array($capname, $enabledcaps)) {
2269                 $this->assertTrue($capvalue);
2270             } else {
2271                 $this->assertFalse($capvalue);
2272             }
2273         }
2274     }
2276     /**
2277      * Test add_discussion_post
2278      */
2279     public function test_add_discussion_post_private() {
2280         global $DB;
2282         $this->resetAfterTest(true);
2284         self::setAdminUser();
2286         // Create course to add the module.
2287         $course = self::getDataGenerator()->create_course();
2289         // Standard forum.
2290         $record = new stdClass();
2291         $record->course = $course->id;
2292         $forum = self::getDataGenerator()->create_module('forum', $record);
2293         $cm = get_coursemodule_from_id('forum', $forum->cmid, 0, false, MUST_EXIST);
2294         $forumcontext = context_module::instance($forum->cmid);
2295         $generator = self::getDataGenerator()->get_plugin_generator('mod_forum');
2297         // Create an enrol users.
2298         $student1 = self::getDataGenerator()->create_user();
2299         $this->getDataGenerator()->enrol_user($student1->id, $course->id, 'student');
2300         $student2 = self::getDataGenerator()->create_user();
2301         $this->getDataGenerator()->enrol_user($student2->id, $course->id, 'student');
2302         $teacher1 = self::getDataGenerator()->create_user();
2303         $this->getDataGenerator()->enrol_user($teacher1->id, $course->id, 'editingteacher');
2304         $teacher2 = self::getDataGenerator()->create_user();
2305         $this->getDataGenerator()->enrol_user($teacher2->id, $course->id, 'editingteacher');
2307         // Add a new discussion to the forum.
2308         self::setUser($student1);
2309         $record = new stdClass();
2310         $record->course = $course->id;
2311         $record->userid = $student1->id;
2312         $record->forum = $forum->id;
2313         $discussion = $generator->create_discussion($record);
2315         // Have the teacher reply privately.
2316         self::setUser($teacher1);
2317         $post = mod_forum_external::add_discussion_post($discussion->firstpost, 'some subject', 'some text here...', [
2318                 [
2319                     'name' => 'private',
2320                     'value' => true,
2321                 ],
2322             ]);
2323         $post = external_api::clean_returnvalue(mod_forum_external::add_discussion_post_returns(), $post);
2324         $privatereply = $DB->get_record('forum_posts', array('id' => $post['postid']));
2325         $this->assertEquals($student1->id, $privatereply->privatereplyto);
2326         // Bump the time of the private reply to ensure order.
2327         $privatereply->created++;
2328         $privatereply->modified = $privatereply->created;
2329         $DB->update_record('forum_posts', $privatereply);
2331         // The teacher will receive their private reply.
2332         self::setUser($teacher1);
2333         $posts = mod_forum_external::get_forum_discussion_posts($discussion->id);
2334         $posts = external_api::clean_returnvalue(mod_forum_external::get_forum_discussion_posts_returns(), $posts);
2335         $this->assertEquals(2, count($posts['posts']));
2336         $this->assertTrue($posts['posts'][0]['isprivatereply']);
2338         // Another teacher on the course will also receive the private reply.
2339         self::setUser($teacher2);
2340         $posts = mod_forum_external::get_forum_discussion_posts($discussion->id);
2341         $posts = external_api::clean_returnvalue(mod_forum_external::get_forum_discussion_posts_returns(), $posts);
2342         $this->assertEquals(2, count($posts['posts']));
2343         $this->assertTrue($posts['posts'][0]['isprivatereply']);
2345         // The student will receive the private reply.
2346         self::setUser($student1);
2347         $posts = mod_forum_external::get_forum_discussion_posts($discussion->id);
2348         $posts = external_api::clean_returnvalue(mod_forum_external::get_forum_discussion_posts_returns(), $posts);
2349         $this->assertEquals(2, count($posts['posts']));
2350         $this->assertTrue($posts['posts'][0]['isprivatereply']);
2352         // Another student will not receive the private reply.
2353         self::setUser($student2);
2354         $posts = mod_forum_external::get_forum_discussion_posts($discussion->id);
2355         $posts = external_api::clean_returnvalue(mod_forum_external::get_forum_discussion_posts_returns(), $posts);
2356         $this->assertEquals(1, count($posts['posts']));
2357         $this->assertFalse($posts['posts'][0]['isprivatereply']);
2359         // A user cannot reply to a private reply.
2360         self::setUser($teacher2);
2361         $this->expectException('coding_exception');
2362         $post = mod_forum_external::add_discussion_post($privatereply->id, 'some subject', 'some text here...', [
2363                 'options' => [
2364                     'name' => 'private',
2365                     'value' => false,
2366                 ],
2367             ]);
2368     }
2370     /**
2371      * Test trusted text enabled.
2372      */
2373     public function test_trusted_text_enabled() {
2374         global $USER, $CFG;
2376         $this->resetAfterTest(true);
2377         $CFG->enabletrusttext = 1;
2379         $dangeroustext = '<button>Untrusted text</button>';
2380         $cleantext = 'Untrusted text';
2382         // Create courses to add the modules.
2383         $course = self::getDataGenerator()->create_course();
2384         $user1 = self::getDataGenerator()->create_user();
2386         // First forum with tracking off.
2387         $record = new stdClass();
2388         $record->course = $course->id;
2389         $record->type = 'qanda';
2390         $forum = self::getDataGenerator()->create_module('forum', $record);
2391         $context = context_module::instance($forum->cmid);
2393         // Add discussions to the forums.
2394         $discussionrecord = new stdClass();
2395         $discussionrecord->course = $course->id;
2396         $discussionrecord->userid = $user1->id;
2397         $discussionrecord->forum = $forum->id;
2398         $discussionrecord->message = $dangeroustext;
2399         $discussionrecord->messagetrust  = trusttext_trusted($context);
2400         $discussion1 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($discussionrecord);
2402         self::setAdminUser();
2403         $discussionrecord->userid = $USER->id;
2404         $discussionrecord->messagetrust  = trusttext_trusted($context);
2405         $discussion2 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($discussionrecord);
2407         $discussions = mod_forum_external::get_forum_discussions_paginated($forum->id);
2408         $discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_paginated_returns(), $discussions);
2410         $this->assertCount(2, $discussions['discussions']);
2411         $this->assertCount(0, $discussions['warnings']);
2412         // Admin message is fully trusted.
2413         $this->assertEquals(1, $discussions['discussions'][0]['messagetrust']);
2414         $this->assertEquals($dangeroustext, $discussions['discussions'][0]['message']);
2415         // Student message is not trusted.
2416         $this->assertEquals(0, $discussions['discussions'][1]['messagetrust']);
2417         $this->assertEquals($cleantext, $discussions['discussions'][1]['message']);
2419         // Get posts now.
2420         $posts = mod_forum_external::get_forum_discussion_posts($discussion2->id);
2421         $posts = external_api::clean_returnvalue(mod_forum_external::get_forum_discussion_posts_returns(), $posts);
2422         // Admin message is fully trusted.
2423         $this->assertEquals(1, $posts['posts'][0]['messagetrust']);
2424         $this->assertEquals($dangeroustext, $posts['posts'][0]['message']);
2426         $posts = mod_forum_external::get_forum_discussion_posts($discussion1->id);
2427         $posts = external_api::clean_returnvalue(mod_forum_external::get_forum_discussion_posts_returns(), $posts);
2428         // Student message is not trusted.
2429         $this->assertEquals(0, $posts['posts'][0]['messagetrust']);
2430         $this->assertEquals($cleantext, $posts['posts'][0]['message']);
2431     }
2433     /**
2434      * Test trusted text disabled.
2435      */
2436     public function test_trusted_text_disabled() {
2437         global $USER, $CFG;
2439         $this->resetAfterTest(true);
2440         $CFG->enabletrusttext = 0;
2442         $dangeroustext = '<button>Untrusted text</button>';
2443         $cleantext = 'Untrusted text';
2445         // Create courses to add the modules.
2446         $course = self::getDataGenerator()->create_course();
2447         $user1 = self::getDataGenerator()->create_user();
2449         // First forum with tracking off.
2450         $record = new stdClass();
2451         $record->course = $course->id;
2452         $record->type = 'qanda';
2453         $forum = self::getDataGenerator()->create_module('forum', $record);
2454         $context = context_module::instance($forum->cmid);
2456         // Add discussions to the forums.
2457         $discussionrecord = new stdClass();
2458         $discussionrecord->course = $course->id;
2459         $discussionrecord->userid = $user1->id;
2460         $discussionrecord->forum = $forum->id;
2461         $discussionrecord->message = $dangeroustext;
2462         $discussionrecord->messagetrust  = trusttext_trusted($context);
2463         $discussion1 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($discussionrecord);
2465         self::setAdminUser();
2466         $discussionrecord->userid = $USER->id;
2467         $discussionrecord->messagetrust  = trusttext_trusted($context);
2468         $discussion2 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($discussionrecord);
2470         $discussions = mod_forum_external::get_forum_discussions($forum->id);
2471         $discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_returns(), $discussions);
2473         $this->assertCount(2, $discussions['discussions']);
2474         $this->assertCount(0, $discussions['warnings']);
2475         // Admin message is not trusted because enabletrusttext is disabled.
2476         $this->assertEquals(0, $discussions['discussions'][0]['messagetrust']);
2477         $this->assertEquals($cleantext, $discussions['discussions'][0]['message']);
2478         // Student message is not trusted.
2479         $this->assertEquals(0, $discussions['discussions'][1]['messagetrust']);
2480         $this->assertEquals($cleantext, $discussions['discussions'][1]['message']);
2482         // Get posts now.
2483         $posts = mod_forum_external::get_forum_discussion_posts($discussion2->id);
2484         $posts = external_api::clean_returnvalue(mod_forum_external::get_forum_discussion_posts_returns(), $posts);
2485         // Admin message is not trusted because enabletrusttext is disabled.
2486         $this->assertEquals(0, $posts['posts'][0]['messagetrust']);
2487         $this->assertEquals($cleantext, $posts['posts'][0]['message']);
2489         $posts = mod_forum_external::get_forum_discussion_posts($discussion1->id);
2490         $posts = external_api::clean_returnvalue(mod_forum_external::get_forum_discussion_posts_returns(), $posts);
2491         // Student message is not trusted.
2492         $this->assertEquals(0, $posts['posts'][0]['messagetrust']);
2493         $this->assertEquals($cleantext, $posts['posts'][0]['message']);
2494     }