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   * External message popup functions unit tests
  19   *
  20   * @package    message_popup
  21   * @copyright  2016 Ryan Wyllie <ryan@moodle.com>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  global $CFG;
  28  
  29  require_once($CFG->dirroot . '/webservice/tests/helpers.php');
  30  require_once($CFG->dirroot . '/message/output/popup/externallib.php');
  31  require_once($CFG->dirroot . '/message/output/popup/tests/base.php');
  32  
  33  /**
  34   * Class for external message popup functions unit tests.
  35   *
  36   * @copyright  2016 Ryan Wyllie <ryan@moodle.com>
  37   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   */
  39  class message_popup_externallib_testcase extends advanced_testcase {
  40      use message_popup_test_helper;
  41  
  42      /**
  43       * Test set up.
  44       *
  45       * This is executed before running any test in this file.
  46       */
  47      public function setUp(): void {
  48          $this->preventResetByRollback(); // Messaging is not compatible with transactions.
  49          $this->messagesink = $this->redirectMessages();
  50          $this->resetAfterTest();
  51      }
  52  
  53      /**
  54       * Test that get_popup_notifications throws an exception if the user
  55       * doesn't exist.
  56       */
  57      public function test_get_popup_notifications_no_user_exception() {
  58          $this->resetAfterTest(true);
  59  
  60          $this->expectException('moodle_exception');
  61          $result = message_popup_external::get_popup_notifications(-2132131, false, 0, 0);
  62      }
  63  
  64      /**
  65       * get_popup_notifications should throw exception if user isn't logged in
  66       * user.
  67       */
  68      public function test_get_popup_notifications_access_denied_exception() {
  69          $this->resetAfterTest(true);
  70  
  71          $sender = $this->getDataGenerator()->create_user();
  72          $user = $this->getDataGenerator()->create_user();
  73  
  74          $this->setUser($user);
  75          $this->expectException('moodle_exception');
  76          $result = message_popup_external::get_popup_notifications($sender->id, false, 0, 0);
  77      }
  78  
  79      /**
  80       * get_popup_notifications should return notifications for the recipient.
  81       */
  82      public function test_get_popup_notifications_as_recipient() {
  83          $this->resetAfterTest(true);
  84  
  85          $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Sendy', 'lastname' => 'Sender'));
  86          $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Recipy', 'lastname' => 'Recipient'));
  87  
  88          $notificationids = array(
  89              $this->send_fake_unread_popup_notification($sender, $recipient),
  90              $this->send_fake_unread_popup_notification($sender, $recipient),
  91              $this->send_fake_read_popup_notification($sender, $recipient),
  92              $this->send_fake_read_popup_notification($sender, $recipient),
  93          );
  94  
  95          // Confirm that admin has super powers to retrieve any notifications.
  96          $this->setAdminUser();
  97          $result = message_popup_external::get_popup_notifications($recipient->id, false, 0, 0);
  98          $this->assertCount(4, $result['notifications']);
  99          // Check we receive custom data as a unserialisable json.
 100          $found = 0;
 101          foreach ($result['notifications'] as $notification) {
 102              if (!empty($notification->customdata)) {
 103                  $this->assertObjectHasAttribute('datakey', json_decode($notification->customdata));
 104                  $found++;
 105              }
 106          }
 107          $this->assertEquals(2, $found);
 108  
 109          $this->setUser($recipient);
 110          $result = message_popup_external::get_popup_notifications($recipient->id, false, 0, 0);
 111          $this->assertCount(4, $result['notifications']);
 112      }
 113  
 114      /**
 115       * get_popup_notifications result set should work with limit and offset.
 116       */
 117      public function test_get_popup_notification_limit_offset() {
 118          $this->resetAfterTest(true);
 119  
 120          $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Sendy', 'lastname' => 'Sender'));
 121          $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Recipy', 'lastname' => 'Recipient'));
 122  
 123          $this->setUser($recipient);
 124  
 125          $notificationids = array(
 126              $this->send_fake_unread_popup_notification($sender, $recipient, 'Notification 1', 1),
 127              $this->send_fake_unread_popup_notification($sender, $recipient, 'Notification 2', 2),
 128              $this->send_fake_unread_popup_notification($sender, $recipient, 'Notification 3', 3),
 129              $this->send_fake_unread_popup_notification($sender, $recipient, 'Notification 4', 4),
 130              $this->send_fake_read_popup_notification($sender, $recipient, 'Notification 5', 5),
 131              $this->send_fake_read_popup_notification($sender, $recipient, 'Notification 6', 6),
 132              $this->send_fake_read_popup_notification($sender, $recipient, 'Notification 7', 7),
 133              $this->send_fake_read_popup_notification($sender, $recipient, 'Notification 8', 8),
 134          );
 135  
 136          $result = message_popup_external::get_popup_notifications($recipient->id, true, 2, 0);
 137  
 138          $this->assertEquals($result['notifications'][0]->id, $notificationids[7]);
 139          $this->assertEquals($result['notifications'][1]->id, $notificationids[6]);
 140  
 141          $result = message_popup_external::get_popup_notifications($recipient->id, true, 2, 2);
 142  
 143          $this->assertEquals($result['notifications'][0]->id, $notificationids[5]);
 144          $this->assertEquals($result['notifications'][1]->id, $notificationids[4]);
 145      }
 146  
 147      /**
 148       * get_unread_popup_notification should throw an exception for an invalid user.
 149       */
 150      public function test_get_unread_popup_notification_count_invalid_user_exception() {
 151          $this->resetAfterTest(true);
 152  
 153          $this->expectException('moodle_exception');
 154          $result = message_popup_external::get_unread_popup_notification_count(-2132131, 0);
 155      }
 156  
 157      /**
 158       * get_unread_popup_notification_count should throw exception if being requested for
 159       * non-logged in user.
 160       */
 161      public function test_get_unread_popup_notification_count_access_denied_exception() {
 162          $this->resetAfterTest(true);
 163  
 164          $sender = $this->getDataGenerator()->create_user();
 165          $user = $this->getDataGenerator()->create_user();
 166  
 167          $this->setUser($user);
 168          $this->expectException('moodle_exception');
 169          $result = message_popup_external::get_unread_popup_notification_count($sender->id, 0);
 170      }
 171  
 172      /**
 173       * Test get_unread_popup_notification_count.
 174       */
 175      public function test_get_unread_popup_notification_count() {
 176          $this->resetAfterTest(true);
 177  
 178          $sender1 = $this->getDataGenerator()->create_user();
 179          $sender2 = $this->getDataGenerator()->create_user();
 180          $sender3 = $this->getDataGenerator()->create_user();
 181          $recipient = $this->getDataGenerator()->create_user();
 182  
 183          $this->setUser($recipient);
 184  
 185          $notificationids = array(
 186              $this->send_fake_unread_popup_notification($sender1, $recipient, 'Notification', 1),
 187              $this->send_fake_unread_popup_notification($sender1, $recipient, 'Notification', 2),
 188              $this->send_fake_unread_popup_notification($sender2, $recipient, 'Notification', 3),
 189              $this->send_fake_unread_popup_notification($sender2, $recipient, 'Notification', 4),
 190              $this->send_fake_unread_popup_notification($sender3, $recipient, 'Notification', 5),
 191              $this->send_fake_unread_popup_notification($sender3, $recipient, 'Notification', 6),
 192          );
 193  
 194          $count = message_popup_external::get_unread_popup_notification_count($recipient->id);
 195          $this->assertEquals($count, 6);
 196      }
 197  }