function OptionsFieldUITest::testOptionsAllowedValuesInteger

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

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

File

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

Class

OptionsFieldUITest
Tests the Options field UI functionality.

Namespace

Drupal\Tests\options\Functional

Code

public function testOptionsAllowedValuesInteger() : void {
    $this->fieldName = 'field_options_integer';
    $this->createOptionsField('list_integer');
    $page = $this->getSession()
        ->getPage();
    $assert_session = $this->assertSession();
    // Explicit integer 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]' => 2,
        'field_storage[subform][settings][allowed_values][table][1][item][label]' => 'Two',
    ];
    $array = [
        0 => 'Zero',
        2 => 'Two',
    ];
    $this->assertAllowedValuesInput($input, $array, 'Integer keys are accepted.');
    // Non-integer keys.
    $input = [
        'field_storage[subform][settings][allowed_values][table][0][item][key]' => 1.1,
        'field_storage[subform][settings][allowed_values][table][0][item][label]' => 'One',
    ];
    $this->assertAllowedValuesInput($input, 'keys must be integers', 'Non integer keys are rejected.');
    $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, 'keys must be integers', 'Non integer 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]' => 1,
        'field_storage[subform][settings][allowed_values][table][1][item][label]' => 'One',
    ];
    $array = [
        0 => 'Zero',
        1 => 'One',
    ];
    $this->assertAllowedValuesInput($input, $array, '');
    // Create a node with actual data for the field.
    $settings = [
        'type' => $this->type,
        $this->fieldName => [
            [
                'value' => 1,
            ],
        ],
    ];
    $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',
    ]);
    // Check that the same key can only be used once.
    $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,
        'field_storage[subform][settings][allowed_values][table][1][item][label]' => 'One',
    ];
    $array = [
        '0' => 'One',
    ];
    $this->assertAllowedValuesInput($input, $array, 'Same value cannot be used multiple times.');
}

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