You are here

public function OptionsWidgetsTest::testSelectListFloat in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/options/tests/src/Functional/OptionsWidgetsTest.php \Drupal\Tests\options\Functional\OptionsWidgetsTest::testSelectListFloat()

Tests the 'options_select' widget (float values).

File

core/modules/options/tests/src/Functional/OptionsWidgetsTest.php, line 495

Class

OptionsWidgetsTest
Tests the Options widgets.

Namespace

Drupal\Tests\options\Functional

Code

public function testSelectListFloat() {

  // Create an instance of the 'float value' field.
  $field = FieldConfig::create([
    'field_storage' => $this->float,
    'bundle' => 'entity_test',
    'required' => TRUE,
  ]);
  $field
    ->save();
  $this->container
    ->get('entity_type.manager')
    ->getStorage('entity_form_display')
    ->load('entity_test.entity_test.default')
    ->setComponent($this->float
    ->getName(), [
    'type' => 'options_select',
  ])
    ->save();

  // Create an entity.
  $entity = EntityTest::create([
    'user_id' => 1,
    'name' => $this
      ->randomMachineName(),
  ]);
  $entity
    ->save();

  // Display form.
  $this
    ->drupalGet('entity_test/manage/' . $entity
    ->id() . '/edit');

  // With no field data, nothing is selected.
  $this
    ->assertFalse($this
    ->assertSession()
    ->optionExists('float', 0)
    ->isSelected());
  $this
    ->assertFalse($this
    ->assertSession()
    ->optionExists('float', 1.5)
    ->isSelected());
  $this
    ->assertFalse($this
    ->assertSession()
    ->optionExists('float', 2)
    ->isSelected());

  // Submit form.
  $edit = [
    'float' => 1.5,
  ];
  $this
    ->drupalPostForm(NULL, $edit, t('Save'));
  $this
    ->assertFieldValues($entity, 'float', [
    1.5,
  ]);

  // Display form: check that the right options are selected.
  $this
    ->drupalGet('entity_test/manage/' . $entity
    ->id() . '/edit');
  $this
    ->assertFalse($this
    ->assertSession()
    ->optionExists('float', 0)
    ->isSelected());
  $this
    ->assertTrue($this
    ->assertSession()
    ->optionExists('float', 1.5)
    ->isSelected());
  $this
    ->assertFalse($this
    ->assertSession()
    ->optionExists('float', 2)
    ->isSelected());
}