Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 and 403]

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