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