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