MDL-53700 competency: Migrating data generator to core
[moodle.git] / competency / tests / persistent_test.php
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/>.
17 /**
18  * Persistent class tests.
19  *
20  * @package    core_competency
21  * @copyright  2015 Frédéric Massart - FMCorz.net
22  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23  */
25 defined('MOODLE_INTERNAL') || die();
26 global $CFG;
28 /**
29  * Persistent testcase.
30  *
31  * @package    core_competency
32  * @copyright  2015 Frédéric Massart - FMCorz.net
33  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34  */
35 class core_competency_persistent_testcase extends advanced_testcase {
37     public function setUp() {
38         $this->resetAfterTest();
39     }
41     public function test_properties_definition() {
42         $expected = array(
43             'shortname' => array(
44                 'type' => PARAM_TEXT,
45                 'default' => '',
46                 'null' => NULL_NOT_ALLOWED
47             ),
48             'idnumber' => array(
49                 'type' => PARAM_TEXT,
50                 'null' => NULL_NOT_ALLOWED
51             ),
52             'description' => array(
53                 'type' => PARAM_TEXT,
54                 'default' => '',
55                 'null' => NULL_NOT_ALLOWED
56             ),
57             'descriptionformat' => array(
58                 'choices' => array(FORMAT_HTML, FORMAT_MOODLE, FORMAT_PLAIN, FORMAT_MARKDOWN),
59                 'type' => PARAM_INT,
60                 'default' => FORMAT_HTML,
61                 'null' => NULL_NOT_ALLOWED
62             ),
63             'parentid' => array(
64                 'type' => PARAM_INT,
65                 'default' => 0,
66                 'null' => NULL_NOT_ALLOWED
67             ),
68             'path' => array(
69                 'type' => PARAM_RAW,
70                 'default' => '',
71                 'null' => NULL_NOT_ALLOWED
72             ),
73             'sortorder' => array(
74                 'type' => PARAM_INT,
75                 'message' => new lang_string('invalidrequest', 'error'),
76                 'null' => NULL_NOT_ALLOWED
77             ),
78             'competencyframeworkid' => array(
79                 'type' => PARAM_INT,
80                 'default' => 0,
81                 'null' => NULL_ALLOWED
82             ),
83             'id' => array(
84                 'default' => 0,
85                 'type' => PARAM_INT,
86                 'null' => NULL_NOT_ALLOWED
87             ),
88             'timecreated' => array(
89                 'default' => 0,
90                 'type' => PARAM_INT,
91                 'null' => NULL_NOT_ALLOWED
92             ),
93             'timemodified' => array(
94                 'default' => 0,
95                 'type' => PARAM_INT,
96                 'null' => NULL_NOT_ALLOWED
97             ),
98             'usermodified' => array(
99                 'default' => 0,
100                 'type' => PARAM_INT,
101                 'null' => NULL_NOT_ALLOWED
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,
115                 'default' => 0,
116                 'null' => NULL_NOT_ALLOWED
117             ),
118             'scaleid' => array(
119                 'default' => null,
120                 'type' => PARAM_INT,
121                 'null' => NULL_ALLOWED
122             ),
123             'scaleconfiguration' => array(
124                 'type' => PARAM_RAW,
125                 'default' => null,
126                 'null' => NULL_ALLOWED
127             )
128         );
129         $this->assertEquals($expected, core_competency_testable_persistent::properties_definition());
130     }
132     public function test_to_record() {
133         $p = new core_competency_testable_persistent();
134         $expected = (object) array(
135             'shortname' => '',
136             'idnumber' => null,
137             'description' => '',
138             'descriptionformat' => FORMAT_HTML,
139             'parentid' => 0,
140             'path' => '',
141             'sortorder' => null,
142             'competencyframeworkid' => null,
143             'id' => 0,
144             'timecreated' => 0,
145             'timemodified' => 0,
146             'usermodified' => 0,
147             'ruletype' => null,
148             'ruleconfig' => null,
149             'ruleoutcome' => 0,
150             'scaleid' => null,
151             'scaleconfiguration' => null,
152         );
153         $this->assertEquals($expected, $p->to_record());
154     }
156     public function test_from_record() {
157         $p = new core_competency_testable_persistent();
158         $data = (object) array(
159             'shortname' => 'ddd',
160             'idnumber' => 'abc',
161             'description' => 'xyz',
162             'descriptionformat' => FORMAT_PLAIN,
163             'parentid' => 999,
164             'path' => '/a/b/c',
165             'sortorder' => 12,
166             'competencyframeworkid' => 5,
167             'id' => 1,
168             'timecreated' => 2,
169             'timemodified' => 3,
170             'usermodified' => 4,
171             'ruletype' => null,
172             'ruleconfig' => null,
173             'ruleoutcome' => 0,
174             'scaleid' => null,
175             'scaleconfiguration' => null,
176         );
177         $p->from_record($data);
178         $this->assertEquals($data, $p->to_record());
179     }
181     public function test_from_record_invalid_param() {
182         $p = new core_competency_testable_persistent();
183         $data = (object) array(
184             'invalidparam' => 'abc'
185         );
186         $this->setExpectedException('coding_exception');
187         $p->from_record($data);
188     }
190     public function test_validate() {
191         $data = (object) array(
192             'idnumber' => 'abc',
193             'sortorder' => 0
194         );
195         $p = new core_competency_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);
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     }
211     public function test_validation_required() {
212         $data = (object) array(
213             'idnumber' => 'abc'
214         );
215         $p = new core_competency_testable_persistent(0, $data);
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     }
223     public function test_validation_custom() {
224         $data = (object) array(
225             'idnumber' => 'abc',
226             'sortorder' => 10,
227         );
228         $p = new core_competency_testable_persistent(0, $data);
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     }
236     public function test_validation_custom_message() {
237         $data = (object) array(
238             'idnumber' => 'abc',
239             'sortorder' => 'abc',
240         );
241         $p = new core_competency_testable_persistent(0, $data);
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     }
249     public function test_validation_choices() {
250         $data = (object) array(
251             'idnumber' => 'abc',
252             'sortorder' => 0,
253             'descriptionformat' => -100
254         );
255         $p = new core_competency_testable_persistent(0, $data);
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     }
263     public function test_validation_type() {
264         $data = (object) array(
265             'idnumber' => 'abc',
266             'sortorder' => 'NaN'
267         );
268         $p = new core_competency_testable_persistent(0, $data);
269         $this->assertFalse($p->is_valid());
270         $this->assertArrayHasKey('sortorder', $p->get_errors());
271     }
273     public function test_validation_null() {
274         $data = (object) array(
275             'idnumber' => null,
276             'sortorder' => 0,
277             'competencyframeworkid' => 'bad!'
278         );
279         $p = new core_competency_testable_persistent(0, $data);
280         $this->assertFalse($p->is_valid());
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());
290     }
292     public function test_create() {
293         global $DB;
294         $p = new core_competency_testable_persistent(0, (object) array('sortorder' => 123, 'idnumber' => 'abc'));
295         $this->assertFalse(isset($p->beforecreate));
296         $this->assertFalse(isset($p->aftercreate));
297         $p->create();
298         $record = $DB->get_record(core_competency_testable_persistent::TABLE, array('id' => $p->get_id()), '*', MUST_EXIST);
299         $expected = $p->to_record();
300         $this->assertTrue(isset($p->beforecreate));
301         $this->assertTrue(isset($p->aftercreate));
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     }
308     public function test_update() {
309         global $DB;
310         $p = new core_competency_testable_persistent(0, (object) array('sortorder' => 123, 'idnumber' => 'abc'));
311         $p->create();
312         $id = $p->get_id();
313         $p->set_sortorder(456);
314         $p->from_record((object) array('idnumber' => 'def'));
315         $this->assertFalse(isset($p->beforeupdate));
316         $this->assertFalse(isset($p->afterupdate));
317         $p->update();
319         $expected = $p->to_record();
320         $record = $DB->get_record(core_competency_testable_persistent::TABLE, array('id' => $p->get_id()), '*', MUST_EXIST);
321         $this->assertTrue(isset($p->beforeupdate));
322         $this->assertTrue(isset($p->afterupdate));
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     }
329     public function test_read() {
330         $p = new core_competency_testable_persistent(0, (object) array('sortorder' => 123, 'idnumber' => 'abc'));
331         $p->create();
332         unset($p->beforevalidate);
333         unset($p->beforecreate);
334         unset($p->aftercreate);
336         $p2 = new core_competency_testable_persistent($p->get_id());
337         $this->assertEquals($p, $p2);
339         $p3 = new core_competency_testable_persistent();
340         $p3->set_id($p->get_id());
341         $p3->read();
342         $this->assertEquals($p, $p3);
343     }
345     public function test_delete() {
346         global $DB;
348         $p = new core_competency_testable_persistent(0, (object) array('sortorder' => 123, 'idnumber' => 'abc'));
349         $p->create();
350         $this->assertNotEquals(0, $p->get_id());
351         $this->assertTrue($DB->record_exists_select(core_competency_testable_persistent::TABLE, 'id = ?', array($p->get_id())));
352         $this->assertFalse(isset($p->beforedelete));
353         $this->assertFalse(isset($p->afterdelete));
355         $p->delete();
356         $this->assertFalse($DB->record_exists_select(core_competency_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);
360     }
362     public function test_has_property() {
363         $this->assertFalse(core_competency_testable_persistent::has_property('unknown'));
364         $this->assertTrue(core_competency_testable_persistent::has_property('idnumber'));
365     }
367     public function test_custom_setter_getter() {
368         global $DB;
370         $path = array(1, 2, 3);
371         $json = json_encode($path);
373         $p = new core_competency_testable_persistent(0, (object) array('sortorder' => 0, 'idnumber' => 'abc'));
374         $p->set_path($path);
375         $this->assertEquals($path, $p->get_path());
376         $this->assertEquals($json, $p->to_record()->path);
378         $p->create();
379         $record = $DB->get_record(core_competency_testable_persistent::TABLE, array('id' => $p->get_id()), 'id, path', MUST_EXIST);
380         $this->assertEquals($json, $record->path);
381     }
383     public function test_record_exists() {
384         global $DB;
385         $this->assertFalse($DB->record_exists(core_competency_testable_persistent::TABLE, array('idnumber' => 'abc')));
386         $p = new core_competency_testable_persistent(0, (object) array('sortorder' => 123, 'idnumber' => 'abc'));
387         $p->create();
388         $id = $p->get_id();
389         $this->assertTrue(core_competency_testable_persistent::record_exists($id));
390         $this->assertTrue($DB->record_exists(core_competency_testable_persistent::TABLE, array('idnumber' => 'abc')));
391         $p->delete();
392         $this->assertFalse(core_competency_testable_persistent::record_exists($id));
393     }
397 /**
398  * Example persistent class.
399  *
400  * @package    core_competency
401  * @copyright  2015 Frédéric Massart - FMCorz.net
402  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
403  */
404 class core_competency_testable_persistent extends \core_competency\persistent {
406     const TABLE = 'competency';
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             ),
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,
440                 'default' => 0,
441                 'null' => NULL_ALLOWED
442             ),
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
456             ),
457             'scaleid' => array(
458                 'type' => PARAM_INT,
459                 'default' => null,
460                 'null' => NULL_ALLOWED
461             ),
462             'scaleconfiguration' => array(
463                 'type' => PARAM_RAW,
464                 'default' => null,
465                 'null' => NULL_ALLOWED
466             )
467         );
468     }
470     protected function before_validate() {
471         $this->beforevalidate = true;
472     }
474     protected function before_create() {
475         $this->beforecreate = true;
476     }
478     protected function before_update() {
479         $this->beforeupdate = true;
480     }
482     protected function before_delete() {
483         $this->beforedelete = true;
484     }
486     protected function after_create() {
487         $this->aftercreate = true;
488     }
490     protected function after_update($result) {
491         $this->afterupdate = true;
492     }
494     protected function after_delete($result) {
495         $this->afterdelete = true;
496     }
498     public function get_path() {
499         $value = $this->get('path');
500         if (!empty($value)) {
501             $value = json_decode($value);
502         }
503         return $value;
504     }
506     public function set_path($value) {
507         if (!empty($value)) {
508             $value = json_encode($value);
509         }
510         $this->set('path', $value);
511     }
513     protected function validate_sortorder($value) {
514         if ($value == 10) {
515             return new lang_string('invalidkey', 'error');
516         }
517         return true;
518     }