Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 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 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403] [Versions 39 and 310]

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