[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/competency/tests/ -> persistent_test.php (source)

   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    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   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  global $CFG;
  27  
  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 {
  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                  '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      }
 131  
 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      }
 155  
 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      }
 180  
 181      /**
 182       * @expectedException coding_exception
 183       */
 184      public function test_from_record_invalid_param() {
 185          $p = new core_competency_testable_persistent();
 186          $data = (object) array(
 187              'invalidparam' => 'abc'
 188          );
 189  
 190          $p->from_record($data);
 191      }
 192  
 193      public function test_validate() {
 194          $data = (object) array(
 195              'idnumber' => 'abc',
 196              'sortorder' => 0
 197          );
 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);
 205  
 206          $expected = array(
 207              'descriptionformat' => new lang_string('invaliddata', 'error'),
 208          );
 209          $this->assertEquals($expected, $p->validate());
 210          $this->assertFalse($p->is_valid());
 211          $this->assertEquals($expected, $p->get_errors());
 212      }
 213  
 214      public function test_validation_required() {
 215          $data = (object) array(
 216              'idnumber' => 'abc'
 217          );
 218          $p = new core_competency_testable_persistent(0, $data);
 219          $expected = array(
 220              'sortorder' => new lang_string('requiredelement', 'form'),
 221          );
 222          $this->assertFalse($p->is_valid());
 223          $this->assertEquals($expected, $p->get_errors());
 224      }
 225  
 226      public function test_validation_custom() {
 227          $data = (object) array(
 228              'idnumber' => 'abc',
 229              'sortorder' => 10,
 230          );
 231          $p = new core_competency_testable_persistent(0, $data);
 232          $expected = array(
 233              'sortorder' => new lang_string('invalidkey', 'error'),
 234          );
 235          $this->assertFalse($p->is_valid());
 236          $this->assertEquals($expected, $p->get_errors());
 237      }
 238  
 239      public function test_validation_custom_message() {
 240          $data = (object) array(
 241              'idnumber' => 'abc',
 242              'sortorder' => 'abc',
 243          );
 244          $p = new core_competency_testable_persistent(0, $data);
 245          $expected = array(
 246              'sortorder' => new lang_string('invalidrequest', 'error'),
 247          );
 248          $this->assertFalse($p->is_valid());
 249          $this->assertEquals($expected, $p->get_errors());
 250      }
 251  
 252      public function test_validation_choices() {
 253          $data = (object) array(
 254              'idnumber' => 'abc',
 255              'sortorder' => 0,
 256              'descriptionformat' => -100
 257          );
 258          $p = new core_competency_testable_persistent(0, $data);
 259          $expected = array(
 260              'descriptionformat' => new lang_string('invaliddata', 'error'),
 261          );
 262          $this->assertFalse($p->is_valid());
 263          $this->assertEquals($expected, $p->get_errors());
 264      }
 265  
 266      public function test_validation_type() {
 267          $data = (object) array(
 268              'idnumber' => 'abc',
 269              'sortorder' => 'NaN'
 270          );
 271          $p = new core_competency_testable_persistent(0, $data);
 272          $this->assertFalse($p->is_valid());
 273          $this->assertArrayHasKey('sortorder', $p->get_errors());
 274      }
 275  
 276      public function test_validation_null() {
 277          $data = (object) array(
 278              'idnumber' => null,
 279              'sortorder' => 0,
 280              'competencyframeworkid' => 'bad!'
 281          );
 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());
 293      }
 294  
 295      public function test_create() {
 296          global $DB;
 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));
 300          $p->create();
 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.
 309      }
 310  
 311      public function test_update() {
 312          global $DB;
 313          $p = new core_competency_testable_persistent(0, (object) array('sortorder' => 123, 'idnumber' => 'abc'));
 314          $p->create();
 315          $id = $p->get_id();
 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));
 320          $p->update();
 321  
 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.
 330      }
 331  
 332      public function test_read() {
 333          $p = new core_competency_testable_persistent(0, (object) array('sortorder' => 123, 'idnumber' => 'abc'));
 334          $p->create();
 335          unset($p->beforevalidate);
 336          unset($p->beforecreate);
 337          unset($p->aftercreate);
 338  
 339          $p2 = new core_competency_testable_persistent($p->get_id());
 340          $this->assertEquals($p, $p2);
 341  
 342          $p3 = new core_competency_testable_persistent();
 343          $p3->set_id($p->get_id());
 344          $p3->read();
 345          $this->assertEquals($p, $p3);
 346      }
 347  
 348      public function test_delete() {
 349          global $DB;
 350  
 351          $p = new core_competency_testable_persistent(0, (object) array('sortorder' => 123, 'idnumber' => 'abc'));
 352          $p->create();
 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));
 357  
 358          $p->delete();
 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);
 363      }
 364  
 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'));
 368      }
 369  
 370      public function test_custom_setter_getter() {
 371          global $DB;
 372  
 373          $path = array(1, 2, 3);
 374          $json = json_encode($path);
 375  
 376          $p = new core_competency_testable_persistent(0, (object) array('sortorder' => 0, 'idnumber' => 'abc'));
 377          $p->set_path($path);
 378          $this->assertEquals($path, $p->get_path());
 379          $this->assertEquals($json, $p->to_record()->path);
 380  
 381          $p->create();
 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);
 384      }
 385  
 386      public function test_record_exists() {
 387          global $DB;
 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'));
 390          $p->create();
 391          $id = $p->get_id();
 392          $this->assertTrue(core_competency_testable_persistent::record_exists($id));
 393          $this->assertTrue($DB->record_exists(core_competency_testable_persistent::TABLE, array('idnumber' => 'abc')));
 394          $p->delete();
 395          $this->assertFalse(core_competency_testable_persistent::record_exists($id));
 396      }
 397  
 398      public function test_get_sql_fields() {
 399          $expected = '' .
 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_'));
 418      }
 419  
 420      /**
 421       * @expectedException               coding_exception
 422       * @expectedExceptionMessageRegExp  /The alias .+ exceeds 30 characters/
 423       */
 424      public function test_get_sql_fields_too_long() {
 425          core_competency_testable_persistent::get_sql_fields('c');
 426      }
 427  }
 428  
 429  /**
 430   * Example persistent class.
 431   *
 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
 435   */
 436  class core_competency_testable_persistent extends \core_competency\persistent {
 437  
 438      const TABLE = 'competency';
 439  
 440      protected static function define_properties() {
 441          return array(
 442              'shortname' => array(
 443                  'type' => PARAM_TEXT,
 444                  'default' => ''
 445              ),
 446              'idnumber' => array(
 447                  'type' => PARAM_TEXT,
 448              ),
 449              'description' => array(
 450                  'type' => PARAM_TEXT,
 451                  'default' => ''
 452              ),
 453              'descriptionformat' => array(
 454                  'choices' => array(FORMAT_HTML, FORMAT_MOODLE, FORMAT_PLAIN, FORMAT_MARKDOWN),
 455                  'type' => PARAM_INT,
 456                  'default' => FORMAT_HTML
 457              ),
 458              'parentid' => array(
 459                  'type' => PARAM_INT,
 460                  'default' => 0
 461              ),
 462              'path' => array(
 463                  'type' => PARAM_RAW,
 464                  'default' => ''
 465              ),
 466              'sortorder' => array(
 467                  'type' => PARAM_INT,
 468                  'message' => new lang_string('invalidrequest', 'error')
 469              ),
 470              'competencyframeworkid' => array(
 471                  'type' => PARAM_INT,
 472                  'default' => 0,
 473                  'null' => NULL_ALLOWED
 474              ),
 475              'ruletype' => array(
 476                  'type' => PARAM_RAW,
 477                  'default' => null,
 478                  'null' => NULL_ALLOWED,
 479              ),
 480              'ruleconfig' => array(
 481                  'type' => PARAM_RAW,
 482                  'default' => null,
 483                  'null' => NULL_ALLOWED,
 484              ),
 485              'ruleoutcome' => array(
 486                  'type' => PARAM_RAW,
 487                  'default' => 0
 488              ),
 489              'scaleid' => array(
 490                  'type' => PARAM_INT,
 491                  'default' => null,
 492                  'null' => NULL_ALLOWED
 493              ),
 494              'scaleconfiguration' => array(
 495                  'type' => PARAM_RAW,
 496                  'default' => null,
 497                  'null' => NULL_ALLOWED
 498              )
 499          );
 500      }
 501  
 502      protected function before_validate() {
 503          $this->beforevalidate = true;
 504      }
 505  
 506      protected function before_create() {
 507          $this->beforecreate = true;
 508      }
 509  
 510      protected function before_update() {
 511          $this->beforeupdate = true;
 512      }
 513  
 514      protected function before_delete() {
 515          $this->beforedelete = true;
 516      }
 517  
 518      protected function after_create() {
 519          $this->aftercreate = true;
 520      }
 521  
 522      protected function after_update($result) {
 523          $this->afterupdate = true;
 524      }
 525  
 526      protected function after_delete($result) {
 527          $this->afterdelete = true;
 528      }
 529  
 530      public function get_path() {
 531          $value = $this->get('path');
 532          if (!empty($value)) {
 533              $value = json_decode($value);
 534          }
 535          return $value;
 536      }
 537  
 538      public function set_path($value) {
 539          if (!empty($value)) {
 540              $value = json_encode($value);
 541          }
 542          $this->set('path', $value);
 543      }
 544  
 545      protected function validate_sortorder($value) {
 546          if ($value == 10) {
 547              return new lang_string('invalidkey', 'error');
 548          }
 549          return true;
 550      }
 551  
 552  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1