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 core;
  18  
  19  defined('MOODLE_INTERNAL') || die();
  20  
  21  global $CFG;
  22  require_once($CFG->libdir . '/externallib.php');
  23  
  24  /**
  25   * Unit tests for /lib/externallib.php.
  26   *
  27   * @package    core
  28   * @subpackage phpunit
  29   * @copyright  2009 Petr Skoda {@link http://skodak.org}
  30   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  31   */
  32  class externallib_test extends \advanced_testcase {
  33      protected $DB;
  34  
  35      public function setUp(): void {
  36          $this->DB = null;
  37      }
  38  
  39      public function tearDown(): void {
  40          global $DB;
  41          if ($this->DB !== null) {
  42              $DB = $this->DB;
  43          }
  44      }
  45  
  46      /**
  47       * Tests for external_settings class.
  48       */
  49      public function test_external_settings() {
  50  
  51          $settings = \external_settings::get_instance();
  52          $currentraw = $settings->get_raw();
  53          $currentfilter = $settings->get_filter();
  54          $currentfile = $settings->get_file();
  55          $currentfileurl = $settings->get_fileurl();
  56  
  57          $this->assertInstanceOf(\external_settings::class, $settings);
  58  
  59          // Check apis.
  60          $settings->set_file('plugin.php');
  61          $this->assertEquals('plugin.php', $settings->get_file());
  62          $settings->set_filter(false);
  63          $this->assertFalse($settings->get_filter());
  64          $settings->set_fileurl(false);
  65          $this->assertFalse($settings->get_fileurl());
  66          $settings->set_raw(true);
  67          $this->assertTrue($settings->get_raw());
  68  
  69          // Restore original values.
  70          $settings->set_file($currentfile);
  71          $settings->set_filter($currentfilter);
  72          $settings->set_fileurl($currentfileurl);
  73          $settings->set_raw($currentraw);
  74      }
  75  
  76      public function test_validate_params() {
  77          $params = array('text'=>'aaa', 'someid'=>'6');
  78          $description = new \external_function_parameters(array('someid' => new \external_value(PARAM_INT, 'Some int value'),
  79              'text'   => new \external_value(PARAM_ALPHA, 'Some text value')));
  80          $result = \external_api::validate_parameters($description, $params);
  81          $this->assertCount(2, $result);
  82          reset($result);
  83          $this->assertSame('someid', key($result));
  84          $this->assertSame(6, $result['someid']);
  85          $this->assertSame('aaa', $result['text']);
  86  
  87          $params = array('someids'=>array('1', 2, 'a'=>'3'), 'scalar'=>666);
  88          $description = new \external_function_parameters(array('someids' => new \external_multiple_structure(new \external_value(PARAM_INT, 'Some ID')),
  89              'scalar'  => new \external_value(PARAM_ALPHANUM, 'Some text value')));
  90          $result = \external_api::validate_parameters($description, $params);
  91          $this->assertCount(2, $result);
  92          reset($result);
  93          $this->assertSame('someids', key($result));
  94          $this->assertEquals(array(0=>1, 1=>2, 2=>3), $result['someids']);
  95          $this->assertSame('666', $result['scalar']);
  96  
  97          $params = array('text'=>'aaa');
  98          $description = new \external_function_parameters(array('someid' => new \external_value(PARAM_INT, 'Some int value', false),
  99              'text'   => new \external_value(PARAM_ALPHA, 'Some text value')));
 100          $result = \external_api::validate_parameters($description, $params);
 101          $this->assertCount(2, $result);
 102          reset($result);
 103          $this->assertSame('someid', key($result));
 104          $this->assertNull($result['someid']);
 105          $this->assertSame('aaa', $result['text']);
 106  
 107          $params = array('text'=>'aaa');
 108          $description = new \external_function_parameters(array('someid' => new \external_value(PARAM_INT, 'Some int value', false, 6),
 109              'text'   => new \external_value(PARAM_ALPHA, 'Some text value')));
 110          $result = \external_api::validate_parameters($description, $params);
 111          $this->assertCount(2, $result);
 112          reset($result);
 113          $this->assertSame('someid', key($result));
 114          $this->assertSame(6, $result['someid']);
 115          $this->assertSame('aaa', $result['text']);
 116      }
 117  
 118      public function test_external_format_text() {
 119          $settings = \external_settings::get_instance();
 120  
 121          $currentraw = $settings->get_raw();
 122          $currentfilter = $settings->get_filter();
 123  
 124          $settings->set_raw(true);
 125          $settings->set_filter(false);
 126          $context = \context_system::instance();
 127  
 128          $test = '$$ \pi $$';
 129          $testformat = FORMAT_MARKDOWN;
 130          $correct = array($test, $testformat);
 131          // Function external_format_text should work with context id or context instance.
 132          $this->assertSame(external_format_text($test, $testformat, $context->id, 'core', '', 0), $correct);
 133          $this->assertSame(external_format_text($test, $testformat, $context, 'core', '', 0), $correct);
 134  
 135          $settings->set_raw(false);
 136          $settings->set_filter(true);
 137  
 138          $test = '$$ \pi $$';
 139          $testformat = FORMAT_MARKDOWN;
 140          $correct = array('<span class="filter_mathjaxloader_equation"><p><span class="nolink">$$ \pi $$</span></p>
 141  </span>', FORMAT_HTML);
 142          // Function external_format_text should work with context id or context instance.
 143          $this->assertSame(external_format_text($test, $testformat, $context->id, 'core', '', 0), $correct);
 144          $this->assertSame(external_format_text($test, $testformat, $context, 'core', '', 0), $correct);
 145  
 146          // Filters can be opted out from by the developer.
 147          $test = '$$ \pi $$';
 148          $testformat = FORMAT_MARKDOWN;
 149          $correct = array('<p>$$ \pi $$</p>
 150  ', FORMAT_HTML);
 151          // Function external_format_text should work with context id or context instance.
 152          $this->assertSame(external_format_text($test, $testformat, $context->id, 'core', '', 0, ['filter' => false]), $correct);
 153          $this->assertSame(external_format_text($test, $testformat, $context, 'core', '', 0, ['filter' => false]), $correct);
 154  
 155          $test = '<p><a id="test"></a><a href="#test">Text</a></p>';
 156          $testformat = FORMAT_HTML;
 157          $correct = array($test, FORMAT_HTML);
 158          $options = array('allowid' => true);
 159          // Function external_format_text should work with context id or context instance.
 160          $this->assertSame(external_format_text($test, $testformat, $context->id, 'core', '', 0, $options), $correct);
 161          $this->assertSame(external_format_text($test, $testformat, $context, 'core', '', 0, $options), $correct);
 162  
 163          $test = '<p><a id="test"></a><a href="#test">Text</a></p>';
 164          $testformat = FORMAT_HTML;
 165          $correct = array('<p><a></a><a href="#test">Text</a></p>', FORMAT_HTML);
 166          $options = new \stdClass();
 167          $options->allowid = false;
 168          // Function external_format_text should work with context id or context instance.
 169          $this->assertSame(external_format_text($test, $testformat, $context->id, 'core', '', 0, $options), $correct);
 170          $this->assertSame(external_format_text($test, $testformat, $context, 'core', '', 0, $options), $correct);
 171  
 172          $test = '<p><a id="test"></a><a href="#test">Text</a></p>'."\n".'Newline';
 173          $testformat = FORMAT_MOODLE;
 174          $correct = array('<p><a id="test"></a><a href="#test">Text</a></p> Newline', FORMAT_HTML);
 175          $options = new \stdClass();
 176          $options->newlines = false;
 177          // Function external_format_text should work with context id or context instance.
 178          $this->assertSame(external_format_text($test, $testformat, $context->id, 'core', '', 0, $options), $correct);
 179          $this->assertSame(external_format_text($test, $testformat, $context, 'core', '', 0, $options), $correct);
 180  
 181          $test = '<p><a id="test"></a><a href="#test">Text</a></p>';
 182          $testformat = FORMAT_MOODLE;
 183          $correct = array('<div class="text_to_html">'.$test.'</div>', FORMAT_HTML);
 184          $options = new \stdClass();
 185          $options->para = true;
 186          // Function external_format_text should work with context id or context instance.
 187          $this->assertSame(external_format_text($test, $testformat, $context->id, 'core', '', 0, $options), $correct);
 188          $this->assertSame(external_format_text($test, $testformat, $context, 'core', '', 0, $options), $correct);
 189  
 190          $test = '<p><a id="test"></a><a href="#test">Text</a></p>';
 191          $testformat = FORMAT_MOODLE;
 192          $correct = array($test, FORMAT_HTML);
 193          $options = new \stdClass();
 194          $options->context = $context;
 195          // Function external_format_text should work with context id or context instance.
 196          $this->assertSame(external_format_text($test, $testformat, $context->id, 'core', '', 0, $options), $correct);
 197          $this->assertSame(external_format_text($test, $testformat, $context, 'core', '', 0, $options), $correct);
 198  
 199          $settings->set_raw($currentraw);
 200          $settings->set_filter($currentfilter);
 201      }
 202  
 203      public function test_external_format_string() {
 204          $this->resetAfterTest();
 205          $settings = \external_settings::get_instance();
 206          $currentraw = $settings->get_raw();
 207          $currentfilter = $settings->get_filter();
 208  
 209          // Enable multilang filter to on content and heading.
 210          filter_set_global_state('multilang', TEXTFILTER_ON);
 211          filter_set_applies_to_strings('multilang', 1);
 212          $filtermanager = \filter_manager::instance();
 213          $filtermanager->reset_caches();
 214  
 215          $settings->set_raw(true);
 216          $settings->set_filter(true);
 217          $context = \context_system::instance();
 218  
 219          $test = '<span lang="en" class="multilang">EN</span><span lang="fr" class="multilang">FR</span> ' .
 220              '<script>hi</script> <h3>there</h3>!';
 221          $correct = $test;
 222          // Function external_format_string should work with context id or context instance.
 223          $this->assertSame($correct, external_format_string($test, $context->id));
 224          $this->assertSame($correct, external_format_string($test, $context));
 225  
 226          $settings->set_raw(false);
 227          $settings->set_filter(false);
 228  
 229          $test = '<span lang="en" class="multilang">EN</span><span lang="fr" class="multilang">FR</span> ' .
 230              '<script>hi</script> <h3>there</h3>?';
 231          $correct = 'ENFR hi there?';
 232          // Function external_format_string should work with context id or context instance.
 233          $this->assertSame($correct, external_format_string($test, $context->id));
 234          $this->assertSame($correct, external_format_string($test, $context));
 235  
 236          $settings->set_filter(true);
 237  
 238          $test = '<span lang="en" class="multilang">EN</span><span lang="fr" class="multilang">FR</span> ' .
 239              '<script>hi</script> <h3>there</h3>@';
 240          $correct = 'EN hi there@';
 241          // Function external_format_string should work with context id or context instance.
 242          $this->assertSame($correct, external_format_string($test, $context->id));
 243          $this->assertSame($correct, external_format_string($test, $context));
 244  
 245          // Filters can be opted out.
 246          $test = '<span lang="en" class="multilang">EN</span><span lang="fr" class="multilang">FR</span> ' .
 247              '<script>hi</script> <h3>there</h3>%';
 248          $correct = 'ENFR hi there%';
 249          // Function external_format_string should work with context id or context instance.
 250          $this->assertSame($correct, external_format_string($test, $context->id, false, ['filter' => false]));
 251          $this->assertSame($correct, external_format_string($test, $context, false, ['filter' => false]));
 252  
 253          $this->assertSame("& < > \" '", format_string("& < > \" '", true, ['escape' => false]));
 254  
 255          $settings->set_raw($currentraw);
 256          $settings->set_filter($currentfilter);
 257      }
 258  
 259      /**
 260       * Test for clean_returnvalue() for testing that returns the PHP type.
 261       */
 262      public function test_clean_returnvalue_return_php_type() {
 263  
 264          $returndesc = new \external_single_structure(
 265              array(
 266                  'value' => new \external_value(PARAM_RAW, 'Some text', VALUE_OPTIONAL, null, NULL_NOT_ALLOWED)
 267              )
 268          );
 269  
 270          // Check return type on exception because the external values does not allow NULL values.
 271          $testdata = array('value' => null);
 272          try {
 273              $cleanedvalue = \external_api::clean_returnvalue($returndesc, $testdata);
 274          } catch (\moodle_exception $e) {
 275              $this->assertInstanceOf(\invalid_response_exception::class, $e);
 276              $this->assertStringContainsString('of PHP type "NULL"', $e->debuginfo);
 277          }
 278      }
 279  
 280      /**
 281       * Test for clean_returnvalue().
 282       */
 283      public function test_clean_returnvalue() {
 284  
 285          // Build some return value decription.
 286          $returndesc = new \external_multiple_structure(
 287              new \external_single_structure(
 288                  array(
 289                      'object' => new \external_single_structure(
 290                                  array('value1' => new \external_value(PARAM_INT, 'this is a int'))),
 291                      'value2' => new \external_value(PARAM_TEXT, 'some text', VALUE_OPTIONAL))
 292              ));
 293  
 294          // Clean an object (it should be cast into an array).
 295          $object = new \stdClass();
 296          $object->value1 = 1;
 297          $singlestructure['object'] = $object;
 298          $singlestructure['value2'] = 'Some text';
 299          $testdata = array($singlestructure);
 300          $cleanedvalue = \external_api::clean_returnvalue($returndesc, $testdata);
 301          $cleanedsinglestructure = array_pop($cleanedvalue);
 302          $this->assertSame($object->value1, $cleanedsinglestructure['object']['value1']);
 303          $this->assertSame($singlestructure['value2'], $cleanedsinglestructure['value2']);
 304  
 305          // Missing VALUE_OPTIONAL.
 306          $object = new \stdClass();
 307          $object->value1 = 1;
 308          $singlestructure = new \stdClass();
 309          $singlestructure->object = $object;
 310          $testdata = array($singlestructure);
 311          $cleanedvalue = \external_api::clean_returnvalue($returndesc, $testdata);
 312          $cleanedsinglestructure = array_pop($cleanedvalue);
 313          $this->assertSame($object->value1, $cleanedsinglestructure['object']['value1']);
 314          $this->assertArrayNotHasKey('value2', $cleanedsinglestructure);
 315  
 316          // Unknown attribute (the value should be ignored).
 317          $object = array();
 318          $object['value1'] = 1;
 319          $singlestructure = array();
 320          $singlestructure['object'] = $object;
 321          $singlestructure['value2'] = 'Some text';
 322          $singlestructure['unknownvalue'] = 'Some text to ignore';
 323          $testdata = array($singlestructure);
 324          $cleanedvalue = \external_api::clean_returnvalue($returndesc, $testdata);
 325          $cleanedsinglestructure = array_pop($cleanedvalue);
 326          $this->assertSame($object['value1'], $cleanedsinglestructure['object']['value1']);
 327          $this->assertSame($singlestructure['value2'], $cleanedsinglestructure['value2']);
 328          $this->assertArrayNotHasKey('unknownvalue', $cleanedsinglestructure);
 329  
 330          // Missing required value (an exception is thrown).
 331          $object = array();
 332          $singlestructure = array();
 333          $singlestructure['object'] = $object;
 334          $singlestructure['value2'] = 'Some text';
 335          $testdata = array($singlestructure);
 336          $this->expectException('invalid_response_exception');
 337          $cleanedvalue = \external_api::clean_returnvalue($returndesc, $testdata);
 338      }
 339      /*
 340       * Test \external_api::get_context_from_params().
 341       */
 342      public function test_get_context_from_params() {
 343          $this->resetAfterTest(true);
 344          $course = $this->getDataGenerator()->create_course();
 345          $realcontext = \context_course::instance($course->id);
 346  
 347          // Use context id.
 348          $fetchedcontext = test_exernal_api::get_context_wrapper(array("contextid" => $realcontext->id));
 349          $this->assertEquals($realcontext, $fetchedcontext);
 350  
 351          // Use context level and instance id.
 352          $fetchedcontext = test_exernal_api::get_context_wrapper(array("contextlevel" => "course", "instanceid" => $course->id));
 353          $this->assertEquals($realcontext, $fetchedcontext);
 354  
 355          // Passing empty values.
 356          try {
 357              $fetchedcontext = test_exernal_api::get_context_wrapper(array("contextid" => 0));
 358              $this->fail('Exception expected from get_context_wrapper()');
 359          } catch (\moodle_exception $e) {
 360              $this->assertInstanceOf(\invalid_parameter_exception::class, $e);
 361          }
 362  
 363          try {
 364              $fetchedcontext = test_exernal_api::get_context_wrapper(array("instanceid" => 0));
 365              $this->fail('Exception expected from get_context_wrapper()');
 366          } catch (\moodle_exception $e) {
 367              $this->assertInstanceOf(\invalid_parameter_exception::class, $e);
 368          }
 369  
 370          try {
 371              $fetchedcontext = test_exernal_api::get_context_wrapper(array("contextid" => null));
 372              $this->fail('Exception expected from get_context_wrapper()');
 373          } catch (\moodle_exception $e) {
 374              $this->assertInstanceOf(\invalid_parameter_exception::class, $e);
 375          }
 376  
 377          // Tests for context with instanceid equal to 0 (System context).
 378          $realcontext = \context_system::instance();
 379          $fetchedcontext = test_exernal_api::get_context_wrapper(array("contextlevel" => "system", "instanceid" => 0));
 380          $this->assertEquals($realcontext, $fetchedcontext);
 381  
 382          // Passing wrong level.
 383          $this->expectException('invalid_parameter_exception');
 384          $fetchedcontext = test_exernal_api::get_context_wrapper(array("contextlevel" => "random", "instanceid" => $course->id));
 385      }
 386  
 387      /*
 388       * Test \external_api::get_context()_from_params parameter validation.
 389       */
 390      public function test_get_context_params() {
 391          global $USER;
 392  
 393          // Call without correct context details.
 394          $this->expectException('invalid_parameter_exception');
 395          test_exernal_api::get_context_wrapper(array('roleid' => 3, 'userid' => $USER->id));
 396      }
 397  
 398      /*
 399       * Test \external_api::get_context()_from_params parameter validation.
 400       */
 401      public function test_get_context_params2() {
 402          global $USER;
 403  
 404          // Call without correct context details.
 405          $this->expectException('invalid_parameter_exception');
 406          test_exernal_api::get_context_wrapper(array('roleid' => 3, 'userid' => $USER->id, 'contextlevel' => "course"));
 407      }
 408  
 409      /*
 410       * Test \external_api::get_context()_from_params parameter validation.
 411       */
 412      public function test_get_context_params3() {
 413          global $USER;
 414  
 415          // Call without correct context details.
 416          $this->resetAfterTest(true);
 417          $course = self::getDataGenerator()->create_course();
 418          $this->expectException('invalid_parameter_exception');
 419          test_exernal_api::get_context_wrapper(array('roleid' => 3, 'userid' => $USER->id, 'instanceid' => $course->id));
 420      }
 421  
 422      public function all_external_info_provider() {
 423          global $DB;
 424  
 425          // We are testing here that all the external function descriptions can be generated without
 426          // producing warnings. E.g. misusing optional params will generate a debugging message which
 427          // will fail this test.
 428          $functions = $DB->get_records('external_functions', array(), 'name');
 429          $return = array();
 430          foreach ($functions as $f) {
 431              $return[$f->name] = array($f);
 432          }
 433          return $return;
 434      }
 435  
 436      /**
 437       * @dataProvider all_external_info_provider
 438       */
 439      public function test_all_external_info($f) {
 440          $desc = \external_api::external_function_info($f);
 441          $this->assertNotEmpty($desc->name);
 442          $this->assertNotEmpty($desc->classname);
 443          $this->assertNotEmpty($desc->methodname);
 444          $this->assertEquals($desc->component, clean_param($desc->component, PARAM_COMPONENT));
 445          $this->assertInstanceOf(\external_function_parameters::class, $desc->parameters_desc);
 446          if ($desc->returns_desc != null) {
 447              $this->assertInstanceOf(\external_description::class, $desc->returns_desc);
 448          }
 449      }
 450  
 451      public function test_validate_courses() {
 452          $this->resetAfterTest(true);
 453  
 454          $c1 = $this->getDataGenerator()->create_course();
 455          $c2 = $this->getDataGenerator()->create_course();
 456          $c3 = $this->getDataGenerator()->create_course();
 457          $u1 = $this->getDataGenerator()->create_user();
 458          $this->getDataGenerator()->enrol_user($u1->id, $c1->id);
 459          $courseids = array($c1->id, $c2->id, $c3->id);
 460  
 461          $this->setAdminUser();
 462          list($courses, $warnings) = \external_util::validate_courses($courseids);
 463          $this->assertEmpty($warnings);
 464          $this->assertCount(3, $courses);
 465          $this->assertArrayHasKey($c1->id, $courses);
 466          $this->assertArrayHasKey($c2->id, $courses);
 467          $this->assertArrayHasKey($c3->id, $courses);
 468          $this->assertEquals($c1->id, $courses[$c1->id]->id);
 469          $this->assertEquals($c2->id, $courses[$c2->id]->id);
 470          $this->assertEquals($c3->id, $courses[$c3->id]->id);
 471  
 472          $this->setUser($u1);
 473          list($courses, $warnings) = \external_util::validate_courses($courseids);
 474          $this->assertCount(2, $warnings);
 475          $this->assertEquals($c2->id, $warnings[0]['itemid']);
 476          $this->assertEquals($c3->id, $warnings[1]['itemid']);
 477          $this->assertCount(1, $courses);
 478          $this->assertArrayHasKey($c1->id, $courses);
 479          $this->assertArrayNotHasKey($c2->id, $courses);
 480          $this->assertArrayNotHasKey($c3->id, $courses);
 481          $this->assertEquals($c1->id, $courses[$c1->id]->id);
 482      }
 483  
 484      /**
 485       * Validate courses, but still return courses even if they fail validation.
 486       */
 487      public function test_validate_courses_keepfails() {
 488          $this->resetAfterTest(true);
 489  
 490          $c1 = $this->getDataGenerator()->create_course();
 491          $c2 = $this->getDataGenerator()->create_course();
 492          $c3 = $this->getDataGenerator()->create_course();
 493          $u1 = $this->getDataGenerator()->create_user();
 494          $this->getDataGenerator()->enrol_user($u1->id, $c1->id);
 495          $courseids = array($c1->id, $c2->id, $c3->id);
 496  
 497          $this->setUser($u1);
 498          list($courses, $warnings) = \external_util::validate_courses($courseids, [], false, true);
 499          $this->assertCount(2, $warnings);
 500          $this->assertEquals($c2->id, $warnings[0]['itemid']);
 501          $this->assertEquals($c3->id, $warnings[1]['itemid']);
 502          $this->assertCount(3, $courses);
 503          $this->assertTrue($courses[$c1->id]->contextvalidated);
 504          $this->assertFalse($courses[$c2->id]->contextvalidated);
 505          $this->assertFalse($courses[$c3->id]->contextvalidated);
 506      }
 507  
 508      /**
 509       * Validate courses can re-use an array of prefetched courses.
 510       */
 511      public function test_validate_courses_prefetch() {
 512          $this->resetAfterTest(true);
 513  
 514          $c1 = $this->getDataGenerator()->create_course();
 515          $c2 = $this->getDataGenerator()->create_course();
 516          $c3 = $this->getDataGenerator()->create_course();
 517          $c4 = $this->getDataGenerator()->create_course();
 518          $u1 = $this->getDataGenerator()->create_user();
 519          $this->getDataGenerator()->enrol_user($u1->id, $c1->id);
 520          $this->getDataGenerator()->enrol_user($u1->id, $c2->id);
 521  
 522          $courseids = array($c1->id, $c2->id, $c3->id);
 523          $courses = array($c2->id => $c2, $c3->id => $c3, $c4->id => $c4);
 524  
 525          $this->setUser($u1);
 526          list($courses, $warnings) = \external_util::validate_courses($courseids, $courses);
 527          $this->assertCount(2, $courses);
 528          $this->assertCount(1, $warnings);
 529          $this->assertArrayHasKey($c1->id, $courses);
 530          $this->assertSame($c2, $courses[$c2->id]);
 531          $this->assertArrayNotHasKey($c3->id, $courses);
 532          // The extra course passed is not returned.
 533          $this->assertArrayNotHasKey($c4->id, $courses);
 534      }
 535  
 536  
 537      public function test_call_external_function() {
 538          global $PAGE, $COURSE, $CFG;
 539  
 540          $this->resetAfterTest(true);
 541  
 542          // Call some webservice functions and verify they are correctly handling $PAGE and $COURSE.
 543          // First test a function that calls validate_context outside a course.
 544          $this->setAdminUser();
 545          $category = $this->getDataGenerator()->create_category();
 546          $params = array(
 547              'contextid' => \context_coursecat::instance($category->id)->id,
 548              'name' => 'aaagrrryyy',
 549              'idnumber' => '',
 550              'description' => ''
 551          );
 552          $cohort1 = $this->getDataGenerator()->create_cohort($params);
 553          $cohort2 = $this->getDataGenerator()->create_cohort();
 554  
 555          $beforepage = $PAGE;
 556          $beforecourse = $COURSE;
 557          $params = array('cohortids' => array($cohort1->id, $cohort2->id));
 558          $result = \external_api::call_external_function('core_cohort_get_cohorts', $params);
 559  
 560          $this->assertSame($beforepage, $PAGE);
 561          $this->assertSame($beforecourse, $COURSE);
 562  
 563          // Now test a function that calls validate_context inside a course.
 564          $course = $this->getDataGenerator()->create_course();
 565  
 566          $beforepage = $PAGE;
 567          $beforecourse = $COURSE;
 568          $params = array('courseid' => $course->id, 'options' => array());
 569          $result = \external_api::call_external_function('core_enrol_get_enrolled_users', $params);
 570  
 571          $this->assertSame($beforepage, $PAGE);
 572          $this->assertSame($beforecourse, $COURSE);
 573  
 574          // Test a function that triggers a PHP exception.
 575          require_once($CFG->dirroot . '/lib/tests/fixtures/test_external_function_throwable.php');
 576  
 577          // Call our test function.
 578          $result = \test_external_function_throwable::call_external_function('core_throw_exception', array(), false);
 579  
 580          $this->assertTrue($result['error']);
 581          $this->assertArrayHasKey('exception', $result);
 582          $this->assertEquals($result['exception']->message, 'Exception - Modulo by zero');
 583      }
 584  
 585      /**
 586       * Text \external_util::get_area_files
 587       */
 588      public function test_external_util_get_area_files() {
 589          global $CFG, $DB;
 590  
 591          $this->DB = $DB;
 592          $DB = $this->getMockBuilder('moodle_database')->getMock();
 593  
 594          $content = base64_encode("Let us create a nice simple file.");
 595          $timemodified = 102030405;
 596          $itemid = 42;
 597          $filesize = strlen($content);
 598  
 599          $DB->method('get_records_sql')->willReturn([
 600              (object) [
 601                  'filename'      => 'example.txt',
 602                  'filepath'      => '/',
 603                  'mimetype'      => 'text/plain',
 604                  'filesize'      => $filesize,
 605                  'timemodified'  => $timemodified,
 606                  'itemid'        => $itemid,
 607                  'pathnamehash'  => sha1('/example.txt'),
 608              ],
 609          ]);
 610  
 611          $component = 'mod_foo';
 612          $filearea = 'area';
 613          $context = 12345;
 614  
 615          $expectedfiles[] = array(
 616              'filename' => 'example.txt',
 617              'filepath' => '/',
 618              'fileurl' => "{$CFG->wwwroot}/webservice/pluginfile.php/{$context}/{$component}/{$filearea}/{$itemid}/example.txt",
 619              'timemodified' => $timemodified,
 620              'filesize' => $filesize,
 621              'mimetype' => 'text/plain',
 622              'isexternalfile' => false,
 623          );
 624          // Get all the files for the area.
 625          $files = \external_util::get_area_files($context, $component, $filearea, false);
 626          $this->assertEquals($expectedfiles, $files);
 627  
 628          $DB->method('get_in_or_equal')->willReturn([
 629              '= :mock1',
 630              ['mock1' => $itemid]
 631          ]);
 632  
 633          // Get just the file indicated by $itemid.
 634          $files = \external_util::get_area_files($context, $component, $filearea, $itemid);
 635          $this->assertEquals($expectedfiles, $files);
 636  
 637      }
 638  
 639      /**
 640       * Text external files structure.
 641       */
 642      public function test_external_files() {
 643  
 644          $description = new \external_files();
 645  
 646          // First check that the expected default values and keys are returned.
 647          $expectedkeys = array_flip(array('filename', 'filepath', 'filesize', 'fileurl', 'timemodified', 'mimetype',
 648              'isexternalfile', 'repositorytype'));
 649          $returnedkeys = array_flip(array_keys($description->content->keys));
 650          $this->assertEquals($expectedkeys, $returnedkeys);
 651          $this->assertEquals('List of files.', $description->desc);
 652          $this->assertEquals(VALUE_REQUIRED, $description->required);
 653          foreach ($description->content->keys as $key) {
 654              $this->assertEquals(VALUE_OPTIONAL, $key->required);
 655          }
 656  
 657      }
 658  
 659      /**
 660       * Test default time for user created tokens.
 661       */
 662      public function test_user_created_tokens_duration() {
 663          global $CFG, $DB;
 664          $this->resetAfterTest(true);
 665  
 666          $CFG->enablewebservices = 1;
 667          $CFG->enablemobilewebservice = 1;
 668          $user1 = $this->getDataGenerator()->create_user();
 669          $user2 = $this->getDataGenerator()->create_user();
 670          $service = $DB->get_record('external_services', array('shortname' => MOODLE_OFFICIAL_MOBILE_SERVICE, 'enabled' => 1));
 671  
 672          $this->setUser($user1);
 673          $timenow = time();
 674          $token = external_generate_token_for_current_user($service);
 675          $this->assertGreaterThanOrEqual($timenow + $CFG->tokenduration, $token->validuntil);
 676  
 677          // Change token default time.
 678          $this->setUser($user2);
 679          set_config('tokenduration', DAYSECS);
 680          $token = external_generate_token_for_current_user($service);
 681          $timenow = time();
 682          $this->assertLessThanOrEqual($timenow + DAYSECS, $token->validuntil);
 683      }
 684  }
 685  
 686  /*
 687   * Just a wrapper to access protected apis for testing
 688   */
 689  class test_exernal_api extends \external_api {
 690  
 691      public static function get_context_wrapper($params) {
 692          return self::get_context_from_params($params);
 693      }
 694  }