function OptionsWidgetsTest::testSelectListFloat

Same name in other branches
  1. 8.9.x core/modules/options/tests/src/Functional/OptionsWidgetsTest.php \Drupal\Tests\options\Functional\OptionsWidgetsTest::testSelectListFloat()
  2. 10 core/modules/options/tests/src/Functional/OptionsWidgetsTest.php \Drupal\Tests\options\Functional\OptionsWidgetsTest::testSelectListFloat()
  3. 11.x 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 505

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->submitForm($edit, '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());
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.