Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 39 and 401] [Versions 401 and 402] [Versions 401 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  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      /**
  38       * Test set up.
  39       *
  40       * This is executed before running any test in this file.
  41       */
  42      public function setUp(): void {
  43          $this->preventResetByRollback(); // Messaging is not compatible with transactions.
  44          $this->messagesink = $this->redirectMessages();
  45          $this->resetAfterTest();
  46      }
  47  
  48      /**
  49       * Test that the get_popup_notifications function will return the correct notifications.
  50       */
  51      public function test_message_get_popup_notifications() {
  52          $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
  53          $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
  54  
  55          $this->send_fake_read_popup_notification($sender, $recipient, 'Message 1', 1);
  56          $this->send_fake_unread_popup_notification($sender, $recipient, 'Message 2', 2);
  57          $this->send_fake_read_popup_notification($sender, $recipient, 'Message 3', 3, 1);
  58          $this->send_fake_read_popup_notification($sender, $recipient, 'Message 4', 3, 2);
  59          $this->send_fake_unread_popup_notification($sender, $recipient, 'Message 5', 4);
  60  
  61          $notifications = \message_popup\api::get_popup_notifications($recipient->id);
  62  
  63          $this->assertEquals($notifications[0]->fullmessage, 'Message 5');
  64          $this->assertEquals($notifications[1]->fullmessage, 'Message 4');
  65          $this->assertEquals($notifications[2]->fullmessage, 'Message 3');
  66          $this->assertEquals($notifications[3]->fullmessage, 'Message 2');
  67          $this->assertEquals($notifications[4]->fullmessage, 'Message 1');
  68      }
  69  
  70      /**
  71       * Test that the get_popup_notifications function works correctly with limiting and offsetting
  72       * the result set if requested.
  73       */
  74      public function test_message_get_popup_notifications_all_limit_and_offset() {
  75          $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
  76          $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
  77  
  78          $this->send_fake_read_popup_notification($sender, $recipient, 'Message 1', 1);
  79          $this->send_fake_unread_popup_notification($sender, $recipient, 'Message 2', 2);
  80          $this->send_fake_read_popup_notification($sender, $recipient, 'Message 3', 3, 1);
  81          $this->send_fake_read_popup_notification($sender, $recipient, 'Message 4', 3, 2);
  82          $this->send_fake_unread_popup_notification($sender, $recipient, 'Message 5', 4);
  83          $this->send_fake_unread_popup_notification($sender, $recipient, 'Message 6', 5);
  84  
  85          $notifications = \message_popup\api::get_popup_notifications($recipient->id, 'DESC', 2, 0);
  86  
  87          $this->assertEquals($notifications[0]->fullmessage, 'Message 6');
  88          $this->assertEquals($notifications[1]->fullmessage, 'Message 5');
  89  
  90          $notifications = \message_popup\api::get_popup_notifications($recipient->id, 'DESC', 2, 2);
  91  
  92          $this->assertEquals($notifications[0]->fullmessage, 'Message 4');
  93          $this->assertEquals($notifications[1]->fullmessage, 'Message 3');
  94  
  95          $notifications = \message_popup\api::get_popup_notifications($recipient->id, 'DESC', 0, 3);
  96  
  97          $this->assertEquals($notifications[0]->fullmessage, 'Message 3');
  98          $this->assertEquals($notifications[1]->fullmessage, 'Message 2');
  99          $this->assertEquals($notifications[2]->fullmessage, 'Message 1');
 100      }
 101  
 102      /**
 103       * Test count_unread_popup_notifications.
 104       */
 105      public function test_message_count_unread_popup_notifications() {
 106          $sender1 = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
 107          $sender2 = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
 108          $recipient1 = $this->getDataGenerator()->create_user(array('firstname' => 'Test3', 'lastname' => 'User3'));
 109          $recipient2 = $this->getDataGenerator()->create_user(array('firstname' => 'Test4', 'lastname' => 'User4'));
 110  
 111          $this->send_fake_unread_popup_notification($sender1, $recipient1);
 112          $this->send_fake_unread_popup_notification($sender1, $recipient1);
 113          $this->send_fake_unread_popup_notification($sender2, $recipient1);
 114          $this->send_fake_unread_popup_notification($sender1, $recipient2);
 115          $this->send_fake_unread_popup_notification($sender2, $recipient2);
 116          $this->send_fake_unread_popup_notification($sender2, $recipient2);
 117          $this->send_fake_unread_popup_notification($sender2, $recipient2);
 118          $this->send_fake_unread_popup_notification($sender2, $recipient2);
 119  
 120          $this->assertEquals(\message_popup\api::count_unread_popup_notifications($recipient1->id), 3);
 121          $this->assertEquals(\message_popup\api::count_unread_popup_notifications($recipient2->id), 5);
 122      }
 123  }