Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 310 and 311] [Versions 39 and 311]

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