MDL-65071 forum: Add unit tests
[moodle.git] / mod / forum / tests / vaults_discussion_list_test.php
CommitLineData
47d38303
RW
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/**
18 * The discussion_list vault tests.
19 *
20 * @package mod_forum
21 * @copyright 2019 Ryan Wyllie <ryan@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25defined('MOODLE_INTERNAL') || die();
26
27require_once(__DIR__ . '/generator_trait.php');
28
29/**
30 * The discussion_list vault tests.
31 *
32 * @package mod_forum
33 * @copyright 2019 Ryan Wyllie <ryan@moodle.com>
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35 */
36class mod_forum_vaults_discussion_list_testcase extends advanced_testcase {
37 // Make use of the test generator trait.
38 use mod_forum_tests_generator_trait;
39
40 /** @var \mod_forum\local\vaults\discussion_list */
41 private $vault;
42
43 /**
44 * Set up function for tests.
45 */
46 public function setUp() {
47 $vaultfactory = \mod_forum\local\container::get_vault_factory();
48 $this->vault = $vaultfactory->get_discussions_in_forum_vault();
49 }
50
51 /**
52 * Test get_from_id.
53 */
54 public function test_get_from_id() {
55 $this->resetAfterTest();
56
57 $datagenerator = $this->getDataGenerator();
58 $vault = $this->vault;
59 $user = $datagenerator->create_user();
60 $course = $datagenerator->create_course();
61 $forum = $datagenerator->create_module('forum', ['course' => $course->id]);
62 [$discussion, $post] = $this->helper_post_to_forum($forum, $user);
63
64 $discussionlist = $vault->get_from_id($discussion->id);
65
66 $this->assertEquals($discussion->id, $discussionlist->get_discussion()->get_id());
67 $this->assertEquals($post->id, $discussionlist->get_first_post()->get_id());
68 $this->assertEquals($user->id, $discussionlist->get_first_post_author()->get_id());
69 $this->assertEquals($user->id, $discussionlist->get_latest_post_author()->get_id());
70 }
71
72 /**
73 * Test get_from_forum_id.
74 */
75 public function test_get_from_forum_id() {
76 $this->resetAfterTest();
77
78 $datagenerator = $this->getDataGenerator();
79 $vault = $this->vault;
80 $user = $datagenerator->create_user();
27ca632f 81 self::setUser($user);
47d38303
RW
82 $course = $datagenerator->create_course();
83 $forum = $datagenerator->create_module('forum', ['course' => $course->id]);
84
3f9ed16e
MG
85 $this->assertEquals([], $vault->get_from_forum_id($forum->id, true, $user->id, null,
86 0, 0, $user));
47d38303
RW
87
88 $now = time();
89 [$discussion1, $post1] = $this->helper_post_to_forum($forum, $user, ['timestart' => $now - 10, 'timemodified' => 1]);
90 [$discussion2, $post2] = $this->helper_post_to_forum($forum, $user, ['timestart' => $now - 9, 'timemodified' => 2]);
91 [$hiddendiscussion, $post3] = $this->helper_post_to_forum($forum, $user, ['timestart' => $now + 10, 'timemodified' => 3]);
3f9ed16e 92 [$discussion3, $post4] = $this->helper_post_to_forum($forum, $user, ['timestart' => $now - 8, 'timemodified' => 4]);
47d38303 93
3f9ed16e
MG
94 // By default orders the discussions by last post.
95 $summaries = array_values($vault->get_from_forum_id($forum->id, false, null, null,
96 0, 0, $user));
47d38303 97 $this->assertCount(3, $summaries);
3f9ed16e 98 $this->assertEquals($discussion3->id, $summaries[0]->get_discussion()->get_id());
47d38303
RW
99 $this->assertEquals($discussion2->id, $summaries[1]->get_discussion()->get_id());
100 $this->assertEquals($discussion1->id, $summaries[2]->get_discussion()->get_id());
101
3f9ed16e
MG
102 $summaries = array_values($vault->get_from_forum_id($forum->id, true, null, null,
103 0, 0, $user));
104 $this->assertCount(4, $summaries);
47d38303 105 $this->assertEquals($hiddendiscussion->id, $summaries[0]->get_discussion()->get_id());
3f9ed16e
MG
106 $this->assertEquals($discussion3->id, $summaries[1]->get_discussion()->get_id());
107 $this->assertEquals($discussion2->id, $summaries[2]->get_discussion()->get_id());
108 $this->assertEquals($discussion1->id, $summaries[3]->get_discussion()->get_id());
47d38303 109
3f9ed16e
MG
110 $summaries = array_values($vault->get_from_forum_id($forum->id, false, $user->id, null,
111 0, 0, $user));
112 $this->assertCount(4, $summaries);
113 $this->assertEquals($hiddendiscussion->id, $summaries[0]->get_discussion()->get_id());
114 $this->assertEquals($discussion3->id, $summaries[1]->get_discussion()->get_id());
115 $this->assertEquals($discussion2->id, $summaries[2]->get_discussion()->get_id());
116 $this->assertEquals($discussion1->id, $summaries[3]->get_discussion()->get_id());
117
118 $summaries = array_values($vault->get_from_forum_id($forum->id, true, null, null,
119 1, 0, $user));
47d38303
RW
120 $this->assertCount(1, $summaries);
121 $this->assertEquals($hiddendiscussion->id, $summaries[0]->get_discussion()->get_id());
122
3f9ed16e
MG
123 $summaries = array_values($vault->get_from_forum_id($forum->id, true, null, null,
124 1, 1, $user));
47d38303 125 $this->assertCount(1, $summaries);
3f9ed16e 126 $this->assertEquals($discussion3->id, $summaries[0]->get_discussion()->get_id());
47d38303 127
3f9ed16e
MG
128 $summaries = array_values($vault->get_from_forum_id($forum->id, true, null, null,
129 1, 2, $user));
47d38303 130 $this->assertCount(1, $summaries);
3f9ed16e
MG
131 $this->assertEquals($discussion2->id, $summaries[0]->get_discussion()->get_id());
132
133 // Create 2 replies for $post1.
134 $this->helper_reply_to_post($post1, $user);
135 $this->helper_reply_to_post($post1, $user);
136 // Create 3 replies for $post2.
137 $this->helper_reply_to_post($post2, $user);
138 $this->helper_reply_to_post($post2, $user);
139 $this->helper_reply_to_post($post2, $user);
140 // Create 1 reply for $post3.
141 $this->helper_reply_to_post($post3, $user);
142
143 // Sort discussions by last post DESC.
144 $summaries = array_values($vault->get_from_forum_id($forum->id, true, null,
145 $vault::SORTORDER_LASTPOST_DESC, 0, 0, $user));
146 $this->assertCount(4, $summaries);
147 $this->assertEquals($hiddendiscussion->id, $summaries[0]->get_discussion()->get_id());
148 $this->assertEquals($discussion2->id, $summaries[1]->get_discussion()->get_id());
149 $this->assertEquals($discussion1->id, $summaries[2]->get_discussion()->get_id());
150 $this->assertEquals($discussion3->id, $summaries[3]->get_discussion()->get_id());
151
152 // Sort discussions by last post ASC.
153 $summaries = array_values($vault->get_from_forum_id($forum->id, true, null,
154 $vault::SORTORDER_LASTPOST_ASC, 0, 0, $user));
155 $this->assertCount(4, $summaries);
156 $this->assertEquals($discussion3->id, $summaries[0]->get_discussion()->get_id());
157 $this->assertEquals($discussion1->id, $summaries[1]->get_discussion()->get_id());
158 $this->assertEquals($discussion2->id, $summaries[2]->get_discussion()->get_id());
159 $this->assertEquals($hiddendiscussion->id, $summaries[3]->get_discussion()->get_id());
160
161 // Sort discussions by replies DESC.
162 $summaries = array_values($vault->get_from_forum_id($forum->id, true, null,
163 $vault::SORTORDER_REPLIES_DESC, 0, 0, $user));
164 $this->assertCount(4, $summaries);
165 $this->assertEquals($discussion2->id, $summaries[0]->get_discussion()->get_id());
166 $this->assertEquals($discussion1->id, $summaries[1]->get_discussion()->get_id());
167 $this->assertEquals($hiddendiscussion->id, $summaries[2]->get_discussion()->get_id());
168 $this->assertEquals($discussion3->id, $summaries[3]->get_discussion()->get_id());
169
170 // Sort discussions by replies ASC.
171 $summaries = array_values($vault->get_from_forum_id($forum->id, true, null,
172 $vault::SORTORDER_REPLIES_ASC, 0, 0, $user));
173 $this->assertCount(4, $summaries);
174 $this->assertEquals($discussion3->id, $summaries[0]->get_discussion()->get_id());
175 $this->assertEquals($hiddendiscussion->id, $summaries[1]->get_discussion()->get_id());
176 $this->assertEquals($discussion1->id, $summaries[2]->get_discussion()->get_id());
177 $this->assertEquals($discussion2->id, $summaries[3]->get_discussion()->get_id());
178
179 // Sort discussions by discussion created DESC.
180 $summaries = array_values($vault->get_from_forum_id($forum->id, true, null,
181 $vault::SORTORDER_CREATED_DESC, 0, 0, $user));
182 $this->assertCount(4, $summaries);
183 $this->assertEquals($discussion3->id, $summaries[0]->get_discussion()->get_id());
184 $this->assertEquals($hiddendiscussion->id, $summaries[1]->get_discussion()->get_id());
185 $this->assertEquals($discussion2->id, $summaries[2]->get_discussion()->get_id());
186 $this->assertEquals($discussion1->id, $summaries[3]->get_discussion()->get_id());
187
188 // Sort discussions by discussion created ASC.
189 $summaries = array_values($vault->get_from_forum_id($forum->id, true, null,
190 $vault::SORTORDER_CREATED_ASC, 0, 0, $user));
191 $this->assertCount(4, $summaries);
192 $this->assertEquals($discussion1->id, $summaries[0]->get_discussion()->get_id());
193 $this->assertEquals($discussion2->id, $summaries[1]->get_discussion()->get_id());
194 $this->assertEquals($hiddendiscussion->id, $summaries[2]->get_discussion()->get_id());
195 $this->assertEquals($discussion3->id, $summaries[3]->get_discussion()->get_id());
196
197 // Sort discussions when there is a pinned discussion.
198 $this->pin_discussion($discussion1);
199 $summaries = array_values($vault->get_from_forum_id($forum->id, true, null,
200 $vault::SORTORDER_LASTPOST_ASC, 0, 0, $user));
201 $this->assertCount(4, $summaries);
202 $this->assertEquals($discussion1->id, $summaries[0]->get_discussion()->get_id());
203 $this->assertEquals($discussion3->id, $summaries[1]->get_discussion()->get_id());
204 $this->assertEquals($discussion2->id, $summaries[2]->get_discussion()->get_id());
205 $this->assertEquals($hiddendiscussion->id, $summaries[3]->get_discussion()->get_id());
206
207 $summaries = array_values($vault->get_from_forum_id($forum->id, true, null,
208 $vault::SORTORDER_LASTPOST_DESC, 0, 0, $user));
209 $this->assertCount(4, $summaries);
210 $this->assertEquals($discussion1->id, $summaries[0]->get_discussion()->get_id());
211 $this->assertEquals($hiddendiscussion->id, $summaries[1]->get_discussion()->get_id());
212 $this->assertEquals($discussion2->id, $summaries[2]->get_discussion()->get_id());
213 $this->assertEquals($discussion3->id, $summaries[3]->get_discussion()->get_id());
214
215 $summaries = array_values($vault->get_from_forum_id($forum->id, true, null,
216 $vault::SORTORDER_REPLIES_DESC, 0, 0, $user));
217 $this->assertCount(4, $summaries);
47d38303 218 $this->assertEquals($discussion1->id, $summaries[0]->get_discussion()->get_id());
3f9ed16e
MG
219 $this->assertEquals($discussion2->id, $summaries[1]->get_discussion()->get_id());
220 $this->assertEquals($hiddendiscussion->id, $summaries[2]->get_discussion()->get_id());
221 $this->assertEquals($discussion3->id, $summaries[3]->get_discussion()->get_id());
47d38303
RW
222 }
223
224 /**
225 * Test get_from_forum_id_and_group_id.
226 */
227 public function test_get_from_forum_id_and_group_id() {
228 $this->resetAfterTest();
229
230 $datagenerator = $this->getDataGenerator();
231 $vault = $this->vault;
232 $user = $datagenerator->create_user();
27ca632f 233 self::setUser($user);
47d38303
RW
234 $course = $datagenerator->create_course();
235 $forum = $datagenerator->create_module('forum', ['course' => $course->id]);
236
3f9ed16e
MG
237 $this->assertEquals([], $vault->get_from_forum_id($forum->id, true, true,
238 null, 0, 0, $user));
47d38303
RW
239
240 $now = time();
241 [$discussion1, $post1] = $this->helper_post_to_forum($forum, $user, ['timestart' => $now - 10, 'timemodified' => 1]);
242 [$discussion2, $post2] = $this->helper_post_to_forum($forum, $user, ['timestart' => $now - 9, 'timemodified' => 2]);
243 [$hiddendiscussion, $post3] = $this->helper_post_to_forum($forum, $user, ['timestart' => $now + 10, 'timemodified' => 3]);
244 [$groupdiscussion1, $post4] = $this->helper_post_to_forum(
245 $forum,
246 $user,
247 ['timestart' => $now - 8, 'timemodified' => 4, 'groupid' => 1]
248 );
249 [$groupdiscussion2, $post5] = $this->helper_post_to_forum(
250 $forum,
251 $user,
252 ['timestart' => $now - 7, 'timemodified' => 5, 'groupid' => 2]
253 );
254 [$hiddengroupdiscussion, $post6] = $this->helper_post_to_forum(
255 $forum,
256 $user,
257 ['timestart' => $now + 11, 'timemodified' => 6, 'groupid' => 3]
258 );
259
3f9ed16e
MG
260 $summaries = array_values($vault->get_from_forum_id_and_group_id($forum->id, [1, 2, 3], true,
261 null, null, 0, 0, $user));
47d38303
RW
262 $this->assertCount(6, $summaries);
263 $this->assertEquals($hiddengroupdiscussion->id, $summaries[0]->get_discussion()->get_id());
264 $this->assertEquals($hiddendiscussion->id, $summaries[1]->get_discussion()->get_id());
265 $this->assertEquals($groupdiscussion2->id, $summaries[2]->get_discussion()->get_id());
266 $this->assertEquals($groupdiscussion1->id, $summaries[3]->get_discussion()->get_id());
267 $this->assertEquals($discussion2->id, $summaries[4]->get_discussion()->get_id());
268 $this->assertEquals($discussion1->id, $summaries[5]->get_discussion()->get_id());
269
3f9ed16e
MG
270 $summaries = array_values($vault->get_from_forum_id_and_group_id($forum->id, [1, 2, 3], false,
271 $user->id, null, 0, 0, $user));
47d38303
RW
272 $this->assertCount(6, $summaries);
273 $this->assertEquals($hiddengroupdiscussion->id, $summaries[0]->get_discussion()->get_id());
274 $this->assertEquals($hiddendiscussion->id, $summaries[1]->get_discussion()->get_id());
275 $this->assertEquals($groupdiscussion2->id, $summaries[2]->get_discussion()->get_id());
276 $this->assertEquals($groupdiscussion1->id, $summaries[3]->get_discussion()->get_id());
277 $this->assertEquals($discussion2->id, $summaries[4]->get_discussion()->get_id());
278 $this->assertEquals($discussion1->id, $summaries[5]->get_discussion()->get_id());
279
3f9ed16e
MG
280 $summaries = array_values($vault->get_from_forum_id_and_group_id($forum->id, [1, 2, 3], true,
281 null, null, 1, 0, $user));
47d38303
RW
282 $this->assertCount(1, $summaries);
283 $this->assertEquals($hiddengroupdiscussion->id, $summaries[0]->get_discussion()->get_id());
284
3f9ed16e
MG
285 $summaries = array_values($vault->get_from_forum_id_and_group_id($forum->id, [1, 2, 3], true,
286 null, null, 1, 1, $user));
47d38303
RW
287 $this->assertCount(1, $summaries);
288 $this->assertEquals($hiddendiscussion->id, $summaries[0]->get_discussion()->get_id());
289
3f9ed16e
MG
290 $summaries = array_values($vault->get_from_forum_id_and_group_id($forum->id, [1, 2, 3], true,
291 null, null, 1, 2, $user));
47d38303
RW
292 $this->assertCount(1, $summaries);
293 $this->assertEquals($groupdiscussion2->id, $summaries[0]->get_discussion()->get_id());
294
3f9ed16e
MG
295 $summaries = array_values($vault->get_from_forum_id_and_group_id($forum->id, [1, 2, 3], false,
296 null, null, 0, 0, $user));
47d38303
RW
297 $this->assertCount(4, $summaries);
298 $this->assertEquals($groupdiscussion2->id, $summaries[0]->get_discussion()->get_id());
299 $this->assertEquals($groupdiscussion1->id, $summaries[1]->get_discussion()->get_id());
300 $this->assertEquals($discussion2->id, $summaries[2]->get_discussion()->get_id());
301 $this->assertEquals($discussion1->id, $summaries[3]->get_discussion()->get_id());
302
3f9ed16e
MG
303 $summaries = array_values($vault->get_from_forum_id_and_group_id($forum->id, [], true,
304 null, null, 0, 0, $user));
47d38303
RW
305 $this->assertCount(3, $summaries);
306 $this->assertEquals($hiddendiscussion->id, $summaries[0]->get_discussion()->get_id());
307 $this->assertEquals($discussion2->id, $summaries[1]->get_discussion()->get_id());
308 $this->assertEquals($discussion1->id, $summaries[2]->get_discussion()->get_id());
47d38303 309
3f9ed16e
MG
310 // Add 4 replies to $post1.
311 $this->helper_reply_to_post($post1, $user);
312 $this->helper_reply_to_post($post1, $user);
313 $this->helper_reply_to_post($post1, $user);
314 $this->helper_reply_to_post($post1, $user);
315 // Add 5 replies to $post2.
316 $this->helper_reply_to_post($post2, $user);
317 $this->helper_reply_to_post($post2, $user);
318 $this->helper_reply_to_post($post2, $user);
319 $this->helper_reply_to_post($post2, $user);
320 $this->helper_reply_to_post($post2, $user);
321 // Add 3 replies to $post3.
322 $this->helper_reply_to_post($post3, $user);
323 $this->helper_reply_to_post($post3, $user);
324 $this->helper_reply_to_post($post3, $user);
325 // Add 2 replies to $post4.
326 $this->helper_reply_to_post($post4, $user);
327 $this->helper_reply_to_post($post4, $user);
328 // Add 1 reply to $post5.
329 $this->helper_reply_to_post($post5, $user);
330
331 // Sort discussions by last post DESC.
332 $summaries = array_values($vault->get_from_forum_id_and_group_id($forum->id, [1, 2, 3], true,
333 $user->id, $vault::SORTORDER_LASTPOST_DESC, 0, 0, $user));
334 $this->assertCount(6, $summaries);
335 $this->assertEquals($groupdiscussion2->id, $summaries[0]->get_discussion()->get_id());
336 $this->assertEquals($groupdiscussion1->id, $summaries[1]->get_discussion()->get_id());
337 $this->assertEquals($hiddendiscussion->id, $summaries[2]->get_discussion()->get_id());
338 $this->assertEquals($hiddengroupdiscussion->id, $summaries[3]->get_discussion()->get_id());
339 $this->assertEquals($discussion2->id, $summaries[4]->get_discussion()->get_id());
340 $this->assertEquals($discussion1->id, $summaries[5]->get_discussion()->get_id());
341
342 // Sort discussions by last post ASC.
343 $summaries = array_values($vault->get_from_forum_id_and_group_id($forum->id, [1, 2, 3], true,
344 $user->id, $vault::SORTORDER_LASTPOST_ASC, 0, 0, $user));
345 $this->assertCount(6, $summaries);
346 $this->assertEquals($discussion1->id, $summaries[0]->get_discussion()->get_id());
347 $this->assertEquals($discussion2->id, $summaries[1]->get_discussion()->get_id());
348 $this->assertEquals($hiddengroupdiscussion->id, $summaries[2]->get_discussion()->get_id());
349 $this->assertEquals($hiddendiscussion->id, $summaries[3]->get_discussion()->get_id());
350 $this->assertEquals($groupdiscussion1->id, $summaries[4]->get_discussion()->get_id());
351 $this->assertEquals($groupdiscussion2->id, $summaries[5]->get_discussion()->get_id());
352
353 // Sort discussions by replies DESC.
354 $summaries = array_values($vault->get_from_forum_id_and_group_id($forum->id, [1, 2, 3], true,
355 $user->id, $vault::SORTORDER_REPLIES_DESC, 0, 0, $user));
356 $this->assertCount(6, $summaries);
357 $this->assertEquals($discussion2->id, $summaries[0]->get_discussion()->get_id());
358 $this->assertEquals($discussion1->id, $summaries[1]->get_discussion()->get_id());
359 $this->assertEquals($hiddendiscussion->id, $summaries[2]->get_discussion()->get_id());
360 $this->assertEquals($groupdiscussion1->id, $summaries[3]->get_discussion()->get_id());
361 $this->assertEquals($groupdiscussion2->id, $summaries[4]->get_discussion()->get_id());
362 $this->assertEquals($hiddengroupdiscussion->id, $summaries[5]->get_discussion()->get_id());
363
364 // Sort discussions by replies ASC.
365 $summaries = array_values($vault->get_from_forum_id_and_group_id($forum->id, [1, 2, 3], true,
366 $user->id, $vault::SORTORDER_REPLIES_ASC, 0, 0, $user));
367 $this->assertCount(6, $summaries);
368 $this->assertEquals($hiddengroupdiscussion->id, $summaries[0]->get_discussion()->get_id());
369 $this->assertEquals($groupdiscussion2->id, $summaries[1]->get_discussion()->get_id());
370 $this->assertEquals($groupdiscussion1->id, $summaries[2]->get_discussion()->get_id());
371 $this->assertEquals($hiddendiscussion->id, $summaries[3]->get_discussion()->get_id());
372 $this->assertEquals($discussion1->id, $summaries[4]->get_discussion()->get_id());
373 $this->assertEquals($discussion2->id, $summaries[5]->get_discussion()->get_id());
374
375 // Sort discussions by discussion created DESC.
376 $summaries = array_values($vault->get_from_forum_id_and_group_id($forum->id, [1, 2, 3], true,
377 $user->id, $vault::SORTORDER_CREATED_DESC, 0, 0, $user));
378 $this->assertCount(6, $summaries);
379 $this->assertEquals($hiddengroupdiscussion->id, $summaries[0]->get_discussion()->get_id());
380 $this->assertEquals($groupdiscussion2->id, $summaries[1]->get_discussion()->get_id());
381 $this->assertEquals($groupdiscussion1->id, $summaries[2]->get_discussion()->get_id());
382 $this->assertEquals($hiddendiscussion->id, $summaries[3]->get_discussion()->get_id());
383 $this->assertEquals($discussion2->id, $summaries[4]->get_discussion()->get_id());
384 $this->assertEquals($discussion1->id, $summaries[5]->get_discussion()->get_id());
385
386 // Sort discussions by discussion created ASC.
387 $summaries = array_values($vault->get_from_forum_id_and_group_id($forum->id, [1, 2, 3], true,
388 $user->id, $vault::SORTORDER_CREATED_ASC, 0, 0, $user));
389 $this->assertCount(6, $summaries);
390 $this->assertEquals($discussion1->id, $summaries[0]->get_discussion()->get_id());
391 $this->assertEquals($discussion2->id, $summaries[1]->get_discussion()->get_id());
392 $this->assertEquals($hiddendiscussion->id, $summaries[2]->get_discussion()->get_id());
393 $this->assertEquals($groupdiscussion1->id, $summaries[3]->get_discussion()->get_id());
394 $this->assertEquals($groupdiscussion2->id, $summaries[4]->get_discussion()->get_id());
395 $this->assertEquals($hiddengroupdiscussion->id, $summaries[5]->get_discussion()->get_id());
396
397 // Sort discussions when there is a pinned discussion.
398 $this->pin_discussion($discussion1);
399 $this->pin_discussion($hiddendiscussion);
400
401 $summaries = array_values($vault->get_from_forum_id($forum->id, true, null,
402 $vault::SORTORDER_LASTPOST_DESC, 0, 0, $user));
403 $this->assertCount(6, $summaries);
404 $this->assertEquals($hiddendiscussion->id, $summaries[0]->get_discussion()->get_id());
405 $this->assertEquals($discussion1->id, $summaries[1]->get_discussion()->get_id());
406 $this->assertEquals($groupdiscussion2->id, $summaries[2]->get_discussion()->get_id());
407 $this->assertEquals($groupdiscussion1->id, $summaries[3]->get_discussion()->get_id());
408 $this->assertEquals($hiddengroupdiscussion->id, $summaries[4]->get_discussion()->get_id());
409 $this->assertEquals($discussion2->id, $summaries[5]->get_discussion()->get_id());
410
411 $summaries = array_values($vault->get_from_forum_id($forum->id, true, null,
412 $vault::SORTORDER_LASTPOST_ASC, 0, 0, $user));
413 $this->assertCount(6, $summaries);
414 $this->assertEquals($discussion1->id, $summaries[0]->get_discussion()->get_id());
415 $this->assertEquals($hiddendiscussion->id, $summaries[1]->get_discussion()->get_id());
416 $this->assertEquals($discussion2->id, $summaries[2]->get_discussion()->get_id());
417 $this->assertEquals($hiddengroupdiscussion->id, $summaries[3]->get_discussion()->get_id());
418 $this->assertEquals($groupdiscussion1->id, $summaries[4]->get_discussion()->get_id());
419 $this->assertEquals($groupdiscussion2->id, $summaries[5]->get_discussion()->get_id());
420
421 $summaries = array_values($vault->get_from_forum_id($forum->id, true, null,
422 $vault::SORTORDER_REPLIES_DESC, 0, 0, $user));
423 $this->assertCount(6, $summaries);
424 $this->assertEquals($discussion1->id, $summaries[0]->get_discussion()->get_id());
425 $this->assertEquals($hiddendiscussion->id, $summaries[1]->get_discussion()->get_id());
426 $this->assertEquals($discussion2->id, $summaries[2]->get_discussion()->get_id());
427 $this->assertEquals($groupdiscussion1->id, $summaries[3]->get_discussion()->get_id());
428 $this->assertEquals($groupdiscussion2->id, $summaries[4]->get_discussion()->get_id());
429 $this->assertEquals($hiddengroupdiscussion->id, $summaries[5]->get_discussion()->get_id());
430 }
47d38303
RW
431
432 /**
433 * Test get_total_discussion_count_from_forum_id.
434 */
435 public function test_get_total_discussion_count_from_forum_id() {
436 $this->resetAfterTest();
437
438 $datagenerator = $this->getDataGenerator();
439 $vault = $this->vault;
440 $user = $datagenerator->create_user();
441 $course = $datagenerator->create_course();
442 $forum = $datagenerator->create_module('forum', ['course' => $course->id]);
443
3f9ed16e
MG
444 $this->assertEquals(0, $vault->get_total_discussion_count_from_forum_id($forum->id, true,
445 null));
47d38303
RW
446
447 $now = time();
448 [$discussion1, $post1] = $this->helper_post_to_forum($forum, $user, ['timestart' => $now - 10, 'timemodified' => 1]);
449 [$discussion2, $post2] = $this->helper_post_to_forum($forum, $user, ['timestart' => $now - 9, 'timemodified' => 2]);
450 [$hiddendiscussion, $post3] = $this->helper_post_to_forum($forum, $user, ['timestart' => $now + 10, 'timemodified' => 3]);
451
3f9ed16e
MG
452 $this->assertEquals(2, $vault->get_total_discussion_count_from_forum_id($forum->id, false,
453 null));
454 $this->assertEquals(3, $vault->get_total_discussion_count_from_forum_id($forum->id, true,
455 null));
456 $this->assertEquals(3, $vault->get_total_discussion_count_from_forum_id($forum->id, false,
457 $user->id));
47d38303
RW
458 }
459
47d38303
RW
460 /**
461 * Test get_total_discussion_count_from_forum_id_and_group_id.
462 */
463 public function test_get_total_discussion_count_from_forum_id_and_group_id() {
464 $this->resetAfterTest();
465
466 $datagenerator = $this->getDataGenerator();
467 $vault = $this->vault;
468 $user = $datagenerator->create_user();
27ca632f 469 self::setUser($user);
47d38303
RW
470 $course = $datagenerator->create_course();
471 $forum = $datagenerator->create_module('forum', ['course' => $course->id]);
472
3f9ed16e
MG
473 $this->assertEquals([], $vault->get_from_forum_id($forum->id, true, true,
474 null, 0, 0, $user));
47d38303
RW
475
476 $now = time();
477 [$discussion1, $post1] = $this->helper_post_to_forum($forum, $user, ['timestart' => $now - 10, 'timemodified' => 1]);
478 [$discussion2, $post2] = $this->helper_post_to_forum($forum, $user, ['timestart' => $now - 9, 'timemodified' => 2]);
479 [$hiddendiscussion, $post3] = $this->helper_post_to_forum($forum, $user, ['timestart' => $now + 10, 'timemodified' => 3]);
480 [$groupdiscussion1, $post4] = $this->helper_post_to_forum(
481 $forum,
482 $user,
483 ['timestart' => $now - 8, 'timemodified' => 4, 'groupid' => 1]
484 );
485 [$groupdiscussion2, $post5] = $this->helper_post_to_forum(
486 $forum,
487 $user,
488 ['timestart' => $now - 7, 'timemodified' => 5, 'groupid' => 2]
489 );
490 [$hiddengroupdiscussion, $post6] = $this->helper_post_to_forum(
491 $forum,
492 $user,
493 ['timestart' => $now + 11, 'timemodified' => 6, 'groupid' => 3]
494 );
495
3f9ed16e
MG
496 $this->assertEquals(6, $vault->get_total_discussion_count_from_forum_id_and_group_id($forum->id, [1, 2, 3],
497 true, null));
47d38303
RW
498 $this->assertEquals(6, $vault->get_total_discussion_count_from_forum_id_and_group_id(
499 $forum->id,
500 [1, 2, 3],
501 false,
502 $user->id
503 ));
3f9ed16e
MG
504 $this->assertEquals(4, $vault->get_total_discussion_count_from_forum_id_and_group_id($forum->id, [1, 2, 3],
505 false, null));
506 $this->assertEquals(3, $vault->get_total_discussion_count_from_forum_id_and_group_id($forum->id, [],
507 true, null));
508 }
509
510 /**
511 * Pin a duscussion.
512 *
513 * @param stdClass $discussion
514 */
515 private function pin_discussion(stdClass $discussion) {
516 global $DB;
517
518 $DB->update_record('forum_discussions',
519 (object) array('id' => $discussion->id, 'pinned' => FORUM_DISCUSSION_PINNED));
47d38303
RW
520 }
521}