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_date;
  18  
  19  use core_customfield_generator;
  20  use core_customfield_test_instance_form;
  21  
  22  /**
  23   * Functional test for customfield_date
  24   *
  25   * @package    customfield_date
  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' => 'date']);
  50          $this->cfields[2] = $this->get_generator()->create_field(
  51              ['categoryid' => $this->cfcat->get('id'), 'shortname' => 'myfield2', 'type' => 'date',
  52                  'configdata' => ['required' => 1, 'includetime' => 0, 'mindate' => 946684800, 'maxdate' => 1893456000]]);
  53  
  54          $this->courses[1] = $this->getDataGenerator()->create_course();
  55          $this->courses[2] = $this->getDataGenerator()->create_course();
  56          $this->courses[3] = $this->getDataGenerator()->create_course();
  57  
  58          $this->cfdata[1] = $this->get_generator()->add_instance_data($this->cfields[1], $this->courses[1]->id, 1546300800);
  59          $this->cfdata[2] = $this->get_generator()->add_instance_data($this->cfields[1], $this->courses[2]->id, 1546300800);
  60  
  61          $this->setUser($this->getDataGenerator()->create_user());
  62      }
  63  
  64      /**
  65       * Get generator
  66       * @return core_customfield_generator
  67       */
  68      protected function get_generator() : core_customfield_generator {
  69          return $this->getDataGenerator()->get_plugin_generator('core_customfield');
  70      }
  71  
  72      /**
  73       * Test for initialising field and data controllers
  74       */
  75      public function test_initialise() {
  76          $f = \core_customfield\field_controller::create($this->cfields[1]->get('id'));
  77          $this->assertTrue($f instanceof field_controller);
  78  
  79          $f = \core_customfield\field_controller::create(0, (object)['type' => 'date'], $this->cfcat);
  80          $this->assertTrue($f instanceof field_controller);
  81  
  82          $d = \core_customfield\data_controller::create($this->cfdata[1]->get('id'));
  83          $this->assertTrue($d instanceof data_controller);
  84  
  85          $d = \core_customfield\data_controller::create(0, null, $this->cfields[1]);
  86          $this->assertTrue($d instanceof data_controller);
  87      }
  88  
  89      /**
  90       * Test for configuration form functions
  91       *
  92       * Create a configuration form and submit it with the same values as in the field
  93       */
  94      public function test_config_form() {
  95          $this->setAdminUser();
  96          $submitdata = (array)$this->cfields[1]->to_record();
  97          $submitdata['configdata'] = $this->cfields[1]->get('configdata');
  98  
  99          $submitdata = \core_customfield\field_config_form::mock_ajax_submit($submitdata);
 100          $form = new \core_customfield\field_config_form(null, null, 'post', '', null, true,
 101              $submitdata, true);
 102          $form->set_data_for_dynamic_submission();
 103          $this->assertTrue($form->is_validated());
 104          $form->process_dynamic_submission();
 105      }
 106  
 107      /**
 108       * Test for instance form functions
 109       */
 110      public function test_instance_form() {
 111          global $CFG;
 112          require_once($CFG->dirroot . '/customfield/tests/fixtures/test_instance_form.php');
 113          $this->setAdminUser();
 114          $handler = $this->cfcat->get_handler();
 115  
 116          // First try to submit without required field.
 117          $submitdata = (array)$this->courses[1];
 118          core_customfield_test_instance_form::mock_submit($submitdata, []);
 119          $form = new core_customfield_test_instance_form('POST',
 120              ['handler' => $handler, 'instance' => $this->courses[1]]);
 121          $this->assertFalse($form->is_validated());
 122  
 123          // Now with required field.
 124          $submitdata['customfield_myfield2'] = time();
 125          core_customfield_test_instance_form::mock_submit($submitdata, []);
 126          $form = new core_customfield_test_instance_form('POST',
 127              ['handler' => $handler, 'instance' => $this->courses[1]]);
 128          $this->assertTrue($form->is_validated());
 129  
 130          $data = $form->get_data();
 131          $this->assertEmpty($data->customfield_myfield1);
 132          $this->assertNotEmpty($data->customfield_myfield2);
 133          $handler->instance_form_save($data);
 134      }
 135  
 136      /**
 137       * Test for min/max date validation
 138       */
 139      public function test_instance_form_validation() {
 140          $this->setAdminUser();
 141          $handler = $this->cfcat->get_handler();
 142          $submitdata = (array)$this->courses[1];
 143          $data = data_controller::create(0, null, $this->cfields[2]);
 144  
 145          // Submit with date less than mindate.
 146          $submitdata['customfield_myfield2'] = 915148800;
 147          $this->assertNotEmpty($data->instance_form_validation($submitdata, []));
 148  
 149          // Submit with date more than maxdate.
 150          $submitdata['customfield_myfield2'] = 1893557000;
 151          $this->assertNotEmpty($data->instance_form_validation($submitdata, []));
 152      }
 153  
 154      /**
 155       * Test for data_controller::get_value and export_value
 156       */
 157      public function test_get_export_value() {
 158          $this->assertEquals(1546300800, $this->cfdata[1]->get_value());
 159          $this->assertStringMatchesFormat('%a 1 January 2019%a', $this->cfdata[1]->export_value());
 160  
 161          // Field without data.
 162          $d = \core_customfield\data_controller::create(0, null, $this->cfields[2]);
 163          $this->assertEquals(0, $d->get_value());
 164          $this->assertEquals(null, $d->export_value());
 165      }
 166  
 167      /**
 168       * Data provider for {@see test_parse_value}
 169       *
 170       * @return array
 171       */
 172      public function parse_value_provider() : array {
 173          return [
 174              // Valid times.
 175              ['2019-10-01', strtotime('2019-10-01')],
 176              ['2019-10-01 14:00', strtotime('2019-10-01 14:00')],
 177              // Invalid times.
 178              ['ZZZZZ', 0],
 179              ['202-04-01', 0],
 180              ['2019-15-15', 0],
 181          ];
 182      }
 183      /**
 184       * Test field parse_value method
 185       *
 186       * @param string $value
 187       * @param int $expected
 188       * @return void
 189       *
 190       * @dataProvider parse_value_provider
 191       */
 192      public function test_parse_value(string $value, int $expected) {
 193          $this->assertSame($expected, $this->cfields[1]->parse_value($value));
 194      }
 195  
 196      /**
 197       * Deleting fields and data
 198       */
 199      public function test_delete() {
 200          $this->cfcat->get_handler()->delete_all();
 201      }
 202  }