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 310 and 311] [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403] [Versions 39 and 311]

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