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