MDL-52119 cbe: Use generated structures for persistent classes
[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 */
096166a0 35class tool_lp_persistent_testcase extends advanced_testcase {
dee2fb57
FM
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,
096166a0
FM
77 'default' => 0,
78 'null' => NULL_ALLOWED
dee2fb57
FM
79 ),
80 'id' => array(
81 'default' => 0,
82 'type' => PARAM_INT,
83 ),
84 'timecreated' => array(
85 'default' => 0,
86 'type' => PARAM_INT,
87 ),
88 'timemodified' => array(
89 'default' => 0,
90 'type' => PARAM_INT
91 ),
92 'usermodified' => array(
93 'default' => 0,
94 'type' => PARAM_INT
1896274f
FM
95 ),
96 'ruletype' => array(
97 'type' => PARAM_RAW,
98 'default' => null,
99 'null' => NULL_ALLOWED,
100 ),
101 'ruleconfig' => array(
102 'type' => PARAM_RAW,
103 'default' => null,
104 'null' => NULL_ALLOWED,
105 ),
106 'ruleoutcome' => array(
107 'type' => PARAM_RAW,
108 'default' => 0
dee2fb57
FM
109 )
110 );
096166a0 111 $this->assertEquals($expected, tool_lp_testable_persistent::properties_definition());
dee2fb57
FM
112 }
113
114 public function test_to_record() {
096166a0 115 $p = new tool_lp_testable_persistent();
dee2fb57
FM
116 $expected = (object) array(
117 'shortname' => '',
118 'idnumber' => null,
119 'description' => '',
120 'descriptionformat' => FORMAT_HTML,
121 'parentid' => 0,
122 'visible' => true,
123 'path' => '',
124 'sortorder' => null,
096166a0 125 'competencyframeworkid' => null,
dee2fb57
FM
126 'id' => 0,
127 'timecreated' => 0,
128 'timemodified' => 0,
129 'usermodified' => 0,
1896274f
FM
130 'ruletype' => null,
131 'ruleconfig' => null,
132 'ruleoutcome' => 0,
dee2fb57
FM
133 );
134 $this->assertEquals($expected, $p->to_record());
135 }
136
137 public function test_from_record() {
096166a0 138 $p = new tool_lp_testable_persistent();
dee2fb57
FM
139 $data = (object) array(
140 'shortname' => 'ddd',
141 'idnumber' => 'abc',
142 'description' => 'xyz',
143 'descriptionformat' => FORMAT_PLAIN,
144 'parentid' => 999,
145 'visible' => false,
146 'path' => '/a/b/c',
147 'sortorder' => 12,
148 'competencyframeworkid' => 5,
149 'id' => 1,
150 'timecreated' => 2,
151 'timemodified' => 3,
152 'usermodified' => 4,
1896274f
FM
153 'ruletype' => null,
154 'ruleconfig' => null,
155 'ruleoutcome' => 0,
dee2fb57
FM
156 );
157 $p->from_record($data);
158 $this->assertEquals($data, $p->to_record());
159 }
160
161 public function test_from_record_invalid_param() {
096166a0 162 $p = new tool_lp_testable_persistent();
dee2fb57
FM
163 $data = (object) array(
164 'invalidparam' => 'abc'
165 );
166 $this->setExpectedException('coding_exception');
167 $p->from_record($data);
168 }
169
096166a0
FM
170 public function test_validate() {
171 $data = (object) array(
172 'idnumber' => 'abc',
173 'sortorder' => 0
174 );
175 $p = new tool_lp_testable_persistent(0, $data);
176 $this->assertFalse(isset($p->beforevalidate));
177 $this->assertTrue($p->validate());
178 $this->assertTrue(isset($p->beforevalidate));
179 $this->assertTrue($p->is_valid());
180 $this->assertEquals(array(), $p->get_errors());
181 $p->set_descriptionformat(-100);
182
183 $expected = array(
184 'descriptionformat' => new lang_string('invaliddata', 'error'),
185 );
186 $this->assertEquals($expected, $p->validate());
187 $this->assertFalse($p->is_valid());
188 $this->assertEquals($expected, $p->get_errors());
189 }
190
dee2fb57
FM
191 public function test_validation_required() {
192 $data = (object) array(
193 'idnumber' => 'abc'
194 );
096166a0 195 $p = new tool_lp_testable_persistent(0, $data);
dee2fb57
FM
196 $expected = array(
197 'sortorder' => new lang_string('requiredelement', 'form'),
198 );
199 $this->assertFalse($p->is_valid());
200 $this->assertEquals($expected, $p->get_errors());
201 }
202
203 public function test_validation_custom() {
204 $data = (object) array(
205 'idnumber' => 'abc',
206 'sortorder' => 10,
207 );
096166a0 208 $p = new tool_lp_testable_persistent(0, $data);
dee2fb57
FM
209 $expected = array(
210 'sortorder' => new lang_string('invalidkey', 'error'),
211 );
212 $this->assertFalse($p->is_valid());
213 $this->assertEquals($expected, $p->get_errors());
214 }
215
216 public function test_validation_custom_message() {
217 $data = (object) array(
218 'idnumber' => 'abc',
219 'sortorder' => 'abc',
220 );
096166a0 221 $p = new tool_lp_testable_persistent(0, $data);
dee2fb57
FM
222 $expected = array(
223 'sortorder' => new lang_string('invalidrequest', 'error'),
224 );
225 $this->assertFalse($p->is_valid());
226 $this->assertEquals($expected, $p->get_errors());
227 }
228
229 public function test_validation_choices() {
230 $data = (object) array(
231 'idnumber' => 'abc',
232 'sortorder' => 0,
233 'descriptionformat' => -100
234 );
096166a0 235 $p = new tool_lp_testable_persistent(0, $data);
dee2fb57
FM
236 $expected = array(
237 'descriptionformat' => new lang_string('invaliddata', 'error'),
238 );
239 $this->assertFalse($p->is_valid());
240 $this->assertEquals($expected, $p->get_errors());
241 }
242
243 public function test_validation_type() {
244 $data = (object) array(
245 'idnumber' => 'abc',
246 'sortorder' => 0,
247 'visible' => 'NaN'
248 );
096166a0 249 $p = new tool_lp_testable_persistent(0, $data);
dee2fb57
FM
250 $expected = array(
251 'visible' => new lang_string('invaliddata', 'error'),
252 );
253 $this->assertFalse($p->is_valid());
254 $this->assertEquals($expected, $p->get_errors());
255 }
256
096166a0 257 public function test_validation_null() {
dee2fb57 258 $data = (object) array(
096166a0
FM
259 'idnumber' => null,
260 'sortorder' => 0,
261 'competencyframeworkid' => 'bad!'
dee2fb57 262 );
096166a0 263 $p = new tool_lp_testable_persistent(0, $data);
dee2fb57 264 $this->assertFalse($p->is_valid());
096166a0
FM
265 $this->assertArrayHasKey('idnumber', $p->get_errors());
266 $this->assertArrayHasKey('competencyframeworkid', $p->get_errors());
267 $p->set_idnumber('abc');
268 $this->assertFalse($p->is_valid());
269 $this->assertArrayNotHasKey('idnumber', $p->get_errors());
270 $this->assertArrayHasKey('competencyframeworkid', $p->get_errors());
271 $p->set_competencyframeworkid(null);
272 $this->assertTrue($p->is_valid());
273 $this->assertArrayNotHasKey('competencyframeworkid', $p->get_errors());
dee2fb57
FM
274 }
275
276 public function test_create() {
277 global $DB;
096166a0
FM
278 $p = new tool_lp_testable_persistent(0, (object) array('sortorder' => 123, 'idnumber' => 'abc'));
279 $this->assertFalse(isset($p->beforecreate));
280 $this->assertFalse(isset($p->aftercreate));
dee2fb57 281 $p->create();
096166a0 282 $record = $DB->get_record(tool_lp_testable_persistent::TABLE, array('id' => $p->get_id()), '*', MUST_EXIST);
dee2fb57 283 $expected = $p->to_record();
096166a0
FM
284 $this->assertTrue(isset($p->beforecreate));
285 $this->assertTrue(isset($p->aftercreate));
dee2fb57
FM
286 $this->assertEquals($expected->sortorder, $record->sortorder);
287 $this->assertEquals($expected->idnumber, $record->idnumber);
288 $this->assertEquals($expected->id, $record->id);
289 $this->assertTrue($p->is_valid()); // Should always be valid after a create.
290 }
291
292 public function test_update() {
293 global $DB;
096166a0 294 $p = new tool_lp_testable_persistent(0, (object) array('sortorder' => 123, 'idnumber' => 'abc'));
dee2fb57
FM
295 $p->create();
296 $id = $p->get_id();
297 $p->set_sortorder(456);
298 $p->from_record((object) array('idnumber' => 'def'));
096166a0
FM
299 $this->assertFalse(isset($p->beforeupdate));
300 $this->assertFalse(isset($p->afterupdate));
dee2fb57
FM
301 $p->update();
302
303 $expected = $p->to_record();
096166a0
FM
304 $record = $DB->get_record(tool_lp_testable_persistent::TABLE, array('id' => $p->get_id()), '*', MUST_EXIST);
305 $this->assertTrue(isset($p->beforeupdate));
306 $this->assertTrue(isset($p->afterupdate));
dee2fb57
FM
307 $this->assertEquals($id, $record->id);
308 $this->assertEquals(456, $record->sortorder);
309 $this->assertEquals('def', $record->idnumber);
310 $this->assertTrue($p->is_valid()); // Should always be valid after an update.
311 }
312
313 public function test_read() {
096166a0 314 $p = new tool_lp_testable_persistent(0, (object) array('sortorder' => 123, 'idnumber' => 'abc'));
dee2fb57 315 $p->create();
096166a0
FM
316 unset($p->beforevalidate);
317 unset($p->beforecreate);
318 unset($p->aftercreate);
dee2fb57 319
096166a0 320 $p2 = new tool_lp_testable_persistent($p->get_id());
dee2fb57
FM
321 $this->assertEquals($p, $p2);
322
096166a0 323 $p3 = new tool_lp_testable_persistent();
dee2fb57
FM
324 $p3->set_id($p->get_id());
325 $p3->read();
326 $this->assertEquals($p, $p3);
327 }
328
329 public function test_delete() {
330 global $DB;
331
096166a0 332 $p = new tool_lp_testable_persistent(0, (object) array('sortorder' => 123, 'idnumber' => 'abc'));
dee2fb57 333 $p->create();
096166a0
FM
334 $this->assertNotEquals(0, $p->get_id());
335 $this->assertTrue($DB->record_exists_select(tool_lp_testable_persistent::TABLE, 'id = ?', array($p->get_id())));
336 $this->assertFalse(isset($p->beforedelete));
337 $this->assertFalse(isset($p->afterdelete));
dee2fb57
FM
338
339 $p->delete();
096166a0
FM
340 $this->assertFalse($DB->record_exists_select(tool_lp_testable_persistent::TABLE, 'id = ?', array($p->get_id())));
341 $this->assertEquals(0, $p->get_id());
342 $this->assertEquals(true, $p->beforedelete);
343 $this->assertEquals(true, $p->afterdelete);
dee2fb57
FM
344 }
345
346 public function test_has_property() {
096166a0
FM
347 $this->assertFalse(tool_lp_testable_persistent::has_property('unknown'));
348 $this->assertTrue(tool_lp_testable_persistent::has_property('idnumber'));
dee2fb57
FM
349 }
350
351 public function test_custom_setter_getter() {
352 global $DB;
353
354 $path = array(1, 2, 3);
355 $json = json_encode($path);
356
096166a0 357 $p = new tool_lp_testable_persistent(0, (object) array('sortorder' => 0, 'idnumber' => 'abc'));
dee2fb57
FM
358 $p->set_path($path);
359 $this->assertEquals($path, $p->get_path());
360 $this->assertEquals($json, $p->to_record()->path);
361
362 $p->create();
096166a0 363 $record = $DB->get_record(tool_lp_testable_persistent::TABLE, array('id' => $p->get_id()), 'id, path', MUST_EXIST);
dee2fb57
FM
364 $this->assertEquals($json, $record->path);
365 }
366
096166a0
FM
367 public function test_record_exists() {
368 global $DB;
369 $this->assertFalse($DB->record_exists(tool_lp_testable_persistent::TABLE, array('idnumber' => 'abc')));
370 $p = new tool_lp_testable_persistent(0, (object) array('sortorder' => 123, 'idnumber' => 'abc'));
371 $p->create();
372 $id = $p->get_id();
373 $this->assertTrue(tool_lp_testable_persistent::record_exists($id));
374 $this->assertTrue($DB->record_exists(tool_lp_testable_persistent::TABLE, array('idnumber' => 'abc')));
375 $p->delete();
376 $this->assertFalse(tool_lp_testable_persistent::record_exists($id));
377 }
378
dee2fb57
FM
379}
380
381/**
382 * Example persistent class.
383 *
384 * @package tool_lp
385 * @copyright 2015 Frédéric Massart - FMCorz.net
386 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
387 */
096166a0 388class tool_lp_testable_persistent extends \tool_lp\persistent {
dee2fb57
FM
389
390 const TABLE = 'tool_lp_competency';
391
392 protected static function define_properties() {
393 return array(
394 'shortname' => array(
395 'type' => PARAM_TEXT,
396 'default' => ''
397 ),
398 'idnumber' => array(
399 'type' => PARAM_TEXT,
400 ),
401 'description' => array(
402 'type' => PARAM_TEXT,
403 'default' => ''
404 ),
405 'descriptionformat' => array(
406 'choices' => array(FORMAT_HTML, FORMAT_MOODLE, FORMAT_PLAIN, FORMAT_MARKDOWN),
407 'type' => PARAM_INT,
408 'default' => FORMAT_HTML
409 ),
410 'parentid' => array(
411 'type' => PARAM_INT,
412 'default' => 0
413 ),
414 'visible' => array(
415 'type' => PARAM_BOOL,
416 'default' => 1
417 ),
418 'path' => array(
419 'type' => PARAM_RAW,
420 'default' => ''
421 ),
422 'sortorder' => array(
423 'type' => PARAM_INT,
424 'message' => new lang_string('invalidrequest', 'error')
425 ),
426 'competencyframeworkid' => array(
427 'type' => PARAM_INT,
096166a0
FM
428 'default' => 0,
429 'null' => NULL_ALLOWED
dee2fb57 430 ),
1896274f
FM
431 'ruletype' => array(
432 'type' => PARAM_RAW,
433 'default' => null,
434 'null' => NULL_ALLOWED,
435 ),
436 'ruleconfig' => array(
437 'type' => PARAM_RAW,
438 'default' => null,
439 'null' => NULL_ALLOWED,
440 ),
441 'ruleoutcome' => array(
442 'type' => PARAM_RAW,
443 'default' => 0
444 )
dee2fb57
FM
445 );
446 }
447
096166a0
FM
448 protected function before_validate() {
449 $this->beforevalidate = true;
450 }
451
452 protected function before_create() {
453 $this->beforecreate = true;
454 }
455
456 protected function before_update() {
457 $this->beforeupdate = true;
458 }
459
460 protected function before_delete() {
461 $this->beforedelete = true;
462 }
463
464 protected function after_create() {
465 $this->aftercreate = true;
466 }
467
468 protected function after_update($result) {
469 $this->afterupdate = true;
470 }
471
472 protected function after_delete($result) {
473 $this->afterdelete = true;
474 }
475
dee2fb57
FM
476 public function get_path() {
477 $value = $this->get('path');
478 if (!empty($value)) {
479 $value = json_decode($value);
480 }
481 return $value;
482 }
483
484 public function set_path($value) {
485 if (!empty($value)) {
486 $value = json_encode($value);
487 }
488 $this->set('path', $value);
489 }
490
03c66658 491 protected function validate_sortorder($value) {
dee2fb57
FM
492 if ($value == 10) {
493 return new lang_string('invalidkey', 'error');
494 }
495 return true;
496 }
497
498}