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   * 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          require_once($CFG->dirroot . '/message/lib.php');
  73  
  74          $this->resetAfterTest(true);
  75          $this->setAdminUser();
  76  
  77          $CFG->userquota = '73289234723498234723423489273423497234234';
  78          $CFG->debugdisplay = 1;
  79          set_config('debugauthdb', 1, 'auth_db');
  80          set_config('debugdb', 1, 'enrol_database');
  81          $expectedissues = array('nohttpsformobilewarning', 'invaliduserquotawarning', 'adodbdebugwarning', 'displayerrorswarning');
  82  
  83          $issues = api::get_potential_config_issues();
  84          $this->assertCount(count($expectedissues), $issues);
  85          foreach ($issues as $issue) {
  86              $this->assertTrue(in_array($issue[0], $expectedissues));
  87          }
  88      }
  89  
  90      /**
  91       * Test pre_processor_message_send callback.
  92       */
  93      public function test_pre_processor_message_send_callback() {
  94          global $DB, $CFG;
  95          require_once($CFG->libdir . '/externallib.php');
  96          $this->preventResetByRollback();
  97          $this->resetAfterTest();
  98  
  99          // Enable mobile services and required configuration.
 100          $CFG->enablewebservices = 1;
 101          $CFG->enablemobilewebservice = 1;
 102          $mobileappdownloadpage = 'htt://mobileappdownloadpage';
 103          set_config('setuplink', $mobileappdownloadpage, 'tool_mobile');
 104  
 105          $user1 = $this->getDataGenerator()->create_user(array('maildisplay' => 1));
 106          $user2 = $this->getDataGenerator()->create_user();
 107          set_config('allowedemaildomains', 'example.com');
 108  
 109          $DB->set_field_select('message_processors', 'enabled', 0, "name <> 'email'");
 110          set_user_preference('message_provider_moodle_instantmessage_loggedoff', 'email', $user2);
 111  
 112          // Extra content for all types of messages.
 113          $message = new \core\message\message();
 114          $message->courseid          = 1;
 115          $message->component         = 'moodle';
 116          $message->name              = 'instantmessage';
 117          $message->userfrom          = $user1;
 118          $message->userto            = $user2;
 119          $message->subject           = 'message subject 1';
 120          $message->fullmessage       = 'message body';
 121          $message->fullmessageformat = FORMAT_MARKDOWN;
 122          $message->fullmessagehtml   = '<p>message body</p>';
 123          $message->smallmessage      = 'small message';
 124          $message->notification      = '0';
 125          $content = array('*' => array('header' => ' test ', 'footer' => ' test '));
 126          $message->set_additional_content('email', $content);
 127  
 128          $sink = $this->redirectEmails();
 129          $messageid = message_send($message);
 130          $emails = $sink->get_messages();
 131          $this->assertCount(1, $emails);
 132          $email = reset($emails);
 133  
 134          // Check we got the promotion text.
 135          $this->assertStringContainsString($mobileappdownloadpage, quoted_printable_decode($email->body));
 136          $sink->clear();
 137  
 138          // Disable mobile so we don't get mobile promotions.
 139          $CFG->enablemobilewebservice = 0;
 140          $messageid = message_send($message);
 141          $emails = $sink->get_messages();
 142          $this->assertCount(1, $emails);
 143          $email = reset($emails);
 144          // Check we don't get the promotion text.
 145          $this->assertStringNotContainsString($mobileappdownloadpage, quoted_printable_decode($email->body));
 146          $sink->clear();
 147  
 148          // Enable mobile again and set current user mobile token so we don't get mobile promotions.
 149          $CFG->enablemobilewebservice = 1;
 150          $user3 = $this->getDataGenerator()->create_user();
 151          $this->setUser($user3);
 152          $service = $DB->get_record('external_services', array('shortname' => MOODLE_OFFICIAL_MOBILE_SERVICE));
 153          $token = external_generate_token_for_current_user($service);
 154  
 155          $message->userto = $user3;
 156          $messageid = message_send($message);
 157          $emails = $sink->get_messages();
 158          $this->assertCount(1, $emails);
 159          $email = reset($emails);
 160          // Check we don't get the promotion text.
 161          $this->assertStringNotContainsString($mobileappdownloadpage, quoted_printable_decode($email->body));
 162          $sink->clear();
 163          $sink->close();
 164      }
 165  }