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_airnotifier;
  18  
  19  use core_external\external_api;
  20  use externallib_advanced_testcase;
  21  use message_airnotifier_external;
  22  
  23  defined('MOODLE_INTERNAL') || die();
  24  
  25  global $CFG;
  26  
  27  require_once($CFG->dirroot . '/webservice/tests/helpers.php');
  28  
  29  /**
  30   * External airnotifier functions unit tests
  31   *
  32   * @package    message_airnotifier
  33   * @category   external
  34   * @copyright  2012 Jerome Mouneyrac
  35   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   */
  37  class externallib_test extends externallib_advanced_testcase {
  38  
  39      /**
  40       * Tests set up
  41       */
  42      protected function setUp(): void {
  43          global $CFG;
  44          require_once($CFG->dirroot . '/message/output/airnotifier/externallib.php');
  45      }
  46  
  47      /**
  48       * Test is_system_configured
  49       */
  50      public function test_is_system_configured() {
  51          global $DB;
  52  
  53          $this->resetAfterTest(true);
  54  
  55          $user  = self::getDataGenerator()->create_user();
  56          self::setUser($user);
  57  
  58          // In a clean installation, it should be not configured.
  59          $configured = message_airnotifier_external::is_system_configured();
  60          $configured = external_api::clean_returnvalue(message_airnotifier_external::is_system_configured_returns(), $configured);
  61          $this->assertEquals(0, $configured);
  62  
  63          // Fake configuration.
  64          set_config('airnotifieraccesskey', random_string());
  65          // Enable the plugin.
  66          $DB->set_field('message_processors', 'enabled', 1, array('name' => 'airnotifier'));
  67  
  68          $configured = message_airnotifier_external::is_system_configured();
  69          $configured = external_api::clean_returnvalue(message_airnotifier_external::is_system_configured_returns(), $configured);
  70          $this->assertEquals(1, $configured);
  71      }
  72  
  73      /**
  74       * Test are_notification_preferences_configured
  75       */
  76      public function test_are_notification_preferences_configured() {
  77  
  78          $this->resetAfterTest(true);
  79  
  80          $user1  = self::getDataGenerator()->create_user();
  81          $user2  = self::getDataGenerator()->create_user();
  82          $user3  = self::getDataGenerator()->create_user();
  83  
  84          self::setUser($user1);
  85  
  86          set_user_preference('message_provider_moodle_instantmessage_enabled', 'airnotifier', $user1);
  87          set_user_preference('message_provider_moodle_instantmessage_enabled', 'airnotifier', $user2);
  88  
  89          $params = array($user1->id, $user2->id, $user3->id);
  90  
  91          $preferences = message_airnotifier_external::are_notification_preferences_configured($params);
  92          $returnsdescription = message_airnotifier_external::are_notification_preferences_configured_returns();
  93          $preferences = external_api::clean_returnvalue($returnsdescription, $preferences);
  94  
  95          $expected = array(
  96              array(
  97                  'userid' => $user1->id,
  98                  'configured' => 1
  99              )
 100          );
 101  
 102          $this->assertEquals(1, count($preferences['users']));
 103          $this->assertEquals($expected, $preferences['users']);
 104          $this->assertEquals(2, count($preferences['warnings']));
 105  
 106          // Now, remove one user.
 107          delete_user($user2);
 108          $preferences = message_airnotifier_external::are_notification_preferences_configured($params);
 109          $preferences = external_api::clean_returnvalue($returnsdescription, $preferences);
 110          $this->assertEquals(1, count($preferences['users']));
 111          $this->assertEquals($expected, $preferences['users']);
 112          $this->assertEquals(2, count($preferences['warnings']));
 113  
 114          // Now, remove one user1 preference (the user still has one preference for airnotifier).
 115          unset_user_preference('message_provider_moodle_instantmessage_enabled', $user1);
 116          $preferences = message_airnotifier_external::are_notification_preferences_configured($params);
 117          $preferences = external_api::clean_returnvalue($returnsdescription, $preferences);
 118          $this->assertEquals($expected, $preferences['users']);
 119      }
 120  
 121      /**
 122       * Test get_user_devices
 123       */
 124      public function test_get_user_devices() {
 125          global $CFG, $DB;
 126          require_once($CFG->dirroot . '/user/externallib.php');
 127  
 128          $this->resetAfterTest(true);
 129          $this->setAdminUser();
 130  
 131          // System not configured.
 132          $devices = message_airnotifier_external::get_user_devices('');
 133          $devices = external_api::clean_returnvalue(message_airnotifier_external::get_user_devices_returns(), $devices);
 134          $this->assertCount(1, $devices['warnings']);
 135          $this->assertEquals('systemnotconfigured', $devices['warnings'][0]['warningcode']);
 136  
 137          // Fake configuration.
 138          set_config('airnotifieraccesskey', random_string());
 139          // Enable the plugin.
 140          $DB->set_field('message_processors', 'enabled', 1, array('name' => 'airnotifier'));
 141  
 142          // Get devices.
 143          $devices = message_airnotifier_external::get_user_devices('');
 144          $devices = external_api::clean_returnvalue(message_airnotifier_external::get_user_devices_returns(), $devices);
 145          $this->assertCount(0, $devices['warnings']);
 146          // No devices, unfortunatelly we cannot create devices (we can't mock airnotifier server).
 147          $this->assertCount(0, $devices['devices']);
 148      }
 149  
 150      /**
 151       * Test get_user_devices permissions
 152       */
 153      public function test_get_user_devices_permissions() {
 154          global $CFG, $DB;
 155          require_once($CFG->dirroot . '/user/externallib.php');
 156  
 157          $this->resetAfterTest(true);
 158          $user  = self::getDataGenerator()->create_user();
 159          $otheruser  = self::getDataGenerator()->create_user();
 160          self::setUser($user);
 161  
 162          // No permission to get other users devices.
 163          $this->expectException('required_capability_exception');
 164          $devices = message_airnotifier_external::get_user_devices('', $otheruser->id);
 165      }
 166  
 167      /**
 168       * Test enable_device
 169       */
 170      public function test_enable_device() {
 171          global $USER, $DB;
 172  
 173          $this->resetAfterTest(true);
 174          $this->setAdminUser();
 175  
 176          // Add fake core device.
 177          $device = array(
 178              'appid' => 'com.moodle.moodlemobile',
 179              'name' => 'occam',
 180              'model' => 'Nexus 4',
 181              'platform' => 'Android',
 182              'version' => '4.2.2',
 183              'pushid' => 'apushdkasdfj4835',
 184              'uuid' => 'asdnfl348qlksfaasef859',
 185              'userid' => $USER->id,
 186              'timecreated' => time(),
 187              'timemodified' => time(),
 188          );
 189          $coredeviceid = $DB->insert_record('user_devices', (object) $device);
 190  
 191          $airnotifierdev = array(
 192              'userdeviceid' => $coredeviceid,
 193              'enable' => 1
 194          );
 195          $airnotifierdevid = $DB->insert_record('message_airnotifier_devices', (object) $airnotifierdev);
 196  
 197          // Disable and enable.
 198          $result = message_airnotifier_external::enable_device($airnotifierdevid, false);
 199          $result = external_api::clean_returnvalue(message_airnotifier_external::enable_device_returns(), $result);
 200          $this->assertCount(0, $result['warnings']);
 201          $this->assertTrue($result['success']);
 202          $this->assertEquals(0, $DB->get_field('message_airnotifier_devices', 'enable', array('id' => $airnotifierdevid)));
 203  
 204          $result = message_airnotifier_external::enable_device($airnotifierdevid, true);
 205          $result = external_api::clean_returnvalue(message_airnotifier_external::enable_device_returns(), $result);
 206          $this->assertCount(0, $result['warnings']);
 207          $this->assertTrue($result['success']);
 208          $this->assertEquals(1, $DB->get_field('message_airnotifier_devices', 'enable', array('id' => $airnotifierdevid)));
 209      }
 210  }