Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\field\Tests\Number;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Component\Utility\Unicode;
|
Chris@0
|
6 use Drupal\field\Entity\FieldConfig;
|
Chris@0
|
7 use Drupal\node\Entity\Node;
|
Chris@0
|
8 use Drupal\simpletest\WebTestBase;
|
Chris@0
|
9 use Drupal\field\Entity\FieldStorageConfig;
|
Chris@0
|
10
|
Chris@0
|
11 /**
|
Chris@0
|
12 * Tests the creation of numeric fields.
|
Chris@0
|
13 *
|
Chris@0
|
14 * @group field
|
Chris@0
|
15 */
|
Chris@0
|
16 class NumberFieldTest extends WebTestBase {
|
Chris@0
|
17
|
Chris@0
|
18 /**
|
Chris@0
|
19 * Modules to enable.
|
Chris@0
|
20 *
|
Chris@0
|
21 * @var array
|
Chris@0
|
22 */
|
Chris@0
|
23 public static $modules = ['node', 'entity_test', 'field_ui'];
|
Chris@0
|
24
|
Chris@0
|
25 protected function setUp() {
|
Chris@0
|
26 parent::setUp();
|
Chris@0
|
27 $this->drupalLogin($this->drupalCreateUser([
|
Chris@0
|
28 'view test entity',
|
Chris@0
|
29 'administer entity_test content',
|
Chris@0
|
30 'administer content types',
|
Chris@0
|
31 'administer node fields',
|
Chris@0
|
32 'administer node display',
|
Chris@0
|
33 'bypass node access',
|
Chris@0
|
34 'administer entity_test fields',
|
Chris@0
|
35 ]));
|
Chris@0
|
36 }
|
Chris@0
|
37
|
Chris@0
|
38 /**
|
Chris@0
|
39 * Test decimal field.
|
Chris@0
|
40 */
|
Chris@0
|
41 public function testNumberDecimalField() {
|
Chris@0
|
42 // Create a field with settings to validate.
|
Chris@0
|
43 $field_name = Unicode::strtolower($this->randomMachineName());
|
Chris@0
|
44 FieldStorageConfig::create([
|
Chris@0
|
45 'field_name' => $field_name,
|
Chris@0
|
46 'entity_type' => 'entity_test',
|
Chris@0
|
47 'type' => 'decimal',
|
Chris@0
|
48 'settings' => ['precision' => 8, 'scale' => 4],
|
Chris@0
|
49 ])->save();
|
Chris@0
|
50 FieldConfig::create([
|
Chris@0
|
51 'field_name' => $field_name,
|
Chris@0
|
52 'entity_type' => 'entity_test',
|
Chris@0
|
53 'bundle' => 'entity_test',
|
Chris@0
|
54 ])->save();
|
Chris@0
|
55
|
Chris@0
|
56 entity_get_form_display('entity_test', 'entity_test', 'default')
|
Chris@0
|
57 ->setComponent($field_name, [
|
Chris@0
|
58 'type' => 'number',
|
Chris@0
|
59 'settings' => [
|
Chris@0
|
60 'placeholder' => '0.00'
|
Chris@0
|
61 ],
|
Chris@0
|
62 ])
|
Chris@0
|
63 ->save();
|
Chris@0
|
64 entity_get_display('entity_test', 'entity_test', 'default')
|
Chris@0
|
65 ->setComponent($field_name, [
|
Chris@0
|
66 'type' => 'number_decimal',
|
Chris@0
|
67 ])
|
Chris@0
|
68 ->save();
|
Chris@0
|
69
|
Chris@0
|
70 // Display creation form.
|
Chris@0
|
71 $this->drupalGet('entity_test/add');
|
Chris@0
|
72 $this->assertFieldByName("{$field_name}[0][value]", '', 'Widget is displayed');
|
Chris@0
|
73 $this->assertRaw('placeholder="0.00"');
|
Chris@0
|
74
|
Chris@0
|
75 // Submit a signed decimal value within the allowed precision and scale.
|
Chris@0
|
76 $value = '-1234.5678';
|
Chris@0
|
77 $edit = [
|
Chris@0
|
78 "{$field_name}[0][value]" => $value,
|
Chris@0
|
79 ];
|
Chris@0
|
80 $this->drupalPostForm(NULL, $edit, t('Save'));
|
Chris@0
|
81 preg_match('|entity_test/manage/(\d+)|', $this->url, $match);
|
Chris@0
|
82 $id = $match[1];
|
Chris@0
|
83 $this->assertText(t('entity_test @id has been created.', ['@id' => $id]), 'Entity was created');
|
Chris@0
|
84 $this->assertRaw($value, 'Value is displayed.');
|
Chris@0
|
85
|
Chris@0
|
86 // Try to create entries with more than one decimal separator; assert fail.
|
Chris@0
|
87 $wrong_entries = [
|
Chris@0
|
88 '3.14.159',
|
Chris@0
|
89 '0..45469',
|
Chris@0
|
90 '..4589',
|
Chris@0
|
91 '6.459.52',
|
Chris@0
|
92 '6.3..25',
|
Chris@0
|
93 ];
|
Chris@0
|
94
|
Chris@0
|
95 foreach ($wrong_entries as $wrong_entry) {
|
Chris@0
|
96 $this->drupalGet('entity_test/add');
|
Chris@0
|
97 $edit = [
|
Chris@0
|
98 "{$field_name}[0][value]" => $wrong_entry,
|
Chris@0
|
99 ];
|
Chris@0
|
100 $this->drupalPostForm(NULL, $edit, t('Save'));
|
Chris@0
|
101 $this->assertRaw(t('%name must be a number.', ['%name' => $field_name]), 'Correctly failed to save decimal value with more than one decimal point.');
|
Chris@0
|
102 }
|
Chris@0
|
103
|
Chris@0
|
104 // Try to create entries with minus sign not in the first position.
|
Chris@0
|
105 $wrong_entries = [
|
Chris@0
|
106 '3-3',
|
Chris@0
|
107 '4-',
|
Chris@0
|
108 '1.3-',
|
Chris@0
|
109 '1.2-4',
|
Chris@0
|
110 '-10-10',
|
Chris@0
|
111 ];
|
Chris@0
|
112
|
Chris@0
|
113 foreach ($wrong_entries as $wrong_entry) {
|
Chris@0
|
114 $this->drupalGet('entity_test/add');
|
Chris@0
|
115 $edit = [
|
Chris@0
|
116 "{$field_name}[0][value]" => $wrong_entry,
|
Chris@0
|
117 ];
|
Chris@0
|
118 $this->drupalPostForm(NULL, $edit, t('Save'));
|
Chris@0
|
119 $this->assertRaw(t('%name must be a number.', ['%name' => $field_name]), 'Correctly failed to save decimal value with minus sign in the wrong position.');
|
Chris@0
|
120 }
|
Chris@0
|
121 }
|
Chris@0
|
122
|
Chris@0
|
123 /**
|
Chris@0
|
124 * Test integer field.
|
Chris@0
|
125 */
|
Chris@0
|
126 public function testNumberIntegerField() {
|
Chris@0
|
127 $minimum = rand(-4000, -2000);
|
Chris@0
|
128 $maximum = rand(2000, 4000);
|
Chris@0
|
129
|
Chris@0
|
130 // Create a field with settings to validate.
|
Chris@0
|
131 $field_name = Unicode::strtolower($this->randomMachineName());
|
Chris@0
|
132 $storage = FieldStorageConfig::create([
|
Chris@0
|
133 'field_name' => $field_name,
|
Chris@0
|
134 'entity_type' => 'entity_test',
|
Chris@0
|
135 'type' => 'integer',
|
Chris@0
|
136 ]);
|
Chris@0
|
137 $storage->save();
|
Chris@0
|
138
|
Chris@0
|
139 FieldConfig::create([
|
Chris@0
|
140 'field_name' => $field_name,
|
Chris@0
|
141 'entity_type' => 'entity_test',
|
Chris@0
|
142 'bundle' => 'entity_test',
|
Chris@0
|
143 'settings' => [
|
Chris@0
|
144 'min' => $minimum,
|
Chris@0
|
145 'max' => $maximum,
|
Chris@0
|
146 'prefix' => 'ThePrefix',
|
Chris@0
|
147 ],
|
Chris@0
|
148 ])->save();
|
Chris@0
|
149
|
Chris@0
|
150 entity_get_form_display('entity_test', 'entity_test', 'default')
|
Chris@0
|
151 ->setComponent($field_name, [
|
Chris@0
|
152 'type' => 'number',
|
Chris@0
|
153 'settings' => [
|
Chris@0
|
154 'placeholder' => '4'
|
Chris@0
|
155 ],
|
Chris@0
|
156 ])
|
Chris@0
|
157 ->save();
|
Chris@0
|
158 entity_get_display('entity_test', 'entity_test', 'default')
|
Chris@0
|
159 ->setComponent($field_name, [
|
Chris@0
|
160 'type' => 'number_integer',
|
Chris@0
|
161 'settings' => [
|
Chris@0
|
162 'prefix_suffix' => FALSE,
|
Chris@0
|
163 ],
|
Chris@0
|
164 ])
|
Chris@0
|
165 ->save();
|
Chris@0
|
166
|
Chris@0
|
167 // Check the storage schema.
|
Chris@0
|
168 $expected = [
|
Chris@0
|
169 'columns' => [
|
Chris@0
|
170 'value' => [
|
Chris@0
|
171 'type' => 'int',
|
Chris@0
|
172 'unsigned' => '',
|
Chris@0
|
173 'size' => 'normal'
|
Chris@0
|
174 ],
|
Chris@0
|
175 ],
|
Chris@0
|
176 'unique keys' => [],
|
Chris@0
|
177 'indexes' => [],
|
Chris@0
|
178 'foreign keys' => []
|
Chris@0
|
179 ];
|
Chris@0
|
180 $this->assertEqual($storage->getSchema(), $expected);
|
Chris@0
|
181
|
Chris@0
|
182 // Display creation form.
|
Chris@0
|
183 $this->drupalGet('entity_test/add');
|
Chris@0
|
184 $this->assertFieldByName("{$field_name}[0][value]", '', 'Widget is displayed');
|
Chris@0
|
185 $this->assertRaw('placeholder="4"');
|
Chris@0
|
186
|
Chris@0
|
187 // Submit a valid integer
|
Chris@0
|
188 $value = rand($minimum, $maximum);
|
Chris@0
|
189 $edit = [
|
Chris@0
|
190 "{$field_name}[0][value]" => $value,
|
Chris@0
|
191 ];
|
Chris@0
|
192 $this->drupalPostForm(NULL, $edit, t('Save'));
|
Chris@0
|
193 preg_match('|entity_test/manage/(\d+)|', $this->url, $match);
|
Chris@0
|
194 $id = $match[1];
|
Chris@0
|
195 $this->assertText(t('entity_test @id has been created.', ['@id' => $id]), 'Entity was created');
|
Chris@0
|
196
|
Chris@0
|
197 // Try to set a value below the minimum value
|
Chris@0
|
198 $this->drupalGet('entity_test/add');
|
Chris@0
|
199 $edit = [
|
Chris@0
|
200 "{$field_name}[0][value]" => $minimum - 1,
|
Chris@0
|
201 ];
|
Chris@0
|
202 $this->drupalPostForm(NULL, $edit, t('Save'));
|
Chris@0
|
203 $this->assertRaw(t('%name must be higher than or equal to %minimum.', ['%name' => $field_name, '%minimum' => $minimum]), 'Correctly failed to save integer value less than minimum allowed value.');
|
Chris@0
|
204
|
Chris@0
|
205 // Try to set a decimal value
|
Chris@0
|
206 $this->drupalGet('entity_test/add');
|
Chris@0
|
207 $edit = [
|
Chris@0
|
208 "{$field_name}[0][value]" => 1.5,
|
Chris@0
|
209 ];
|
Chris@0
|
210 $this->drupalPostForm(NULL, $edit, t('Save'));
|
Chris@0
|
211 $this->assertRaw(t('%name is not a valid number.', ['%name' => $field_name]), 'Correctly failed to save decimal value to integer field.');
|
Chris@0
|
212
|
Chris@0
|
213 // Try to set a value above the maximum value
|
Chris@0
|
214 $this->drupalGet('entity_test/add');
|
Chris@0
|
215 $edit = [
|
Chris@0
|
216 "{$field_name}[0][value]" => $maximum + 1,
|
Chris@0
|
217 ];
|
Chris@0
|
218 $this->drupalPostForm(NULL, $edit, t('Save'));
|
Chris@0
|
219 $this->assertRaw(t('%name must be lower than or equal to %maximum.', ['%name' => $field_name, '%maximum' => $maximum]), 'Correctly failed to save integer value greater than maximum allowed value.');
|
Chris@0
|
220
|
Chris@0
|
221 // Try to set a wrong integer value.
|
Chris@0
|
222 $this->drupalGet('entity_test/add');
|
Chris@0
|
223 $edit = [
|
Chris@0
|
224 "{$field_name}[0][value]" => '20-40',
|
Chris@0
|
225 ];
|
Chris@0
|
226 $this->drupalPostForm(NULL, $edit, t('Save'));
|
Chris@0
|
227 $this->assertRaw(t('%name must be a number.', ['%name' => $field_name]), 'Correctly failed to save wrong integer value.');
|
Chris@0
|
228
|
Chris@0
|
229 // Test with valid entries.
|
Chris@0
|
230 $valid_entries = [
|
Chris@0
|
231 '-1234',
|
Chris@0
|
232 '0',
|
Chris@0
|
233 '1234',
|
Chris@0
|
234 ];
|
Chris@0
|
235
|
Chris@0
|
236 foreach ($valid_entries as $valid_entry) {
|
Chris@0
|
237 $this->drupalGet('entity_test/add');
|
Chris@0
|
238 $edit = [
|
Chris@0
|
239 "{$field_name}[0][value]" => $valid_entry,
|
Chris@0
|
240 ];
|
Chris@0
|
241 $this->drupalPostForm(NULL, $edit, t('Save'));
|
Chris@0
|
242 preg_match('|entity_test/manage/(\d+)|', $this->url, $match);
|
Chris@0
|
243 $id = $match[1];
|
Chris@0
|
244 $this->assertText(t('entity_test @id has been created.', ['@id' => $id]), 'Entity was created');
|
Chris@0
|
245 $this->assertRaw($valid_entry, 'Value is displayed.');
|
Chris@0
|
246 $this->assertNoFieldByXpath('//div[@content="' . $valid_entry . '"]', NULL, 'The "content" attribute is not present since the Prefix is not being displayed');
|
Chris@0
|
247 }
|
Chris@0
|
248
|
Chris@0
|
249 // Test for the content attribute when a Prefix is displayed. Presumably this also tests for the attribute when a Suffix is displayed.
|
Chris@0
|
250 entity_get_display('entity_test', 'entity_test', 'default')
|
Chris@0
|
251 ->setComponent($field_name, [
|
Chris@0
|
252 'type' => 'number_integer',
|
Chris@0
|
253 'settings' => [
|
Chris@0
|
254 'prefix_suffix' => TRUE,
|
Chris@0
|
255 ],
|
Chris@0
|
256 ])
|
Chris@0
|
257 ->save();
|
Chris@0
|
258 $integer_value = '123';
|
Chris@0
|
259 $this->drupalGet('entity_test/add');
|
Chris@0
|
260 $edit = [
|
Chris@0
|
261 "{$field_name}[0][value]" => $integer_value,
|
Chris@0
|
262 ];
|
Chris@0
|
263 $this->drupalPostForm(NULL, $edit, t('Save'));
|
Chris@0
|
264 preg_match('|entity_test/manage/(\d+)|', $this->url, $match);
|
Chris@0
|
265 $id = $match[1];
|
Chris@0
|
266 $this->assertText(t('entity_test @id has been created.', ['@id' => $id]), 'Entity was created');
|
Chris@0
|
267 $this->drupalGet('entity_test/' . $id);
|
Chris@0
|
268 $this->assertFieldByXPath('//div[@content="' . $integer_value . '"]', 'ThePrefix' . $integer_value, 'The "content" attribute has been set to the value of the field, and the prefix is being displayed.');
|
Chris@0
|
269 }
|
Chris@0
|
270
|
Chris@0
|
271 /**
|
Chris@0
|
272 * Test float field.
|
Chris@0
|
273 */
|
Chris@0
|
274 public function testNumberFloatField() {
|
Chris@0
|
275 // Create a field with settings to validate.
|
Chris@0
|
276 $field_name = Unicode::strtolower($this->randomMachineName());
|
Chris@0
|
277 FieldStorageConfig::create([
|
Chris@0
|
278 'field_name' => $field_name,
|
Chris@0
|
279 'entity_type' => 'entity_test',
|
Chris@0
|
280 'type' => 'float',
|
Chris@0
|
281 ])->save();
|
Chris@0
|
282
|
Chris@0
|
283 FieldConfig::create([
|
Chris@0
|
284 'field_name' => $field_name,
|
Chris@0
|
285 'entity_type' => 'entity_test',
|
Chris@0
|
286 'bundle' => 'entity_test',
|
Chris@0
|
287 ])->save();
|
Chris@0
|
288
|
Chris@0
|
289 entity_get_form_display('entity_test', 'entity_test', 'default')
|
Chris@0
|
290 ->setComponent($field_name, [
|
Chris@0
|
291 'type' => 'number',
|
Chris@0
|
292 'settings' => [
|
Chris@0
|
293 'placeholder' => '0.00'
|
Chris@0
|
294 ],
|
Chris@0
|
295 ])
|
Chris@0
|
296 ->save();
|
Chris@0
|
297
|
Chris@0
|
298 entity_get_display('entity_test', 'entity_test', 'default')
|
Chris@0
|
299 ->setComponent($field_name, [
|
Chris@0
|
300 'type' => 'number_decimal',
|
Chris@0
|
301 ])
|
Chris@0
|
302 ->save();
|
Chris@0
|
303
|
Chris@0
|
304 // Display creation form.
|
Chris@0
|
305 $this->drupalGet('entity_test/add');
|
Chris@0
|
306 $this->assertFieldByName("{$field_name}[0][value]", '', 'Widget is displayed');
|
Chris@0
|
307 $this->assertRaw('placeholder="0.00"');
|
Chris@0
|
308
|
Chris@0
|
309 // Submit a signed decimal value within the allowed precision and scale.
|
Chris@0
|
310 $value = '-1234.5678';
|
Chris@0
|
311 $edit = [
|
Chris@0
|
312 "{$field_name}[0][value]" => $value,
|
Chris@0
|
313 ];
|
Chris@0
|
314 $this->drupalPostForm(NULL, $edit, t('Save'));
|
Chris@0
|
315 preg_match('|entity_test/manage/(\d+)|', $this->url, $match);
|
Chris@0
|
316 $id = $match[1];
|
Chris@0
|
317 $this->assertText(t('entity_test @id has been created.', ['@id' => $id]), 'Entity was created');
|
Chris@0
|
318
|
Chris@0
|
319 // Ensure that the 'number_decimal' formatter displays the number with the
|
Chris@0
|
320 // expected rounding.
|
Chris@0
|
321 $this->drupalGet('entity_test/' . $id);
|
Chris@0
|
322 $this->assertRaw(round($value, 2));
|
Chris@0
|
323
|
Chris@0
|
324 // Try to create entries with more than one decimal separator; assert fail.
|
Chris@0
|
325 $wrong_entries = [
|
Chris@0
|
326 '3.14.159',
|
Chris@0
|
327 '0..45469',
|
Chris@0
|
328 '..4589',
|
Chris@0
|
329 '6.459.52',
|
Chris@0
|
330 '6.3..25',
|
Chris@0
|
331 ];
|
Chris@0
|
332
|
Chris@0
|
333 foreach ($wrong_entries as $wrong_entry) {
|
Chris@0
|
334 $this->drupalGet('entity_test/add');
|
Chris@0
|
335 $edit = [
|
Chris@0
|
336 "{$field_name}[0][value]" => $wrong_entry,
|
Chris@0
|
337 ];
|
Chris@0
|
338 $this->drupalPostForm(NULL, $edit, t('Save'));
|
Chris@0
|
339 $this->assertRaw(t('%name must be a number.', ['%name' => $field_name]), 'Correctly failed to save float value with more than one decimal point.');
|
Chris@0
|
340 }
|
Chris@0
|
341
|
Chris@0
|
342 // Try to create entries with minus sign not in the first position.
|
Chris@0
|
343 $wrong_entries = [
|
Chris@0
|
344 '3-3',
|
Chris@0
|
345 '4-',
|
Chris@0
|
346 '1.3-',
|
Chris@0
|
347 '1.2-4',
|
Chris@0
|
348 '-10-10',
|
Chris@0
|
349 ];
|
Chris@0
|
350
|
Chris@0
|
351 foreach ($wrong_entries as $wrong_entry) {
|
Chris@0
|
352 $this->drupalGet('entity_test/add');
|
Chris@0
|
353 $edit = [
|
Chris@0
|
354 "{$field_name}[0][value]" => $wrong_entry,
|
Chris@0
|
355 ];
|
Chris@0
|
356 $this->drupalPostForm(NULL, $edit, t('Save'));
|
Chris@0
|
357 $this->assertRaw(t('%name must be a number.', ['%name' => $field_name]), 'Correctly failed to save float value with minus sign in the wrong position.');
|
Chris@0
|
358 }
|
Chris@0
|
359 }
|
Chris@0
|
360
|
Chris@0
|
361 /**
|
Chris@0
|
362 * Test default formatter behavior
|
Chris@0
|
363 */
|
Chris@0
|
364 public function testNumberFormatter() {
|
Chris@0
|
365 $type = Unicode::strtolower($this->randomMachineName());
|
Chris@0
|
366 $float_field = Unicode::strtolower($this->randomMachineName());
|
Chris@0
|
367 $integer_field = Unicode::strtolower($this->randomMachineName());
|
Chris@0
|
368 $thousand_separators = ['', '.', ',', ' ', chr(8201), "'"];
|
Chris@0
|
369 $decimal_separators = ['.', ','];
|
Chris@0
|
370 $prefix = $this->randomMachineName();
|
Chris@0
|
371 $suffix = $this->randomMachineName();
|
Chris@0
|
372 $random_float = rand(0, pow(10, 6));
|
Chris@0
|
373 $random_integer = rand(0, pow(10, 6));
|
Chris@0
|
374
|
Chris@0
|
375 // Create a content type containing float and integer fields.
|
Chris@0
|
376 $this->drupalCreateContentType(['type' => $type]);
|
Chris@0
|
377
|
Chris@0
|
378 FieldStorageConfig::create([
|
Chris@0
|
379 'field_name' => $float_field,
|
Chris@0
|
380 'entity_type' => 'node',
|
Chris@0
|
381 'type' => 'float',
|
Chris@0
|
382 ])->save();
|
Chris@0
|
383
|
Chris@0
|
384 FieldStorageConfig::create([
|
Chris@0
|
385 'field_name' => $integer_field,
|
Chris@0
|
386 'entity_type' => 'node',
|
Chris@0
|
387 'type' => 'integer',
|
Chris@0
|
388 ])->save();
|
Chris@0
|
389
|
Chris@0
|
390 FieldConfig::create([
|
Chris@0
|
391 'field_name' => $float_field,
|
Chris@0
|
392 'entity_type' => 'node',
|
Chris@0
|
393 'bundle' => $type,
|
Chris@0
|
394 'settings' => [
|
Chris@0
|
395 'prefix' => $prefix,
|
Chris@0
|
396 'suffix' => $suffix
|
Chris@0
|
397 ],
|
Chris@0
|
398 ])->save();
|
Chris@0
|
399
|
Chris@0
|
400 FieldConfig::create([
|
Chris@0
|
401 'field_name' => $integer_field,
|
Chris@0
|
402 'entity_type' => 'node',
|
Chris@0
|
403 'bundle' => $type,
|
Chris@0
|
404 'settings' => [
|
Chris@0
|
405 'prefix' => $prefix,
|
Chris@0
|
406 'suffix' => $suffix
|
Chris@0
|
407 ],
|
Chris@0
|
408 ])->save();
|
Chris@0
|
409
|
Chris@0
|
410 entity_get_form_display('node', $type, 'default')
|
Chris@0
|
411 ->setComponent($float_field, [
|
Chris@0
|
412 'type' => 'number',
|
Chris@0
|
413 'settings' => [
|
Chris@0
|
414 'placeholder' => '0.00'
|
Chris@0
|
415 ],
|
Chris@0
|
416 ])
|
Chris@0
|
417 ->setComponent($integer_field, [
|
Chris@0
|
418 'type' => 'number',
|
Chris@0
|
419 'settings' => [
|
Chris@0
|
420 'placeholder' => '0.00'
|
Chris@0
|
421 ],
|
Chris@0
|
422 ])
|
Chris@0
|
423 ->save();
|
Chris@0
|
424
|
Chris@0
|
425 entity_get_display('node', $type, 'default')
|
Chris@0
|
426 ->setComponent($float_field, [
|
Chris@0
|
427 'type' => 'number_decimal',
|
Chris@0
|
428 ])
|
Chris@0
|
429 ->setComponent($integer_field, [
|
Chris@0
|
430 'type' => 'number_unformatted',
|
Chris@0
|
431 ])
|
Chris@0
|
432 ->save();
|
Chris@0
|
433
|
Chris@0
|
434 // Create a node to test formatters.
|
Chris@0
|
435 $node = Node::create([
|
Chris@0
|
436 'type' => $type,
|
Chris@0
|
437 'title' => $this->randomMachineName(),
|
Chris@0
|
438 $float_field => ['value' => $random_float],
|
Chris@0
|
439 $integer_field => ['value' => $random_integer],
|
Chris@0
|
440 ]);
|
Chris@0
|
441 $node->save();
|
Chris@0
|
442
|
Chris@0
|
443 // Go to manage display page.
|
Chris@0
|
444 $this->drupalGet("admin/structure/types/manage/$type/display");
|
Chris@0
|
445
|
Chris@0
|
446 // Configure number_decimal formatter for the 'float' field type.
|
Chris@0
|
447 $thousand_separator = $thousand_separators[array_rand($thousand_separators)];
|
Chris@0
|
448 $decimal_separator = $decimal_separators[array_rand($decimal_separators)];
|
Chris@0
|
449 $scale = rand(0, 10);
|
Chris@0
|
450
|
Chris@0
|
451 $this->drupalPostAjaxForm(NULL, [], "${float_field}_settings_edit");
|
Chris@0
|
452 $edit = [
|
Chris@0
|
453 "fields[${float_field}][settings_edit_form][settings][prefix_suffix]" => TRUE,
|
Chris@0
|
454 "fields[${float_field}][settings_edit_form][settings][scale]" => $scale,
|
Chris@0
|
455 "fields[${float_field}][settings_edit_form][settings][decimal_separator]" => $decimal_separator,
|
Chris@0
|
456 "fields[${float_field}][settings_edit_form][settings][thousand_separator]" => $thousand_separator,
|
Chris@0
|
457 ];
|
Chris@0
|
458 $this->drupalPostAjaxForm(NULL, $edit, "${float_field}_plugin_settings_update");
|
Chris@0
|
459 $this->drupalPostForm(NULL, [], t('Save'));
|
Chris@0
|
460
|
Chris@0
|
461 // Check number_decimal and number_unformatted formatters behavior.
|
Chris@0
|
462 $this->drupalGet('node/' . $node->id());
|
Chris@0
|
463 $float_formatted = number_format($random_float, $scale, $decimal_separator, $thousand_separator);
|
Chris@0
|
464 $this->assertRaw("$prefix$float_formatted$suffix", 'Prefix and suffix added');
|
Chris@0
|
465 $this->assertRaw((string) $random_integer);
|
Chris@0
|
466
|
Chris@0
|
467 // Configure the number_decimal formatter.
|
Chris@0
|
468 entity_get_display('node', $type, 'default')
|
Chris@0
|
469 ->setComponent($integer_field, [
|
Chris@0
|
470 'type' => 'number_integer',
|
Chris@0
|
471 ])
|
Chris@0
|
472 ->save();
|
Chris@0
|
473 $this->drupalGet("admin/structure/types/manage/$type/display");
|
Chris@0
|
474
|
Chris@0
|
475 $thousand_separator = $thousand_separators[array_rand($thousand_separators)];
|
Chris@0
|
476
|
Chris@0
|
477 $this->drupalPostAjaxForm(NULL, [], "${integer_field}_settings_edit");
|
Chris@0
|
478 $edit = [
|
Chris@0
|
479 "fields[${integer_field}][settings_edit_form][settings][prefix_suffix]" => FALSE,
|
Chris@0
|
480 "fields[${integer_field}][settings_edit_form][settings][thousand_separator]" => $thousand_separator,
|
Chris@0
|
481 ];
|
Chris@0
|
482 $this->drupalPostAjaxForm(NULL, $edit, "${integer_field}_plugin_settings_update");
|
Chris@0
|
483 $this->drupalPostForm(NULL, [], t('Save'));
|
Chris@0
|
484
|
Chris@0
|
485 // Check number_integer formatter behavior.
|
Chris@0
|
486 $this->drupalGet('node/' . $node->id());
|
Chris@0
|
487
|
Chris@0
|
488 $integer_formatted = number_format($random_integer, 0, '', $thousand_separator);
|
Chris@0
|
489 $this->assertRaw($integer_formatted, 'Random integer formatted');
|
Chris@0
|
490 }
|
Chris@0
|
491
|
Chris@0
|
492 /**
|
Chris@0
|
493 * Tests setting the minimum value of a float field through the interface.
|
Chris@0
|
494 */
|
Chris@0
|
495 public function testCreateNumberFloatField() {
|
Chris@0
|
496 // Create a float field.
|
Chris@0
|
497 $field_name = Unicode::strtolower($this->randomMachineName());
|
Chris@0
|
498 FieldStorageConfig::create([
|
Chris@0
|
499 'field_name' => $field_name,
|
Chris@0
|
500 'entity_type' => 'entity_test',
|
Chris@0
|
501 'type' => 'float',
|
Chris@0
|
502 ])->save();
|
Chris@0
|
503
|
Chris@0
|
504 $field = FieldConfig::create([
|
Chris@0
|
505 'field_name' => $field_name,
|
Chris@0
|
506 'entity_type' => 'entity_test',
|
Chris@0
|
507 'bundle' => 'entity_test',
|
Chris@0
|
508 ]);
|
Chris@0
|
509 $field->save();
|
Chris@0
|
510
|
Chris@0
|
511 // Set the minimum value to a float value.
|
Chris@0
|
512 $this->assertSetMinimumValue($field, 0.0001);
|
Chris@0
|
513 // Set the minimum value to an integer value.
|
Chris@0
|
514 $this->assertSetMinimumValue($field, 1);
|
Chris@0
|
515 }
|
Chris@0
|
516
|
Chris@0
|
517 /**
|
Chris@0
|
518 * Tests setting the minimum value of a decimal field through the interface.
|
Chris@0
|
519 */
|
Chris@0
|
520 public function testCreateNumberDecimalField() {
|
Chris@0
|
521 // Create a decimal field.
|
Chris@0
|
522 $field_name = Unicode::strtolower($this->randomMachineName());
|
Chris@0
|
523 FieldStorageConfig::create([
|
Chris@0
|
524 'field_name' => $field_name,
|
Chris@0
|
525 'entity_type' => 'entity_test',
|
Chris@0
|
526 'type' => 'decimal',
|
Chris@0
|
527 ])->save();
|
Chris@0
|
528
|
Chris@0
|
529 $field = FieldConfig::create([
|
Chris@0
|
530 'field_name' => $field_name,
|
Chris@0
|
531 'entity_type' => 'entity_test',
|
Chris@0
|
532 'bundle' => 'entity_test',
|
Chris@0
|
533 ]);
|
Chris@0
|
534 $field->save();
|
Chris@0
|
535
|
Chris@0
|
536 // Set the minimum value to a decimal value.
|
Chris@0
|
537 $this->assertSetMinimumValue($field, 0.1);
|
Chris@0
|
538 // Set the minimum value to an integer value.
|
Chris@0
|
539 $this->assertSetMinimumValue($field, 1);
|
Chris@0
|
540 }
|
Chris@0
|
541
|
Chris@0
|
542 /**
|
Chris@0
|
543 * Helper function to set the minimum value of a field.
|
Chris@0
|
544 */
|
Chris@0
|
545 public function assertSetMinimumValue($field, $minimum_value) {
|
Chris@0
|
546 $field_configuration_url = 'entity_test/structure/entity_test/fields/entity_test.entity_test.' . $field->getName();
|
Chris@0
|
547
|
Chris@0
|
548 // Set the minimum value.
|
Chris@0
|
549 $edit = [
|
Chris@0
|
550 'settings[min]' => $minimum_value,
|
Chris@0
|
551 ];
|
Chris@0
|
552 $this->drupalPostForm($field_configuration_url, $edit, t('Save settings'));
|
Chris@0
|
553 // Check if an error message is shown.
|
Chris@0
|
554 $this->assertNoRaw(t('%name is not a valid number.', ['%name' => t('Minimum')]), 'Saved ' . gettype($minimum_value) . ' value as minimal value on a ' . $field->getType() . ' field');
|
Chris@0
|
555 // Check if a success message is shown.
|
Chris@0
|
556 $this->assertRaw(t('Saved %label configuration.', ['%label' => $field->getLabel()]));
|
Chris@0
|
557 // Check if the minimum value was actually set.
|
Chris@0
|
558 $this->drupalGet($field_configuration_url);
|
Chris@0
|
559 $this->assertFieldById('edit-settings-min', $minimum_value, 'Minimal ' . gettype($minimum_value) . ' value was set on a ' . $field->getType() . ' field.');
|
Chris@0
|
560 }
|
Chris@0
|
561
|
Chris@0
|
562 }
|