Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 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 message_airnotifier_manager;
  20  
  21  /**
  22   * Unit tests for message_airnotifier_manager.
  23   *
  24   * @package     message_airnotifier
  25   * @category    test
  26   * @copyright   2020 Juan Leyva <juan@moodle.com>
  27   * @license     http://www.gnu.org/copyleft/gpl.html GNU Public License
  28   */
  29  class manager_test extends \advanced_testcase {
  30  
  31      /** Test check_configuration by default **/
  32      public function test_check_configuration_default() {
  33          global $CFG;
  34          $this->resetAfterTest(true);
  35  
  36          $manager = new message_airnotifier_manager();
  37  
  38          // Mock server responses.
  39          $CFG->airnotifierurl = 'localhost';
  40          \curl::mock_response(json_encode(['error' => 'Invalid access key']));  // Mock request to check access key.
  41          $checks = $manager->check_configuration();
  42  
  43          $this->assertEquals(\core\check\result::OK, $checks[0]->get_status());   // Mobile service enabled.
  44          $this->assertEquals(\core\check\result::OK, $checks[1]->get_status());   // Mobile notifications enabled.
  45          $this->assertEquals(\core\check\result::ERROR, $checks[2]->get_status());    // Airnotifier NOT configured, missing key.
  46          $this->assertEquals(\core\check\result::OK, $checks[3]->get_status());   // Airnotifier URL available.
  47          $this->assertEquals(\core\check\result::ERROR, $checks[4]->get_status());    // Missing access key.
  48          $this->assertEquals(\core\check\result::WARNING, $checks[5]->get_status());  // Only a few of default mobile notifications.
  49      }
  50  
  51      /** Test check_configuration with token **/
  52      public function test_check_configuration_with_token() {
  53          global $CFG;
  54          $this->resetAfterTest(true);
  55  
  56          $manager = new message_airnotifier_manager();
  57  
  58          // Mock server responses.
  59          $CFG->airnotifierurl = 'localhost';
  60          \curl::mock_response(json_encode(['status' => 'ok']));   // Mock first request to check URL.
  61          \curl::mock_response(json_encode(['error' => 'Invalid access key']));  // Mock second request to check acces key.
  62          $CFG->airnotifieraccesskey = 'test';    // For enabling Airnotifier.
  63          $checks = $manager->check_configuration();
  64  
  65          $this->assertEquals(\core\check\result::OK, $checks[0]->get_status());   // Mobile service enabled.
  66          $this->assertEquals(\core\check\result::OK, $checks[1]->get_status());   // Mobile notifications enabled.
  67          $this->assertEquals(\core\check\result::OK, $checks[2]->get_status());    // Airnotifier configured.
  68          $this->assertEquals(\core\check\result::OK, $checks[3]->get_status());   // Airnotifier URL available.
  69          // The original function fourth check (access key valid in the remote Airnotifier server) is not mockable.
  70          $this->assertEquals(\core\check\result::WARNING, $checks[4]->get_status());  // Only a few of default mobile notifications.
  71      }
  72  
  73      /** Test check_configuration bad settings **/
  74      public function test_check_configuration_incorrect_settings() {
  75          global $CFG;
  76          $this->resetAfterTest(true);
  77  
  78          $manager = new message_airnotifier_manager();
  79  
  80          // Mock server responses.
  81          $CFG->airnotifierurl = 'localhost';
  82          \curl::mock_response(json_encode(['status' => 'ok']));   // Mock first request to check URL.
  83          \curl::mock_response(json_encode(['error' => 'Invalid access key']));  // Mock second request to check acces key.
  84          $CFG->airnotifieraccesskey = 'test';    // For enabling Airnotifier.
  85          $CFG->airnotifierappname .= ' ';
  86          $checks = $manager->check_configuration();
  87  
  88          $this->assertEquals(\core\check\result::OK, $checks[0]->get_status());   // Mobile service enabled.
  89          $this->assertEquals(\core\check\result::OK, $checks[1]->get_status());   // Mobile notifications enabled.
  90          $this->assertEquals(\core\check\result::OK, $checks[2]->get_status());    // Airnotifier configured.
  91          $this->assertEquals(\core\check\result::ERROR, $checks[3]->get_status());   // Airnotifier URL available.
  92          $this->assertEquals(\core\check\result::OK, $checks[4]->get_status());   // Invalid setting (empty space).
  93          // The original function fifth check (access key valid in the remote Airnotifier server) is not mockable.
  94          $this->assertEquals(\core\check\result::WARNING, $checks[5]->get_status());  // Only a few of default mobile notifications.
  95      }
  96  
  97      /** Test has_enabled_devices **/
  98      public function test_has_enabled_devices() {
  99          global $CFG, $DB, $USER;
 100          $this->resetAfterTest(true);
 101  
 102          $CFG->airnotifieraccesskey = 'test';    // For mocking the request.
 103          $manager = new message_airnotifier_manager();
 104  
 105          // No devices yet for current user.
 106          $this->assertFalse($manager->has_enabled_devices($CFG->airnotifiermobileappname));
 107  
 108          // Add devices.
 109          \curl::mock_response(json_encode(['status' => 'ok']));
 110          $DB->insert_record('user_devices',
 111              ['userid' => $USER->id, 'appid' => $CFG->airnotifiermobileappname, 'platform' => 'ios',
 112              'timecreated' => time(), 'timemodified' => time()]);
 113          $this->assertTrue($manager->has_enabled_devices($CFG->airnotifiermobileappname));
 114      }
 115  }