function OptionsFieldUITest::testOptionsAllowedValuesFloat

Same name in other branches
  1. 9 core/modules/options/tests/src/Functional/OptionsFieldUITest.php \Drupal\Tests\options\Functional\OptionsFieldUITest::testOptionsAllowedValuesFloat()
  2. 8.9.x core/modules/options/tests/src/Functional/OptionsFieldUITest.php \Drupal\Tests\options\Functional\OptionsFieldUITest::testOptionsAllowedValuesFloat()
  3. 11.x core/modules/options/tests/src/Functional/OptionsFieldUITest.php \Drupal\Tests\options\Functional\OptionsFieldUITest::testOptionsAllowedValuesFloat()

Options (float) : test 'allowed values' input.

File

core/modules/options/tests/src/Functional/OptionsFieldUITest.php, line 164

Class

OptionsFieldUITest
Tests the Options field UI functionality.

Namespace

Drupal\Tests\options\Functional

Code

public function testOptionsAllowedValuesFloat() : void {
    $this->fieldName = 'field_options_float';
    $this->createOptionsField('list_float');
    $page = $this->getSession()
        ->getPage();
    $assert_session = $this->assertSession();
    // Explicit numeric keys.
    $input = [
        'field_storage[subform][settings][allowed_values][table][0][item][key]' => 0,
        'field_storage[subform][settings][allowed_values][table][0][item][label]' => 'Zero',
        'field_storage[subform][settings][allowed_values][table][1][item][key]' => 0.5,
        'field_storage[subform][settings][allowed_values][table][1][item][label]' => 'Point five',
    ];
    $array = [
        '0' => 'Zero',
        '0.5' => 'Point five',
    ];
    $this->assertAllowedValuesInput($input, $array, 'Integer keys are accepted.');
    // Check that values can be added.
    $input = [
        'field_storage[subform][settings][allowed_values][table][0][item][key]' => 0,
        'field_storage[subform][settings][allowed_values][table][0][item][label]' => 'Zero',
        'field_storage[subform][settings][allowed_values][table][1][item][key]' => 0.5,
        'field_storage[subform][settings][allowed_values][table][1][item][label]' => 'Point five',
        'field_storage[subform][settings][allowed_values][table][2][item][key]' => 1,
        'field_storage[subform][settings][allowed_values][table][2][item][label]' => 'One',
    ];
    $array = [
        '0' => 'Zero',
        '0.5' => 'Point five',
        '1' => 'One',
    ];
    $this->assertAllowedValuesInput($input, $array, 'Values can be added.');
    // Non-numeric keys.
    $input = [
        'field_storage[subform][settings][allowed_values][table][0][item][key]' => 'abc',
        'field_storage[subform][settings][allowed_values][table][0][item][label]' => 'abc',
    ];
    $this->assertAllowedValuesInput($input, 'each key must be a valid integer or decimal', 'Non numeric keys are rejected.');
    $input = [
        'field_storage[subform][settings][allowed_values][table][0][item][key]' => 0,
        'field_storage[subform][settings][allowed_values][table][0][item][label]' => 'Zero',
        'field_storage[subform][settings][allowed_values][table][1][item][key]' => 0.5,
        'field_storage[subform][settings][allowed_values][table][1][item][label]' => 'Point five',
        'field_storage[subform][settings][allowed_values][table][2][item][key]' => 2,
        'field_storage[subform][settings][allowed_values][table][2][item][label]' => 'Two',
    ];
    $array = [
        '0' => 'Zero',
        '0.5' => 'Point five',
        '2' => 'Two',
    ];
    $this->assertAllowedValuesInput($input, $array, '');
    // Create a node with actual data for the field.
    $settings = [
        'type' => $this->type,
        $this->fieldName => [
            [
                'value' => 0.5,
            ],
        ],
    ];
    $node = $this->drupalCreateNode($settings);
    // Check that the values in use cannot be removed.
    $this->drupalGet($this->adminPath);
    $assert_session->elementExists('css', '#remove_row_button__1');
    $delete_button_1 = $page->findById('remove_row_button__1');
    $this->assertTrue($delete_button_1->hasAttribute('disabled'), 'Button is disabled');
    // Delete the node, remove the value.
    $node->delete();
    $this->drupalGet($this->adminPath);
    $delete_button_1->click();
    $assert_session->pageTextNotContains('Processing...');
    $page->findById('edit-submit')
        ->click();
    $field_storage = FieldStorageConfig::loadByName('node', $this->fieldName);
    $this->assertSame($field_storage->getSetting('allowed_values'), [
        0 => 'Zero',
        2 => 'Two',
    ]);
    $input = [
        'field_storage[subform][settings][allowed_values][table][0][item][key]' => 0.5,
        'field_storage[subform][settings][allowed_values][table][0][item][label]' => 'Point five',
        'field_storage[subform][settings][allowed_values][table][1][item][key]' => 0.5,
        'field_storage[subform][settings][allowed_values][table][1][item][label]' => 'Half',
    ];
    $array = [
        '0.5' => 'Half',
    ];
    $this->assertAllowedValuesInput($input, $array, 'Same value cannot be used multiple times.');
    // Check that different forms of the same float value cannot be used.
    $input = [
        'field_storage[subform][settings][allowed_values][table][0][item][key]' => 0.5,
        'field_storage[subform][settings][allowed_values][table][0][item][label]' => 'Point five',
        'field_storage[subform][settings][allowed_values][table][1][item][key]' => 0.5,
        'field_storage[subform][settings][allowed_values][table][1][item][label]' => 'Half',
    ];
    $array = [
        '0.5' => 'Half',
    ];
    $this->assertAllowedValuesInput($input, $array, 'Different forms of the same value cannot be used.');
}

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