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   * Exporter class tests.
  19   *
  20   * @package    core
  21   * @copyright  2015 Damyon Wiese
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  global $CFG;
  27  
  28  /**
  29   * Exporter testcase.
  30   *
  31   * @package    core
  32   * @copyright  2015 Damyon Wiese
  33   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  34   */
  35  class core_exporter_testcase extends advanced_testcase {
  36  
  37      protected $validrelated = null;
  38      protected $invalidrelated = null;
  39      protected $validdata = null;
  40      protected $invaliddata = null;
  41  
  42      public function setUp(): void {
  43          $s = new stdClass();
  44          $this->validrelated = array(
  45              'simplestdClass' => $s,
  46              'arrayofstdClass' => array($s, $s),
  47              'context' => null,
  48              'aint' => 5,
  49              'astring' => 'valid string',
  50              'abool' => false,
  51              'ints' => []
  52          );
  53          $this->invalidrelated = array(
  54              'simplestdClass' => 'a string',
  55              'arrayofstdClass' => 5,
  56              'context' => null,
  57              'aint' => false,
  58              'astring' => 4,
  59              'abool' => 'not a boolean',
  60              'ints' => null
  61          );
  62  
  63          $this->validdata = array('stringA' => 'A string', 'stringAformat' => FORMAT_HTML, 'intB' => 4);
  64  
  65          $this->invaliddata = array('stringA' => 'A string');
  66      }
  67  
  68      public function test_get_read_structure() {
  69          $structure = core_testable_exporter::get_read_structure();
  70  
  71          $this->assertInstanceOf('external_single_structure', $structure);
  72          $this->assertInstanceOf('external_value', $structure->keys['stringA']);
  73          $this->assertInstanceOf('external_format_value', $structure->keys['stringAformat']);
  74          $this->assertInstanceOf('external_value', $structure->keys['intB']);
  75          $this->assertInstanceOf('external_value', $structure->keys['otherstring']);
  76          $this->assertInstanceOf('external_multiple_structure', $structure->keys['otherstrings']);
  77      }
  78  
  79      public function test_get_create_structure() {
  80          $structure = core_testable_exporter::get_create_structure();
  81  
  82          $this->assertInstanceOf('external_single_structure', $structure);
  83          $this->assertInstanceOf('external_value', $structure->keys['stringA']);
  84          $this->assertInstanceOf('external_format_value', $structure->keys['stringAformat']);
  85          $this->assertInstanceOf('external_value', $structure->keys['intB']);
  86          $this->assertArrayNotHasKey('otherstring', $structure->keys);
  87          $this->assertArrayNotHasKey('otherstrings', $structure->keys);
  88      }
  89  
  90      public function test_get_update_structure() {
  91          $structure = core_testable_exporter::get_update_structure();
  92  
  93          $this->assertInstanceOf('external_single_structure', $structure);
  94          $this->assertInstanceOf('external_value', $structure->keys['stringA']);
  95          $this->assertInstanceOf('external_format_value', $structure->keys['stringAformat']);
  96          $this->assertInstanceOf('external_value', $structure->keys['intB']);
  97          $this->assertArrayNotHasKey('otherstring', $structure->keys);
  98          $this->assertArrayNotHasKey('otherstrings', $structure->keys);
  99      }
 100  
 101      public function test_invalid_data() {
 102          global $PAGE;
 103          $exporter = new core_testable_exporter($this->invaliddata, $this->validrelated);
 104          $output = $PAGE->get_renderer('core');
 105  
 106          // The exception message is a bit misleading, it actually indicates an expected property wasn't found.
 107          $this->expectException(coding_exception::class);
 108          $this->expectExceptionMessage('Unexpected property stringAformat');
 109          $result = $exporter->export($output);
 110      }
 111  
 112      public function test_invalid_related() {
 113          $this->expectException(coding_exception::class);
 114          $this->expectExceptionMessage('Exporter class is missing required related data: (core_testable_exporter) ' .
 115              'simplestdClass => stdClass');
 116          $exporter = new core_testable_exporter($this->validdata, $this->invalidrelated);
 117      }
 118  
 119      public function test_invalid_related_all_cases() {
 120          global $PAGE;
 121  
 122          foreach ($this->invalidrelated as $key => $value) {
 123              $data = $this->validrelated;
 124              $data[$key] = $value;
 125  
 126              try {
 127                  $exporter = new core_testable_exporter($this->validdata, $data);
 128                  $output = $PAGE->get_renderer('core');
 129                  $result = $exporter->export($output);
 130              } catch (coding_exception $e) {
 131                  $this->assertNotFalse(strpos($e->getMessage(), $key));
 132              }
 133          }
 134      }
 135  
 136      public function test_valid_data_and_related() {
 137          global $PAGE;
 138          $output = $PAGE->get_renderer('core');
 139          $exporter = new core_testable_exporter($this->validdata, $this->validrelated);
 140          $result = $exporter->export($output);
 141          $this->assertSame('>Another string', $result->otherstring);
 142          $this->assertSame(array('String &gt;a', 'String b'), $result->otherstrings);
 143      }
 144  
 145      public function test_format_text() {
 146          global $PAGE;
 147  
 148          $this->resetAfterTest();
 149          $course = $this->getDataGenerator()->create_course();
 150          $syscontext = context_system::instance();
 151          $coursecontext = context_course::instance($course->id);
 152  
 153          external_settings::get_instance()->set_filter(true);
 154          filter_set_global_state('urltolink', TEXTFILTER_OFF);
 155          filter_set_local_state('urltolink', $coursecontext->id, TEXTFILTER_ON);
 156          set_config('formats', FORMAT_MARKDOWN, 'filter_urltolink');
 157          filter_manager::reset_caches();
 158  
 159          $data = [
 160              'stringA' => '__Watch out:__ https://moodle.org @@PLUGINFILE@@/test.pdf',
 161              'stringAformat' => FORMAT_MARKDOWN,
 162              'intB' => 1
 163          ];
 164  
 165          // Export simulated in the system context.
 166          $output = $PAGE->get_renderer('core');
 167          $exporter = new core_testable_exporter($data, ['context' => $syscontext] + $this->validrelated);
 168          $result = $exporter->export($output);
 169  
 170          $youtube = 'https://moodle.org';
 171          $fileurl = (new moodle_url('/webservice/pluginfile.php/' . $syscontext->id . '/test/area/9/test.pdf'))->out(false);
 172          $expected = "<p><strong>Watch out:</strong> $youtube $fileurl</p>\n";
 173          $this->assertEquals($expected, $result->stringA);
 174          $this->assertEquals(FORMAT_HTML, $result->stringAformat);
 175  
 176          // Export simulated in the course context where the filter is enabled.
 177          $exporter = new core_testable_exporter($data, ['context' => $coursecontext] + $this->validrelated);
 178          $result = $exporter->export($output);
 179          $youtube = '<a href="https://moodle.org" class="_blanktarget">https://moodle.org</a>';
 180          $fileurl = (new moodle_url('/webservice/pluginfile.php/' . $coursecontext->id . '/test/area/9/test.pdf'))->out(false);
 181          $expected = "<p><strong>Watch out:</strong> $youtube <a href=\"$fileurl\" class=\"_blanktarget\">$fileurl</a></p>\n";
 182          $this->assertEquals($expected, $result->stringA);
 183          $this->assertEquals(FORMAT_HTML, $result->stringAformat);
 184      }
 185  
 186      public function test_properties_description() {
 187          $properties = core_testable_exporter::read_properties_definition();
 188          // Properties default description.
 189          $this->assertEquals('stringA', $properties['stringA']['description']);
 190          $this->assertEquals('stringAformat', $properties['stringAformat']['description']);
 191          // Properties custom description.
 192          $this->assertEquals('intB description', $properties['intB']['description']);
 193          // Other properties custom description.
 194          $this->assertEquals('otherstring description', $properties['otherstring']['description']);
 195          // Other properties default description.
 196          $this->assertEquals('otherstrings', $properties['otherstrings']['description']);
 197          // Assert nested elements are formatted correctly.
 198          $this->assertEquals('id', $properties['nestedarray']['type']['id']['description']);
 199      }
 200  }
 201  
 202  /**
 203   * Example persistent class.
 204   *
 205   * @package    core
 206   * @copyright  2015 Frédéric Massart - FMCorz.net
 207   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 208   */
 209  class core_testable_exporter extends \core\external\exporter {
 210  
 211      protected static function define_related() {
 212          // We cache the context so it does not need to be retrieved from the course.
 213          return array('simplestdClass' => 'stdClass', 'arrayofstdClass' => 'stdClass[]', 'context' => 'context?',
 214              'astring' => 'string', 'abool' => 'bool', 'aint' => 'int', 'ints' => 'int[]');
 215      }
 216  
 217      protected function get_other_values(renderer_base $output) {
 218          return array(
 219              'otherstring' => '>Another <strong>string</strong>',
 220              'otherstrings' => array('String >a', 'String <strong>b</strong>')
 221          );
 222      }
 223  
 224      public static function define_properties() {
 225          return array(
 226              'stringA' => array(
 227                  'type' => PARAM_RAW,
 228              ),
 229              'stringAformat' => array(
 230                  'type' => PARAM_INT,
 231              ),
 232              'intB' => array(
 233                  'type' => PARAM_INT,
 234                  'description' => 'intB description',
 235              )
 236          );
 237      }
 238  
 239      public static function define_other_properties() {
 240          return array(
 241              'otherstring' => array(
 242                  'type' => PARAM_TEXT,
 243                  'description' => 'otherstring description',
 244              ),
 245              'otherstrings' => array(
 246                  'type' => PARAM_TEXT,
 247                  'multiple' => true
 248              ),
 249              'nestedarray' => array(
 250                  'multiple' => true,
 251                  'optional' => true,
 252                  'type' => [
 253                      'id' => ['type' => PARAM_INT]
 254                  ]
 255              )
 256          );
 257      }
 258  
 259      protected function get_format_parameters_for_stringA() {
 260          return [
 261              // For testing use the passed context if any.
 262              'context' => isset($this->related['context']) ? $this->related['context'] : context_system::instance(),
 263              'component' => 'test',
 264              'filearea' => 'area',
 265              'itemid' => 9,
 266          ];
 267      }
 268  
 269      protected function get_format_parameters_for_otherstring() {
 270          return [
 271              'context' => context_system::instance(),
 272              'options' => ['escape' => false]
 273          ];
 274      }
 275  
 276      protected function get_format_parameters_for_otherstrings() {
 277          return [
 278              'context' => context_system::instance(),
 279          ];
 280      }
 281  }