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