Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403]

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * 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   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  use core\task\messaging_cleanup_task;
  29  
  30  global $CFG;
  31  require_once($CFG->dirroot . '/message/output/popup/tests/base.php');
  32  
  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 {
  42  
  43      // Helper trait for sending fake popup notifications.
  44      use message_popup_test_helper;
  45  
  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;
  53  
  54          $this->resetAfterTest();
  55  
  56          $userfrom = $this->getDataGenerator()->create_user();
  57          $userto = $this->getDataGenerator()->create_user();
  58  
  59          $now = time();
  60  
  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);
  63  
  64          // Sanity check.
  65          $this->assertEquals(2, $DB->count_records('message_popup_notifications'));
  66  
  67          // Delete all notifications >5 seconds old.
  68          set_config('messagingdeleteallnotificationsdelay', 5);
  69          (new messaging_cleanup_task())->execute();
  70  
  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      }
  76  
  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;
  84  
  85          $this->resetAfterTest();
  86  
  87          $userfrom = $this->getDataGenerator()->create_user();
  88          $userto = $this->getDataGenerator()->create_user();
  89  
  90          $now = time();
  91  
  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);
  94  
  95          // Sanity check.
  96          $this->assertEquals(2, $DB->count_records('message_popup_notifications'));
  97  
  98          // Delete read notifications >5 seconds old.
  99          set_config('messagingdeletereadnotificationsdelay', 5);
 100          (new messaging_cleanup_task())->execute();
 101  
 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      }
 107  }