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