MDL-51506 tool_lp: Implementing validation in the models
[moodle.git] / admin / tool / lp / tests / persistent_test.php
CommitLineData
dee2fb57
FM
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 * Persistent class tests.
19 *
20 * @package tool_lp
21 * @copyright 2015 Frédéric Massart - FMCorz.net
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25defined('MOODLE_INTERNAL') || die();
26global $CFG;
27
28/**
29 * Persistent testcase.
30 *
31 * @package tool_lp
32 * @copyright 2015 Frédéric Massart - FMCorz.net
33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34 */
35class tool_lp_external_testcase extends advanced_testcase {
36
37 public function setUp() {
38 $this->resetAfterTest();
39 }
40
41 public function test_properties_definition() {
42 $expected = array(
43 'shortname' => array(
44 'type' => PARAM_TEXT,
45 'default' => ''
46 ),
47 'idnumber' => array(
48 'type' => PARAM_TEXT,
49 ),
50 'description' => array(
51 'type' => PARAM_TEXT,
52 'default' => ''
53 ),
54 'descriptionformat' => array(
55 'choices' => array(FORMAT_HTML, FORMAT_MOODLE, FORMAT_PLAIN, FORMAT_MARKDOWN),
56 'type' => PARAM_INT,
57 'default' => FORMAT_HTML
58 ),
59 'parentid' => array(
60 'type' => PARAM_INT,
61 'default' => 0
62 ),
63 'visible' => array(
64 'type' => PARAM_BOOL,
65 'default' => true
66 ),
67 'path' => array(
68 'type' => PARAM_RAW,
69 'default' => ''
70 ),
71 'sortorder' => array(
72 'type' => PARAM_INT,
73 'message' => new lang_string('invalidrequest', 'error')
74 ),
75 'competencyframeworkid' => array(
76 'type' => PARAM_INT,
77 'default' => 0
78 ),
79 'id' => array(
80 'default' => 0,
81 'type' => PARAM_INT,
82 ),
83 'timecreated' => array(
84 'default' => 0,
85 'type' => PARAM_INT,
86 ),
87 'timemodified' => array(
88 'default' => 0,
89 'type' => PARAM_INT
90 ),
91 'usermodified' => array(
92 'default' => 0,
93 'type' => PARAM_INT
94 )
95 );
96 $this->assertEquals($expected, tool_lp_example_persistent::properties_definition());
97 }
98
99 public function test_to_record() {
100 $p = new tool_lp_example_persistent();
101 $expected = (object) array(
102 'shortname' => '',
103 'idnumber' => null,
104 'description' => '',
105 'descriptionformat' => FORMAT_HTML,
106 'parentid' => 0,
107 'visible' => true,
108 'path' => '',
109 'sortorder' => null,
110 'competencyframeworkid' => 0,
111 'id' => 0,
112 'timecreated' => 0,
113 'timemodified' => 0,
114 'usermodified' => 0,
115 );
116 $this->assertEquals($expected, $p->to_record());
117 }
118
119 public function test_from_record() {
120 $p = new tool_lp_example_persistent();
121 $data = (object) array(
122 'shortname' => 'ddd',
123 'idnumber' => 'abc',
124 'description' => 'xyz',
125 'descriptionformat' => FORMAT_PLAIN,
126 'parentid' => 999,
127 'visible' => false,
128 'path' => '/a/b/c',
129 'sortorder' => 12,
130 'competencyframeworkid' => 5,
131 'id' => 1,
132 'timecreated' => 2,
133 'timemodified' => 3,
134 'usermodified' => 4,
135 );
136 $p->from_record($data);
137 $this->assertEquals($data, $p->to_record());
138 }
139
140 public function test_from_record_invalid_param() {
141 $p = new tool_lp_example_persistent();
142 $data = (object) array(
143 'invalidparam' => 'abc'
144 );
145 $this->setExpectedException('coding_exception');
146 $p->from_record($data);
147 }
148
149 public function test_validation_required() {
150 $data = (object) array(
151 'idnumber' => 'abc'
152 );
153 $p = new tool_lp_example_persistent(0, $data);
154 $expected = array(
155 'sortorder' => new lang_string('requiredelement', 'form'),
156 );
157 $this->assertFalse($p->is_valid());
158 $this->assertEquals($expected, $p->get_errors());
159 }
160
161 public function test_validation_custom() {
162 $data = (object) array(
163 'idnumber' => 'abc',
164 'sortorder' => 10,
165 );
166 $p = new tool_lp_example_persistent(0, $data);
167 $expected = array(
168 'sortorder' => new lang_string('invalidkey', 'error'),
169 );
170 $this->assertFalse($p->is_valid());
171 $this->assertEquals($expected, $p->get_errors());
172 }
173
174 public function test_validation_custom_message() {
175 $data = (object) array(
176 'idnumber' => 'abc',
177 'sortorder' => 'abc',
178 );
179 $p = new tool_lp_example_persistent(0, $data);
180 $expected = array(
181 'sortorder' => new lang_string('invalidrequest', 'error'),
182 );
183 $this->assertFalse($p->is_valid());
184 $this->assertEquals($expected, $p->get_errors());
185 }
186
187 public function test_validation_choices() {
188 $data = (object) array(
189 'idnumber' => 'abc',
190 'sortorder' => 0,
191 'descriptionformat' => -100
192 );
193 $p = new tool_lp_example_persistent(0, $data);
194 $expected = array(
195 'descriptionformat' => new lang_string('invaliddata', 'error'),
196 );
197 $this->assertFalse($p->is_valid());
198 $this->assertEquals($expected, $p->get_errors());
199 }
200
201 public function test_validation_type() {
202 $data = (object) array(
203 'idnumber' => 'abc',
204 'sortorder' => 0,
205 'visible' => 'NaN'
206 );
207 $p = new tool_lp_example_persistent(0, $data);
208 $expected = array(
209 'visible' => new lang_string('invaliddata', 'error'),
210 );
211 $this->assertFalse($p->is_valid());
212 $this->assertEquals($expected, $p->get_errors());
213 }
214
215 public function test_validate() {
216 $data = (object) array(
217 'idnumber' => 'abc',
218 'sortorder' => 0
219 );
220 $p = new tool_lp_example_persistent(0, $data);
221 $this->assertTrue($p->validate());
222 $this->assertTrue($p->is_valid());
223 $this->assertEquals(array(), $p->get_errors());
224 $p->set_descriptionformat(-100);
225
226 $expected = array(
227 'descriptionformat' => new lang_string('invaliddata', 'error'),
228 );
229 $this->assertEquals($expected, $p->validate());
230 $this->assertFalse($p->is_valid());
231 $this->assertEquals($expected, $p->get_errors());
232 }
233
234 public function test_create() {
235 global $DB;
236 $p = new tool_lp_example_persistent(0, array('sortorder' => 123, 'idnumber' => 'abc'));
237 $p->create();
238 $record = $DB->get_record(tool_lp_example_persistent::TABLE, array('id' => $p->get_id()), '*', MUST_EXIST);
239 $expected = $p->to_record();
240 $this->assertEquals($expected->sortorder, $record->sortorder);
241 $this->assertEquals($expected->idnumber, $record->idnumber);
242 $this->assertEquals($expected->id, $record->id);
243 $this->assertTrue($p->is_valid()); // Should always be valid after a create.
244 }
245
246 public function test_update() {
247 global $DB;
248 $p = new tool_lp_example_persistent(0, array('sortorder' => 123, 'idnumber' => 'abc'));
249 $p->create();
250 $id = $p->get_id();
251 $p->set_sortorder(456);
252 $p->from_record((object) array('idnumber' => 'def'));
253 $p->update();
254
255 $expected = $p->to_record();
256 $record = $DB->get_record(tool_lp_example_persistent::TABLE, array('id' => $p->get_id()), '*', MUST_EXIST);
257 $this->assertEquals($id, $record->id);
258 $this->assertEquals(456, $record->sortorder);
259 $this->assertEquals('def', $record->idnumber);
260 $this->assertTrue($p->is_valid()); // Should always be valid after an update.
261 }
262
263 public function test_read() {
264 $p = new tool_lp_example_persistent(0, array('sortorder' => 123, 'idnumber' => 'abc'));
265 $p->create();
266
267 $p2 = new tool_lp_example_persistent($p->get_id());
268 $this->assertEquals($p, $p2);
269
270 $p3 = new tool_lp_example_persistent();
271 $p3->set_id($p->get_id());
272 $p3->read();
273 $this->assertEquals($p, $p3);
274 }
275
276 public function test_delete() {
277 global $DB;
278
279 $p = new tool_lp_example_persistent(0, array('sortorder' => 123, 'idnumber' => 'abc'));
280 $p->create();
281 $this->assertTrue($DB->record_exists_select(tool_lp_example_persistent::TABLE, 'id = ?', array($p->get_id())));
282
283 $p->delete();
284 $this->assertFalse($DB->record_exists_select(tool_lp_example_persistent::TABLE, 'id = ?', array($p->get_id())));
285 }
286
287 public function test_has_property() {
288 $this->assertFalse(tool_lp_example_persistent::has_property('unknown'));
289 $this->assertTrue(tool_lp_example_persistent::has_property('idnumber'));
290 }
291
292 public function test_custom_setter_getter() {
293 global $DB;
294
295 $path = array(1, 2, 3);
296 $json = json_encode($path);
297
298 $p = new tool_lp_example_persistent(0, array('sortorder' => 0, 'idnumber' => 'abc'));
299 $p->set_path($path);
300 $this->assertEquals($path, $p->get_path());
301 $this->assertEquals($json, $p->to_record()->path);
302
303 $p->create();
304 $record = $DB->get_record(tool_lp_example_persistent::TABLE, array('id' => $p->get_id()), 'id, path', MUST_EXIST);
305 $this->assertEquals($json, $record->path);
306 }
307
308}
309
310/**
311 * Example persistent class.
312 *
313 * @package tool_lp
314 * @copyright 2015 Frédéric Massart - FMCorz.net
315 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
316 */
317class tool_lp_example_persistent extends \tool_lp\persistent {
318
319 const TABLE = 'tool_lp_competency';
320
321 protected static function define_properties() {
322 return array(
323 'shortname' => array(
324 'type' => PARAM_TEXT,
325 'default' => ''
326 ),
327 'idnumber' => array(
328 'type' => PARAM_TEXT,
329 ),
330 'description' => array(
331 'type' => PARAM_TEXT,
332 'default' => ''
333 ),
334 'descriptionformat' => array(
335 'choices' => array(FORMAT_HTML, FORMAT_MOODLE, FORMAT_PLAIN, FORMAT_MARKDOWN),
336 'type' => PARAM_INT,
337 'default' => FORMAT_HTML
338 ),
339 'parentid' => array(
340 'type' => PARAM_INT,
341 'default' => 0
342 ),
343 'visible' => array(
344 'type' => PARAM_BOOL,
345 'default' => 1
346 ),
347 'path' => array(
348 'type' => PARAM_RAW,
349 'default' => ''
350 ),
351 'sortorder' => array(
352 'type' => PARAM_INT,
353 'message' => new lang_string('invalidrequest', 'error')
354 ),
355 'competencyframeworkid' => array(
356 'type' => PARAM_INT,
357 'default' => 0
358 ),
359 );
360 }
361
362 public function get_path() {
363 $value = $this->get('path');
364 if (!empty($value)) {
365 $value = json_decode($value);
366 }
367 return $value;
368 }
369
370 public function set_path($value) {
371 if (!empty($value)) {
372 $value = json_encode($value);
373 }
374 $this->set('path', $value);
375 }
376
377 public function validate_sortorder($value) {
378 if ($value == 10) {
379 return new lang_string('invalidkey', 'error');
380 }
381 return true;
382 }
383
384}