2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Persistent class tests.
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
25 defined('MOODLE_INTERNAL') || die();
29 * Persistent testcase.
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
35 class core_competency_persistent_testcase extends advanced_testcase {
37 public function setUp() {
38 $this->resetAfterTest();
41 public function test_properties_definition() {
46 'null' => NULL_NOT_ALLOWED
50 'null' => NULL_NOT_ALLOWED
52 'description' => array(
55 'null' => NULL_NOT_ALLOWED
57 'descriptionformat' => array(
58 'choices' => array(FORMAT_HTML, FORMAT_MOODLE, FORMAT_PLAIN, FORMAT_MARKDOWN),
60 'default' => FORMAT_HTML,
61 'null' => NULL_NOT_ALLOWED
66 'null' => NULL_NOT_ALLOWED
71 'null' => NULL_NOT_ALLOWED
75 'message' => new lang_string('invalidrequest', 'error'),
76 'null' => NULL_NOT_ALLOWED
78 'competencyframeworkid' => array(
81 'null' => NULL_ALLOWED
86 'null' => NULL_NOT_ALLOWED
88 'timecreated' => array(
91 'null' => NULL_NOT_ALLOWED
93 'timemodified' => array(
96 'null' => NULL_NOT_ALLOWED
98 'usermodified' => array(
101 'null' => NULL_NOT_ALLOWED
106 'null' => NULL_ALLOWED,
108 'ruleconfig' => array(
111 'null' => NULL_ALLOWED,
113 'ruleoutcome' => array(
116 'null' => NULL_NOT_ALLOWED
121 'null' => NULL_ALLOWED
123 'scaleconfiguration' => array(
126 'null' => NULL_ALLOWED
129 $this->assertEquals($expected, core_competency_testable_persistent::properties_definition());
132 public function test_to_record() {
133 $p = new core_competency_testable_persistent();
134 $expected = (object) array(
138 'descriptionformat' => FORMAT_HTML,
142 'competencyframeworkid' => null,
148 'ruleconfig' => null,
151 'scaleconfiguration' => null,
153 $this->assertEquals($expected, $p->to_record());
156 public function test_from_record() {
157 $p = new core_competency_testable_persistent();
158 $data = (object) array(
159 'shortname' => 'ddd',
161 'description' => 'xyz',
162 'descriptionformat' => FORMAT_PLAIN,
166 'competencyframeworkid' => 5,
172 'ruleconfig' => null,
175 'scaleconfiguration' => null,
177 $p->from_record($data);
178 $this->assertEquals($data, $p->to_record());
181 public function test_from_record_invalid_param() {
182 $p = new core_competency_testable_persistent();
183 $data = (object) array(
184 'invalidparam' => 'abc'
186 $this->setExpectedException('coding_exception');
187 $p->from_record($data);
190 public function test_validate() {
191 $data = (object) array(
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);
204 'descriptionformat' => new lang_string('invaliddata', 'error'),
206 $this->assertEquals($expected, $p->validate());
207 $this->assertFalse($p->is_valid());
208 $this->assertEquals($expected, $p->get_errors());
211 public function test_validation_required() {
212 $data = (object) array(
215 $p = new core_competency_testable_persistent(0, $data);
217 'sortorder' => new lang_string('requiredelement', 'form'),
219 $this->assertFalse($p->is_valid());
220 $this->assertEquals($expected, $p->get_errors());
223 public function test_validation_custom() {
224 $data = (object) array(
228 $p = new core_competency_testable_persistent(0, $data);
230 'sortorder' => new lang_string('invalidkey', 'error'),
232 $this->assertFalse($p->is_valid());
233 $this->assertEquals($expected, $p->get_errors());
236 public function test_validation_custom_message() {
237 $data = (object) array(
239 'sortorder' => 'abc',
241 $p = new core_competency_testable_persistent(0, $data);
243 'sortorder' => new lang_string('invalidrequest', 'error'),
245 $this->assertFalse($p->is_valid());
246 $this->assertEquals($expected, $p->get_errors());
249 public function test_validation_choices() {
250 $data = (object) array(
253 'descriptionformat' => -100
255 $p = new core_competency_testable_persistent(0, $data);
257 'descriptionformat' => new lang_string('invaliddata', 'error'),
259 $this->assertFalse($p->is_valid());
260 $this->assertEquals($expected, $p->get_errors());
263 public function test_validation_type() {
264 $data = (object) array(
268 $p = new core_competency_testable_persistent(0, $data);
269 $this->assertFalse($p->is_valid());
270 $this->assertArrayHasKey('sortorder', $p->get_errors());
273 public function test_validation_null() {
274 $data = (object) array(
277 'competencyframeworkid' => 'bad!'
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());
292 public function test_create() {
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));
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.
308 public function test_update() {
310 $p = new core_competency_testable_persistent(0, (object) array('sortorder' => 123, 'idnumber' => 'abc'));
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));
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.
329 public function test_read() {
330 $p = new core_competency_testable_persistent(0, (object) array('sortorder' => 123, 'idnumber' => 'abc'));
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());
342 $this->assertEquals($p, $p3);
345 public function test_delete() {
348 $p = new core_competency_testable_persistent(0, (object) array('sortorder' => 123, 'idnumber' => 'abc'));
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));
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);
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'));
367 public function test_custom_setter_getter() {
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'));
375 $this->assertEquals($path, $p->get_path());
376 $this->assertEquals($json, $p->to_record()->path);
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);
383 public function test_record_exists() {
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'));
389 $this->assertTrue(core_competency_testable_persistent::record_exists($id));
390 $this->assertTrue($DB->record_exists(core_competency_testable_persistent::TABLE, array('idnumber' => 'abc')));
392 $this->assertFalse(core_competency_testable_persistent::record_exists($id));
398 * Example persistent class.
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
404 class core_competency_testable_persistent extends \core_competency\persistent {
406 const TABLE = 'competency';
408 protected static function define_properties() {
410 'shortname' => array(
411 'type' => PARAM_TEXT,
415 'type' => PARAM_TEXT,
417 'description' => array(
418 'type' => PARAM_TEXT,
421 'descriptionformat' => array(
422 'choices' => array(FORMAT_HTML, FORMAT_MOODLE, FORMAT_PLAIN, FORMAT_MARKDOWN),
424 'default' => FORMAT_HTML
434 'sortorder' => array(
436 'message' => new lang_string('invalidrequest', 'error')
438 'competencyframeworkid' => array(
441 'null' => NULL_ALLOWED
446 'null' => NULL_ALLOWED,
448 'ruleconfig' => array(
451 'null' => NULL_ALLOWED,
453 'ruleoutcome' => array(
460 'null' => NULL_ALLOWED
462 'scaleconfiguration' => array(
465 'null' => NULL_ALLOWED
470 protected function before_validate() {
471 $this->beforevalidate = true;
474 protected function before_create() {
475 $this->beforecreate = true;
478 protected function before_update() {
479 $this->beforeupdate = true;
482 protected function before_delete() {
483 $this->beforedelete = true;
486 protected function after_create() {
487 $this->aftercreate = true;
490 protected function after_update($result) {
491 $this->afterupdate = true;
494 protected function after_delete($result) {
495 $this->afterdelete = true;
498 public function get_path() {
499 $value = $this->get('path');
500 if (!empty($value)) {
501 $value = json_decode($value);
506 public function set_path($value) {
507 if (!empty($value)) {
508 $value = json_encode($value);
510 $this->set('path', $value);
513 protected function validate_sortorder($value) {
515 return new lang_string('invalidkey', 'error');