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