Merge branch 'MDL-57846-master' of git://github.com/jleyva/moodle
[moodle.git] / lib / tests / exporter_test.php
blobf456628946a08ef2202508d109ab1c881e77e11f
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.
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/>.
17 /**
18 * Exporter class tests.
20 * @package core
21 * @copyright 2015 Damyon Wiese
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') || die();
26 global $CFG;
28 /**
29 * Exporter testcase.
31 * @package core
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() {
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
52 $this->invalidrelated = array(
53 'simplestdClass' => 'a string',
54 'arrayofstdClass' => 5,
55 'context' => null,
56 'aint' => false,
57 'astring' => 4,
58 'abool' => 'not a boolean'
61 $this->validdata = array('stringA' => 'A string', 'stringAformat' => FORMAT_HTML, 'intB' => 4);
63 $this->invaliddata = array('stringA' => 'A string');
66 public function test_get_read_structure() {
67 $structure = core_testable_exporter::get_read_structure();
69 $this->assertInstanceOf('external_single_structure', $structure);
70 $this->assertInstanceOf('external_value', $structure->keys['stringA']);
71 $this->assertInstanceOf('external_format_value', $structure->keys['stringAformat']);
72 $this->assertInstanceOf('external_value', $structure->keys['intB']);
73 $this->assertInstanceOf('external_value', $structure->keys['otherstring']);
74 $this->assertInstanceOf('external_multiple_structure', $structure->keys['otherstrings']);
77 public function test_get_create_structure() {
78 $structure = core_testable_exporter::get_create_structure();
80 $this->assertInstanceOf('external_single_structure', $structure);
81 $this->assertInstanceOf('external_value', $structure->keys['stringA']);
82 $this->assertInstanceOf('external_format_value', $structure->keys['stringAformat']);
83 $this->assertInstanceOf('external_value', $structure->keys['intB']);
84 $this->assertArrayNotHasKey('otherstring', $structure->keys);
85 $this->assertArrayNotHasKey('otherstrings', $structure->keys);
88 public function test_get_update_structure() {
89 $structure = core_testable_exporter::get_update_structure();
91 $this->assertInstanceOf('external_single_structure', $structure);
92 $this->assertInstanceOf('external_value', $structure->keys['stringA']);
93 $this->assertInstanceOf('external_format_value', $structure->keys['stringAformat']);
94 $this->assertInstanceOf('external_value', $structure->keys['intB']);
95 $this->assertArrayNotHasKey('otherstring', $structure->keys);
96 $this->assertArrayNotHasKey('otherstrings', $structure->keys);
99 /**
100 * @expectedException coding_exception
102 public function test_invalid_data() {
103 global $PAGE;
104 $exporter = new core_testable_exporter($this->invaliddata, $this->validrelated);
105 $output = $PAGE->get_renderer('core');
107 $result = $exporter->export($output);
111 * @expectedException coding_exception
113 public function test_invalid_related() {
114 global $PAGE;
115 $exporter = new core_testable_exporter($this->validdata, $this->invalidrelated);
116 $output = $PAGE->get_renderer('core');
118 $result = $exporter->export($output);
121 public function test_valid_data_and_related() {
122 global $PAGE;
123 $output = $PAGE->get_renderer('core');
124 $exporter = new core_testable_exporter($this->validdata, $this->validrelated);
125 $result = $exporter->export($output);
126 $this->assertSame('>Another string', $result->otherstring);
127 $this->assertSame(array('String &gt;a', 'String b'), $result->otherstrings);
130 public function test_format_text() {
131 global $PAGE;
133 $this->resetAfterTest();
134 $course = $this->getDataGenerator()->create_course();
135 $syscontext = context_system::instance();
136 $coursecontext = context_course::instance($course->id);
138 external_settings::get_instance()->set_filter(true);
139 filter_set_global_state('urltolink', TEXTFILTER_OFF);
140 filter_set_local_state('urltolink', $coursecontext->id, TEXTFILTER_ON);
141 set_config('formats', FORMAT_MARKDOWN, 'filter_urltolink');
142 filter_manager::reset_caches();
144 $data = [
145 'stringA' => '__Watch out:__ https://moodle.org @@PLUGINFILE@@/test.pdf',
146 'stringAformat' => FORMAT_MARKDOWN,
147 'intB' => 1
150 // Export simulated in the system context.
151 $output = $PAGE->get_renderer('core');
152 $exporter = new core_testable_exporter($data, ['context' => $syscontext] + $this->validrelated);
153 $result = $exporter->export($output);
155 $youtube = 'https://moodle.org';
156 $fileurl = (new moodle_url('/webservice/pluginfile.php/' . $syscontext->id . '/test/area/9/test.pdf'))->out(false);
157 $expected = "<p><strong>Watch out:</strong> $youtube $fileurl</p>\n";
158 $this->assertEquals($expected, $result->stringA);
159 $this->assertEquals(FORMAT_HTML, $result->stringAformat);
161 // Export simulated in the course context where the filter is enabled.
162 $exporter = new core_testable_exporter($data, ['context' => $coursecontext] + $this->validrelated);
163 $result = $exporter->export($output);
164 $youtube = '<a href="https://moodle.org" class="_blanktarget">https://moodle.org</a>';
165 $fileurl = (new moodle_url('/webservice/pluginfile.php/' . $coursecontext->id . '/test/area/9/test.pdf'))->out(false);
166 $expected = "<p><strong>Watch out:</strong> $youtube <a href=\"$fileurl\" class=\"_blanktarget\">$fileurl</a></p>\n";
167 $this->assertEquals($expected, $result->stringA);
168 $this->assertEquals(FORMAT_HTML, $result->stringAformat);
171 public function test_properties_description() {
172 $properties = core_testable_exporter::read_properties_definition();
173 // Properties default description.
174 $this->assertEquals('stringA', $properties['stringA']['description']);
175 $this->assertEquals('stringAformat', $properties['stringAformat']['description']);
176 // Properties custom description.
177 $this->assertEquals('intB description', $properties['intB']['description']);
178 // Other properties custom description.
179 $this->assertEquals('otherstring description', $properties['otherstring']['description']);
180 // Other properties default description.
181 $this->assertEquals('otherstrings', $properties['otherstrings']['description']);
186 * Example persistent class.
188 * @package core
189 * @copyright 2015 Frédéric Massart - FMCorz.net
190 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
192 class core_testable_exporter extends \core\external\exporter {
194 protected static function define_related() {
195 // We cache the context so it does not need to be retrieved from the course.
196 return array('simplestdClass' => 'stdClass', 'arrayofstdClass' => 'stdClass[]', 'context' => 'context?',
197 'astring' => 'string', 'abool' => 'bool', 'aint' => 'int');
200 protected function get_other_values(renderer_base $output) {
201 return array(
202 'otherstring' => '>Another <strong>string</strong>',
203 'otherstrings' => array('String >a', 'String <strong>b</strong>')
207 public static function define_properties() {
208 return array(
209 'stringA' => array(
210 'type' => PARAM_RAW,
212 'stringAformat' => array(
213 'type' => PARAM_INT,
215 'intB' => array(
216 'type' => PARAM_INT,
217 'description' => 'intB description',
222 public static function define_other_properties() {
223 return array(
224 'otherstring' => array(
225 'type' => PARAM_TEXT,
226 'description' => 'otherstring description',
228 'otherstrings' => array(
229 'type' => PARAM_TEXT,
230 'multiple' => true
235 protected function get_format_parameters_for_stringA() {
236 return [
237 // For testing use the passed context if any.
238 'context' => isset($this->related['context']) ? $this->related['context'] : context_system::instance(),
239 'component' => 'test',
240 'filearea' => 'area',
241 'itemid' => 9,
245 protected function get_format_parameters_for_otherstring() {
246 return [
247 'context' => context_system::instance(),
248 'options' => ['escape' => false]
252 protected function get_format_parameters_for_otherstrings() {
253 return [
254 'context' => context_system::instance(),