Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 310 and 311] [Versions 39 and 311]

   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  namespace core_customfield;
  18  
  19  use core_customfield_generator;
  20  use customfield_checkbox;
  21  use customfield_date;
  22  use customfield_select;
  23  use customfield_text;
  24  use customfield_textarea;
  25  
  26  /**
  27   * Functional test for class \core_customfield\field_controller.
  28   *
  29   * @package    core_customfield
  30   * @category   test
  31   * @copyright  2018 Ruslan Kabalin
  32   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   */
  34  class field_controller_test extends \advanced_testcase {
  35  
  36      /**
  37       * Get generator.
  38       *
  39       * @return core_customfield_generator
  40       */
  41      protected function get_generator(): core_customfield_generator {
  42          return $this->getDataGenerator()->get_plugin_generator('core_customfield');
  43      }
  44  
  45      /**
  46       * Test for function \core_customfield\field_controller::create()
  47       */
  48      public function test_constructor() {
  49          global $DB;
  50          $this->resetAfterTest();
  51  
  52          // Create the category.
  53          $category0 = $this->get_generator()->create_category();
  54  
  55          // Initiate objects without id, try with the category object or with category id or with both.
  56          $field0 = field_controller::create(0, (object)['type' => 'checkbox'], $category0);
  57          $this->assertInstanceOf(customfield_checkbox\field_controller::class, $field0);
  58          $field1 = field_controller::create(0, (object)['type' => 'date', 'categoryid' => $category0->get('id')]);
  59          $this->assertInstanceOf(customfield_date\field_controller::class, $field1);
  60          $field2 = field_controller::create(0, (object)['type' => 'select', 'categoryid' => $category0->get('id')], $category0);
  61          $this->assertInstanceOf(customfield_select\field_controller::class, $field2);
  62          $field3 = field_controller::create(0, (object)['type' => 'text'], $category0);
  63          $this->assertInstanceOf(customfield_text\field_controller::class, $field3);
  64          $field4 = field_controller::create(0, (object)['type' => 'textarea'], $category0);
  65          $this->assertInstanceOf(customfield_textarea\field_controller::class, $field4);
  66  
  67          // Save fields to the db so we have ids.
  68          \core_customfield\api::save_field_configuration($field0, (object)['name' => 'a', 'shortname' => 'a']);
  69          \core_customfield\api::save_field_configuration($field1, (object)['name' => 'b', 'shortname' => 'b']);
  70          \core_customfield\api::save_field_configuration($field2, (object)['name' => 'c', 'shortname' => 'c']);
  71          \core_customfield\api::save_field_configuration($field3, (object)['name' => 'd', 'shortname' => 'd']);
  72          \core_customfield\api::save_field_configuration($field4, (object)['name' => 'e', 'shortname' => 'e']);
  73  
  74          // Retrieve fields by id.
  75          $this->assertInstanceOf(customfield_checkbox\field_controller::class, field_controller::create($field0->get('id')));
  76          $this->assertInstanceOf(customfield_date\field_controller::class, field_controller::create($field1->get('id')));
  77  
  78          // Retrieve field by id and category.
  79          $this->assertInstanceOf(customfield_select\field_controller::class,
  80              field_controller::create($field2->get('id'), null, $category0));
  81  
  82          // Retrieve fields by record without category.
  83          $fieldrecord = $DB->get_record(\core_customfield\field::TABLE, ['id' => $field3->get('id')], '*', MUST_EXIST);
  84          $this->assertInstanceOf(customfield_text\field_controller::class, field_controller::create(0, $fieldrecord));
  85  
  86          // Retrieve fields by record with category.
  87          $fieldrecord = $DB->get_record(\core_customfield\field::TABLE, ['id' => $field4->get('id')], '*', MUST_EXIST);
  88          $this->assertInstanceOf(customfield_textarea\field_controller::class,
  89              field_controller::create(0, $fieldrecord, $category0));
  90      }
  91  
  92      /**
  93       * Test for function \core_customfield\field_controller::create() in case of wrong parameters
  94       */
  95      public function test_constructor_errors() {
  96          global $DB;
  97          $this->resetAfterTest();
  98  
  99          // Create a category and a field.
 100          $category = $this->get_generator()->create_category();
 101          $field = $this->get_generator()->create_field(['categoryid' => $category->get('id')]);
 102  
 103          $fieldrecord = $DB->get_record(\core_customfield\field::TABLE, ['id' => $field->get('id')], '*', MUST_EXIST);
 104  
 105          // Both id and record give warning.
 106          $field = field_controller::create($fieldrecord->id, $fieldrecord);
 107          $debugging = $this->getDebuggingMessages();
 108          $this->assertEquals(1, count($debugging));
 109          $this->assertEquals('Too many parameters, either id need to be specified or a record, but not both.',
 110              $debugging[0]->message);
 111          $this->resetDebugging();
 112          $this->assertInstanceOf(customfield_text\field_controller::class, $field);
 113  
 114          // Retrieve non-existing field.
 115          try {
 116              field_controller::create($fieldrecord->id + 1);
 117              $this->fail('Expected exception');
 118          } catch (\moodle_exception $e) {
 119              $this->assertEquals('Field not found', $e->getMessage());
 120              $this->assertEquals(\moodle_exception::class, get_class($e));
 121          }
 122  
 123          // Retrieve without id and without type.
 124          try {
 125              field_controller::create(0, (object)['name' => 'a'], $category);
 126              $this->fail('Expected exception');
 127          } catch (\coding_exception $e) {
 128              $this->assertEquals('Coding error detected, it must be fixed by a programmer: Not enough parameters to ' .
 129                  'initialise field_controller - unknown field type', $e->getMessage());
 130              $this->assertEquals(\coding_exception::class, get_class($e));
 131          }
 132  
 133          // Missing category id.
 134          try {
 135              field_controller::create(0, (object)['type' => 'text']);
 136              $this->fail('Expected exception');
 137          } catch (\coding_exception $e) {
 138              $this->assertEquals('Coding error detected, it must be fixed by a programmer: Not enough parameters ' .
 139                  'to initialise field_controller - unknown category', $e->getMessage());
 140              $this->assertEquals(\coding_exception::class, get_class($e));
 141          }
 142  
 143          // Mismatching category id.
 144          try {
 145              field_controller::create(0, (object)['type' => 'text', 'categoryid' => $category->get('id') + 1], $category);
 146              $this->fail('Expected exception');
 147          } catch (\coding_exception $e) {
 148              $this->assertEquals('Coding error detected, it must be fixed by a programmer: Category of the field ' .
 149                  'does not match category from the parameter', $e->getMessage());
 150              $this->assertEquals(\coding_exception::class, get_class($e));
 151          }
 152  
 153          // Non-existing type.
 154          try {
 155              field_controller::create(0, (object)['type' => 'nonexisting'], $category);
 156              $this->fail('Expected exception');
 157          } catch (\moodle_exception $e) {
 158              $this->assertEquals('Field type nonexisting not found', $e->getMessage());
 159              $this->assertEquals(\moodle_exception::class, get_class($e));
 160          }
 161      }
 162  
 163      /**
 164       * Tests for behaviour of:
 165       * \core_customfield\field_controller::save()
 166       * \core_customfield\field_controller::get()
 167       * \core_customfield\field_controller::get_category()
 168       */
 169      public function test_create_field() {
 170          global $DB;
 171          $this->resetAfterTest();
 172  
 173          $lpg = $this->get_generator();
 174          $category = $lpg->create_category();
 175          $fields = $DB->get_records(\core_customfield\field::TABLE, ['categoryid' => $category->get('id')]);
 176          $this->assertCount(0, $fields);
 177  
 178          // Create field.
 179          $fielddata = new \stdClass();
 180          $fielddata->name = 'Field';
 181          $fielddata->shortname = 'field';
 182          $fielddata->type = 'text';
 183          $fielddata->categoryid = $category->get('id');
 184          $field = field_controller::create(0, $fielddata);
 185          $field->save();
 186  
 187          $fields = $DB->get_records(\core_customfield\field::TABLE, ['categoryid' => $category->get('id')]);
 188          $this->assertCount(1, $fields);
 189          $this->assertTrue(\core_customfield\field::record_exists($field->get('id')));
 190          $this->assertInstanceOf(\customfield_text\field_controller::class, $field);
 191          $this->assertSame($field->get('name'), $fielddata->name);
 192          $this->assertSame($field->get('type'), $fielddata->type);
 193          $this->assertEquals($field->get_category()->get('id'), $category->get('id'));
 194      }
 195  
 196      /**
 197       * Tests for \core_customfield\field_controller::delete() behaviour.
 198       */
 199      public function test_delete_field() {
 200          global $DB;
 201          $this->resetAfterTest();
 202  
 203          $lpg = $this->get_generator();
 204          $category = $lpg->create_category();
 205          $fields = $DB->get_records(\core_customfield\field::TABLE, ['categoryid' => $category->get('id')]);
 206          $this->assertCount(0, $fields);
 207  
 208          // Create field using generator.
 209          $field1 = $lpg->create_field(array('categoryid' => $category->get('id')));
 210          $field2 = $lpg->create_field(array('categoryid' => $category->get('id')));
 211          $fields = $DB->get_records(\core_customfield\field::TABLE, ['categoryid' => $category->get('id')]);
 212          $this->assertCount(2, $fields);
 213  
 214          // Delete fields.
 215          $this->assertTrue($field1->delete());
 216          $this->assertTrue($field2->delete());
 217  
 218          // Check that the fields have been deleted.
 219          $fields = $DB->get_records(\core_customfield\field::TABLE, ['categoryid' => $category->get('id')]);
 220          $this->assertCount(0, $fields);
 221          $this->assertFalse(\core_customfield\field::record_exists($field1->get('id')));
 222          $this->assertFalse(\core_customfield\field::record_exists($field2->get('id')));
 223      }
 224  
 225      /**
 226       * Tests for \core_customfield\field_controller::get_configdata_property() behaviour.
 227       */
 228      public function test_get_configdata_property() {
 229          $this->resetAfterTest();
 230  
 231          $lpg = $this->get_generator();
 232          $category = $lpg->create_category();
 233          $configdata = ['a' => 'b', 'c' => ['d', 'e']];
 234          $field = field_controller::create(0, (object)['type' => 'text',
 235              'configdata' => json_encode($configdata), 'shortname' => 'a', 'name' => 'a'], $category);
 236          $field->save();
 237  
 238          // Retrieve field and check configdata.
 239          $field = field_controller::create($field->get('id'));
 240          $this->assertEquals($configdata, $field->get('configdata'));
 241          $this->assertEquals('b', $field->get_configdata_property('a'));
 242          $this->assertEquals(['d', 'e'], $field->get_configdata_property('c'));
 243          $this->assertEquals(null, $field->get_configdata_property('x'));
 244      }
 245  }