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 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 core_message\external;
  18  
  19  defined('MOODLE_INTERNAL') || die();
  20  
  21  global $CFG;
  22  require_once($CFG->dirroot . '/webservice/tests/helpers.php');
  23  
  24  use external_api;
  25  use externallib_advanced_testcase;
  26  use \core_message\tests\helper as testhelper;
  27  
  28  /**
  29   * External function test for get_unread_notification_count.
  30   *
  31   * @package    core_message
  32   * @category   test
  33   * @copyright  2021 Dani Palou <dani@moodle.com>, based on Ryan Wyllie <ryan@moodle.com> code
  34   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   * @since      Moodle 4.0
  36   */
  37  class get_unread_notification_count_test extends externallib_advanced_testcase {
  38  
  39      /**
  40       * get_unread_notification should throw an exception for an invalid user.
  41       */
  42      public function test_get_unread_notification_count_invalid_user_exception(): void {
  43          $this->resetAfterTest(true);
  44  
  45          $this->expectException('moodle_exception');
  46          $result = get_unread_notification_count::execute(-2132131);
  47      }
  48  
  49      /**
  50       * get_unread_notification_count should throw exception if being requested for non-logged in user.
  51       */
  52      public function test_get_unread_notification_count_access_denied_exception(): void {
  53          $this->resetAfterTest(true);
  54  
  55          $sender = $this->getDataGenerator()->create_user();
  56          $user = $this->getDataGenerator()->create_user();
  57  
  58          $this->setUser($user);
  59          $this->expectException('moodle_exception');
  60          $result = get_unread_notification_count::execute($sender->id);
  61      }
  62  
  63      /**
  64       * Test get_unread_notification_count.
  65       */
  66      public function test_get_unread_notification_count(): void {
  67          $this->resetAfterTest(true);
  68  
  69          $sender1 = $this->getDataGenerator()->create_user();
  70          $sender2 = $this->getDataGenerator()->create_user();
  71          $sender3 = $this->getDataGenerator()->create_user();
  72          $recipient = $this->getDataGenerator()->create_user();
  73  
  74          $this->setUser($recipient);
  75  
  76          $notificationids = [
  77              testhelper::send_fake_unread_notification($sender1, $recipient, 'Notification', 1),
  78              testhelper::send_fake_unread_notification($sender1, $recipient, 'Notification', 2),
  79              testhelper::send_fake_unread_notification($sender2, $recipient, 'Notification', 3),
  80              testhelper::send_fake_unread_notification($sender2, $recipient, 'Notification', 4),
  81              testhelper::send_fake_unread_notification($sender3, $recipient, 'Notification', 5),
  82              testhelper::send_fake_unread_notification($sender3, $recipient, 'Notification', 6),
  83          ];
  84  
  85          $count = get_unread_notification_count::execute($recipient->id);
  86          $this->assertEquals($count, 6);
  87  
  88      }
  89  
  90  }