MDL-57898 core_customfield: Custom fields API
[moodle.git] / customfield / field / date / tests / plugin_test.php
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/>.
17 /**
18  * Tests for class customfield_date
19  *
20  * @package    customfield_date
21  * @copyright  2019 Marina Glancy
22  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23  */
25 defined('MOODLE_INTERNAL') || die();
27 use customfield_date\field_controller;
28 use customfield_date\data_controller;
30 /**
31  * Functional test for customfield_date
32  *
33  * @package    customfield_date
34  * @copyright  2019 Marina Glancy
35  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36  */
37 class customfield_date_plugin_testcase extends advanced_testcase {
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;
48     /**
49      * Tests set up.
50      */
51     public function setUp() {
52         $this->resetAfterTest();
54         $this->cfcat = $this->get_generator()->create_category();
56         $this->cfields[1] = $this->get_generator()->create_field(
57             ['categoryid' => $this->cfcat->get('id'), 'shortname' => 'myfield1', 'type' => 'date']);
58         $this->cfields[2] = $this->get_generator()->create_field(
59             ['categoryid' => $this->cfcat->get('id'), 'shortname' => 'myfield2', 'type' => 'date',
60                 'configdata' => ['required' => 1, 'includetime' => 0, 'mindate' => 946684800, 'maxdate' => 1893456000]]);
62         $this->courses[1] = $this->getDataGenerator()->create_course();
63         $this->courses[2] = $this->getDataGenerator()->create_course();
64         $this->courses[3] = $this->getDataGenerator()->create_course();
66         $this->cfdata[1] = $this->get_generator()->add_instance_data($this->cfields[1], $this->courses[1]->id, 1546300800);
67         $this->cfdata[2] = $this->get_generator()->add_instance_data($this->cfields[1], $this->courses[2]->id, 1546300800);
69         $this->setUser($this->getDataGenerator()->create_user());
70     }
72     /**
73      * Get generator
74      * @return core_customfield_generator
75      */
76     protected function get_generator(): core_customfield_generator {
77         return $this->getDataGenerator()->get_plugin_generator('core_customfield');
78     }
80     /**
81      * Test for initialising field and data controllers
82      */
83     public function test_initialise() {
84         $f = \core_customfield\field_controller::create($this->cfields[1]->get('id'));
85         $this->assertTrue($f instanceof field_controller);
87         $f = \core_customfield\field_controller::create(0, (object)['type' => 'date'], $this->cfcat);
88         $this->assertTrue($f instanceof field_controller);
90         $d = \core_customfield\data_controller::create($this->cfdata[1]->get('id'));
91         $this->assertTrue($d instanceof data_controller);
93         $d = \core_customfield\data_controller::create(0, null, $this->cfields[1]);
94         $this->assertTrue($d instanceof data_controller);
95     }
97     /**
98      * Test for configuration form functions
99      *
100      * Create a configuration form and submit it with the same values as in the field
101      */
102     public function test_config_form() {
103         $submitdata = (array)$this->cfields[1]->to_record();
104         $submitdata['configdata'] = $this->cfields[1]->get('configdata');
106         \core_customfield\field_config_form::mock_submit($submitdata, []);
107         $handler = $this->cfcat->get_handler();
108         $form = $handler->get_field_config_form($this->cfields[1]);
109         $this->assertTrue($form->is_validated());
110         $data = $form->get_data();
111         $handler->save_field_configuration($this->cfields[1], $data);
112     }
114     /**
115      * Test for instance form functions
116      */
117     public function test_instance_form() {
118         global $CFG;
119         require_once($CFG->dirroot . '/customfield/tests/fixtures/test_instance_form.php');
120         $this->setAdminUser();
121         $handler = $this->cfcat->get_handler();
123         // First try to submit without required field.
124         $submitdata = (array)$this->courses[1];
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->assertFalse($form->is_validated());
130         // Now with required field.
131         $submitdata['customfield_myfield2'] = time();
132         core_customfield_test_instance_form::mock_submit($submitdata, []);
133         $form = new core_customfield_test_instance_form('POST',
134             ['handler' => $handler, 'instance' => $this->courses[1]]);
135         $this->assertTrue($form->is_validated());
137         $data = $form->get_data();
138         $this->assertEmpty($data->customfield_myfield1);
139         $this->assertNotEmpty($data->customfield_myfield2);
140         $handler->instance_form_save($data);
141     }
143     /**
144      * Test for min/max date validation
145      */
146     public function test_instance_form_validation() {
147         $this->setAdminUser();
148         $handler = $this->cfcat->get_handler();
149         $submitdata = (array)$this->courses[1];
150         $data = data_controller::create(0, null, $this->cfields[2]);
152         // Submit with date less than mindate.
153         $submitdata['customfield_myfield2'] = 915148800;
154         $this->assertNotEmpty($data->instance_form_validation($submitdata, []));
156         // Submit with date more than maxdate.
157         $submitdata['customfield_myfield2'] = 1893557000;
158         $this->assertNotEmpty($data->instance_form_validation($submitdata, []));
159     }
161     /**
162      * Test for data_controller::get_value and export_value
163      */
164     public function test_get_export_value() {
165         $this->assertEquals(1546300800, $this->cfdata[1]->get_value());
166         $this->assertStringMatchesFormat('%a 1 January 2019%a', $this->cfdata[1]->export_value());
168         // Field without data.
169         $d = core_customfield\data_controller::create(0, null, $this->cfields[2]);
170         $this->assertEquals(0, $d->get_value());
171         $this->assertEquals(null, $d->export_value());
172     }
174     /**
175      * Deleting fields and data
176      */
177     public function test_delete() {
178         $this->cfcat->get_handler()->delete_all();
179     }