Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / field / tests / src / FunctionalJavascript / Number / NumberFieldTest.php
1 <?php
2
3 namespace Drupal\Tests\field\FunctionalJavascript\Number;
4
5 use Drupal\field\Entity\FieldConfig;
6 use Drupal\field\Entity\FieldStorageConfig;
7 use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
8 use Drupal\node\Entity\Node;
9
10 /**
11  * Tests the numeric field widget.
12  *
13  * @group field
14  */
15 class NumberFieldTest extends WebDriverTestBase {
16
17   /**
18    * {@inheritdoc}
19    */
20   protected static $modules = ['node', 'entity_test', 'field_ui'];
21
22   /**
23    * {@inheritdoc}
24    */
25   protected function setUp() {
26     parent::setUp();
27
28     $this->drupalLogin($this->drupalCreateUser([
29       'view test entity',
30       'administer entity_test content',
31       'administer content types',
32       'administer node fields',
33       'administer node display',
34       'bypass node access',
35       'administer entity_test fields',
36     ]));
37   }
38
39   /**
40    * Test default formatter behavior.
41    */
42   public function testNumberFormatter() {
43     $type = mb_strtolower($this->randomMachineName());
44     $float_field = mb_strtolower($this->randomMachineName());
45     $integer_field = mb_strtolower($this->randomMachineName());
46     $thousand_separators = ['', '.', ',', ' ', chr(8201), "'"];
47     $decimal_separators = ['.', ','];
48     $prefix = $this->randomMachineName();
49     $suffix = $this->randomMachineName();
50     $random_float = rand(0, pow(10, 6));
51     $random_integer = rand(0, pow(10, 6));
52     $assert_session = $this->assertSession();
53
54     // Create a content type containing float and integer fields.
55     $this->drupalCreateContentType(['type' => $type]);
56
57     FieldStorageConfig::create([
58       'field_name' => $float_field,
59       'entity_type' => 'node',
60       'type' => 'float',
61     ])->save();
62
63     FieldStorageConfig::create([
64       'field_name' => $integer_field,
65       'entity_type' => 'node',
66       'type' => 'integer',
67     ])->save();
68
69     FieldConfig::create([
70       'field_name' => $float_field,
71       'entity_type' => 'node',
72       'bundle' => $type,
73       'settings' => [
74         'prefix' => $prefix,
75         'suffix' => $suffix,
76       ],
77     ])->save();
78
79     FieldConfig::create([
80       'field_name' => $integer_field,
81       'entity_type' => 'node',
82       'bundle' => $type,
83       'settings' => [
84         'prefix' => $prefix,
85         'suffix' => $suffix,
86       ],
87     ])->save();
88
89     entity_get_form_display('node', $type, 'default')
90       ->setComponent($float_field, [
91         'type' => 'number',
92         'settings' => [
93           'placeholder' => '0.00',
94         ],
95       ])
96       ->setComponent($integer_field, [
97         'type' => 'number',
98         'settings' => [
99           'placeholder' => '0.00',
100         ],
101       ])
102       ->save();
103
104     entity_get_display('node', $type, 'default')
105       ->setComponent($float_field, [
106         'type' => 'number_decimal',
107       ])
108       ->setComponent($integer_field, [
109         'type' => 'number_unformatted',
110       ])
111       ->save();
112
113     // Create a node to test formatters.
114     $node = Node::create([
115       'type' => $type,
116       'title' => $this->randomMachineName(),
117       $float_field => ['value' => $random_float],
118       $integer_field => ['value' => $random_integer],
119     ]);
120     $node->save();
121
122     // Go to manage display page.
123     $this->drupalGet("admin/structure/types/manage/$type/display");
124
125     // Configure number_decimal formatter for the 'float' field type.
126     $thousand_separator = $thousand_separators[array_rand($thousand_separators)];
127     $decimal_separator = $decimal_separators[array_rand($decimal_separators)];
128     $scale = rand(0, 10);
129
130     $page = $this->getSession()->getPage();
131     $page->pressButton("${float_field}_settings_edit");
132     $assert_session->waitForElement('css', '.ajax-new-content');
133     $edit = [
134       "fields[${float_field}][settings_edit_form][settings][prefix_suffix]" => TRUE,
135       "fields[${float_field}][settings_edit_form][settings][scale]" => $scale,
136       "fields[${float_field}][settings_edit_form][settings][decimal_separator]" => $decimal_separator,
137       "fields[${float_field}][settings_edit_form][settings][thousand_separator]" => $thousand_separator,
138     ];
139     foreach ($edit as $name => $value) {
140       $page->fillField($name, $value);
141     }
142     $page->pressButton("${float_field}_plugin_settings_update");
143     $assert_session->waitForElement('css', '.field-plugin-summary-cell > .ajax-new-content');
144     $this->drupalPostForm(NULL, [], t('Save'));
145
146     // Check number_decimal and number_unformatted formatters behavior.
147     $this->drupalGet('node/' . $node->id());
148     $float_formatted = number_format($random_float, $scale, $decimal_separator, $thousand_separator);
149     $this->assertRaw("$prefix$float_formatted$suffix", 'Prefix and suffix added');
150     $this->assertRaw((string) $random_integer);
151
152     // Configure the number_decimal formatter.
153     entity_get_display('node', $type, 'default')
154       ->setComponent($integer_field, [
155         'type' => 'number_integer',
156       ])
157       ->save();
158     $this->drupalGet("admin/structure/types/manage/$type/display");
159
160     $thousand_separator = $thousand_separators[array_rand($thousand_separators)];
161
162     $page = $this->getSession()->getPage();
163     $page->pressButton("${integer_field}_settings_edit");
164     $assert_session->waitForElement('css', '.ajax-new-content');
165     $edit = [
166       "fields[${integer_field}][settings_edit_form][settings][prefix_suffix]" => FALSE,
167       "fields[${integer_field}][settings_edit_form][settings][thousand_separator]" => $thousand_separator,
168     ];
169     foreach ($edit as $name => $value) {
170       $page->fillField($name, $value);
171     }
172     $page->pressButton("${integer_field}_plugin_settings_update");
173     $assert_session->waitForElement('css', '.field-plugin-summary-cell > .ajax-new-content');
174     $this->drupalPostForm(NULL, [], t('Save'));
175
176     // Check number_integer formatter behavior.
177     $this->drupalGet('node/' . $node->id());
178
179     $integer_formatted = number_format($random_integer, 0, '', $thousand_separator);
180     $this->assertRaw($integer_formatted, 'Random integer formatted');
181   }
182
183 }