See Release Notes
Long Term Support Release
Differences Between: [Versions 39 and 310] [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 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 /** 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() { 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 /** 102 * @expectedException coding_exception 103 */ 104 public function test_invalid_data() { 105 global $PAGE; 106 $exporter = new core_testable_exporter($this->invaliddata, $this->validrelated); 107 $output = $PAGE->get_renderer('core'); 108 109 $result = $exporter->export($output); 110 } 111 112 /** 113 * @expectedException coding_exception 114 */ 115 public function test_invalid_related() { 116 global $PAGE; 117 $exporter = new core_testable_exporter($this->validdata, $this->invalidrelated); 118 $output = $PAGE->get_renderer('core'); 119 120 $result = $exporter->export($output); 121 } 122 123 public function test_invalid_related_all_cases() { 124 global $PAGE; 125 126 foreach ($this->invalidrelated as $key => $value) { 127 $data = $this->validrelated; 128 $data[$key] = $value; 129 130 try { 131 $exporter = new core_testable_exporter($this->validdata, $data); 132 $output = $PAGE->get_renderer('core'); 133 $result = $exporter->export($output); 134 } catch (coding_exception $e) { 135 $this->assertNotFalse(strpos($e->getMessage(), $key)); 136 } 137 } 138 } 139 140 public function test_valid_data_and_related() { 141 global $PAGE; 142 $output = $PAGE->get_renderer('core'); 143 $exporter = new core_testable_exporter($this->validdata, $this->validrelated); 144 $result = $exporter->export($output); 145 $this->assertSame('>Another string', $result->otherstring); 146 $this->assertSame(array('String >a', 'String b'), $result->otherstrings); 147 } 148 149 public function test_format_text() { 150 global $PAGE; 151 152 $this->resetAfterTest(); 153 $course = $this->getDataGenerator()->create_course(); 154 $syscontext = context_system::instance(); 155 $coursecontext = context_course::instance($course->id); 156 157 external_settings::get_instance()->set_filter(true); 158 filter_set_global_state('urltolink', TEXTFILTER_OFF); 159 filter_set_local_state('urltolink', $coursecontext->id, TEXTFILTER_ON); 160 set_config('formats', FORMAT_MARKDOWN, 'filter_urltolink'); 161 filter_manager::reset_caches(); 162 163 $data = [ 164 'stringA' => '__Watch out:__ https://moodle.org @@PLUGINFILE@@/test.pdf', 165 'stringAformat' => FORMAT_MARKDOWN, 166 'intB' => 1 167 ]; 168 169 // Export simulated in the system context. 170 $output = $PAGE->get_renderer('core'); 171 $exporter = new core_testable_exporter($data, ['context' => $syscontext] + $this->validrelated); 172 $result = $exporter->export($output); 173 174 $youtube = 'https://moodle.org'; 175 $fileurl = (new moodle_url('/webservice/pluginfile.php/' . $syscontext->id . '/test/area/9/test.pdf'))->out(false); 176 $expected = "<p><strong>Watch out:</strong> $youtube $fileurl</p>\n"; 177 $this->assertEquals($expected, $result->stringA); 178 $this->assertEquals(FORMAT_HTML, $result->stringAformat); 179 180 // Export simulated in the course context where the filter is enabled. 181 $exporter = new core_testable_exporter($data, ['context' => $coursecontext] + $this->validrelated); 182 $result = $exporter->export($output); 183 $youtube = '<a href="https://moodle.org" class="_blanktarget">https://moodle.org</a>'; 184 $fileurl = (new moodle_url('/webservice/pluginfile.php/' . $coursecontext->id . '/test/area/9/test.pdf'))->out(false); 185 $expected = "<p><strong>Watch out:</strong> $youtube <a href=\"$fileurl\" class=\"_blanktarget\">$fileurl</a></p>\n"; 186 $this->assertEquals($expected, $result->stringA); 187 $this->assertEquals(FORMAT_HTML, $result->stringAformat); 188 } 189 190 public function test_properties_description() { 191 $properties = core_testable_exporter::read_properties_definition(); 192 // Properties default description. 193 $this->assertEquals('stringA', $properties['stringA']['description']); 194 $this->assertEquals('stringAformat', $properties['stringAformat']['description']); 195 // Properties custom description. 196 $this->assertEquals('intB description', $properties['intB']['description']); 197 // Other properties custom description. 198 $this->assertEquals('otherstring description', $properties['otherstring']['description']); 199 // Other properties default description. 200 $this->assertEquals('otherstrings', $properties['otherstrings']['description']); 201 // Assert nested elements are formatted correctly. 202 $this->assertEquals('id', $properties['nestedarray']['type']['id']['description']); 203 } 204 } 205 206 /** 207 * Example persistent class. 208 * 209 * @package core 210 * @copyright 2015 Frédéric Massart - FMCorz.net 211 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 212 */ 213 class core_testable_exporter extends \core\external\exporter { 214 215 protected static function define_related() { 216 // We cache the context so it does not need to be retrieved from the course. 217 return array('simplestdClass' => 'stdClass', 'arrayofstdClass' => 'stdClass[]', 'context' => 'context?', 218 'astring' => 'string', 'abool' => 'bool', 'aint' => 'int', 'ints' => 'int[]'); 219 } 220 221 protected function get_other_values(renderer_base $output) { 222 return array( 223 'otherstring' => '>Another <strong>string</strong>', 224 'otherstrings' => array('String >a', 'String <strong>b</strong>') 225 ); 226 } 227 228 public static function define_properties() { 229 return array( 230 'stringA' => array( 231 'type' => PARAM_RAW, 232 ), 233 'stringAformat' => array( 234 'type' => PARAM_INT, 235 ), 236 'intB' => array( 237 'type' => PARAM_INT, 238 'description' => 'intB description', 239 ) 240 ); 241 } 242 243 public static function define_other_properties() { 244 return array( 245 'otherstring' => array( 246 'type' => PARAM_TEXT, 247 'description' => 'otherstring description', 248 ), 249 'otherstrings' => array( 250 'type' => PARAM_TEXT, 251 'multiple' => true 252 ), 253 'nestedarray' => array( 254 'multiple' => true, 255 'optional' => true, 256 'type' => [ 257 'id' => ['type' => PARAM_INT] 258 ] 259 ) 260 ); 261 } 262 263 protected function get_format_parameters_for_stringA() { 264 return [ 265 // For testing use the passed context if any. 266 'context' => isset($this->related['context']) ? $this->related['context'] : context_system::instance(), 267 'component' => 'test', 268 'filearea' => 'area', 269 'itemid' => 9, 270 ]; 271 } 272 273 protected function get_format_parameters_for_otherstring() { 274 return [ 275 'context' => context_system::instance(), 276 'options' => ['escape' => false] 277 ]; 278 } 279 280 protected function get_format_parameters_for_otherstrings() { 281 return [ 282 'context' => context_system::instance(), 283 ]; 284 } 285 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body