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 data_controller.
  28   *
  29   * @package    core_customfield
  30   * @category   test
  31   * @copyright  2018 Toni Barbera <toni@moodle.com>
  32   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   */
  34  class data_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 data_controller::create()
  47       */
  48      public function test_constructor() {
  49          global $DB;
  50          $this->resetAfterTest();
  51  
  52          // Create a course, fields category and fields.
  53          $course = $this->getDataGenerator()->create_course();
  54          $category0 = $this->get_generator()->create_category(['name' => 'aaaa']);
  55  
  56          // Add fields to this category.
  57          $fielddata                = new \stdClass();
  58          $fielddata->categoryid    = $category0->get('id');
  59          $fielddata->configdata    = "{\"required\":\"0\",\"uniquevalues\":\"0\",\"locked\":\"0\",\"visibility\":\"0\",
  60                                      \"defaultvalue\":\"\",\"displaysize\":0,\"maxlength\":0,\"ispassword\":\"0\",
  61                                      \"link\":\"\",\"linktarget\":\"\"}";
  62  
  63          $fielddata->type = 'checkbox';
  64          $field0 = $this->get_generator()->create_field($fielddata);
  65          $fielddata->type = 'date';
  66          $field1 = $this->get_generator()->create_field($fielddata);
  67          $fielddata->type = 'select';
  68          $field2 = $this->get_generator()->create_field($fielddata);
  69          $fielddata->type = 'text';
  70          $field3 = $this->get_generator()->create_field($fielddata);
  71          $fielddata->type = 'textarea';
  72          $field4 = $this->get_generator()->create_field($fielddata);
  73  
  74          $params = ['instanceid' => $course->id, 'contextid' => \context_course::instance($course->id)->id];
  75  
  76          // Generate new data_controller records for these fields, specifying field controller or fieldid or both.
  77          $data0 = data_controller::create(0, (object)$params, $field0);
  78          $this->assertInstanceOf(customfield_checkbox\data_controller::class, $data0);
  79          $data1 = data_controller::create(0,
  80              (object)($params + ['fieldid' => $field1->get('id')]), $field1);
  81          $this->assertInstanceOf(customfield_date\data_controller::class, $data1);
  82          $data2 = data_controller::create(0,
  83              (object)($params + ['fieldid' => $field2->get('id')]));
  84          $this->assertInstanceOf(customfield_select\data_controller::class, $data2);
  85          $data3 = data_controller::create(0, (object)$params, $field3);
  86          $this->assertInstanceOf(customfield_text\data_controller::class, $data3);
  87          $data4 = data_controller::create(0, (object)$params, $field4);
  88          $this->assertInstanceOf(customfield_textarea\data_controller::class, $data4);
  89  
  90          // Save data so we can have ids.
  91          $data0->save();
  92          $data1->save();
  93          $data2->save();
  94          $data3->save();
  95          $data4->save();
  96  
  97          // Retrieve data by id.
  98          $this->assertInstanceOf(customfield_checkbox\data_controller::class, data_controller::create($data0->get('id')));
  99          $this->assertInstanceOf(customfield_date\data_controller::class, data_controller::create($data1->get('id')));
 100  
 101          // Retrieve data by id and field.
 102          $this->assertInstanceOf(customfield_select\data_controller::class,
 103              data_controller::create($data2->get('id'), null, $field2));
 104  
 105          // Retrieve data by record without field.
 106          $datarecord = $DB->get_record(\core_customfield\data::TABLE, ['id' => $data3->get('id')], '*', MUST_EXIST);
 107          $this->assertInstanceOf(customfield_text\data_controller::class, data_controller::create(0, $datarecord));
 108  
 109          // Retrieve data by record with field.
 110          $datarecord = $DB->get_record(\core_customfield\data::TABLE, ['id' => $data4->get('id')], '*', MUST_EXIST);
 111          $this->assertInstanceOf(customfield_textarea\data_controller::class, data_controller::create(0, $datarecord, $field4));
 112  
 113      }
 114  
 115      /**
 116       * Test for function \core_customfield\field_controller::create() in case of wrong parameters
 117       */
 118      public function test_constructor_errors() {
 119          global $DB;
 120          $this->resetAfterTest();
 121  
 122          // Create a category, field and data.
 123          $category = $this->get_generator()->create_category();
 124          $field = $this->get_generator()->create_field(['categoryid' => $category->get('id')]);
 125          $course = $this->getDataGenerator()->create_course();
 126          $data = data_controller::create(0, (object)['instanceid' => $course->id,
 127              'contextid' => \context_course::instance($course->id)->id], $field);
 128          $data->save();
 129  
 130          $datarecord = $DB->get_record(\core_customfield\data::TABLE, ['id' => $data->get('id')], '*', MUST_EXIST);
 131  
 132          // Both id and record give warning.
 133          $d = data_controller::create($datarecord->id, $datarecord);
 134          $debugging = $this->getDebuggingMessages();
 135          $this->assertEquals(1, count($debugging));
 136          $this->assertEquals('Too many parameters, either id need to be specified or a record, but not both.',
 137              $debugging[0]->message);
 138          $this->resetDebugging();
 139          $this->assertInstanceOf(customfield_text\data_controller::class, $d);
 140  
 141          // Retrieve non-existing data.
 142          try {
 143              data_controller::create($datarecord->id + 1);
 144              $this->fail('Expected exception');
 145          } catch (\dml_missing_record_exception $e) {
 146              $this->assertStringMatchesFormat('Can\'t find data record in database table customfield_data%a', $e->getMessage());
 147              $this->assertEquals(\dml_missing_record_exception::class, get_class($e));
 148          }
 149  
 150          // Missing field id.
 151          try {
 152              data_controller::create(0, (object)['instanceid' => $course->id]);
 153              $this->fail('Expected exception');
 154          } catch (\coding_exception $e) {
 155              $this->assertEquals('Coding error detected, it must be fixed by a programmer: Not enough parameters to ' .
 156                  'initialise data_controller - unknown field', $e->getMessage());
 157              $this->assertEquals(\coding_exception::class, get_class($e));
 158          }
 159  
 160          // Mismatching field id.
 161          try {
 162              data_controller::create(0, (object)['instanceid' => $course->id, 'fieldid' => $field->get('id') + 1], $field);
 163              $this->fail('Expected exception');
 164          } catch (\coding_exception $e) {
 165              $this->assertEquals('Coding error detected, it must be fixed by a programmer: Field id from the record ' .
 166                  'does not match field from the parameter', $e->getMessage());
 167              $this->assertEquals(\coding_exception::class, get_class($e));
 168          }
 169  
 170          // Nonexisting class.
 171          try {
 172              $field->set('type', 'invalid');
 173              data_controller::create(0, (object)['instanceid' => $course->id], $field);
 174              $this->fail('Expected exception');
 175          } catch (\moodle_exception $e) {
 176              $this->assertEquals('Field type invalid not found', $e->getMessage());
 177              $this->assertEquals(\moodle_exception::class, get_class($e));
 178          }
 179      }
 180  }