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