MDL-64417 message_popup: remove old notifications during cleanup task.
[moodle.git] / message / output / popup / tests / messaging_cleanup_test.php
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/>.
17 /**
18  * Test message popup messaging cleanup task
19  *
20  * @package     message_popup
21  * @category    test
22  * @copyright   2020 Paul Holden <paulh@moodle.com>
23  * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24  */
26 defined('MOODLE_INTERNAL') || die();
28 use core\task\messaging_cleanup_task;
30 global $CFG;
31 require_once($CFG->dirroot . '/message/output/popup/tests/base.php');
33 /**
34  * Test class
35  *
36  * @package     message_popup
37  * @category    test
38  * @copyright   2020 Paul Holden <paulh@moodle.com>
39  * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40  */
41 class message_popup_messaging_cleanup_testcase extends advanced_testcase {
43     // Helper trait for sending fake popup notifications.
44     use message_popup_test_helper;
46     /**
47      * Test that all popup notifications are cleaned up
48      *
49      * @return void
50      */
51     public function test_cleanup_all_notifications() {
52         global $DB;
54         $this->resetAfterTest();
56         $userfrom = $this->getDataGenerator()->create_user();
57         $userto = $this->getDataGenerator()->create_user();
59         $now = time();
61         $this->send_fake_unread_popup_notification($userfrom, $userto, 'Message 1', $now - 10);
62         $notificationid = $this->send_fake_unread_popup_notification($userfrom, $userto, 'Message 2', $now);
64         // Sanity check.
65         $this->assertEquals(2, $DB->count_records('message_popup_notifications'));
67         // Delete all notifications >5 seconds old.
68         set_config('messagingdeleteallnotificationsdelay', 5);
69         (new messaging_cleanup_task())->execute();
71         // We should have just one record now, matching the second notification we sent.
72         $records = $DB->get_records('message_popup_notifications');
73         $this->assertCount(1, $records);
74         $this->assertEquals($notificationid, reset($records)->notificationid);
75     }
77     /**
78      * Test that read popup notifications are cleaned up
79      *
80      * @return void
81      */
82     public function test_cleanup_read_notifications() {
83         global $DB;
85         $this->resetAfterTest();
87         $userfrom = $this->getDataGenerator()->create_user();
88         $userto = $this->getDataGenerator()->create_user();
90         $now = time();
92         $this->send_fake_read_popup_notification($userfrom, $userto, 'Message 1', $now - 20, $now - 10);
93         $notificationid = $this->send_fake_read_popup_notification($userfrom, $userto, 'Message 2', $now - 15, $now);
95         // Sanity check.
96         $this->assertEquals(2, $DB->count_records('message_popup_notifications'));
98         // Delete read notifications >5 seconds old.
99         set_config('messagingdeletereadnotificationsdelay', 5);
100         (new messaging_cleanup_task())->execute();
102         // We should have just one record now, matching the second notification we sent.
103         $records = $DB->get_records('message_popup_notifications');
104         $this->assertCount(1, $records);
105         $this->assertEquals($notificationid, reset($records)->notificationid);
106     }