Proyectos de Subversion Moodle

Rev

Rev 11 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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 mod_forum_tests_generator_trait;
20
 
21
defined('MOODLE_INTERNAL') || die();
22
 
23
require_once(__DIR__ . '/generator_trait.php');
24
 
25
/**
26
 * The post vault tests.
27
 *
28
 * @package    mod_forum
29
 * @copyright  2019 Ryan Wyllie <ryan@moodle.com>
30
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
31
 * @coversDefaultClass \mod_forum\local\vaults\post
32
 */
1441 ariadna 33
final class vaults_post_test extends \advanced_testcase {
1 efrain 34
    // Make use of the test generator trait.
35
    use mod_forum_tests_generator_trait;
36
 
37
    /** @var \mod_forum\local\vaults\post */
38
    private $vault;
39
 
40
    /**
41
     * Set up function for tests.
42
     */
43
    public function setUp(): void {
1441 ariadna 44
        parent::setUp();
1 efrain 45
        $vaultfactory = \mod_forum\local\container::get_vault_factory();
46
        $this->vault = $vaultfactory->get_post_vault();
47
    }
48
 
49
    /**
50
     * Teardown for all tests.
51
     */
52
    public function tearDown(): void {
53
        unset($this->vault);
1441 ariadna 54
        parent::tearDown();
1 efrain 55
    }
56
 
57
    /**
58
     * Test get_from_id.
59
     */
11 efrain 60
    public function test_get_from_id(): void {
1 efrain 61
        $this->resetAfterTest();
62
 
63
        $datagenerator = $this->getDataGenerator();
64
        $user = $datagenerator->create_user();
65
        $course = $datagenerator->create_course();
66
        $forum = $datagenerator->create_module('forum', ['course' => $course->id]);
67
        [$discussion, $post] = $this->helper_post_to_forum($forum, $user);
68
 
69
        $postentity = $this->vault->get_from_id($post->id);
70
 
71
        $this->assertEquals($post->id, $postentity->get_id());
72
    }
73
 
74
    /**
75
     * Test get_from_discussion_id.
76
     *
77
     * @covers ::get_from_discussion_id
78
     */
11 efrain 79
    public function test_get_from_discussion_id(): void {
1 efrain 80
        $this->resetAfterTest();
81
 
82
        $datagenerator = $this->getDataGenerator();
83
        $user = $datagenerator->create_user();
84
        $course = $datagenerator->create_course();
85
        $forum = $datagenerator->create_module('forum', ['course' => $course->id]);
86
        [$discussion1, $post1] = $this->helper_post_to_forum($forum, $user);
87
        $post2 = $this->helper_reply_to_post($post1, $user);
88
        $post3 = $this->helper_reply_to_post($post1, $user);
89
        [$discussion2, $post4] = $this->helper_post_to_forum($forum, $user);
90
 
91
        $entities = array_values($this->vault->get_from_discussion_id($user, $discussion1->id, false));
92
 
93
        $this->assertCount(3, $entities);
94
        $this->assertEquals($post1->id, $entities[0]->get_id());
95
        $this->assertEquals($post2->id, $entities[1]->get_id());
96
        $this->assertEquals($post3->id, $entities[2]->get_id());
97
 
98
        $entities = array_values($this->vault->get_from_discussion_id($user, $discussion1->id + 1000, false));
99
        $this->assertCount(0, $entities);
100
    }
101
 
102
    /**
103
     * Ensure that selecting posts in a discussion only returns posts that the user can see, when considering private
104
     * replies.
105
     *
106
     * @covers ::get_from_discussion_id
107
     */
11 efrain 108
    public function test_get_from_discussion_id_private_replies(): void {
1 efrain 109
        $this->resetAfterTest();
110
 
111
        $course = $this->getDataGenerator()->create_course();
112
        $forum = $this->getDataGenerator()->create_module('forum', [
113
            'course' => $course->id,
114
        ]);
115
 
116
        [$student, $otherstudent] = $this->helper_create_users($course, 2, 'student');
117
        [$teacher, $otherteacher] = $this->helper_create_users($course, 2, 'teacher');
118
        [$discussion, $post] = $this->helper_post_to_forum($forum, $teacher);
119
        $reply = $this->helper_post_to_discussion($forum, $discussion, $teacher, [
120
                'privatereplyto' => $student->id,
121
            ]);
122
 
123
        // The user is the author.
124
        $entities = array_values($this->vault->get_from_discussion_id($teacher, $discussion->id, true));
125
        $this->assertCount(2, $entities);
126
        $this->assertEquals($post->id, $entities[0]->get_id());
127
        $this->assertEquals($reply->id, $entities[1]->get_id());
128
 
129
        // The user is the intended recipient.
130
        $entities = array_values($this->vault->get_from_discussion_id($student, $discussion->id, false));
131
        $this->assertCount(2, $entities);
132
        $this->assertEquals($post->id, $entities[0]->get_id());
133
        $this->assertEquals($reply->id, $entities[1]->get_id());
134
 
135
        // The user is another teacher..
136
        $entities = array_values($this->vault->get_from_discussion_id($otherteacher, $discussion->id, true));
137
        $this->assertCount(2, $entities);
138
        $this->assertEquals($post->id, $entities[0]->get_id());
139
        $this->assertEquals($reply->id, $entities[1]->get_id());
140
 
141
        // The user is a different student.
142
        $entities = array_values($this->vault->get_from_discussion_id($otherstudent, $discussion->id, false));
143
        $this->assertCount(1, $entities);
144
        $this->assertEquals($post->id, $entities[0]->get_id());
145
    }
146
 
147
    /**
148
     * Test get_from_discussion_ids when no discussion ids were provided.
149
     *
150
     * @covers ::get_from_discussion_ids
151
     */
11 efrain 152
    public function test_get_from_discussion_ids_empty(): void {
1 efrain 153
        $this->resetAfterTest();
154
 
155
        $datagenerator = $this->getDataGenerator();
156
        $user = $datagenerator->create_user();
157
        $course = $datagenerator->create_course();
158
        $forum = $datagenerator->create_module('forum', ['course' => $course->id]);
159
 
160
        $this->assertEquals([], $this->vault->get_from_discussion_ids($user, [], false));
161
    }
162
 
163
    /**
164
     * Test get_from_discussion_ids.
165
     *
166
     * @covers ::get_from_discussion_ids
167
     */
11 efrain 168
    public function test_get_from_discussion_ids(): void {
1 efrain 169
        $this->resetAfterTest();
170
 
171
        $datagenerator = $this->getDataGenerator();
172
        $user = $datagenerator->create_user();
173
        $course = $datagenerator->create_course();
174
        $forum = $datagenerator->create_module('forum', ['course' => $course->id]);
175
        [$discussion1, $post1] = $this->helper_post_to_forum($forum, $user);
176
        $post2 = $this->helper_reply_to_post($post1, $user);
177
        $post3 = $this->helper_reply_to_post($post1, $user);
178
        [$discussion2, $post4] = $this->helper_post_to_forum($forum, $user);
179
 
180
        $entities = $this->vault->get_from_discussion_ids($user, [$discussion1->id], false);
181
        $this->assertCount(3, $entities);
182
        $this->assertArrayHasKey($post1->id, $entities); // Order is not guaranteed, so just verify element existence.
183
        $this->assertArrayHasKey($post2->id, $entities);
184
        $this->assertArrayHasKey($post3->id, $entities);
185
 
186
        $entities = $this->vault->get_from_discussion_ids($user, [$discussion1->id, $discussion2->id], false);
187
        $this->assertCount(4, $entities);
188
        $this->assertArrayHasKey($post1->id, $entities); // Order is not guaranteed, so just verify element existence.
189
        $this->assertArrayHasKey($post2->id, $entities);
190
        $this->assertArrayHasKey($post3->id, $entities);
191
        $this->assertArrayHasKey($post4->id, $entities);
192
 
193
        // Test ordering by id descending.
194
        $entities = $this->vault->get_from_discussion_ids($user, [$discussion1->id, $discussion2->id], false, 'id DESC');
195
        $this->assertEquals($post4->id, array_values($entities)[0]->get_id());
196
        $this->assertEquals($post3->id, array_values($entities)[1]->get_id());
197
        $this->assertEquals($post2->id, array_values($entities)[2]->get_id());
198
        $this->assertEquals($post1->id, array_values($entities)[3]->get_id());
199
 
200
        // Test ordering by id ascending.
201
        $entities = $this->vault->get_from_discussion_ids($user, [$discussion1->id, $discussion2->id], false, 'id ASC');
202
        $this->assertEquals($post1->id, array_values($entities)[0]->get_id());
203
        $this->assertEquals($post2->id, array_values($entities)[1]->get_id());
204
        $this->assertEquals($post3->id, array_values($entities)[2]->get_id());
205
        $this->assertEquals($post4->id, array_values($entities)[3]->get_id());
206
    }
207
 
208
    /**
209
     * Ensure that selecting posts in a discussion only returns posts that the user can see, when considering private
210
     * replies.
211
     *
212
     * @covers ::get_from_discussion_ids
213
     */
11 efrain 214
    public function test_get_from_discussion_ids_private_replies(): void {
1 efrain 215
        $this->resetAfterTest();
216
 
217
        $course = $this->getDataGenerator()->create_course();
218
        $forum = $this->getDataGenerator()->create_module('forum', [
219
            'course' => $course->id,
220
        ]);
221
 
222
        [$student, $otherstudent] = $this->helper_create_users($course, 2, 'student');
223
        [$teacher, $otherteacher] = $this->helper_create_users($course, 2, 'teacher');
224
 
225
        // Create the posts structure below.
226
        // Forum:
227
        // -> Post (student 1)
228
        // ---> Post private reply (teacher 1)
229
        // -> Otherpost (teacher 1)
230
        // ---> Otherpost private reply (teacher 2)
231
        // ---> Otherpost reply (student 1)
232
        // ----> Otherpost reply private reply (teacher 1).
233
        [$discussion, $post] = $this->helper_post_to_forum($forum, $student);
234
        $postprivatereply = $this->helper_reply_to_post($post, $teacher, [
235
            'privatereplyto' => $student->id
236
        ]);
237
        [$otherdiscussion, $otherpost] = $this->helper_post_to_forum($forum, $teacher);
238
        $otherpostprivatereply = $this->helper_reply_to_post($otherpost, $otherteacher, [
239
            'privatereplyto' => $teacher->id,
240
        ]);
241
        $otherpostreply = $this->helper_reply_to_post($otherpost, $student);
242
        $otherpostreplyprivatereply = $this->helper_reply_to_post($otherpostreply, $teacher, [
243
            'privatereplyto' => $student->id
244
        ]);
245
 
246
        // Teacher 1. Request all posts from the vault, telling the vault that the teacher CAN see private replies made by anyone.
247
        $entities = $this->vault->get_from_discussion_ids($teacher, [$discussion->id, $otherdiscussion->id], true);
248
        $this->assertCount(6, $entities);
249
        $this->assertArrayHasKey($post->id, $entities); // Order is not guaranteed, so just verify element existence.
250
        $this->assertArrayHasKey($postprivatereply->id, $entities);
251
        $this->assertArrayHasKey($otherpost->id, $entities);
252
        $this->assertArrayHasKey($otherpostprivatereply->id, $entities);
253
        $this->assertArrayHasKey($otherpostreply->id, $entities);
254
        $this->assertArrayHasKey($otherpostreplyprivatereply->id, $entities);
255
 
256
        // Student 1. Request all posts from the vault, telling the vault that the student CAN'T see private replies made by anyone.
257
        // Teacher2's private reply to otherpost is omitted.
258
        $entities = $this->vault->get_from_discussion_ids($student, [$discussion->id, $otherdiscussion->id], false);
259
        $this->assertCount(5, $entities);
260
        $this->assertArrayHasKey($post->id, $entities); // Order is not guaranteed, so just verify element existence.
261
        $this->assertArrayHasKey($postprivatereply->id, $entities);
262
        $this->assertArrayHasKey($otherpost->id, $entities);
263
        $this->assertArrayHasKey($otherpostreply->id, $entities);
264
        $this->assertArrayHasKey($otherpostreplyprivatereply->id, $entities);
265
 
266
        // Student 1. Request all posts from the vault, telling the vault that student CAN see all private replies made.
267
        // The private reply made by teacher 2 to otherpost is now included.
268
        $entities = $this->vault->get_from_discussion_ids($student, [$discussion->id, $otherdiscussion->id], true);
269
        $this->assertCount(6, $entities);
270
        $this->assertArrayHasKey($post->id, $entities); // Order is not guaranteed, so just verify element existence.
271
        $this->assertArrayHasKey($postprivatereply->id, $entities);
272
        $this->assertArrayHasKey($otherpost->id, $entities);
273
        $this->assertArrayHasKey($otherpostprivatereply->id, $entities);
274
        $this->assertArrayHasKey($otherpostreply->id, $entities);
275
        $this->assertArrayHasKey($otherpostreplyprivatereply->id, $entities);
276
 
277
        // Teacher 2. Request all posts from the vault, telling the vault that teacher2 CAN see all private replies made.
278
        $entities = $this->vault->get_from_discussion_ids($otherteacher, [$discussion->id, $otherdiscussion->id], true);
279
        $this->assertCount(6, $entities);
280
        $this->assertArrayHasKey($post->id, $entities); // Order is not guaranteed, so just verify element existence.
281
        $this->assertArrayHasKey($postprivatereply->id, $entities);
282
        $this->assertArrayHasKey($otherpost->id, $entities);
283
        $this->assertArrayHasKey($otherpostprivatereply->id, $entities);
284
        $this->assertArrayHasKey($otherpostreply->id, $entities);
285
        $this->assertArrayHasKey($otherpostreplyprivatereply->id, $entities);
286
 
287
        // Teacher 2. Request all posts from the vault, telling the vault that teacher2 CANNOT see all private replies made.
288
        // The private replies not relating to teacher 2 directly are omitted.
289
        $entities = $this->vault->get_from_discussion_ids($otherteacher, [$discussion->id, $otherdiscussion->id], false);
290
        $this->assertCount(4, $entities);
291
        $this->assertArrayHasKey($post->id, $entities); // Order is not guaranteed, so just verify element existence.
292
        $this->assertArrayHasKey($otherpost->id, $entities);
293
        $this->assertArrayHasKey($otherpostprivatereply->id, $entities);
294
        $this->assertArrayHasKey($otherpostreply->id, $entities);
295
 
296
        // Student 2. Request all posts from the vault, telling the vault that student2 CAN'T see all private replies made.
297
        // All private replies are omitted, as none relate to student2.
298
        $entities = $this->vault->get_from_discussion_ids($otherstudent, [$discussion->id, $otherdiscussion->id], false);
299
        $this->assertCount(3, $entities);
300
        $this->assertArrayHasKey($post->id, $entities); // Order is not guaranteed, so just verify element existence.
301
        $this->assertArrayHasKey($otherpost->id, $entities);
302
        $this->assertArrayHasKey($otherpostreply->id, $entities);
303
    }
304
 
305
    /**
306
     * Test get_replies_to_post.
307
     *
308
     * @covers ::get_replies_to_post
309
     */
11 efrain 310
    public function test_get_replies_to_post(): void {
1 efrain 311
        $this->resetAfterTest();
312
 
313
        $datagenerator = $this->getDataGenerator();
314
        $forumgenerator = $datagenerator->get_plugin_generator('mod_forum');
315
        $user = $datagenerator->create_user();
316
        $course = $datagenerator->create_course();
317
        $forum = $datagenerator->create_module('forum', ['course' => $course->id]);
318
        [$discussion1, $post1] = $this->helper_post_to_forum($forum, $user);
319
        // Create a post with the same created time as the parent post to ensure
320
        // we've covered every possible scenario.
321
        $post2 = $forumgenerator->create_post((object) [
322
            'discussion' => $post1->discussion,
323
            'parent' => $post1->id,
324
            'userid' => $user->id,
325
            'mailnow' => 1,
326
            'subject' => 'Some subject',
327
            'created' => $post1->created
328
        ]);
329
        $post3 = $this->helper_reply_to_post($post1, $user);
330
        $post4 = $this->helper_reply_to_post($post2, $user);
331
        [$discussion2, $post5] = $this->helper_post_to_forum($forum, $user);
332
 
333
        $entityfactory = \mod_forum\local\container::get_entity_factory();
334
        $post1 = $entityfactory->get_post_from_stdClass($post1);
335
        $post2 = $entityfactory->get_post_from_stdClass($post2);
336
        $post3 = $entityfactory->get_post_from_stdClass($post3);
337
        $post4 = $entityfactory->get_post_from_stdClass($post4);
338
 
339
        $entities = $this->vault->get_replies_to_post($user, $post1, false);
340
        $this->assertCount(3, $entities);
341
        $this->assertEquals($post2->get_id(), $entities[0]->get_id());
342
        $this->assertEquals($post3->get_id(), $entities[1]->get_id());
343
        $this->assertEquals($post4->get_id(), $entities[2]->get_id());
344
 
345
        $entities = $this->vault->get_replies_to_post($user, $post2, false);
346
        $this->assertCount(1, $entities);
347
        $this->assertEquals($post4->get_id(), $entities[0]->get_id());
348
 
349
        $entities = $this->vault->get_replies_to_post($user, $post3, false);
350
        $this->assertCount(0, $entities);
351
    }
352
 
353
    /**
354
     * Test get_replies_to_post with private replies.
355
     *
356
     * @covers ::get_replies_to_post
357
     */
11 efrain 358
    public function test_get_replies_to_post_private_replies(): void {
1 efrain 359
        $this->resetAfterTest();
360
 
361
        $course = $this->getDataGenerator()->create_course();
362
        $forum = $this->getDataGenerator()->create_module('forum', [
363
            'course' => $course->id,
364
        ]);
365
 
366
        // Generate a structure:
367
        // Initial post p [student]
368
        // -> Reply pa [otherstudent]
369
        // ---> Reply paa [student]
370
        // ---> Private Reply pab [teacher]
371
        // -> Private Reply pb [teacher]
372
        // -> Reply pc [otherstudent]
373
        // ---> Reply pca [student]
374
        // -----> Reply pcaa [otherstudent]
375
        // -------> Private Reply pcaaa [teacher].
376
 
377
        [$student, $otherstudent] = $this->helper_create_users($course, 2, 'student');
378
        [$teacher, $otherteacher] = $this->helper_create_users($course, 2, 'teacher');
379
 
380
        [$discussion, $p] = $this->helper_post_to_forum($forum, $student);
381
 
382
        $pa = $this->helper_reply_to_post($p, $otherstudent);
383
        $paa = $this->helper_reply_to_post($pa, $student);
384
        $pab = $this->helper_reply_to_post($pa, $teacher, ['privatereplyto' => $otherstudent->id]);
385
 
386
        $pb = $this->helper_reply_to_post($p, $teacher, ['privatereplyto' => $student->id]);
387
 
388
        $pc = $this->helper_reply_to_post($p, $otherteacher);
389
        $pca = $this->helper_reply_to_post($pc, $student);
390
        $pcaa = $this->helper_reply_to_post($pca, $otherstudent);
391
        $pcaaa = $this->helper_reply_to_post($pcaa, $teacher, ['privatereplyto' => $otherstudent->id]);
392
 
393
        $entityfactory = \mod_forum\local\container::get_entity_factory();
394
        $ep = $entityfactory->get_post_from_stdClass($p);
395
        $epa = $entityfactory->get_post_from_stdClass($pa);
396
        $epaa = $entityfactory->get_post_from_stdClass($paa);
397
        $epab = $entityfactory->get_post_from_stdClass($pab);
398
        $epb = $entityfactory->get_post_from_stdClass($pb);
399
        $epc = $entityfactory->get_post_from_stdClass($pc);
400
        $epca = $entityfactory->get_post_from_stdClass($pca);
401
        $epcaa = $entityfactory->get_post_from_stdClass($pcaa);
402
        $epcaaa = $entityfactory->get_post_from_stdClass($pcaaa);
403
 
404
        // As `student`, you should see all public posts, plus all private replies intended for you.
405
        $entities = $this->vault->get_replies_to_post($student, $ep, false);
406
        $this->assertCount(6, $entities);
407
        $this->assertEquals($epa->get_id(), $entities[0]->get_id());
408
        $this->assertEquals($epaa->get_id(), $entities[1]->get_id());
409
        $this->assertEquals($epb->get_id(), $entities[2]->get_id());
410
        $this->assertEquals($epc->get_id(), $entities[3]->get_id());
411
        $this->assertEquals($epca->get_id(), $entities[4]->get_id());
412
        $this->assertEquals($epcaa->get_id(), $entities[5]->get_id());
413
 
414
        $entities = $this->vault->get_replies_to_post($student, $epa, false);
415
        $this->assertCount(1, $entities);
416
        $this->assertEquals($epaa->get_id(), $entities[0]->get_id());
417
 
418
        $this->assertEmpty($this->vault->get_replies_to_post($student, $epaa, false));
419
        $this->assertEmpty($this->vault->get_replies_to_post($student, $epab, false));
420
        $this->assertEmpty($this->vault->get_replies_to_post($student, $epb, false));
421
        $this->assertEmpty($this->vault->get_replies_to_post($student, $epcaa, false));
422
        $this->assertEmpty($this->vault->get_replies_to_post($student, $epcaaa, false));
423
 
424
        $entities = $this->vault->get_replies_to_post($student, $epc, false);
425
        $this->assertCount(2, $entities);
426
        $this->assertEquals($epca->get_id(), $entities[0]->get_id());
427
        $this->assertEquals($epcaa->get_id(), $entities[1]->get_id());
428
 
429
        // As `otherstudent`, you should see all public posts, plus all private replies intended for you.
430
        $entities = $this->vault->get_replies_to_post($otherstudent, $ep, false);
431
        $this->assertCount(7, $entities);
432
        $this->assertEquals($epa->get_id(), $entities[0]->get_id());
433
        $this->assertEquals($epaa->get_id(), $entities[1]->get_id());
434
        $this->assertEquals($epab->get_id(), $entities[2]->get_id());
435
        $this->assertEquals($epc->get_id(), $entities[3]->get_id());
436
        $this->assertEquals($epca->get_id(), $entities[4]->get_id());
437
        $this->assertEquals($epcaa->get_id(), $entities[5]->get_id());
438
        $this->assertEquals($epcaaa->get_id(), $entities[6]->get_id());
439
 
440
        $entities = $this->vault->get_replies_to_post($otherstudent, $epa, false);
441
        $this->assertCount(2, $entities);
442
        $this->assertEquals($epaa->get_id(), $entities[0]->get_id());
443
        $this->assertEquals($epab->get_id(), $entities[1]->get_id());
444
 
445
        $this->assertEmpty($this->vault->get_replies_to_post($otherstudent, $epaa, false));
446
        $this->assertEmpty($this->vault->get_replies_to_post($otherstudent, $epab, false));
447
        $this->assertEmpty($this->vault->get_replies_to_post($otherstudent, $epb, false));
448
        $this->assertEmpty($this->vault->get_replies_to_post($otherstudent, $epcaaa, false));
449
 
450
        $entities = $this->vault->get_replies_to_post($otherstudent, $epc, false);
451
        $this->assertCount(3, $entities);
452
        $this->assertEquals($epca->get_id(), $entities[0]->get_id());
453
        $this->assertEquals($epcaa->get_id(), $entities[1]->get_id());
454
        $this->assertEquals($epcaaa->get_id(), $entities[2]->get_id());
455
 
456
        // The teacher who authored the private replies can see all.
457
        $entities = $this->vault->get_replies_to_post($teacher, $ep, true);
458
        $this->assertCount(8, $entities);
459
        $this->assertEquals($epa->get_id(), $entities[0]->get_id());
460
        $this->assertEquals($epaa->get_id(), $entities[1]->get_id());
461
        $this->assertEquals($epab->get_id(), $entities[2]->get_id());
462
        $this->assertEquals($epb->get_id(), $entities[3]->get_id());
463
        $this->assertEquals($epc->get_id(), $entities[4]->get_id());
464
        $this->assertEquals($epca->get_id(), $entities[5]->get_id());
465
        $this->assertEquals($epcaa->get_id(), $entities[6]->get_id());
466
        $this->assertEquals($epcaaa->get_id(), $entities[7]->get_id());
467
 
468
        $entities = $this->vault->get_replies_to_post($teacher, $epa, true);
469
        $this->assertCount(2, $entities);
470
        $this->assertEquals($epaa->get_id(), $entities[0]->get_id());
471
        $this->assertEquals($epab->get_id(), $entities[1]->get_id());
472
 
473
        $this->assertEmpty($this->vault->get_replies_to_post($teacher, $epaa, true));
474
        $this->assertEmpty($this->vault->get_replies_to_post($teacher, $epab, true));
475
        $this->assertEmpty($this->vault->get_replies_to_post($teacher, $epb, true));
476
        $this->assertEmpty($this->vault->get_replies_to_post($teacher, $epcaaa, true));
477
 
478
        $entities = $this->vault->get_replies_to_post($teacher, $epc, true);
479
        $this->assertCount(3, $entities);
480
        $this->assertEquals($epca->get_id(), $entities[0]->get_id());
481
        $this->assertEquals($epcaa->get_id(), $entities[1]->get_id());
482
        $this->assertEquals($epcaaa->get_id(), $entities[2]->get_id());
483
 
484
        // Any other teacher can also see all.
485
        $entities = $this->vault->get_replies_to_post($otherteacher, $ep, true);
486
        $this->assertCount(8, $entities);
487
        $this->assertEquals($epa->get_id(), $entities[0]->get_id());
488
        $this->assertEquals($epaa->get_id(), $entities[1]->get_id());
489
        $this->assertEquals($epab->get_id(), $entities[2]->get_id());
490
        $this->assertEquals($epb->get_id(), $entities[3]->get_id());
491
        $this->assertEquals($epc->get_id(), $entities[4]->get_id());
492
        $this->assertEquals($epca->get_id(), $entities[5]->get_id());
493
        $this->assertEquals($epcaa->get_id(), $entities[6]->get_id());
494
        $this->assertEquals($epcaaa->get_id(), $entities[7]->get_id());
495
 
496
        $entities = $this->vault->get_replies_to_post($otherteacher, $epa, true);
497
        $this->assertCount(2, $entities);
498
        $this->assertEquals($epaa->get_id(), $entities[0]->get_id());
499
        $this->assertEquals($epab->get_id(), $entities[1]->get_id());
500
 
501
        $this->assertEmpty($this->vault->get_replies_to_post($otherteacher, $epaa, true));
502
        $this->assertEmpty($this->vault->get_replies_to_post($otherteacher, $epab, true));
503
        $this->assertEmpty($this->vault->get_replies_to_post($otherteacher, $epb, true));
504
        $this->assertEmpty($this->vault->get_replies_to_post($otherteacher, $epcaaa, true));
505
 
506
        $entities = $this->vault->get_replies_to_post($otherteacher, $epc, true);
507
        $this->assertCount(3, $entities);
508
        $this->assertEquals($epca->get_id(), $entities[0]->get_id());
509
        $this->assertEquals($epcaa->get_id(), $entities[1]->get_id());
510
        $this->assertEquals($epcaaa->get_id(), $entities[2]->get_id());
511
    }
512
 
513
    /**
514
     * Test get_reply_count_for_discussion_ids when no discussion ids were provided.
515
     *
516
     * @covers ::get_reply_count_for_discussion_ids
517
     */
11 efrain 518
    public function test_get_reply_count_for_discussion_ids_empty(): void {
1 efrain 519
        $this->resetAfterTest();
520
 
521
        $datagenerator = $this->getDataGenerator();
522
        $user = $datagenerator->create_user();
523
        $course = $datagenerator->create_course();
524
        $forum = $datagenerator->create_module('forum', ['course' => $course->id]);
525
 
526
        $this->assertCount(0, $this->vault->get_reply_count_for_discussion_ids($user, [], false));
527
    }
528
 
529
    /**
530
     * Test get_reply_count_for_discussion_ids.
531
     *
532
     * @covers ::get_reply_count_for_discussion_ids
533
     */
11 efrain 534
    public function test_get_reply_count_for_discussion_ids(): void {
1 efrain 535
        $this->resetAfterTest();
536
 
537
        $datagenerator = $this->getDataGenerator();
538
        $user = $datagenerator->create_user();
539
        $course = $datagenerator->create_course();
540
        $forum = $datagenerator->create_module('forum', ['course' => $course->id]);
541
        [$discussion1, $post1] = $this->helper_post_to_forum($forum, $user);
542
        $post2 = $this->helper_reply_to_post($post1, $user);
543
        $post3 = $this->helper_reply_to_post($post1, $user);
544
        $post4 = $this->helper_reply_to_post($post2, $user);
545
        [$discussion2, $post5] = $this->helper_post_to_forum($forum, $user);
546
        $post6 = $this->helper_reply_to_post($post5, $user);
547
        [$discussion3, $post7] = $this->helper_post_to_forum($forum, $user);
548
 
549
        $counts = $this->vault->get_reply_count_for_discussion_ids($user, [$discussion1->id], false);
550
        $this->assertCount(1, $counts);
551
        $this->assertEquals(3, $counts[$discussion1->id]);
552
 
553
        $counts = $this->vault->get_reply_count_for_discussion_ids($user, [$discussion1->id, $discussion2->id], false);
554
        $this->assertCount(2, $counts);
555
        $this->assertEquals(3, $counts[$discussion1->id]);
556
        $this->assertEquals(1, $counts[$discussion2->id]);
557
 
558
        $counts = $this->vault->get_reply_count_for_discussion_ids($user, [
559
            $discussion1->id,
560
            $discussion2->id,
561
            $discussion3->id
562
        ], false);
563
        $this->assertCount(2, $counts);
564
        $this->assertEquals(3, $counts[$discussion1->id]);
565
        $this->assertEquals(1, $counts[$discussion2->id]);
566
 
567
        $counts = $this->vault->get_reply_count_for_discussion_ids($user, [
568
            $discussion1->id,
569
            $discussion2->id,
570
            $discussion3->id,
571
            $discussion3->id + 1000
572
        ], false);
573
        $this->assertCount(2, $counts);
574
        $this->assertEquals(3, $counts[$discussion1->id]);
575
        $this->assertEquals(1, $counts[$discussion2->id]);
576
    }
577
 
578
    /**
579
     * Test get_reply_count_for_discussion_ids.
580
     *
581
     * @covers ::get_reply_count_for_discussion_ids
582
     */
11 efrain 583
    public function test_get_reply_count_for_discussion_ids_private_replies(): void {
1 efrain 584
        $this->resetAfterTest();
585
 
586
        $course = $this->getDataGenerator()->create_course();
587
        $forum = $this->getDataGenerator()->create_module('forum', [
588
            'course' => $course->id,
589
        ]);
590
 
591
        // Generate a structure:
592
        // Initial post p [student]
593
        // -> Reply pa [otherstudent]
594
        // ---> Reply paa [student]
595
        // ---> Private Reply pab [teacher]
596
        // -> Private Reply pb [teacher]
597
        // -> Reply pc [otherstudent]
598
        // ---> Reply pca [student]
599
        // -----> Reply pcaa [otherstudent]
600
        // -------> Private Reply pcaaa [teacher].
601
 
602
        [$student, $otherstudent] = $this->helper_create_users($course, 2, 'student');
603
        [$teacher, $otherteacher] = $this->helper_create_users($course, 2, 'teacher');
604
 
605
        [$discussion, $p] = $this->helper_post_to_forum($forum, $student);
606
 
607
        $pa = $this->helper_reply_to_post($p, $otherstudent);
608
        $paa = $this->helper_reply_to_post($pa, $student);
609
        $pab = $this->helper_reply_to_post($pa, $teacher, ['privatereplyto' => $otherstudent->id]);
610
 
611
        $pb = $this->helper_reply_to_post($p, $teacher, ['privatereplyto' => $student->id]);
612
 
613
        $pc = $this->helper_reply_to_post($p, $otherteacher);
614
        $pca = $this->helper_reply_to_post($pc, $student);
615
        $pcaa = $this->helper_reply_to_post($pca, $otherstudent);
616
        $pcaaa = $this->helper_reply_to_post($pcaa, $teacher, ['privatereplyto' => $otherstudent->id]);
617
 
618
        $this->assertEquals([$discussion->id => 6],
619
            $this->vault->get_reply_count_for_discussion_ids($student, [$discussion->id], false));
620
        $this->assertEquals([$discussion->id => 7],
621
            $this->vault->get_reply_count_for_discussion_ids($otherstudent, [$discussion->id], false));
622
        $this->assertEquals([$discussion->id => 8],
623
            $this->vault->get_reply_count_for_discussion_ids($teacher, [$discussion->id], true));
624
        $this->assertEquals([$discussion->id => 8],
625
            $this->vault->get_reply_count_for_discussion_ids($otherteacher, [$discussion->id], true));
626
    }
627
 
628
    /**
629
     * Test get_reply_count_for_discussion_id.
630
     *
631
     * @covers ::get_reply_count_for_post_id_in_discussion_id
632
     */
11 efrain 633
    public function test_get_reply_count_for_post_id_in_discussion_id(): void {
1 efrain 634
        $this->resetAfterTest();
635
 
636
        $datagenerator = $this->getDataGenerator();
637
        $user = $datagenerator->create_user();
638
        $course = $datagenerator->create_course();
639
        $forum = $datagenerator->create_module('forum', ['course' => $course->id]);
640
        [$discussion1, $post1] = $this->helper_post_to_forum($forum, $user);
641
        $post2 = $this->helper_reply_to_post($post1, $user);
642
        $post3 = $this->helper_reply_to_post($post1, $user);
643
        $post4 = $this->helper_reply_to_post($post2, $user);
644
        [$discussion2, $post5] = $this->helper_post_to_forum($forum, $user);
645
        $post6 = $this->helper_reply_to_post($post5, $user);
646
        [$discussion3, $post7] = $this->helper_post_to_forum($forum, $user);
647
 
648
        $this->assertEquals(3,
649
            $this->vault->get_reply_count_for_post_id_in_discussion_id($user, $post1->id, $discussion1->id, false));
650
        $this->assertEquals(1,
651
            $this->vault->get_reply_count_for_post_id_in_discussion_id($user, $post5->id, $discussion2->id, false));
652
        $this->assertEquals(0,
653
            $this->vault->get_reply_count_for_post_id_in_discussion_id($user, $post7->id, $discussion3->id, false));
654
        $this->assertEquals(0,
655
            $this->vault->get_reply_count_for_post_id_in_discussion_id($user, $post7->id + 1000, $discussion3->id, false));
656
    }
657
 
658
    /**
659
     * Test get_reply_count_for_post_id_in_discussion_id.
660
     *
661
     * @covers ::get_reply_count_for_post_id_in_discussion_id
662
     */
11 efrain 663
    public function test_get_reply_count_for_post_id_in_discussion_id_private_replies(): void {
1 efrain 664
        $this->resetAfterTest();
665
 
666
        $course = $this->getDataGenerator()->create_course();
667
        $forum = $this->getDataGenerator()->create_module('forum', [
668
            'course' => $course->id,
669
        ]);
670
 
671
        // Generate a structure:
672
        // Initial post p [student]
673
        // -> Reply pa [otherstudent]
674
        // ---> Reply paa [student]
675
        // ---> Private Reply pab [teacher]
676
        // -> Private Reply pb [teacher]
677
        // -> Reply pc [otherstudent]
678
        // ---> Reply pca [student]
679
        // -----> Reply pcaa [otherstudent]
680
        // -------> Private Reply pcaaa [teacher].
681
 
682
        [$student, $otherstudent] = $this->helper_create_users($course, 2, 'student');
683
        [$teacher, $otherteacher] = $this->helper_create_users($course, 2, 'teacher');
684
 
685
        [$discussion, $p] = $this->helper_post_to_forum($forum, $student);
686
 
687
        $pa = $this->helper_reply_to_post($p, $otherstudent);
688
        $paa = $this->helper_reply_to_post($pa, $student);
689
        $pab = $this->helper_reply_to_post($pa, $teacher, ['privatereplyto' => $otherstudent->id]);
690
 
691
        $pb = $this->helper_reply_to_post($p, $teacher, ['privatereplyto' => $student->id]);
692
 
693
        $pc = $this->helper_reply_to_post($p, $otherteacher);
694
        $pca = $this->helper_reply_to_post($pc, $student);
695
        $pcaa = $this->helper_reply_to_post($pca, $otherstudent);
696
        $pcaaa = $this->helper_reply_to_post($pcaa, $teacher, ['privatereplyto' => $otherstudent->id]);
697
 
698
        $this->assertEquals(6,
699
            $this->vault->get_reply_count_for_post_id_in_discussion_id($student, $p->id, $discussion->id, false));
700
        $this->assertEquals(7,
701
            $this->vault->get_reply_count_for_post_id_in_discussion_id($otherstudent, $p->id, $discussion->id, false));
702
        $this->assertEquals(8,
703
            $this->vault->get_reply_count_for_post_id_in_discussion_id($teacher, $p->id, $discussion->id, true));
704
        $this->assertEquals(8,
705
            $this->vault->get_reply_count_for_post_id_in_discussion_id($otherteacher, $p->id, $discussion->id, true));
706
    }
707
 
708
    /**
709
     * Test get_unread_count_for_discussion_ids.
710
     *
711
     * @covers ::get_unread_count_for_discussion_ids
712
     */
11 efrain 713
    public function test_get_unread_count_for_discussion_ids(): void {
1 efrain 714
        global $CFG;
715
        $this->resetAfterTest();
716
 
717
        $datagenerator = $this->getDataGenerator();
718
        $user = $datagenerator->create_user();
719
        $otheruser = $datagenerator->create_user();
720
        $course = $datagenerator->create_course();
721
        $forum = $datagenerator->create_module('forum', ['course' => $course->id]);
722
        [$discussion1, $post1] = $this->helper_post_to_forum($forum, $user);
723
        $post2 = $this->helper_reply_to_post($post1, $user);
724
        $post3 = $this->helper_reply_to_post($post1, $user);
725
        $post4 = $this->helper_reply_to_post($post2, $user);
726
        [$discussion2, $post5] = $this->helper_post_to_forum($forum, $user);
727
        $post6 = $this->helper_reply_to_post($post5, $user);
728
 
729
        $modgenerator = $this->getDataGenerator()->get_plugin_generator('mod_forum');
730
        $post7 = $modgenerator->create_post((object) [
731
            'discussion' => $post5->discussion,
732
            'parent' => $post5->id,
733
            'userid' => $user->id,
734
            'mailnow' => 1,
735
            'subject' => 'old post',
736
            // Two days ago which makes it an "old post".
737
            'modified' => time() - 172800
738
        ]);
739
 
740
        forum_tp_add_read_record($user->id, $post1->id);
741
        forum_tp_add_read_record($user->id, $post4->id);
742
        $CFG->forum_oldpostdays = 1;
743
 
744
        $counts = $this->vault->get_unread_count_for_discussion_ids($user, [$discussion1->id], false);
745
        $this->assertCount(1, $counts);
746
        $this->assertEquals(2, $counts[$discussion1->id]);
747
 
748
        $counts = $this->vault->get_unread_count_for_discussion_ids($user, [$discussion1->id, $discussion2->id], false);
749
        $this->assertCount(2, $counts);
750
        $this->assertEquals(2, $counts[$discussion1->id]);
751
        $this->assertEquals(2, $counts[$discussion2->id]);
752
 
753
        $counts = $this->vault->get_unread_count_for_discussion_ids($user, [
754
            $discussion1->id,
755
            $discussion2->id,
756
            $discussion2->id + 1000
757
        ], false);
758
        $this->assertCount(2, $counts);
759
        $this->assertEquals(2, $counts[$discussion1->id]);
760
        $this->assertEquals(2, $counts[$discussion2->id]);
761
 
762
        $counts = $this->vault->get_unread_count_for_discussion_ids($otheruser, [$discussion1->id, $discussion2->id], false);
763
        $this->assertCount(2, $counts);
764
        $this->assertEquals(4, $counts[$discussion1->id]);
765
        $this->assertEquals(2, $counts[$discussion2->id]);
766
    }
767
 
768
    /**
769
     * Test get_unread_count_for_discussion_ids when no discussion ids were provided.
770
     *
771
     * @covers ::get_unread_count_for_discussion_ids
772
     */
11 efrain 773
    public function test_get_unread_count_for_discussion_ids_empty(): void {
1 efrain 774
        $this->resetAfterTest();
775
 
776
        $datagenerator = $this->getDataGenerator();
777
        $user = $datagenerator->create_user();
778
        $course = $datagenerator->create_course();
779
        $forum = $datagenerator->create_module('forum', ['course' => $course->id]);
780
 
781
        $this->assertEquals([], $this->vault->get_unread_count_for_discussion_ids($user, [], false));
782
    }
783
 
784
    /**
785
     * Test get_latest_posts_for_discussion_ids.
786
     *
787
     * @covers ::get_latest_posts_for_discussion_ids
788
     */
11 efrain 789
    public function test_get_latest_posts_for_discussion_ids(): void {
1 efrain 790
        $this->resetAfterTest();
791
 
792
        $datagenerator = $this->getDataGenerator();
793
        $course = $datagenerator->create_course();
794
        [$teacher, $otherteacher] = $this->helper_create_users($course, 2, 'teacher');
795
        [$user, $user2] = $this->helper_create_users($course, 2, 'student');
1441 ariadna 796
        $now = time();
1 efrain 797
        $forum = $datagenerator->create_module('forum', ['course' => $course->id]);
798
        [$discussion1, $post1] = $this->helper_post_to_forum($forum, $user);
1441 ariadna 799
        $post2 = $this->helper_reply_to_post($post1, $user, ['created' => $now + 100]);
800
        $post3 = $this->helper_reply_to_post($post1, $user, ['created' => $now + 101]);
801
        $post4 = $this->helper_reply_to_post($post2, $user, ['created' => $now + 102]);
1 efrain 802
        [$discussion2, $post5] = $this->helper_post_to_forum($forum, $user);
803
        $post6 = $this->helper_reply_to_post($post5, $user);
1441 ariadna 804
        $post7 = $this->helper_reply_to_post($post5, $user);
805
        $post8 = $this->helper_reply_to_post($post6, $user);
806
        [$discussion3, $post9] = $this->helper_post_to_forum($forum, $user);
807
        $post10 = $this->helper_post_to_discussion($forum, $discussion3, $teacher, [
1 efrain 808
            'privatereplyto' => $user->id,
809
        ]);
810
 
811
        $ids = $this->vault->get_latest_posts_for_discussion_ids($user, [$discussion1->id], false);
812
        $this->assertCount(1, $ids);
813
        $this->assertEquals($post4->id, $ids[$discussion1->id]->get_id());
814
 
815
        $ids = $this->vault->get_latest_posts_for_discussion_ids($user,
816
            [$discussion1->id, $discussion2->id], false);
817
        $this->assertCount(2, $ids);
818
        $this->assertEquals($post4->id, $ids[$discussion1->id]->get_id());
1441 ariadna 819
        $this->assertEquals($post8->id, $ids[$discussion2->id]->get_id());
1 efrain 820
 
821
        $ids = $this->vault->get_latest_posts_for_discussion_ids($user,
822
            [$discussion1->id, $discussion2->id, $discussion3->id], false);
823
        $this->assertCount(3, $ids);
824
        $this->assertEquals($post4->id, $ids[$discussion1->id]->get_id());
1441 ariadna 825
        $this->assertEquals($post8->id, $ids[$discussion2->id]->get_id());
826
        $this->assertEquals($post10->id, $ids[$discussion3->id]->get_id());
1 efrain 827
 
828
        // Checks the user who doesn't have access to the private reply.
829
        $ids = $this->vault->get_latest_posts_for_discussion_ids($user2,
830
            [$discussion1->id, $discussion2->id, $discussion3->id], false);
831
        $this->assertCount(3, $ids);
832
        $this->assertEquals($post4->id, $ids[$discussion1->id]->get_id());
1441 ariadna 833
        $this->assertEquals($post8->id, $ids[$discussion2->id]->get_id());
834
        $this->assertEquals($post9->id, $ids[$discussion3->id]->get_id());
1 efrain 835
 
836
        // Checks the user with the private reply to.
837
        $ids = $this->vault->get_latest_posts_for_discussion_ids($user, [
838
            $discussion1->id,
839
            $discussion2->id,
840
            $discussion3->id,
841
            $discussion3->id + 1000
842
        ], false);
843
        $this->assertCount(3, $ids);
844
        $this->assertEquals($post4->id, $ids[$discussion1->id]->get_id());
1441 ariadna 845
        $this->assertEquals($post8->id, $ids[$discussion2->id]->get_id());
846
        $this->assertEquals($post10->id, $ids[$discussion3->id]->get_id());
1 efrain 847
    }
848
 
849
    /**
850
     * Test get_latest_posts_for_discussion_ids when no discussion ids were provided.
851
     *
852
     * @covers ::get_latest_posts_for_discussion_ids
853
     */
11 efrain 854
    public function test_get_latest_posts_for_discussion_ids_empty(): void {
1 efrain 855
        $this->resetAfterTest();
856
 
857
        $datagenerator = $this->getDataGenerator();
858
        $user = $datagenerator->create_user();
859
        $course = $datagenerator->create_course();
860
        $forum = $datagenerator->create_module('forum', ['course' => $course->id]);
861
 
862
        $this->assertEquals([], $this->vault->get_latest_posts_for_discussion_ids($user, [], false));
863
    }
864
 
865
    /**
866
     * Test get_first_post_for_discussion_ids.
867
     *
868
     * @covers ::get_first_post_for_discussion_ids
869
     */
11 efrain 870
    public function test_get_first_post_for_discussion_ids(): void {
1 efrain 871
        $this->resetAfterTest();
872
 
873
        $datagenerator = $this->getDataGenerator();
874
        $user = $datagenerator->create_user();
875
        $course = $datagenerator->create_course();
876
        $forum = $datagenerator->create_module('forum', ['course' => $course->id]);
1441 ariadna 877
        $now = time();
1 efrain 878
        [$discussion1, $post1] = $this->helper_post_to_forum($forum, $user);
1441 ariadna 879
        $post2 = $this->helper_reply_to_post($post1, $user, ['created' => $now + 100]);
880
        $post3 = $this->helper_reply_to_post($post1, $user, ['created' => $now + 101]);
881
        $post4 = $this->helper_reply_to_post($post2, $user, ['created' => $now + 102]);
1 efrain 882
        [$discussion2, $post5] = $this->helper_post_to_forum($forum, $user);
883
        $post6 = $this->helper_reply_to_post($post5, $user);
1441 ariadna 884
        $post7 = $this->helper_reply_to_post($post5, $user);
885
        $post8 = $this->helper_reply_to_post($post6, $user);
886
        [$discussion3, $post9] = $this->helper_post_to_forum($forum, $user);
1 efrain 887
 
888
        $firstposts = $this->vault->get_first_post_for_discussion_ids([$discussion1->id]);
889
        $this->assertCount(1, $firstposts);
890
        $this->assertEquals($post1->id, reset($firstposts)->get_id());
891
 
892
        $firstposts = $this->vault->get_first_post_for_discussion_ids([$discussion1->id, $discussion2->id]);
893
        $this->assertCount(2, $firstposts);
894
        $this->assertEquals($post1->id, $firstposts[$post1->id]->get_id());
895
        $this->assertEquals($post5->id, $firstposts[$post5->id]->get_id());
896
 
897
        $firstposts = $this->vault->get_first_post_for_discussion_ids([$discussion1->id, $discussion2->id, $discussion3->id]);
898
        $this->assertCount(3, $firstposts);
899
        $this->assertEquals($post1->id, $firstposts[$post1->id]->get_id());
900
        $this->assertEquals($post5->id, $firstposts[$post5->id]->get_id());
1441 ariadna 901
        $this->assertEquals($post9->id, $firstposts[$post9->id]->get_id());
1 efrain 902
 
903
        $firstposts = $this->vault->get_first_post_for_discussion_ids([
904
            $discussion1->id,
905
            $discussion2->id,
906
            $discussion3->id,
907
            $discussion3->id + 1000
908
        ]);
909
        $this->assertCount(3, $firstposts);
910
        $this->assertEquals($post1->id, $firstposts[$post1->id]->get_id());
911
        $this->assertEquals($post5->id, $firstposts[$post5->id]->get_id());
1441 ariadna 912
        $this->assertEquals($post9->id, $firstposts[$post9->id]->get_id());
1 efrain 913
    }
914
 
915
    /**
916
     * Test get_first_post_for_discussion_ids when no discussion ids were provided.
917
     *
918
     * @covers ::get_first_post_for_discussion_ids
919
     */
11 efrain 920
    public function test_get_first_post_for_discussion_ids_empty(): void {
1 efrain 921
        $this->resetAfterTest();
922
 
923
        $datagenerator = $this->getDataGenerator();
924
        $user = $datagenerator->create_user();
925
        $course = $datagenerator->create_course();
926
        $forum = $datagenerator->create_module('forum', ['course' => $course->id]);
927
 
928
        $this->assertEquals([], $this->vault->get_first_post_for_discussion_ids([]));
929
    }
930
 
931
    /**
932
     * Test get_from_filters.
933
     *
934
     * @covers ::get_from_filters
935
     */
11 efrain 936
    public function test_get_from_filters(): void {
1 efrain 937
        $this->resetAfterTest();
938
 
939
        $datagenerator = $this->getDataGenerator();
940
        $course = $datagenerator->create_course();
941
        [$user, $user2] = $this->helper_create_users($course, 2, 'student');
942
        $forum = $datagenerator->create_module('forum', ['course' => $course->id]);
943
 
944
        [$discussion1, $post1] = $this->helper_post_to_forum($forum, $user);
945
        $post2 = $this->helper_reply_to_post($post1, $user);
946
        $post3 = $this->helper_reply_to_post($post1, $user);
947
 
948
        [$discussion2, $post4] = $this->helper_post_to_forum($forum, $user);
949
        $discussionids = [$discussion1->id, $discussion2->id];
950
 
951
        $userids = [$user->id];
952
        $entities = array_values($this->vault->get_from_filters($user,
953
            ['discussionids' => $discussionids, 'userids' => $userids],
954
            true,
955
            'id ASC'));
956
 
957
        $this->assertCount(4, $entities);
958
        $this->assertEquals($post1->id, $entities[0]->get_id());
959
        $this->assertEquals($post2->id, $entities[1]->get_id());
960
        $this->assertEquals($post3->id, $entities[2]->get_id());
961
        $this->assertEquals($post4->id, $entities[3]->get_id());
962
 
963
        $entities = $this->vault->get_from_filters($user, ['discussionids' => $discussion1->id, 'userids' => $userids],
964
                false);
965
        $this->assertCount(3, $entities);
966
        $this->assertArrayHasKey($post1->id, $entities);
967
        $this->assertArrayHasKey($post2->id, $entities);
968
        $this->assertArrayHasKey($post3->id, $entities);
969
 
970
        $discussionids = [$discussion1->id, $discussion2->id];
971
        $userids = [$user->id, $user2->id];
972
        $entities = $this->vault->get_from_filters($user, ['discussionids' => $discussionids, 'userids' => $userids],
973
                false);
974
        $this->assertCount(4, $entities);
975
        $this->assertArrayHasKey($post1->id, $entities);
976
        $this->assertArrayHasKey($post2->id, $entities);
977
        $this->assertArrayHasKey($post3->id, $entities);
978
        $this->assertArrayHasKey($post4->id, $entities);
979
 
980
        // Test ordering by id descending.
981
        $entities = $this->vault->get_from_filters($user, ['discussionids' => $discussionids, 'userids' => $user->id],
982
                false, 'id DESC');
983
        $this->assertEquals($post4->id, array_values($entities)[0]->get_id());
984
        $this->assertEquals($post3->id, array_values($entities)[1]->get_id());
985
        $this->assertEquals($post2->id, array_values($entities)[2]->get_id());
986
        $this->assertEquals($post1->id, array_values($entities)[3]->get_id());
987
 
988
        // Test ordering by id ascending.
989
        $entities = $this->vault->get_from_filters($user, ['discussionids' => $discussionids, 'userids' => $user->id],
990
            false, 'id ASC');
991
        $this->assertEquals($post1->id, array_values($entities)[0]->get_id());
992
        $this->assertEquals($post2->id, array_values($entities)[1]->get_id());
993
        $this->assertEquals($post3->id, array_values($entities)[2]->get_id());
994
        $this->assertEquals($post4->id, array_values($entities)[3]->get_id());
995
    }
996
 
11 efrain 997
    public function test_get_from_filters_from_to_dates(): void {
1 efrain 998
        $this->resetAfterTest();
999
 
1000
        $datagenerator = $this->getDataGenerator();
1001
        $course = $datagenerator->create_course();
1002
        [$user, $user2] = $this->helper_create_users($course, 2, 'student');
1003
        $forum = $datagenerator->create_module('forum', ['course' => $course->id]);
1004
 
1005
        [$discussion1, $post1] = $this->helper_post_to_forum($forum, $user);
1006
 
1007
        $date = new \DateTime('2019-07-05');
1008
        $post2 = $this->helper_reply_to_post($post1, $user, ['created' => $date->getTimestamp()]);
1009
        $post3 = $this->helper_reply_to_post($post1, $user, ['created' => $date->getTimestamp()]);
1010
        $date->modify('+1 month');
1011
        $post4 = $this->helper_reply_to_post($post1, $user, ['created' => $date->getTimestamp()]);
1012
        $post5 = $this->helper_reply_to_post($post1, $user, ['created' => $date->getTimestamp()]);
1013
        $post6 = $this->helper_reply_to_post($post1, $user, ['created' => $date->getTimestamp()]);
1014
 
1015
        [$discussion2, $post4] = $this->helper_post_to_forum($forum, $user);
1016
 
1017
        $datefilter = new \DateTime('2019-07-01');
1018
        $filters = ['from' => $datefilter->getTimestamp()];
1019
        $entities = $this->vault->get_from_filters($user, $filters, false);
1020
        $this->assertCount(7, $entities);
1021
 
1022
        $filters['to'] = $datefilter->modify('+1 month')->getTimestamp();
1023
        $entities = $this->vault->get_from_filters($user, $filters, false);
1024
        $this->assertCount(2, $entities);
1025
    }
1026
 
1027
    /**
1028
     * Test get_from_filters when no discussion ids were provided.
1029
     *
1030
     * @covers ::get_from_filters
1031
     */
11 efrain 1032
    public function test_get_from_filters_empty(): void {
1 efrain 1033
        $this->resetAfterTest();
1034
 
1035
        $datagenerator = $this->getDataGenerator();
1036
        $course = $datagenerator->create_course();
1037
        [$student1] = $this->helper_create_users($course, 1, 'student');
1038
        $forum = $datagenerator->create_module('forum', ['course' => $course->id]);
1039
        $this->helper_post_to_forum($forum, $student1);
1040
        $this->assertEquals([], $this->vault->get_from_filters($student1, [], false));
1041
    }
1042
 
1043
    /**
1044
     * Ensure that selecting posts in a discussion only returns posts that the user can see, when considering private
1045
     * replies.
1046
     *
1047
     * @covers ::get_from_filters
1048
     */
11 efrain 1049
    public function test_get_from_filters_private_replies(): void {
1 efrain 1050
        $this->resetAfterTest();
1051
 
1052
        $course = $this->getDataGenerator()->create_course();
1053
        $forum = $this->getDataGenerator()->create_module('forum', [
1054
            'course' => $course->id,
1055
        ]);
1056
 
1057
        [$student, $otherstudent] = $this->helper_create_users($course, 2, 'student');
1058
        [$teacher, $otherteacher] = $this->helper_create_users($course, 2, 'teacher');
1059
 
1060
        // Create the posts structure below.
1061
        // Forum:
1062
        // -> Post (student 1)
1063
        // ---> Post private reply (teacher 1)
1064
        // -> Otherpost (teacher 1)
1065
        // ---> Otherpost private reply (teacher 2)
1066
        // ---> Otherpost reply (student 1)
1067
        // ----> Otherpost reply private reply (teacher 1).
1068
        [$discussion, $post] = $this->helper_post_to_forum($forum, $student);
1069
        $postprivatereply = $this->helper_reply_to_post($post, $teacher, [
1070
            'privatereplyto' => $student->id
1071
        ]);
1072
        [$otherdiscussion, $otherpost] = $this->helper_post_to_forum($forum, $teacher);
1073
        $otherpostprivatereply = $this->helper_reply_to_post($otherpost, $otherteacher, [
1074
            'privatereplyto' => $teacher->id,
1075
        ]);
1076
        $otherpostreply = $this->helper_reply_to_post($otherpost, $student);
1077
        $otherpostreplyprivatereply = $this->helper_reply_to_post($otherpostreply, $teacher, [
1078
            'privatereplyto' => $student->id
1079
        ]);
1080
 
1081
        $userids = [$otherstudent->id, $teacher->id, $otherteacher->id];
1082
        $discussionids = [$discussion->id, $otherdiscussion->id];
1083
 
1084
        // Teacher 1. Request all posts from the vault, telling the vault that the teacher CAN see private replies made by anyone.
1085
        $entities = $this->vault->get_from_filters($teacher, ['discussionids' => $discussionids, 'userids' => $userids],
1086
            true);
1087
        $this->assertCount(4, $entities);
1088
        $this->assertArrayHasKey($postprivatereply->id, $entities);
1089
        $this->assertArrayHasKey($otherpost->id, $entities);
1090
        $this->assertArrayHasKey($otherpostprivatereply->id, $entities);
1091
        $this->assertArrayHasKey($otherpostreplyprivatereply->id, $entities);
1092
 
1093
        // Student 1. Request all posts from the vault, telling the vault that the student CAN'T see private replies made by anyone.
1094
        // Teacher2's private reply to otherpost is omitted.
1095
        $entities = $this->vault->get_from_filters($student, ['discussionids' => $discussionids, 'userids' => $userids],
1096
                false);
1097
        $this->assertCount(3, $entities);
1098
        $this->assertArrayHasKey($postprivatereply->id, $entities);
1099
        $this->assertArrayHasKey($otherpost->id, $entities);
1100
        $this->assertArrayHasKey($otherpostreplyprivatereply->id, $entities);
1101
 
1102
        // Student 1. Request all posts from the vault, telling the vault that student CAN see all private replies made.
1103
        // The private reply made by teacher 2 to otherpost is now included.
1104
        $entities = $this->vault->get_from_filters($student, ['discussionids' => $discussionids, 'userids' => $userids],
1105
                true);
1106
        $this->assertCount(4, $entities);
1107
        $this->assertArrayHasKey($postprivatereply->id, $entities);
1108
        $this->assertArrayHasKey($otherpost->id, $entities);
1109
        $this->assertArrayHasKey($otherpostprivatereply->id, $entities);
1110
        $this->assertArrayHasKey($otherpostreplyprivatereply->id, $entities);
1111
 
1112
        // Teacher 2. Request all posts from the vault, telling the vault that teacher2 CAN see all private replies made.
1113
        $entities = $this->vault->get_from_filters($otherteacher,
1114
                ['discussionids' => $discussionids, 'userids' => $userids], true);
1115
        $this->assertCount(4, $entities);
1116
        $this->assertArrayHasKey($otherpost->id, $entities);
1117
        $this->assertArrayHasKey($otherpostprivatereply->id, $entities);
1118
        $this->assertArrayHasKey($otherpostreplyprivatereply->id, $entities);
1119
 
1120
        // Teacher 2. Request all posts from the vault, telling the vault that teacher2 CANNOT see all private replies made.
1121
        // The private replies not relating to teacher 2 directly are omitted.
1122
        $entities = $this->vault->get_from_filters($otherteacher,
1123
                ['discussionids' => $discussionids, 'userids' => $userids], false);
1124
        $this->assertCount(2, $entities);
1125
        $this->assertArrayHasKey($otherpost->id, $entities);
1126
        $this->assertArrayHasKey($otherpostprivatereply->id, $entities);
1127
 
1128
        // Student 2. Request all posts from the vault, telling the vault that student2 CAN'T see all private replies made.
1129
        // All private replies are omitted, as none relate to student2.
1130
        $entities = $this->vault->get_from_filters($otherstudent,
1131
                ['discussionids' => $discussionids, 'userids' => $userids], false);
1132
        $this->assertCount(1, $entities);
1133
        $this->assertArrayHasKey($otherpost->id, $entities);
1134
 
1135
    }
1136
}