MDL-65675 forum: Remove duplicate Re in subjects
[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             'groups' => [],
552             'urls' => [
553                 'profile' => $urlfactory->get_author_profile_url($user2entity),
554                 'profileimage' => $urlfactory->get_author_profile_image_url($user2entity),
555             ]
556         ];
557         $user2->fullname = $exporteduser2['fullname'];
559         $user3 = self::getDataGenerator()->create_user(['fullname' => "Mr Pants 1"]);
560         $user3entity = $entityfactory->get_author_from_stdclass($user3);
561         $exporteduser3 = [
562             'id' => (int) $user3->id,
563             'fullname' => fullname($user3),
564             'groups' => [],
565             'urls' => [
566                 'profile' => $urlfactory->get_author_profile_url($user3entity),
567                 'profileimage' => $urlfactory->get_author_profile_image_url($user3entity),
568             ]
569         ];
570         $user3->fullname = $exporteduser3['fullname'];
571         $forumgenerator = self::getDataGenerator()->get_plugin_generator('mod_forum');
573         // Set the first created user to the test user.
574         self::setUser($user1);
576         // Create course to add the module.
577         $course1 = self::getDataGenerator()->create_course();
579         // Forum with tracking off.
580         $record = new stdClass();
581         $record->course = $course1->id;
582         $record->trackingtype = FORUM_TRACKING_OFF;
583         $forum1 = self::getDataGenerator()->create_module('forum', $record);
584         $forum1context = context_module::instance($forum1->cmid);
586         // Forum with tracking enabled.
587         $record = new stdClass();
588         $record->course = $course1->id;
589         $forum2 = self::getDataGenerator()->create_module('forum', $record);
590         $forum2cm = get_coursemodule_from_id('forum', $forum2->cmid);
591         $forum2context = context_module::instance($forum2->cmid);
593         // Add discussions to the forums.
594         $record = new stdClass();
595         $record->course = $course1->id;
596         $record->userid = $user1->id;
597         $record->forum = $forum1->id;
598         $discussion1 = $forumgenerator->create_discussion($record);
600         $record = new stdClass();
601         $record->course = $course1->id;
602         $record->userid = $user2->id;
603         $record->forum = $forum1->id;
604         $discussion2 = $forumgenerator->create_discussion($record);
606         $record = new stdClass();
607         $record->course = $course1->id;
608         $record->userid = $user2->id;
609         $record->forum = $forum2->id;
610         $discussion3 = $forumgenerator->create_discussion($record);
612         // Add 2 replies to the discussion 1 from different users.
613         $record = new stdClass();
614         $record->discussion = $discussion1->id;
615         $record->parent = $discussion1->firstpost;
616         $record->userid = $user2->id;
617         $discussion1reply1 = $forumgenerator->create_post($record);
618         $filename = 'shouldbeanimage.jpg';
619         // Add a fake inline image to the post.
620         $filerecordinline = array(
621             'contextid' => $forum1context->id,
622             'component' => 'mod_forum',
623             'filearea'  => 'post',
624             'itemid'    => $discussion1reply1->id,
625             'filepath'  => '/',
626             'filename'  => $filename,
627         );
628         $fs = get_file_storage();
629         $timepost = time();
630         $fs->create_file_from_string($filerecordinline, 'image contents (not really)');
632         $record->parent = $discussion1reply1->id;
633         $record->userid = $user3->id;
634         $discussion1reply2 = $forumgenerator->create_post($record);
636         // Enrol the user in the  course.
637         $enrol = enrol_get_plugin('manual');
638         // Following line enrol and assign default role id to the user.
639         // So the user automatically gets mod/forum:viewdiscussion on all forums of the course.
640         $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
641         $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
643         // Delete one user, to test that we still receive posts by this user.
644         delete_user($user3);
646         // Create what we expect to be returned when querying the discussion.
647         $expectedposts = array(
648             'posts' => array(),
649             'ratinginfo' => array(
650                 'contextid' => $forum1context->id,
651                 'component' => 'mod_forum',
652                 'ratingarea' => 'post',
653                 'canviewall' => null,
654                 'canviewany' => null,
655                 'scales' => array(),
656                 'ratings' => array(),
657             ),
658             'warnings' => array(),
659         );
661         // User pictures are initially empty, we should get the links once the external function is called.
662         $isolatedurl = $urlfactory->get_discussion_view_url_from_discussion_id($discussion1reply2->discussion);
663         $isolatedurl->params(['parent' => $discussion1reply2->id]);
664         $expectedposts['posts'][] = array(
665             'id' => $discussion1reply2->id,
666             'discussionid' => $discussion1reply2->discussion,
667             'parentid' => $discussion1reply2->parent,
668             'hasparent' => true,
669             'timecreated' => $discussion1reply2->created,
670             'subject' => $discussion1reply2->subject,
671             'replysubject' => get_string('re', 'mod_forum') . " {$discussion1reply2->subject}",
672             'message' => file_rewrite_pluginfile_urls($discussion1reply2->message, 'pluginfile.php',
673                     $forum1context->id, 'mod_forum', 'post', $discussion1reply2->id),
674             'messageformat' => 1,   // This value is usually changed by external_format_text() function.
675             'unread' => null,
676             'isdeleted' => false,
677             'isprivatereply' => false,
678             'haswordcount' => false,
679             'wordcount' => null,
680             'author'=> $exporteduser3,
681             'attachments' => [],
682             'tags' => [],
683             'html' => [
684                 'rating' => null,
685                 'taglist' => null,
686                 'authorsubheading' => $forumgenerator->get_author_subheading_html((object)$exporteduser3, $discussion1reply2->created)
687             ],
688             'capabilities' => [
689                 'view' => 1,
690                 'edit' => 0,
691                 'delete' => 0,
692                 'split' => 0,
693                 'reply' => 1,
694                 'export' => 0,
695                 'controlreadstatus' => 0,
696                 'canreplyprivately' => 0
697             ],
698             'urls' => [
699                 'view' => $urlfactory->get_view_post_url_from_post_id($discussion1reply2->discussion, $discussion1reply2->id),
700                 'viewisolated' => $isolatedurl->out(false),
701                 'viewparent' => $urlfactory->get_view_post_url_from_post_id($discussion1reply2->discussion, $discussion1reply2->parent),
702                 'edit' => null,
703                 'delete' =>null,
704                 'split' => null,
705                 'reply' => (new moodle_url('/mod/forum/post.php#mformforum', [
706                     'reply' => $discussion1reply2->id
707                 ]))->out(false),
708                 'export' => null,
709                 'markasread' => null,
710                 'markasunread' => null,
711                 'discuss' => $urlfactory->get_discussion_view_url_from_discussion_id($discussion1reply2->discussion),
712             ],
713         );
716         $isolatedurl = $urlfactory->get_discussion_view_url_from_discussion_id($discussion1reply1->discussion);
717         $isolatedurl->params(['parent' => $discussion1reply1->id]);
718         $expectedposts['posts'][] = array(
719             'id' => $discussion1reply1->id,
720             'discussionid' => $discussion1reply1->discussion,
721             'parentid' => $discussion1reply1->parent,
722             'hasparent' => true,
723             'timecreated' => $discussion1reply1->created,
724             'subject' => $discussion1reply1->subject,
725             'replysubject' => get_string('re', 'mod_forum') . " {$discussion1reply1->subject}",
726             'message' => file_rewrite_pluginfile_urls($discussion1reply1->message, 'pluginfile.php',
727                     $forum1context->id, 'mod_forum', 'post', $discussion1reply1->id),
728             'messageformat' => 1,   // This value is usually changed by external_format_text() function.
729             'unread' => null,
730             'isdeleted' => false,
731             'isprivatereply' => false,
732             'haswordcount' => false,
733             'wordcount' => null,
734             'author'=> $exporteduser2,
735             'attachments' => [],
736             'tags' => [],
737             'html' => [
738                 'rating' => null,
739                 'taglist' => null,
740                 'authorsubheading' => $forumgenerator->get_author_subheading_html((object)$exporteduser2, $discussion1reply1->created)
741             ],
742             'capabilities' => [
743                 'view' => 1,
744                 'edit' => 0,
745                 'delete' => 0,
746                 'split' => 0,
747                 'reply' => 1,
748                 'export' => 0,
749                 'controlreadstatus' => 0,
750                 'canreplyprivately' => 0
751             ],
752             'urls' => [
753                 'view' => $urlfactory->get_view_post_url_from_post_id($discussion1reply1->discussion, $discussion1reply1->id),
754                 'viewisolated' => $isolatedurl->out(false),
755                 'viewparent' => $urlfactory->get_view_post_url_from_post_id($discussion1reply1->discussion, $discussion1reply1->parent),
756                 'edit' => null,
757                 'delete' =>null,
758                 'split' => null,
759                 'reply' => (new moodle_url('/mod/forum/post.php#mformforum', [
760                     'reply' => $discussion1reply1->id
761                 ]))->out(false),
762                 'export' => null,
763                 'markasread' => null,
764                 'markasunread' => null,
765                 'discuss' => $urlfactory->get_discussion_view_url_from_discussion_id($discussion1reply1->discussion),
766             ],
767         );
769         // Test a discussion with two additional posts (total 3 posts).
770         $posts = mod_forum_external::get_discussion_posts($discussion1->id, 'modified', 'DESC');
771         $posts = external_api::clean_returnvalue(mod_forum_external::get_discussion_posts_returns(), $posts);
772         $this->assertEquals(3, count($posts['posts']));
774         // Unset the initial discussion post.
775         array_pop($posts['posts']);
776         $this->assertEquals($expectedposts, $posts);
778         // Check we receive the unread count correctly on tracked forum.
779         forum_tp_count_forum_unread_posts($forum2cm, $course1, true);    // Reset static cache.
780         $result = mod_forum_external::get_forums_by_courses(array($course1->id));
781         $result = external_api::clean_returnvalue(mod_forum_external::get_forums_by_courses_returns(), $result);
782         foreach ($result as $f) {
783             if ($f['id'] == $forum2->id) {
784                 $this->assertEquals(1, $f['unreadpostscount']);
785             }
786         }
788         // Test discussion without additional posts. There should be only one post (the one created by the discussion).
789         $posts = mod_forum_external::get_discussion_posts($discussion2->id, 'modified', 'DESC');
790         $posts = external_api::clean_returnvalue(mod_forum_external::get_discussion_posts_returns(), $posts);
791         $this->assertEquals(1, count($posts['posts']));
793         // Test discussion tracking on not tracked forum.
794         $result = mod_forum_external::view_forum_discussion($discussion1->id);
795         $result = external_api::clean_returnvalue(mod_forum_external::view_forum_discussion_returns(), $result);
796         $this->assertTrue($result['status']);
797         $this->assertEmpty($result['warnings']);
799         // Test posts have not been marked as read.
800         $posts = mod_forum_external::get_discussion_posts($discussion1->id, 'modified', 'DESC');
801         $posts = external_api::clean_returnvalue(mod_forum_external::get_discussion_posts_returns(), $posts);
802         foreach ($posts['posts'] as $post) {
803             $this->assertNull($post['unread']);
804         }
806         // Test discussion tracking on tracked forum.
807         $result = mod_forum_external::view_forum_discussion($discussion3->id);
808         $result = external_api::clean_returnvalue(mod_forum_external::view_forum_discussion_returns(), $result);
809         $this->assertTrue($result['status']);
810         $this->assertEmpty($result['warnings']);
812         // Test posts have been marked as read.
813         $posts = mod_forum_external::get_discussion_posts($discussion3->id, 'modified', 'DESC');
814         $posts = external_api::clean_returnvalue(mod_forum_external::get_discussion_posts_returns(), $posts);
815         foreach ($posts['posts'] as $post) {
816             $this->assertFalse($post['unread']);
817         }
819         // Check we receive 0 unread posts.
820         forum_tp_count_forum_unread_posts($forum2cm, $course1, true);    // Reset static cache.
821         $result = mod_forum_external::get_forums_by_courses(array($course1->id));
822         $result = external_api::clean_returnvalue(mod_forum_external::get_forums_by_courses_returns(), $result);
823         foreach ($result as $f) {
824             if ($f['id'] == $forum2->id) {
825                 $this->assertEquals(0, $f['unreadpostscount']);
826             }
827         }
828     }
830     /**
831      * Test get forum posts
832      */
833     public function test_mod_forum_get_forum_discussion_posts_deleted() {
834         global $CFG, $PAGE;
836         $this->resetAfterTest(true);
837         $generator = self::getDataGenerator()->get_plugin_generator('mod_forum');
839         // Create a course and enrol some users in it.
840         $course1 = self::getDataGenerator()->create_course();
842         // Create users.
843         $user1 = self::getDataGenerator()->create_user();
844         $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
845         $user2 = self::getDataGenerator()->create_user();
846         $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
848         // Set the first created user to the test user.
849         self::setUser($user1);
851         // Create test data.
852         $forum1 = self::getDataGenerator()->create_module('forum', (object) [
853                 'course' => $course1->id,
854             ]);
855         $forum1context = context_module::instance($forum1->cmid);
857         // Add discussions to the forum.
858         $discussion = $generator->create_discussion((object) [
859                 'course' => $course1->id,
860                 'userid' => $user1->id,
861                 'forum' => $forum1->id,
862             ]);
864         $discussion2 = $generator->create_discussion((object) [
865                 'course' => $course1->id,
866                 'userid' => $user2->id,
867                 'forum' => $forum1->id,
868             ]);
870         // Add replies to the discussion.
871         $discussionreply1 = $generator->create_post((object) [
872                 'discussion' => $discussion->id,
873                 'parent' => $discussion->firstpost,
874                 'userid' => $user2->id,
875             ]);
876         $discussionreply2 = $generator->create_post((object) [
877                 'discussion' => $discussion->id,
878                 'parent' => $discussionreply1->id,
879                 'userid' => $user2->id,
880                 'subject' => '',
881                 'message' => '',
882                 'messageformat' => FORMAT_PLAIN,
883                 'deleted' => 1,
884             ]);
885         $discussionreply3 = $generator->create_post((object) [
886                 'discussion' => $discussion->id,
887                 'parent' => $discussion->firstpost,
888                 'userid' => $user2->id,
889             ]);
891         // Test where some posts have been marked as deleted.
892         $posts = mod_forum_external::get_forum_discussion_posts($discussion->id, 'modified', 'DESC');
893         $posts = external_api::clean_returnvalue(mod_forum_external::get_forum_discussion_posts_returns(), $posts);
894         $deletedsubject = get_string('privacy:request:delete:post:subject', 'mod_forum');
895         $deletedmessage = get_string('privacy:request:delete:post:message', 'mod_forum');
897         foreach ($posts['posts'] as $post) {
898             if ($post['id'] == $discussionreply2->id) {
899                 $this->assertTrue($post['deleted']);
900                 $this->assertEquals($deletedsubject, $post['subject']);
901                 $this->assertEquals($deletedmessage, $post['message']);
902             } else {
903                 $this->assertFalse($post['deleted']);
904                 $this->assertNotEquals($deletedsubject, $post['subject']);
905                 $this->assertNotEquals($deletedmessage, $post['message']);
906             }
907         }
908     }
910     /**
911      * Test get forum posts (qanda forum)
912      */
913     public function test_mod_forum_get_forum_discussion_posts_qanda() {
914         global $CFG, $DB;
916         $this->resetAfterTest(true);
918         $record = new stdClass();
919         $user1 = self::getDataGenerator()->create_user($record);
920         $user2 = self::getDataGenerator()->create_user();
922         // Set the first created user to the test user.
923         self::setUser($user1);
925         // Create course to add the module.
926         $course1 = self::getDataGenerator()->create_course();
927         $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
928         $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
930         // Forum with tracking off.
931         $record = new stdClass();
932         $record->course = $course1->id;
933         $record->type = 'qanda';
934         $forum1 = self::getDataGenerator()->create_module('forum', $record);
935         $forum1context = context_module::instance($forum1->cmid);
937         // Add discussions to the forums.
938         $record = new stdClass();
939         $record->course = $course1->id;
940         $record->userid = $user2->id;
941         $record->forum = $forum1->id;
942         $discussion1 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
944         // Add 1 reply (not the actual user).
945         $record = new stdClass();
946         $record->discussion = $discussion1->id;
947         $record->parent = $discussion1->firstpost;
948         $record->userid = $user2->id;
949         $discussion1reply1 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
951         // We still see only the original post.
952         $posts = mod_forum_external::get_forum_discussion_posts($discussion1->id, 'modified', 'DESC');
953         $posts = external_api::clean_returnvalue(mod_forum_external::get_forum_discussion_posts_returns(), $posts);
954         $this->assertEquals(1, count($posts['posts']));
956         // Add a new reply, the user is going to be able to see only the original post and their new post.
957         $record = new stdClass();
958         $record->discussion = $discussion1->id;
959         $record->parent = $discussion1->firstpost;
960         $record->userid = $user1->id;
961         $discussion1reply2 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
963         $posts = mod_forum_external::get_forum_discussion_posts($discussion1->id, 'modified', 'DESC');
964         $posts = external_api::clean_returnvalue(mod_forum_external::get_forum_discussion_posts_returns(), $posts);
965         $this->assertEquals(2, count($posts['posts']));
967         // Now, we can fake the time of the user post, so he can se the rest of the discussion posts.
968         $discussion1reply2->created -= $CFG->maxeditingtime * 2;
969         $DB->update_record('forum_posts', $discussion1reply2);
971         $posts = mod_forum_external::get_forum_discussion_posts($discussion1->id, 'modified', 'DESC');
972         $posts = external_api::clean_returnvalue(mod_forum_external::get_forum_discussion_posts_returns(), $posts);
973         $this->assertEquals(3, count($posts['posts']));
974     }
976     /**
977      * Test get forum discussions paginated
978      */
979     public function test_mod_forum_get_forum_discussions_paginated() {
980         global $USER, $CFG, $DB, $PAGE;
982         $this->resetAfterTest(true);
984         // Set the CFG variable to allow track forums.
985         $CFG->forum_trackreadposts = true;
987         // Create a user who can track forums.
988         $record = new stdClass();
989         $record->trackforums = true;
990         $user1 = self::getDataGenerator()->create_user($record);
991         // Create a bunch of other users to post.
992         $user2 = self::getDataGenerator()->create_user();
993         $user3 = self::getDataGenerator()->create_user();
994         $user4 = self::getDataGenerator()->create_user();
996         // Set the first created user to the test user.
997         self::setUser($user1);
999         // Create courses to add the modules.
1000         $course1 = self::getDataGenerator()->create_course();
1002         // First forum with tracking off.
1003         $record = new stdClass();
1004         $record->course = $course1->id;
1005         $record->trackingtype = FORUM_TRACKING_OFF;
1006         $forum1 = self::getDataGenerator()->create_module('forum', $record);
1008         // Add discussions to the forums.
1009         $record = new stdClass();
1010         $record->course = $course1->id;
1011         $record->userid = $user1->id;
1012         $record->forum = $forum1->id;
1013         $discussion1 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1015         // Add three replies to the discussion 1 from different users.
1016         $record = new stdClass();
1017         $record->discussion = $discussion1->id;
1018         $record->parent = $discussion1->firstpost;
1019         $record->userid = $user2->id;
1020         $discussion1reply1 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1022         $record->parent = $discussion1reply1->id;
1023         $record->userid = $user3->id;
1024         $discussion1reply2 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1026         $record->userid = $user4->id;
1027         $discussion1reply3 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1029         // Enrol the user in the first course.
1030         $enrol = enrol_get_plugin('manual');
1032         // We don't use the dataGenerator as we need to get the $instance2 to unenrol later.
1033         $enrolinstances = enrol_get_instances($course1->id, true);
1034         foreach ($enrolinstances as $courseenrolinstance) {
1035             if ($courseenrolinstance->enrol == "manual") {
1036                 $instance1 = $courseenrolinstance;
1037                 break;
1038             }
1039         }
1040         $enrol->enrol_user($instance1, $user1->id);
1042         // Delete one user.
1043         delete_user($user4);
1045         // Assign capabilities to view discussions for forum 1.
1046         $cm = get_coursemodule_from_id('forum', $forum1->cmid, 0, false, MUST_EXIST);
1047         $context = context_module::instance($cm->id);
1048         $newrole = create_role('Role 2', 'role2', 'Role 2 description');
1049         $this->assignUserCapability('mod/forum:viewdiscussion', $context->id, $newrole);
1051         // Create what we expect to be returned when querying the forums.
1053         $post1 = $DB->get_record('forum_posts', array('id' => $discussion1->firstpost), '*', MUST_EXIST);
1055         // User pictures are initially empty, we should get the links once the external function is called.
1056         $expecteddiscussions = array(
1057                 'id' => $discussion1->firstpost,
1058                 'name' => $discussion1->name,
1059                 'groupid' => (int) $discussion1->groupid,
1060                 'timemodified' => $discussion1reply3->created,
1061                 'usermodified' => (int) $discussion1reply3->userid,
1062                 'timestart' => (int) $discussion1->timestart,
1063                 'timeend' => (int) $discussion1->timeend,
1064                 'discussion' => $discussion1->id,
1065                 'parent' => 0,
1066                 'userid' => (int) $discussion1->userid,
1067                 'created' => (int) $post1->created,
1068                 'modified' => (int) $post1->modified,
1069                 'mailed' => (int) $post1->mailed,
1070                 'subject' => $post1->subject,
1071                 'message' => $post1->message,
1072                 'messageformat' => (int) $post1->messageformat,
1073                 'messagetrust' => (int) $post1->messagetrust,
1074                 'attachment' => $post1->attachment,
1075                 'totalscore' => (int) $post1->totalscore,
1076                 'mailnow' => (int) $post1->mailnow,
1077                 'userfullname' => fullname($user1),
1078                 'usermodifiedfullname' => fullname($user4),
1079                 'userpictureurl' => '',
1080                 'usermodifiedpictureurl' => '',
1081                 'numreplies' => 3,
1082                 'numunread' => 0,
1083                 'pinned' => (bool) FORUM_DISCUSSION_UNPINNED,
1084                 'locked' => false,
1085                 'canreply' => false,
1086                 'canlock' => false
1087             );
1089         // Call the external function passing forum id.
1090         $discussions = mod_forum_external::get_forum_discussions_paginated($forum1->id);
1091         $discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_paginated_returns(), $discussions);
1092         $expectedreturn = array(
1093             'discussions' => array($expecteddiscussions),
1094             'warnings' => array()
1095         );
1097         // Wait the theme to be loaded (the external_api call does that) to generate the user profiles.
1098         $userpicture = new user_picture($user1);
1099         $userpicture->size = 1; // Size f1.
1100         $expectedreturn['discussions'][0]['userpictureurl'] = $userpicture->get_url($PAGE)->out(false);
1102         $userpicture = new user_picture($user4);
1103         $userpicture->size = 1; // Size f1.
1104         $expectedreturn['discussions'][0]['usermodifiedpictureurl'] = $userpicture->get_url($PAGE)->out(false);
1106         $this->assertEquals($expectedreturn, $discussions);
1108         // Call without required view discussion capability.
1109         $this->unassignUserCapability('mod/forum:viewdiscussion', $context->id, $newrole);
1110         try {
1111             mod_forum_external::get_forum_discussions_paginated($forum1->id);
1112             $this->fail('Exception expected due to missing capability.');
1113         } catch (moodle_exception $e) {
1114             $this->assertEquals('noviewdiscussionspermission', $e->errorcode);
1115         }
1117         // Unenrol user from second course.
1118         $enrol->unenrol_user($instance1, $user1->id);
1120         // Call for the second course we unenrolled the user from, make sure exception thrown.
1121         try {
1122             mod_forum_external::get_forum_discussions_paginated($forum1->id);
1123             $this->fail('Exception expected due to being unenrolled from the course.');
1124         } catch (moodle_exception $e) {
1125             $this->assertEquals('requireloginerror', $e->errorcode);
1126         }
1128         $this->setAdminUser();
1129         $discussions = mod_forum_external::get_forum_discussions_paginated($forum1->id);
1130         $discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_paginated_returns(), $discussions);
1131         $this->assertTrue($discussions['discussions'][0]['canlock']);
1132     }
1134     /**
1135      * Test get forum discussions paginated (qanda forums)
1136      */
1137     public function test_mod_forum_get_forum_discussions_paginated_qanda() {
1139         $this->resetAfterTest(true);
1141         // Create courses to add the modules.
1142         $course = self::getDataGenerator()->create_course();
1144         $user1 = self::getDataGenerator()->create_user();
1145         $user2 = self::getDataGenerator()->create_user();
1147         // First forum with tracking off.
1148         $record = new stdClass();
1149         $record->course = $course->id;
1150         $record->type = 'qanda';
1151         $forum = self::getDataGenerator()->create_module('forum', $record);
1153         // Add discussions to the forums.
1154         $discussionrecord = new stdClass();
1155         $discussionrecord->course = $course->id;
1156         $discussionrecord->userid = $user2->id;
1157         $discussionrecord->forum = $forum->id;
1158         $discussion = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($discussionrecord);
1160         self::setAdminUser();
1161         $discussions = mod_forum_external::get_forum_discussions_paginated($forum->id);
1162         $discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_paginated_returns(), $discussions);
1164         $this->assertCount(1, $discussions['discussions']);
1165         $this->assertCount(0, $discussions['warnings']);
1167         self::setUser($user1);
1168         $this->getDataGenerator()->enrol_user($user1->id, $course->id);
1170         $discussions = mod_forum_external::get_forum_discussions_paginated($forum->id);
1171         $discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_paginated_returns(), $discussions);
1173         $this->assertCount(1, $discussions['discussions']);
1174         $this->assertCount(0, $discussions['warnings']);
1176     }
1178     /**
1179      * Test get forum discussions
1180      */
1181     public function test_mod_forum_get_forum_discussions() {
1182         global $CFG, $DB, $PAGE;
1184         $this->resetAfterTest(true);
1186         // Set the CFG variable to allow track forums.
1187         $CFG->forum_trackreadposts = true;
1189         // Create a user who can track forums.
1190         $record = new stdClass();
1191         $record->trackforums = true;
1192         $user1 = self::getDataGenerator()->create_user($record);
1193         // Create a bunch of other users to post.
1194         $user2 = self::getDataGenerator()->create_user();
1195         $user3 = self::getDataGenerator()->create_user();
1196         $user4 = self::getDataGenerator()->create_user();
1198         // Set the first created user to the test user.
1199         self::setUser($user1);
1201         // Create courses to add the modules.
1202         $course1 = self::getDataGenerator()->create_course();
1204         // First forum with tracking off.
1205         $record = new stdClass();
1206         $record->course = $course1->id;
1207         $record->trackingtype = FORUM_TRACKING_OFF;
1208         $forum1 = self::getDataGenerator()->create_module('forum', $record);
1210         // Add discussions to the forums.
1211         $record = new stdClass();
1212         $record->course = $course1->id;
1213         $record->userid = $user1->id;
1214         $record->forum = $forum1->id;
1215         $discussion1 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1217         // Add three replies to the discussion 1 from different users.
1218         $record = new stdClass();
1219         $record->discussion = $discussion1->id;
1220         $record->parent = $discussion1->firstpost;
1221         $record->userid = $user2->id;
1222         $discussion1reply1 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1224         $record->parent = $discussion1reply1->id;
1225         $record->userid = $user3->id;
1226         $discussion1reply2 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1228         $record->userid = $user4->id;
1229         $discussion1reply3 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1231         // Enrol the user in the first course.
1232         $enrol = enrol_get_plugin('manual');
1234         // We don't use the dataGenerator as we need to get the $instance2 to unenrol later.
1235         $enrolinstances = enrol_get_instances($course1->id, true);
1236         foreach ($enrolinstances as $courseenrolinstance) {
1237             if ($courseenrolinstance->enrol == "manual") {
1238                 $instance1 = $courseenrolinstance;
1239                 break;
1240             }
1241         }
1242         $enrol->enrol_user($instance1, $user1->id);
1244         // Delete one user.
1245         delete_user($user4);
1247         // Assign capabilities to view discussions for forum 1.
1248         $cm = get_coursemodule_from_id('forum', $forum1->cmid, 0, false, MUST_EXIST);
1249         $context = context_module::instance($cm->id);
1250         $newrole = create_role('Role 2', 'role2', 'Role 2 description');
1251         $this->assignUserCapability('mod/forum:viewdiscussion', $context->id, $newrole);
1253         // Create what we expect to be returned when querying the forums.
1255         $post1 = $DB->get_record('forum_posts', array('id' => $discussion1->firstpost), '*', MUST_EXIST);
1257         // User pictures are initially empty, we should get the links once the external function is called.
1258         $expecteddiscussions = array(
1259             'id' => $discussion1->firstpost,
1260             'name' => $discussion1->name,
1261             'groupid' => (int) $discussion1->groupid,
1262             'timemodified' => (int) $discussion1reply3->created,
1263             'usermodified' => (int) $discussion1reply3->userid,
1264             'timestart' => (int) $discussion1->timestart,
1265             'timeend' => (int) $discussion1->timeend,
1266             'discussion' => (int) $discussion1->id,
1267             'parent' => 0,
1268             'userid' => (int) $discussion1->userid,
1269             'created' => (int) $post1->created,
1270             'modified' => (int) $post1->modified,
1271             'mailed' => (int) $post1->mailed,
1272             'subject' => $post1->subject,
1273             'message' => $post1->message,
1274             'messageformat' => (int) $post1->messageformat,
1275             'messagetrust' => (int) $post1->messagetrust,
1276             'attachment' => $post1->attachment,
1277             'totalscore' => (int) $post1->totalscore,
1278             'mailnow' => (int) $post1->mailnow,
1279             'userfullname' => fullname($user1),
1280             'usermodifiedfullname' => fullname($user4),
1281             'userpictureurl' => '',
1282             'usermodifiedpictureurl' => '',
1283             'numreplies' => 3,
1284             'numunread' => 0,
1285             'pinned' => (bool) FORUM_DISCUSSION_UNPINNED,
1286             'locked' => false,
1287             'canreply' => false,
1288             'canlock' => false,
1289             'starred' => false,
1290             'canfavourite' => true
1291         );
1293         // Call the external function passing forum id.
1294         $discussions = mod_forum_external::get_forum_discussions($forum1->id);
1295         $discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_returns(), $discussions);
1296         $expectedreturn = array(
1297             'discussions' => array($expecteddiscussions),
1298             'warnings' => array()
1299         );
1301         // Wait the theme to be loaded (the external_api call does that) to generate the user profiles.
1302         $userpicture = new user_picture($user1);
1303         $userpicture->size = 2; // Size f2.
1304         $expectedreturn['discussions'][0]['userpictureurl'] = $userpicture->get_url($PAGE)->out(false);
1306         $userpicture = new user_picture($user4);
1307         $userpicture->size = 2; // Size f2.
1308         $expectedreturn['discussions'][0]['usermodifiedpictureurl'] = $userpicture->get_url($PAGE)->out(false);
1310         $this->assertEquals($expectedreturn, $discussions);
1312         // Test the starring functionality return.
1313         $t = mod_forum_external::toggle_favourite_state($discussion1->id, 1);
1314         $expectedreturn['discussions'][0]['starred'] = true;
1315         $discussions = mod_forum_external::get_forum_discussions($forum1->id);
1316         $discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_returns(), $discussions);
1317         $this->assertEquals($expectedreturn, $discussions);
1319         // Call without required view discussion capability.
1320         $this->unassignUserCapability('mod/forum:viewdiscussion', $context->id, $newrole);
1321         try {
1322             mod_forum_external::get_forum_discussions($forum1->id);
1323             $this->fail('Exception expected due to missing capability.');
1324         } catch (moodle_exception $e) {
1325             $this->assertEquals('noviewdiscussionspermission', $e->errorcode);
1326         }
1328         // Unenrol user from second course.
1329         $enrol->unenrol_user($instance1, $user1->id);
1331         // Call for the second course we unenrolled the user from, make sure exception thrown.
1332         try {
1333             mod_forum_external::get_forum_discussions($forum1->id);
1334             $this->fail('Exception expected due to being unenrolled from the course.');
1335         } catch (moodle_exception $e) {
1336             $this->assertEquals('requireloginerror', $e->errorcode);
1337         }
1339         $this->setAdminUser();
1340         $discussions = mod_forum_external::get_forum_discussions($forum1->id);
1341         $discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_returns(), $discussions);
1342         $this->assertTrue($discussions['discussions'][0]['canlock']);
1343     }
1345     /**
1346      * Test the sorting in get forum discussions
1347      */
1348     public function test_mod_forum_get_forum_discussions_sorting() {
1349         global $CFG, $DB, $PAGE;
1351         $this->resetAfterTest(true);
1353         // Set the CFG variable to allow track forums.
1354         $CFG->forum_trackreadposts = true;
1356         // Create a user who can track forums.
1357         $record = new stdClass();
1358         $record->trackforums = true;
1359         $user1 = self::getDataGenerator()->create_user($record);
1360         // Create a bunch of other users to post.
1361         $user2 = self::getDataGenerator()->create_user();
1362         $user3 = self::getDataGenerator()->create_user();
1363         $user4 = self::getDataGenerator()->create_user();
1365         // Set the first created user to the test user.
1366         self::setUser($user1);
1368         // Create courses to add the modules.
1369         $course1 = self::getDataGenerator()->create_course();
1371         // Enrol the user in the first course.
1372         $enrol = enrol_get_plugin('manual');
1374         // We don't use the dataGenerator as we need to get the $instance2 to unenrol later.
1375         $enrolinstances = enrol_get_instances($course1->id, true);
1376         foreach ($enrolinstances as $courseenrolinstance) {
1377             if ($courseenrolinstance->enrol == "manual") {
1378                 $instance1 = $courseenrolinstance;
1379                 break;
1380             }
1381         }
1382         $enrol->enrol_user($instance1, $user1->id);
1384         // First forum with tracking off.
1385         $record = new stdClass();
1386         $record->course = $course1->id;
1387         $record->trackingtype = FORUM_TRACKING_OFF;
1388         $forum1 = self::getDataGenerator()->create_module('forum', $record);
1390         // Assign capabilities to view discussions for forum 1.
1391         $cm = get_coursemodule_from_id('forum', $forum1->cmid, 0, false, MUST_EXIST);
1392         $context = context_module::instance($cm->id);
1393         $newrole = create_role('Role 2', 'role2', 'Role 2 description');
1394         $this->assignUserCapability('mod/forum:viewdiscussion', $context->id, $newrole);
1396         // Add discussions to the forums.
1397         $record = new stdClass();
1398         $record->course = $course1->id;
1399         $record->userid = $user1->id;
1400         $record->forum = $forum1->id;
1401         $discussion1 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1402         sleep(1);
1404         // Add three replies to the discussion 1 from different users.
1405         $record = new stdClass();
1406         $record->discussion = $discussion1->id;
1407         $record->parent = $discussion1->firstpost;
1408         $record->userid = $user2->id;
1409         $discussion1reply1 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1410         sleep(1);
1412         $record->parent = $discussion1reply1->id;
1413         $record->userid = $user3->id;
1414         $discussion1reply2 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1415         sleep(1);
1417         $record->userid = $user4->id;
1418         $discussion1reply3 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1419         sleep(1);
1421         // Create discussion2.
1422         $record2 = new stdClass();
1423         $record2->course = $course1->id;
1424         $record2->userid = $user1->id;
1425         $record2->forum = $forum1->id;
1426         $discussion2 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record2);
1427         sleep(1);
1429         // Add one reply to the discussion 2.
1430         $record2 = new stdClass();
1431         $record2->discussion = $discussion2->id;
1432         $record2->parent = $discussion2->firstpost;
1433         $record2->userid = $user2->id;
1434         $discussion2reply1 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record2);
1435         sleep(1);
1437         // Create discussion 3.
1438         $record3 = new stdClass();
1439         $record3->course = $course1->id;
1440         $record3->userid = $user1->id;
1441         $record3->forum = $forum1->id;
1442         $discussion3 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record3);
1443         sleep(1);
1445         // Add two replies to the discussion 3.
1446         $record3 = new stdClass();
1447         $record3->discussion = $discussion3->id;
1448         $record3->parent = $discussion3->firstpost;
1449         $record3->userid = $user2->id;
1450         $discussion3reply1 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record3);
1451         sleep(1);
1453         $record3->parent = $discussion3reply1->id;
1454         $record3->userid = $user3->id;
1455         $discussion3reply2 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record3);
1457         // Call the external function passing forum id.
1458         $discussions = mod_forum_external::get_forum_discussions($forum1->id);
1459         $discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_returns(), $discussions);
1460         // Discussions should be ordered by last post date in descending order by default.
1461         $this->assertEquals($discussions['discussions'][0]['discussion'], $discussion3->id);
1462         $this->assertEquals($discussions['discussions'][1]['discussion'], $discussion2->id);
1463         $this->assertEquals($discussions['discussions'][2]['discussion'], $discussion1->id);
1465         $vaultfactory = \mod_forum\local\container::get_vault_factory();
1466         $discussionlistvault = $vaultfactory->get_discussions_in_forum_vault();
1468         // Call the external function passing forum id and sort order parameter.
1469         $discussions = mod_forum_external::get_forum_discussions($forum1->id, $discussionlistvault::SORTORDER_LASTPOST_ASC);
1470         $discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_returns(), $discussions);
1471         // Discussions should be ordered by last post date in ascending order.
1472         $this->assertEquals($discussions['discussions'][0]['discussion'], $discussion1->id);
1473         $this->assertEquals($discussions['discussions'][1]['discussion'], $discussion2->id);
1474         $this->assertEquals($discussions['discussions'][2]['discussion'], $discussion3->id);
1476         // Call the external function passing forum id and sort order parameter.
1477         $discussions = mod_forum_external::get_forum_discussions($forum1->id, $discussionlistvault::SORTORDER_CREATED_DESC);
1478         $discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_returns(), $discussions);
1479         // Discussions should be ordered by discussion creation date in descending order.
1480         $this->assertEquals($discussions['discussions'][0]['discussion'], $discussion3->id);
1481         $this->assertEquals($discussions['discussions'][1]['discussion'], $discussion2->id);
1482         $this->assertEquals($discussions['discussions'][2]['discussion'], $discussion1->id);
1484         // Call the external function passing forum id and sort order parameter.
1485         $discussions = mod_forum_external::get_forum_discussions($forum1->id, $discussionlistvault::SORTORDER_CREATED_ASC);
1486         $discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_returns(), $discussions);
1487         // Discussions should be ordered by discussion creation date in ascending order.
1488         $this->assertEquals($discussions['discussions'][0]['discussion'], $discussion1->id);
1489         $this->assertEquals($discussions['discussions'][1]['discussion'], $discussion2->id);
1490         $this->assertEquals($discussions['discussions'][2]['discussion'], $discussion3->id);
1492         // Call the external function passing forum id and sort order parameter.
1493         $discussions = mod_forum_external::get_forum_discussions($forum1->id, $discussionlistvault::SORTORDER_REPLIES_DESC);
1494         $discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_returns(), $discussions);
1495         // Discussions should be ordered by the number of replies in descending order.
1496         $this->assertEquals($discussions['discussions'][0]['discussion'], $discussion1->id);
1497         $this->assertEquals($discussions['discussions'][1]['discussion'], $discussion3->id);
1498         $this->assertEquals($discussions['discussions'][2]['discussion'], $discussion2->id);
1500         // Call the external function passing forum id and sort order parameter.
1501         $discussions = mod_forum_external::get_forum_discussions($forum1->id, $discussionlistvault::SORTORDER_REPLIES_ASC);
1502         $discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_returns(), $discussions);
1503         // Discussions should be ordered by the number of replies in ascending order.
1504         $this->assertEquals($discussions['discussions'][0]['discussion'], $discussion2->id);
1505         $this->assertEquals($discussions['discussions'][1]['discussion'], $discussion3->id);
1506         $this->assertEquals($discussions['discussions'][2]['discussion'], $discussion1->id);
1508         // Pin discussion2.
1509         $DB->update_record('forum_discussions',
1510             (object) array('id' => $discussion2->id, 'pinned' => FORUM_DISCUSSION_PINNED));
1512         // Call the external function passing forum id.
1513         $discussions = mod_forum_external::get_forum_discussions($forum1->id);
1514         $discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_returns(), $discussions);
1515         // Discussions should be ordered by last post date in descending order by default.
1516         // Pinned discussions should be at the top of the list.
1517         $this->assertEquals($discussions['discussions'][0]['discussion'], $discussion2->id);
1518         $this->assertEquals($discussions['discussions'][1]['discussion'], $discussion3->id);
1519         $this->assertEquals($discussions['discussions'][2]['discussion'], $discussion1->id);
1521         // Call the external function passing forum id and sort order parameter.
1522         $discussions = mod_forum_external::get_forum_discussions($forum1->id, $discussionlistvault::SORTORDER_LASTPOST_ASC);
1523         $discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_returns(), $discussions);
1524         // Discussions should be ordered by last post date in ascending order.
1525         // Pinned discussions should be at the top of the list.
1526         $this->assertEquals($discussions['discussions'][0]['discussion'], $discussion2->id);
1527         $this->assertEquals($discussions['discussions'][1]['discussion'], $discussion1->id);
1528         $this->assertEquals($discussions['discussions'][2]['discussion'], $discussion3->id);
1529     }
1531     /**
1532      * Test add_discussion_post
1533      */
1534     public function test_add_discussion_post() {
1535         global $CFG;
1537         $this->resetAfterTest(true);
1539         $user = self::getDataGenerator()->create_user();
1540         $otheruser = self::getDataGenerator()->create_user();
1542         self::setAdminUser();
1544         // Create course to add the module.
1545         $course = self::getDataGenerator()->create_course(array('groupmode' => VISIBLEGROUPS, 'groupmodeforce' => 0));
1547         // Forum with tracking off.
1548         $record = new stdClass();
1549         $record->course = $course->id;
1550         $forum = self::getDataGenerator()->create_module('forum', $record);
1551         $cm = get_coursemodule_from_id('forum', $forum->cmid, 0, false, MUST_EXIST);
1552         $forumcontext = context_module::instance($forum->cmid);
1554         // Add discussions to the forums.
1555         $record = new stdClass();
1556         $record->course = $course->id;
1557         $record->userid = $user->id;
1558         $record->forum = $forum->id;
1559         $discussion = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1561         // Try to post (user not enrolled).
1562         self::setUser($user);
1563         try {
1564             mod_forum_external::add_discussion_post($discussion->firstpost, 'some subject', 'some text here...');
1565             $this->fail('Exception expected due to being unenrolled from the course.');
1566         } catch (moodle_exception $e) {
1567             $this->assertEquals('requireloginerror', $e->errorcode);
1568         }
1570         $this->getDataGenerator()->enrol_user($user->id, $course->id);
1571         $this->getDataGenerator()->enrol_user($otheruser->id, $course->id);
1573         $createdpost = mod_forum_external::add_discussion_post($discussion->firstpost, 'some subject', 'some text here...');
1574         $createdpost = external_api::clean_returnvalue(mod_forum_external::add_discussion_post_returns(), $createdpost);
1576         $posts = mod_forum_external::get_forum_discussion_posts($discussion->id);
1577         $posts = external_api::clean_returnvalue(mod_forum_external::get_forum_discussion_posts_returns(), $posts);
1578         // We receive the discussion and the post.
1579         $this->assertEquals(2, count($posts['posts']));
1581         $tested = false;
1582         foreach ($posts['posts'] as $thispost) {
1583             if ($createdpost['postid'] == $thispost['id']) {
1584                 $this->assertEquals('some subject', $thispost['subject']);
1585                 $this->assertEquals('some text here...', $thispost['message']);
1586                 $this->assertEquals(FORMAT_HTML, $thispost['messageformat']); // This is the default if format was not specified.
1587                 $tested = true;
1588             }
1589         }
1590         $this->assertTrue($tested);
1592         // Let's simulate a call with any other format, it should be stored that way.
1593         global $DB; // Yes, we are going to use DB facilities too, because cannot rely on other functions for checking
1594                     // the format. They eat it completely (going back to FORMAT_HTML. So we only can trust DB for further
1595                     // processing.
1596         $formats = [FORMAT_PLAIN, FORMAT_MOODLE, FORMAT_MARKDOWN, FORMAT_HTML];
1597         $options = [];
1598         foreach ($formats as $format) {
1599             $createdpost = mod_forum_external::add_discussion_post($discussion->firstpost,
1600                 'with some format', 'some formatted here...', $options, $format);
1601             $createdpost = external_api::clean_returnvalue(mod_forum_external::add_discussion_post_returns(), $createdpost);
1602             $dbformat = $DB->get_field('forum_posts', 'messageformat', ['id' => $createdpost['postid']]);
1603             $this->assertEquals($format, $dbformat);
1604         }
1606         // Now let's try the 'topreferredformat' option. That should end with the content
1607         // transformed and the format being FORMAT_HTML (when, like in this case,  user preferred
1608         // format is HTML, inferred from editor in preferences).
1609         $options = [['name' => 'topreferredformat', 'value' => true]];
1610         $createdpost = mod_forum_external::add_discussion_post($discussion->firstpost,
1611             'interesting subject', 'with some https://example.com link', $options, FORMAT_MOODLE);
1612         $createdpost = external_api::clean_returnvalue(mod_forum_external::add_discussion_post_returns(), $createdpost);
1613         $dbpost = $DB->get_record('forum_posts', ['id' => $createdpost['postid']]);
1614         // Format HTML and content converted, we should get.
1615         $this->assertEquals(FORMAT_HTML, $dbpost->messageformat);
1616         $this->assertEquals('<div class="text_to_html">with some https://example.com link</div>', $dbpost->message);
1618         // Test inline and regular attachment in post
1619         // Create a file in a draft area for inline attachments.
1620         $draftidinlineattach = file_get_unused_draft_itemid();
1621         $draftidattach = file_get_unused_draft_itemid();
1622         self::setUser($user);
1623         $usercontext = context_user::instance($user->id);
1624         $filepath = '/';
1625         $filearea = 'draft';
1626         $component = 'user';
1627         $filenameimg = 'shouldbeanimage.txt';
1628         $filerecordinline = array(
1629             'contextid' => $usercontext->id,
1630             'component' => $component,
1631             'filearea'  => $filearea,
1632             'itemid'    => $draftidinlineattach,
1633             'filepath'  => $filepath,
1634             'filename'  => $filenameimg,
1635         );
1636         $fs = get_file_storage();
1638         // Create a file in a draft area for regular attachments.
1639         $filerecordattach = $filerecordinline;
1640         $attachfilename = 'attachment.txt';
1641         $filerecordattach['filename'] = $attachfilename;
1642         $filerecordattach['itemid'] = $draftidattach;
1643         $fs->create_file_from_string($filerecordinline, 'image contents (not really)');
1644         $fs->create_file_from_string($filerecordattach, 'simple text attachment');
1646         $options = array(array('name' => 'inlineattachmentsid', 'value' => $draftidinlineattach),
1647                          array('name' => 'attachmentsid', 'value' => $draftidattach));
1648         $dummytext = 'Here is an inline image: <img src="' . $CFG->wwwroot
1649                      . "/draftfile.php/{$usercontext->id}/user/draft/{$draftidinlineattach}/{$filenameimg}"
1650                      . '" alt="inlineimage">.';
1651         $createdpost = mod_forum_external::add_discussion_post($discussion->firstpost, 'new post inline attachment',
1652                                                                $dummytext, $options);
1653         $createdpost = external_api::clean_returnvalue(mod_forum_external::add_discussion_post_returns(), $createdpost);
1655         $posts = mod_forum_external::get_forum_discussion_posts($discussion->id);
1656         $posts = external_api::clean_returnvalue(mod_forum_external::get_forum_discussion_posts_returns(), $posts);
1657         // We receive the discussion and the post.
1658         // Can't guarantee order of posts during tests.
1659         $postfound = false;
1660         foreach ($posts['posts'] as $thispost) {
1661             if ($createdpost['postid'] == $thispost['id']) {
1662                 $this->assertEquals($createdpost['postid'], $thispost['id']);
1663                 $this->assertEquals($thispost['attachment'], 1, "There should be a non-inline attachment");
1664                 $this->assertCount(1, $thispost['attachments'], "There should be 1 attachment");
1665                 $this->assertEquals($thispost['attachments'][0]['filename'], $attachfilename, "There should be 1 attachment");
1666                 $this->assertContains('pluginfile.php', $thispost['message']);
1667                 $postfound = true;
1668                 break;
1669             }
1670         }
1672         $this->assertTrue($postfound);
1674         // Check not posting in groups the user is not member of.
1675         $group = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
1676         groups_add_member($group->id, $otheruser->id);
1678         $forum = self::getDataGenerator()->create_module('forum', $record, array('groupmode' => SEPARATEGROUPS));
1679         $record->forum = $forum->id;
1680         $record->userid = $otheruser->id;
1681         $record->groupid = $group->id;
1682         $discussion = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1684         try {
1685             mod_forum_external::add_discussion_post($discussion->firstpost, 'some subject', 'some text here...');
1686             $this->fail('Exception expected due to invalid permissions for posting.');
1687         } catch (moodle_exception $e) {
1688             $this->assertEquals('nopostforum', $e->errorcode);
1689         }
1690     }
1692     /*
1693      * Test add_discussion. A basic test since all the API functions are already covered by unit tests.
1694      */
1695     public function test_add_discussion() {
1696         global $CFG, $USER;
1697         $this->resetAfterTest(true);
1699         // Create courses to add the modules.
1700         $course = self::getDataGenerator()->create_course();
1702         $user1 = self::getDataGenerator()->create_user();
1703         $user2 = self::getDataGenerator()->create_user();
1705         // First forum with tracking off.
1706         $record = new stdClass();
1707         $record->course = $course->id;
1708         $record->type = 'news';
1709         $forum = self::getDataGenerator()->create_module('forum', $record);
1711         self::setUser($user1);
1712         $this->getDataGenerator()->enrol_user($user1->id, $course->id);
1714         try {
1715             mod_forum_external::add_discussion($forum->id, 'the subject', 'some text here...');
1716             $this->fail('Exception expected due to invalid permissions.');
1717         } catch (moodle_exception $e) {
1718             $this->assertEquals('cannotcreatediscussion', $e->errorcode);
1719         }
1721         self::setAdminUser();
1722         $createddiscussion = mod_forum_external::add_discussion($forum->id, 'the subject', 'some text here...');
1723         $createddiscussion = external_api::clean_returnvalue(mod_forum_external::add_discussion_returns(), $createddiscussion);
1725         $discussions = mod_forum_external::get_forum_discussions_paginated($forum->id);
1726         $discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_paginated_returns(), $discussions);
1728         $this->assertCount(1, $discussions['discussions']);
1729         $this->assertCount(0, $discussions['warnings']);
1731         $this->assertEquals($createddiscussion['discussionid'], $discussions['discussions'][0]['discussion']);
1732         $this->assertEquals(-1, $discussions['discussions'][0]['groupid']);
1733         $this->assertEquals('the subject', $discussions['discussions'][0]['subject']);
1734         $this->assertEquals('some text here...', $discussions['discussions'][0]['message']);
1736         $discussion2pinned = mod_forum_external::add_discussion($forum->id, 'the pinned subject', 'some 2 text here...', -1,
1737                                                                 array('options' => array('name' => 'discussionpinned',
1738                                                                                          'value' => true)));
1739         $discussion3 = mod_forum_external::add_discussion($forum->id, 'the non pinnedsubject', 'some 3 text here...');
1740         $discussions = mod_forum_external::get_forum_discussions_paginated($forum->id);
1741         $discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_paginated_returns(), $discussions);
1742         $this->assertCount(3, $discussions['discussions']);
1743         $this->assertEquals($discussion2pinned['discussionid'], $discussions['discussions'][0]['discussion']);
1745         // Test inline and regular attachment in new discussion
1746         // Create a file in a draft area for inline attachments.
1748         $fs = get_file_storage();
1750         $draftidinlineattach = file_get_unused_draft_itemid();
1751         $draftidattach = file_get_unused_draft_itemid();
1753         $usercontext = context_user::instance($USER->id);
1754         $filepath = '/';
1755         $filearea = 'draft';
1756         $component = 'user';
1757         $filenameimg = 'shouldbeanimage.txt';
1758         $filerecord = array(
1759             'contextid' => $usercontext->id,
1760             'component' => $component,
1761             'filearea'  => $filearea,
1762             'itemid'    => $draftidinlineattach,
1763             'filepath'  => $filepath,
1764             'filename'  => $filenameimg,
1765         );
1767         // Create a file in a draft area for regular attachments.
1768         $filerecordattach = $filerecord;
1769         $attachfilename = 'attachment.txt';
1770         $filerecordattach['filename'] = $attachfilename;
1771         $filerecordattach['itemid'] = $draftidattach;
1772         $fs->create_file_from_string($filerecord, 'image contents (not really)');
1773         $fs->create_file_from_string($filerecordattach, 'simple text attachment');
1775         $dummytext = 'Here is an inline image: <img src="' . $CFG->wwwroot .
1776                     "/draftfile.php/{$usercontext->id}/user/draft/{$draftidinlineattach}/{$filenameimg}" .
1777                     '" alt="inlineimage">.';
1779         $options = array(array('name' => 'inlineattachmentsid', 'value' => $draftidinlineattach),
1780                          array('name' => 'attachmentsid', 'value' => $draftidattach));
1781         $createddiscussion = mod_forum_external::add_discussion($forum->id, 'the inline attachment subject',
1782                                                                 $dummytext, -1, $options);
1783         $createddiscussion = external_api::clean_returnvalue(mod_forum_external::add_discussion_returns(), $createddiscussion);
1785         $discussions = mod_forum_external::get_forum_discussions_paginated($forum->id);
1786         $discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_paginated_returns(), $discussions);
1788         $this->assertCount(4, $discussions['discussions']);
1789         $this->assertCount(0, $createddiscussion['warnings']);
1790         // Can't guarantee order of posts during tests.
1791         $postfound = false;
1792         foreach ($discussions['discussions'] as $thisdiscussion) {
1793             if ($createddiscussion['discussionid'] == $thisdiscussion['discussion']) {
1794                 $this->assertEquals($thisdiscussion['attachment'], 1, "There should be a non-inline attachment");
1795                 $this->assertCount(1, $thisdiscussion['attachments'], "There should be 1 attachment");
1796                 $this->assertEquals($thisdiscussion['attachments'][0]['filename'], $attachfilename, "There should be 1 attachment");
1797                 $this->assertNotContains('draftfile.php', $thisdiscussion['message']);
1798                 $this->assertContains('pluginfile.php', $thisdiscussion['message']);
1799                 $postfound = true;
1800                 break;
1801             }
1802         }
1804         $this->assertTrue($postfound);
1805     }
1807     /**
1808      * Test adding discussions in a course with gorups
1809      */
1810     public function test_add_discussion_in_course_with_groups() {
1811         global $CFG;
1813         $this->resetAfterTest(true);
1815         // Create course to add the module.
1816         $course = self::getDataGenerator()->create_course(array('groupmode' => VISIBLEGROUPS, 'groupmodeforce' => 0));
1817         $user = self::getDataGenerator()->create_user();
1818         $this->getDataGenerator()->enrol_user($user->id, $course->id);
1820         // Forum forcing separate gropus.
1821         $record = new stdClass();
1822         $record->course = $course->id;
1823         $forum = self::getDataGenerator()->create_module('forum', $record, array('groupmode' => SEPARATEGROUPS));
1825         // Try to post (user not enrolled).
1826         self::setUser($user);
1828         // The user is not enroled in any group, try to post in a forum with separate groups.
1829         try {
1830             mod_forum_external::add_discussion($forum->id, 'the subject', 'some text here...');
1831             $this->fail('Exception expected due to invalid group permissions.');
1832         } catch (moodle_exception $e) {
1833             $this->assertEquals('cannotcreatediscussion', $e->errorcode);
1834         }
1836         try {
1837             mod_forum_external::add_discussion($forum->id, 'the subject', 'some text here...', 0);
1838             $this->fail('Exception expected due to invalid group permissions.');
1839         } catch (moodle_exception $e) {
1840             $this->assertEquals('cannotcreatediscussion', $e->errorcode);
1841         }
1843         // Create a group.
1844         $group = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
1846         // Try to post in a group the user is not enrolled.
1847         try {
1848             mod_forum_external::add_discussion($forum->id, 'the subject', 'some text here...', $group->id);
1849             $this->fail('Exception expected due to invalid group permissions.');
1850         } catch (moodle_exception $e) {
1851             $this->assertEquals('cannotcreatediscussion', $e->errorcode);
1852         }
1854         // Add the user to a group.
1855         groups_add_member($group->id, $user->id);
1857         // Try to post in a group the user is not enrolled.
1858         try {
1859             mod_forum_external::add_discussion($forum->id, 'the subject', 'some text here...', $group->id + 1);
1860             $this->fail('Exception expected due to invalid group.');
1861         } catch (moodle_exception $e) {
1862             $this->assertEquals('cannotcreatediscussion', $e->errorcode);
1863         }
1865         // Nost add the discussion using a valid group.
1866         $discussion = mod_forum_external::add_discussion($forum->id, 'the subject', 'some text here...', $group->id);
1867         $discussion = external_api::clean_returnvalue(mod_forum_external::add_discussion_returns(), $discussion);
1869         $discussions = mod_forum_external::get_forum_discussions_paginated($forum->id);
1870         $discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_paginated_returns(), $discussions);
1872         $this->assertCount(1, $discussions['discussions']);
1873         $this->assertCount(0, $discussions['warnings']);
1874         $this->assertEquals($discussion['discussionid'], $discussions['discussions'][0]['discussion']);
1875         $this->assertEquals($group->id, $discussions['discussions'][0]['groupid']);
1877         // Now add a discussions without indicating a group. The function should guess the correct group.
1878         $discussion = mod_forum_external::add_discussion($forum->id, 'the subject', 'some text here...');
1879         $discussion = external_api::clean_returnvalue(mod_forum_external::add_discussion_returns(), $discussion);
1881         $discussions = mod_forum_external::get_forum_discussions_paginated($forum->id);
1882         $discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_paginated_returns(), $discussions);
1884         $this->assertCount(2, $discussions['discussions']);
1885         $this->assertCount(0, $discussions['warnings']);
1886         $this->assertEquals($group->id, $discussions['discussions'][0]['groupid']);
1887         $this->assertEquals($group->id, $discussions['discussions'][1]['groupid']);
1889         // Enrol the same user in other group.
1890         $group2 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
1891         groups_add_member($group2->id, $user->id);
1893         // Now add a discussions without indicating a group. The function should guess the correct group (the first one).
1894         $discussion = mod_forum_external::add_discussion($forum->id, 'the subject', 'some text here...');
1895         $discussion = external_api::clean_returnvalue(mod_forum_external::add_discussion_returns(), $discussion);
1897         $discussions = mod_forum_external::get_forum_discussions_paginated($forum->id);
1898         $discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_paginated_returns(), $discussions);
1900         $this->assertCount(3, $discussions['discussions']);
1901         $this->assertCount(0, $discussions['warnings']);
1902         $this->assertEquals($group->id, $discussions['discussions'][0]['groupid']);
1903         $this->assertEquals($group->id, $discussions['discussions'][1]['groupid']);
1904         $this->assertEquals($group->id, $discussions['discussions'][2]['groupid']);
1906     }
1908     /*
1909      * Test set_lock_state.
1910      */
1911     public function test_set_lock_state() {
1912         global $DB;
1913         $this->resetAfterTest(true);
1915         // Create courses to add the modules.
1916         $course = self::getDataGenerator()->create_course();
1917         $user = self::getDataGenerator()->create_user();
1918         $studentrole = $DB->get_record('role', array('shortname' => 'student'));
1920         // First forum with tracking off.
1921         $record = new stdClass();
1922         $record->course = $course->id;
1923         $record->type = 'news';
1924         $forum = self::getDataGenerator()->create_module('forum', $record);
1926         $record = new stdClass();
1927         $record->course = $course->id;
1928         $record->userid = $user->id;
1929         $record->forum = $forum->id;
1930         $discussion = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1932         // User who is a student.
1933         self::setUser($user);
1934         $this->getDataGenerator()->enrol_user($user->id, $course->id, $studentrole->id, 'manual');
1936         // Only a teacher should be able to lock a discussion.
1937         try {
1938             $result = mod_forum_external::set_lock_state($forum->id, $discussion->id, 0);
1939             $this->fail('Exception expected due to missing capability.');
1940         } catch (moodle_exception $e) {
1941             $this->assertEquals('errorcannotlock', $e->errorcode);
1942         }
1944         // Set the lock.
1945         self::setAdminUser();
1946         $result = mod_forum_external::set_lock_state($forum->id, $discussion->id, 0);
1947         $result = external_api::clean_returnvalue(mod_forum_external::set_lock_state_returns(), $result);
1948         $this->assertTrue($result['locked']);
1949         $this->assertNotEquals(0, $result['times']['locked']);
1951         // Unset the lock.
1952         $result = mod_forum_external::set_lock_state($forum->id, $discussion->id, time());
1953         $result = external_api::clean_returnvalue(mod_forum_external::set_lock_state_returns(), $result);
1954         $this->assertFalse($result['locked']);
1955         $this->assertEquals('0', $result['times']['locked']);
1956     }
1958     /*
1959      * Test can_add_discussion. A basic test since all the API functions are already covered by unit tests.
1960      */
1961     public function test_can_add_discussion() {
1962         global $DB;
1963         $this->resetAfterTest(true);
1965         // Create courses to add the modules.
1966         $course = self::getDataGenerator()->create_course();
1968         $user = self::getDataGenerator()->create_user();
1970         // First forum with tracking off.
1971         $record = new stdClass();
1972         $record->course = $course->id;
1973         $record->type = 'news';
1974         $forum = self::getDataGenerator()->create_module('forum', $record);
1976         // User with no permissions to add in a news forum.
1977         self::setUser($user);
1978         $this->getDataGenerator()->enrol_user($user->id, $course->id);
1980         $result = mod_forum_external::can_add_discussion($forum->id);
1981         $result = external_api::clean_returnvalue(mod_forum_external::can_add_discussion_returns(), $result);
1982         $this->assertFalse($result['status']);
1983         $this->assertFalse($result['canpindiscussions']);
1984         $this->assertTrue($result['cancreateattachment']);
1986         // Disable attachments.
1987         $DB->set_field('forum', 'maxattachments', 0, array('id' => $forum->id));
1988         $result = mod_forum_external::can_add_discussion($forum->id);
1989         $result = external_api::clean_returnvalue(mod_forum_external::can_add_discussion_returns(), $result);
1990         $this->assertFalse($result['status']);
1991         $this->assertFalse($result['canpindiscussions']);
1992         $this->assertFalse($result['cancreateattachment']);
1993         $DB->set_field('forum', 'maxattachments', 1, array('id' => $forum->id));    // Enable attachments again.
1995         self::setAdminUser();
1996         $result = mod_forum_external::can_add_discussion($forum->id);
1997         $result = external_api::clean_returnvalue(mod_forum_external::can_add_discussion_returns(), $result);
1998         $this->assertTrue($result['status']);
1999         $this->assertTrue($result['canpindiscussions']);
2000         $this->assertTrue($result['cancreateattachment']);
2001     }
2003     /*
2004      * A basic test to make sure users cannot post to forum after the cutoff date.
2005      */
2006     public function test_can_add_discussion_after_cutoff() {
2007         $this->resetAfterTest(true);
2009         // Create courses to add the modules.
2010         $course = self::getDataGenerator()->create_course();
2012         $user = self::getDataGenerator()->create_user();
2014         // Create a forum with cutoff date set to a past date.
2015         $forum = self::getDataGenerator()->create_module('forum', ['course' => $course->id, 'cutoffdate' => time() - 1]);
2017         // User with no mod/forum:canoverridecutoff capability.
2018         self::setUser($user);
2019         $this->getDataGenerator()->enrol_user($user->id, $course->id);
2021         $result = mod_forum_external::can_add_discussion($forum->id);
2022         $result = external_api::clean_returnvalue(mod_forum_external::can_add_discussion_returns(), $result);
2023         $this->assertFalse($result['status']);
2025         self::setAdminUser();
2026         $result = mod_forum_external::can_add_discussion($forum->id);
2027         $result = external_api::clean_returnvalue(mod_forum_external::can_add_discussion_returns(), $result);
2028         $this->assertTrue($result['status']);
2029     }
2031     /**
2032      * Test get forum posts discussions including rating information.
2033      */
2034     public function test_mod_forum_get_forum_discussion_rating_information() {
2035         global $DB, $CFG;
2036         require_once($CFG->dirroot . '/rating/lib.php');
2038         $this->resetAfterTest(true);
2040         $user1 = self::getDataGenerator()->create_user();
2041         $user2 = self::getDataGenerator()->create_user();
2042         $user3 = self::getDataGenerator()->create_user();
2043         $teacher = self::getDataGenerator()->create_user();
2045         // Create course to add the module.
2046         $course = self::getDataGenerator()->create_course();
2048         $studentrole = $DB->get_record('role', array('shortname' => 'student'));
2049         $teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'));
2050         $this->getDataGenerator()->enrol_user($user1->id, $course->id, $studentrole->id, 'manual');
2051         $this->getDataGenerator()->enrol_user($user2->id, $course->id, $studentrole->id, 'manual');
2052         $this->getDataGenerator()->enrol_user($user3->id, $course->id, $studentrole->id, 'manual');
2053         $this->getDataGenerator()->enrol_user($teacher->id, $course->id, $teacherrole->id, 'manual');
2055         // Create the forum.
2056         $record = new stdClass();
2057         $record->course = $course->id;
2058         // Set Aggregate type = Average of ratings.
2059         $record->assessed = RATING_AGGREGATE_AVERAGE;
2060         $record->scale = 100;
2061         $forum = self::getDataGenerator()->create_module('forum', $record);
2062         $context = context_module::instance($forum->cmid);
2064         // Add discussion to the forum.
2065         $record = new stdClass();
2066         $record->course = $course->id;
2067         $record->userid = $user1->id;
2068         $record->forum = $forum->id;
2069         $discussion = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
2071         // Retrieve the first post.
2072         $post = $DB->get_record('forum_posts', array('discussion' => $discussion->id));
2074         // Rate the discussion as user2.
2075         $rating1 = new stdClass();
2076         $rating1->contextid = $context->id;
2077         $rating1->component = 'mod_forum';
2078         $rating1->ratingarea = 'post';
2079         $rating1->itemid = $post->id;
2080         $rating1->rating = 50;
2081         $rating1->scaleid = 100;
2082         $rating1->userid = $user2->id;
2083         $rating1->timecreated = time();
2084         $rating1->timemodified = time();
2085         $rating1->id = $DB->insert_record('rating', $rating1);
2087         // Rate the discussion as user3.
2088         $rating2 = new stdClass();
2089         $rating2->contextid = $context->id;
2090         $rating2->component = 'mod_forum';
2091         $rating2->ratingarea = 'post';
2092         $rating2->itemid = $post->id;
2093         $rating2->rating = 100;
2094         $rating2->scaleid = 100;
2095         $rating2->userid = $user3->id;
2096         $rating2->timecreated = time() + 1;
2097         $rating2->timemodified = time() + 1;
2098         $rating2->id = $DB->insert_record('rating', $rating2);
2100         // Retrieve the rating for the post as student.
2101         $this->setUser($user1);
2102         $posts = mod_forum_external::get_forum_discussion_posts($discussion->id);
2103         $posts = external_api::clean_returnvalue(mod_forum_external::get_forum_discussion_posts_returns(), $posts);
2104         $this->assertCount(1, $posts['ratinginfo']['ratings']);
2105         $this->assertTrue($posts['ratinginfo']['ratings'][0]['canviewaggregate']);
2106         $this->assertFalse($posts['ratinginfo']['canviewall']);
2107         $this->assertFalse($posts['ratinginfo']['ratings'][0]['canrate']);
2108         $this->assertEquals(2, $posts['ratinginfo']['ratings'][0]['count']);
2109         $this->assertEquals(($rating1->rating + $rating2->rating) / 2, $posts['ratinginfo']['ratings'][0]['aggregate']);
2111         // Retrieve the rating for the post as teacher.
2112         $this->setUser($teacher);
2113         $posts = mod_forum_external::get_forum_discussion_posts($discussion->id);
2114         $posts = external_api::clean_returnvalue(mod_forum_external::get_forum_discussion_posts_returns(), $posts);
2115         $this->assertCount(1, $posts['ratinginfo']['ratings']);
2116         $this->assertTrue($posts['ratinginfo']['ratings'][0]['canviewaggregate']);
2117         $this->assertTrue($posts['ratinginfo']['canviewall']);
2118         $this->assertTrue($posts['ratinginfo']['ratings'][0]['canrate']);
2119         $this->assertEquals(2, $posts['ratinginfo']['ratings'][0]['count']);
2120         $this->assertEquals(($rating1->rating + $rating2->rating) / 2, $posts['ratinginfo']['ratings'][0]['aggregate']);
2121     }
2123     /**
2124      * Test mod_forum_get_forum_access_information.
2125      */
2126     public function test_mod_forum_get_forum_access_information() {
2127         global $DB;
2129         $this->resetAfterTest(true);
2131         $student = self::getDataGenerator()->create_user();
2132         $course = self::getDataGenerator()->create_course();
2133         // Create the forum.
2134         $record = new stdClass();
2135         $record->course = $course->id;
2136         $forum = self::getDataGenerator()->create_module('forum', $record);
2138         $studentrole = $DB->get_record('role', array('shortname' => 'student'));
2139         $this->getDataGenerator()->enrol_user($student->id, $course->id, $studentrole->id, 'manual');
2141         self::setUser($student);
2142         $result = mod_forum_external::get_forum_access_information($forum->id);
2143         $result = external_api::clean_returnvalue(mod_forum_external::get_forum_access_information_returns(), $result);
2145         // Check default values for capabilities.
2146         $enabledcaps = array('canviewdiscussion', 'canstartdiscussion', 'canreplypost', 'canviewrating', 'cancreateattachment',
2147             'canexportownpost', 'cancantogglefavourite', 'candeleteownpost', 'canallowforcesubscribe');
2149         unset($result['warnings']);
2150         foreach ($result as $capname => $capvalue) {
2151             if (in_array($capname, $enabledcaps)) {
2152                 $this->assertTrue($capvalue);
2153             } else {
2154                 $this->assertFalse($capvalue);
2155             }
2156         }
2157         // Now, unassign some capabilities.
2158         unassign_capability('mod/forum:deleteownpost', $studentrole->id);
2159         unassign_capability('mod/forum:allowforcesubscribe', $studentrole->id);
2160         array_pop($enabledcaps);
2161         array_pop($enabledcaps);
2162         accesslib_clear_all_caches_for_unit_testing();
2164         $result = mod_forum_external::get_forum_access_information($forum->id);
2165         $result = external_api::clean_returnvalue(mod_forum_external::get_forum_access_information_returns(), $result);
2166         unset($result['warnings']);
2167         foreach ($result as $capname => $capvalue) {
2168             if (in_array($capname, $enabledcaps)) {
2169                 $this->assertTrue($capvalue);
2170             } else {
2171                 $this->assertFalse($capvalue);
2172             }
2173         }
2174     }
2176     /**
2177      * Test add_discussion_post
2178      */
2179     public function test_add_discussion_post_private() {
2180         global $DB;
2182         $this->resetAfterTest(true);
2184         self::setAdminUser();
2186         // Create course to add the module.
2187         $course = self::getDataGenerator()->create_course();
2189         // Standard forum.
2190         $record = new stdClass();
2191         $record->course = $course->id;
2192         $forum = self::getDataGenerator()->create_module('forum', $record);
2193         $cm = get_coursemodule_from_id('forum', $forum->cmid, 0, false, MUST_EXIST);
2194         $forumcontext = context_module::instance($forum->cmid);
2195         $generator = self::getDataGenerator()->get_plugin_generator('mod_forum');
2197         // Create an enrol users.
2198         $student1 = self::getDataGenerator()->create_user();
2199         $this->getDataGenerator()->enrol_user($student1->id, $course->id, 'student');
2200         $student2 = self::getDataGenerator()->create_user();
2201         $this->getDataGenerator()->enrol_user($student2->id, $course->id, 'student');
2202         $teacher1 = self::getDataGenerator()->create_user();
2203         $this->getDataGenerator()->enrol_user($teacher1->id, $course->id, 'editingteacher');
2204         $teacher2 = self::getDataGenerator()->create_user();
2205         $this->getDataGenerator()->enrol_user($teacher2->id, $course->id, 'editingteacher');
2207         // Add a new discussion to the forum.
2208         self::setUser($student1);
2209         $record = new stdClass();
2210         $record->course = $course->id;
2211         $record->userid = $student1->id;
2212         $record->forum = $forum->id;
2213         $discussion = $generator->create_discussion($record);
2215         // Have the teacher reply privately.
2216         self::setUser($teacher1);
2217         $post = mod_forum_external::add_discussion_post($discussion->firstpost, 'some subject', 'some text here...', [
2218                 [
2219                     'name' => 'private',
2220                     'value' => true,
2221                 ],
2222             ]);
2223         $post = external_api::clean_returnvalue(mod_forum_external::add_discussion_post_returns(), $post);
2224         $privatereply = $DB->get_record('forum_posts', array('id' => $post['postid']));
2225         $this->assertEquals($student1->id, $privatereply->privatereplyto);
2226         // Bump the time of the private reply to ensure order.
2227         $privatereply->created++;
2228         $privatereply->modified = $privatereply->created;
2229         $DB->update_record('forum_posts', $privatereply);
2231         // The teacher will receive their private reply.
2232         self::setUser($teacher1);
2233         $posts = mod_forum_external::get_forum_discussion_posts($discussion->id);
2234         $posts = external_api::clean_returnvalue(mod_forum_external::get_forum_discussion_posts_returns(), $posts);
2235         $this->assertEquals(2, count($posts['posts']));
2236         $this->assertTrue($posts['posts'][0]['isprivatereply']);
2238         // Another teacher on the course will also receive the private reply.
2239         self::setUser($teacher2);
2240         $posts = mod_forum_external::get_forum_discussion_posts($discussion->id);
2241         $posts = external_api::clean_returnvalue(mod_forum_external::get_forum_discussion_posts_returns(), $posts);
2242         $this->assertEquals(2, count($posts['posts']));
2243         $this->assertTrue($posts['posts'][0]['isprivatereply']);
2245         // The student will receive the private reply.
2246         self::setUser($student1);
2247         $posts = mod_forum_external::get_forum_discussion_posts($discussion->id);
2248         $posts = external_api::clean_returnvalue(mod_forum_external::get_forum_discussion_posts_returns(), $posts);
2249         $this->assertEquals(2, count($posts['posts']));
2250         $this->assertTrue($posts['posts'][0]['isprivatereply']);
2252         // Another student will not receive the private reply.
2253         self::setUser($student2);
2254         $posts = mod_forum_external::get_forum_discussion_posts($discussion->id);
2255         $posts = external_api::clean_returnvalue(mod_forum_external::get_forum_discussion_posts_returns(), $posts);
2256         $this->assertEquals(1, count($posts['posts']));
2257         $this->assertFalse($posts['posts'][0]['isprivatereply']);
2259         // A user cannot reply to a private reply.
2260         self::setUser($teacher2);
2261         $this->expectException('coding_exception');
2262         $post = mod_forum_external::add_discussion_post($privatereply->id, 'some subject', 'some text here...', [
2263                 'options' => [
2264                     'name' => 'private',
2265                     'value' => false,
2266                 ],
2267             ]);
2268     }