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