Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

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

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