Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

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