Proyectos de Subversion Moodle

Rev

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