Proyectos de Subversion Moodle

Rev

Rev 11 | | Comparar con el anterior | Ultima modificación | Ver Log |

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