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
 
3
// This file is part of Moodle - http://moodle.org/
4
//
5
// Moodle is free software: you can redistribute it and/or modify
6
// it under the terms of the GNU General Public License as published by
7
// the Free Software Foundation, either version 3 of the License, or
8
// (at your option) any later version.
9
//
10
// Moodle is distributed in the hope that it will be useful,
11
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
// GNU General Public License for more details.
14
//
15
// You should have received a copy of the GNU General Public License
16
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
17
 
18
namespace mod_forum;
19
 
20
use mod_forum_tests_cron_trait;
21
use mod_forum_tests_generator_trait;
22
 
23
defined('MOODLE_INTERNAL') || die;
24
 
25
require_once(__DIR__ . '/cron_trait.php');
26
require_once(__DIR__ . '/generator_trait.php');
27
 
28
/**
29
 * The module forums external functions unit tests
30
 *
31
 * @package    mod_forum
32
 * @category   test
33
 * @copyright  2013 Andrew Nicols
34
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35
 */
1441 ariadna 36
final class maildigest_test extends \advanced_testcase {
1 efrain 37
 
38
    // Make use of the cron tester trait.
39
    use mod_forum_tests_cron_trait;
40
 
41
    // Make use of the test generator trait.
42
    use mod_forum_tests_generator_trait;
43
 
44
    /** @var \phpunit_message_sink */
45
    protected $messagesink;
46
 
47
    /** @var \phpunit_message_sink */
48
    protected $mailsink;
49
 
50
    /**
51
     * Set up message and mail sinks, and set up other requirements for the
52
     * cron to be tested here.
53
     */
54
    public function setUp(): void {
55
        global $CFG;
1441 ariadna 56
        parent::setUp();
1 efrain 57
 
58
        // Messaging is not compatible with transactions...
59
        $this->preventResetByRollback();
60
 
61
        // Catch all messages
62
        $this->messagesink = $this->redirectMessages();
63
        $this->mailsink = $this->redirectEmails();
64
 
65
        // Confirm that we have an empty message sink so far.
66
        $messages = $this->messagesink->get_messages_by_component('mod_forum');
67
        $this->assertEquals(0, count($messages));
68
 
69
        $messages = $this->mailsink->get_messages();
70
        $this->assertEquals(0, count($messages));
71
 
72
        // Tell Moodle that we've not sent any digest messages out recently.
73
        $CFG->digestmailtimelast = 0;
74
 
75
        // And set the digest sending time to a negative number - this has
76
        // the effect of making it 11pm the previous day.
77
        $CFG->digestmailtime = -1;
78
 
79
        // Forcibly reduce the maxeditingtime to a one second to ensure that
80
        // messages are sent out.
81
        $CFG->maxeditingtime = 1;
82
 
83
        // We must clear the subscription caches. This has to be done both before each test, and after in case of other
84
        // tests using these functions.
85
        \mod_forum\subscriptions::reset_forum_cache();
86
        \mod_forum\subscriptions::reset_discussion_cache();
87
    }
88
 
89
    /**
90
     * Clear the message sinks set up in this test.
91
     */
92
    public function tearDown(): void {
93
        $this->messagesink->clear();
94
        $this->messagesink->close();
95
 
96
        $this->mailsink->clear();
97
        $this->mailsink->close();
1441 ariadna 98
        parent::tearDown();
1 efrain 99
    }
100
 
101
    /**
102
     * Setup a user, course, and forums.
103
     *
104
     * @return stdClass containing the list of forums, courses, forumids,
105
     * and the user enrolled in them.
106
     */
107
    protected function helper_setup_user_in_course() {
108
        global $DB;
109
 
110
        $return = new \stdClass();
111
        $return->courses = new \stdClass();
112
        $return->forums = new \stdClass();
113
        $return->forumids = array();
114
 
115
        // Create a user.
116
        $user = $this->getDataGenerator()->create_user();
117
        $return->user = $user;
118
 
119
        // Create courses to add the modules.
120
        $return->courses->course1 = $this->getDataGenerator()->create_course();
121
 
122
        // Create forums.
123
        $record = new \stdClass();
124
        $record->course = $return->courses->course1->id;
125
        $record->forcesubscribe = 1;
126
 
127
        $return->forums->forum1 = $this->getDataGenerator()->create_module('forum', $record);
128
        $return->forumsids[] = $return->forums->forum1->id;
129
 
130
        $return->forums->forum2 = $this->getDataGenerator()->create_module('forum', $record);
131
        $return->forumsids[] = $return->forums->forum2->id;
132
 
133
        // Check the forum was correctly created.
134
        list ($test, $params) = $DB->get_in_or_equal($return->forumsids);
135
 
136
        // Enrol the user in the courses.
137
        // DataGenerator->enrol_user automatically sets a role for the user
138
        $this->getDataGenerator()->enrol_user($return->user->id, $return->courses->course1->id);
139
 
140
        return $return;
141
    }
142
 
11 efrain 143
    public function test_set_maildigest(): void {
1 efrain 144
        global $DB;
145
 
146
        $this->resetAfterTest(true);
147
 
148
        $helper = $this->helper_setup_user_in_course();
149
        $user = $helper->user;
150
        $course1 = $helper->courses->course1;
151
        $forum1 = $helper->forums->forum1;
152
 
153
        // Set to the user.
154
        self::setUser($helper->user);
155
 
156
        // Confirm that there is no current value.
157
        $currentsetting = $DB->get_record('forum_digests', array(
158
            'forum' => $forum1->id,
159
            'userid' => $user->id,
160
        ));
161
        $this->assertFalse($currentsetting);
162
 
163
        // Test with each of the valid values:
164
        // 0, 1, and 2 are valid values.
165
        forum_set_user_maildigest($forum1, 0, $user);
166
        $currentsetting = $DB->get_record('forum_digests', array(
167
            'forum' => $forum1->id,
168
            'userid' => $user->id,
169
        ));
170
        $this->assertEquals($currentsetting->maildigest, 0);
171
 
172
        forum_set_user_maildigest($forum1, 1, $user);
173
        $currentsetting = $DB->get_record('forum_digests', array(
174
            'forum' => $forum1->id,
175
            'userid' => $user->id,
176
        ));
177
        $this->assertEquals($currentsetting->maildigest, 1);
178
 
179
        forum_set_user_maildigest($forum1, 2, $user);
180
        $currentsetting = $DB->get_record('forum_digests', array(
181
            'forum' => $forum1->id,
182
            'userid' => $user->id,
183
        ));
184
        $this->assertEquals($currentsetting->maildigest, 2);
185
 
186
        // And the default value - this should delete the record again
187
        forum_set_user_maildigest($forum1, -1, $user);
188
        $currentsetting = $DB->get_record('forum_digests', array(
189
            'forum' => $forum1->id,
190
            'userid' => $user->id,
191
        ));
192
        $this->assertFalse($currentsetting);
193
 
194
        // Try with an invalid value.
195
        $this->expectException('moodle_exception');
196
        forum_set_user_maildigest($forum1, 42, $user);
197
    }
198
 
11 efrain 199
    public function test_get_user_digest_options_default(): void {
1 efrain 200
        global $USER, $DB;
201
 
202
        $this->resetAfterTest(true);
203
 
204
        // Set up a basic user enrolled in a course.
205
        $helper = $this->helper_setup_user_in_course();
206
        $user = $helper->user;
207
        $course1 = $helper->courses->course1;
208
        $forum1 = $helper->forums->forum1;
209
 
210
        // Set to the user.
211
        self::setUser($helper->user);
212
 
213
        // We test against these options.
214
        $digestoptions = array(
215
            '0' => get_string('emaildigestoffshort', 'mod_forum'),
216
            '1' => get_string('emaildigestcompleteshort', 'mod_forum'),
217
            '2' => get_string('emaildigestsubjectsshort', 'mod_forum'),
218
        );
219
 
220
        // The default settings is 0.
221
        $this->assertEquals(0, $user->maildigest);
222
        $options = forum_get_user_digest_options();
223
        $this->assertEquals($options[-1], get_string('emaildigestdefault', 'mod_forum', $digestoptions[0]));
224
 
225
        // Update the setting to 1.
226
        $USER->maildigest = 1;
227
        $this->assertEquals(1, $USER->maildigest);
228
        $options = forum_get_user_digest_options();
229
        $this->assertEquals($options[-1], get_string('emaildigestdefault', 'mod_forum', $digestoptions[1]));
230
 
231
        // Update the setting to 2.
232
        $USER->maildigest = 2;
233
        $this->assertEquals(2, $USER->maildigest);
234
        $options = forum_get_user_digest_options();
235
        $this->assertEquals($options[-1], get_string('emaildigestdefault', 'mod_forum', $digestoptions[2]));
236
    }
237
 
11 efrain 238
    public function test_get_user_digest_options_sorting(): void {
1 efrain 239
        global $USER, $DB;
240
 
241
        $this->resetAfterTest(true);
242
 
243
        // Set up a basic user enrolled in a course.
244
        $helper = $this->helper_setup_user_in_course();
245
        $user = $helper->user;
246
        $course1 = $helper->courses->course1;
247
        $forum1 = $helper->forums->forum1;
248
 
249
        // Set to the user.
250
        self::setUser($helper->user);
251
 
252
        // Retrieve the list of applicable options.
253
        $options = forum_get_user_digest_options();
254
 
255
        // The default option must always be at the top of the list.
256
        $lastoption = -2;
257
        foreach ($options as $value => $description) {
258
            $this->assertGreaterThan($lastoption, $value);
259
            $lastoption = $value;
260
        }
261
    }
262
 
11 efrain 263
    public function test_cron_no_posts(): void {
1 efrain 264
        global $DB;
265
 
266
        $this->resetAfterTest(true);
267
 
268
        // Initially the forum cron should generate no messages as we've made no posts.
269
        $expect = [];
270
        $this->queue_tasks_and_assert($expect);
271
    }
272
 
273
    /**
274
     * Sends several notifications to one user as:
275
     * * single messages based on a user profile setting.
276
     */
11 efrain 277
    public function test_cron_profile_single_mails(): void {
1 efrain 278
        global $DB;
279
 
280
        $this->resetAfterTest(true);
281
 
282
        // Set up a basic user enrolled in a course.
283
        $userhelper = $this->helper_setup_user_in_course();
284
        $user = $userhelper->user;
285
        $course1 = $userhelper->courses->course1;
286
        $forum1 = $userhelper->forums->forum1;
287
        $forum2 = $userhelper->forums->forum2;
288
 
289
        // Add 5 discussions to forum 1.
290
        $posts = [];
291
        for ($i = 0; $i < 5; $i++) {
292
            list($discussion, $post) = $this->helper_post_to_forum($forum1, $user, ['mailnow' => 1]);
293
            $posts[] = $post;
294
        }
295
 
296
        // Add 5 discussions to forum 2.
297
        for ($i = 0; $i < 5; $i++) {
298
            list($discussion, $post) = $this->helper_post_to_forum($forum2, $user, ['mailnow' => 1]);
299
            $posts[] = $post;
300
        }
301
 
302
        // Set the tested user's default maildigest setting.
303
        $DB->set_field('user', 'maildigest', 0, array('id' => $user->id));
304
 
305
        // Set the maildigest preference for forum1 to default.
306
        forum_set_user_maildigest($forum1, -1, $user);
307
 
308
        // Set the maildigest preference for forum2 to default.
309
        forum_set_user_maildigest($forum2, -1, $user);
310
 
311
        // No digests mails should be sent, but 10 forum mails will be sent.
312
        $expect = [
313
            (object) [
314
                'userid' => $user->id,
315
                'messages' => 10,
316
                'digests' => 0,
317
            ],
318
        ];
319
        $this->queue_tasks_and_assert($expect);
320
 
321
        $this->send_notifications_and_assert($user, $posts);
322
    }
323
 
324
    /**
325
     * Sends several notifications to one user as:
326
     * * daily digests coming from the user profile setting.
327
     */
11 efrain 328
    public function test_cron_profile_digest_email(): void {
1 efrain 329
        global $DB, $CFG;
330
 
331
        $this->resetAfterTest(true);
332
 
333
        // Set up a basic user enrolled in a course.
334
        $userhelper = $this->helper_setup_user_in_course();
335
        $user = $userhelper->user;
336
        $course1 = $userhelper->courses->course1;
337
        $forum1 = $userhelper->forums->forum1;
338
        $forum2 = $userhelper->forums->forum2;
339
        $posts = [];
340
 
341
        // Add 5 discussions to forum 1.
342
        for ($i = 0; $i < 5; $i++) {
343
            list($discussion, $post) = $this->helper_post_to_forum($forum1, $user, ['mailnow' => 1]);
344
            $posts[] = $post;
345
        }
346
 
347
        // Add 5 discussions to forum 2.
348
        for ($i = 0; $i < 5; $i++) {
349
            list($discussion, $post) = $this->helper_post_to_forum($forum2, $user, ['mailnow' => 1]);
350
            $posts[] = $post;
351
        }
352
 
353
        // Set the tested user's default maildigest setting.
354
        $DB->set_field('user', 'maildigest', 1, array('id' => $user->id));
355
 
356
        // Set the maildigest preference for forum1 to default.
357
        forum_set_user_maildigest($forum1, -1, $user);
358
 
359
        // Set the maildigest preference for forum2 to default.
360
        forum_set_user_maildigest($forum2, -1, $user);
361
 
362
        // No digests mails should be sent, but 10 forum mails will be sent.
363
        $expect = [
364
            (object) [
365
                'userid' => $user->id,
366
                'messages' => 0,
367
                'digests' => 1,
368
            ],
369
        ];
370
        $this->queue_tasks_and_assert($expect);
371
 
372
        $this->send_digests_and_assert($user, $posts);
373
    }
374
 
375
    /**
376
     * Send digests to a user who cannot view fullnames
377
     */
11 efrain 378
    public function test_cron_digest_view_fullnames_off(): void {
1 efrain 379
        global $DB, $CFG;
380
 
381
        $CFG->fullnamedisplay = 'lastname';
382
        $this->resetAfterTest(true);
383
 
384
        // Set up a basic user enrolled in a course.
385
        $userhelper = $this->helper_setup_user_in_course();
386
        $user = $userhelper->user;
387
        $course1 = $userhelper->courses->course1;
388
        $forum1 = $userhelper->forums->forum1;
389
        $posts = [];
390
 
391
        // Add 1 discussions to forum 1.
392
        list($discussion, $post) = $this->helper_post_to_forum($forum1, $user, ['mailnow' => 1]);
393
        $posts[] = $post;
394
 
395
        // Set the tested user's default maildigest setting.
396
        $DB->set_field('user', 'maildigest', 1, array('id' => $user->id));
397
 
398
        // No digests mails should be sent, but 1 forum mails will be sent.
399
        $expect = [
400
            (object) [
401
                'userid' => $user->id,
402
                'messages' => 0,
403
                'digests' => 1,
404
            ],
405
        ];
406
        $this->queue_tasks_and_assert($expect);
407
        $this->send_digests_and_assert($user, $posts);
408
 
409
        // The user does not, by default, have permission to view the fullname.
410
        $messages = $this->messagesink->get_messages_by_component('mod_forum');
411
        $messages = reset($messages);
412
        $messagecontent = $messages->fullmessage;
413
 
414
        // Assert that the expected name is present (lastname only).
415
        $this->assertStringContainsString(fullname($user, false), $messagecontent);
416
 
417
        // Assert that the full name is not present (firstname lastname only).
418
        $this->assertStringNotContainsString(fullname($user, true), $messagecontent);
419
    }
420
 
421
    /**
422
     * Send digests to a user who can view fullnames.
423
     */
11 efrain 424
    public function test_cron_digest_view_fullnames_on(): void {
1 efrain 425
        global $DB, $CFG;
426
 
427
        $CFG->fullnamedisplay = 'lastname';
428
        $this->resetAfterTest(true);
429
 
430
        // Set up a basic user enrolled in a course.
431
        $userhelper = $this->helper_setup_user_in_course();
432
        $user = $userhelper->user;
433
        $course1 = $userhelper->courses->course1;
434
        $forum1 = $userhelper->forums->forum1;
435
        $posts = [];
436
        assign_capability(
437
            'moodle/site:viewfullnames',
438
            CAP_ALLOW,
439
            $DB->get_field('role', 'id', ['shortname' => 'student']),
440
            \context_course::instance($course1->id)
441
        );
442
 
443
        // Add 1 discussions to forum 1.
444
        list($discussion, $post) = $this->helper_post_to_forum($forum1, $user, ['mailnow' => 1]);
445
        $posts[] = $post;
446
 
447
        // Set the tested user's default maildigest setting.
448
        $DB->set_field('user', 'maildigest', 1, array('id' => $user->id));
449
 
450
        // No digests mails should be sent, but 1 forum mails will be sent.
451
        $expect = [
452
            (object) [
453
                'userid' => $user->id,
454
                'messages' => 0,
455
                'digests' => 1,
456
            ],
457
        ];
458
        $this->queue_tasks_and_assert($expect);
459
        $this->send_digests_and_assert($user, $posts);
460
 
461
        // The user does not, by default, have permission to view the fullname.
462
        // However we have given the user that capability so we expect to see both firstname and lastname.
463
        $messages = $this->messagesink->get_messages_by_component('mod_forum');
464
        $messages = reset($messages);
465
        $messagecontent = $messages->fullmessage;
466
 
467
        // Assert that the expected name is present (lastname only).
468
        $this->assertStringContainsString(fullname($user, false), $messagecontent);
469
 
470
        // Assert that the full name is also present (firstname lastname only).
471
        $this->assertStringContainsString(fullname($user, true), $messagecontent);
472
    }
473
 
474
    /**
475
     * Sends several notifications to one user as:
476
     * * daily digests coming from the per-forum setting; and
477
     * * single e-mails from the profile setting.
478
     */
11 efrain 479
    public function test_cron_mixed_email_1(): void {
1 efrain 480
        global $DB, $CFG;
481
 
482
        $this->resetAfterTest(true);
483
 
484
        // Set up a basic user enrolled in a course.
485
        $userhelper = $this->helper_setup_user_in_course();
486
        $user = $userhelper->user;
487
        $course1 = $userhelper->courses->course1;
488
        $forum1 = $userhelper->forums->forum1;
489
        $forum2 = $userhelper->forums->forum2;
490
        $posts = [];
491
        $digests = [];
492
 
493
        // Add 5 discussions to forum 1.
494
        for ($i = 0; $i < 5; $i++) {
495
            list($discussion, $post) = $this->helper_post_to_forum($forum1, $user, ['mailnow' => 1]);
496
            $digests[] = $post;
497
        }
498
 
499
        // Add 5 discussions to forum 2.
500
        for ($i = 0; $i < 5; $i++) {
501
            list($discussion, $post) = $this->helper_post_to_forum($forum2, $user, ['mailnow' => 1]);
502
            $posts[] = $post;
503
        }
504
 
505
        // Set the tested user's default maildigest setting.
506
        $DB->set_field('user', 'maildigest', 0, array('id' => $user->id));
507
 
508
        // Set the maildigest preference for forum1 to digest.
509
        forum_set_user_maildigest($forum1, 1, $user);
510
 
511
        // Set the maildigest preference for forum2 to default (single).
512
        forum_set_user_maildigest($forum2, -1, $user);
513
 
514
        // One digest e-mail should be sent, and five individual notifications.
515
        $expect = [
516
            (object) [
517
                'userid' => $user->id,
518
                'messages' => 5,
519
                'digests' => 1,
520
            ],
521
        ];
522
        $this->queue_tasks_and_assert($expect);
523
 
524
        $this->send_notifications_and_assert($user, $posts);
525
        $this->send_digests_and_assert($user, $digests);
526
    }
527
 
528
    /**
529
     * Sends several notifications to one user as:
530
     * * single e-mails from the per-forum setting; and
531
     * * daily digests coming from the per-user setting.
532
     */
11 efrain 533
    public function test_cron_mixed_email_2(): void {
1 efrain 534
        global $DB, $CFG;
535
 
536
        $this->resetAfterTest(true);
537
 
538
        // Set up a basic user enrolled in a course.
539
        $userhelper = $this->helper_setup_user_in_course();
540
        $user = $userhelper->user;
541
        $course1 = $userhelper->courses->course1;
542
        $forum1 = $userhelper->forums->forum1;
543
        $forum2 = $userhelper->forums->forum2;
544
        $posts = [];
545
        $digests = [];
546
 
547
        // Add 5 discussions to forum 1.
548
        for ($i = 0; $i < 5; $i++) {
549
            list($discussion, $post) = $this->helper_post_to_forum($forum1, $user, ['mailnow' => 1]);
550
            $digests[] = $post;
551
        }
552
 
553
        // Add 5 discussions to forum 2.
554
        for ($i = 0; $i < 5; $i++) {
555
            list($discussion, $post) = $this->helper_post_to_forum($forum2, $user, ['mailnow' => 1]);
556
            $posts[] = $post;
557
        }
558
 
559
        // Set the tested user's default maildigest setting.
560
        $DB->set_field('user', 'maildigest', 1, array('id' => $user->id));
561
 
562
        // Set the maildigest preference for forum1 to digest.
563
        forum_set_user_maildigest($forum1, -1, $user);
564
 
565
        // Set the maildigest preference for forum2 to single.
566
        forum_set_user_maildigest($forum2, 0, $user);
567
 
568
        // One digest e-mail should be sent, and five individual notifications.
569
        $expect = [
570
            (object) [
571
                'userid' => $user->id,
572
                'messages' => 5,
573
                'digests' => 1,
574
            ],
575
        ];
576
        $this->queue_tasks_and_assert($expect);
577
 
578
        $this->send_notifications_and_assert($user, $posts);
579
        $this->send_digests_and_assert($user, $digests);
580
    }
581
 
582
    /**
583
     * Sends several notifications to one user as:
584
     * * daily digests coming from the per-forum setting.
585
     */
11 efrain 586
    public function test_cron_forum_digest_email(): void {
1 efrain 587
        global $DB, $CFG;
588
 
589
        $this->resetAfterTest(true);
590
 
591
        // Set up a basic user enrolled in a course.
592
        $userhelper = $this->helper_setup_user_in_course();
593
        $user = $userhelper->user;
594
        $course1 = $userhelper->courses->course1;
595
        $forum1 = $userhelper->forums->forum1;
596
        $forum2 = $userhelper->forums->forum2;
597
        $fulldigests = [];
598
        $shortdigests = [];
599
 
600
        // Add 5 discussions to forum 1.
601
        for ($i = 0; $i < 5; $i++) {
602
            list($discussion, $post) = $this->helper_post_to_forum($forum1, $user, ['mailnow' => 1]);
603
            $fulldigests[] = $post;
604
        }
605
 
606
        // Add 5 discussions to forum 2.
607
        for ($i = 0; $i < 5; $i++) {
608
            list($discussion, $post) = $this->helper_post_to_forum($forum2, $user, ['mailnow' => 1]);
609
            $shortdigests[] = $post;
610
        }
611
 
612
        // Set the tested user's default maildigest setting.
613
        $DB->set_field('user', 'maildigest', 0, array('id' => $user->id));
614
 
615
        // Set the maildigest preference for forum1 to digest (complete).
616
        forum_set_user_maildigest($forum1, 1, $user);
617
 
618
        // Set the maildigest preference for forum2 to digest (short).
619
        forum_set_user_maildigest($forum2, 2, $user);
620
 
621
        // One digest e-mail should be sent, and no individual notifications.
622
        $expect = [
623
            (object) [
624
                'userid' => $user->id,
625
                'digests' => 1,
626
            ],
627
        ];
628
        $this->queue_tasks_and_assert($expect);
629
 
630
        $this->send_digests_and_assert($user, $fulldigests, $shortdigests);
631
    }
632
 
633
    /**
634
     * The digest being in the past is queued til the next day.
635
     */
11 efrain 636
    public function test_cron_digest_previous_day(): void {
1 efrain 637
        global $DB, $CFG;
638
 
639
        $this->resetAfterTest(true);
640
 
641
        // Set up a basic user enrolled in a course.
642
        $userhelper = $this->helper_setup_user_in_course();
643
        $user = $userhelper->user;
644
        $course1 = $userhelper->courses->course1;
645
        $forum1 = $userhelper->forums->forum1;
646
        $forum2 = $userhelper->forums->forum2;
647
        $fulldigests = [];
648
        $shortdigests = [];
649
 
650
        // Add 1 discussions to forum 1.
651
        list($discussion, $post) = $this->helper_post_to_forum($forum1, $user, ['mailnow' => 1]);
652
        $fulldigests[] = $post;
653
 
654
        // Set the tested user's default maildigest setting.
655
        $DB->set_field('user', 'maildigest', 1, array('id' => $user->id));
656
 
657
        // Set the digest time to midnight.
658
        $CFG->digestmailtime = 0;
659
        // One digest e-mail should be sent, and no individual notifications.
660
        $expect = [
661
            (object) [
662
                'userid' => $user->id,
663
                'digests' => 1,
664
            ],
665
        ];
666
        $this->queue_tasks_and_assert($expect);
667
 
668
        $tasks = $DB->get_records('task_adhoc', ['component' => 'mod_forum']);
669
        $task = reset($tasks);
670
        $this->assertGreaterThanOrEqual(time(), $task->nextruntime);
671
    }
672
 
673
    /**
674
     * The digest being in the future is queued for today.
675
     */
11 efrain 676
    public function test_cron_digest_same_day(): void {
1 efrain 677
        global $DB, $CFG;
678
 
679
        $this->resetAfterTest(true);
680
 
681
        // Set up a basic user enrolled in a course.
682
        $userhelper = $this->helper_setup_user_in_course();
683
        $user = $userhelper->user;
684
        $course1 = $userhelper->courses->course1;
685
        $forum1 = $userhelper->forums->forum1;
686
        $forum2 = $userhelper->forums->forum2;
687
        $fulldigests = [];
688
        $shortdigests = [];
689
 
690
        // Add 1 discussions to forum 1.
691
        list($discussion, $post) = $this->helper_post_to_forum($forum1, $user, ['mailnow' => 1]);
692
        $fulldigests[] = $post;
693
 
694
        // Set the tested user's default maildigest setting.
695
        $DB->set_field('user', 'maildigest', 1, array('id' => $user->id));
696
 
697
        // Set the digest time to the future (magic, shouldn't work).
698
        $CFG->digestmailtime = 25;
699
        // One digest e-mail should be sent, and no individual notifications.
700
        $expect = [
701
            (object) [
702
                'userid' => $user->id,
703
                'digests' => 1,
704
            ],
705
        ];
706
        $this->queue_tasks_and_assert($expect);
707
 
708
        $tasks = $DB->get_records('task_adhoc', ['component' => 'mod_forum']);
709
        $task = reset($tasks);
710
        $digesttime = usergetmidnight(time(), \core_date::get_server_timezone()) + ($CFG->digestmailtime * 3600);
711
        $this->assertLessThanOrEqual($digesttime, $task->nextruntime);
712
    }
713
 
714
    /**
715
     * Tests that if a new message is posted after the days digest time,
716
     * but before that days digests are sent a new task is created.
717
     */
11 efrain 718
    public function test_cron_digest_queue_next_before_current_processed(): void {
1 efrain 719
        global $DB, $CFG;
720
 
721
        $this->resetAfterTest(true);
722
 
723
        // Set up a basic user enrolled in a course.
724
        $userhelper = $this->helper_setup_user_in_course();
725
        $user = $userhelper->user;
726
        $forum1 = $userhelper->forums->forum1;
727
 
728
        // Add 1 discussions to forum 1.
729
        $this->helper_post_to_forum($forum1, $user, ['mailnow' => 1]);
730
 
731
        // Set the tested user's default maildigest setting.
732
        $DB->set_field('user', 'maildigest', 1, ['id' => $user->id]);
733
 
734
        // Set the digest time to the future (magic, shouldn't work).
735
        $CFG->digestmailtime = 25;
736
        // One digest e-mail should be sent, and no individual notifications.
737
        $expect = [
738
            (object) [
739
                'userid' => $user->id,
740
                'digests' => 1,
741
            ],
742
        ];
743
        $this->queue_tasks_and_assert($expect);
744
 
745
        // Set the digest time to midnight.
746
        $CFG->digestmailtime = 0;
747
 
748
        // Add another discussions to forum 1.
749
        $this->helper_post_to_forum($forum1, $user, ['mailnow' => 1]);
750
 
751
        // One digest e-mail should be sent, and no individual notifications.
752
        $expect = [
753
            (object) [
754
                'userid' => $user->id,
755
                'digests' => 1,
756
            ],
757
        ];
758
        $this->queue_tasks_and_assert($expect);
759
 
760
        // There should now be two tasks queued.
761
        $tasks = $DB->get_records('task_adhoc', ['component' => 'mod_forum']);
762
        $this->assertCount(2, $tasks);
763
 
764
        // Add yet another another discussions to forum 1.
765
        $this->helper_post_to_forum($forum1, $user, ['mailnow' => 1]);
766
 
767
        // One digest e-mail should be sent, and no individual notifications.
768
        $expect = [
769
            (object) [
770
                'userid' => $user->id,
771
                'digests' => 1,
772
            ],
773
        ];
774
        $this->queue_tasks_and_assert($expect);
775
 
776
        // There should still be two tasks queued.
777
        $tasks = $DB->get_records('task_adhoc', ['component' => 'mod_forum']);
778
        $this->assertCount(2, $tasks);
779
    }
780
 
781
    /**
782
     * The sending of a digest marks posts as read if automatic message read marking is set.
783
     */
11 efrain 784
    public function test_cron_digest_marks_posts_read(): void {
1 efrain 785
        global $DB, $CFG;
786
 
787
        $this->resetAfterTest(true);
788
 
789
        // Disable the 'Manual message read marking' option.
790
        $CFG->forum_usermarksread = false;
791
 
792
        // Set up a basic user enrolled in a course.
793
        $userhelper = $this->helper_setup_user_in_course();
794
        $user = $userhelper->user;
795
        $course1 = $userhelper->courses->course1;
796
        $forum1 = $userhelper->forums->forum1;
797
        $posts = [];
798
 
799
        // Set the tested user's default maildigest, trackforums, read tracking settings.
800
        $DB->set_field('user', 'maildigest', 1, ['id' => $user->id]);
801
        $DB->set_field('user', 'trackforums', 1, ['id' => $user->id]);
802
        set_user_preference('forum_markasreadonnotification', 1, $user->id);
803
 
804
        // Set the maildigest preference for forum1 to default.
805
        forum_set_user_maildigest($forum1, -1, $user);
806
 
807
        // Add 5 discussions to forum 1.
808
        for ($i = 0; $i < 5; $i++) {
809
            list($discussion, $post) = $this->helper_post_to_forum($forum1, $user, ['mailnow' => 1]);
810
            $posts[] = $post;
811
        }
812
 
813
        // There should be unread posts for the forum.
814
        $expectedposts = [
815
            $forum1->id => (object) [
816
                'id' => $forum1->id,
817
                'unread' => count($posts),
818
            ],
819
        ];
820
        $this->assertEquals($expectedposts, forum_tp_get_course_unread_posts($user->id, $course1->id));
821
 
822
        // One digest mail should be sent and no other messages.
823
        $expect = [
824
            (object) [
825
                'userid' => $user->id,
826
                'messages' => 0,
827
                'digests' => 1,
828
            ],
829
        ];
830
        $this->queue_tasks_and_assert($expect);
831
 
832
        $this->send_digests_and_assert($user, $posts);
833
 
834
        // Verify that there are no unread posts for any forums.
835
        $this->assertEmpty(forum_tp_get_course_unread_posts($user->id, $course1->id));
836
    }
837
 
838
    /**
839
     * The sending of a digest does not mark posts as read when manual message read marking is set.
840
     */
11 efrain 841
    public function test_cron_digest_leaves_posts_unread(): void {
1 efrain 842
        global $DB, $CFG;
843
 
844
        $this->resetAfterTest(true);
845
 
846
        // Enable the 'Manual message read marking' option.
847
        $CFG->forum_usermarksread = true;
848
 
849
        // Set up a basic user enrolled in a course.
850
        $userhelper = $this->helper_setup_user_in_course();
851
        $user = $userhelper->user;
852
        $course1 = $userhelper->courses->course1;
853
        $forum1 = $userhelper->forums->forum1;
854
        $posts = [];
855
 
856
        // Set the tested user's default maildigest, trackforums, read tracking settings.
857
        $DB->set_field('user', 'maildigest', 1, ['id' => $user->id]);
858
        $DB->set_field('user', 'trackforums', 1, ['id' => $user->id]);
859
        set_user_preference('forum_markasreadonnotification', 1, $user->id);
860
 
861
        // Set the maildigest preference for forum1 to default.
862
        forum_set_user_maildigest($forum1, -1, $user);
863
 
864
        // Add 5 discussions to forum 1.
865
        for ($i = 0; $i < 5; $i++) {
866
            list($discussion, $post) = $this->helper_post_to_forum($forum1, $user, ['mailnow' => 1]);
867
            $posts[] = $post;
868
        }
869
 
870
        // There should be unread posts for the forum.
871
        $expectedposts = [
872
            $forum1->id => (object) [
873
                'id' => $forum1->id,
874
                'unread' => count($posts),
875
            ],
876
        ];
877
        $this->assertEquals($expectedposts, forum_tp_get_course_unread_posts($user->id, $course1->id));
878
 
879
        // One digest mail should be sent and no other messages.
880
        $expect = [
881
            (object) [
882
                'userid' => $user->id,
883
                'messages' => 0,
884
                'digests' => 1,
885
            ],
886
        ];
887
        $this->queue_tasks_and_assert($expect);
888
 
889
        $this->send_digests_and_assert($user, $posts);
890
 
891
        // Verify that there are still the same unread posts for the forum.
892
        $this->assertEquals($expectedposts, forum_tp_get_course_unread_posts($user->id, $course1->id));
893
    }
894
}