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/>.
21 * @copyright 2015 Damyon Wiese
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 defined('MOODLE_INTERNAL') ||
die();
34 * @copyright 2015 Damyon Wiese
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 class exporter_test
extends \advanced_testcase
{
39 protected $validrelated = null;
40 protected $invalidrelated = null;
41 protected $validdata = null;
42 protected $invaliddata = null;
44 public function setUp(): void
{
46 $this->validrelated
= array(
47 'simplestdClass' => $s,
48 'arrayofstdClass' => array($s, $s),
51 'astring' => 'valid string',
55 $this->invalidrelated
= array(
56 'simplestdClass' => 'a string',
57 'arrayofstdClass' => 5,
61 'abool' => 'not a boolean',
65 $this->validdata
= array('stringA' => 'A string', 'stringAformat' => FORMAT_HTML
, 'intB' => 4);
67 $this->invaliddata
= array('stringA' => 'A string');
70 public function test_get_read_structure() {
71 $structure = core_testable_exporter
::get_read_structure();
73 $this->assertInstanceOf('external_single_structure', $structure);
74 $this->assertInstanceOf('external_value', $structure->keys
['stringA']);
75 $this->assertInstanceOf('external_format_value', $structure->keys
['stringAformat']);
76 $this->assertInstanceOf('external_value', $structure->keys
['intB']);
77 $this->assertInstanceOf('external_value', $structure->keys
['otherstring']);
78 $this->assertInstanceOf('external_multiple_structure', $structure->keys
['otherstrings']);
81 public function test_get_create_structure() {
82 $structure = core_testable_exporter
::get_create_structure();
84 $this->assertInstanceOf('external_single_structure', $structure);
85 $this->assertInstanceOf('external_value', $structure->keys
['stringA']);
86 $this->assertInstanceOf('external_format_value', $structure->keys
['stringAformat']);
87 $this->assertInstanceOf('external_value', $structure->keys
['intB']);
88 $this->assertArrayNotHasKey('otherstring', $structure->keys
);
89 $this->assertArrayNotHasKey('otherstrings', $structure->keys
);
92 public function test_get_update_structure() {
93 $structure = core_testable_exporter
::get_update_structure();
95 $this->assertInstanceOf('external_single_structure', $structure);
96 $this->assertInstanceOf('external_value', $structure->keys
['stringA']);
97 $this->assertInstanceOf('external_format_value', $structure->keys
['stringAformat']);
98 $this->assertInstanceOf('external_value', $structure->keys
['intB']);
99 $this->assertArrayNotHasKey('otherstring', $structure->keys
);
100 $this->assertArrayNotHasKey('otherstrings', $structure->keys
);
103 public function test_invalid_data() {
105 $exporter = new core_testable_exporter($this->invaliddata
, $this->validrelated
);
106 $output = $PAGE->get_renderer('core');
108 // The exception message is a bit misleading, it actually indicates an expected property wasn't found.
109 $this->expectException(\coding_exception
::class);
110 $this->expectExceptionMessage('Unexpected property stringAformat');
111 $result = $exporter->export($output);
114 public function test_invalid_related() {
115 $this->expectException(\coding_exception
::class);
116 $this->expectExceptionMessage('Exporter class is missing required related data: (core\core_testable_exporter) ' .
117 'simplestdClass => stdClass');
118 $exporter = new core_testable_exporter($this->validdata
, $this->invalidrelated
);
121 public function test_invalid_related_all_cases() {
124 foreach ($this->invalidrelated
as $key => $value) {
125 $data = $this->validrelated
;
126 $data[$key] = $value;
129 $exporter = new core_testable_exporter($this->validdata
, $data);
130 $output = $PAGE->get_renderer('core');
131 $result = $exporter->export($output);
132 } catch (\coding_exception
$e) {
133 $this->assertNotFalse(strpos($e->getMessage(), $key));
138 public function test_valid_data_and_related() {
140 $output = $PAGE->get_renderer('core');
141 $exporter = new core_testable_exporter($this->validdata
, $this->validrelated
);
142 $result = $exporter->export($output);
143 $this->assertSame('>Another string', $result->otherstring
);
144 $this->assertSame(array('String >a', 'String b'), $result->otherstrings
);
147 public function test_format_text() {
150 $this->resetAfterTest();
151 $course = $this->getDataGenerator()->create_course();
152 $syscontext = \context_system
::instance();
153 $coursecontext = \context_course
::instance($course->id
);
155 \external_settings
::get_instance()->set_filter(true);
156 filter_set_global_state('urltolink', TEXTFILTER_OFF
);
157 filter_set_local_state('urltolink', $coursecontext->id
, TEXTFILTER_ON
);
158 set_config('formats', FORMAT_MARKDOWN
, 'filter_urltolink');
159 \filter_manager
::reset_caches();
162 'stringA' => '__Watch out:__ https://moodle.org @@PLUGINFILE@@/test.pdf',
163 'stringAformat' => FORMAT_MARKDOWN
,
167 // Export simulated in the system context.
168 $output = $PAGE->get_renderer('core');
169 $exporter = new core_testable_exporter($data, ['context' => $syscontext] +
$this->validrelated
);
170 $result = $exporter->export($output);
172 $youtube = 'https://moodle.org';
173 $fileurl = (new \
moodle_url('/webservice/pluginfile.php/' . $syscontext->id
. '/test/area/9/test.pdf'))->out(false);
174 $expected = "<p><strong>Watch out:</strong> $youtube $fileurl</p>\n";
175 $this->assertEquals($expected, $result->stringA
);
176 $this->assertEquals(FORMAT_HTML
, $result->stringAformat
);
178 // Export simulated in the course context where the filter is enabled.
179 $exporter = new core_testable_exporter($data, ['context' => $coursecontext] +
$this->validrelated
);
180 $result = $exporter->export($output);
181 $youtube = '<a href="https://moodle.org" class="_blanktarget">https://moodle.org</a>';
182 $fileurl = (new \
moodle_url('/webservice/pluginfile.php/' . $coursecontext->id
. '/test/area/9/test.pdf'))->out(false);
183 $expected = "<p><strong>Watch out:</strong> $youtube <a href=\"$fileurl\" class=\"_blanktarget\">$fileurl</a></p>\n";
184 $this->assertEquals($expected, $result->stringA
);
185 $this->assertEquals(FORMAT_HTML
, $result->stringAformat
);
188 public function test_properties_description() {
189 $properties = core_testable_exporter
::read_properties_definition();
190 // Properties default description.
191 $this->assertEquals('stringA', $properties['stringA']['description']);
192 $this->assertEquals('stringAformat', $properties['stringAformat']['description']);
193 // Properties custom description.
194 $this->assertEquals('intB description', $properties['intB']['description']);
195 // Other properties custom description.
196 $this->assertEquals('otherstring description', $properties['otherstring']['description']);
197 // Other properties default description.
198 $this->assertEquals('otherstrings', $properties['otherstrings']['description']);
199 // Assert nested elements are formatted correctly.
200 $this->assertEquals('id', $properties['nestedarray']['type']['id']['description']);
205 * Example persistent class.
208 * @copyright 2015 Frédéric Massart - FMCorz.net
209 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
211 class core_testable_exporter
extends \core\external\exporter
{
213 protected static function define_related() {
214 // We cache the context so it does not need to be retrieved from the course.
215 return array('simplestdClass' => 'stdClass', 'arrayofstdClass' => 'stdClass[]', 'context' => 'context?',
216 'astring' => 'string', 'abool' => 'bool', 'aint' => 'int', 'ints' => 'int[]');
219 protected function get_other_values(\renderer_base
$output) {
221 'otherstring' => '>Another <strong>string</strong>',
222 'otherstrings' => array('String >a', 'String <strong>b</strong>')
226 public static function define_properties() {
231 'stringAformat' => array(
236 'description' => 'intB description',
241 public static function define_other_properties() {
243 'otherstring' => array(
244 'type' => PARAM_TEXT
,
245 'description' => 'otherstring description',
247 'otherstrings' => array(
248 'type' => PARAM_TEXT
,
251 'nestedarray' => array(
255 'id' => ['type' => PARAM_INT
]
261 protected function get_format_parameters_for_stringA() {
263 // For testing use the passed context if any.
264 'context' => isset($this->related
['context']) ?
$this->related
['context'] : \context_system
::instance(),
265 'component' => 'test',
266 'filearea' => 'area',
271 protected function get_format_parameters_for_otherstring() {
273 'context' => \context_system
::instance(),
274 'options' => ['escape' => false]
278 protected function get_format_parameters_for_otherstrings() {
280 'context' => \context_system
::instance(),