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] [Versions 39 and 310]

   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 API.
  19   *
  20   * @package message_popup
  21   * @category test
  22   * @copyright 2016 Ryan Wyllie <ryan@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  global $CFG;
  29  
  30  require_once($CFG->dirroot . '/message/tests/messagelib_test.php');
  31  require_once($CFG->dirroot . '/message/output/popup/tests/base.php');
  32  
  33  /**
  34   * Test message popup API.
  35   *
  36   * @package message_popup
  37   * @category test
  38   * @copyright 2016 Ryan Wyllie <ryan@moodle.com>
  39   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  40   */
  41  class message_popup_api_testcase extends advanced_testcase {
  42      use message_popup_test_helper;
  43  
  44      /**
  45       * Test set up.
  46       *
  47       * This is executed before running any test in this file.
  48       */
  49      public function setUp(): void {
  50          $this->preventResetByRollback(); // Messaging is not compatible with transactions.
  51          $this->messagesink = $this->redirectMessages();
  52          $this->resetAfterTest();
  53      }
  54  
  55      /**
  56       * Test that the get_popup_notifications function will return the correct notifications.
  57       */
  58      public function test_message_get_popup_notifications() {
  59          $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
  60          $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
  61  
  62          $this->send_fake_read_popup_notification($sender, $recipient, 'Message 1', 1);
  63          $this->send_fake_unread_popup_notification($sender, $recipient, 'Message 2', 2);
  64          $this->send_fake_read_popup_notification($sender, $recipient, 'Message 3', 3, 1);
  65          $this->send_fake_read_popup_notification($sender, $recipient, 'Message 4', 3, 2);
  66          $this->send_fake_unread_popup_notification($sender, $recipient, 'Message 5', 4);
  67  
  68          $notifications = \message_popup\api::get_popup_notifications($recipient->id);
  69  
  70          $this->assertEquals($notifications[0]->fullmessage, 'Message 5');
  71          $this->assertEquals($notifications[1]->fullmessage, 'Message 4');
  72          $this->assertEquals($notifications[2]->fullmessage, 'Message 3');
  73          $this->assertEquals($notifications[3]->fullmessage, 'Message 2');
  74          $this->assertEquals($notifications[4]->fullmessage, 'Message 1');
  75      }
  76  
  77      /**
  78       * Test that the get_popup_notifications function works correctly with limiting and offsetting
  79       * the result set if requested.
  80       */
  81      public function test_message_get_popup_notifications_all_limit_and_offset() {
  82          $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
  83          $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
  84  
  85          $this->send_fake_read_popup_notification($sender, $recipient, 'Message 1', 1);
  86          $this->send_fake_unread_popup_notification($sender, $recipient, 'Message 2', 2);
  87          $this->send_fake_read_popup_notification($sender, $recipient, 'Message 3', 3, 1);
  88          $this->send_fake_read_popup_notification($sender, $recipient, 'Message 4', 3, 2);
  89          $this->send_fake_unread_popup_notification($sender, $recipient, 'Message 5', 4);
  90          $this->send_fake_unread_popup_notification($sender, $recipient, 'Message 6', 5);
  91  
  92          $notifications = \message_popup\api::get_popup_notifications($recipient->id, 'DESC', 2, 0);
  93  
  94          $this->assertEquals($notifications[0]->fullmessage, 'Message 6');
  95          $this->assertEquals($notifications[1]->fullmessage, 'Message 5');
  96  
  97          $notifications = \message_popup\api::get_popup_notifications($recipient->id, 'DESC', 2, 2);
  98  
  99          $this->assertEquals($notifications[0]->fullmessage, 'Message 4');
 100          $this->assertEquals($notifications[1]->fullmessage, 'Message 3');
 101  
 102          $notifications = \message_popup\api::get_popup_notifications($recipient->id, 'DESC', 0, 3);
 103  
 104          $this->assertEquals($notifications[0]->fullmessage, 'Message 3');
 105          $this->assertEquals($notifications[1]->fullmessage, 'Message 2');
 106          $this->assertEquals($notifications[2]->fullmessage, 'Message 1');
 107      }
 108  
 109      /**
 110       * Test count_unread_popup_notifications.
 111       */
 112      public function test_message_count_unread_popup_notifications() {
 113          $sender1 = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
 114          $sender2 = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
 115          $recipient1 = $this->getDataGenerator()->create_user(array('firstname' => 'Test3', 'lastname' => 'User3'));
 116          $recipient2 = $this->getDataGenerator()->create_user(array('firstname' => 'Test4', 'lastname' => 'User4'));
 117  
 118          $this->send_fake_unread_popup_notification($sender1, $recipient1);
 119          $this->send_fake_unread_popup_notification($sender1, $recipient1);
 120          $this->send_fake_unread_popup_notification($sender2, $recipient1);
 121          $this->send_fake_unread_popup_notification($sender1, $recipient2);
 122          $this->send_fake_unread_popup_notification($sender2, $recipient2);
 123          $this->send_fake_unread_popup_notification($sender2, $recipient2);
 124          $this->send_fake_unread_popup_notification($sender2, $recipient2);
 125          $this->send_fake_unread_popup_notification($sender2, $recipient2);
 126  
 127          $this->assertEquals(\message_popup\api::count_unread_popup_notifications($recipient1->id), 3);
 128          $this->assertEquals(\message_popup\api::count_unread_popup_notifications($recipient2->id), 5);
 129      }
 130  }