Commit | Line | Data |
---|---|---|
c80630da DW |
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 | * | |
e0b9ba28 | 20 | * @package core_competency |
c80630da DW |
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 | * | |
e0b9ba28 | 31 | * @package core_competency |
c80630da DW |
32 | * @copyright 2015 Damyon Wiese |
33 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
34 | */ | |
e0b9ba28 | 35 | class core_competency_exporter_testcase extends advanced_testcase { |
c80630da DW |
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('simplestdClass' => $s, 'arrayofstdClass' => array($s, $s)); | |
45 | $this->invalidrelated = array('simplestdClass' => 'a string', 'arrayofstdClass' => 5); | |
46 | ||
47 | $this->validdata = array('stringA' => 'A string', 'stringAformat' => FORMAT_HTML, 'intB' => 4); | |
48 | ||
49 | $this->invaliddata = array('stringA' => 'A string'); | |
50 | } | |
51 | ||
52 | public function test_get_read_structure() { | |
e0b9ba28 | 53 | $structure = core_competency_testable_exporter::get_read_structure(); |
c80630da DW |
54 | |
55 | $this->assertInstanceOf('external_single_structure', $structure); | |
56 | $this->assertInstanceOf('external_value', $structure->keys['stringA']); | |
57 | $this->assertInstanceOf('external_format_value', $structure->keys['stringAformat']); | |
58 | $this->assertInstanceOf('external_value', $structure->keys['intB']); | |
59 | $this->assertInstanceOf('external_value', $structure->keys['otherstring']); | |
60 | $this->assertInstanceOf('external_multiple_structure', $structure->keys['otherstrings']); | |
61 | } | |
62 | ||
63 | public function test_get_create_structure() { | |
e0b9ba28 | 64 | $structure = core_competency_testable_exporter::get_create_structure(); |
c80630da DW |
65 | |
66 | $this->assertInstanceOf('external_single_structure', $structure); | |
67 | $this->assertInstanceOf('external_value', $structure->keys['stringA']); | |
68 | $this->assertInstanceOf('external_format_value', $structure->keys['stringAformat']); | |
69 | $this->assertInstanceOf('external_value', $structure->keys['intB']); | |
70 | $this->assertArrayNotHasKey('otherstring', $structure->keys); | |
71 | $this->assertArrayNotHasKey('otherstrings', $structure->keys); | |
72 | } | |
73 | ||
74 | public function test_get_update_structure() { | |
e0b9ba28 | 75 | $structure = core_competency_testable_exporter::get_update_structure(); |
c80630da DW |
76 | |
77 | $this->assertInstanceOf('external_single_structure', $structure); | |
78 | $this->assertInstanceOf('external_value', $structure->keys['stringA']); | |
79 | $this->assertInstanceOf('external_format_value', $structure->keys['stringAformat']); | |
80 | $this->assertInstanceOf('external_value', $structure->keys['intB']); | |
81 | $this->assertArrayNotHasKey('otherstring', $structure->keys); | |
82 | $this->assertArrayNotHasKey('otherstrings', $structure->keys); | |
83 | } | |
84 | ||
52f3e060 RT |
85 | /** |
86 | * @expectedException coding_exception | |
87 | */ | |
c80630da DW |
88 | public function test_invalid_data() { |
89 | global $PAGE; | |
e0b9ba28 | 90 | $exporter = new core_competency_testable_exporter($this->invaliddata, $this->validrelated); |
c80630da DW |
91 | $output = $PAGE->get_renderer('tool_lp'); |
92 | ||
93 | $result = $exporter->export($output); | |
94 | } | |
95 | ||
52f3e060 RT |
96 | /** |
97 | * @expectedException coding_exception | |
98 | */ | |
c80630da DW |
99 | public function test_invalid_related() { |
100 | global $PAGE; | |
e0b9ba28 | 101 | $exporter = new core_competency_testable_exporter($this->validdata, $this->invalidrelated); |
c80630da DW |
102 | $output = $PAGE->get_renderer('tool_lp'); |
103 | ||
104 | $result = $exporter->export($output); | |
105 | } | |
106 | ||
107 | public function test_valid_data_and_related() { | |
108 | global $PAGE; | |
e0b9ba28 | 109 | $exporter = new core_competency_testable_exporter($this->validdata, $this->validrelated); |
c80630da DW |
110 | |
111 | $output = $PAGE->get_renderer('tool_lp'); | |
112 | ||
113 | $result = $exporter->export($output); | |
0eda810d FM |
114 | |
115 | $this->assertSame('Another string', $result->otherstring); | |
116 | $this->assertSame(array('String a', 'String b'), $result->otherstrings); | |
c80630da DW |
117 | } |
118 | } | |
119 | ||
120 | /** | |
121 | * Example persistent class. | |
122 | * | |
e0b9ba28 | 123 | * @package core_competency |
c80630da DW |
124 | * @copyright 2015 Frédéric Massart - FMCorz.net |
125 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
126 | */ | |
e0b9ba28 | 127 | class core_competency_testable_exporter extends \core_competency\external\exporter { |
c80630da DW |
128 | |
129 | protected static function define_related() { | |
130 | // We cache the context so it does not need to be retrieved from the course. | |
131 | return array('simplestdClass' => 'stdClass', 'arrayofstdClass' => 'stdClass[]'); | |
132 | } | |
133 | ||
134 | protected function get_other_values(renderer_base $output) { | |
135 | return array( | |
0eda810d FM |
136 | 'otherstring' => 'Another <strong>string</strong>', |
137 | 'otherstrings' => array('String a', 'String <strong>b</strong>') | |
c80630da DW |
138 | ); |
139 | } | |
140 | ||
141 | public static function define_properties() { | |
142 | return array( | |
143 | 'stringA' => array( | |
d323d6d3 | 144 | 'type' => PARAM_RAW, |
c80630da DW |
145 | ), |
146 | 'stringAformat' => array( | |
147 | 'type' => PARAM_INT, | |
148 | ), | |
149 | 'intB' => array( | |
150 | 'type' => PARAM_INT, | |
151 | ) | |
152 | ); | |
153 | } | |
154 | ||
155 | public static function define_other_properties() { | |
156 | return array( | |
157 | 'otherstring' => array( | |
158 | 'type' => PARAM_TEXT, | |
159 | ), | |
160 | 'otherstrings' => array( | |
161 | 'type' => PARAM_TEXT, | |
162 | 'multiple' => true | |
163 | ) | |
164 | ); | |
165 | } | |
166 | ||
167 | ||
168 | } |