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());
182 * @expectedException coding_exception
184 public function test_from_record_invalid_param() {
185 $p = new core_competency_testable_persistent();
186 $data = (object) array(
187 'invalidparam' => 'abc'
190 $p->from_record($data);
193 public function test_validate() {
194 $data = (object) array(
198 $p = new core_competency_testable_persistent(0, $data);
199 $this->assertFalse(isset($p->beforevalidate
));
200 $this->assertTrue($p->validate());
201 $this->assertTrue(isset($p->beforevalidate
));
202 $this->assertTrue($p->is_valid());
203 $this->assertEquals(array(), $p->get_errors());
204 $p->set_descriptionformat(-100);
207 'descriptionformat' => new lang_string('invaliddata', 'error'),
209 $this->assertEquals($expected, $p->validate());
210 $this->assertFalse($p->is_valid());
211 $this->assertEquals($expected, $p->get_errors());
214 public function test_validation_required() {
215 $data = (object) array(
218 $p = new core_competency_testable_persistent(0, $data);
220 'sortorder' => new lang_string('requiredelement', 'form'),
222 $this->assertFalse($p->is_valid());
223 $this->assertEquals($expected, $p->get_errors());
226 public function test_validation_custom() {
227 $data = (object) array(
231 $p = new core_competency_testable_persistent(0, $data);
233 'sortorder' => new lang_string('invalidkey', 'error'),
235 $this->assertFalse($p->is_valid());
236 $this->assertEquals($expected, $p->get_errors());
239 public function test_validation_custom_message() {
240 $data = (object) array(
242 'sortorder' => 'abc',
244 $p = new core_competency_testable_persistent(0, $data);
246 'sortorder' => new lang_string('invalidrequest', 'error'),
248 $this->assertFalse($p->is_valid());
249 $this->assertEquals($expected, $p->get_errors());
252 public function test_validation_choices() {
253 $data = (object) array(
256 'descriptionformat' => -100
258 $p = new core_competency_testable_persistent(0, $data);
260 'descriptionformat' => new lang_string('invaliddata', 'error'),
262 $this->assertFalse($p->is_valid());
263 $this->assertEquals($expected, $p->get_errors());
266 public function test_validation_type() {
267 $data = (object) array(
271 $p = new core_competency_testable_persistent(0, $data);
272 $this->assertFalse($p->is_valid());
273 $this->assertArrayHasKey('sortorder', $p->get_errors());
276 public function test_validation_null() {
277 $data = (object) array(
280 'competencyframeworkid' => 'bad!'
282 $p = new core_competency_testable_persistent(0, $data);
283 $this->assertFalse($p->is_valid());
284 $this->assertArrayHasKey('idnumber', $p->get_errors());
285 $this->assertArrayHasKey('competencyframeworkid', $p->get_errors());
286 $p->set_idnumber('abc');
287 $this->assertFalse($p->is_valid());
288 $this->assertArrayNotHasKey('idnumber', $p->get_errors());
289 $this->assertArrayHasKey('competencyframeworkid', $p->get_errors());
290 $p->set_competencyframeworkid(null);
291 $this->assertTrue($p->is_valid());
292 $this->assertArrayNotHasKey('competencyframeworkid', $p->get_errors());
295 public function test_create() {
297 $p = new core_competency_testable_persistent(0, (object) array('sortorder' => 123, 'idnumber' => 'abc'));
298 $this->assertFalse(isset($p->beforecreate
));
299 $this->assertFalse(isset($p->aftercreate
));
301 $record = $DB->get_record(core_competency_testable_persistent
::TABLE
, array('id' => $p->get_id()), '*', MUST_EXIST
);
302 $expected = $p->to_record();
303 $this->assertTrue(isset($p->beforecreate
));
304 $this->assertTrue(isset($p->aftercreate
));
305 $this->assertEquals($expected->sortorder
, $record->sortorder
);
306 $this->assertEquals($expected->idnumber
, $record->idnumber
);
307 $this->assertEquals($expected->id
, $record->id
);
308 $this->assertTrue($p->is_valid()); // Should always be valid after a create.
311 public function test_update() {
313 $p = new core_competency_testable_persistent(0, (object) array('sortorder' => 123, 'idnumber' => 'abc'));
316 $p->set_sortorder(456);
317 $p->from_record((object) array('idnumber' => 'def'));
318 $this->assertFalse(isset($p->beforeupdate
));
319 $this->assertFalse(isset($p->afterupdate
));
322 $expected = $p->to_record();
323 $record = $DB->get_record(core_competency_testable_persistent
::TABLE
, array('id' => $p->get_id()), '*', MUST_EXIST
);
324 $this->assertTrue(isset($p->beforeupdate
));
325 $this->assertTrue(isset($p->afterupdate
));
326 $this->assertEquals($id, $record->id
);
327 $this->assertEquals(456, $record->sortorder
);
328 $this->assertEquals('def', $record->idnumber
);
329 $this->assertTrue($p->is_valid()); // Should always be valid after an update.
332 public function test_read() {
333 $p = new core_competency_testable_persistent(0, (object) array('sortorder' => 123, 'idnumber' => 'abc'));
335 unset($p->beforevalidate
);
336 unset($p->beforecreate
);
337 unset($p->aftercreate
);
339 $p2 = new core_competency_testable_persistent($p->get_id());
340 $this->assertEquals($p, $p2);
342 $p3 = new core_competency_testable_persistent();
343 $p3->set_id($p->get_id());
345 $this->assertEquals($p, $p3);
348 public function test_delete() {
351 $p = new core_competency_testable_persistent(0, (object) array('sortorder' => 123, 'idnumber' => 'abc'));
353 $this->assertNotEquals(0, $p->get_id());
354 $this->assertTrue($DB->record_exists_select(core_competency_testable_persistent
::TABLE
, 'id = ?', array($p->get_id())));
355 $this->assertFalse(isset($p->beforedelete
));
356 $this->assertFalse(isset($p->afterdelete
));
359 $this->assertFalse($DB->record_exists_select(core_competency_testable_persistent
::TABLE
, 'id = ?', array($p->get_id())));
360 $this->assertEquals(0, $p->get_id());
361 $this->assertEquals(true, $p->beforedelete
);
362 $this->assertEquals(true, $p->afterdelete
);
365 public function test_has_property() {
366 $this->assertFalse(core_competency_testable_persistent
::has_property('unknown'));
367 $this->assertTrue(core_competency_testable_persistent
::has_property('idnumber'));
370 public function test_custom_setter_getter() {
373 $path = array(1, 2, 3);
374 $json = json_encode($path);
376 $p = new core_competency_testable_persistent(0, (object) array('sortorder' => 0, 'idnumber' => 'abc'));
378 $this->assertEquals($path, $p->get_path());
379 $this->assertEquals($json, $p->to_record()->path
);
382 $record = $DB->get_record(core_competency_testable_persistent
::TABLE
, array('id' => $p->get_id()), 'id, path', MUST_EXIST
);
383 $this->assertEquals($json, $record->path
);
386 public function test_record_exists() {
388 $this->assertFalse($DB->record_exists(core_competency_testable_persistent
::TABLE
, array('idnumber' => 'abc')));
389 $p = new core_competency_testable_persistent(0, (object) array('sortorder' => 123, 'idnumber' => 'abc'));
392 $this->assertTrue(core_competency_testable_persistent
::record_exists($id));
393 $this->assertTrue($DB->record_exists(core_competency_testable_persistent
::TABLE
, array('idnumber' => 'abc')));
395 $this->assertFalse(core_competency_testable_persistent
::record_exists($id));
398 public function test_get_sql_fields() {
400 'c.id AS comp_id, ' .
401 'c.shortname AS comp_shortname, ' .
402 'c.idnumber AS comp_idnumber, ' .
403 'c.description AS comp_description, ' .
404 'c.descriptionformat AS comp_descriptionformat, ' .
405 'c.parentid AS comp_parentid, ' .
406 'c.path AS comp_path, ' .
407 'c.sortorder AS comp_sortorder, ' .
408 'c.competencyframeworkid AS comp_competencyframeworkid, ' .
409 'c.ruletype AS comp_ruletype, ' .
410 'c.ruleconfig AS comp_ruleconfig, ' .
411 'c.ruleoutcome AS comp_ruleoutcome, ' .
412 'c.scaleid AS comp_scaleid, ' .
413 'c.scaleconfiguration AS comp_scaleconfiguration, ' .
414 'c.timecreated AS comp_timecreated, ' .
415 'c.timemodified AS comp_timemodified, ' .
416 'c.usermodified AS comp_usermodified';
417 $this->assertEquals($expected, core_competency_testable_persistent
::get_sql_fields('c', 'comp_'));
421 * @expectedException coding_exception
422 * @expectedExceptionMessageRegExp /The alias .+ exceeds 30 characters/
424 public function test_get_sql_fields_too_long() {
425 core_competency_testable_persistent
::get_sql_fields('c');
430 * Example persistent class.
432 * @package core_competency
433 * @copyright 2015 Frédéric Massart - FMCorz.net
434 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
436 class core_competency_testable_persistent
extends \core_competency\persistent
{
438 const TABLE
= 'competency';
440 protected static function define_properties() {
442 'shortname' => array(
443 'type' => PARAM_TEXT
,
447 'type' => PARAM_TEXT
,
449 'description' => array(
450 'type' => PARAM_TEXT
,
453 'descriptionformat' => array(
454 'choices' => array(FORMAT_HTML
, FORMAT_MOODLE
, FORMAT_PLAIN
, FORMAT_MARKDOWN
),
456 'default' => FORMAT_HTML
466 'sortorder' => array(
468 'message' => new lang_string('invalidrequest', 'error')
470 'competencyframeworkid' => array(
473 'null' => NULL_ALLOWED
478 'null' => NULL_ALLOWED
,
480 'ruleconfig' => array(
483 'null' => NULL_ALLOWED
,
485 'ruleoutcome' => array(
492 'null' => NULL_ALLOWED
494 'scaleconfiguration' => array(
497 'null' => NULL_ALLOWED
502 protected function before_validate() {
503 $this->beforevalidate
= true;
506 protected function before_create() {
507 $this->beforecreate
= true;
510 protected function before_update() {
511 $this->beforeupdate
= true;
514 protected function before_delete() {
515 $this->beforedelete
= true;
518 protected function after_create() {
519 $this->aftercreate
= true;
522 protected function after_update($result) {
523 $this->afterupdate
= true;
526 protected function after_delete($result) {
527 $this->afterdelete
= true;
530 public function get_path() {
531 $value = $this->get('path');
532 if (!empty($value)) {
533 $value = json_decode($value);
538 public function set_path($value) {
539 if (!empty($value)) {
540 $value = json_encode($value);
542 $this->set('path', $value);
545 protected function validate_sortorder($value) {
547 return new lang_string('invalidkey', 'error');