1 |
efrain |
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/>.
|
|
|
16 |
|
|
|
17 |
namespace mod_forum;
|
|
|
18 |
|
|
|
19 |
use core_external\external_api;
|
|
|
20 |
use externallib_advanced_testcase;
|
|
|
21 |
use mod_forum_external;
|
|
|
22 |
|
|
|
23 |
defined('MOODLE_INTERNAL') || die();
|
|
|
24 |
|
|
|
25 |
global $CFG;
|
|
|
26 |
|
|
|
27 |
require_once($CFG->dirroot . '/webservice/tests/helpers.php');
|
|
|
28 |
require_once($CFG->dirroot . '/mod/forum/lib.php');
|
|
|
29 |
|
|
|
30 |
/**
|
|
|
31 |
* The module forums external functions unit tests
|
|
|
32 |
*
|
|
|
33 |
* @package mod_forum
|
|
|
34 |
* @category external
|
|
|
35 |
* @copyright 2012 Mark Nelson <markn@moodle.com>
|
|
|
36 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
37 |
*/
|
|
|
38 |
final class externallib_test extends externallib_advanced_testcase {
|
|
|
39 |
|
|
|
40 |
/**
|
|
|
41 |
* Tests set up
|
|
|
42 |
*/
|
|
|
43 |
protected function setUp(): void {
|
|
|
44 |
global $CFG;
|
|
|
45 |
|
|
|
46 |
// We must clear the subscription caches. This has to be done both before each test, and after in case of other
|
|
|
47 |
// tests using these functions.
|
|
|
48 |
\mod_forum\subscriptions::reset_forum_cache();
|
|
|
49 |
|
|
|
50 |
require_once($CFG->dirroot . '/mod/forum/externallib.php');
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
public function tearDown(): void {
|
|
|
54 |
// We must clear the subscription caches. This has to be done both before each test, and after in case of other
|
|
|
55 |
// tests using these functions.
|
|
|
56 |
\mod_forum\subscriptions::reset_forum_cache();
|
|
|
57 |
}
|
|
|
58 |
|
|
|
59 |
/**
|
|
|
60 |
* Get the expected attachment.
|
|
|
61 |
*
|
|
|
62 |
* @param \stored_file $file
|
|
|
63 |
* @param array $values
|
|
|
64 |
* @param \moodle_url|null $url
|
|
|
65 |
* @return array
|
|
|
66 |
*/
|
|
|
67 |
protected function get_expected_attachment(\stored_file $file, array $values = [], ?\moodle_url $url = null): array {
|
|
|
68 |
if (!$url) {
|
|
|
69 |
$url = \moodle_url::make_pluginfile_url(
|
|
|
70 |
$file->get_contextid(),
|
|
|
71 |
$file->get_component(),
|
|
|
72 |
$file->get_filearea(),
|
|
|
73 |
$file->get_itemid(),
|
|
|
74 |
$file->get_filepath(),
|
|
|
75 |
$file->get_filename()
|
|
|
76 |
);
|
|
|
77 |
$url->param('forcedownload', 1);
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
return array_merge(
|
|
|
81 |
[
|
|
|
82 |
'contextid' => $file->get_contextid(),
|
|
|
83 |
'component' => $file->get_component(),
|
|
|
84 |
'filearea' => $file->get_filearea(),
|
|
|
85 |
'itemid' => $file->get_itemid(),
|
|
|
86 |
'filepath' => $file->get_filepath(),
|
|
|
87 |
'filename' => $file->get_filename(),
|
|
|
88 |
'isdir' => $file->is_directory(),
|
|
|
89 |
'isimage' => $file->is_valid_image(),
|
|
|
90 |
'timemodified' => $file->get_timemodified(),
|
|
|
91 |
'timecreated' => $file->get_timecreated(),
|
|
|
92 |
'filesize' => $file->get_filesize(),
|
|
|
93 |
'author' => $file->get_author(),
|
|
|
94 |
'license' => $file->get_license(),
|
|
|
95 |
'filenameshort' => $file->get_filename(),
|
|
|
96 |
'filesizeformatted' => display_size((int) $file->get_filesize()),
|
|
|
97 |
'icon' => $file->is_directory() ? file_folder_icon() : file_file_icon($file),
|
|
|
98 |
'timecreatedformatted' => userdate($file->get_timecreated()),
|
|
|
99 |
'timemodifiedformatted' => userdate($file->get_timemodified()),
|
|
|
100 |
'url' => $url->out(),
|
|
|
101 |
], $values);
|
|
|
102 |
}
|
|
|
103 |
|
|
|
104 |
/**
|
|
|
105 |
* Test get forums
|
|
|
106 |
*/
|
11 |
efrain |
107 |
public function test_mod_forum_get_forums_by_courses(): void {
|
1 |
efrain |
108 |
global $USER, $CFG, $DB;
|
|
|
109 |
|
|
|
110 |
$this->resetAfterTest(true);
|
|
|
111 |
|
|
|
112 |
// Create a user.
|
|
|
113 |
$user = self::getDataGenerator()->create_user(array('trackforums' => 1));
|
|
|
114 |
|
|
|
115 |
// Set to the user.
|
|
|
116 |
self::setUser($user);
|
|
|
117 |
|
|
|
118 |
// Create courses to add the modules.
|
|
|
119 |
$course1 = self::getDataGenerator()->create_course();
|
|
|
120 |
$course2 = self::getDataGenerator()->create_course();
|
|
|
121 |
|
|
|
122 |
// First forum.
|
|
|
123 |
$record = new \stdClass();
|
|
|
124 |
$record->introformat = FORMAT_HTML;
|
|
|
125 |
$record->course = $course1->id;
|
|
|
126 |
$record->trackingtype = FORUM_TRACKING_FORCED;
|
|
|
127 |
$forum1 = self::getDataGenerator()->create_module('forum', $record);
|
|
|
128 |
|
|
|
129 |
// Second forum.
|
|
|
130 |
$record = new \stdClass();
|
|
|
131 |
$record->introformat = FORMAT_HTML;
|
|
|
132 |
$record->course = $course2->id;
|
|
|
133 |
$record->trackingtype = FORUM_TRACKING_OFF;
|
|
|
134 |
$forum2 = self::getDataGenerator()->create_module('forum', $record);
|
|
|
135 |
$forum2->introfiles = [];
|
|
|
136 |
|
|
|
137 |
// Add discussions to the forums.
|
|
|
138 |
$record = new \stdClass();
|
|
|
139 |
$record->course = $course1->id;
|
|
|
140 |
$record->userid = $user->id;
|
|
|
141 |
$record->forum = $forum1->id;
|
|
|
142 |
$discussion1 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
|
|
|
143 |
// Expect one discussion.
|
|
|
144 |
$forum1->numdiscussions = 1;
|
|
|
145 |
$forum1->cancreatediscussions = true;
|
|
|
146 |
$forum1->istracked = true;
|
|
|
147 |
$forum1->unreadpostscount = 0;
|
|
|
148 |
$forum1->introfiles = [];
|
|
|
149 |
$forum1->lang = '';
|
|
|
150 |
|
|
|
151 |
$record = new \stdClass();
|
|
|
152 |
$record->course = $course2->id;
|
|
|
153 |
$record->userid = $user->id;
|
|
|
154 |
$record->forum = $forum2->id;
|
|
|
155 |
$discussion2 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
|
|
|
156 |
$discussion3 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
|
|
|
157 |
// Expect two discussions.
|
|
|
158 |
$forum2->numdiscussions = 2;
|
|
|
159 |
// Default limited role, no create discussion capability enabled.
|
|
|
160 |
$forum2->cancreatediscussions = false;
|
|
|
161 |
$forum2->istracked = false;
|
|
|
162 |
$forum2->lang = '';
|
|
|
163 |
|
|
|
164 |
// Check the forum was correctly created.
|
|
|
165 |
$this->assertEquals(2, $DB->count_records_select('forum', 'id = :forum1 OR id = :forum2',
|
|
|
166 |
array('forum1' => $forum1->id, 'forum2' => $forum2->id)));
|
|
|
167 |
|
|
|
168 |
// Enrol the user in two courses.
|
|
|
169 |
// DataGenerator->enrol_user automatically sets a role for the user with the permission mod/form:viewdiscussion.
|
|
|
170 |
$this->getDataGenerator()->enrol_user($user->id, $course1->id, null, 'manual');
|
|
|
171 |
// Execute real Moodle enrolment as we'll call unenrol() method on the instance later.
|
|
|
172 |
$enrol = enrol_get_plugin('manual');
|
|
|
173 |
$enrolinstances = enrol_get_instances($course2->id, true);
|
|
|
174 |
foreach ($enrolinstances as $courseenrolinstance) {
|
|
|
175 |
if ($courseenrolinstance->enrol == "manual") {
|
|
|
176 |
$instance2 = $courseenrolinstance;
|
|
|
177 |
break;
|
|
|
178 |
}
|
|
|
179 |
}
|
|
|
180 |
$enrol->enrol_user($instance2, $user->id);
|
|
|
181 |
|
|
|
182 |
// Assign capabilities to view forums for forum 2.
|
|
|
183 |
$cm2 = get_coursemodule_from_id('forum', $forum2->cmid, 0, false, MUST_EXIST);
|
|
|
184 |
$context2 = \context_module::instance($cm2->id);
|
|
|
185 |
$newrole = create_role('Role 2', 'role2', 'Role 2 description');
|
|
|
186 |
$roleid2 = $this->assignUserCapability('mod/forum:viewdiscussion', $context2->id, $newrole);
|
|
|
187 |
|
|
|
188 |
// Create what we expect to be returned when querying the two courses.
|
|
|
189 |
unset($forum1->displaywordcount);
|
|
|
190 |
unset($forum2->displaywordcount);
|
|
|
191 |
|
|
|
192 |
$expectedforums = array();
|
|
|
193 |
$expectedforums[$forum1->id] = (array) $forum1;
|
|
|
194 |
$expectedforums[$forum2->id] = (array) $forum2;
|
|
|
195 |
|
|
|
196 |
// Call the external function passing course ids.
|
|
|
197 |
$forums = mod_forum_external::get_forums_by_courses(array($course1->id, $course2->id));
|
|
|
198 |
$forums = external_api::clean_returnvalue(mod_forum_external::get_forums_by_courses_returns(), $forums);
|
|
|
199 |
$this->assertCount(2, $forums);
|
|
|
200 |
foreach ($forums as $forum) {
|
|
|
201 |
$this->assertEquals($expectedforums[$forum['id']], $forum);
|
|
|
202 |
}
|
|
|
203 |
|
|
|
204 |
// Call the external function without passing course id.
|
|
|
205 |
$forums = mod_forum_external::get_forums_by_courses();
|
|
|
206 |
$forums = external_api::clean_returnvalue(mod_forum_external::get_forums_by_courses_returns(), $forums);
|
|
|
207 |
$this->assertCount(2, $forums);
|
|
|
208 |
foreach ($forums as $forum) {
|
|
|
209 |
$this->assertEquals($expectedforums[$forum['id']], $forum);
|
|
|
210 |
}
|
|
|
211 |
|
|
|
212 |
// Unenrol user from second course and alter expected forums.
|
|
|
213 |
$enrol->unenrol_user($instance2, $user->id);
|
|
|
214 |
unset($expectedforums[$forum2->id]);
|
|
|
215 |
|
|
|
216 |
// Call the external function without passing course id.
|
|
|
217 |
$forums = mod_forum_external::get_forums_by_courses();
|
|
|
218 |
$forums = external_api::clean_returnvalue(mod_forum_external::get_forums_by_courses_returns(), $forums);
|
|
|
219 |
$this->assertCount(1, $forums);
|
|
|
220 |
$this->assertEquals($expectedforums[$forum1->id], $forums[0]);
|
|
|
221 |
$this->assertTrue($forums[0]['cancreatediscussions']);
|
|
|
222 |
|
|
|
223 |
// Change the type of the forum, the user shouldn't be able to add discussions.
|
|
|
224 |
$DB->set_field('forum', 'type', 'news', array('id' => $forum1->id));
|
|
|
225 |
$forums = mod_forum_external::get_forums_by_courses();
|
|
|
226 |
$forums = external_api::clean_returnvalue(mod_forum_external::get_forums_by_courses_returns(), $forums);
|
|
|
227 |
$this->assertFalse($forums[0]['cancreatediscussions']);
|
|
|
228 |
|
|
|
229 |
// Call for the second course we unenrolled the user from.
|
|
|
230 |
$forums = mod_forum_external::get_forums_by_courses(array($course2->id));
|
|
|
231 |
$forums = external_api::clean_returnvalue(mod_forum_external::get_forums_by_courses_returns(), $forums);
|
|
|
232 |
$this->assertCount(0, $forums);
|
|
|
233 |
}
|
|
|
234 |
|
|
|
235 |
/**
|
|
|
236 |
* Test the toggle favourite state
|
|
|
237 |
*/
|
11 |
efrain |
238 |
public function test_mod_forum_toggle_favourite_state(): void {
|
1 |
efrain |
239 |
global $USER, $CFG, $DB;
|
|
|
240 |
|
|
|
241 |
$this->resetAfterTest(true);
|
|
|
242 |
|
|
|
243 |
// Create a user.
|
|
|
244 |
$user = self::getDataGenerator()->create_user(array('trackforums' => 1));
|
|
|
245 |
|
|
|
246 |
// Set to the user.
|
|
|
247 |
self::setUser($user);
|
|
|
248 |
|
|
|
249 |
// Create courses to add the modules.
|
|
|
250 |
$course1 = self::getDataGenerator()->create_course();
|
|
|
251 |
$this->getDataGenerator()->enrol_user($user->id, $course1->id);
|
|
|
252 |
|
|
|
253 |
$record = new \stdClass();
|
|
|
254 |
$record->introformat = FORMAT_HTML;
|
|
|
255 |
$record->course = $course1->id;
|
|
|
256 |
$record->trackingtype = FORUM_TRACKING_OFF;
|
|
|
257 |
$forum1 = self::getDataGenerator()->create_module('forum', $record);
|
|
|
258 |
$forum1->introfiles = [];
|
|
|
259 |
|
|
|
260 |
// Add discussions to the forums.
|
|
|
261 |
$record = new \stdClass();
|
|
|
262 |
$record->course = $course1->id;
|
|
|
263 |
$record->userid = $user->id;
|
|
|
264 |
$record->forum = $forum1->id;
|
|
|
265 |
$discussion1 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
|
|
|
266 |
|
|
|
267 |
$response = mod_forum_external::toggle_favourite_state($discussion1->id, 1);
|
|
|
268 |
$response = external_api::clean_returnvalue(mod_forum_external::toggle_favourite_state_returns(), $response);
|
|
|
269 |
$this->assertTrue($response['userstate']['favourited']);
|
|
|
270 |
|
|
|
271 |
$response = mod_forum_external::toggle_favourite_state($discussion1->id, 0);
|
|
|
272 |
$response = external_api::clean_returnvalue(mod_forum_external::toggle_favourite_state_returns(), $response);
|
|
|
273 |
$this->assertFalse($response['userstate']['favourited']);
|
|
|
274 |
|
|
|
275 |
$this->setUser(0);
|
|
|
276 |
try {
|
|
|
277 |
$response = mod_forum_external::toggle_favourite_state($discussion1->id, 0);
|
|
|
278 |
} catch (\moodle_exception $e) {
|
|
|
279 |
$this->assertEquals('requireloginerror', $e->errorcode);
|
|
|
280 |
}
|
|
|
281 |
}
|
|
|
282 |
|
|
|
283 |
/**
|
|
|
284 |
* Test the toggle pin state
|
|
|
285 |
*/
|
11 |
efrain |
286 |
public function test_mod_forum_set_pin_state(): void {
|
1 |
efrain |
287 |
$this->resetAfterTest(true);
|
|
|
288 |
|
|
|
289 |
// Create a user.
|
|
|
290 |
$user = self::getDataGenerator()->create_user(array('trackforums' => 1));
|
|
|
291 |
|
|
|
292 |
// Set to the user.
|
|
|
293 |
self::setUser($user);
|
|
|
294 |
|
|
|
295 |
// Create courses to add the modules.
|
|
|
296 |
$course1 = self::getDataGenerator()->create_course();
|
|
|
297 |
$this->getDataGenerator()->enrol_user($user->id, $course1->id);
|
|
|
298 |
|
|
|
299 |
$record = new \stdClass();
|
|
|
300 |
$record->introformat = FORMAT_HTML;
|
|
|
301 |
$record->course = $course1->id;
|
|
|
302 |
$record->trackingtype = FORUM_TRACKING_OFF;
|
|
|
303 |
$forum1 = self::getDataGenerator()->create_module('forum', $record);
|
|
|
304 |
$forum1->introfiles = [];
|
|
|
305 |
|
|
|
306 |
// Add discussions to the forums.
|
|
|
307 |
$record = new \stdClass();
|
|
|
308 |
$record->course = $course1->id;
|
|
|
309 |
$record->userid = $user->id;
|
|
|
310 |
$record->forum = $forum1->id;
|
|
|
311 |
$discussion1 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
|
|
|
312 |
|
|
|
313 |
try {
|
|
|
314 |
$response = mod_forum_external::set_pin_state($discussion1->id, 1);
|
|
|
315 |
} catch (\Exception $e) {
|
|
|
316 |
$this->assertEquals('cannotpindiscussions', $e->errorcode);
|
|
|
317 |
}
|
|
|
318 |
|
|
|
319 |
self::setAdminUser();
|
|
|
320 |
$response = mod_forum_external::set_pin_state($discussion1->id, 1);
|
|
|
321 |
$response = external_api::clean_returnvalue(mod_forum_external::set_pin_state_returns(), $response);
|
|
|
322 |
$this->assertTrue($response['pinned']);
|
|
|
323 |
|
|
|
324 |
$response = mod_forum_external::set_pin_state($discussion1->id, 0);
|
|
|
325 |
$response = external_api::clean_returnvalue(mod_forum_external::set_pin_state_returns(), $response);
|
|
|
326 |
$this->assertFalse($response['pinned']);
|
|
|
327 |
}
|
|
|
328 |
|
|
|
329 |
/**
|
|
|
330 |
* Test get forum posts
|
|
|
331 |
*
|
|
|
332 |
* Tests is similar to the get_forum_discussion_posts only utilizing the new return structure and entities
|
|
|
333 |
*/
|
11 |
efrain |
334 |
public function test_mod_forum_get_discussion_posts(): void {
|
1 |
efrain |
335 |
global $CFG;
|
|
|
336 |
|
|
|
337 |
$this->resetAfterTest(true);
|
|
|
338 |
|
|
|
339 |
// Set the CFG variable to allow track forums.
|
|
|
340 |
$CFG->forum_trackreadposts = true;
|
|
|
341 |
|
|
|
342 |
$urlfactory = \mod_forum\local\container::get_url_factory();
|
|
|
343 |
$legacyfactory = \mod_forum\local\container::get_legacy_data_mapper_factory();
|
|
|
344 |
$entityfactory = \mod_forum\local\container::get_entity_factory();
|
|
|
345 |
|
|
|
346 |
// Create course to add the module.
|
|
|
347 |
$course1 = self::getDataGenerator()->create_course();
|
|
|
348 |
|
|
|
349 |
// Create a user who can track forums.
|
|
|
350 |
$record = new \stdClass();
|
|
|
351 |
$record->trackforums = true;
|
|
|
352 |
$user1 = self::getDataGenerator()->create_user($record);
|
|
|
353 |
// Create a bunch of other users to post.
|
|
|
354 |
$user2 = self::getDataGenerator()->create_user();
|
|
|
355 |
$user2entity = $entityfactory->get_author_from_stdClass($user2);
|
|
|
356 |
$exporteduser2 = [
|
|
|
357 |
'id' => (int) $user2->id,
|
|
|
358 |
'fullname' => fullname($user2),
|
|
|
359 |
'isdeleted' => false,
|
|
|
360 |
'groups' => [],
|
|
|
361 |
'urls' => [
|
|
|
362 |
'profile' => $urlfactory->get_author_profile_url($user2entity, $course1->id)->out(false),
|
|
|
363 |
'profileimage' => $urlfactory->get_author_profile_image_url($user2entity),
|
|
|
364 |
]
|
|
|
365 |
];
|
|
|
366 |
$user2->fullname = $exporteduser2['fullname'];
|
|
|
367 |
|
|
|
368 |
$user3 = self::getDataGenerator()->create_user(['fullname' => "Mr Pants 1"]);
|
|
|
369 |
$user3entity = $entityfactory->get_author_from_stdClass($user3);
|
|
|
370 |
$exporteduser3 = [
|
|
|
371 |
'id' => (int) $user3->id,
|
|
|
372 |
'fullname' => fullname($user3),
|
|
|
373 |
'groups' => [],
|
|
|
374 |
'isdeleted' => false,
|
|
|
375 |
'urls' => [
|
|
|
376 |
'profile' => $urlfactory->get_author_profile_url($user3entity, $course1->id)->out(false),
|
|
|
377 |
'profileimage' => $urlfactory->get_author_profile_image_url($user3entity),
|
|
|
378 |
]
|
|
|
379 |
];
|
|
|
380 |
$user3->fullname = $exporteduser3['fullname'];
|
|
|
381 |
$forumgenerator = self::getDataGenerator()->get_plugin_generator('mod_forum');
|
|
|
382 |
|
|
|
383 |
// Set the first created user to the test user.
|
|
|
384 |
self::setUser($user1);
|
|
|
385 |
|
|
|
386 |
// Forum with tracking off.
|
|
|
387 |
$record = new \stdClass();
|
|
|
388 |
$record->course = $course1->id;
|
|
|
389 |
$record->trackingtype = FORUM_TRACKING_OFF;
|
|
|
390 |
// Display word count. Otherwise, word and char counts will be set to null by the forum post exporter.
|
|
|
391 |
$record->displaywordcount = true;
|
|
|
392 |
$forum1 = self::getDataGenerator()->create_module('forum', $record);
|
|
|
393 |
$forum1context = \context_module::instance($forum1->cmid);
|
|
|
394 |
|
|
|
395 |
// Forum with tracking enabled.
|
|
|
396 |
$record = new \stdClass();
|
|
|
397 |
$record->course = $course1->id;
|
|
|
398 |
$forum2 = self::getDataGenerator()->create_module('forum', $record);
|
|
|
399 |
$forum2cm = get_coursemodule_from_id('forum', $forum2->cmid);
|
|
|
400 |
$forum2context = \context_module::instance($forum2->cmid);
|
|
|
401 |
|
|
|
402 |
// Add discussions to the forums.
|
|
|
403 |
$record = new \stdClass();
|
|
|
404 |
$record->course = $course1->id;
|
|
|
405 |
$record->userid = $user1->id;
|
|
|
406 |
$record->forum = $forum1->id;
|
|
|
407 |
$discussion1 = $forumgenerator->create_discussion($record);
|
|
|
408 |
|
|
|
409 |
$record = new \stdClass();
|
|
|
410 |
$record->course = $course1->id;
|
|
|
411 |
$record->userid = $user2->id;
|
|
|
412 |
$record->forum = $forum1->id;
|
|
|
413 |
$discussion2 = $forumgenerator->create_discussion($record);
|
|
|
414 |
|
|
|
415 |
$record = new \stdClass();
|
|
|
416 |
$record->course = $course1->id;
|
|
|
417 |
$record->userid = $user2->id;
|
|
|
418 |
$record->forum = $forum2->id;
|
|
|
419 |
$discussion3 = $forumgenerator->create_discussion($record);
|
|
|
420 |
|
|
|
421 |
// Add 2 replies to the discussion 1 from different users.
|
|
|
422 |
$record = new \stdClass();
|
|
|
423 |
$record->discussion = $discussion1->id;
|
|
|
424 |
$record->parent = $discussion1->firstpost;
|
|
|
425 |
$record->userid = $user2->id;
|
|
|
426 |
$discussion1reply1 = $forumgenerator->create_post($record);
|
|
|
427 |
$filename = 'shouldbeanimage.jpg';
|
|
|
428 |
// Add a fake inline image to the post.
|
|
|
429 |
$filerecordinline = array(
|
|
|
430 |
'contextid' => $forum1context->id,
|
|
|
431 |
'component' => 'mod_forum',
|
|
|
432 |
'filearea' => 'post',
|
|
|
433 |
'itemid' => $discussion1reply1->id,
|
|
|
434 |
'filepath' => '/',
|
|
|
435 |
'filename' => $filename,
|
|
|
436 |
);
|
|
|
437 |
$fs = get_file_storage();
|
|
|
438 |
$timepost = time();
|
|
|
439 |
$file = $fs->create_file_from_string($filerecordinline, 'image contents (not really)');
|
|
|
440 |
|
|
|
441 |
$record->parent = $discussion1reply1->id;
|
|
|
442 |
$record->userid = $user3->id;
|
|
|
443 |
$discussion1reply2 = $forumgenerator->create_post($record);
|
|
|
444 |
|
|
|
445 |
// Enrol the user in the course.
|
|
|
446 |
$enrol = enrol_get_plugin('manual');
|
|
|
447 |
// Following line enrol and assign default role id to the user.
|
|
|
448 |
// So the user automatically gets mod/forum:viewdiscussion on all forums of the course.
|
|
|
449 |
$this->getDataGenerator()->enrol_user($user1->id, $course1->id);
|
|
|
450 |
$this->getDataGenerator()->enrol_user($user2->id, $course1->id);
|
|
|
451 |
|
|
|
452 |
// Delete one user, to test that we still receive posts by this user.
|
|
|
453 |
delete_user($user3);
|
|
|
454 |
$exporteduser3 = [
|
|
|
455 |
'id' => (int) $user3->id,
|
|
|
456 |
'fullname' => get_string('deleteduser', 'mod_forum'),
|
|
|
457 |
'groups' => [],
|
|
|
458 |
'isdeleted' => true,
|
|
|
459 |
'urls' => [
|
|
|
460 |
'profile' => $urlfactory->get_author_profile_url($user3entity, $course1->id)->out(false),
|
|
|
461 |
'profileimage' => $urlfactory->get_author_profile_image_url($user3entity),
|
|
|
462 |
]
|
|
|
463 |
];
|
|
|
464 |
|
|
|
465 |
// Create what we expect to be returned when querying the discussion.
|
|
|
466 |
$expectedposts = array(
|
|
|
467 |
'posts' => array(),
|
|
|
468 |
'courseid' => $course1->id,
|
|
|
469 |
'forumid' => $forum1->id,
|
|
|
470 |
'ratinginfo' => array(
|
|
|
471 |
'contextid' => $forum1context->id,
|
|
|
472 |
'component' => 'mod_forum',
|
|
|
473 |
'ratingarea' => 'post',
|
|
|
474 |
'canviewall' => null,
|
|
|
475 |
'canviewany' => null,
|
|
|
476 |
'scales' => array(),
|
|
|
477 |
'ratings' => array(),
|
|
|
478 |
),
|
|
|
479 |
'warnings' => array(),
|
|
|
480 |
);
|
|
|
481 |
|
|
|
482 |
// User pictures are initially empty, we should get the links once the external function is called.
|
|
|
483 |
$isolatedurl = $urlfactory->get_discussion_view_url_from_discussion_id($discussion1reply2->discussion);
|
|
|
484 |
$isolatedurl->params(['parent' => $discussion1reply2->id]);
|
|
|
485 |
$message = file_rewrite_pluginfile_urls($discussion1reply2->message, 'pluginfile.php',
|
|
|
486 |
$forum1context->id, 'mod_forum', 'post', $discussion1reply2->id);
|
|
|
487 |
$expectedposts['posts'][] = array(
|
|
|
488 |
'id' => $discussion1reply2->id,
|
|
|
489 |
'discussionid' => $discussion1reply2->discussion,
|
|
|
490 |
'parentid' => $discussion1reply2->parent,
|
|
|
491 |
'hasparent' => true,
|
|
|
492 |
'timecreated' => $discussion1reply2->created,
|
|
|
493 |
'timemodified' => $discussion1reply2->modified,
|
|
|
494 |
'subject' => $discussion1reply2->subject,
|
|
|
495 |
'replysubject' => get_string('re', 'mod_forum') . " {$discussion1reply2->subject}",
|
|
|
496 |
'message' => $message,
|
|
|
497 |
'messageformat' => 1, // This value is usually changed by \core_external\util::format_text() function.
|
|
|
498 |
'unread' => null,
|
|
|
499 |
'isdeleted' => false,
|
|
|
500 |
'isprivatereply' => false,
|
|
|
501 |
'haswordcount' => true,
|
|
|
502 |
'wordcount' => count_words($message),
|
|
|
503 |
'charcount' => count_letters($message),
|
|
|
504 |
'author'=> $exporteduser3,
|
|
|
505 |
'attachments' => [],
|
|
|
506 |
'messageinlinefiles' => [],
|
|
|
507 |
'tags' => [],
|
|
|
508 |
'html' => [
|
|
|
509 |
'rating' => null,
|
|
|
510 |
'taglist' => null,
|
|
|
511 |
'authorsubheading' => $forumgenerator->get_author_subheading_html((object)$exporteduser3, $discussion1reply2->created)
|
|
|
512 |
],
|
|
|
513 |
'capabilities' => [
|
|
|
514 |
'view' => 1,
|
|
|
515 |
'edit' => 0,
|
|
|
516 |
'delete' => 0,
|
|
|
517 |
'split' => 0,
|
|
|
518 |
'reply' => 1,
|
|
|
519 |
'export' => 0,
|
|
|
520 |
'controlreadstatus' => 0,
|
|
|
521 |
'canreplyprivately' => 0,
|
|
|
522 |
'selfenrol' => 0
|
|
|
523 |
],
|
|
|
524 |
'urls' => [
|
|
|
525 |
'view' => $urlfactory->get_view_post_url_from_post_id($discussion1reply2->discussion, $discussion1reply2->id),
|
|
|
526 |
'viewisolated' => $isolatedurl->out(false),
|
|
|
527 |
'viewparent' => $urlfactory->get_view_post_url_from_post_id($discussion1reply2->discussion, $discussion1reply2->parent),
|
|
|
528 |
'edit' => null,
|
|
|
529 |
'delete' =>null,
|
|
|
530 |
'split' => null,
|
|
|
531 |
'reply' => (new \moodle_url('/mod/forum/post.php#mformforum', [
|
|
|
532 |
'reply' => $discussion1reply2->id
|
|
|
533 |
]))->out(false),
|
|
|
534 |
'export' => null,
|
|
|
535 |
'markasread' => null,
|
|
|
536 |
'markasunread' => null,
|
|
|
537 |
'discuss' => $urlfactory->get_discussion_view_url_from_discussion_id($discussion1reply2->discussion),
|
|
|
538 |
],
|
|
|
539 |
);
|
|
|
540 |
|
|
|
541 |
|
|
|
542 |
$isolatedurl = $urlfactory->get_discussion_view_url_from_discussion_id($discussion1reply1->discussion);
|
|
|
543 |
$isolatedurl->params(['parent' => $discussion1reply1->id]);
|
|
|
544 |
$message = file_rewrite_pluginfile_urls($discussion1reply1->message, 'pluginfile.php',
|
|
|
545 |
$forum1context->id, 'mod_forum', 'post', $discussion1reply1->id);
|
|
|
546 |
$expectedposts['posts'][] = array(
|
|
|
547 |
'id' => $discussion1reply1->id,
|
|
|
548 |
'discussionid' => $discussion1reply1->discussion,
|
|
|
549 |
'parentid' => $discussion1reply1->parent,
|
|
|
550 |
'hasparent' => true,
|
|
|
551 |
'timecreated' => $discussion1reply1->created,
|
|
|
552 |
'timemodified' => $discussion1reply1->modified,
|
|
|
553 |
'subject' => $discussion1reply1->subject,
|
|
|
554 |
'replysubject' => get_string('re', 'mod_forum') . " {$discussion1reply1->subject}",
|
|
|
555 |
'message' => $message,
|
|
|
556 |
'messageformat' => 1, // This value is usually changed by \core_external\util::format_text() function.
|
|
|
557 |
'unread' => null,
|
|
|
558 |
'isdeleted' => false,
|
|
|
559 |
'isprivatereply' => false,
|
|
|
560 |
'haswordcount' => true,
|
|
|
561 |
'wordcount' => count_words($message),
|
|
|
562 |
'charcount' => count_letters($message),
|
|
|
563 |
'author'=> $exporteduser2,
|
|
|
564 |
'attachments' => [],
|
|
|
565 |
'messageinlinefiles' => [
|
|
|
566 |
|
|
|
567 |
],
|
|
|
568 |
'tags' => [],
|
|
|
569 |
'html' => [
|
|
|
570 |
'rating' => null,
|
|
|
571 |
'taglist' => null,
|
|
|
572 |
'authorsubheading' => $forumgenerator->get_author_subheading_html((object)$exporteduser2, $discussion1reply1->created)
|
|
|
573 |
],
|
|
|
574 |
'capabilities' => [
|
|
|
575 |
'view' => 1,
|
|
|
576 |
'edit' => 0,
|
|
|
577 |
'delete' => 0,
|
|
|
578 |
'split' => 0,
|
|
|
579 |
'reply' => 1,
|
|
|
580 |
'export' => 0,
|
|
|
581 |
'controlreadstatus' => 0,
|
|
|
582 |
'canreplyprivately' => 0,
|
|
|
583 |
'selfenrol' => 0
|
|
|
584 |
],
|
|
|
585 |
'urls' => [
|
|
|
586 |
'view' => $urlfactory->get_view_post_url_from_post_id($discussion1reply1->discussion, $discussion1reply1->id),
|
|
|
587 |
'viewisolated' => $isolatedurl->out(false),
|
|
|
588 |
'viewparent' => $urlfactory->get_view_post_url_from_post_id($discussion1reply1->discussion, $discussion1reply1->parent),
|
|
|
589 |
'edit' => null,
|
|
|
590 |
'delete' =>null,
|
|
|
591 |
'split' => null,
|
|
|
592 |
'reply' => (new \moodle_url('/mod/forum/post.php#mformforum', [
|
|
|
593 |
'reply' => $discussion1reply1->id
|
|
|
594 |
]))->out(false),
|
|
|
595 |
'export' => null,
|
|
|
596 |
'markasread' => null,
|
|
|
597 |
'markasunread' => null,
|
|
|
598 |
'discuss' => $urlfactory->get_discussion_view_url_from_discussion_id($discussion1reply1->discussion),
|
|
|
599 |
],
|
|
|
600 |
);
|
|
|
601 |
|
|
|
602 |
// Test a discussion with two additional posts (total 3 posts).
|
|
|
603 |
$posts = mod_forum_external::get_discussion_posts($discussion1->id, 'modified', 'DESC', true);
|
|
|
604 |
$posts = external_api::clean_returnvalue(mod_forum_external::get_discussion_posts_returns(), $posts);
|
|
|
605 |
$this->assertEquals(3, count($posts['posts']));
|
|
|
606 |
|
|
|
607 |
// Unset the initial discussion post.
|
|
|
608 |
array_pop($posts['posts']);
|
|
|
609 |
$this->assertEquals($expectedposts, $posts);
|
|
|
610 |
|
|
|
611 |
// Check we receive the unread count correctly on tracked forum.
|
|
|
612 |
forum_tp_count_forum_unread_posts($forum2cm, $course1, true); // Reset static cache.
|
|
|
613 |
$result = mod_forum_external::get_forums_by_courses(array($course1->id));
|
|
|
614 |
$result = external_api::clean_returnvalue(mod_forum_external::get_forums_by_courses_returns(), $result);
|
|
|
615 |
foreach ($result as $f) {
|
|
|
616 |
if ($f['id'] == $forum2->id) {
|
|
|
617 |
$this->assertEquals(1, $f['unreadpostscount']);
|
|
|
618 |
}
|
|
|
619 |
}
|
|
|
620 |
|
|
|
621 |
// Test discussion without additional posts. There should be only one post (the one created by the discussion).
|
|
|
622 |
$posts = mod_forum_external::get_discussion_posts($discussion2->id, 'modified', 'DESC');
|
|
|
623 |
$posts = external_api::clean_returnvalue(mod_forum_external::get_discussion_posts_returns(), $posts);
|
|
|
624 |
$this->assertEquals(1, count($posts['posts']));
|
|
|
625 |
|
|
|
626 |
// Test discussion tracking on not tracked forum.
|
|
|
627 |
$result = mod_forum_external::view_forum_discussion($discussion1->id);
|
|
|
628 |
$result = external_api::clean_returnvalue(mod_forum_external::view_forum_discussion_returns(), $result);
|
|
|
629 |
$this->assertTrue($result['status']);
|
|
|
630 |
$this->assertEmpty($result['warnings']);
|
|
|
631 |
|
|
|
632 |
// Test posts have not been marked as read.
|
|
|
633 |
$posts = mod_forum_external::get_discussion_posts($discussion1->id, 'modified', 'DESC');
|
|
|
634 |
$posts = external_api::clean_returnvalue(mod_forum_external::get_discussion_posts_returns(), $posts);
|
|
|
635 |
foreach ($posts['posts'] as $post) {
|
|
|
636 |
$this->assertNull($post['unread']);
|
|
|
637 |
}
|
|
|
638 |
|
|
|
639 |
// Test discussion tracking on tracked forum.
|
|
|
640 |
$result = mod_forum_external::view_forum_discussion($discussion3->id);
|
|
|
641 |
$result = external_api::clean_returnvalue(mod_forum_external::view_forum_discussion_returns(), $result);
|
|
|
642 |
$this->assertTrue($result['status']);
|
|
|
643 |
$this->assertEmpty($result['warnings']);
|
|
|
644 |
|
|
|
645 |
// Test posts have been marked as read.
|
|
|
646 |
$posts = mod_forum_external::get_discussion_posts($discussion3->id, 'modified', 'DESC');
|
|
|
647 |
$posts = external_api::clean_returnvalue(mod_forum_external::get_discussion_posts_returns(), $posts);
|
|
|
648 |
foreach ($posts['posts'] as $post) {
|
|
|
649 |
$this->assertFalse($post['unread']);
|
|
|
650 |
}
|
|
|
651 |
|
|
|
652 |
// Check we receive 0 unread posts.
|
|
|
653 |
forum_tp_count_forum_unread_posts($forum2cm, $course1, true); // Reset static cache.
|
|
|
654 |
$result = mod_forum_external::get_forums_by_courses(array($course1->id));
|
|
|
655 |
$result = external_api::clean_returnvalue(mod_forum_external::get_forums_by_courses_returns(), $result);
|
|
|
656 |
foreach ($result as $f) {
|
|
|
657 |
if ($f['id'] == $forum2->id) {
|
|
|
658 |
$this->assertEquals(0, $f['unreadpostscount']);
|
|
|
659 |
}
|
|
|
660 |
}
|
|
|
661 |
}
|
|
|
662 |
|
|
|
663 |
/**
|
|
|
664 |
* Test get forum posts
|
|
|
665 |
*/
|
11 |
efrain |
666 |
public function test_mod_forum_get_discussion_posts_deleted(): void {
|
1 |
efrain |
667 |
global $CFG, $PAGE;
|
|
|
668 |
|
|
|
669 |
$this->resetAfterTest(true);
|
|
|
670 |
$generator = self::getDataGenerator()->get_plugin_generator('mod_forum');
|
|
|
671 |
|
|
|
672 |
// Create a course and enrol some users in it.
|
|
|
673 |
$course1 = self::getDataGenerator()->create_course();
|
|
|
674 |
|
|
|
675 |
// Create users.
|
|
|
676 |
$user1 = self::getDataGenerator()->create_user();
|
|
|
677 |
$this->getDataGenerator()->enrol_user($user1->id, $course1->id);
|
|
|
678 |
$user2 = self::getDataGenerator()->create_user();
|
|
|
679 |
$this->getDataGenerator()->enrol_user($user2->id, $course1->id);
|
|
|
680 |
|
|
|
681 |
// Set the first created user to the test user.
|
|
|
682 |
self::setUser($user1);
|
|
|
683 |
|
|
|
684 |
// Create test data.
|
|
|
685 |
$forum1 = self::getDataGenerator()->create_module('forum', (object) [
|
|
|
686 |
'course' => $course1->id,
|
|
|
687 |
]);
|
|
|
688 |
$forum1context = \context_module::instance($forum1->cmid);
|
|
|
689 |
|
|
|
690 |
// Add discussions to the forum.
|
|
|
691 |
$discussion = $generator->create_discussion((object) [
|
|
|
692 |
'course' => $course1->id,
|
|
|
693 |
'userid' => $user1->id,
|
|
|
694 |
'forum' => $forum1->id,
|
|
|
695 |
]);
|
|
|
696 |
|
|
|
697 |
$discussion2 = $generator->create_discussion((object) [
|
|
|
698 |
'course' => $course1->id,
|
|
|
699 |
'userid' => $user2->id,
|
|
|
700 |
'forum' => $forum1->id,
|
|
|
701 |
]);
|
|
|
702 |
|
|
|
703 |
// Add replies to the discussion.
|
|
|
704 |
$discussionreply1 = $generator->create_post((object) [
|
|
|
705 |
'discussion' => $discussion->id,
|
|
|
706 |
'parent' => $discussion->firstpost,
|
|
|
707 |
'userid' => $user2->id,
|
|
|
708 |
]);
|
|
|
709 |
$discussionreply2 = $generator->create_post((object) [
|
|
|
710 |
'discussion' => $discussion->id,
|
|
|
711 |
'parent' => $discussionreply1->id,
|
|
|
712 |
'userid' => $user2->id,
|
|
|
713 |
'subject' => '',
|
|
|
714 |
'message' => '',
|
|
|
715 |
'messageformat' => FORMAT_PLAIN,
|
|
|
716 |
'deleted' => 1,
|
|
|
717 |
]);
|
|
|
718 |
$discussionreply3 = $generator->create_post((object) [
|
|
|
719 |
'discussion' => $discussion->id,
|
|
|
720 |
'parent' => $discussion->firstpost,
|
|
|
721 |
'userid' => $user2->id,
|
|
|
722 |
]);
|
|
|
723 |
|
|
|
724 |
// Test where some posts have been marked as deleted.
|
|
|
725 |
$posts = mod_forum_external::get_discussion_posts($discussion->id, 'modified', 'DESC');
|
|
|
726 |
$posts = external_api::clean_returnvalue(mod_forum_external::get_discussion_posts_returns(), $posts);
|
|
|
727 |
$deletedsubject = get_string('forumsubjectdeleted', 'mod_forum');
|
|
|
728 |
$deletedmessage = get_string('forumbodydeleted', 'mod_forum');
|
|
|
729 |
|
|
|
730 |
foreach ($posts['posts'] as $post) {
|
|
|
731 |
if ($post['id'] == $discussionreply2->id) {
|
|
|
732 |
$this->assertTrue($post['isdeleted']);
|
|
|
733 |
$this->assertEquals($deletedsubject, $post['subject']);
|
|
|
734 |
$this->assertEquals($deletedmessage, $post['message']);
|
|
|
735 |
} else {
|
|
|
736 |
$this->assertFalse($post['isdeleted']);
|
|
|
737 |
$this->assertNotEquals($deletedsubject, $post['subject']);
|
|
|
738 |
$this->assertNotEquals($deletedmessage, $post['message']);
|
|
|
739 |
}
|
|
|
740 |
}
|
|
|
741 |
}
|
|
|
742 |
|
|
|
743 |
/**
|
|
|
744 |
* Test get forum posts returns inline attachments.
|
|
|
745 |
*/
|
11 |
efrain |
746 |
public function test_mod_forum_get_discussion_posts_inline_attachments(): void {
|
1 |
efrain |
747 |
global $CFG;
|
|
|
748 |
|
|
|
749 |
$this->resetAfterTest(true);
|
|
|
750 |
|
|
|
751 |
// Create a course and enrol some users in it.
|
|
|
752 |
$course = self::getDataGenerator()->create_course();
|
|
|
753 |
|
|
|
754 |
// Create users.
|
|
|
755 |
$user = self::getDataGenerator()->create_user();
|
|
|
756 |
$this->getDataGenerator()->enrol_user($user->id, $course->id);
|
|
|
757 |
|
|
|
758 |
|
|
|
759 |
// Set the first created user to the test user.
|
|
|
760 |
self::setUser($user);
|
|
|
761 |
|
|
|
762 |
// Create test data.
|
|
|
763 |
$forum = self::getDataGenerator()->create_module('forum', (object) [
|
|
|
764 |
'course' => $course->id,
|
|
|
765 |
]);
|
|
|
766 |
|
|
|
767 |
// Create a file in a draft area for inline attachments.
|
|
|
768 |
$draftidinlineattach = file_get_unused_draft_itemid();
|
|
|
769 |
$draftidattach = file_get_unused_draft_itemid();
|
|
|
770 |
self::setUser($user);
|
|
|
771 |
$usercontext = \context_user::instance($user->id);
|
|
|
772 |
$filepath = '/';
|
|
|
773 |
$filearea = 'draft';
|
|
|
774 |
$component = 'user';
|
|
|
775 |
$filenameimg = 'fakeimage.png';
|
|
|
776 |
$filerecordinline = [
|
|
|
777 |
'contextid' => $usercontext->id,
|
|
|
778 |
'component' => $component,
|
|
|
779 |
'filearea' => $filearea,
|
|
|
780 |
'itemid' => $draftidinlineattach,
|
|
|
781 |
'filepath' => $filepath,
|
|
|
782 |
'filename' => $filenameimg,
|
|
|
783 |
];
|
|
|
784 |
$fs = get_file_storage();
|
|
|
785 |
$fs->create_file_from_string($filerecordinline, 'image contents (not really)');
|
|
|
786 |
|
|
|
787 |
// Create discussion.
|
|
|
788 |
$dummytext = 'Here is an inline image: <img src="' . $CFG->wwwroot .
|
|
|
789 |
"/draftfile.php/{$usercontext->id}/user/draft/{$draftidinlineattach}/{$filenameimg}" .
|
|
|
790 |
'" alt="inlineimage">.';
|
|
|
791 |
$options = [
|
|
|
792 |
[
|
|
|
793 |
'name' => 'inlineattachmentsid',
|
|
|
794 |
'value' => $draftidinlineattach
|
|
|
795 |
],
|
|
|
796 |
[
|
|
|
797 |
'name' => 'attachmentsid',
|
|
|
798 |
'value' => $draftidattach
|
|
|
799 |
]
|
|
|
800 |
];
|
|
|
801 |
$discussion = mod_forum_external::add_discussion($forum->id, 'the inline attachment subject', $dummytext,
|
|
|
802 |
-1, $options);
|
|
|
803 |
|
|
|
804 |
$posts = mod_forum_external::get_discussion_posts($discussion['discussionid'], 'modified', 'DESC');
|
|
|
805 |
$posts = external_api::clean_returnvalue(mod_forum_external::get_discussion_posts_returns(), $posts);
|
|
|
806 |
$post = $posts['posts'][0];
|
|
|
807 |
$this->assertCount(0, $post['messageinlinefiles']);
|
|
|
808 |
$this->assertEmpty($post['messageinlinefiles']);
|
|
|
809 |
|
|
|
810 |
$posts = mod_forum_external::get_discussion_posts($discussion['discussionid'], 'modified', 'DESC',
|
|
|
811 |
true);
|
|
|
812 |
$posts = external_api::clean_returnvalue(mod_forum_external::get_discussion_posts_returns(), $posts);
|
|
|
813 |
$post = $posts['posts'][0];
|
|
|
814 |
$this->assertCount(1, $post['messageinlinefiles']);
|
|
|
815 |
$this->assertEquals('fakeimage.png', $post['messageinlinefiles'][0]['filename']);
|
|
|
816 |
}
|
|
|
817 |
|
|
|
818 |
/**
|
|
|
819 |
* Test get forum posts (qanda forum)
|
|
|
820 |
*/
|
11 |
efrain |
821 |
public function test_mod_forum_get_discussion_posts_qanda(): void {
|
1 |
efrain |
822 |
global $CFG, $DB;
|
|
|
823 |
|
|
|
824 |
$this->resetAfterTest(true);
|
|
|
825 |
|
|
|
826 |
$record = new \stdClass();
|
|
|
827 |
$user1 = self::getDataGenerator()->create_user($record);
|
|
|
828 |
$user2 = self::getDataGenerator()->create_user();
|
|
|
829 |
|
|
|
830 |
// Set the first created user to the test user.
|
|
|
831 |
self::setUser($user1);
|
|
|
832 |
|
|
|
833 |
// Create course to add the module.
|
|
|
834 |
$course1 = self::getDataGenerator()->create_course();
|
|
|
835 |
$this->getDataGenerator()->enrol_user($user1->id, $course1->id);
|
|
|
836 |
$this->getDataGenerator()->enrol_user($user2->id, $course1->id);
|
|
|
837 |
|
|
|
838 |
// Forum with tracking off.
|
|
|
839 |
$record = new \stdClass();
|
|
|
840 |
$record->course = $course1->id;
|
|
|
841 |
$record->type = 'qanda';
|
|
|
842 |
$forum1 = self::getDataGenerator()->create_module('forum', $record);
|
|
|
843 |
$forum1context = \context_module::instance($forum1->cmid);
|
|
|
844 |
|
|
|
845 |
// Add discussions to the forums.
|
|
|
846 |
$record = new \stdClass();
|
|
|
847 |
$record->course = $course1->id;
|
|
|
848 |
$record->userid = $user2->id;
|
|
|
849 |
$record->forum = $forum1->id;
|
|
|
850 |
$discussion1 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
|
|
|
851 |
|
|
|
852 |
// Add 1 reply (not the actual user).
|
|
|
853 |
$record = new \stdClass();
|
|
|
854 |
$record->discussion = $discussion1->id;
|
|
|
855 |
$record->parent = $discussion1->firstpost;
|
|
|
856 |
$record->userid = $user2->id;
|
|
|
857 |
$discussion1reply1 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
|
|
|
858 |
|
|
|
859 |
// We still see only the original post.
|
|
|
860 |
$posts = mod_forum_external::get_discussion_posts($discussion1->id, 'modified', 'DESC');
|
|
|
861 |
$posts = external_api::clean_returnvalue(mod_forum_external::get_discussion_posts_returns(), $posts);
|
|
|
862 |
$this->assertEquals(1, count($posts['posts']));
|
|
|
863 |
|
|
|
864 |
// Add a new reply, the user is going to be able to see only the original post and their new post.
|
|
|
865 |
$record = new \stdClass();
|
|
|
866 |
$record->discussion = $discussion1->id;
|
|
|
867 |
$record->parent = $discussion1->firstpost;
|
|
|
868 |
$record->userid = $user1->id;
|
|
|
869 |
$discussion1reply2 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
|
|
|
870 |
|
|
|
871 |
$posts = mod_forum_external::get_discussion_posts($discussion1->id, 'modified', 'DESC');
|
|
|
872 |
$posts = external_api::clean_returnvalue(mod_forum_external::get_discussion_posts_returns(), $posts);
|
|
|
873 |
$this->assertEquals(2, count($posts['posts']));
|
|
|
874 |
|
|
|
875 |
// Now, we can fake the time of the user post, so he can se the rest of the discussion posts.
|
|
|
876 |
$discussion1reply2->created -= $CFG->maxeditingtime * 2;
|
|
|
877 |
$DB->update_record('forum_posts', $discussion1reply2);
|
|
|
878 |
|
|
|
879 |
$posts = mod_forum_external::get_discussion_posts($discussion1->id, 'modified', 'DESC');
|
|
|
880 |
$posts = external_api::clean_returnvalue(mod_forum_external::get_discussion_posts_returns(), $posts);
|
|
|
881 |
$this->assertEquals(3, count($posts['posts']));
|
|
|
882 |
}
|
|
|
883 |
|
|
|
884 |
/**
|
|
|
885 |
* Test get forum discussions
|
|
|
886 |
*/
|
11 |
efrain |
887 |
public function test_mod_forum_get_forum_discussions(): void {
|
1 |
efrain |
888 |
global $CFG, $DB, $PAGE;
|
|
|
889 |
|
|
|
890 |
$this->resetAfterTest(true);
|
|
|
891 |
|
|
|
892 |
// Set the CFG variable to allow track forums.
|
|
|
893 |
$CFG->forum_trackreadposts = true;
|
|
|
894 |
|
|
|
895 |
// Create a user who can track forums.
|
|
|
896 |
$record = new \stdClass();
|
|
|
897 |
$record->trackforums = true;
|
|
|
898 |
$user1 = self::getDataGenerator()->create_user($record);
|
|
|
899 |
// Create a bunch of other users to post.
|
|
|
900 |
$user2 = self::getDataGenerator()->create_user();
|
|
|
901 |
$user3 = self::getDataGenerator()->create_user();
|
|
|
902 |
$user4 = self::getDataGenerator()->create_user();
|
|
|
903 |
|
|
|
904 |
// Set the first created user to the test user.
|
|
|
905 |
self::setUser($user1);
|
|
|
906 |
|
|
|
907 |
// Create courses to add the modules.
|
|
|
908 |
$course1 = self::getDataGenerator()->create_course();
|
|
|
909 |
|
|
|
910 |
// First forum with tracking off.
|
|
|
911 |
$record = new \stdClass();
|
|
|
912 |
$record->course = $course1->id;
|
|
|
913 |
$record->trackingtype = FORUM_TRACKING_OFF;
|
|
|
914 |
$forum1 = self::getDataGenerator()->create_module('forum', $record);
|
|
|
915 |
|
|
|
916 |
// Add discussions to the forums.
|
|
|
917 |
$record = new \stdClass();
|
|
|
918 |
$record->course = $course1->id;
|
|
|
919 |
$record->userid = $user1->id;
|
|
|
920 |
$record->forum = $forum1->id;
|
|
|
921 |
$discussion1 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
|
|
|
922 |
|
|
|
923 |
// Add three replies to the discussion 1 from different users.
|
|
|
924 |
$record = new \stdClass();
|
|
|
925 |
$record->discussion = $discussion1->id;
|
|
|
926 |
$record->parent = $discussion1->firstpost;
|
|
|
927 |
$record->userid = $user2->id;
|
|
|
928 |
$discussion1reply1 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
|
|
|
929 |
|
|
|
930 |
$record->parent = $discussion1reply1->id;
|
|
|
931 |
$record->userid = $user3->id;
|
|
|
932 |
$discussion1reply2 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
|
|
|
933 |
|
|
|
934 |
$record->userid = $user4->id;
|
|
|
935 |
$discussion1reply3 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
|
|
|
936 |
|
|
|
937 |
// Enrol the user in the first course.
|
|
|
938 |
$enrol = enrol_get_plugin('manual');
|
|
|
939 |
|
|
|
940 |
// We don't use the dataGenerator as we need to get the $instance2 to unenrol later.
|
|
|
941 |
$enrolinstances = enrol_get_instances($course1->id, true);
|
|
|
942 |
foreach ($enrolinstances as $courseenrolinstance) {
|
|
|
943 |
if ($courseenrolinstance->enrol == "manual") {
|
|
|
944 |
$instance1 = $courseenrolinstance;
|
|
|
945 |
break;
|
|
|
946 |
}
|
|
|
947 |
}
|
|
|
948 |
$enrol->enrol_user($instance1, $user1->id);
|
|
|
949 |
|
|
|
950 |
// Delete one user.
|
|
|
951 |
delete_user($user4);
|
|
|
952 |
|
|
|
953 |
// Assign capabilities to view discussions for forum 1.
|
|
|
954 |
$cm = get_coursemodule_from_id('forum', $forum1->cmid, 0, false, MUST_EXIST);
|
|
|
955 |
$context = \context_module::instance($cm->id);
|
|
|
956 |
$newrole = create_role('Role 2', 'role2', 'Role 2 description');
|
|
|
957 |
$this->assignUserCapability('mod/forum:viewdiscussion', $context->id, $newrole);
|
|
|
958 |
|
|
|
959 |
// Create what we expect to be returned when querying the forums.
|
|
|
960 |
|
|
|
961 |
$post1 = $DB->get_record('forum_posts', array('id' => $discussion1->firstpost), '*', MUST_EXIST);
|
|
|
962 |
|
|
|
963 |
// User pictures are initially empty, we should get the links once the external function is called.
|
|
|
964 |
$expecteddiscussions = array(
|
|
|
965 |
'id' => $discussion1->firstpost,
|
|
|
966 |
'name' => $discussion1->name,
|
|
|
967 |
'groupid' => (int) $discussion1->groupid,
|
|
|
968 |
'timemodified' => (int) $discussion1reply3->created,
|
|
|
969 |
'usermodified' => (int) $discussion1reply3->userid,
|
|
|
970 |
'timestart' => (int) $discussion1->timestart,
|
|
|
971 |
'timeend' => (int) $discussion1->timeend,
|
|
|
972 |
'discussion' => (int) $discussion1->id,
|
|
|
973 |
'parent' => 0,
|
|
|
974 |
'userid' => (int) $discussion1->userid,
|
|
|
975 |
'created' => (int) $post1->created,
|
|
|
976 |
'modified' => (int) $post1->modified,
|
|
|
977 |
'mailed' => (int) $post1->mailed,
|
|
|
978 |
'subject' => $post1->subject,
|
|
|
979 |
'message' => $post1->message,
|
|
|
980 |
'messageformat' => (int) $post1->messageformat,
|
|
|
981 |
'messagetrust' => (int) $post1->messagetrust,
|
|
|
982 |
'attachment' => $post1->attachment,
|
|
|
983 |
'totalscore' => (int) $post1->totalscore,
|
|
|
984 |
'mailnow' => (int) $post1->mailnow,
|
|
|
985 |
'userfullname' => fullname($user1),
|
|
|
986 |
'usermodifiedfullname' => fullname($user4),
|
|
|
987 |
'userpictureurl' => '',
|
|
|
988 |
'usermodifiedpictureurl' => '',
|
|
|
989 |
'numreplies' => 3,
|
|
|
990 |
'numunread' => 0,
|
|
|
991 |
'pinned' => (bool) FORUM_DISCUSSION_UNPINNED,
|
|
|
992 |
'locked' => false,
|
|
|
993 |
'canreply' => false,
|
|
|
994 |
'canlock' => false,
|
|
|
995 |
'starred' => false,
|
|
|
996 |
'canfavourite' => true
|
|
|
997 |
);
|
|
|
998 |
|
|
|
999 |
// Call the external function passing forum id.
|
|
|
1000 |
$discussions = mod_forum_external::get_forum_discussions($forum1->id);
|
|
|
1001 |
$discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_returns(), $discussions);
|
|
|
1002 |
$expectedreturn = array(
|
|
|
1003 |
'discussions' => array($expecteddiscussions),
|
|
|
1004 |
'warnings' => array()
|
|
|
1005 |
);
|
|
|
1006 |
|
|
|
1007 |
// Wait the theme to be loaded (the external_api call does that) to generate the user profiles.
|
|
|
1008 |
$userpicture = new \user_picture($user1);
|
|
|
1009 |
$userpicture->size = 2; // Size f2.
|
|
|
1010 |
$expectedreturn['discussions'][0]['userpictureurl'] = $userpicture->get_url($PAGE)->out(false);
|
|
|
1011 |
|
|
|
1012 |
$userpicture = new \user_picture($user4);
|
|
|
1013 |
$userpicture->size = 2; // Size f2.
|
|
|
1014 |
$expectedreturn['discussions'][0]['usermodifiedpictureurl'] = $userpicture->get_url($PAGE)->out(false);
|
|
|
1015 |
|
|
|
1016 |
$this->assertEquals($expectedreturn, $discussions);
|
|
|
1017 |
|
|
|
1018 |
// Test the starring functionality return.
|
|
|
1019 |
$t = mod_forum_external::toggle_favourite_state($discussion1->id, 1);
|
|
|
1020 |
$expectedreturn['discussions'][0]['starred'] = true;
|
|
|
1021 |
$discussions = mod_forum_external::get_forum_discussions($forum1->id);
|
|
|
1022 |
$discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_returns(), $discussions);
|
|
|
1023 |
$this->assertEquals($expectedreturn, $discussions);
|
|
|
1024 |
|
|
|
1025 |
// Call without required view discussion capability.
|
|
|
1026 |
$this->unassignUserCapability('mod/forum:viewdiscussion', $context->id, $newrole);
|
|
|
1027 |
try {
|
|
|
1028 |
mod_forum_external::get_forum_discussions($forum1->id);
|
|
|
1029 |
$this->fail('Exception expected due to missing capability.');
|
|
|
1030 |
} catch (\moodle_exception $e) {
|
|
|
1031 |
$this->assertEquals('noviewdiscussionspermission', $e->errorcode);
|
|
|
1032 |
}
|
|
|
1033 |
|
|
|
1034 |
// Unenrol user from second course.
|
|
|
1035 |
$enrol->unenrol_user($instance1, $user1->id);
|
|
|
1036 |
|
|
|
1037 |
// Call for the second course we unenrolled the user from, make sure exception thrown.
|
|
|
1038 |
try {
|
|
|
1039 |
mod_forum_external::get_forum_discussions($forum1->id);
|
|
|
1040 |
$this->fail('Exception expected due to being unenrolled from the course.');
|
|
|
1041 |
} catch (\moodle_exception $e) {
|
|
|
1042 |
$this->assertEquals('requireloginerror', $e->errorcode);
|
|
|
1043 |
}
|
|
|
1044 |
|
|
|
1045 |
$this->setAdminUser();
|
|
|
1046 |
$discussions = mod_forum_external::get_forum_discussions($forum1->id);
|
|
|
1047 |
$discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_returns(), $discussions);
|
|
|
1048 |
$this->assertTrue($discussions['discussions'][0]['canlock']);
|
|
|
1049 |
}
|
|
|
1050 |
|
|
|
1051 |
/**
|
|
|
1052 |
* Test the sorting in get forum discussions
|
|
|
1053 |
*/
|
11 |
efrain |
1054 |
public function test_mod_forum_get_forum_discussions_sorting(): void {
|
1 |
efrain |
1055 |
global $CFG, $DB, $PAGE;
|
|
|
1056 |
|
|
|
1057 |
$this->resetAfterTest(true);
|
|
|
1058 |
|
|
|
1059 |
$clock = $this->mock_clock_with_frozen();
|
|
|
1060 |
|
|
|
1061 |
// Set the CFG variable to allow track forums.
|
|
|
1062 |
$CFG->forum_trackreadposts = true;
|
|
|
1063 |
|
|
|
1064 |
// Create a user who can track forums.
|
|
|
1065 |
$record = new \stdClass();
|
|
|
1066 |
$record->trackforums = true;
|
|
|
1067 |
$user1 = self::getDataGenerator()->create_user($record);
|
|
|
1068 |
// Create a bunch of other users to post.
|
|
|
1069 |
$user2 = self::getDataGenerator()->create_user();
|
|
|
1070 |
$user3 = self::getDataGenerator()->create_user();
|
|
|
1071 |
$user4 = self::getDataGenerator()->create_user();
|
|
|
1072 |
|
|
|
1073 |
// Set the first created user to the test user.
|
|
|
1074 |
self::setUser($user1);
|
|
|
1075 |
|
|
|
1076 |
// Create courses to add the modules.
|
|
|
1077 |
$course1 = self::getDataGenerator()->create_course();
|
|
|
1078 |
|
|
|
1079 |
// Enrol the user in the first course.
|
|
|
1080 |
$enrol = enrol_get_plugin('manual');
|
|
|
1081 |
|
|
|
1082 |
// We don't use the dataGenerator as we need to get the $instance2 to unenrol later.
|
|
|
1083 |
$enrolinstances = enrol_get_instances($course1->id, true);
|
|
|
1084 |
foreach ($enrolinstances as $courseenrolinstance) {
|
|
|
1085 |
if ($courseenrolinstance->enrol == "manual") {
|
|
|
1086 |
$instance1 = $courseenrolinstance;
|
|
|
1087 |
break;
|
|
|
1088 |
}
|
|
|
1089 |
}
|
|
|
1090 |
$enrol->enrol_user($instance1, $user1->id);
|
|
|
1091 |
|
|
|
1092 |
// First forum with tracking off.
|
|
|
1093 |
$record = new \stdClass();
|
|
|
1094 |
$record->course = $course1->id;
|
|
|
1095 |
$record->trackingtype = FORUM_TRACKING_OFF;
|
|
|
1096 |
$forum1 = self::getDataGenerator()->create_module('forum', $record);
|
|
|
1097 |
|
|
|
1098 |
// Assign capabilities to view discussions for forum 1.
|
|
|
1099 |
$cm = get_coursemodule_from_id('forum', $forum1->cmid, 0, false, MUST_EXIST);
|
|
|
1100 |
$context = \context_module::instance($cm->id);
|
|
|
1101 |
$newrole = create_role('Role 2', 'role2', 'Role 2 description');
|
|
|
1102 |
$this->assignUserCapability('mod/forum:viewdiscussion', $context->id, $newrole);
|
|
|
1103 |
|
|
|
1104 |
// Add discussions to the forums.
|
|
|
1105 |
$record = new \stdClass();
|
|
|
1106 |
$record->course = $course1->id;
|
|
|
1107 |
$record->userid = $user1->id;
|
|
|
1108 |
$record->forum = $forum1->id;
|
|
|
1109 |
$discussion1 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
|
|
|
1110 |
$clock->bump();
|
|
|
1111 |
|
|
|
1112 |
// Add three replies to the discussion 1 from different users.
|
|
|
1113 |
$record = new \stdClass();
|
|
|
1114 |
$record->discussion = $discussion1->id;
|
|
|
1115 |
$record->parent = $discussion1->firstpost;
|
|
|
1116 |
$record->userid = $user2->id;
|
|
|
1117 |
$discussion1reply1 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
|
|
|
1118 |
$clock->bump();
|
|
|
1119 |
|
|
|
1120 |
$record->parent = $discussion1reply1->id;
|
|
|
1121 |
$record->userid = $user3->id;
|
|
|
1122 |
$discussion1reply2 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
|
|
|
1123 |
$clock->bump();
|
|
|
1124 |
|
|
|
1125 |
$record->userid = $user4->id;
|
|
|
1126 |
$discussion1reply3 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
|
|
|
1127 |
$clock->bump();
|
|
|
1128 |
|
|
|
1129 |
// Create discussion2.
|
|
|
1130 |
$record2 = new \stdClass();
|
|
|
1131 |
$record2->course = $course1->id;
|
|
|
1132 |
$record2->userid = $user1->id;
|
|
|
1133 |
$record2->forum = $forum1->id;
|
|
|
1134 |
$discussion2 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record2);
|
|
|
1135 |
$clock->bump();
|
|
|
1136 |
|
|
|
1137 |
// Add one reply to the discussion 2.
|
|
|
1138 |
$record2 = new \stdClass();
|
|
|
1139 |
$record2->discussion = $discussion2->id;
|
|
|
1140 |
$record2->parent = $discussion2->firstpost;
|
|
|
1141 |
$record2->userid = $user2->id;
|
|
|
1142 |
$discussion2reply1 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record2);
|
|
|
1143 |
$clock->bump();
|
|
|
1144 |
|
|
|
1145 |
// Create discussion 3.
|
|
|
1146 |
$record3 = new \stdClass();
|
|
|
1147 |
$record3->course = $course1->id;
|
|
|
1148 |
$record3->userid = $user1->id;
|
|
|
1149 |
$record3->forum = $forum1->id;
|
|
|
1150 |
$discussion3 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record3);
|
|
|
1151 |
$clock->bump();
|
|
|
1152 |
|
|
|
1153 |
// Add two replies to the discussion 3.
|
|
|
1154 |
$record3 = new \stdClass();
|
|
|
1155 |
$record3->discussion = $discussion3->id;
|
|
|
1156 |
$record3->parent = $discussion3->firstpost;
|
|
|
1157 |
$record3->userid = $user2->id;
|
|
|
1158 |
$discussion3reply1 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record3);
|
|
|
1159 |
$clock->bump();
|
|
|
1160 |
|
|
|
1161 |
$record3->parent = $discussion3reply1->id;
|
|
|
1162 |
$record3->userid = $user3->id;
|
|
|
1163 |
$discussion3reply2 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record3);
|
|
|
1164 |
|
|
|
1165 |
// Call the external function passing forum id.
|
|
|
1166 |
$discussions = mod_forum_external::get_forum_discussions($forum1->id);
|
|
|
1167 |
$discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_returns(), $discussions);
|
|
|
1168 |
// Discussions should be ordered by last post date in descending order by default.
|
|
|
1169 |
$this->assertEquals($discussions['discussions'][0]['discussion'], $discussion3->id);
|
|
|
1170 |
$this->assertEquals($discussions['discussions'][1]['discussion'], $discussion2->id);
|
|
|
1171 |
$this->assertEquals($discussions['discussions'][2]['discussion'], $discussion1->id);
|
|
|
1172 |
|
|
|
1173 |
$vaultfactory = \mod_forum\local\container::get_vault_factory();
|
|
|
1174 |
$discussionlistvault = $vaultfactory->get_discussions_in_forum_vault();
|
|
|
1175 |
|
|
|
1176 |
// Call the external function passing forum id and sort order parameter.
|
|
|
1177 |
$discussions = mod_forum_external::get_forum_discussions($forum1->id, $discussionlistvault::SORTORDER_LASTPOST_ASC);
|
|
|
1178 |
$discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_returns(), $discussions);
|
|
|
1179 |
// Discussions should be ordered by last post date in ascending order.
|
|
|
1180 |
$this->assertEquals($discussions['discussions'][0]['discussion'], $discussion1->id);
|
|
|
1181 |
$this->assertEquals($discussions['discussions'][1]['discussion'], $discussion2->id);
|
|
|
1182 |
$this->assertEquals($discussions['discussions'][2]['discussion'], $discussion3->id);
|
|
|
1183 |
|
|
|
1184 |
// Call the external function passing forum id and sort order parameter.
|
|
|
1185 |
$discussions = mod_forum_external::get_forum_discussions($forum1->id, $discussionlistvault::SORTORDER_CREATED_DESC);
|
|
|
1186 |
$discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_returns(), $discussions);
|
|
|
1187 |
// Discussions should be ordered by discussion creation date in descending order.
|
|
|
1188 |
$this->assertEquals($discussions['discussions'][0]['discussion'], $discussion3->id);
|
|
|
1189 |
$this->assertEquals($discussions['discussions'][1]['discussion'], $discussion2->id);
|
|
|
1190 |
$this->assertEquals($discussions['discussions'][2]['discussion'], $discussion1->id);
|
|
|
1191 |
|
|
|
1192 |
// Call the external function passing forum id and sort order parameter.
|
|
|
1193 |
$discussions = mod_forum_external::get_forum_discussions($forum1->id, $discussionlistvault::SORTORDER_CREATED_ASC);
|
|
|
1194 |
$discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_returns(), $discussions);
|
|
|
1195 |
// Discussions should be ordered by discussion creation date in ascending order.
|
|
|
1196 |
$this->assertEquals($discussions['discussions'][0]['discussion'], $discussion1->id);
|
|
|
1197 |
$this->assertEquals($discussions['discussions'][1]['discussion'], $discussion2->id);
|
|
|
1198 |
$this->assertEquals($discussions['discussions'][2]['discussion'], $discussion3->id);
|
|
|
1199 |
|
|
|
1200 |
// Call the external function passing forum id and sort order parameter.
|
|
|
1201 |
$discussions = mod_forum_external::get_forum_discussions($forum1->id, $discussionlistvault::SORTORDER_REPLIES_DESC);
|
|
|
1202 |
$discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_returns(), $discussions);
|
|
|
1203 |
// Discussions should be ordered by the number of replies in descending order.
|
|
|
1204 |
$this->assertEquals($discussions['discussions'][0]['discussion'], $discussion1->id);
|
|
|
1205 |
$this->assertEquals($discussions['discussions'][1]['discussion'], $discussion3->id);
|
|
|
1206 |
$this->assertEquals($discussions['discussions'][2]['discussion'], $discussion2->id);
|
|
|
1207 |
|
|
|
1208 |
// Call the external function passing forum id and sort order parameter.
|
|
|
1209 |
$discussions = mod_forum_external::get_forum_discussions($forum1->id, $discussionlistvault::SORTORDER_REPLIES_ASC);
|
|
|
1210 |
$discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_returns(), $discussions);
|
|
|
1211 |
// Discussions should be ordered by the number of replies in ascending order.
|
|
|
1212 |
$this->assertEquals($discussions['discussions'][0]['discussion'], $discussion2->id);
|
|
|
1213 |
$this->assertEquals($discussions['discussions'][1]['discussion'], $discussion3->id);
|
|
|
1214 |
$this->assertEquals($discussions['discussions'][2]['discussion'], $discussion1->id);
|
|
|
1215 |
|
|
|
1216 |
// Pin discussion2.
|
|
|
1217 |
$DB->update_record('forum_discussions',
|
|
|
1218 |
(object) array('id' => $discussion2->id, 'pinned' => FORUM_DISCUSSION_PINNED));
|
|
|
1219 |
|
|
|
1220 |
// Call the external function passing forum id.
|
|
|
1221 |
$discussions = mod_forum_external::get_forum_discussions($forum1->id);
|
|
|
1222 |
$discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_returns(), $discussions);
|
|
|
1223 |
// Discussions should be ordered by last post date in descending order by default.
|
|
|
1224 |
// Pinned discussions should be at the top of the list.
|
|
|
1225 |
$this->assertEquals($discussions['discussions'][0]['discussion'], $discussion2->id);
|
|
|
1226 |
$this->assertEquals($discussions['discussions'][1]['discussion'], $discussion3->id);
|
|
|
1227 |
$this->assertEquals($discussions['discussions'][2]['discussion'], $discussion1->id);
|
|
|
1228 |
|
|
|
1229 |
// Call the external function passing forum id and sort order parameter.
|
|
|
1230 |
$discussions = mod_forum_external::get_forum_discussions($forum1->id, $discussionlistvault::SORTORDER_LASTPOST_ASC);
|
|
|
1231 |
$discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_returns(), $discussions);
|
|
|
1232 |
// Discussions should be ordered by last post date in ascending order.
|
|
|
1233 |
// Pinned discussions should be at the top of the list.
|
|
|
1234 |
$this->assertEquals($discussions['discussions'][0]['discussion'], $discussion2->id);
|
|
|
1235 |
$this->assertEquals($discussions['discussions'][1]['discussion'], $discussion1->id);
|
|
|
1236 |
$this->assertEquals($discussions['discussions'][2]['discussion'], $discussion3->id);
|
|
|
1237 |
}
|
|
|
1238 |
|
|
|
1239 |
/**
|
|
|
1240 |
* Test add_discussion_post
|
|
|
1241 |
*/
|
11 |
efrain |
1242 |
public function test_add_discussion_post(): void {
|
1 |
efrain |
1243 |
global $CFG;
|
|
|
1244 |
|
|
|
1245 |
$this->resetAfterTest(true);
|
|
|
1246 |
|
|
|
1247 |
$user = self::getDataGenerator()->create_user();
|
|
|
1248 |
$otheruser = self::getDataGenerator()->create_user();
|
|
|
1249 |
|
|
|
1250 |
self::setAdminUser();
|
|
|
1251 |
|
|
|
1252 |
// Create course to add the module.
|
|
|
1253 |
$course = self::getDataGenerator()->create_course(array('groupmode' => VISIBLEGROUPS, 'groupmodeforce' => 0));
|
|
|
1254 |
|
|
|
1255 |
// Forum with tracking off.
|
|
|
1256 |
$record = new \stdClass();
|
|
|
1257 |
$record->course = $course->id;
|
|
|
1258 |
$forum = self::getDataGenerator()->create_module('forum', $record);
|
|
|
1259 |
$cm = get_coursemodule_from_id('forum', $forum->cmid, 0, false, MUST_EXIST);
|
|
|
1260 |
$forumcontext = \context_module::instance($forum->cmid);
|
|
|
1261 |
|
|
|
1262 |
// Add discussions to the forums.
|
|
|
1263 |
$record = new \stdClass();
|
|
|
1264 |
$record->course = $course->id;
|
|
|
1265 |
$record->userid = $user->id;
|
|
|
1266 |
$record->forum = $forum->id;
|
|
|
1267 |
$discussion = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
|
|
|
1268 |
|
|
|
1269 |
// Try to post (user not enrolled).
|
|
|
1270 |
self::setUser($user);
|
|
|
1271 |
try {
|
|
|
1272 |
mod_forum_external::add_discussion_post($discussion->firstpost, 'some subject', 'some text here...');
|
|
|
1273 |
$this->fail('Exception expected due to being unenrolled from the course.');
|
|
|
1274 |
} catch (\moodle_exception $e) {
|
|
|
1275 |
$this->assertEquals('requireloginerror', $e->errorcode);
|
|
|
1276 |
}
|
|
|
1277 |
|
|
|
1278 |
$this->getDataGenerator()->enrol_user($user->id, $course->id);
|
|
|
1279 |
$this->getDataGenerator()->enrol_user($otheruser->id, $course->id);
|
|
|
1280 |
|
|
|
1281 |
$createdpost = mod_forum_external::add_discussion_post($discussion->firstpost, 'some subject', 'some text here...');
|
|
|
1282 |
$createdpost = external_api::clean_returnvalue(mod_forum_external::add_discussion_post_returns(), $createdpost);
|
|
|
1283 |
|
|
|
1284 |
$posts = mod_forum_external::get_discussion_posts($discussion->id, 'modified', 'ASC');
|
|
|
1285 |
$posts = external_api::clean_returnvalue(mod_forum_external::get_discussion_posts_returns(), $posts);
|
|
|
1286 |
// We receive the discussion and the post.
|
|
|
1287 |
$this->assertEquals(2, count($posts['posts']));
|
|
|
1288 |
|
|
|
1289 |
$tested = false;
|
|
|
1290 |
foreach ($posts['posts'] as $thispost) {
|
|
|
1291 |
if ($createdpost['postid'] == $thispost['id']) {
|
|
|
1292 |
$this->assertEquals('some subject', $thispost['subject']);
|
|
|
1293 |
$this->assertEquals('some text here...', $thispost['message']);
|
|
|
1294 |
$this->assertEquals(FORMAT_HTML, $thispost['messageformat']); // This is the default if format was not specified.
|
|
|
1295 |
$tested = true;
|
|
|
1296 |
}
|
|
|
1297 |
}
|
|
|
1298 |
$this->assertTrue($tested);
|
|
|
1299 |
|
|
|
1300 |
// Let's simulate a call with any other format, it should be stored that way.
|
|
|
1301 |
global $DB; // Yes, we are going to use DB facilities too, because cannot rely on other functions for checking
|
|
|
1302 |
// the format. They eat it completely (going back to FORMAT_HTML. So we only can trust DB for further
|
|
|
1303 |
// processing.
|
|
|
1304 |
$formats = [FORMAT_PLAIN, FORMAT_MOODLE, FORMAT_MARKDOWN, FORMAT_HTML];
|
|
|
1305 |
$options = [];
|
|
|
1306 |
foreach ($formats as $format) {
|
|
|
1307 |
$createdpost = mod_forum_external::add_discussion_post($discussion->firstpost,
|
|
|
1308 |
'with some format', 'some formatted here...', $options, $format);
|
|
|
1309 |
$createdpost = external_api::clean_returnvalue(mod_forum_external::add_discussion_post_returns(), $createdpost);
|
|
|
1310 |
$dbformat = $DB->get_field('forum_posts', 'messageformat', ['id' => $createdpost['postid']]);
|
|
|
1311 |
$this->assertEquals($format, $dbformat);
|
|
|
1312 |
}
|
|
|
1313 |
|
|
|
1314 |
// Now let's try the 'topreferredformat' option. That should end with the content
|
|
|
1315 |
// transformed and the format being FORMAT_HTML (when, like in this case, user preferred
|
|
|
1316 |
// format is HTML, inferred from editor in preferences).
|
|
|
1317 |
$options = [['name' => 'topreferredformat', 'value' => true]];
|
|
|
1318 |
$createdpost = mod_forum_external::add_discussion_post($discussion->firstpost,
|
|
|
1319 |
'interesting subject', 'with some https://example.com link', $options, FORMAT_MOODLE);
|
|
|
1320 |
$createdpost = external_api::clean_returnvalue(mod_forum_external::add_discussion_post_returns(), $createdpost);
|
|
|
1321 |
$dbpost = $DB->get_record('forum_posts', ['id' => $createdpost['postid']]);
|
|
|
1322 |
// Format HTML and content converted, we should get.
|
|
|
1323 |
$this->assertEquals(FORMAT_HTML, $dbpost->messageformat);
|
|
|
1324 |
$this->assertEquals('<div class="text_to_html">with some https://example.com link</div>', $dbpost->message);
|
|
|
1325 |
|
|
|
1326 |
// Test inline and regular attachment in post
|
|
|
1327 |
// Create a file in a draft area for inline attachments.
|
|
|
1328 |
$draftidinlineattach = file_get_unused_draft_itemid();
|
|
|
1329 |
$draftidattach = file_get_unused_draft_itemid();
|
|
|
1330 |
self::setUser($user);
|
|
|
1331 |
$usercontext = \context_user::instance($user->id);
|
|
|
1332 |
$filepath = '/';
|
|
|
1333 |
$filearea = 'draft';
|
|
|
1334 |
$component = 'user';
|
|
|
1335 |
$filenameimg = 'shouldbeanimage.txt';
|
|
|
1336 |
$filerecordinline = array(
|
|
|
1337 |
'contextid' => $usercontext->id,
|
|
|
1338 |
'component' => $component,
|
|
|
1339 |
'filearea' => $filearea,
|
|
|
1340 |
'itemid' => $draftidinlineattach,
|
|
|
1341 |
'filepath' => $filepath,
|
|
|
1342 |
'filename' => $filenameimg,
|
|
|
1343 |
);
|
|
|
1344 |
$fs = get_file_storage();
|
|
|
1345 |
|
|
|
1346 |
// Create a file in a draft area for regular attachments.
|
|
|
1347 |
$filerecordattach = $filerecordinline;
|
|
|
1348 |
$attachfilename = 'attachment.txt';
|
|
|
1349 |
$filerecordattach['filename'] = $attachfilename;
|
|
|
1350 |
$filerecordattach['itemid'] = $draftidattach;
|
|
|
1351 |
$fs->create_file_from_string($filerecordinline, 'image contents (not really)');
|
|
|
1352 |
$fs->create_file_from_string($filerecordattach, 'simple text attachment');
|
|
|
1353 |
|
|
|
1354 |
$options = array(array('name' => 'inlineattachmentsid', 'value' => $draftidinlineattach),
|
|
|
1355 |
array('name' => 'attachmentsid', 'value' => $draftidattach));
|
|
|
1356 |
$dummytext = 'Here is an inline image: <img src="' . $CFG->wwwroot
|
|
|
1357 |
. "/draftfile.php/{$usercontext->id}/user/draft/{$draftidinlineattach}/{$filenameimg}"
|
|
|
1358 |
. '" alt="inlineimage">.';
|
|
|
1359 |
$createdpost = mod_forum_external::add_discussion_post($discussion->firstpost, 'new post inline attachment',
|
|
|
1360 |
$dummytext, $options);
|
|
|
1361 |
$createdpost = external_api::clean_returnvalue(mod_forum_external::add_discussion_post_returns(), $createdpost);
|
|
|
1362 |
|
|
|
1363 |
$posts = mod_forum_external::get_discussion_posts($discussion->id, 'modified', 'ASC');
|
|
|
1364 |
$posts = external_api::clean_returnvalue(mod_forum_external::get_discussion_posts_returns(), $posts);
|
|
|
1365 |
// We receive the discussion and the post.
|
|
|
1366 |
// Can't guarantee order of posts during tests.
|
|
|
1367 |
$postfound = false;
|
|
|
1368 |
foreach ($posts['posts'] as $thispost) {
|
|
|
1369 |
if ($createdpost['postid'] == $thispost['id']) {
|
|
|
1370 |
$this->assertEquals($createdpost['postid'], $thispost['id']);
|
|
|
1371 |
$this->assertCount(1, $thispost['attachments']);
|
|
|
1372 |
$this->assertEquals('attachment.txt', $thispost['attachments'][0]['filename']);
|
|
|
1373 |
$this->assertEquals($thispost['attachments'][0]['filename'], $attachfilename, "There should be 1 attachment");
|
|
|
1374 |
$this->assertStringContainsString('pluginfile.php', $thispost['message']);
|
|
|
1375 |
$postfound = true;
|
|
|
1376 |
break;
|
|
|
1377 |
}
|
|
|
1378 |
}
|
|
|
1379 |
|
|
|
1380 |
$this->assertTrue($postfound);
|
|
|
1381 |
|
|
|
1382 |
// Check not posting in groups the user is not member of.
|
|
|
1383 |
$group = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
|
|
|
1384 |
groups_add_member($group->id, $otheruser->id);
|
|
|
1385 |
|
|
|
1386 |
$forum = self::getDataGenerator()->create_module('forum', $record, array('groupmode' => SEPARATEGROUPS));
|
|
|
1387 |
$record->forum = $forum->id;
|
|
|
1388 |
$record->userid = $otheruser->id;
|
|
|
1389 |
$record->groupid = $group->id;
|
|
|
1390 |
$discussion = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
|
|
|
1391 |
|
|
|
1392 |
try {
|
|
|
1393 |
mod_forum_external::add_discussion_post($discussion->firstpost, 'some subject', 'some text here...');
|
|
|
1394 |
$this->fail('Exception expected due to invalid permissions for posting.');
|
|
|
1395 |
} catch (\moodle_exception $e) {
|
|
|
1396 |
$this->assertEquals('nopostforum', $e->errorcode);
|
|
|
1397 |
}
|
|
|
1398 |
}
|
|
|
1399 |
|
|
|
1400 |
/**
|
|
|
1401 |
* Test add_discussion_post and auto subscription to a discussion.
|
|
|
1402 |
*/
|
11 |
efrain |
1403 |
public function test_add_discussion_post_subscribe_discussion(): void {
|
1 |
efrain |
1404 |
global $USER;
|
|
|
1405 |
|
|
|
1406 |
$this->resetAfterTest(true);
|
|
|
1407 |
|
|
|
1408 |
self::setAdminUser();
|
|
|
1409 |
|
|
|
1410 |
$user = self::getDataGenerator()->create_user();
|
|
|
1411 |
$admin = get_admin();
|
|
|
1412 |
// Create course to add the module.
|
|
|
1413 |
$course = self::getDataGenerator()->create_course(array('groupmode' => VISIBLEGROUPS, 'groupmodeforce' => 0));
|
|
|
1414 |
|
|
|
1415 |
$this->getDataGenerator()->enrol_user($user->id, $course->id);
|
|
|
1416 |
|
|
|
1417 |
// Forum with tracking off.
|
|
|
1418 |
$record = new \stdClass();
|
|
|
1419 |
$record->course = $course->id;
|
|
|
1420 |
$forum = self::getDataGenerator()->create_module('forum', $record);
|
|
|
1421 |
$cm = get_coursemodule_from_id('forum', $forum->cmid, 0, false, MUST_EXIST);
|
|
|
1422 |
|
|
|
1423 |
// Add discussions to the forums.
|
|
|
1424 |
$record = new \stdClass();
|
|
|
1425 |
$record->course = $course->id;
|
|
|
1426 |
$record->userid = $admin->id;
|
|
|
1427 |
$record->forum = $forum->id;
|
|
|
1428 |
$discussion1 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
|
|
|
1429 |
$discussion2 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
|
|
|
1430 |
|
|
|
1431 |
// Try to post as user.
|
|
|
1432 |
self::setUser($user);
|
|
|
1433 |
// Enable auto subscribe discussion.
|
|
|
1434 |
$USER->autosubscribe = true;
|
|
|
1435 |
// Add a discussion post in a forum discussion where the user is not subscribed (auto-subscribe preference enabled).
|
|
|
1436 |
mod_forum_external::add_discussion_post($discussion1->firstpost, 'some subject', 'some text here...');
|
|
|
1437 |
|
|
|
1438 |
$posts = mod_forum_external::get_discussion_posts($discussion1->id, 'modified', 'ASC');
|
|
|
1439 |
$posts = external_api::clean_returnvalue(mod_forum_external::get_discussion_posts_returns(), $posts);
|
|
|
1440 |
// We receive the discussion and the post.
|
|
|
1441 |
$this->assertEquals(2, count($posts['posts']));
|
|
|
1442 |
// The user should be subscribed to the discussion after adding a discussion post.
|
|
|
1443 |
$this->assertTrue(\mod_forum\subscriptions::is_subscribed($user->id, $forum, $discussion1->id, $cm));
|
|
|
1444 |
|
|
|
1445 |
// Disable auto subscribe discussion.
|
|
|
1446 |
$USER->autosubscribe = false;
|
|
|
1447 |
$this->assertTrue(\mod_forum\subscriptions::is_subscribed($user->id, $forum, $discussion1->id, $cm));
|
|
|
1448 |
// Add a discussion post in a forum discussion where the user is subscribed (auto-subscribe preference disabled).
|
|
|
1449 |
mod_forum_external::add_discussion_post($discussion1->firstpost, 'some subject 1', 'some text here 1...');
|
|
|
1450 |
|
|
|
1451 |
$posts = mod_forum_external::get_discussion_posts($discussion1->id, 'modified', 'ASC');
|
|
|
1452 |
$posts = external_api::clean_returnvalue(mod_forum_external::get_discussion_posts_returns(), $posts);
|
|
|
1453 |
// We receive the discussion and the post.
|
|
|
1454 |
$this->assertEquals(3, count($posts['posts']));
|
|
|
1455 |
// The user should still be subscribed to the discussion after adding a discussion post.
|
|
|
1456 |
$this->assertTrue(\mod_forum\subscriptions::is_subscribed($user->id, $forum, $discussion1->id, $cm));
|
|
|
1457 |
|
|
|
1458 |
$this->assertFalse(\mod_forum\subscriptions::is_subscribed($user->id, $forum, $discussion2->id, $cm));
|
|
|
1459 |
// Add a discussion post in a forum discussion where the user is not subscribed (auto-subscribe preference disabled).
|
|
|
1460 |
mod_forum_external::add_discussion_post($discussion2->firstpost, 'some subject 2', 'some text here 2...');
|
|
|
1461 |
|
|
|
1462 |
$posts = mod_forum_external::get_discussion_posts($discussion2->id, 'modified', 'ASC');
|
|
|
1463 |
$posts = external_api::clean_returnvalue(mod_forum_external::get_discussion_posts_returns(), $posts);
|
|
|
1464 |
// We receive the discussion and the post.
|
|
|
1465 |
$this->assertEquals(2, count($posts['posts']));
|
|
|
1466 |
// The user should still not be subscribed to the discussion after adding a discussion post.
|
|
|
1467 |
$this->assertFalse(\mod_forum\subscriptions::is_subscribed($user->id, $forum, $discussion2->id, $cm));
|
|
|
1468 |
|
|
|
1469 |
// Passing a value for the discussionsubscribe option parameter.
|
|
|
1470 |
$this->assertFalse(\mod_forum\subscriptions::is_subscribed($user->id, $forum, $discussion2->id, $cm));
|
|
|
1471 |
// Add a discussion post in a forum discussion where the user is not subscribed (auto-subscribe preference disabled),
|
|
|
1472 |
// and the option parameter 'discussionsubscribe' => true in the webservice.
|
|
|
1473 |
$option = array('name' => 'discussionsubscribe', 'value' => true);
|
|
|
1474 |
$options[] = $option;
|
|
|
1475 |
mod_forum_external::add_discussion_post($discussion2->firstpost, 'some subject 2', 'some text here 2...',
|
|
|
1476 |
$options);
|
|
|
1477 |
|
|
|
1478 |
$posts = mod_forum_external::get_discussion_posts($discussion2->id, 'modified', 'ASC');
|
|
|
1479 |
$posts = external_api::clean_returnvalue(mod_forum_external::get_discussion_posts_returns(), $posts);
|
|
|
1480 |
// We receive the discussion and the post.
|
|
|
1481 |
$this->assertEquals(3, count($posts['posts']));
|
|
|
1482 |
// The user should now be subscribed to the discussion after adding a discussion post.
|
|
|
1483 |
$this->assertTrue(\mod_forum\subscriptions::is_subscribed($user->id, $forum, $discussion2->id, $cm));
|
|
|
1484 |
}
|
|
|
1485 |
|
|
|
1486 |
/*
|
|
|
1487 |
* Test add_discussion. A basic test since all the API functions are already covered by unit tests.
|
|
|
1488 |
*/
|
11 |
efrain |
1489 |
public function test_add_discussion(): void {
|
1 |
efrain |
1490 |
global $CFG, $USER;
|
|
|
1491 |
$this->resetAfterTest(true);
|
|
|
1492 |
|
|
|
1493 |
// Create courses to add the modules.
|
|
|
1494 |
$course = self::getDataGenerator()->create_course();
|
|
|
1495 |
|
|
|
1496 |
$user1 = self::getDataGenerator()->create_user();
|
|
|
1497 |
$user2 = self::getDataGenerator()->create_user();
|
|
|
1498 |
|
|
|
1499 |
// First forum with tracking off.
|
|
|
1500 |
$record = new \stdClass();
|
|
|
1501 |
$record->course = $course->id;
|
|
|
1502 |
$record->type = 'news';
|
|
|
1503 |
$forum = self::getDataGenerator()->create_module('forum', $record);
|
|
|
1504 |
|
|
|
1505 |
self::setUser($user1);
|
|
|
1506 |
$this->getDataGenerator()->enrol_user($user1->id, $course->id);
|
|
|
1507 |
|
|
|
1508 |
try {
|
|
|
1509 |
mod_forum_external::add_discussion($forum->id, 'the subject', 'some text here...');
|
|
|
1510 |
$this->fail('Exception expected due to invalid permissions.');
|
|
|
1511 |
} catch (\moodle_exception $e) {
|
|
|
1512 |
$this->assertEquals('cannotcreatediscussion', $e->errorcode);
|
|
|
1513 |
}
|
|
|
1514 |
|
|
|
1515 |
self::setAdminUser();
|
|
|
1516 |
$createddiscussion = mod_forum_external::add_discussion($forum->id, 'the subject', 'some text here...');
|
|
|
1517 |
$createddiscussion = external_api::clean_returnvalue(mod_forum_external::add_discussion_returns(), $createddiscussion);
|
|
|
1518 |
|
|
|
1519 |
$discussions = mod_forum_external::get_forum_discussions($forum->id);
|
|
|
1520 |
$discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_returns(), $discussions);
|
|
|
1521 |
|
|
|
1522 |
$this->assertCount(1, $discussions['discussions']);
|
|
|
1523 |
$this->assertCount(0, $discussions['warnings']);
|
|
|
1524 |
|
|
|
1525 |
$this->assertEquals($createddiscussion['discussionid'], $discussions['discussions'][0]['discussion']);
|
|
|
1526 |
$this->assertEquals(-1, $discussions['discussions'][0]['groupid']);
|
|
|
1527 |
$this->assertEquals('the subject', $discussions['discussions'][0]['subject']);
|
|
|
1528 |
$this->assertEquals('some text here...', $discussions['discussions'][0]['message']);
|
|
|
1529 |
|
|
|
1530 |
$discussion2pinned = mod_forum_external::add_discussion($forum->id, 'the pinned subject', 'some 2 text here...', -1,
|
|
|
1531 |
array('options' => array('name' => 'discussionpinned',
|
|
|
1532 |
'value' => true)));
|
|
|
1533 |
$discussion3 = mod_forum_external::add_discussion($forum->id, 'the non pinnedsubject', 'some 3 text here...');
|
|
|
1534 |
$discussions = mod_forum_external::get_forum_discussions($forum->id);
|
|
|
1535 |
$discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_returns(), $discussions);
|
|
|
1536 |
$this->assertCount(3, $discussions['discussions']);
|
|
|
1537 |
$this->assertEquals($discussion2pinned['discussionid'], $discussions['discussions'][0]['discussion']);
|
|
|
1538 |
|
|
|
1539 |
// Test inline and regular attachment in new discussion
|
|
|
1540 |
// Create a file in a draft area for inline attachments.
|
|
|
1541 |
|
|
|
1542 |
$fs = get_file_storage();
|
|
|
1543 |
|
|
|
1544 |
$draftidinlineattach = file_get_unused_draft_itemid();
|
|
|
1545 |
$draftidattach = file_get_unused_draft_itemid();
|
|
|
1546 |
|
|
|
1547 |
$usercontext = \context_user::instance($USER->id);
|
|
|
1548 |
$filepath = '/';
|
|
|
1549 |
$filearea = 'draft';
|
|
|
1550 |
$component = 'user';
|
|
|
1551 |
$filenameimg = 'shouldbeanimage.txt';
|
|
|
1552 |
$filerecord = array(
|
|
|
1553 |
'contextid' => $usercontext->id,
|
|
|
1554 |
'component' => $component,
|
|
|
1555 |
'filearea' => $filearea,
|
|
|
1556 |
'itemid' => $draftidinlineattach,
|
|
|
1557 |
'filepath' => $filepath,
|
|
|
1558 |
'filename' => $filenameimg,
|
|
|
1559 |
);
|
|
|
1560 |
|
|
|
1561 |
// Create a file in a draft area for regular attachments.
|
|
|
1562 |
$filerecordattach = $filerecord;
|
|
|
1563 |
$attachfilename = 'attachment.txt';
|
|
|
1564 |
$filerecordattach['filename'] = $attachfilename;
|
|
|
1565 |
$filerecordattach['itemid'] = $draftidattach;
|
|
|
1566 |
$fs->create_file_from_string($filerecord, 'image contents (not really)');
|
|
|
1567 |
$fs->create_file_from_string($filerecordattach, 'simple text attachment');
|
|
|
1568 |
|
|
|
1569 |
$dummytext = 'Here is an inline image: <img src="' . $CFG->wwwroot .
|
|
|
1570 |
"/draftfile.php/{$usercontext->id}/user/draft/{$draftidinlineattach}/{$filenameimg}" .
|
|
|
1571 |
'" alt="inlineimage">.';
|
|
|
1572 |
|
|
|
1573 |
$options = array(array('name' => 'inlineattachmentsid', 'value' => $draftidinlineattach),
|
|
|
1574 |
array('name' => 'attachmentsid', 'value' => $draftidattach));
|
|
|
1575 |
$createddiscussion = mod_forum_external::add_discussion($forum->id, 'the inline attachment subject',
|
|
|
1576 |
$dummytext, -1, $options);
|
|
|
1577 |
$createddiscussion = external_api::clean_returnvalue(mod_forum_external::add_discussion_returns(), $createddiscussion);
|
|
|
1578 |
|
|
|
1579 |
$discussions = mod_forum_external::get_forum_discussions($forum->id);
|
|
|
1580 |
$discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_returns(), $discussions);
|
|
|
1581 |
|
|
|
1582 |
$this->assertCount(4, $discussions['discussions']);
|
|
|
1583 |
$this->assertCount(0, $createddiscussion['warnings']);
|
|
|
1584 |
// Can't guarantee order of posts during tests.
|
|
|
1585 |
$postfound = false;
|
|
|
1586 |
foreach ($discussions['discussions'] as $thisdiscussion) {
|
|
|
1587 |
if ($createddiscussion['discussionid'] == $thisdiscussion['discussion']) {
|
|
|
1588 |
$this->assertEquals($thisdiscussion['attachment'], 1, "There should be a non-inline attachment");
|
|
|
1589 |
$this->assertCount(1, $thisdiscussion['attachments'], "There should be 1 attachment");
|
|
|
1590 |
$this->assertEquals($thisdiscussion['attachments'][0]['filename'], $attachfilename, "There should be 1 attachment");
|
|
|
1591 |
$this->assertStringNotContainsString('draftfile.php', $thisdiscussion['message']);
|
|
|
1592 |
$this->assertStringContainsString('pluginfile.php', $thisdiscussion['message']);
|
|
|
1593 |
$postfound = true;
|
|
|
1594 |
break;
|
|
|
1595 |
}
|
|
|
1596 |
}
|
|
|
1597 |
|
|
|
1598 |
$this->assertTrue($postfound);
|
|
|
1599 |
}
|
|
|
1600 |
|
|
|
1601 |
/**
|
|
|
1602 |
* Test adding discussions in a course with gorups
|
|
|
1603 |
*/
|
11 |
efrain |
1604 |
public function test_add_discussion_in_course_with_groups(): void {
|
1 |
efrain |
1605 |
global $CFG;
|
|
|
1606 |
|
|
|
1607 |
$this->resetAfterTest(true);
|
|
|
1608 |
|
|
|
1609 |
// Create course to add the module.
|
|
|
1610 |
$course = self::getDataGenerator()->create_course(array('groupmode' => VISIBLEGROUPS, 'groupmodeforce' => 0));
|
|
|
1611 |
$user = self::getDataGenerator()->create_user();
|
|
|
1612 |
$this->getDataGenerator()->enrol_user($user->id, $course->id);
|
|
|
1613 |
|
|
|
1614 |
// Forum forcing separate gropus.
|
|
|
1615 |
$record = new \stdClass();
|
|
|
1616 |
$record->course = $course->id;
|
|
|
1617 |
$forum = self::getDataGenerator()->create_module('forum', $record, array('groupmode' => SEPARATEGROUPS));
|
|
|
1618 |
|
|
|
1619 |
// Try to post (user not enrolled).
|
|
|
1620 |
self::setUser($user);
|
|
|
1621 |
|
|
|
1622 |
// The user is not enroled in any group, try to post in a forum with separate groups.
|
|
|
1623 |
try {
|
|
|
1624 |
mod_forum_external::add_discussion($forum->id, 'the subject', 'some text here...');
|
|
|
1625 |
$this->fail('Exception expected due to invalid group permissions.');
|
|
|
1626 |
} catch (\moodle_exception $e) {
|
|
|
1627 |
$this->assertEquals('cannotcreatediscussion', $e->errorcode);
|
|
|
1628 |
}
|
|
|
1629 |
|
|
|
1630 |
try {
|
|
|
1631 |
mod_forum_external::add_discussion($forum->id, 'the subject', 'some text here...', 0);
|
|
|
1632 |
$this->fail('Exception expected due to invalid group permissions.');
|
|
|
1633 |
} catch (\moodle_exception $e) {
|
|
|
1634 |
$this->assertEquals('cannotcreatediscussion', $e->errorcode);
|
|
|
1635 |
}
|
|
|
1636 |
|
|
|
1637 |
// Create a group.
|
|
|
1638 |
$group = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
|
|
|
1639 |
|
|
|
1640 |
// Try to post in a group the user is not enrolled.
|
|
|
1641 |
try {
|
|
|
1642 |
mod_forum_external::add_discussion($forum->id, 'the subject', 'some text here...', $group->id);
|
|
|
1643 |
$this->fail('Exception expected due to invalid group permissions.');
|
|
|
1644 |
} catch (\moodle_exception $e) {
|
|
|
1645 |
$this->assertEquals('cannotcreatediscussion', $e->errorcode);
|
|
|
1646 |
}
|
|
|
1647 |
|
|
|
1648 |
// Add the user to a group.
|
|
|
1649 |
groups_add_member($group->id, $user->id);
|
|
|
1650 |
|
|
|
1651 |
// Try to post in a group the user is not enrolled.
|
|
|
1652 |
try {
|
|
|
1653 |
mod_forum_external::add_discussion($forum->id, 'the subject', 'some text here...', $group->id + 1);
|
|
|
1654 |
$this->fail('Exception expected due to invalid group.');
|
|
|
1655 |
} catch (\moodle_exception $e) {
|
|
|
1656 |
$this->assertEquals('cannotcreatediscussion', $e->errorcode);
|
|
|
1657 |
}
|
|
|
1658 |
|
|
|
1659 |
// Nost add the discussion using a valid group.
|
|
|
1660 |
$discussion = mod_forum_external::add_discussion($forum->id, 'the subject', 'some text here...', $group->id);
|
|
|
1661 |
$discussion = external_api::clean_returnvalue(mod_forum_external::add_discussion_returns(), $discussion);
|
|
|
1662 |
|
|
|
1663 |
$discussions = mod_forum_external::get_forum_discussions($forum->id);
|
|
|
1664 |
$discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_returns(), $discussions);
|
|
|
1665 |
|
|
|
1666 |
$this->assertCount(1, $discussions['discussions']);
|
|
|
1667 |
$this->assertCount(0, $discussions['warnings']);
|
|
|
1668 |
$this->assertEquals($discussion['discussionid'], $discussions['discussions'][0]['discussion']);
|
|
|
1669 |
$this->assertEquals($group->id, $discussions['discussions'][0]['groupid']);
|
|
|
1670 |
|
|
|
1671 |
// Now add a discussions without indicating a group. The function should guess the correct group.
|
|
|
1672 |
$discussion = mod_forum_external::add_discussion($forum->id, 'the subject', 'some text here...');
|
|
|
1673 |
$discussion = external_api::clean_returnvalue(mod_forum_external::add_discussion_returns(), $discussion);
|
|
|
1674 |
|
|
|
1675 |
$discussions = mod_forum_external::get_forum_discussions($forum->id);
|
|
|
1676 |
$discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_returns(), $discussions);
|
|
|
1677 |
|
|
|
1678 |
$this->assertCount(2, $discussions['discussions']);
|
|
|
1679 |
$this->assertCount(0, $discussions['warnings']);
|
|
|
1680 |
$this->assertEquals($group->id, $discussions['discussions'][0]['groupid']);
|
|
|
1681 |
$this->assertEquals($group->id, $discussions['discussions'][1]['groupid']);
|
|
|
1682 |
|
|
|
1683 |
// Enrol the same user in other group.
|
|
|
1684 |
$group2 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
|
|
|
1685 |
groups_add_member($group2->id, $user->id);
|
|
|
1686 |
|
|
|
1687 |
// Now add a discussions without indicating a group. The function should guess the correct group (the first one).
|
|
|
1688 |
$discussion = mod_forum_external::add_discussion($forum->id, 'the subject', 'some text here...');
|
|
|
1689 |
$discussion = external_api::clean_returnvalue(mod_forum_external::add_discussion_returns(), $discussion);
|
|
|
1690 |
|
|
|
1691 |
$discussions = mod_forum_external::get_forum_discussions($forum->id);
|
|
|
1692 |
$discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_returns(), $discussions);
|
|
|
1693 |
|
|
|
1694 |
$this->assertCount(3, $discussions['discussions']);
|
|
|
1695 |
$this->assertCount(0, $discussions['warnings']);
|
|
|
1696 |
$this->assertEquals($group->id, $discussions['discussions'][0]['groupid']);
|
|
|
1697 |
$this->assertEquals($group->id, $discussions['discussions'][1]['groupid']);
|
|
|
1698 |
$this->assertEquals($group->id, $discussions['discussions'][2]['groupid']);
|
|
|
1699 |
|
|
|
1700 |
}
|
|
|
1701 |
|
|
|
1702 |
/*
|
|
|
1703 |
* Test set_lock_state.
|
|
|
1704 |
*
|
|
|
1705 |
* @covers \mod_forum\event\discussion_lock_updated
|
|
|
1706 |
*/
|
11 |
efrain |
1707 |
public function test_set_lock_state(): void {
|
1 |
efrain |
1708 |
global $DB;
|
|
|
1709 |
$this->resetAfterTest(true);
|
|
|
1710 |
|
|
|
1711 |
// Create courses to add the modules.
|
|
|
1712 |
$course = self::getDataGenerator()->create_course();
|
|
|
1713 |
$user = self::getDataGenerator()->create_user();
|
|
|
1714 |
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
|
|
|
1715 |
|
|
|
1716 |
// First forum with tracking off.
|
|
|
1717 |
$record = new \stdClass();
|
|
|
1718 |
$record->course = $course->id;
|
|
|
1719 |
$record->type = 'news';
|
|
|
1720 |
$forum = self::getDataGenerator()->create_module('forum', $record);
|
|
|
1721 |
$context = \context_module::instance($forum->cmid);
|
|
|
1722 |
|
|
|
1723 |
$record = new \stdClass();
|
|
|
1724 |
$record->course = $course->id;
|
|
|
1725 |
$record->userid = $user->id;
|
|
|
1726 |
$record->forum = $forum->id;
|
|
|
1727 |
$discussion = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
|
|
|
1728 |
|
|
|
1729 |
// User who is a student.
|
|
|
1730 |
self::setUser($user);
|
|
|
1731 |
$this->getDataGenerator()->enrol_user($user->id, $course->id, $studentrole->id, 'manual');
|
|
|
1732 |
|
|
|
1733 |
// Only a teacher should be able to lock a discussion.
|
|
|
1734 |
try {
|
|
|
1735 |
$result = mod_forum_external::set_lock_state($forum->id, $discussion->id, 0);
|
|
|
1736 |
$this->fail('Exception expected due to missing capability.');
|
|
|
1737 |
} catch (\moodle_exception $e) {
|
|
|
1738 |
$this->assertEquals('errorcannotlock', $e->errorcode);
|
|
|
1739 |
}
|
|
|
1740 |
|
|
|
1741 |
// Set the lock.
|
|
|
1742 |
self::setAdminUser();
|
|
|
1743 |
$sink = $this->redirectEvents(); // Capturing the event.
|
|
|
1744 |
$result = mod_forum_external::set_lock_state($forum->id, $discussion->id, 0);
|
|
|
1745 |
$result = external_api::clean_returnvalue(mod_forum_external::set_lock_state_returns(), $result);
|
|
|
1746 |
$this->assertTrue($result['locked']);
|
|
|
1747 |
$this->assertNotEquals(0, $result['times']['locked']);
|
|
|
1748 |
|
|
|
1749 |
// Check that the event contains the expected values.
|
|
|
1750 |
$events = $sink->get_events();
|
|
|
1751 |
$this->assertCount(1, $events);
|
|
|
1752 |
$event = reset($events);
|
|
|
1753 |
$this->assertInstanceOf('\mod_forum\event\discussion_lock_updated', $event);
|
|
|
1754 |
$this->assertEquals($context, $event->get_context());
|
|
|
1755 |
$this->assertEventContextNotUsed($event);
|
|
|
1756 |
$this->assertNotEmpty($event->get_name());
|
|
|
1757 |
$this->assertStringContainsString(' locked the discussion:', $event->get_description());
|
|
|
1758 |
|
|
|
1759 |
// Unset the lock.
|
|
|
1760 |
$sink = $this->redirectEvents(); // Capturing the event.
|
|
|
1761 |
$result = mod_forum_external::set_lock_state($forum->id, $discussion->id, time());
|
|
|
1762 |
$result = external_api::clean_returnvalue(mod_forum_external::set_lock_state_returns(), $result);
|
|
|
1763 |
$this->assertFalse($result['locked']);
|
|
|
1764 |
$this->assertEquals('0', $result['times']['locked']);
|
|
|
1765 |
|
|
|
1766 |
// Check that the event contains the expected values.
|
|
|
1767 |
$events = $sink->get_events();
|
|
|
1768 |
$this->assertCount(1, $events);
|
|
|
1769 |
$event = reset($events);
|
|
|
1770 |
$this->assertInstanceOf('\mod_forum\event\discussion_lock_updated', $event);
|
|
|
1771 |
$this->assertEquals($context, $event->get_context());
|
|
|
1772 |
$this->assertEventContextNotUsed($event);
|
|
|
1773 |
$this->assertNotEmpty($event->get_name());
|
|
|
1774 |
$this->assertStringContainsString(' unlocked the discussion:', $event->get_description());
|
|
|
1775 |
}
|
|
|
1776 |
|
|
|
1777 |
/*
|
|
|
1778 |
* Test can_add_discussion. A basic test since all the API functions are already covered by unit tests.
|
|
|
1779 |
*/
|
11 |
efrain |
1780 |
public function test_can_add_discussion(): void {
|
1 |
efrain |
1781 |
global $DB;
|
|
|
1782 |
$this->resetAfterTest(true);
|
|
|
1783 |
|
|
|
1784 |
// Create courses to add the modules.
|
|
|
1785 |
$course = self::getDataGenerator()->create_course();
|
|
|
1786 |
|
|
|
1787 |
$user = self::getDataGenerator()->create_user();
|
|
|
1788 |
|
|
|
1789 |
// First forum with tracking off.
|
|
|
1790 |
$record = new \stdClass();
|
|
|
1791 |
$record->course = $course->id;
|
|
|
1792 |
$record->type = 'news';
|
|
|
1793 |
$forum = self::getDataGenerator()->create_module('forum', $record);
|
|
|
1794 |
|
|
|
1795 |
// User with no permissions to add in a news forum.
|
|
|
1796 |
self::setUser($user);
|
|
|
1797 |
$this->getDataGenerator()->enrol_user($user->id, $course->id);
|
|
|
1798 |
|
|
|
1799 |
$result = mod_forum_external::can_add_discussion($forum->id);
|
|
|
1800 |
$result = external_api::clean_returnvalue(mod_forum_external::can_add_discussion_returns(), $result);
|
|
|
1801 |
$this->assertFalse($result['status']);
|
|
|
1802 |
$this->assertFalse($result['canpindiscussions']);
|
|
|
1803 |
$this->assertTrue($result['cancreateattachment']);
|
|
|
1804 |
|
|
|
1805 |
// Disable attachments.
|
|
|
1806 |
$DB->set_field('forum', 'maxattachments', 0, array('id' => $forum->id));
|
|
|
1807 |
$result = mod_forum_external::can_add_discussion($forum->id);
|
|
|
1808 |
$result = external_api::clean_returnvalue(mod_forum_external::can_add_discussion_returns(), $result);
|
|
|
1809 |
$this->assertFalse($result['status']);
|
|
|
1810 |
$this->assertFalse($result['canpindiscussions']);
|
|
|
1811 |
$this->assertFalse($result['cancreateattachment']);
|
|
|
1812 |
$DB->set_field('forum', 'maxattachments', 1, array('id' => $forum->id)); // Enable attachments again.
|
|
|
1813 |
|
|
|
1814 |
self::setAdminUser();
|
|
|
1815 |
$result = mod_forum_external::can_add_discussion($forum->id);
|
|
|
1816 |
$result = external_api::clean_returnvalue(mod_forum_external::can_add_discussion_returns(), $result);
|
|
|
1817 |
$this->assertTrue($result['status']);
|
|
|
1818 |
$this->assertTrue($result['canpindiscussions']);
|
|
|
1819 |
$this->assertTrue($result['cancreateattachment']);
|
|
|
1820 |
}
|
|
|
1821 |
|
|
|
1822 |
/*
|
|
|
1823 |
* A basic test to make sure users cannot post to forum after the cutoff date.
|
|
|
1824 |
*/
|
11 |
efrain |
1825 |
public function test_can_add_discussion_after_cutoff(): void {
|
1 |
efrain |
1826 |
$this->resetAfterTest(true);
|
|
|
1827 |
|
|
|
1828 |
// Create courses to add the modules.
|
|
|
1829 |
$course = self::getDataGenerator()->create_course();
|
|
|
1830 |
|
|
|
1831 |
$user = self::getDataGenerator()->create_user();
|
|
|
1832 |
|
|
|
1833 |
// Create a forum with cutoff date set to a past date.
|
|
|
1834 |
$forum = self::getDataGenerator()->create_module('forum', ['course' => $course->id, 'cutoffdate' => time() - 1]);
|
|
|
1835 |
|
|
|
1836 |
// User with no mod/forum:canoverridecutoff capability.
|
|
|
1837 |
self::setUser($user);
|
|
|
1838 |
$this->getDataGenerator()->enrol_user($user->id, $course->id);
|
|
|
1839 |
|
|
|
1840 |
$result = mod_forum_external::can_add_discussion($forum->id);
|
|
|
1841 |
$result = external_api::clean_returnvalue(mod_forum_external::can_add_discussion_returns(), $result);
|
|
|
1842 |
$this->assertFalse($result['status']);
|
|
|
1843 |
|
|
|
1844 |
self::setAdminUser();
|
|
|
1845 |
$result = mod_forum_external::can_add_discussion($forum->id);
|
|
|
1846 |
$result = external_api::clean_returnvalue(mod_forum_external::can_add_discussion_returns(), $result);
|
|
|
1847 |
$this->assertTrue($result['status']);
|
|
|
1848 |
}
|
|
|
1849 |
|
|
|
1850 |
/**
|
|
|
1851 |
* Test get posts discussions including rating information.
|
|
|
1852 |
*/
|
11 |
efrain |
1853 |
public function test_mod_forum_get_discussion_rating_information(): void {
|
1 |
efrain |
1854 |
global $DB, $CFG, $PAGE;
|
|
|
1855 |
require_once($CFG->dirroot . '/rating/lib.php');
|
|
|
1856 |
$PAGE->set_url('/my/index.php'); // Need this because some internal API calls require the $PAGE url to be set.
|
|
|
1857 |
$this->resetAfterTest(true);
|
|
|
1858 |
|
|
|
1859 |
$user1 = self::getDataGenerator()->create_user();
|
|
|
1860 |
$user2 = self::getDataGenerator()->create_user();
|
|
|
1861 |
$user3 = self::getDataGenerator()->create_user();
|
|
|
1862 |
$teacher = self::getDataGenerator()->create_user();
|
|
|
1863 |
|
|
|
1864 |
// Create course to add the module.
|
|
|
1865 |
$course = self::getDataGenerator()->create_course();
|
|
|
1866 |
|
|
|
1867 |
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
|
|
|
1868 |
$teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'));
|
|
|
1869 |
$this->getDataGenerator()->enrol_user($user1->id, $course->id, $studentrole->id, 'manual');
|
|
|
1870 |
$this->getDataGenerator()->enrol_user($user2->id, $course->id, $studentrole->id, 'manual');
|
|
|
1871 |
$this->getDataGenerator()->enrol_user($user3->id, $course->id, $studentrole->id, 'manual');
|
|
|
1872 |
$this->getDataGenerator()->enrol_user($teacher->id, $course->id, $teacherrole->id, 'manual');
|
|
|
1873 |
|
|
|
1874 |
// Create the forum.
|
|
|
1875 |
$record = new \stdClass();
|
|
|
1876 |
$record->course = $course->id;
|
|
|
1877 |
// Set Aggregate type = Average of ratings.
|
|
|
1878 |
$record->assessed = RATING_AGGREGATE_AVERAGE;
|
|
|
1879 |
$record->scale = 100;
|
|
|
1880 |
$forum = self::getDataGenerator()->create_module('forum', $record);
|
|
|
1881 |
$context = \context_module::instance($forum->cmid);
|
|
|
1882 |
|
|
|
1883 |
// Add discussion to the forum.
|
|
|
1884 |
$record = new \stdClass();
|
|
|
1885 |
$record->course = $course->id;
|
|
|
1886 |
$record->userid = $user1->id;
|
|
|
1887 |
$record->forum = $forum->id;
|
|
|
1888 |
$discussion = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
|
|
|
1889 |
|
|
|
1890 |
// Retrieve the first post.
|
|
|
1891 |
$post = $DB->get_record('forum_posts', array('discussion' => $discussion->id));
|
|
|
1892 |
|
|
|
1893 |
// Rate the discussion as user2.
|
|
|
1894 |
$rating1 = new \stdClass();
|
|
|
1895 |
$rating1->contextid = $context->id;
|
|
|
1896 |
$rating1->component = 'mod_forum';
|
|
|
1897 |
$rating1->ratingarea = 'post';
|
|
|
1898 |
$rating1->itemid = $post->id;
|
|
|
1899 |
$rating1->rating = 50;
|
|
|
1900 |
$rating1->scaleid = 100;
|
|
|
1901 |
$rating1->userid = $user2->id;
|
|
|
1902 |
$rating1->timecreated = time();
|
|
|
1903 |
$rating1->timemodified = time();
|
|
|
1904 |
$rating1->id = $DB->insert_record('rating', $rating1);
|
|
|
1905 |
|
|
|
1906 |
// Rate the discussion as user3.
|
|
|
1907 |
$rating2 = new \stdClass();
|
|
|
1908 |
$rating2->contextid = $context->id;
|
|
|
1909 |
$rating2->component = 'mod_forum';
|
|
|
1910 |
$rating2->ratingarea = 'post';
|
|
|
1911 |
$rating2->itemid = $post->id;
|
|
|
1912 |
$rating2->rating = 100;
|
|
|
1913 |
$rating2->scaleid = 100;
|
|
|
1914 |
$rating2->userid = $user3->id;
|
|
|
1915 |
$rating2->timecreated = time() + 1;
|
|
|
1916 |
$rating2->timemodified = time() + 1;
|
|
|
1917 |
$rating2->id = $DB->insert_record('rating', $rating2);
|
|
|
1918 |
|
|
|
1919 |
// Retrieve the rating for the post as student.
|
|
|
1920 |
$this->setUser($user1);
|
|
|
1921 |
$posts = mod_forum_external::get_discussion_posts($discussion->id, 'id', 'DESC');
|
|
|
1922 |
$posts = external_api::clean_returnvalue(mod_forum_external::get_discussion_posts_returns(), $posts);
|
|
|
1923 |
$this->assertCount(1, $posts['ratinginfo']['ratings']);
|
|
|
1924 |
$this->assertTrue($posts['ratinginfo']['ratings'][0]['canviewaggregate']);
|
|
|
1925 |
$this->assertFalse($posts['ratinginfo']['canviewall']);
|
|
|
1926 |
$this->assertFalse($posts['ratinginfo']['ratings'][0]['canrate']);
|
|
|
1927 |
$this->assertEquals(2, $posts['ratinginfo']['ratings'][0]['count']);
|
|
|
1928 |
$this->assertEquals(($rating1->rating + $rating2->rating) / 2, $posts['ratinginfo']['ratings'][0]['aggregate']);
|
|
|
1929 |
|
|
|
1930 |
// Retrieve the rating for the post as teacher.
|
|
|
1931 |
$this->setUser($teacher);
|
|
|
1932 |
$posts = mod_forum_external::get_discussion_posts($discussion->id, 'id', 'DESC');
|
|
|
1933 |
$posts = external_api::clean_returnvalue(mod_forum_external::get_discussion_posts_returns(), $posts);
|
|
|
1934 |
$this->assertCount(1, $posts['ratinginfo']['ratings']);
|
|
|
1935 |
$this->assertTrue($posts['ratinginfo']['ratings'][0]['canviewaggregate']);
|
|
|
1936 |
$this->assertTrue($posts['ratinginfo']['canviewall']);
|
|
|
1937 |
$this->assertTrue($posts['ratinginfo']['ratings'][0]['canrate']);
|
|
|
1938 |
$this->assertEquals(2, $posts['ratinginfo']['ratings'][0]['count']);
|
|
|
1939 |
$this->assertEquals(($rating1->rating + $rating2->rating) / 2, $posts['ratinginfo']['ratings'][0]['aggregate']);
|
|
|
1940 |
}
|
|
|
1941 |
|
|
|
1942 |
/**
|
|
|
1943 |
* Test mod_forum_get_forum_access_information.
|
|
|
1944 |
*/
|
11 |
efrain |
1945 |
public function test_mod_forum_get_forum_access_information(): void {
|
1 |
efrain |
1946 |
global $DB;
|
|
|
1947 |
|
|
|
1948 |
$this->resetAfterTest(true);
|
|
|
1949 |
|
|
|
1950 |
$student = self::getDataGenerator()->create_user();
|
|
|
1951 |
$course = self::getDataGenerator()->create_course();
|
|
|
1952 |
// Create the forum.
|
|
|
1953 |
$record = new \stdClass();
|
|
|
1954 |
$record->course = $course->id;
|
|
|
1955 |
$forum = self::getDataGenerator()->create_module('forum', $record);
|
|
|
1956 |
|
|
|
1957 |
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
|
|
|
1958 |
$this->getDataGenerator()->enrol_user($student->id, $course->id, $studentrole->id, 'manual');
|
|
|
1959 |
|
|
|
1960 |
self::setUser($student);
|
|
|
1961 |
$result = mod_forum_external::get_forum_access_information($forum->id);
|
|
|
1962 |
$result = external_api::clean_returnvalue(mod_forum_external::get_forum_access_information_returns(), $result);
|
|
|
1963 |
|
|
|
1964 |
// Check default values for capabilities.
|
|
|
1965 |
$enabledcaps = array('canviewdiscussion', 'canstartdiscussion', 'canreplypost', 'canviewrating', 'cancreateattachment',
|
|
|
1966 |
'canexportownpost', 'cancantogglefavourite', 'cancanmailnow', 'candeleteownpost', 'canallowforcesubscribe');
|
|
|
1967 |
|
|
|
1968 |
unset($result['warnings']);
|
|
|
1969 |
foreach ($result as $capname => $capvalue) {
|
|
|
1970 |
if (in_array($capname, $enabledcaps)) {
|
|
|
1971 |
$this->assertTrue($capvalue);
|
|
|
1972 |
} else {
|
|
|
1973 |
$this->assertFalse($capvalue);
|
|
|
1974 |
}
|
|
|
1975 |
}
|
|
|
1976 |
// Now, unassign some capabilities.
|
|
|
1977 |
unassign_capability('mod/forum:deleteownpost', $studentrole->id);
|
|
|
1978 |
unassign_capability('mod/forum:allowforcesubscribe', $studentrole->id);
|
|
|
1979 |
array_pop($enabledcaps);
|
|
|
1980 |
array_pop($enabledcaps);
|
|
|
1981 |
accesslib_clear_all_caches_for_unit_testing();
|
|
|
1982 |
|
|
|
1983 |
$result = mod_forum_external::get_forum_access_information($forum->id);
|
|
|
1984 |
$result = external_api::clean_returnvalue(mod_forum_external::get_forum_access_information_returns(), $result);
|
|
|
1985 |
unset($result['warnings']);
|
|
|
1986 |
foreach ($result as $capname => $capvalue) {
|
|
|
1987 |
if (in_array($capname, $enabledcaps)) {
|
|
|
1988 |
$this->assertTrue($capvalue);
|
|
|
1989 |
} else {
|
|
|
1990 |
$this->assertFalse($capvalue);
|
|
|
1991 |
}
|
|
|
1992 |
}
|
|
|
1993 |
}
|
|
|
1994 |
|
|
|
1995 |
/**
|
|
|
1996 |
* Test add_discussion_post
|
|
|
1997 |
*/
|
11 |
efrain |
1998 |
public function test_add_discussion_post_private(): void {
|
1 |
efrain |
1999 |
global $DB;
|
|
|
2000 |
|
|
|
2001 |
$this->resetAfterTest(true);
|
|
|
2002 |
|
|
|
2003 |
self::setAdminUser();
|
|
|
2004 |
|
|
|
2005 |
// Create course to add the module.
|
|
|
2006 |
$course = self::getDataGenerator()->create_course();
|
|
|
2007 |
|
|
|
2008 |
// Standard forum.
|
|
|
2009 |
$record = new \stdClass();
|
|
|
2010 |
$record->course = $course->id;
|
|
|
2011 |
$forum = self::getDataGenerator()->create_module('forum', $record);
|
|
|
2012 |
$cm = get_coursemodule_from_id('forum', $forum->cmid, 0, false, MUST_EXIST);
|
|
|
2013 |
$forumcontext = \context_module::instance($forum->cmid);
|
|
|
2014 |
$generator = self::getDataGenerator()->get_plugin_generator('mod_forum');
|
|
|
2015 |
|
|
|
2016 |
// Create an enrol users.
|
|
|
2017 |
$student1 = self::getDataGenerator()->create_user();
|
|
|
2018 |
$this->getDataGenerator()->enrol_user($student1->id, $course->id, 'student');
|
|
|
2019 |
$student2 = self::getDataGenerator()->create_user();
|
|
|
2020 |
$this->getDataGenerator()->enrol_user($student2->id, $course->id, 'student');
|
|
|
2021 |
$teacher1 = self::getDataGenerator()->create_user();
|
|
|
2022 |
$this->getDataGenerator()->enrol_user($teacher1->id, $course->id, 'editingteacher');
|
|
|
2023 |
$teacher2 = self::getDataGenerator()->create_user();
|
|
|
2024 |
$this->getDataGenerator()->enrol_user($teacher2->id, $course->id, 'editingteacher');
|
|
|
2025 |
|
|
|
2026 |
// Add a new discussion to the forum.
|
|
|
2027 |
self::setUser($student1);
|
|
|
2028 |
$record = new \stdClass();
|
|
|
2029 |
$record->course = $course->id;
|
|
|
2030 |
$record->userid = $student1->id;
|
|
|
2031 |
$record->forum = $forum->id;
|
|
|
2032 |
$discussion = $generator->create_discussion($record);
|
|
|
2033 |
|
|
|
2034 |
// Have the teacher reply privately.
|
|
|
2035 |
self::setUser($teacher1);
|
|
|
2036 |
$post = mod_forum_external::add_discussion_post($discussion->firstpost, 'some subject', 'some text here...', [
|
|
|
2037 |
[
|
|
|
2038 |
'name' => 'private',
|
|
|
2039 |
'value' => true,
|
|
|
2040 |
],
|
|
|
2041 |
]);
|
|
|
2042 |
$post = external_api::clean_returnvalue(mod_forum_external::add_discussion_post_returns(), $post);
|
|
|
2043 |
$privatereply = $DB->get_record('forum_posts', array('id' => $post['postid']));
|
|
|
2044 |
$this->assertEquals($student1->id, $privatereply->privatereplyto);
|
|
|
2045 |
// Bump the time of the private reply to ensure order.
|
|
|
2046 |
$privatereply->created++;
|
|
|
2047 |
$privatereply->modified = $privatereply->created;
|
|
|
2048 |
$DB->update_record('forum_posts', $privatereply);
|
|
|
2049 |
|
|
|
2050 |
// The teacher will receive their private reply.
|
|
|
2051 |
self::setUser($teacher1);
|
|
|
2052 |
$posts = mod_forum_external::get_discussion_posts($discussion->id, 'id', 'DESC');
|
|
|
2053 |
$posts = external_api::clean_returnvalue(mod_forum_external::get_discussion_posts_returns(), $posts);
|
|
|
2054 |
$this->assertEquals(2, count($posts['posts']));
|
|
|
2055 |
$this->assertTrue($posts['posts'][0]['isprivatereply']);
|
|
|
2056 |
|
|
|
2057 |
// Another teacher on the course will also receive the private reply.
|
|
|
2058 |
self::setUser($teacher2);
|
|
|
2059 |
$posts = mod_forum_external::get_discussion_posts($discussion->id, 'id', 'DESC');
|
|
|
2060 |
$posts = external_api::clean_returnvalue(mod_forum_external::get_discussion_posts_returns(), $posts);
|
|
|
2061 |
$this->assertEquals(2, count($posts['posts']));
|
|
|
2062 |
$this->assertTrue($posts['posts'][0]['isprivatereply']);
|
|
|
2063 |
|
|
|
2064 |
// The student will receive the private reply.
|
|
|
2065 |
self::setUser($student1);
|
|
|
2066 |
$posts = mod_forum_external::get_discussion_posts($discussion->id, 'id', 'DESC');
|
|
|
2067 |
$posts = external_api::clean_returnvalue(mod_forum_external::get_discussion_posts_returns(), $posts);
|
|
|
2068 |
$this->assertEquals(2, count($posts['posts']));
|
|
|
2069 |
$this->assertTrue($posts['posts'][0]['isprivatereply']);
|
|
|
2070 |
|
|
|
2071 |
// Another student will not receive the private reply.
|
|
|
2072 |
self::setUser($student2);
|
|
|
2073 |
$posts = mod_forum_external::get_discussion_posts($discussion->id, 'id', 'ASC');
|
|
|
2074 |
$posts = external_api::clean_returnvalue(mod_forum_external::get_discussion_posts_returns(), $posts);
|
|
|
2075 |
$this->assertEquals(1, count($posts['posts']));
|
|
|
2076 |
$this->assertFalse($posts['posts'][0]['isprivatereply']);
|
|
|
2077 |
|
|
|
2078 |
// A user cannot reply to a private reply.
|
|
|
2079 |
self::setUser($teacher2);
|
|
|
2080 |
$this->expectException('coding_exception');
|
|
|
2081 |
$post = mod_forum_external::add_discussion_post($privatereply->id, 'some subject', 'some text here...', [
|
|
|
2082 |
'options' => [
|
|
|
2083 |
'name' => 'private',
|
|
|
2084 |
'value' => false,
|
|
|
2085 |
],
|
|
|
2086 |
]);
|
|
|
2087 |
}
|
|
|
2088 |
|
|
|
2089 |
/**
|
|
|
2090 |
* Test trusted text enabled.
|
|
|
2091 |
*/
|
11 |
efrain |
2092 |
public function test_trusted_text_enabled(): void {
|
1 |
efrain |
2093 |
global $USER, $CFG;
|
|
|
2094 |
|
|
|
2095 |
$this->resetAfterTest(true);
|
|
|
2096 |
$CFG->enabletrusttext = 1;
|
|
|
2097 |
|
|
|
2098 |
$dangeroustext = '<button>Untrusted text</button>';
|
|
|
2099 |
$cleantext = 'Untrusted text';
|
|
|
2100 |
|
|
|
2101 |
// Create courses to add the modules.
|
|
|
2102 |
$course = self::getDataGenerator()->create_course();
|
|
|
2103 |
$user1 = self::getDataGenerator()->create_user();
|
|
|
2104 |
|
|
|
2105 |
// First forum with tracking off.
|
|
|
2106 |
$record = new \stdClass();
|
|
|
2107 |
$record->course = $course->id;
|
|
|
2108 |
$record->type = 'qanda';
|
|
|
2109 |
$forum = self::getDataGenerator()->create_module('forum', $record);
|
|
|
2110 |
$context = \context_module::instance($forum->cmid);
|
|
|
2111 |
|
|
|
2112 |
// Add discussions to the forums.
|
|
|
2113 |
$discussionrecord = new \stdClass();
|
|
|
2114 |
$discussionrecord->course = $course->id;
|
|
|
2115 |
$discussionrecord->userid = $user1->id;
|
|
|
2116 |
$discussionrecord->forum = $forum->id;
|
|
|
2117 |
$discussionrecord->message = $dangeroustext;
|
|
|
2118 |
$discussionrecord->messagetrust = trusttext_trusted($context);
|
|
|
2119 |
$discussion1 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($discussionrecord);
|
|
|
2120 |
|
|
|
2121 |
self::setAdminUser();
|
|
|
2122 |
$discussionrecord->userid = $USER->id;
|
|
|
2123 |
$discussionrecord->messagetrust = trusttext_trusted($context);
|
|
|
2124 |
$discussion2 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($discussionrecord);
|
|
|
2125 |
|
|
|
2126 |
$discussions = mod_forum_external::get_forum_discussions($forum->id);
|
|
|
2127 |
$discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_returns(), $discussions);
|
|
|
2128 |
|
|
|
2129 |
$this->assertCount(2, $discussions['discussions']);
|
|
|
2130 |
$this->assertCount(0, $discussions['warnings']);
|
|
|
2131 |
// Admin message is fully trusted.
|
|
|
2132 |
$this->assertEquals(1, $discussions['discussions'][0]['messagetrust']);
|
|
|
2133 |
$this->assertEquals($dangeroustext, $discussions['discussions'][0]['message']);
|
|
|
2134 |
// Student message is not trusted.
|
|
|
2135 |
$this->assertEquals(0, $discussions['discussions'][1]['messagetrust']);
|
|
|
2136 |
$this->assertEquals($cleantext, $discussions['discussions'][1]['message']);
|
|
|
2137 |
|
|
|
2138 |
// Get posts now.
|
|
|
2139 |
$posts = mod_forum_external::get_discussion_posts($discussion2->id, 'modified', 'DESC');
|
|
|
2140 |
$posts = external_api::clean_returnvalue(mod_forum_external::get_discussion_posts_returns(), $posts);
|
|
|
2141 |
// Admin message is fully trusted.
|
|
|
2142 |
$this->assertEquals($dangeroustext, $posts['posts'][0]['message']);
|
|
|
2143 |
|
|
|
2144 |
$posts = mod_forum_external::get_discussion_posts($discussion1->id, 'modified', 'ASC');
|
|
|
2145 |
$posts = external_api::clean_returnvalue(mod_forum_external::get_discussion_posts_returns(), $posts);
|
|
|
2146 |
// Student message is not trusted.
|
|
|
2147 |
$this->assertEquals($cleantext, $posts['posts'][0]['message']);
|
|
|
2148 |
}
|
|
|
2149 |
|
|
|
2150 |
/**
|
|
|
2151 |
* Test trusted text disabled.
|
|
|
2152 |
*/
|
11 |
efrain |
2153 |
public function test_trusted_text_disabled(): void {
|
1 |
efrain |
2154 |
global $USER, $CFG;
|
|
|
2155 |
|
|
|
2156 |
$this->resetAfterTest(true);
|
|
|
2157 |
$CFG->enabletrusttext = 0;
|
|
|
2158 |
|
|
|
2159 |
$dangeroustext = '<button>Untrusted text</button>';
|
|
|
2160 |
$cleantext = 'Untrusted text';
|
|
|
2161 |
|
|
|
2162 |
// Create courses to add the modules.
|
|
|
2163 |
$course = self::getDataGenerator()->create_course();
|
|
|
2164 |
$user1 = self::getDataGenerator()->create_user();
|
|
|
2165 |
|
|
|
2166 |
// First forum with tracking off.
|
|
|
2167 |
$record = new \stdClass();
|
|
|
2168 |
$record->course = $course->id;
|
|
|
2169 |
$record->type = 'qanda';
|
|
|
2170 |
$forum = self::getDataGenerator()->create_module('forum', $record);
|
|
|
2171 |
$context = \context_module::instance($forum->cmid);
|
|
|
2172 |
|
|
|
2173 |
// Add discussions to the forums.
|
|
|
2174 |
$discussionrecord = new \stdClass();
|
|
|
2175 |
$discussionrecord->course = $course->id;
|
|
|
2176 |
$discussionrecord->userid = $user1->id;
|
|
|
2177 |
$discussionrecord->forum = $forum->id;
|
|
|
2178 |
$discussionrecord->message = $dangeroustext;
|
|
|
2179 |
$discussionrecord->messagetrust = trusttext_trusted($context);
|
|
|
2180 |
$discussion1 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($discussionrecord);
|
|
|
2181 |
|
|
|
2182 |
self::setAdminUser();
|
|
|
2183 |
$discussionrecord->userid = $USER->id;
|
|
|
2184 |
$discussionrecord->messagetrust = trusttext_trusted($context);
|
|
|
2185 |
$discussion2 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($discussionrecord);
|
|
|
2186 |
|
|
|
2187 |
$discussions = mod_forum_external::get_forum_discussions($forum->id);
|
|
|
2188 |
$discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_returns(), $discussions);
|
|
|
2189 |
|
|
|
2190 |
$this->assertCount(2, $discussions['discussions']);
|
|
|
2191 |
$this->assertCount(0, $discussions['warnings']);
|
|
|
2192 |
// Admin message is not trusted because enabletrusttext is disabled.
|
|
|
2193 |
$this->assertEquals(0, $discussions['discussions'][0]['messagetrust']);
|
|
|
2194 |
$this->assertEquals($cleantext, $discussions['discussions'][0]['message']);
|
|
|
2195 |
// Student message is not trusted.
|
|
|
2196 |
$this->assertEquals(0, $discussions['discussions'][1]['messagetrust']);
|
|
|
2197 |
$this->assertEquals($cleantext, $discussions['discussions'][1]['message']);
|
|
|
2198 |
|
|
|
2199 |
// Get posts now.
|
|
|
2200 |
$posts = mod_forum_external::get_discussion_posts($discussion2->id, 'modified', 'ASC');
|
|
|
2201 |
$posts = external_api::clean_returnvalue(mod_forum_external::get_discussion_posts_returns(), $posts);
|
|
|
2202 |
// Admin message is not trusted because enabletrusttext is disabled.
|
|
|
2203 |
$this->assertEquals($cleantext, $posts['posts'][0]['message']);
|
|
|
2204 |
|
|
|
2205 |
$posts = mod_forum_external::get_discussion_posts($discussion1->id, 'modified', 'ASC');
|
|
|
2206 |
$posts = external_api::clean_returnvalue(mod_forum_external::get_discussion_posts_returns(), $posts);
|
|
|
2207 |
// Student message is not trusted.
|
|
|
2208 |
$this->assertEquals($cleantext, $posts['posts'][0]['message']);
|
|
|
2209 |
}
|
|
|
2210 |
|
|
|
2211 |
/**
|
|
|
2212 |
* Test delete a discussion.
|
|
|
2213 |
*/
|
11 |
efrain |
2214 |
public function test_delete_post_discussion(): void {
|
1 |
efrain |
2215 |
global $DB;
|
|
|
2216 |
$this->resetAfterTest(true);
|
|
|
2217 |
|
|
|
2218 |
// Setup test data.
|
|
|
2219 |
$course = $this->getDataGenerator()->create_course();
|
|
|
2220 |
$forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
|
|
|
2221 |
$user = $this->getDataGenerator()->create_user();
|
|
|
2222 |
$role = $DB->get_record('role', array('shortname' => 'student'), '*', MUST_EXIST);
|
|
|
2223 |
self::getDataGenerator()->enrol_user($user->id, $course->id, $role->id);
|
|
|
2224 |
|
|
|
2225 |
// Add a discussion.
|
|
|
2226 |
$record = new \stdClass();
|
|
|
2227 |
$record->course = $course->id;
|
|
|
2228 |
$record->userid = $user->id;
|
|
|
2229 |
$record->forum = $forum->id;
|
|
|
2230 |
$discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
|
|
|
2231 |
|
|
|
2232 |
$this->setUser($user);
|
|
|
2233 |
$result = mod_forum_external::delete_post($discussion->firstpost);
|
|
|
2234 |
$result = external_api::clean_returnvalue(mod_forum_external::delete_post_returns(), $result);
|
|
|
2235 |
$this->assertTrue($result['status']);
|
|
|
2236 |
$this->assertEquals(0, $DB->count_records('forum_posts', array('id' => $discussion->firstpost)));
|
|
|
2237 |
$this->assertEquals(0, $DB->count_records('forum_discussions', array('id' => $discussion->id)));
|
|
|
2238 |
}
|
|
|
2239 |
|
|
|
2240 |
/**
|
|
|
2241 |
* Test delete a post.
|
|
|
2242 |
*/
|
11 |
efrain |
2243 |
public function test_delete_post_post(): void {
|
1 |
efrain |
2244 |
global $DB;
|
|
|
2245 |
$this->resetAfterTest(true);
|
|
|
2246 |
|
|
|
2247 |
// Setup test data.
|
|
|
2248 |
$course = $this->getDataGenerator()->create_course();
|
|
|
2249 |
$forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
|
|
|
2250 |
$user = $this->getDataGenerator()->create_user();
|
|
|
2251 |
$role = $DB->get_record('role', array('shortname' => 'student'), '*', MUST_EXIST);
|
|
|
2252 |
self::getDataGenerator()->enrol_user($user->id, $course->id, $role->id);
|
|
|
2253 |
|
|
|
2254 |
// Add a discussion.
|
|
|
2255 |
$record = new \stdClass();
|
|
|
2256 |
$record->course = $course->id;
|
|
|
2257 |
$record->userid = $user->id;
|
|
|
2258 |
$record->forum = $forum->id;
|
|
|
2259 |
$discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
|
|
|
2260 |
$parentpost = $DB->get_record('forum_posts', array('discussion' => $discussion->id));
|
|
|
2261 |
|
|
|
2262 |
// Add a post.
|
|
|
2263 |
$record = new \stdClass();
|
|
|
2264 |
$record->course = $course->id;
|
|
|
2265 |
$record->userid = $user->id;
|
|
|
2266 |
$record->forum = $forum->id;
|
|
|
2267 |
$record->discussion = $discussion->id;
|
|
|
2268 |
$record->parent = $parentpost->id;
|
|
|
2269 |
$post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
|
|
|
2270 |
|
|
|
2271 |
$this->setUser($user);
|
|
|
2272 |
$result = mod_forum_external::delete_post($post->id);
|
|
|
2273 |
$result = external_api::clean_returnvalue(mod_forum_external::delete_post_returns(), $result);
|
|
|
2274 |
$this->assertTrue($result['status']);
|
|
|
2275 |
$this->assertEquals(1, $DB->count_records('forum_posts', array('discussion' => $discussion->id)));
|
|
|
2276 |
$this->assertEquals(1, $DB->count_records('forum_discussions', array('id' => $discussion->id)));
|
|
|
2277 |
}
|
|
|
2278 |
|
|
|
2279 |
/**
|
|
|
2280 |
* Test delete a different user post.
|
|
|
2281 |
*/
|
11 |
efrain |
2282 |
public function test_delete_post_other_user_post(): void {
|
1 |
efrain |
2283 |
global $DB;
|
|
|
2284 |
$this->resetAfterTest(true);
|
|
|
2285 |
|
|
|
2286 |
// Setup test data.
|
|
|
2287 |
$course = $this->getDataGenerator()->create_course();
|
|
|
2288 |
$forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
|
|
|
2289 |
$user = $this->getDataGenerator()->create_user();
|
|
|
2290 |
$otheruser = $this->getDataGenerator()->create_user();
|
|
|
2291 |
$role = $DB->get_record('role', array('shortname' => 'student'), '*', MUST_EXIST);
|
|
|
2292 |
self::getDataGenerator()->enrol_user($user->id, $course->id, $role->id);
|
|
|
2293 |
self::getDataGenerator()->enrol_user($otheruser->id, $course->id, $role->id);
|
|
|
2294 |
|
|
|
2295 |
// Add a discussion.
|
|
|
2296 |
$record = array();
|
|
|
2297 |
$record['course'] = $course->id;
|
|
|
2298 |
$record['forum'] = $forum->id;
|
|
|
2299 |
$record['userid'] = $user->id;
|
|
|
2300 |
$discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
|
|
|
2301 |
$parentpost = $DB->get_record('forum_posts', array('discussion' => $discussion->id));
|
|
|
2302 |
|
|
|
2303 |
// Add a post.
|
|
|
2304 |
$record = new \stdClass();
|
|
|
2305 |
$record->course = $course->id;
|
|
|
2306 |
$record->userid = $user->id;
|
|
|
2307 |
$record->forum = $forum->id;
|
|
|
2308 |
$record->discussion = $discussion->id;
|
|
|
2309 |
$record->parent = $parentpost->id;
|
|
|
2310 |
$post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
|
|
|
2311 |
|
|
|
2312 |
$this->setUser($otheruser);
|
|
|
2313 |
$this->expectExceptionMessage(get_string('cannotdeletepost', 'forum'));
|
|
|
2314 |
mod_forum_external::delete_post($post->id);
|
|
|
2315 |
}
|
|
|
2316 |
|
|
|
2317 |
/*
|
|
|
2318 |
* Test get forum posts by user id.
|
|
|
2319 |
*/
|
11 |
efrain |
2320 |
public function test_mod_forum_get_discussion_posts_by_userid(): void {
|
1 |
efrain |
2321 |
global $DB;
|
|
|
2322 |
$this->resetAfterTest(true);
|
|
|
2323 |
|
|
|
2324 |
$urlfactory = \mod_forum\local\container::get_url_factory();
|
|
|
2325 |
$entityfactory = \mod_forum\local\container::get_entity_factory();
|
|
|
2326 |
$vaultfactory = \mod_forum\local\container::get_vault_factory();
|
|
|
2327 |
$postvault = $vaultfactory->get_post_vault();
|
|
|
2328 |
$legacydatamapper = \mod_forum\local\container::get_legacy_data_mapper_factory();
|
|
|
2329 |
$legacypostmapper = $legacydatamapper->get_post_data_mapper();
|
|
|
2330 |
|
|
|
2331 |
// Create course to add the module.
|
|
|
2332 |
$course1 = self::getDataGenerator()->create_course();
|
|
|
2333 |
|
|
|
2334 |
$user1 = self::getDataGenerator()->create_user();
|
|
|
2335 |
$user1entity = $entityfactory->get_author_from_stdClass($user1);
|
|
|
2336 |
$exporteduser1 = [
|
|
|
2337 |
'id' => (int) $user1->id,
|
|
|
2338 |
'fullname' => fullname($user1),
|
|
|
2339 |
'groups' => [],
|
|
|
2340 |
'urls' => [
|
|
|
2341 |
'profile' => $urlfactory->get_author_profile_url($user1entity, $course1->id)->out(false),
|
|
|
2342 |
'profileimage' => $urlfactory->get_author_profile_image_url($user1entity),
|
|
|
2343 |
],
|
|
|
2344 |
'isdeleted' => false,
|
|
|
2345 |
];
|
|
|
2346 |
// Create a bunch of other users to post.
|
|
|
2347 |
$user2 = self::getDataGenerator()->create_user();
|
|
|
2348 |
$user2entity = $entityfactory->get_author_from_stdClass($user2);
|
|
|
2349 |
$exporteduser2 = [
|
|
|
2350 |
'id' => (int) $user2->id,
|
|
|
2351 |
'fullname' => fullname($user2),
|
|
|
2352 |
'groups' => [],
|
|
|
2353 |
'urls' => [
|
|
|
2354 |
'profile' => $urlfactory->get_author_profile_url($user2entity, $course1->id)->out(false),
|
|
|
2355 |
'profileimage' => $urlfactory->get_author_profile_image_url($user2entity),
|
|
|
2356 |
],
|
|
|
2357 |
'isdeleted' => false,
|
|
|
2358 |
];
|
|
|
2359 |
$user2->fullname = $exporteduser2['fullname'];
|
|
|
2360 |
|
|
|
2361 |
$forumgenerator = self::getDataGenerator()->get_plugin_generator('mod_forum');
|
|
|
2362 |
|
|
|
2363 |
// Set the first created user to the test user.
|
|
|
2364 |
self::setUser($user1);
|
|
|
2365 |
|
|
|
2366 |
// Forum with tracking off.
|
|
|
2367 |
$record = new \stdClass();
|
|
|
2368 |
$record->course = $course1->id;
|
|
|
2369 |
$forum1 = self::getDataGenerator()->create_module('forum', $record);
|
|
|
2370 |
$forum1context = \context_module::instance($forum1->cmid);
|
|
|
2371 |
|
|
|
2372 |
// Add discussions to the forums.
|
|
|
2373 |
$time = time();
|
|
|
2374 |
$record = new \stdClass();
|
|
|
2375 |
$record->course = $course1->id;
|
|
|
2376 |
$record->userid = $user1->id;
|
|
|
2377 |
$record->forum = $forum1->id;
|
|
|
2378 |
$record->timemodified = $time + 100;
|
|
|
2379 |
$discussion1 = $forumgenerator->create_discussion($record);
|
|
|
2380 |
$discussion1firstpost = $postvault->get_first_post_for_discussion_ids([$discussion1->id]);
|
|
|
2381 |
$discussion1firstpost = $discussion1firstpost[$discussion1->firstpost];
|
|
|
2382 |
$discussion1firstpostobject = $legacypostmapper->to_legacy_object($discussion1firstpost);
|
|
|
2383 |
|
|
|
2384 |
$record = new \stdClass();
|
|
|
2385 |
$record->course = $course1->id;
|
|
|
2386 |
$record->userid = $user1->id;
|
|
|
2387 |
$record->forum = $forum1->id;
|
|
|
2388 |
$record->timemodified = $time + 200;
|
|
|
2389 |
$discussion2 = $forumgenerator->create_discussion($record);
|
|
|
2390 |
$discussion2firstpost = $postvault->get_first_post_for_discussion_ids([$discussion2->id]);
|
|
|
2391 |
$discussion2firstpost = $discussion2firstpost[$discussion2->firstpost];
|
|
|
2392 |
$discussion2firstpostobject = $legacypostmapper->to_legacy_object($discussion2firstpost);
|
|
|
2393 |
|
|
|
2394 |
// Add 1 reply to the discussion 1 from a different user.
|
|
|
2395 |
$record = new \stdClass();
|
|
|
2396 |
$record->discussion = $discussion1->id;
|
|
|
2397 |
$record->parent = $discussion1->firstpost;
|
|
|
2398 |
$record->userid = $user2->id;
|
|
|
2399 |
$discussion1reply1 = $forumgenerator->create_post($record);
|
|
|
2400 |
$filename = 'shouldbeanimage.jpg';
|
|
|
2401 |
// Add a fake inline image to the post.
|
|
|
2402 |
$filerecordinline = array(
|
|
|
2403 |
'contextid' => $forum1context->id,
|
|
|
2404 |
'component' => 'mod_forum',
|
|
|
2405 |
'filearea' => 'post',
|
|
|
2406 |
'itemid' => $discussion1reply1->id,
|
|
|
2407 |
'filepath' => '/',
|
|
|
2408 |
'filename' => $filename,
|
|
|
2409 |
);
|
|
|
2410 |
$fs = get_file_storage();
|
|
|
2411 |
$file1 = $fs->create_file_from_string($filerecordinline, 'image contents (not really)');
|
|
|
2412 |
|
|
|
2413 |
// Add 1 reply to the discussion 2 from a different user.
|
|
|
2414 |
$record = new \stdClass();
|
|
|
2415 |
$record->discussion = $discussion2->id;
|
|
|
2416 |
$record->parent = $discussion2->firstpost;
|
|
|
2417 |
$record->userid = $user2->id;
|
|
|
2418 |
$discussion2reply1 = $forumgenerator->create_post($record);
|
|
|
2419 |
$filename = 'shouldbeanimage.jpg';
|
|
|
2420 |
// Add a fake inline image to the post.
|
|
|
2421 |
$filerecordinline = array(
|
|
|
2422 |
'contextid' => $forum1context->id,
|
|
|
2423 |
'component' => 'mod_forum',
|
|
|
2424 |
'filearea' => 'post',
|
|
|
2425 |
'itemid' => $discussion2reply1->id,
|
|
|
2426 |
'filepath' => '/',
|
|
|
2427 |
'filename' => $filename,
|
|
|
2428 |
);
|
|
|
2429 |
$fs = get_file_storage();
|
|
|
2430 |
$file2 = $fs->create_file_from_string($filerecordinline, 'image contents (not really)');
|
|
|
2431 |
|
|
|
2432 |
// Following line enrol and assign default role id to the user.
|
|
|
2433 |
// So the user automatically gets mod/forum:viewdiscussion on all forums of the course.
|
|
|
2434 |
$this->getDataGenerator()->enrol_user($user1->id, $course1->id, 'teacher');
|
|
|
2435 |
$this->getDataGenerator()->enrol_user($user2->id, $course1->id);
|
|
|
2436 |
// Changed display period for the discussions in past.
|
|
|
2437 |
$discussion = new \stdClass();
|
|
|
2438 |
$discussion->id = $discussion1->id;
|
|
|
2439 |
$discussion->timestart = $time - 200;
|
|
|
2440 |
$discussion->timeend = $time - 100;
|
|
|
2441 |
$DB->update_record('forum_discussions', $discussion);
|
|
|
2442 |
$discussion = new \stdClass();
|
|
|
2443 |
$discussion->id = $discussion2->id;
|
|
|
2444 |
$discussion->timestart = $time - 200;
|
|
|
2445 |
$discussion->timeend = $time - 100;
|
|
|
2446 |
$DB->update_record('forum_discussions', $discussion);
|
|
|
2447 |
// Create what we expect to be returned when querying the discussion.
|
|
|
2448 |
$expectedposts = array(
|
|
|
2449 |
'discussions' => array(),
|
|
|
2450 |
'warnings' => array(),
|
|
|
2451 |
);
|
|
|
2452 |
|
|
|
2453 |
$isolatedurluser = $urlfactory->get_discussion_view_url_from_discussion_id($discussion1reply1->discussion);
|
|
|
2454 |
$isolatedurluser->params(['parent' => $discussion1reply1->id]);
|
|
|
2455 |
$isolatedurlparent = $urlfactory->get_discussion_view_url_from_discussion_id($discussion1firstpostobject->discussion);
|
|
|
2456 |
$isolatedurlparent->params(['parent' => $discussion1firstpostobject->id]);
|
|
|
2457 |
|
|
|
2458 |
$expectedposts['discussions'][0] = [
|
|
|
2459 |
'name' => $discussion1->name,
|
|
|
2460 |
'id' => $discussion1->id,
|
|
|
2461 |
'timecreated' => $discussion1firstpost->get_time_created(),
|
|
|
2462 |
'authorfullname' => $user1entity->get_full_name(),
|
|
|
2463 |
'posts' => [
|
|
|
2464 |
'userposts' => [
|
|
|
2465 |
[
|
|
|
2466 |
'id' => $discussion1reply1->id,
|
|
|
2467 |
'discussionid' => $discussion1reply1->discussion,
|
|
|
2468 |
'parentid' => $discussion1reply1->parent,
|
|
|
2469 |
'hasparent' => true,
|
|
|
2470 |
'timecreated' => $discussion1reply1->created,
|
|
|
2471 |
'timemodified' => $discussion1reply1->modified,
|
|
|
2472 |
'subject' => $discussion1reply1->subject,
|
|
|
2473 |
'replysubject' => get_string('re', 'mod_forum') . " {$discussion1reply1->subject}",
|
|
|
2474 |
'message' => file_rewrite_pluginfile_urls($discussion1reply1->message, 'pluginfile.php',
|
|
|
2475 |
$forum1context->id, 'mod_forum', 'post', $discussion1reply1->id),
|
|
|
2476 |
'messageformat' => 1, // This value is usually changed by \core_external\util::format_text() function.
|
|
|
2477 |
'unread' => null,
|
|
|
2478 |
'isdeleted' => false,
|
|
|
2479 |
'isprivatereply' => false,
|
|
|
2480 |
'haswordcount' => false,
|
|
|
2481 |
'wordcount' => null,
|
|
|
2482 |
'author' => $exporteduser2,
|
|
|
2483 |
'attachments' => [],
|
|
|
2484 |
'messageinlinefiles' => [],
|
|
|
2485 |
'tags' => [],
|
|
|
2486 |
'html' => [
|
|
|
2487 |
'rating' => null,
|
|
|
2488 |
'taglist' => null,
|
|
|
2489 |
'authorsubheading' => $forumgenerator->get_author_subheading_html(
|
|
|
2490 |
(object)$exporteduser2, $discussion1reply1->created)
|
|
|
2491 |
],
|
|
|
2492 |
'charcount' => null,
|
|
|
2493 |
'capabilities' => [
|
|
|
2494 |
'view' => true,
|
|
|
2495 |
'edit' => true,
|
|
|
2496 |
'delete' => true,
|
|
|
2497 |
'split' => true,
|
|
|
2498 |
'reply' => true,
|
|
|
2499 |
'export' => false,
|
|
|
2500 |
'controlreadstatus' => false,
|
|
|
2501 |
'canreplyprivately' => true,
|
|
|
2502 |
'selfenrol' => false
|
|
|
2503 |
],
|
|
|
2504 |
'urls' => [
|
|
|
2505 |
'view' => $urlfactory->get_view_post_url_from_post_id(
|
|
|
2506 |
$discussion1reply1->discussion, $discussion1reply1->id)->out(false),
|
|
|
2507 |
'viewisolated' => $isolatedurluser->out(false),
|
|
|
2508 |
'viewparent' => $urlfactory->get_view_post_url_from_post_id(
|
|
|
2509 |
$discussion1reply1->discussion, $discussion1reply1->parent)->out(false),
|
|
|
2510 |
'edit' => (new \moodle_url('/mod/forum/post.php', [
|
|
|
2511 |
'edit' => $discussion1reply1->id
|
|
|
2512 |
]))->out(false),
|
|
|
2513 |
'delete' => (new \moodle_url('/mod/forum/post.php', [
|
|
|
2514 |
'delete' => $discussion1reply1->id
|
|
|
2515 |
]))->out(false),
|
|
|
2516 |
'split' => (new \moodle_url('/mod/forum/post.php', [
|
|
|
2517 |
'prune' => $discussion1reply1->id
|
|
|
2518 |
]))->out(false),
|
|
|
2519 |
'reply' => (new \moodle_url('/mod/forum/post.php#mformforum', [
|
|
|
2520 |
'reply' => $discussion1reply1->id
|
|
|
2521 |
]))->out(false),
|
|
|
2522 |
'export' => null,
|
|
|
2523 |
'markasread' => null,
|
|
|
2524 |
'markasunread' => null,
|
|
|
2525 |
'discuss' => $urlfactory->get_discussion_view_url_from_discussion_id(
|
|
|
2526 |
$discussion1reply1->discussion)->out(false),
|
|
|
2527 |
],
|
|
|
2528 |
]
|
|
|
2529 |
],
|
|
|
2530 |
'parentposts' => [
|
|
|
2531 |
[
|
|
|
2532 |
'id' => $discussion1firstpostobject->id,
|
|
|
2533 |
'discussionid' => $discussion1firstpostobject->discussion,
|
|
|
2534 |
'parentid' => null,
|
|
|
2535 |
'hasparent' => false,
|
|
|
2536 |
'timecreated' => $discussion1firstpostobject->created,
|
|
|
2537 |
'timemodified' => $discussion1firstpostobject->modified,
|
|
|
2538 |
'subject' => $discussion1firstpostobject->subject,
|
|
|
2539 |
'replysubject' => get_string('re', 'mod_forum') . " {$discussion1firstpostobject->subject}",
|
|
|
2540 |
'message' => file_rewrite_pluginfile_urls($discussion1firstpostobject->message, 'pluginfile.php',
|
|
|
2541 |
$forum1context->id, 'mod_forum', 'post', $discussion1firstpostobject->id),
|
|
|
2542 |
'messageformat' => 1, // This value is usually changed by \core_external\util::format_text() function.
|
|
|
2543 |
'unread' => null,
|
|
|
2544 |
'isdeleted' => false,
|
|
|
2545 |
'isprivatereply' => false,
|
|
|
2546 |
'haswordcount' => false,
|
|
|
2547 |
'wordcount' => null,
|
|
|
2548 |
'author' => $exporteduser1,
|
|
|
2549 |
'attachments' => [],
|
|
|
2550 |
'messageinlinefiles' => [],
|
|
|
2551 |
'tags' => [],
|
|
|
2552 |
'html' => [
|
|
|
2553 |
'rating' => null,
|
|
|
2554 |
'taglist' => null,
|
|
|
2555 |
'authorsubheading' => $forumgenerator->get_author_subheading_html(
|
|
|
2556 |
(object)$exporteduser1, $discussion1firstpostobject->created)
|
|
|
2557 |
],
|
|
|
2558 |
'charcount' => null,
|
|
|
2559 |
'capabilities' => [
|
|
|
2560 |
'view' => true,
|
|
|
2561 |
'edit' => true,
|
|
|
2562 |
'delete' => true,
|
|
|
2563 |
'split' => false,
|
|
|
2564 |
'reply' => true,
|
|
|
2565 |
'export' => false,
|
|
|
2566 |
'controlreadstatus' => false,
|
|
|
2567 |
'canreplyprivately' => true,
|
|
|
2568 |
'selfenrol' => false
|
|
|
2569 |
],
|
|
|
2570 |
'urls' => [
|
|
|
2571 |
'view' => $urlfactory->get_view_post_url_from_post_id(
|
|
|
2572 |
$discussion1firstpostobject->discussion, $discussion1firstpostobject->id)->out(false),
|
|
|
2573 |
'viewisolated' => $isolatedurlparent->out(false),
|
|
|
2574 |
'viewparent' => null,
|
|
|
2575 |
'edit' => (new \moodle_url('/mod/forum/post.php', [
|
|
|
2576 |
'edit' => $discussion1firstpostobject->id
|
|
|
2577 |
]))->out(false),
|
|
|
2578 |
'delete' => (new \moodle_url('/mod/forum/post.php', [
|
|
|
2579 |
'delete' => $discussion1firstpostobject->id
|
|
|
2580 |
]))->out(false),
|
|
|
2581 |
'split' => null,
|
|
|
2582 |
'reply' => (new \moodle_url('/mod/forum/post.php#mformforum', [
|
|
|
2583 |
'reply' => $discussion1firstpostobject->id
|
|
|
2584 |
]))->out(false),
|
|
|
2585 |
'export' => null,
|
|
|
2586 |
'markasread' => null,
|
|
|
2587 |
'markasunread' => null,
|
|
|
2588 |
'discuss' => $urlfactory->get_discussion_view_url_from_discussion_id(
|
|
|
2589 |
$discussion1firstpostobject->discussion)->out(false),
|
|
|
2590 |
],
|
|
|
2591 |
]
|
|
|
2592 |
],
|
|
|
2593 |
],
|
|
|
2594 |
];
|
|
|
2595 |
|
|
|
2596 |
$isolatedurluser = $urlfactory->get_discussion_view_url_from_discussion_id($discussion2reply1->discussion);
|
|
|
2597 |
$isolatedurluser->params(['parent' => $discussion2reply1->id]);
|
|
|
2598 |
$isolatedurlparent = $urlfactory->get_discussion_view_url_from_discussion_id($discussion2firstpostobject->discussion);
|
|
|
2599 |
$isolatedurlparent->params(['parent' => $discussion2firstpostobject->id]);
|
|
|
2600 |
|
|
|
2601 |
$expectedposts['discussions'][1] = [
|
|
|
2602 |
'name' => $discussion2->name,
|
|
|
2603 |
'id' => $discussion2->id,
|
|
|
2604 |
'timecreated' => $discussion2firstpost->get_time_created(),
|
|
|
2605 |
'authorfullname' => $user1entity->get_full_name(),
|
|
|
2606 |
'posts' => [
|
|
|
2607 |
'userposts' => [
|
|
|
2608 |
[
|
|
|
2609 |
'id' => $discussion2reply1->id,
|
|
|
2610 |
'discussionid' => $discussion2reply1->discussion,
|
|
|
2611 |
'parentid' => $discussion2reply1->parent,
|
|
|
2612 |
'hasparent' => true,
|
|
|
2613 |
'timecreated' => $discussion2reply1->created,
|
|
|
2614 |
'timemodified' => $discussion2reply1->modified,
|
|
|
2615 |
'subject' => $discussion2reply1->subject,
|
|
|
2616 |
'replysubject' => get_string('re', 'mod_forum') . " {$discussion2reply1->subject}",
|
|
|
2617 |
'message' => file_rewrite_pluginfile_urls($discussion2reply1->message, 'pluginfile.php',
|
|
|
2618 |
$forum1context->id, 'mod_forum', 'post', $discussion2reply1->id),
|
|
|
2619 |
'messageformat' => 1, // This value is usually changed by \core_external\util::format_text() function.
|
|
|
2620 |
'unread' => null,
|
|
|
2621 |
'isdeleted' => false,
|
|
|
2622 |
'isprivatereply' => false,
|
|
|
2623 |
'haswordcount' => false,
|
|
|
2624 |
'wordcount' => null,
|
|
|
2625 |
'author' => $exporteduser2,
|
|
|
2626 |
'attachments' => [],
|
|
|
2627 |
'messageinlinefiles' => [],
|
|
|
2628 |
'tags' => [],
|
|
|
2629 |
'html' => [
|
|
|
2630 |
'rating' => null,
|
|
|
2631 |
'taglist' => null,
|
|
|
2632 |
'authorsubheading' => $forumgenerator->get_author_subheading_html(
|
|
|
2633 |
(object)$exporteduser2, $discussion2reply1->created)
|
|
|
2634 |
],
|
|
|
2635 |
'charcount' => null,
|
|
|
2636 |
'capabilities' => [
|
|
|
2637 |
'view' => true,
|
|
|
2638 |
'edit' => true,
|
|
|
2639 |
'delete' => true,
|
|
|
2640 |
'split' => true,
|
|
|
2641 |
'reply' => true,
|
|
|
2642 |
'export' => false,
|
|
|
2643 |
'controlreadstatus' => false,
|
|
|
2644 |
'canreplyprivately' => true,
|
|
|
2645 |
'selfenrol' => false
|
|
|
2646 |
],
|
|
|
2647 |
'urls' => [
|
|
|
2648 |
'view' => $urlfactory->get_view_post_url_from_post_id(
|
|
|
2649 |
$discussion2reply1->discussion, $discussion2reply1->id)->out(false),
|
|
|
2650 |
'viewisolated' => $isolatedurluser->out(false),
|
|
|
2651 |
'viewparent' => $urlfactory->get_view_post_url_from_post_id(
|
|
|
2652 |
$discussion2reply1->discussion, $discussion2reply1->parent)->out(false),
|
|
|
2653 |
'edit' => (new \moodle_url('/mod/forum/post.php', [
|
|
|
2654 |
'edit' => $discussion2reply1->id
|
|
|
2655 |
]))->out(false),
|
|
|
2656 |
'delete' => (new \moodle_url('/mod/forum/post.php', [
|
|
|
2657 |
'delete' => $discussion2reply1->id
|
|
|
2658 |
]))->out(false),
|
|
|
2659 |
'split' => (new \moodle_url('/mod/forum/post.php', [
|
|
|
2660 |
'prune' => $discussion2reply1->id
|
|
|
2661 |
]))->out(false),
|
|
|
2662 |
'reply' => (new \moodle_url('/mod/forum/post.php#mformforum', [
|
|
|
2663 |
'reply' => $discussion2reply1->id
|
|
|
2664 |
]))->out(false),
|
|
|
2665 |
'export' => null,
|
|
|
2666 |
'markasread' => null,
|
|
|
2667 |
'markasunread' => null,
|
|
|
2668 |
'discuss' => $urlfactory->get_discussion_view_url_from_discussion_id(
|
|
|
2669 |
$discussion2reply1->discussion)->out(false),
|
|
|
2670 |
],
|
|
|
2671 |
]
|
|
|
2672 |
],
|
|
|
2673 |
'parentposts' => [
|
|
|
2674 |
[
|
|
|
2675 |
'id' => $discussion2firstpostobject->id,
|
|
|
2676 |
'discussionid' => $discussion2firstpostobject->discussion,
|
|
|
2677 |
'parentid' => null,
|
|
|
2678 |
'hasparent' => false,
|
|
|
2679 |
'timecreated' => $discussion2firstpostobject->created,
|
|
|
2680 |
'timemodified' => $discussion2firstpostobject->modified,
|
|
|
2681 |
'subject' => $discussion2firstpostobject->subject,
|
|
|
2682 |
'replysubject' => get_string('re', 'mod_forum') . " {$discussion2firstpostobject->subject}",
|
|
|
2683 |
'message' => file_rewrite_pluginfile_urls($discussion2firstpostobject->message, 'pluginfile.php',
|
|
|
2684 |
$forum1context->id, 'mod_forum', 'post', $discussion2firstpostobject->id),
|
|
|
2685 |
'messageformat' => 1, // This value is usually changed by \core_external\util::format_text() function.
|
|
|
2686 |
'unread' => null,
|
|
|
2687 |
'isdeleted' => false,
|
|
|
2688 |
'isprivatereply' => false,
|
|
|
2689 |
'haswordcount' => false,
|
|
|
2690 |
'wordcount' => null,
|
|
|
2691 |
'author' => $exporteduser1,
|
|
|
2692 |
'attachments' => [],
|
|
|
2693 |
'messageinlinefiles' => [],
|
|
|
2694 |
'tags' => [],
|
|
|
2695 |
'html' => [
|
|
|
2696 |
'rating' => null,
|
|
|
2697 |
'taglist' => null,
|
|
|
2698 |
'authorsubheading' => $forumgenerator->get_author_subheading_html(
|
|
|
2699 |
(object)$exporteduser1, $discussion2firstpostobject->created)
|
|
|
2700 |
],
|
|
|
2701 |
'charcount' => null,
|
|
|
2702 |
'capabilities' => [
|
|
|
2703 |
'view' => true,
|
|
|
2704 |
'edit' => true,
|
|
|
2705 |
'delete' => true,
|
|
|
2706 |
'split' => false,
|
|
|
2707 |
'reply' => true,
|
|
|
2708 |
'export' => false,
|
|
|
2709 |
'controlreadstatus' => false,
|
|
|
2710 |
'canreplyprivately' => true,
|
|
|
2711 |
'selfenrol' => false
|
|
|
2712 |
],
|
|
|
2713 |
'urls' => [
|
|
|
2714 |
'view' => $urlfactory->get_view_post_url_from_post_id(
|
|
|
2715 |
$discussion2firstpostobject->discussion, $discussion2firstpostobject->id)->out(false),
|
|
|
2716 |
'viewisolated' => $isolatedurlparent->out(false),
|
|
|
2717 |
'viewparent' => null,
|
|
|
2718 |
'edit' => (new \moodle_url('/mod/forum/post.php', [
|
|
|
2719 |
'edit' => $discussion2firstpostobject->id
|
|
|
2720 |
]))->out(false),
|
|
|
2721 |
'delete' => (new \moodle_url('/mod/forum/post.php', [
|
|
|
2722 |
'delete' => $discussion2firstpostobject->id
|
|
|
2723 |
]))->out(false),
|
|
|
2724 |
'split' => null,
|
|
|
2725 |
'reply' => (new \moodle_url('/mod/forum/post.php#mformforum', [
|
|
|
2726 |
'reply' => $discussion2firstpostobject->id
|
|
|
2727 |
]))->out(false),
|
|
|
2728 |
'export' => null,
|
|
|
2729 |
'markasread' => null,
|
|
|
2730 |
'markasunread' => null,
|
|
|
2731 |
'discuss' => $urlfactory->get_discussion_view_url_from_discussion_id(
|
|
|
2732 |
$discussion2firstpostobject->discussion)->out(false),
|
|
|
2733 |
|
|
|
2734 |
]
|
|
|
2735 |
],
|
|
|
2736 |
]
|
|
|
2737 |
],
|
|
|
2738 |
];
|
|
|
2739 |
|
|
|
2740 |
// Test discussions with one additional post each (total 2 posts).
|
|
|
2741 |
// Also testing that we get the parent posts too.
|
|
|
2742 |
$discussions = mod_forum_external::get_discussion_posts_by_userid($user2->id, $forum1->cmid, 'modified', 'DESC');
|
|
|
2743 |
$discussions = external_api::clean_returnvalue(mod_forum_external::get_discussion_posts_by_userid_returns(), $discussions);
|
|
|
2744 |
|
|
|
2745 |
$this->assertEquals(2, count($discussions['discussions']));
|
|
|
2746 |
|
|
|
2747 |
$this->assertEquals($expectedposts, $discussions);
|
|
|
2748 |
|
|
|
2749 |
// When groupmode is SEPARATEGROUPS, even there is no groupid specified, the post not for the user shouldn't be seen.
|
|
|
2750 |
$group1 = self::getDataGenerator()->create_group(['courseid' => $course1->id]);
|
|
|
2751 |
$group2 = self::getDataGenerator()->create_group(['courseid' => $course1->id]);
|
|
|
2752 |
// Update discussion with group.
|
|
|
2753 |
$discussion = new \stdClass();
|
|
|
2754 |
$discussion->id = $discussion1->id;
|
|
|
2755 |
$discussion->groupid = $group1->id;
|
|
|
2756 |
$DB->update_record('forum_discussions', $discussion);
|
|
|
2757 |
$discussion = new \stdClass();
|
|
|
2758 |
$discussion->id = $discussion2->id;
|
|
|
2759 |
$discussion->groupid = $group2->id;
|
|
|
2760 |
$DB->update_record('forum_discussions', $discussion);
|
|
|
2761 |
$cm = get_coursemodule_from_id('forum', $forum1->cmid);
|
|
|
2762 |
$cm->groupmode = SEPARATEGROUPS;
|
|
|
2763 |
$DB->update_record('course_modules', $cm);
|
|
|
2764 |
$teacher = self::getDataGenerator()->create_user();
|
|
|
2765 |
$role = $DB->get_record('role', array('shortname' => 'teacher'), '*', MUST_EXIST);
|
|
|
2766 |
self::getDataGenerator()->enrol_user($teacher->id, $course1->id, $role->id);
|
|
|
2767 |
groups_add_member($group2->id, $teacher->id);
|
|
|
2768 |
self::setUser($teacher);
|
|
|
2769 |
$discussions = mod_forum_external::get_discussion_posts_by_userid($user2->id, $forum1->cmid, 'modified', 'DESC');
|
|
|
2770 |
$discussions = external_api::clean_returnvalue(mod_forum_external::get_discussion_posts_by_userid_returns(), $discussions);
|
|
|
2771 |
// Discussion is only 1 record (group 2).
|
|
|
2772 |
$this->assertEquals(1, count($discussions['discussions']));
|
|
|
2773 |
$this->assertEquals($expectedposts['discussions'][1], $discussions['discussions'][0]);
|
|
|
2774 |
}
|
|
|
2775 |
|
|
|
2776 |
/**
|
|
|
2777 |
* Test get_discussion_post a discussion.
|
|
|
2778 |
*/
|
11 |
efrain |
2779 |
public function test_get_discussion_post_discussion(): void {
|
1 |
efrain |
2780 |
global $DB;
|
|
|
2781 |
$this->resetAfterTest(true);
|
|
|
2782 |
// Setup test data.
|
|
|
2783 |
$course = $this->getDataGenerator()->create_course();
|
|
|
2784 |
$forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
|
|
|
2785 |
$user = $this->getDataGenerator()->create_user();
|
|
|
2786 |
$role = $DB->get_record('role', array('shortname' => 'student'), '*', MUST_EXIST);
|
|
|
2787 |
self::getDataGenerator()->enrol_user($user->id, $course->id, $role->id);
|
|
|
2788 |
// Add a discussion.
|
|
|
2789 |
$record = new \stdClass();
|
|
|
2790 |
$record->course = $course->id;
|
|
|
2791 |
$record->userid = $user->id;
|
|
|
2792 |
$record->forum = $forum->id;
|
|
|
2793 |
$discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
|
|
|
2794 |
$this->setUser($user);
|
|
|
2795 |
$result = mod_forum_external::get_discussion_post($discussion->firstpost);
|
|
|
2796 |
$result = external_api::clean_returnvalue(mod_forum_external::get_discussion_post_returns(), $result);
|
|
|
2797 |
$this->assertEquals($discussion->firstpost, $result['post']['id']);
|
|
|
2798 |
$this->assertFalse($result['post']['hasparent']);
|
|
|
2799 |
$this->assertEquals($discussion->message, $result['post']['message']);
|
|
|
2800 |
}
|
|
|
2801 |
|
|
|
2802 |
/**
|
|
|
2803 |
* Test get_discussion_post a post.
|
|
|
2804 |
*/
|
11 |
efrain |
2805 |
public function test_get_discussion_post_post(): void {
|
1 |
efrain |
2806 |
global $DB;
|
|
|
2807 |
$this->resetAfterTest(true);
|
|
|
2808 |
// Setup test data.
|
|
|
2809 |
$course = $this->getDataGenerator()->create_course();
|
|
|
2810 |
$forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
|
|
|
2811 |
$user = $this->getDataGenerator()->create_user();
|
|
|
2812 |
$role = $DB->get_record('role', array('shortname' => 'student'), '*', MUST_EXIST);
|
|
|
2813 |
self::getDataGenerator()->enrol_user($user->id, $course->id, $role->id);
|
|
|
2814 |
// Add a discussion.
|
|
|
2815 |
$record = new \stdClass();
|
|
|
2816 |
$record->course = $course->id;
|
|
|
2817 |
$record->userid = $user->id;
|
|
|
2818 |
$record->forum = $forum->id;
|
|
|
2819 |
$discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
|
|
|
2820 |
$parentpost = $DB->get_record('forum_posts', array('discussion' => $discussion->id));
|
|
|
2821 |
// Add a post.
|
|
|
2822 |
$record = new \stdClass();
|
|
|
2823 |
$record->course = $course->id;
|
|
|
2824 |
$record->userid = $user->id;
|
|
|
2825 |
$record->forum = $forum->id;
|
|
|
2826 |
$record->discussion = $discussion->id;
|
|
|
2827 |
$record->parent = $parentpost->id;
|
|
|
2828 |
$post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
|
|
|
2829 |
$this->setUser($user);
|
|
|
2830 |
$result = mod_forum_external::get_discussion_post($post->id);
|
|
|
2831 |
$result = external_api::clean_returnvalue(mod_forum_external::get_discussion_post_returns(), $result);
|
|
|
2832 |
$this->assertEquals($post->id, $result['post']['id']);
|
|
|
2833 |
$this->assertTrue($result['post']['hasparent']);
|
|
|
2834 |
$this->assertEquals($post->message, $result['post']['message']);
|
|
|
2835 |
}
|
|
|
2836 |
|
|
|
2837 |
/**
|
|
|
2838 |
* Test get_discussion_post a different user post.
|
|
|
2839 |
*/
|
11 |
efrain |
2840 |
public function test_get_discussion_post_other_user_post(): void {
|
1 |
efrain |
2841 |
global $DB;
|
|
|
2842 |
$this->resetAfterTest(true);
|
|
|
2843 |
// Setup test data.
|
|
|
2844 |
$course = $this->getDataGenerator()->create_course();
|
|
|
2845 |
$forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
|
|
|
2846 |
$user = $this->getDataGenerator()->create_user();
|
|
|
2847 |
$otheruser = $this->getDataGenerator()->create_user();
|
|
|
2848 |
$role = $DB->get_record('role', array('shortname' => 'student'), '*', MUST_EXIST);
|
|
|
2849 |
self::getDataGenerator()->enrol_user($user->id, $course->id, $role->id);
|
|
|
2850 |
self::getDataGenerator()->enrol_user($otheruser->id, $course->id, $role->id);
|
|
|
2851 |
// Add a discussion.
|
|
|
2852 |
$record = array();
|
|
|
2853 |
$record['course'] = $course->id;
|
|
|
2854 |
$record['forum'] = $forum->id;
|
|
|
2855 |
$record['userid'] = $user->id;
|
|
|
2856 |
$discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
|
|
|
2857 |
$parentpost = $DB->get_record('forum_posts', array('discussion' => $discussion->id));
|
|
|
2858 |
// Add a post.
|
|
|
2859 |
$record = new \stdClass();
|
|
|
2860 |
$record->course = $course->id;
|
|
|
2861 |
$record->userid = $user->id;
|
|
|
2862 |
$record->forum = $forum->id;
|
|
|
2863 |
$record->discussion = $discussion->id;
|
|
|
2864 |
$record->parent = $parentpost->id;
|
|
|
2865 |
$post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
|
|
|
2866 |
// Check other user post.
|
|
|
2867 |
$this->setUser($otheruser);
|
|
|
2868 |
$result = mod_forum_external::get_discussion_post($post->id);
|
|
|
2869 |
$result = external_api::clean_returnvalue(mod_forum_external::get_discussion_post_returns(), $result);
|
|
|
2870 |
$this->assertEquals($post->id, $result['post']['id']);
|
|
|
2871 |
$this->assertTrue($result['post']['hasparent']);
|
|
|
2872 |
$this->assertEquals($post->message, $result['post']['message']);
|
|
|
2873 |
}
|
|
|
2874 |
|
|
|
2875 |
/**
|
|
|
2876 |
* Test prepare_draft_area_for_post a different user post.
|
|
|
2877 |
*/
|
11 |
efrain |
2878 |
public function test_prepare_draft_area_for_post(): void {
|
1 |
efrain |
2879 |
global $DB;
|
|
|
2880 |
$this->resetAfterTest(true);
|
|
|
2881 |
// Setup test data.
|
|
|
2882 |
$course = $this->getDataGenerator()->create_course();
|
|
|
2883 |
$forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
|
|
|
2884 |
$user = $this->getDataGenerator()->create_user();
|
|
|
2885 |
$role = $DB->get_record('role', array('shortname' => 'student'), '*', MUST_EXIST);
|
|
|
2886 |
self::getDataGenerator()->enrol_user($user->id, $course->id, $role->id);
|
|
|
2887 |
// Add a discussion.
|
|
|
2888 |
$record = array();
|
|
|
2889 |
$record['course'] = $course->id;
|
|
|
2890 |
$record['forum'] = $forum->id;
|
|
|
2891 |
$record['userid'] = $user->id;
|
|
|
2892 |
$discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
|
|
|
2893 |
$parentpost = $DB->get_record('forum_posts', array('discussion' => $discussion->id));
|
|
|
2894 |
// Add a post.
|
|
|
2895 |
$record = new \stdClass();
|
|
|
2896 |
$record->course = $course->id;
|
|
|
2897 |
$record->userid = $user->id;
|
|
|
2898 |
$record->forum = $forum->id;
|
|
|
2899 |
$record->discussion = $discussion->id;
|
|
|
2900 |
$record->parent = $parentpost->id;
|
|
|
2901 |
$post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
|
|
|
2902 |
|
|
|
2903 |
// Add some files only in the attachment area.
|
|
|
2904 |
$filename = 'faketxt.txt';
|
|
|
2905 |
$filerecordinline = array(
|
|
|
2906 |
'contextid' => \context_module::instance($forum->cmid)->id,
|
|
|
2907 |
'component' => 'mod_forum',
|
|
|
2908 |
'filearea' => 'attachment',
|
|
|
2909 |
'itemid' => $post->id,
|
|
|
2910 |
'filepath' => '/',
|
|
|
2911 |
'filename' => $filename,
|
|
|
2912 |
);
|
|
|
2913 |
$fs = get_file_storage();
|
|
|
2914 |
$fs->create_file_from_string($filerecordinline, 'fake txt contents 1.');
|
|
|
2915 |
$filerecordinline['filename'] = 'otherfaketxt.txt';
|
|
|
2916 |
$fs->create_file_from_string($filerecordinline, 'fake txt contents 2.');
|
|
|
2917 |
|
|
|
2918 |
$this->setUser($user);
|
|
|
2919 |
|
|
|
2920 |
// Check attachment area.
|
|
|
2921 |
$result = mod_forum_external::prepare_draft_area_for_post($post->id, 'attachment');
|
|
|
2922 |
$result = external_api::clean_returnvalue(mod_forum_external::prepare_draft_area_for_post_returns(), $result);
|
|
|
2923 |
$this->assertCount(2, $result['files']);
|
|
|
2924 |
$this->assertEquals($filename, $result['files'][0]['filename']);
|
|
|
2925 |
$this->assertCount(5, $result['areaoptions']);
|
|
|
2926 |
$this->assertEmpty($result['messagetext']);
|
|
|
2927 |
|
|
|
2928 |
// Check again using existing draft item id.
|
|
|
2929 |
$result = mod_forum_external::prepare_draft_area_for_post($post->id, 'attachment', $result['draftitemid']);
|
|
|
2930 |
$result = external_api::clean_returnvalue(mod_forum_external::prepare_draft_area_for_post_returns(), $result);
|
|
|
2931 |
$this->assertCount(2, $result['files']);
|
|
|
2932 |
|
|
|
2933 |
// Keep only certain files in the area.
|
|
|
2934 |
$filestokeep = array(array('filename' => $filename, 'filepath' => '/'));
|
|
|
2935 |
$result = mod_forum_external::prepare_draft_area_for_post($post->id, 'attachment', $result['draftitemid'], $filestokeep);
|
|
|
2936 |
$result = external_api::clean_returnvalue(mod_forum_external::prepare_draft_area_for_post_returns(), $result);
|
|
|
2937 |
$this->assertCount(1, $result['files']);
|
|
|
2938 |
$this->assertEquals($filename, $result['files'][0]['filename']);
|
|
|
2939 |
|
|
|
2940 |
// Check editor (post) area.
|
|
|
2941 |
$filerecordinline['filearea'] = 'post';
|
|
|
2942 |
$filerecordinline['filename'] = 'fakeimage.png';
|
|
|
2943 |
$fs->create_file_from_string($filerecordinline, 'fake image.');
|
|
|
2944 |
$result = mod_forum_external::prepare_draft_area_for_post($post->id, 'post');
|
|
|
2945 |
$result = external_api::clean_returnvalue(mod_forum_external::prepare_draft_area_for_post_returns(), $result);
|
|
|
2946 |
$this->assertCount(1, $result['files']);
|
|
|
2947 |
$this->assertEquals($filerecordinline['filename'], $result['files'][0]['filename']);
|
|
|
2948 |
$this->assertCount(5, $result['areaoptions']);
|
|
|
2949 |
$this->assertEquals($post->message, $result['messagetext']);
|
|
|
2950 |
}
|
|
|
2951 |
|
|
|
2952 |
/**
|
|
|
2953 |
* Test update_discussion_post with a discussion.
|
|
|
2954 |
*/
|
11 |
efrain |
2955 |
public function test_update_discussion_post_discussion(): void {
|
1 |
efrain |
2956 |
global $DB, $USER;
|
|
|
2957 |
$this->resetAfterTest(true);
|
|
|
2958 |
// Setup test data.
|
|
|
2959 |
$course = $this->getDataGenerator()->create_course();
|
|
|
2960 |
$forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
|
|
|
2961 |
|
|
|
2962 |
$this->setAdminUser();
|
|
|
2963 |
|
|
|
2964 |
// Add a discussion.
|
|
|
2965 |
$record = new \stdClass();
|
|
|
2966 |
$record->course = $course->id;
|
|
|
2967 |
$record->userid = $USER->id;
|
|
|
2968 |
$record->forum = $forum->id;
|
|
|
2969 |
$record->pinned = FORUM_DISCUSSION_UNPINNED;
|
|
|
2970 |
$discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
|
|
|
2971 |
|
|
|
2972 |
$subject = 'Hey subject updated';
|
|
|
2973 |
$message = 'Hey message updated';
|
|
|
2974 |
$messageformat = FORMAT_HTML;
|
|
|
2975 |
$options = [
|
|
|
2976 |
['name' => 'pinned', 'value' => true],
|
|
|
2977 |
];
|
|
|
2978 |
|
|
|
2979 |
$result = mod_forum_external::update_discussion_post($discussion->firstpost, $subject, $message, $messageformat,
|
|
|
2980 |
$options);
|
|
|
2981 |
$result = external_api::clean_returnvalue(mod_forum_external::update_discussion_post_returns(), $result);
|
|
|
2982 |
$this->assertTrue($result['status']);
|
|
|
2983 |
|
|
|
2984 |
// Get the post from WS.
|
|
|
2985 |
$result = mod_forum_external::get_discussion_post($discussion->firstpost);
|
|
|
2986 |
$result = external_api::clean_returnvalue(mod_forum_external::get_discussion_post_returns(), $result);
|
|
|
2987 |
$this->assertEquals($subject, $result['post']['subject']);
|
|
|
2988 |
$this->assertEquals($message, $result['post']['message']);
|
|
|
2989 |
$this->assertEquals($messageformat, $result['post']['messageformat']);
|
|
|
2990 |
|
|
|
2991 |
// Get discussion object from DB.
|
|
|
2992 |
$discussion = $DB->get_record('forum_discussions', ['id' => $discussion->id]);
|
|
|
2993 |
$this->assertEquals($subject, $discussion->name); // Check discussion subject.
|
|
|
2994 |
$this->assertEquals(FORUM_DISCUSSION_PINNED, $discussion->pinned); // Check discussion pinned.
|
|
|
2995 |
}
|
|
|
2996 |
|
|
|
2997 |
/**
|
|
|
2998 |
* Test update_discussion_post with a post.
|
|
|
2999 |
*/
|
11 |
efrain |
3000 |
public function test_update_discussion_post_post(): void {
|
1 |
efrain |
3001 |
global $DB, $USER;
|
|
|
3002 |
$this->resetAfterTest(true);
|
|
|
3003 |
// Setup test data.
|
|
|
3004 |
$course = $this->getDataGenerator()->create_course();
|
|
|
3005 |
$forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
|
|
|
3006 |
$cm = get_coursemodule_from_id('forum', $forum->cmid, 0, false, MUST_EXIST);
|
|
|
3007 |
$user = $this->getDataGenerator()->create_user();
|
|
|
3008 |
$role = $DB->get_record('role', array('shortname' => 'student'), '*', MUST_EXIST);
|
|
|
3009 |
self::getDataGenerator()->enrol_user($user->id, $course->id, $role->id);
|
|
|
3010 |
|
|
|
3011 |
$this->setUser($user);
|
|
|
3012 |
// Enable auto subscribe discussion.
|
|
|
3013 |
$USER->autosubscribe = true;
|
|
|
3014 |
|
|
|
3015 |
// Add a discussion.
|
|
|
3016 |
$record = new \stdClass();
|
|
|
3017 |
$record->course = $course->id;
|
|
|
3018 |
$record->userid = $user->id;
|
|
|
3019 |
$record->forum = $forum->id;
|
|
|
3020 |
$discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
|
|
|
3021 |
|
|
|
3022 |
// Add a post via WS (so discussion subscription works).
|
|
|
3023 |
$result = mod_forum_external::add_discussion_post($discussion->firstpost, 'some subject', 'some text here...');
|
|
|
3024 |
$newpost = $result['post'];
|
|
|
3025 |
$this->assertTrue(\mod_forum\subscriptions::is_subscribed($user->id, $forum, $discussion->id, $cm));
|
|
|
3026 |
|
|
|
3027 |
// Test inline and regular attachment in post
|
|
|
3028 |
// Create a file in a draft area for inline attachments.
|
|
|
3029 |
$draftidinlineattach = file_get_unused_draft_itemid();
|
|
|
3030 |
$draftidattach = file_get_unused_draft_itemid();
|
|
|
3031 |
self::setUser($user);
|
|
|
3032 |
$usercontext = \context_user::instance($user->id);
|
|
|
3033 |
$filepath = '/';
|
|
|
3034 |
$filearea = 'draft';
|
|
|
3035 |
$component = 'user';
|
|
|
3036 |
$filenameimg = 'fakeimage.png';
|
|
|
3037 |
$filerecordinline = array(
|
|
|
3038 |
'contextid' => $usercontext->id,
|
|
|
3039 |
'component' => $component,
|
|
|
3040 |
'filearea' => $filearea,
|
|
|
3041 |
'itemid' => $draftidinlineattach,
|
|
|
3042 |
'filepath' => $filepath,
|
|
|
3043 |
'filename' => $filenameimg,
|
|
|
3044 |
);
|
|
|
3045 |
$fs = get_file_storage();
|
|
|
3046 |
|
|
|
3047 |
// Create a file in a draft area for regular attachments.
|
|
|
3048 |
$filerecordattach = $filerecordinline;
|
|
|
3049 |
$attachfilename = 'faketxt.txt';
|
|
|
3050 |
$filerecordattach['filename'] = $attachfilename;
|
|
|
3051 |
$filerecordattach['itemid'] = $draftidattach;
|
|
|
3052 |
$fs->create_file_from_string($filerecordinline, 'image contents (not really)');
|
|
|
3053 |
$fs->create_file_from_string($filerecordattach, 'simple text attachment');
|
|
|
3054 |
|
|
|
3055 |
// Do not update subject.
|
|
|
3056 |
$message = 'Hey message updated';
|
|
|
3057 |
$messageformat = FORMAT_HTML;
|
|
|
3058 |
$options = [
|
|
|
3059 |
['name' => 'discussionsubscribe', 'value' => false],
|
|
|
3060 |
['name' => 'inlineattachmentsid', 'value' => $draftidinlineattach],
|
|
|
3061 |
['name' => 'attachmentsid', 'value' => $draftidattach],
|
|
|
3062 |
];
|
|
|
3063 |
|
|
|
3064 |
$result = mod_forum_external::update_discussion_post($newpost->id, '', $message, $messageformat, $options);
|
|
|
3065 |
$result = external_api::clean_returnvalue(mod_forum_external::update_discussion_post_returns(), $result);
|
|
|
3066 |
$this->assertTrue($result['status']);
|
|
|
3067 |
// Check subscription status.
|
|
|
3068 |
$this->assertFalse(\mod_forum\subscriptions::is_subscribed($user->id, $forum, $discussion->id, $cm));
|
|
|
3069 |
|
|
|
3070 |
// Get the post from WS.
|
|
|
3071 |
$result = mod_forum_external::get_discussion_posts($discussion->id, 'modified', 'DESC', true);
|
|
|
3072 |
$result = external_api::clean_returnvalue(mod_forum_external::get_discussion_posts_returns(), $result);
|
|
|
3073 |
$found = false;
|
|
|
3074 |
foreach ($result['posts'] as $post) {
|
|
|
3075 |
if ($post['id'] == $newpost->id) {
|
|
|
3076 |
$this->assertEquals($newpost->subject, $post['subject']);
|
|
|
3077 |
$this->assertEquals($message, $post['message']);
|
|
|
3078 |
$this->assertEquals($messageformat, $post['messageformat']);
|
|
|
3079 |
$this->assertCount(1, $post['messageinlinefiles']);
|
|
|
3080 |
$this->assertEquals('fakeimage.png', $post['messageinlinefiles'][0]['filename']);
|
|
|
3081 |
$this->assertCount(1, $post['attachments']);
|
|
|
3082 |
$this->assertEquals('faketxt.txt', $post['attachments'][0]['filename']);
|
|
|
3083 |
$found = true;
|
|
|
3084 |
}
|
|
|
3085 |
}
|
|
|
3086 |
$this->assertTrue($found);
|
|
|
3087 |
}
|
|
|
3088 |
|
|
|
3089 |
/**
|
|
|
3090 |
* Test update_discussion_post with other user post (no permissions).
|
|
|
3091 |
*/
|
11 |
efrain |
3092 |
public function test_update_discussion_post_other_user_post(): void {
|
1 |
efrain |
3093 |
global $DB, $USER;
|
|
|
3094 |
$this->resetAfterTest(true);
|
|
|
3095 |
// Setup test data.
|
|
|
3096 |
$course = $this->getDataGenerator()->create_course();
|
|
|
3097 |
$forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
|
|
|
3098 |
$user = $this->getDataGenerator()->create_user();
|
|
|
3099 |
$role = $DB->get_record('role', array('shortname' => 'student'), '*', MUST_EXIST);
|
|
|
3100 |
self::getDataGenerator()->enrol_user($user->id, $course->id, $role->id);
|
|
|
3101 |
|
|
|
3102 |
$this->setAdminUser();
|
|
|
3103 |
// Add a discussion.
|
|
|
3104 |
$record = new \stdClass();
|
|
|
3105 |
$record->course = $course->id;
|
|
|
3106 |
$record->userid = $USER->id;
|
|
|
3107 |
$record->forum = $forum->id;
|
|
|
3108 |
$discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
|
|
|
3109 |
|
|
|
3110 |
// Add a post.
|
|
|
3111 |
$record = new \stdClass();
|
|
|
3112 |
$record->course = $course->id;
|
|
|
3113 |
$record->userid = $USER->id;
|
|
|
3114 |
$record->forum = $forum->id;
|
|
|
3115 |
$record->discussion = $discussion->id;
|
|
|
3116 |
$record->parent = $discussion->firstpost;
|
|
|
3117 |
$newpost = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
|
|
|
3118 |
|
|
|
3119 |
$this->setUser($user);
|
|
|
3120 |
$subject = 'Hey subject updated';
|
|
|
3121 |
$message = 'Hey message updated';
|
|
|
3122 |
$messageformat = FORMAT_HTML;
|
|
|
3123 |
|
|
|
3124 |
$this->expectExceptionMessage(get_string('cannotupdatepost', 'forum'));
|
|
|
3125 |
mod_forum_external::update_discussion_post($newpost->id, $subject, $message, $messageformat);
|
|
|
3126 |
}
|
|
|
3127 |
|
|
|
3128 |
/**
|
|
|
3129 |
* Test that we can update the subject of a post to the string '0'
|
|
|
3130 |
*/
|
|
|
3131 |
public function test_update_discussion_post_set_subject_to_zero(): void {
|
|
|
3132 |
global $DB, $USER;
|
|
|
3133 |
|
|
|
3134 |
$this->resetAfterTest(true);
|
|
|
3135 |
$this->setAdminUser();
|
|
|
3136 |
|
|
|
3137 |
// Setup test data.
|
|
|
3138 |
$course = $this->getDataGenerator()->create_course();
|
|
|
3139 |
$forum = $this->getDataGenerator()->create_module('forum', ['course' => $course->id]);
|
|
|
3140 |
|
|
|
3141 |
$discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion((object) [
|
|
|
3142 |
'userid' => $USER->id,
|
|
|
3143 |
'course' => $course->id,
|
|
|
3144 |
'forum' => $forum->id,
|
|
|
3145 |
'name' => 'Test discussion subject',
|
|
|
3146 |
]);
|
|
|
3147 |
|
|
|
3148 |
// Update discussion post subject.
|
|
|
3149 |
$result = external_api::clean_returnvalue(
|
|
|
3150 |
mod_forum_external::update_discussion_post_returns(),
|
|
|
3151 |
mod_forum_external::update_discussion_post($discussion->firstpost, '0')
|
|
|
3152 |
);
|
|
|
3153 |
$this->assertTrue($result['status']);
|
|
|
3154 |
|
|
|
3155 |
// Get updated discussion post subject from DB.
|
|
|
3156 |
$postsubject = $DB->get_field('forum_posts', 'subject', ['id' => $discussion->firstpost]);
|
|
|
3157 |
$this->assertEquals('0', $postsubject);
|
|
|
3158 |
}
|
|
|
3159 |
|
|
|
3160 |
/**
|
|
|
3161 |
* Test that we can update the message of a post to the string '0'
|
|
|
3162 |
*/
|
|
|
3163 |
public function test_update_discussion_post_set_message_to_zero(): void {
|
|
|
3164 |
global $DB, $USER;
|
|
|
3165 |
|
|
|
3166 |
$this->resetAfterTest(true);
|
|
|
3167 |
$this->setAdminUser();
|
|
|
3168 |
|
|
|
3169 |
// Setup test data.
|
|
|
3170 |
$course = $this->getDataGenerator()->create_course();
|
|
|
3171 |
$forum = $this->getDataGenerator()->create_module('forum', ['course' => $course->id]);
|
|
|
3172 |
|
|
|
3173 |
$discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion((object) [
|
|
|
3174 |
'userid' => $USER->id,
|
|
|
3175 |
'course' => $course->id,
|
|
|
3176 |
'forum' => $forum->id,
|
|
|
3177 |
'message' => 'Test discussion message',
|
|
|
3178 |
'messageformat' => FORMAT_HTML,
|
|
|
3179 |
]);
|
|
|
3180 |
|
|
|
3181 |
// Update discussion post message.
|
|
|
3182 |
$result = external_api::clean_returnvalue(
|
|
|
3183 |
mod_forum_external::update_discussion_post_returns(),
|
|
|
3184 |
mod_forum_external::update_discussion_post($discussion->firstpost, '', '0', FORMAT_HTML)
|
|
|
3185 |
);
|
|
|
3186 |
$this->assertTrue($result['status']);
|
|
|
3187 |
|
|
|
3188 |
// Get updated discussion post subject from DB.
|
|
|
3189 |
$postmessage = $DB->get_field('forum_posts', 'message', ['id' => $discussion->firstpost]);
|
|
|
3190 |
$this->assertEquals('0', $postmessage);
|
|
|
3191 |
}
|
|
|
3192 |
|
|
|
3193 |
/**
|
|
|
3194 |
* Test that we can update the message format of a post to {@see FORMAT_MOODLE}
|
|
|
3195 |
*/
|
|
|
3196 |
public function test_update_discussion_post_set_message_format_moodle(): void {
|
|
|
3197 |
global $DB, $USER;
|
|
|
3198 |
|
|
|
3199 |
$this->resetAfterTest(true);
|
|
|
3200 |
$this->setAdminUser();
|
|
|
3201 |
|
|
|
3202 |
// Setup test data.
|
|
|
3203 |
$course = $this->getDataGenerator()->create_course();
|
|
|
3204 |
$forum = $this->getDataGenerator()->create_module('forum', ['course' => $course->id]);
|
|
|
3205 |
|
|
|
3206 |
$discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion((object) [
|
|
|
3207 |
'userid' => $USER->id,
|
|
|
3208 |
'course' => $course->id,
|
|
|
3209 |
'forum' => $forum->id,
|
|
|
3210 |
'message' => 'Test discussion message',
|
|
|
3211 |
'messageformat' => FORMAT_HTML,
|
|
|
3212 |
]);
|
|
|
3213 |
|
|
|
3214 |
// Update discussion post message & messageformat.
|
|
|
3215 |
$result = external_api::clean_returnvalue(
|
|
|
3216 |
mod_forum_external::update_discussion_post_returns(),
|
|
|
3217 |
mod_forum_external::update_discussion_post($discussion->firstpost, '', 'Update discussion message', FORMAT_MOODLE)
|
|
|
3218 |
);
|
|
|
3219 |
$this->assertTrue($result['status']);
|
|
|
3220 |
|
|
|
3221 |
// Get updated discussion post from DB.
|
|
|
3222 |
$updatedpost = $DB->get_record('forum_posts', ['id' => $discussion->firstpost], 'message,messageformat');
|
|
|
3223 |
$this->assertEquals((object) [
|
|
|
3224 |
'message' => 'Update discussion message',
|
|
|
3225 |
'messageformat' => FORMAT_MOODLE,
|
|
|
3226 |
], $updatedpost);
|
|
|
3227 |
}
|
|
|
3228 |
}
|