Mercurial > hg > isophonics-drupal-site
comparison core/modules/field/src/Tests/Boolean/BooleanFieldTest.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
1 <?php | |
2 | |
3 namespace Drupal\field\Tests\Boolean; | |
4 | |
5 use Drupal\Component\Utility\Unicode; | |
6 use Drupal\entity_test\Entity\EntityTest; | |
7 use Drupal\field\Entity\FieldStorageConfig; | |
8 use Drupal\field\Entity\FieldConfig; | |
9 use Drupal\simpletest\WebTestBase; | |
10 | |
11 /** | |
12 * Tests boolean field functionality. | |
13 * | |
14 * @group field | |
15 */ | |
16 class BooleanFieldTest extends WebTestBase { | |
17 | |
18 /** | |
19 * Modules to enable. | |
20 * | |
21 * @var array | |
22 */ | |
23 public static $modules = [ | |
24 'entity_test', | |
25 'field_ui', | |
26 'options', | |
27 'field_test_boolean_access_denied', | |
28 ]; | |
29 | |
30 /** | |
31 * A field to use in this test class. | |
32 * | |
33 * @var \Drupal\field\Entity\FieldStorageConfig | |
34 */ | |
35 protected $fieldStorage; | |
36 | |
37 /** | |
38 * The field used in this test class. | |
39 * | |
40 * @var \Drupal\field\Entity\FieldConfig | |
41 */ | |
42 protected $field; | |
43 | |
44 /** | |
45 * {@inheritdoc} | |
46 */ | |
47 protected function setUp() { | |
48 parent::setUp(); | |
49 | |
50 $this->drupalLogin($this->drupalCreateUser([ | |
51 'view test entity', | |
52 'administer entity_test content', | |
53 'administer entity_test form display', | |
54 'administer entity_test fields', | |
55 ])); | |
56 } | |
57 | |
58 /** | |
59 * Tests boolean field. | |
60 */ | |
61 public function testBooleanField() { | |
62 $on = $this->randomMachineName(); | |
63 $off = $this->randomMachineName(); | |
64 $label = $this->randomMachineName(); | |
65 | |
66 // Create a field with settings to validate. | |
67 $field_name = Unicode::strtolower($this->randomMachineName()); | |
68 $this->fieldStorage = FieldStorageConfig::create([ | |
69 'field_name' => $field_name, | |
70 'entity_type' => 'entity_test', | |
71 'type' => 'boolean', | |
72 ]); | |
73 $this->fieldStorage->save(); | |
74 $this->field = FieldConfig::create([ | |
75 'field_name' => $field_name, | |
76 'entity_type' => 'entity_test', | |
77 'bundle' => 'entity_test', | |
78 'label' => $label, | |
79 'required' => TRUE, | |
80 'settings' => [ | |
81 'on_label' => $on, | |
82 'off_label' => $off, | |
83 ], | |
84 ]); | |
85 $this->field->save(); | |
86 | |
87 // Create a form display for the default form mode. | |
88 entity_get_form_display('entity_test', 'entity_test', 'default') | |
89 ->setComponent($field_name, [ | |
90 'type' => 'boolean_checkbox', | |
91 ]) | |
92 ->save(); | |
93 // Create a display for the full view mode. | |
94 entity_get_display('entity_test', 'entity_test', 'full') | |
95 ->setComponent($field_name, [ | |
96 'type' => 'boolean', | |
97 ]) | |
98 ->save(); | |
99 | |
100 // Display creation form. | |
101 $this->drupalGet('entity_test/add'); | |
102 $this->assertFieldByName("{$field_name}[value]", '', 'Widget found.'); | |
103 $this->assertText($this->field->label(), 'Uses field label by default.'); | |
104 $this->assertNoRaw($on, 'Does not use the "On" label.'); | |
105 | |
106 // Submit and ensure it is accepted. | |
107 $edit = [ | |
108 "{$field_name}[value]" => 1, | |
109 ]; | |
110 $this->drupalPostForm(NULL, $edit, t('Save')); | |
111 preg_match('|entity_test/manage/(\d+)|', $this->url, $match); | |
112 $id = $match[1]; | |
113 $this->assertText(t('entity_test @id has been created.', ['@id' => $id])); | |
114 | |
115 // Verify that boolean value is displayed. | |
116 $entity = EntityTest::load($id); | |
117 $display = entity_get_display($entity->getEntityTypeId(), $entity->bundle(), 'full'); | |
118 $content = $display->build($entity); | |
119 $this->setRawContent(\Drupal::service('renderer')->renderRoot($content)); | |
120 $this->assertRaw('<div class="field__item">' . $on . '</div>'); | |
121 | |
122 // Test with "On" label option. | |
123 entity_get_form_display('entity_test', 'entity_test', 'default') | |
124 ->setComponent($field_name, [ | |
125 'type' => 'boolean_checkbox', | |
126 'settings' => [ | |
127 'display_label' => FALSE, | |
128 ] | |
129 ]) | |
130 ->save(); | |
131 | |
132 $this->drupalGet('entity_test/add'); | |
133 $this->assertFieldByName("{$field_name}[value]", '', 'Widget found.'); | |
134 $this->assertRaw($on); | |
135 $this->assertNoText($this->field->label()); | |
136 | |
137 // Test if we can change the on label. | |
138 $on = $this->randomMachineName(); | |
139 $edit = [ | |
140 'settings[on_label]' => $on, | |
141 ]; | |
142 $this->drupalPostForm('entity_test/structure/entity_test/fields/entity_test.entity_test.' . $field_name, $edit, t('Save settings')); | |
143 // Check if we see the updated labels in the creation form. | |
144 $this->drupalGet('entity_test/add'); | |
145 $this->assertRaw($on); | |
146 | |
147 // Go to the form display page and check if the default settings works as | |
148 // expected. | |
149 $fieldEditUrl = 'entity_test/structure/entity_test/form-display'; | |
150 $this->drupalGet($fieldEditUrl); | |
151 | |
152 // Click on the widget settings button to open the widget settings form. | |
153 $this->drupalPostAjaxForm(NULL, [], $field_name . "_settings_edit"); | |
154 | |
155 $this->assertText( | |
156 'Use field label instead of the "On label" as label', | |
157 t('Display setting checkbox available.') | |
158 ); | |
159 | |
160 // Enable setting. | |
161 $edit = ['fields[' . $field_name . '][settings_edit_form][settings][display_label]' => 1]; | |
162 $this->drupalPostAjaxForm(NULL, $edit, $field_name . "_plugin_settings_update"); | |
163 $this->drupalPostForm(NULL, NULL, 'Save'); | |
164 | |
165 // Go again to the form display page and check if the setting | |
166 // is stored and has the expected effect. | |
167 $this->drupalGet($fieldEditUrl); | |
168 $this->assertText('Use field label: Yes', 'Checking the display settings checkbox updated the value.'); | |
169 | |
170 $this->drupalPostAjaxForm(NULL, [], $field_name . "_settings_edit"); | |
171 $this->assertText( | |
172 'Use field label instead of the "On label" as label', | |
173 t('Display setting checkbox is available') | |
174 ); | |
175 $this->assertFieldByXPath( | |
176 '*//input[starts-with(@id, "edit-fields-' . $field_name . '-settings-edit-form-settings-display-label") and @value="1"]', | |
177 TRUE, | |
178 t('Display label changes label of the checkbox') | |
179 ); | |
180 | |
181 // Test the boolean field settings. | |
182 $this->drupalGet('entity_test/structure/entity_test/fields/entity_test.entity_test.' . $field_name); | |
183 $this->assertFieldById('edit-settings-on-label', $on); | |
184 $this->assertFieldById('edit-settings-off-label', $off); | |
185 } | |
186 | |
187 /** | |
188 * Test field access. | |
189 */ | |
190 public function testFormAccess() { | |
191 $on = 'boolean_on'; | |
192 $off = 'boolean_off'; | |
193 $label = 'boolean_label'; | |
194 $field_name = 'boolean_name'; | |
195 $this->fieldStorage = FieldStorageConfig::create([ | |
196 'field_name' => $field_name, | |
197 'entity_type' => 'entity_test', | |
198 'type' => 'boolean', | |
199 ]); | |
200 $this->fieldStorage->save(); | |
201 $this->field = FieldConfig::create([ | |
202 'field_name' => $field_name, | |
203 'entity_type' => 'entity_test', | |
204 'bundle' => 'entity_test', | |
205 'label' => $label, | |
206 'settings' => [ | |
207 'on_label' => $on, | |
208 'off_label' => $off, | |
209 ], | |
210 ]); | |
211 $this->field->save(); | |
212 | |
213 // Create a form display for the default form mode. | |
214 entity_get_form_display('entity_test', 'entity_test', 'default') | |
215 ->setComponent($field_name, [ | |
216 'type' => 'boolean_checkbox', | |
217 ]) | |
218 ->save(); | |
219 | |
220 // Create a display for the full view mode. | |
221 entity_get_display('entity_test', 'entity_test', 'full') | |
222 ->setComponent($field_name, [ | |
223 'type' => 'boolean', | |
224 ]) | |
225 ->save(); | |
226 | |
227 // Display creation form. | |
228 $this->drupalGet('entity_test/add'); | |
229 $this->assertFieldByName("{$field_name}[value]"); | |
230 | |
231 // Should be posted OK. | |
232 $this->drupalPostForm(NULL, [], t('Save')); | |
233 preg_match('|entity_test/manage/(\d+)|', $this->url, $match); | |
234 $id = $match[1]; | |
235 $this->assertText(t('entity_test @id has been created.', ['@id' => $id])); | |
236 | |
237 // Tell the test module to disable access to the field. | |
238 \Drupal::state()->set('field.test_boolean_field_access_field', $field_name); | |
239 $this->drupalGet('entity_test/add'); | |
240 // Field should not be there anymore. | |
241 $this->assertNoFieldByName("{$field_name}[value]"); | |
242 // Should still be able to post the form. | |
243 $this->drupalPostForm(NULL, [], t('Save')); | |
244 preg_match('|entity_test/manage/(\d+)|', $this->url, $match); | |
245 $id = $match[1]; | |
246 $this->assertText(t('entity_test @id has been created.', ['@id' => $id])); | |
247 } | |
248 | |
249 } |