1441 |
ariadna |
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 |
declare(strict_types=1);
|
|
|
18 |
|
|
|
19 |
namespace customfield_number;
|
|
|
20 |
|
|
|
21 |
use advanced_testcase;
|
|
|
22 |
use core_customfield_generator;
|
|
|
23 |
use core_customfield_test_instance_form;
|
|
|
24 |
|
|
|
25 |
/**
|
|
|
26 |
* Tests for the data controller
|
|
|
27 |
*
|
|
|
28 |
* @package customfield_number
|
|
|
29 |
* @covers \customfield_number\data_controller
|
|
|
30 |
* @copyright 2024 Paul Holden <paulh@moodle.com>
|
|
|
31 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
32 |
*/
|
|
|
33 |
final class data_controller_test extends advanced_testcase {
|
|
|
34 |
|
|
|
35 |
/**
|
|
|
36 |
* Test that using base field controller returns our number type
|
|
|
37 |
*/
|
|
|
38 |
public function test_create(): void {
|
|
|
39 |
$this->resetAfterTest();
|
|
|
40 |
$this->setAdminUser();
|
|
|
41 |
|
|
|
42 |
$course = $this->getDataGenerator()->create_course();
|
|
|
43 |
|
|
|
44 |
/** @var core_customfield_generator $generator */
|
|
|
45 |
$generator = $this->getDataGenerator()->get_plugin_generator('core_customfield');
|
|
|
46 |
|
|
|
47 |
$category = $generator->create_category();
|
|
|
48 |
$field = $generator->create_field(['categoryid' => $category->get('id'), 'type' => 'number']);
|
|
|
49 |
$data = $generator->add_instance_data($field, (int) $course->id, 1);
|
|
|
50 |
|
|
|
51 |
$this->assertInstanceOf(data_controller::class, \core_customfield\data_controller::create($data->get('id')));
|
|
|
52 |
$this->assertInstanceOf(data_controller::class, \core_customfield\data_controller::create(0, $data->to_record()));
|
|
|
53 |
$this->assertInstanceOf(data_controller::class, \core_customfield\data_controller::create(0, null, $field));
|
|
|
54 |
}
|
|
|
55 |
|
|
|
56 |
/**
|
|
|
57 |
* Test validation of field instance form
|
|
|
58 |
*/
|
|
|
59 |
public function test_form_validation(): void {
|
|
|
60 |
global $CFG;
|
|
|
61 |
|
|
|
62 |
require_once("{$CFG->dirroot}/customfield/tests/fixtures/test_instance_form.php");
|
|
|
63 |
|
|
|
64 |
$this->resetAfterTest();
|
|
|
65 |
$this->setAdminUser();
|
|
|
66 |
|
|
|
67 |
$course = $this->getDataGenerator()->create_course();
|
|
|
68 |
|
|
|
69 |
/** @var core_customfield_generator $generator */
|
|
|
70 |
$generator = $this->getDataGenerator()->get_plugin_generator('core_customfield');
|
|
|
71 |
|
|
|
72 |
$category = $generator->create_category();
|
|
|
73 |
$field = $generator->create_field(['categoryid' => $category->get('id'), 'type' => 'number', 'configdata' => [
|
|
|
74 |
'minimumvalue' => 5,
|
|
|
75 |
'maximumvalue' => 10,
|
|
|
76 |
]]);
|
|
|
77 |
|
|
|
78 |
$data = \core_customfield\data_controller::create(0, null, $field);
|
|
|
79 |
|
|
|
80 |
// Less than minimum value.
|
|
|
81 |
$formdata = array_merge((array) $course, ['customfield_' . $field->get('shortname') => 2]);
|
|
|
82 |
$this->assertEquals([
|
|
|
83 |
'customfield_' . $field->get('shortname') => 'Value must be greater than or equal to 5',
|
|
|
84 |
], $data->instance_form_validation($formdata, []));
|
|
|
85 |
|
|
|
86 |
// Greater than maximum value.
|
|
|
87 |
$formdata = array_merge((array) $course, ['customfield_' . $field->get('shortname') => 12]);
|
|
|
88 |
$this->assertEquals([
|
|
|
89 |
'customfield_' . $field->get('shortname') => 'Value must be less than or equal to 10',
|
|
|
90 |
], $data->instance_form_validation($formdata, []));
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
/**
|
|
|
94 |
* Test submitting field instance form
|
|
|
95 |
*/
|
|
|
96 |
public function test_form_save(): void {
|
|
|
97 |
global $CFG;
|
|
|
98 |
|
|
|
99 |
require_once("{$CFG->dirroot}/customfield/tests/fixtures/test_instance_form.php");
|
|
|
100 |
|
|
|
101 |
$this->resetAfterTest();
|
|
|
102 |
$this->setAdminUser();
|
|
|
103 |
|
|
|
104 |
$course = $this->getDataGenerator()->create_course();
|
|
|
105 |
|
|
|
106 |
/** @var core_customfield_generator $generator */
|
|
|
107 |
$generator = $this->getDataGenerator()->get_plugin_generator('core_customfield');
|
|
|
108 |
|
|
|
109 |
$category = $generator->create_category();
|
|
|
110 |
$field = $generator->create_field(['categoryid' => $category->get('id'), 'type' => 'number']);
|
|
|
111 |
|
|
|
112 |
$formdata = array_merge((array) $course, ['customfield_' . $field->get('shortname') => 42]);
|
|
|
113 |
core_customfield_test_instance_form::mock_submit($formdata);
|
|
|
114 |
|
|
|
115 |
$form = new core_customfield_test_instance_form('POST', ['handler' => $category->get_handler(), 'instance' => $course]);
|
|
|
116 |
$this->assertTrue($form->is_validated());
|
|
|
117 |
|
|
|
118 |
$formsubmission = $form->get_data();
|
|
|
119 |
$this->assertEquals(42.0, $formsubmission->{'customfield_' . $field->get('shortname')});
|
|
|
120 |
$category->get_handler()->instance_form_save($formsubmission);
|
|
|
121 |
}
|
|
|
122 |
|
|
|
123 |
/**
|
|
|
124 |
* Data provider for {@see test_export_value}
|
|
|
125 |
*
|
|
|
126 |
* @return array[]
|
|
|
127 |
*/
|
|
|
128 |
public static function export_value_provider(): array {
|
|
|
129 |
$template = '<span class="multilang" lang="en">$ {value}</span><span class="multilang" lang="es">€ {value}</span>';
|
|
|
130 |
$whenzero = '<span class="multilang" lang="en">Unknown</span><span class="multilang" lang="es">Desconocido</span>';
|
|
|
131 |
return [
|
|
|
132 |
'Export float value' => [42, '42.00', [
|
|
|
133 |
'decimalplaces' => 2,
|
|
|
134 |
'display' => '{value}',
|
|
|
135 |
'displaywhenzero' => 0,
|
|
|
136 |
]],
|
|
|
137 |
'Export value with a prefix' => [10, '$ 10.00', [
|
|
|
138 |
'decimalplaces' => 2,
|
|
|
139 |
'display' => $template,
|
|
|
140 |
'displaywhenzero' => 0,
|
|
|
141 |
]],
|
|
|
142 |
'Export value when zero' => [0, 'Unknown', [
|
|
|
143 |
'display' => '{value}',
|
|
|
144 |
'displaywhenzero' => $whenzero,
|
|
|
145 |
]],
|
|
|
146 |
'Export value when not set' => ['', null, [
|
|
|
147 |
'display' => '{value}',
|
|
|
148 |
'displaywhenzero' => $whenzero,
|
|
|
149 |
]],
|
|
|
150 |
'Export almost zero that rounds to non-zero' => [0.0009, '0.001', [
|
|
|
151 |
'decimalplaces' => 3,
|
|
|
152 |
'display' => '{value}',
|
|
|
153 |
'displaywhenzero' => 'Free',
|
|
|
154 |
]],
|
|
|
155 |
'Export almost zero that rounds to zero' => [0.0004, 'Free', [
|
|
|
156 |
'decimalplaces' => 3,
|
|
|
157 |
'display' => '{value}',
|
|
|
158 |
'displaywhenzero' => 'Free',
|
|
|
159 |
]],
|
|
|
160 |
'Export when config not set' => [42, '42', []],
|
|
|
161 |
'Export zero when config not set' => [0, null, []],
|
|
|
162 |
];
|
|
|
163 |
}
|
|
|
164 |
|
|
|
165 |
/**
|
|
|
166 |
* Test exporting instance
|
|
|
167 |
*
|
|
|
168 |
* @param float|string $datavalue
|
|
|
169 |
* @param string|null $expectedvalue
|
|
|
170 |
* @param array $configdata
|
|
|
171 |
*
|
|
|
172 |
* @dataProvider export_value_provider
|
|
|
173 |
*/
|
|
|
174 |
public function test_export_value(
|
|
|
175 |
float|string $datavalue,
|
|
|
176 |
string|null $expectedvalue,
|
|
|
177 |
array $configdata,
|
|
|
178 |
): void {
|
|
|
179 |
$this->resetAfterTest();
|
|
|
180 |
$this->setAdminUser();
|
|
|
181 |
|
|
|
182 |
// Enable multilang filter.
|
|
|
183 |
filter_set_global_state('multilang', TEXTFILTER_ON);
|
|
|
184 |
filter_set_applies_to_strings('multilang', true);
|
|
|
185 |
|
|
|
186 |
$course = $this->getDataGenerator()->create_course();
|
|
|
187 |
|
|
|
188 |
/** @var core_customfield_generator $generator */
|
|
|
189 |
$generator = $this->getDataGenerator()->get_plugin_generator('core_customfield');
|
|
|
190 |
|
|
|
191 |
$category = $generator->create_category();
|
|
|
192 |
$field = $generator->create_field([
|
|
|
193 |
'categoryid' => $category->get('id'),
|
|
|
194 |
'type' => 'number',
|
|
|
195 |
'configdata' => $configdata,
|
|
|
196 |
]);
|
|
|
197 |
$data = $generator->add_instance_data($field, (int) $course->id, $datavalue);
|
|
|
198 |
|
|
|
199 |
$result = \core_customfield\data_controller::create($data->get('id'))->export_value();
|
|
|
200 |
$this->assertSame($expectedvalue, $result);
|
|
|
201 |
}
|
|
|
202 |
}
|