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 customfield_textarea;
  18  
  19  use core_customfield_generator;
  20  use core_customfield_test_instance_form;
  21  
  22  /**
  23   * Functional test for customfield_textarea
  24   *
  25   * @package    customfield_textarea
  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' => 'textarea']);
  50          $this->cfields[2] = $this->get_generator()->create_field(
  51              ['categoryid' => $this->cfcat->get('id'), 'shortname' => 'myfield2', 'type' => 'textarea',
  52                  'configdata' => ['required' => 1]]);
  53          $this->cfields[3] = $this->get_generator()->create_field(
  54              ['categoryid' => $this->cfcat->get('id'), 'shortname' => 'myfield3', 'type' => 'textarea',
  55                  'configdata' => ['defaultvalue' => 'Value3', 'defaultvalueformat' => FORMAT_MOODLE]]);
  56  
  57          $this->courses[1] = $this->getDataGenerator()->create_course();
  58          $this->courses[2] = $this->getDataGenerator()->create_course();
  59          $this->courses[3] = $this->getDataGenerator()->create_course();
  60  
  61          $this->cfdata[1] = $this->get_generator()->add_instance_data($this->cfields[1], $this->courses[1]->id,
  62              ['text' => 'Value1', 'format' => FORMAT_MOODLE]);
  63          $this->cfdata[2] = $this->get_generator()->add_instance_data($this->cfields[1], $this->courses[2]->id,
  64              ['text' => 'Value2', 'format' => FORMAT_MOODLE]);
  65  
  66          $this->setUser($this->getDataGenerator()->create_user());
  67      }
  68  
  69      /**
  70       * Get generator
  71       * @return core_customfield_generator
  72       */
  73      protected function get_generator() : core_customfield_generator {
  74          return $this->getDataGenerator()->get_plugin_generator('core_customfield');
  75      }
  76  
  77      /**
  78       * Test for initialising field and data controllers
  79       */
  80      public function test_initialise() {
  81          $f = \core_customfield\field_controller::create($this->cfields[1]->get('id'));
  82          $this->assertTrue($f instanceof field_controller);
  83  
  84          $f = \core_customfield\field_controller::create(0, (object)['type' => 'textarea'], $this->cfcat);
  85          $this->assertTrue($f instanceof field_controller);
  86  
  87          $d = \core_customfield\data_controller::create($this->cfdata[1]->get('id'));
  88          $this->assertTrue($d instanceof data_controller);
  89  
  90          $d = \core_customfield\data_controller::create(0, null, $this->cfields[1]);
  91          $this->assertTrue($d instanceof data_controller);
  92      }
  93  
  94      /**
  95       * Test for configuration form functions
  96       *
  97       * Create a configuration form and submit it with the same values as in the field
  98       */
  99      public function test_config_form() {
 100          $this->setAdminUser();
 101          $submitdata = (array)$this->cfields[3]->to_record();
 102          $submitdata['configdata'] = $this->cfields[3]->get('configdata');
 103  
 104          $submitdata = \core_customfield\field_config_form::mock_ajax_submit($submitdata);
 105          $form = new \core_customfield\field_config_form(null, null, 'post', '', null, true,
 106              $submitdata, true);
 107          $form->set_data_for_dynamic_submission();
 108          $this->assertTrue($form->is_validated());
 109          $form->process_dynamic_submission();
 110      }
 111  
 112      /**
 113       * Test for instance form functions
 114       */
 115      public function test_instance_form() {
 116          global $CFG;
 117          require_once($CFG->dirroot . '/customfield/tests/fixtures/test_instance_form.php');
 118          $this->setAdminUser();
 119          $handler = $this->cfcat->get_handler();
 120  
 121          // First try to submit without required field.
 122          $submitdata = (array)$this->courses[1];
 123          core_customfield_test_instance_form::mock_submit($submitdata, []);
 124          $form = new core_customfield_test_instance_form('POST',
 125              ['handler' => $handler, 'instance' => $this->courses[1]]);
 126          $this->assertFalse($form->is_validated());
 127  
 128          // Now with required field.
 129          $submitdata['customfield_myfield2_editor'] = ['text' => 'Some text', 'format' => FORMAT_HTML];
 130          core_customfield_test_instance_form::mock_submit($submitdata, []);
 131          $form = new core_customfield_test_instance_form('POST',
 132              ['handler' => $handler, 'instance' => $this->courses[1]]);
 133          $this->assertTrue($form->is_validated());
 134  
 135          $data = $form->get_data();
 136          $this->assertNotEmpty($data->customfield_myfield1_editor);
 137          $this->assertNotEmpty($data->customfield_myfield2_editor);
 138          $handler->instance_form_save($data);
 139      }
 140  
 141      /**
 142       * Test that instance form save empties the field content for blank values
 143       */
 144      public function test_instance_form_save_clear(): void {
 145          global $CFG;
 146  
 147          require_once("{$CFG->dirroot}/customfield/tests/fixtures/test_instance_form.php");
 148  
 149          $this->setAdminUser();
 150  
 151          $handler = $this->cfcat->get_handler();
 152  
 153          // Set our custom field to a known value.
 154          $submitdata = (array) $this->courses[1] + [
 155              'customfield_myfield1_editor' => ['text' => 'I can see it in your eyes', 'format' => FORMAT_HTML],
 156              'customfield_myfield2_editor' => ['text' => 'I can see it in your smile', 'format' => FORMAT_HTML],
 157          ];
 158  
 159          core_customfield_test_instance_form::mock_submit($submitdata, []);
 160          $form = new core_customfield_test_instance_form('post', ['handler' => $handler, 'instance' => $this->courses[1]]);
 161          $handler->instance_form_save($form->get_data());
 162  
 163          $this->assertEquals($submitdata['customfield_myfield1_editor']['text'],
 164              \core_customfield\data_controller::create($this->cfdata[1]->get('id'))->export_value());
 165  
 166          // Now empty our non-required field.
 167          $submitdata['customfield_myfield1_editor']['text'] = '';
 168  
 169          core_customfield_test_instance_form::mock_submit($submitdata, []);
 170          $form = new core_customfield_test_instance_form('post', ['handler' => $handler, 'instance' => $this->courses[1]]);
 171          $handler->instance_form_save($form->get_data());
 172  
 173          $this->assertEmpty(\core_customfield\data_controller::create($this->cfdata[1]->get('id'))->export_value());
 174      }
 175  
 176      /**
 177       * Test for data_controller::get_value and export_value
 178       */
 179      public function test_get_export_value() {
 180          $this->assertEquals('Value1', $this->cfdata[1]->get_value());
 181          $this->assertEquals('<div class="text_to_html">Value1</div>', $this->cfdata[1]->export_value());
 182  
 183          // Field without data but with a default value.
 184          $d = \core_customfield\data_controller::create(0, null, $this->cfields[3]);
 185          $this->assertEquals('Value3', $d->get_value());
 186          $this->assertEquals('<div class="text_to_html">Value3</div>', $d->export_value());
 187      }
 188  
 189      /**
 190       * Deleting fields and data
 191       */
 192      public function test_delete() {
 193          $this->cfcat->get_handler()->delete_all();
 194      }
 195  }