Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 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 39 and 310] [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 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  /**
  18   * Moodle Mobile admin tool api tests.
  19   *
  20   * @package    tool_mobile
  21   * @category   external
  22   * @copyright  2016 Juan Leyva
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   * @since      Moodle 3.1
  25   */
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  global $CFG;
  30  
  31  require_once($CFG->dirroot . '/webservice/tests/helpers.php');
  32  
  33  use tool_mobile\api;
  34  
  35  /**
  36   * Moodle Mobile admin tool api tests.
  37   *
  38   * @package     tool_mobile
  39   * @copyright   2016 Juan Leyva
  40   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  41   * @since       Moodle 3.1
  42   */
  43  class tool_mobile_api_testcase extends externallib_advanced_testcase {
  44  
  45      /**
  46       * Test get_autologin_key.
  47       */
  48      public function test_get_autologin_key() {
  49          global $USER, $DB;
  50  
  51          $this->resetAfterTest(true);
  52          $this->setAdminUser();
  53  
  54          // Set server timezone for test.
  55          $this->setTimezone('UTC');
  56          // SEt user to GMT+5.
  57          $USER->timezone = 5;
  58  
  59          $timenow = $this->setCurrentTimeStart();
  60          $key = api::get_autologin_key();
  61  
  62          $key = $DB->get_record('user_private_key', array('value' => $key), '*', MUST_EXIST);
  63          $this->assertTimeCurrent($key->validuntil - api::LOGIN_KEY_TTL);
  64          $this->assertEquals('0.0.0.0', $key->iprestriction);
  65      }
  66  
  67      /**
  68       * Test get_potential_config_issues.
  69       */
  70      public function test_get_potential_config_issues() {
  71          global $CFG;
  72  
  73          $this->resetAfterTest(true);
  74          $this->setAdminUser();
  75  
  76          // Set non-SSL wwwroot, to avoid spurious certificate checking.
  77          $CFG->wwwroot = 'http://www.example.com';
  78          $CFG->userquota = '73289234723498234723423489273423497234234';
  79          $CFG->debugdisplay = 1;
  80  
  81          set_config('debugauthdb', 1, 'auth_db');
  82          set_config('debugdb', 1, 'enrol_database');
  83  
  84          // Get potential issues, obtain their keys for comparison.
  85          $issues = api::get_potential_config_issues();
  86          $issuekeys = array_column($issues, 0);
  87  
  88          $this->assertEqualsCanonicalizing([
  89              'nohttpsformobilewarning',
  90              'invaliduserquotawarning',
  91              'adodbdebugwarning',
  92              'displayerrorswarning',
  93          ], $issuekeys);
  94      }
  95  
  96      /**
  97       * Test pre_processor_message_send callback.
  98       */
  99      public function test_pre_processor_message_send_callback() {
 100          global $DB, $CFG;
 101          require_once($CFG->libdir . '/externallib.php');
 102          $this->preventResetByRollback();
 103          $this->resetAfterTest();
 104  
 105          // Enable mobile services and required configuration.
 106          $CFG->enablewebservices = 1;
 107          $CFG->enablemobilewebservice = 1;
 108          $mobileappdownloadpage = 'htt://mobileappdownloadpage';
 109          set_config('setuplink', $mobileappdownloadpage, 'tool_mobile');
 110  
 111          $user1 = $this->getDataGenerator()->create_user(array('maildisplay' => 1));
 112          $user2 = $this->getDataGenerator()->create_user();
 113          set_config('allowedemaildomains', 'example.com');
 114  
 115          $DB->set_field_select('message_processors', 'enabled', 0, "name <> 'email'");
 116          set_user_preference('message_provider_moodle_instantmessage_loggedoff', 'email', $user2);
 117  
 118          // Extra content for all types of messages.
 119          $message = new \core\message\message();
 120          $message->courseid          = 1;
 121          $message->component         = 'moodle';
 122          $message->name              = 'instantmessage';
 123          $message->userfrom          = $user1;
 124          $message->userto            = $user2;
 125          $message->subject           = 'message subject 1';
 126          $message->fullmessage       = 'message body';
 127          $message->fullmessageformat = FORMAT_MARKDOWN;
 128          $message->fullmessagehtml   = '<p>message body</p>';
 129          $message->smallmessage      = 'small message';
 130          $message->notification      = '0';
 131          $content = array('*' => array('header' => ' test ', 'footer' => ' test '));
 132          $message->set_additional_content('email', $content);
 133  
 134          $sink = $this->redirectEmails();
 135          $messageid = message_send($message);
 136          $emails = $sink->get_messages();
 137          $this->assertCount(1, $emails);
 138          $email = reset($emails);
 139  
 140          // Check we got the promotion text.
 141          $this->assertContains($mobileappdownloadpage, quoted_printable_decode($email->body));
 142          $sink->clear();
 143  
 144          // Disable mobile so we don't get mobile promotions.
 145          $CFG->enablemobilewebservice = 0;
 146          $messageid = message_send($message);
 147          $emails = $sink->get_messages();
 148          $this->assertCount(1, $emails);
 149          $email = reset($emails);
 150          // Check we don't get the promotion text.
 151          $this->assertNotContains($mobileappdownloadpage, quoted_printable_decode($email->body));
 152          $sink->clear();
 153  
 154          // Enable mobile again and set current user mobile token so we don't get mobile promotions.
 155          $CFG->enablemobilewebservice = 1;
 156          $user3 = $this->getDataGenerator()->create_user();
 157          $this->setUser($user3);
 158          $service = $DB->get_record('external_services', array('shortname' => MOODLE_OFFICIAL_MOBILE_SERVICE));
 159          $token = external_generate_token_for_current_user($service);
 160  
 161          $message->userto = $user3;
 162          $messageid = message_send($message);
 163          $emails = $sink->get_messages();
 164          $this->assertCount(1, $emails);
 165          $email = reset($emails);
 166          // Check we don't get the promotion text.
 167          $this->assertNotContains($mobileappdownloadpage, quoted_printable_decode($email->body));
 168          $sink->clear();
 169          $sink->close();
 170      }
 171  }