You are here

public function BooleanFieldTest::testBooleanField in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/field/tests/src/Functional/Boolean/BooleanFieldTest.php \Drupal\Tests\field\Functional\Boolean\BooleanFieldTest::testBooleanField()

Tests boolean field.

File

core/modules/field/tests/src/Functional/Boolean/BooleanFieldTest.php, line 65

Class

BooleanFieldTest
Tests boolean field functionality.

Namespace

Drupal\Tests\field\Functional\Boolean

Code

public function testBooleanField() {
  $on = $this
    ->randomMachineName();
  $off = $this
    ->randomMachineName();
  $label = $this
    ->randomMachineName();

  // Create a field with settings to validate.
  $field_name = mb_strtolower($this
    ->randomMachineName());
  $this->fieldStorage = FieldStorageConfig::create([
    'field_name' => $field_name,
    'entity_type' => 'entity_test',
    'type' => 'boolean',
  ]);
  $this->fieldStorage
    ->save();
  $this->field = FieldConfig::create([
    'field_name' => $field_name,
    'entity_type' => 'entity_test',
    'bundle' => 'entity_test',
    'label' => $label,
    'required' => TRUE,
    'settings' => [
      'on_label' => $on,
      'off_label' => $off,
    ],
  ]);
  $this->field
    ->save();

  /** @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface $display_repository */
  $display_repository = \Drupal::service('entity_display.repository');

  // Create a form display for the default form mode.
  $display_repository
    ->getFormDisplay('entity_test', 'entity_test')
    ->setComponent($field_name, [
    'type' => 'boolean_checkbox',
  ])
    ->save();

  // Create a display for the full view mode.
  $display_repository
    ->getViewDisplay('entity_test', 'entity_test', 'full')
    ->setComponent($field_name, [
    'type' => 'boolean',
  ])
    ->save();

  // Display creation form.
  $this
    ->drupalGet('entity_test/add');
  $this
    ->assertSession()
    ->fieldValueEquals("{$field_name}[value]", '');
  $this
    ->assertSession()
    ->pageTextContains($this->field
    ->label());
  $this
    ->assertSession()
    ->responseNotContains($on);

  // Submit and ensure it is accepted.
  $edit = [
    "{$field_name}[value]" => 1,
  ];
  $this
    ->submitForm($edit, 'Save');
  preg_match('|entity_test/manage/(\\d+)|', $this
    ->getUrl(), $match);
  $id = $match[1];
  $this
    ->assertSession()
    ->pageTextContains('entity_test ' . $id . ' has been created.');

  // Verify that boolean value is displayed.
  $entity = EntityTest::load($id);
  $this
    ->drupalGet($entity
    ->toUrl());
  $this
    ->assertSession()
    ->responseContains('<div class="field__item">' . $on . '</div>');

  // Test with "On" label option.
  $display_repository
    ->getFormDisplay('entity_test', 'entity_test')
    ->setComponent($field_name, [
    'type' => 'boolean_checkbox',
    'settings' => [
      'display_label' => FALSE,
    ],
  ])
    ->save();
  $this
    ->drupalGet('entity_test/add');
  $this
    ->assertSession()
    ->fieldValueEquals("{$field_name}[value]", '');
  $this
    ->assertSession()
    ->pageTextContains($on);
  $this
    ->assertSession()
    ->pageTextNotContains($this->field
    ->label());

  // Test if we can change the on label.
  $on = $this
    ->randomMachineName();
  $edit = [
    'settings[on_label]' => $on,
  ];
  $this
    ->drupalGet('entity_test/structure/entity_test/fields/entity_test.entity_test.' . $field_name);
  $this
    ->submitForm($edit, 'Save settings');

  // Check if we see the updated labels in the creation form.
  $this
    ->drupalGet('entity_test/add');
  $this
    ->assertSession()
    ->pageTextContains($on);

  // Go to the form display page and check if the default settings works as
  // expected.
  $fieldEditUrl = 'entity_test/structure/entity_test/form-display';
  $this
    ->drupalGet($fieldEditUrl);

  // Click on the widget settings button to open the widget settings form.
  $this
    ->submitForm([], $field_name . "_settings_edit");
  $this
    ->assertSession()
    ->pageTextContains('Use field label instead of the "On" label as the label.');

  // Enable setting.
  $edit = [
    'fields[' . $field_name . '][settings_edit_form][settings][display_label]' => 1,
  ];
  $this
    ->submitForm($edit, $field_name . "_plugin_settings_update");
  $this
    ->submitForm([], 'Save');

  // Go again to the form display page and check if the setting
  // is stored and has the expected effect.
  $this
    ->drupalGet($fieldEditUrl);
  $this
    ->assertSession()
    ->pageTextContains('Use field label: Yes');
  $this
    ->submitForm([], $field_name . "_settings_edit");
  $this
    ->assertSession()
    ->pageTextContains('Use field label instead of the "On" label as the label.');
  $this
    ->getSession()
    ->getPage()
    ->hasCheckedField('fields[' . $field_name . '][settings_edit_form][settings][display_label]');

  // Test the boolean field settings.
  $this
    ->drupalGet('entity_test/structure/entity_test/fields/entity_test.entity_test.' . $field_name);
  $this
    ->assertSession()
    ->fieldValueEquals('edit-settings-on-label', $on);
  $this
    ->assertSession()
    ->fieldValueEquals('edit-settings-off-label', $off);
}