MDL-51963 tool_lp: Really add the scaleid field on competency
[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
FM
67 ),
68 'visible' => array(
69 'type' => PARAM_BOOL,
578e61c1
FM
70 'default' => true,
71 'null' => NULL_NOT_ALLOWED
dee2fb57
FM
72 ),
73 'path' => array(
74 'type' => PARAM_RAW,
578e61c1
FM
75 'default' => '',
76 'null' => NULL_NOT_ALLOWED
dee2fb57
FM
77 ),
78 'sortorder' => array(
79 'type' => PARAM_INT,
578e61c1
FM
80 'message' => new lang_string('invalidrequest', 'error'),
81 'null' => NULL_NOT_ALLOWED
dee2fb57
FM
82 ),
83 'competencyframeworkid' => array(
84 'type' => PARAM_INT,
096166a0
FM
85 'default' => 0,
86 'null' => NULL_ALLOWED
dee2fb57
FM
87 ),
88 'id' => array(
89 'default' => 0,
90 'type' => PARAM_INT,
578e61c1 91 'null' => NULL_NOT_ALLOWED
dee2fb57
FM
92 ),
93 'timecreated' => array(
94 'default' => 0,
95 'type' => PARAM_INT,
578e61c1 96 'null' => NULL_NOT_ALLOWED
dee2fb57
FM
97 ),
98 'timemodified' => array(
99 'default' => 0,
578e61c1
FM
100 'type' => PARAM_INT,
101 'null' => NULL_NOT_ALLOWED
dee2fb57
FM
102 ),
103 'usermodified' => array(
104 'default' => 0,
578e61c1
FM
105 'type' => PARAM_INT,
106 'null' => NULL_NOT_ALLOWED
1896274f
FM
107 ),
108 'ruletype' => array(
109 'type' => PARAM_RAW,
110 'default' => null,
111 'null' => NULL_ALLOWED,
112 ),
113 'ruleconfig' => array(
114 'type' => PARAM_RAW,
115 'default' => null,
116 'null' => NULL_ALLOWED,
117 ),
118 'ruleoutcome' => array(
119 'type' => PARAM_RAW,
578e61c1
FM
120 'default' => 0,
121 'null' => NULL_NOT_ALLOWED
bf62b21d
FM
122 ),
123 'scaleid' => array(
124 'default' => null,
125 'type' => PARAM_INT,
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,
140 'visible' => true,
141 'path' => '',
142 'sortorder' => null,
096166a0 143 'competencyframeworkid' => null,
dee2fb57
FM
144 'id' => 0,
145 'timecreated' => 0,
146 'timemodified' => 0,
147 'usermodified' => 0,
1896274f
FM
148 'ruletype' => null,
149 'ruleconfig' => null,
150 'ruleoutcome' => 0,
bf62b21d 151 'scaleid' => 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,
164 'visible' => false,
165 'path' => '/a/b/c',
166 'sortorder' => 12,
167 'competencyframeworkid' => 5,
168 'id' => 1,
169 'timecreated' => 2,
170 'timemodified' => 3,
171 'usermodified' => 4,
1896274f
FM
172 'ruletype' => null,
173 'ruleconfig' => null,
174 'ruleoutcome' => 0,
bf62b21d 175 'scaleid' => 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',
266 'sortorder' => 0,
267 'visible' => 'NaN'
268 );
096166a0 269 $p = new tool_lp_testable_persistent(0, $data);
dee2fb57
FM
270 $expected = array(
271 'visible' => new lang_string('invaliddata', 'error'),
272 );
273 $this->assertFalse($p->is_valid());
274 $this->assertEquals($expected, $p->get_errors());
275 }
276
096166a0 277 public function test_validation_null() {
dee2fb57 278 $data = (object) array(
096166a0
FM
279 'idnumber' => null,
280 'sortorder' => 0,
281 'competencyframeworkid' => 'bad!'
dee2fb57 282 );
096166a0 283 $p = new tool_lp_testable_persistent(0, $data);
dee2fb57 284 $this->assertFalse($p->is_valid());
096166a0
FM
285 $this->assertArrayHasKey('idnumber', $p->get_errors());
286 $this->assertArrayHasKey('competencyframeworkid', $p->get_errors());
287 $p->set_idnumber('abc');
288 $this->assertFalse($p->is_valid());
289 $this->assertArrayNotHasKey('idnumber', $p->get_errors());
290 $this->assertArrayHasKey('competencyframeworkid', $p->get_errors());
291 $p->set_competencyframeworkid(null);
292 $this->assertTrue($p->is_valid());
293 $this->assertArrayNotHasKey('competencyframeworkid', $p->get_errors());
dee2fb57
FM
294 }
295
296 public function test_create() {
297 global $DB;
096166a0
FM
298 $p = new tool_lp_testable_persistent(0, (object) array('sortorder' => 123, 'idnumber' => 'abc'));
299 $this->assertFalse(isset($p->beforecreate));
300 $this->assertFalse(isset($p->aftercreate));
dee2fb57 301 $p->create();
096166a0 302 $record = $DB->get_record(tool_lp_testable_persistent::TABLE, array('id' => $p->get_id()), '*', MUST_EXIST);
dee2fb57 303 $expected = $p->to_record();
096166a0
FM
304 $this->assertTrue(isset($p->beforecreate));
305 $this->assertTrue(isset($p->aftercreate));
dee2fb57
FM
306 $this->assertEquals($expected->sortorder, $record->sortorder);
307 $this->assertEquals($expected->idnumber, $record->idnumber);
308 $this->assertEquals($expected->id, $record->id);
309 $this->assertTrue($p->is_valid()); // Should always be valid after a create.
310 }
311
312 public function test_update() {
313 global $DB;
096166a0 314 $p = new tool_lp_testable_persistent(0, (object) array('sortorder' => 123, 'idnumber' => 'abc'));
dee2fb57
FM
315 $p->create();
316 $id = $p->get_id();
317 $p->set_sortorder(456);
318 $p->from_record((object) array('idnumber' => 'def'));
096166a0
FM
319 $this->assertFalse(isset($p->beforeupdate));
320 $this->assertFalse(isset($p->afterupdate));
dee2fb57
FM
321 $p->update();
322
323 $expected = $p->to_record();
096166a0
FM
324 $record = $DB->get_record(tool_lp_testable_persistent::TABLE, array('id' => $p->get_id()), '*', MUST_EXIST);
325 $this->assertTrue(isset($p->beforeupdate));
326 $this->assertTrue(isset($p->afterupdate));
dee2fb57
FM
327 $this->assertEquals($id, $record->id);
328 $this->assertEquals(456, $record->sortorder);
329 $this->assertEquals('def', $record->idnumber);
330 $this->assertTrue($p->is_valid()); // Should always be valid after an update.
331 }
332
333 public function test_read() {
096166a0 334 $p = new tool_lp_testable_persistent(0, (object) array('sortorder' => 123, 'idnumber' => 'abc'));
dee2fb57 335 $p->create();
096166a0
FM
336 unset($p->beforevalidate);
337 unset($p->beforecreate);
338 unset($p->aftercreate);
dee2fb57 339
096166a0 340 $p2 = new tool_lp_testable_persistent($p->get_id());
dee2fb57
FM
341 $this->assertEquals($p, $p2);
342
096166a0 343 $p3 = new tool_lp_testable_persistent();
dee2fb57
FM
344 $p3->set_id($p->get_id());
345 $p3->read();
346 $this->assertEquals($p, $p3);
347 }
348
349 public function test_delete() {
350 global $DB;
351
096166a0 352 $p = new tool_lp_testable_persistent(0, (object) array('sortorder' => 123, 'idnumber' => 'abc'));
dee2fb57 353 $p->create();
096166a0
FM
354 $this->assertNotEquals(0, $p->get_id());
355 $this->assertTrue($DB->record_exists_select(tool_lp_testable_persistent::TABLE, 'id = ?', array($p->get_id())));
356 $this->assertFalse(isset($p->beforedelete));
357 $this->assertFalse(isset($p->afterdelete));
dee2fb57
FM
358
359 $p->delete();
096166a0
FM
360 $this->assertFalse($DB->record_exists_select(tool_lp_testable_persistent::TABLE, 'id = ?', array($p->get_id())));
361 $this->assertEquals(0, $p->get_id());
362 $this->assertEquals(true, $p->beforedelete);
363 $this->assertEquals(true, $p->afterdelete);
dee2fb57
FM
364 }
365
366 public function test_has_property() {
096166a0
FM
367 $this->assertFalse(tool_lp_testable_persistent::has_property('unknown'));
368 $this->assertTrue(tool_lp_testable_persistent::has_property('idnumber'));
dee2fb57
FM
369 }
370
371 public function test_custom_setter_getter() {
372 global $DB;
373
374 $path = array(1, 2, 3);
375 $json = json_encode($path);
376
096166a0 377 $p = new tool_lp_testable_persistent(0, (object) array('sortorder' => 0, 'idnumber' => 'abc'));
dee2fb57
FM
378 $p->set_path($path);
379 $this->assertEquals($path, $p->get_path());
380 $this->assertEquals($json, $p->to_record()->path);
381
382 $p->create();
096166a0 383 $record = $DB->get_record(tool_lp_testable_persistent::TABLE, array('id' => $p->get_id()), 'id, path', MUST_EXIST);
dee2fb57
FM
384 $this->assertEquals($json, $record->path);
385 }
386
096166a0
FM
387 public function test_record_exists() {
388 global $DB;
389 $this->assertFalse($DB->record_exists(tool_lp_testable_persistent::TABLE, array('idnumber' => 'abc')));
390 $p = new tool_lp_testable_persistent(0, (object) array('sortorder' => 123, 'idnumber' => 'abc'));
391 $p->create();
392 $id = $p->get_id();
393 $this->assertTrue(tool_lp_testable_persistent::record_exists($id));
394 $this->assertTrue($DB->record_exists(tool_lp_testable_persistent::TABLE, array('idnumber' => 'abc')));
395 $p->delete();
396 $this->assertFalse(tool_lp_testable_persistent::record_exists($id));
397 }
398
dee2fb57
FM
399}
400
401/**
402 * Example persistent class.
403 *
404 * @package tool_lp
405 * @copyright 2015 Frédéric Massart - FMCorz.net
406 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
407 */
096166a0 408class tool_lp_testable_persistent extends \tool_lp\persistent {
dee2fb57
FM
409
410 const TABLE = 'tool_lp_competency';
411
412 protected static function define_properties() {
413 return array(
414 'shortname' => array(
415 'type' => PARAM_TEXT,
416 'default' => ''
417 ),
418 'idnumber' => array(
419 'type' => PARAM_TEXT,
420 ),
421 'description' => array(
422 'type' => PARAM_TEXT,
423 'default' => ''
424 ),
425 'descriptionformat' => array(
426 'choices' => array(FORMAT_HTML, FORMAT_MOODLE, FORMAT_PLAIN, FORMAT_MARKDOWN),
427 'type' => PARAM_INT,
428 'default' => FORMAT_HTML
429 ),
430 'parentid' => array(
431 'type' => PARAM_INT,
432 'default' => 0
433 ),
434 'visible' => array(
435 'type' => PARAM_BOOL,
436 'default' => 1
437 ),
438 'path' => array(
439 'type' => PARAM_RAW,
440 'default' => ''
441 ),
442 'sortorder' => array(
443 'type' => PARAM_INT,
444 'message' => new lang_string('invalidrequest', 'error')
445 ),
446 'competencyframeworkid' => array(
447 'type' => PARAM_INT,
096166a0
FM
448 'default' => 0,
449 'null' => NULL_ALLOWED
dee2fb57 450 ),
1896274f
FM
451 'ruletype' => array(
452 'type' => PARAM_RAW,
453 'default' => null,
454 'null' => NULL_ALLOWED,
455 ),
456 'ruleconfig' => array(
457 'type' => PARAM_RAW,
458 'default' => null,
459 'null' => NULL_ALLOWED,
460 ),
461 'ruleoutcome' => array(
462 'type' => PARAM_RAW,
463 'default' => 0
bf62b21d
FM
464 ),
465 'scaleid' => array(
466 'type' => PARAM_INT,
467 'default' => null,
468 'null' => NULL_ALLOWED
1896274f 469 )
dee2fb57
FM
470 );
471 }
472
096166a0
FM
473 protected function before_validate() {
474 $this->beforevalidate = true;
475 }
476
477 protected function before_create() {
478 $this->beforecreate = true;
479 }
480
481 protected function before_update() {
482 $this->beforeupdate = true;
483 }
484
485 protected function before_delete() {
486 $this->beforedelete = true;
487 }
488
489 protected function after_create() {
490 $this->aftercreate = true;
491 }
492
493 protected function after_update($result) {
494 $this->afterupdate = true;
495 }
496
497 protected function after_delete($result) {
498 $this->afterdelete = true;
499 }
500
dee2fb57
FM
501 public function get_path() {
502 $value = $this->get('path');
503 if (!empty($value)) {
504 $value = json_decode($value);
505 }
506 return $value;
507 }
508
509 public function set_path($value) {
510 if (!empty($value)) {
511 $value = json_encode($value);
512 }
513 $this->set('path', $value);
514 }
515
03c66658 516 protected function validate_sortorder($value) {
dee2fb57
FM
517 if ($value == 10) {
518 return new lang_string('invalidkey', 'error');
519 }
520 return true;
521 }
522
523}