2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Exporter class tests.
21 * @copyright 2015 Damyon Wiese
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') ||
die();
32 * @copyright 2015 Damyon Wiese
33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35 class core_exporter_testcase
extends advanced_testcase
{
37 protected $validrelated = null;
38 protected $invalidrelated = null;
39 protected $validdata = null;
40 protected $invaliddata = null;
42 public function setUp(): void
{
44 $this->validrelated
= array(
45 'simplestdClass' => $s,
46 'arrayofstdClass' => array($s, $s),
49 'astring' => 'valid string',
53 $this->invalidrelated
= array(
54 'simplestdClass' => 'a string',
55 'arrayofstdClass' => 5,
59 'abool' => 'not a boolean',
63 $this->validdata
= array('stringA' => 'A string', 'stringAformat' => FORMAT_HTML
, 'intB' => 4);
65 $this->invaliddata
= array('stringA' => 'A string');
68 public function test_get_read_structure() {
69 $structure = core_testable_exporter
::get_read_structure();
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']);
79 public function test_get_create_structure() {
80 $structure = core_testable_exporter
::get_create_structure();
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
);
90 public function test_get_update_structure() {
91 $structure = core_testable_exporter
::get_update_structure();
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
);
101 public function test_invalid_data() {
103 $exporter = new core_testable_exporter($this->invaliddata
, $this->validrelated
);
104 $output = $PAGE->get_renderer('core');
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);
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
);
119 public function test_invalid_related_all_cases() {
122 foreach ($this->invalidrelated
as $key => $value) {
123 $data = $this->validrelated
;
124 $data[$key] = $value;
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));
136 public function test_valid_data_and_related() {
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 >a', 'String b'), $result->otherstrings
);
145 public function test_format_text() {
148 $this->resetAfterTest();
149 $course = $this->getDataGenerator()->create_course();
150 $syscontext = context_system
::instance();
151 $coursecontext = context_course
::instance($course->id
);
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();
160 'stringA' => '__Watch out:__ https://moodle.org @@PLUGINFILE@@/test.pdf',
161 'stringAformat' => FORMAT_MARKDOWN
,
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);
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
);
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
);
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']);
203 * Example persistent class.
206 * @copyright 2015 Frédéric Massart - FMCorz.net
207 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
209 class core_testable_exporter
extends \core\external\exporter
{
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[]');
217 protected function get_other_values(renderer_base
$output) {
219 'otherstring' => '>Another <strong>string</strong>',
220 'otherstrings' => array('String >a', 'String <strong>b</strong>')
224 public static function define_properties() {
229 'stringAformat' => array(
234 'description' => 'intB description',
239 public static function define_other_properties() {
241 'otherstring' => array(
242 'type' => PARAM_TEXT
,
243 'description' => 'otherstring description',
245 'otherstrings' => array(
246 'type' => PARAM_TEXT
,
249 'nestedarray' => array(
253 'id' => ['type' => PARAM_INT
]
259 protected function get_format_parameters_for_stringA() {
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',
269 protected function get_format_parameters_for_otherstring() {
271 'context' => context_system
::instance(),
272 'options' => ['escape' => false]
276 protected function get_format_parameters_for_otherstrings() {
278 'context' => context_system
::instance(),