Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

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

   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 customfield_select;
  18  
  19  use core_customfield_generator;
  20  use core_customfield_test_instance_form;
  21  
  22  /**
  23   * Functional test for customfield_select
  24   *
  25   * @package    customfield_select
  26   * @copyright  2019 Marina Glancy
  27   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  28   */
  29  class plugin_test extends \advanced_testcase {
  30  
  31      /** @var stdClass[]  */
  32      private $courses = [];
  33      /** @var \core_customfield\category_controller */
  34      private $cfcat;
  35      /** @var \core_customfield\field_controller[] */
  36      private $cfields;
  37      /** @var \core_customfield\data_controller[] */
  38      private $cfdata;
  39  
  40      /**
  41       * Tests set up.
  42       */
  43      public function setUp(): void {
  44          $this->resetAfterTest();
  45  
  46          $this->cfcat = $this->get_generator()->create_category();
  47  
  48          $this->cfields[1] = $this->get_generator()->create_field(
  49              ['categoryid' => $this->cfcat->get('id'), 'shortname' => 'myfield1', 'type' => 'select',
  50                  'configdata' => ['options' => "a\nb\nc"]]);
  51          $this->cfields[2] = $this->get_generator()->create_field(
  52              ['categoryid' => $this->cfcat->get('id'), 'shortname' => 'myfield2', 'type' => 'select',
  53                  'configdata' => ['required' => 1, 'options' => "a\nb\nc"]]);
  54          $this->cfields[3] = $this->get_generator()->create_field(
  55              ['categoryid' => $this->cfcat->get('id'), 'shortname' => 'myfield3', 'type' => 'select',
  56                  'configdata' => ['defaultvalue' => 'b', 'options' => "a\nb\nc"]]);
  57  
  58          $this->courses[1] = $this->getDataGenerator()->create_course();
  59          $this->courses[2] = $this->getDataGenerator()->create_course();
  60          $this->courses[3] = $this->getDataGenerator()->create_course();
  61  
  62          $this->cfdata[1] = $this->get_generator()->add_instance_data($this->cfields[1], $this->courses[1]->id, 1);
  63          $this->cfdata[2] = $this->get_generator()->add_instance_data($this->cfields[1], $this->courses[2]->id, 1);
  64  
  65          $this->setUser($this->getDataGenerator()->create_user());
  66      }
  67  
  68      /**
  69       * Get generator
  70       * @return core_customfield_generator
  71       */
  72      protected function get_generator() : core_customfield_generator {
  73          return $this->getDataGenerator()->get_plugin_generator('core_customfield');
  74      }
  75  
  76      /**
  77       * Test for initialising field and data controllers
  78       */
  79      public function test_initialise() {
  80          $f = \core_customfield\field_controller::create($this->cfields[1]->get('id'));
  81          $this->assertTrue($f instanceof field_controller);
  82  
  83          $f = \core_customfield\field_controller::create(0, (object)['type' => 'select'], $this->cfcat);
  84          $this->assertTrue($f instanceof field_controller);
  85  
  86          $d = \core_customfield\data_controller::create($this->cfdata[1]->get('id'));
  87          $this->assertTrue($d instanceof data_controller);
  88  
  89          $d = \core_customfield\data_controller::create(0, null, $this->cfields[1]);
  90          $this->assertTrue($d instanceof data_controller);
  91      }
  92  
  93      /**
  94       * Test for configuration form functions
  95       *
  96       * Create a configuration form and submit it with the same values as in the field
  97       */
  98      public function test_config_form() {
  99          $this->setAdminUser();
 100          $submitdata = (array)$this->cfields[1]->to_record();
 101          $submitdata['configdata'] = $this->cfields[1]->get('configdata');
 102  
 103          $submitdata = \core_customfield\field_config_form::mock_ajax_submit($submitdata);
 104          $form = new \core_customfield\field_config_form(null, null, 'post', '', null, true,
 105              $submitdata, true);
 106          $form->set_data_for_dynamic_submission();
 107          $this->assertTrue($form->is_validated());
 108          $form->process_dynamic_submission();
 109      }
 110  
 111      /**
 112       * Test for instance form functions
 113       */
 114      public function test_instance_form() {
 115          global $CFG;
 116          require_once($CFG->dirroot . '/customfield/tests/fixtures/test_instance_form.php');
 117          $this->setAdminUser();
 118          $handler = $this->cfcat->get_handler();
 119  
 120          // First try to submit without required field.
 121          $submitdata = (array)$this->courses[1];
 122          core_customfield_test_instance_form::mock_submit($submitdata, []);
 123          $form = new core_customfield_test_instance_form('POST',
 124              ['handler' => $handler, 'instance' => $this->courses[1]]);
 125          $this->assertFalse($form->is_validated());
 126  
 127          // Now with required field.
 128          $submitdata['customfield_myfield2'] = 1;
 129          core_customfield_test_instance_form::mock_submit($submitdata, []);
 130          $form = new core_customfield_test_instance_form('POST',
 131              ['handler' => $handler, 'instance' => $this->courses[1]]);
 132          $this->assertTrue($form->is_validated());
 133  
 134          $data = $form->get_data();
 135          $this->assertNotEmpty($data->customfield_myfield1);
 136          $this->assertNotEmpty($data->customfield_myfield2);
 137          $handler->instance_form_save($data);
 138      }
 139  
 140      /**
 141       * Test for data_controller::get_value and export_value
 142       */
 143      public function test_get_export_value() {
 144          $this->assertEquals(1, $this->cfdata[1]->get_value());
 145          $this->assertEquals('a', $this->cfdata[1]->export_value());
 146  
 147          // Field without data but with a default value.
 148          $d = \core_customfield\data_controller::create(0, null, $this->cfields[3]);
 149          $this->assertEquals(2, $d->get_value());
 150          $this->assertEquals('b', $d->export_value());
 151      }
 152  
 153      /**
 154       * Data provider for {@see test_parse_value}
 155       *
 156       * @return array
 157       */
 158      public function parse_value_provider() : array {
 159          return [
 160              ['Red', 1],
 161              ['Blue', 2],
 162              ['Green', 3],
 163              ['Mauve', 0],
 164          ];
 165      }
 166  
 167      /**
 168       * Test field parse_value method
 169       *
 170       * @param string $value
 171       * @param int $expected
 172       * @return void
 173       *
 174       * @dataProvider parse_value_provider
 175       */
 176      public function test_parse_value(string $value, int $expected) {
 177          $field = $this->get_generator()->create_field([
 178              'categoryid' => $this->cfcat->get('id'),
 179              'type' => 'select',
 180              'shortname' => 'myselect',
 181              'configdata' => [
 182                  'options' => "Red\nBlue\nGreen",
 183              ],
 184          ]);
 185  
 186          $this->assertSame($expected, $field->parse_value($value));
 187      }
 188  
 189      /**
 190       * Deleting fields and data
 191       */
 192      public function test_delete() {
 193          $this->cfcat->get_handler()->delete_all();
 194      }
 195  }