function OptionsFieldUITest::testOptionsAllowedValuesText

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

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

File

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

Class

OptionsFieldUITest
Tests the Options field UI functionality.

Namespace

Drupal\Tests\options\Functional

Code

public function testOptionsAllowedValuesText() : void {
    $this->fieldName = 'field_options_text';
    $this->createOptionsField('list_string');
    $page = $this->getSession()
        ->getPage();
    $assert_session = $this->assertSession();
    // Explicit keys.
    $input = [
        'field_storage[subform][settings][allowed_values][table][0][item][key]' => '_zero',
        'field_storage[subform][settings][allowed_values][table][0][item][label]' => 'Zero',
        'field_storage[subform][settings][allowed_values][table][1][item][key]' => '_one',
        'field_storage[subform][settings][allowed_values][table][1][item][label]' => 'One',
    ];
    $array = [
        '_zero' => 'Zero',
        '_one' => 'One',
    ];
    $this->assertAllowedValuesInput($input, $array, 'Explicit keys are accepted.');
    // Overly long keys.
    $input = [
        'field_storage[subform][settings][allowed_values][table][0][item][key]' => 'zero',
        'field_storage[subform][settings][allowed_values][table][0][item][label]' => 'Zero',
        'field_storage[subform][settings][allowed_values][table][1][item][key]' => $this->randomMachineName(256),
        'field_storage[subform][settings][allowed_values][table][1][item][label]' => 'One',
    ];
    $this->assertAllowedValuesInput($input, 'each key must be a string at most 255 characters long', 'Overly long keys are rejected.');
    $input = [
        'field_storage[subform][settings][allowed_values][table][0][item][key]' => 'zero',
        'field_storage[subform][settings][allowed_values][table][0][item][label]' => 'Zero',
        'field_storage[subform][settings][allowed_values][table][1][item][key]' => 'one',
        'field_storage[subform][settings][allowed_values][table][1][item][label]' => 'One',
    ];
    $array = [
        'zero' => 'Zero',
        'one' => 'One',
    ];
    $this->assertAllowedValuesInput($input, $array, '');
    // Create a node with actual data for the field.
    $settings = [
        'type' => $this->type,
        $this->fieldName => [
            [
                'value' => 'one',
            ],
        ],
    ];
    $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');
    $value_field_1 = $page->findField('field_storage[subform][settings][allowed_values][table][1][item][key]');
    $this->assertTrue($delete_button_1->hasAttribute('disabled'), 'Button is disabled');
    $this->assertTrue($value_field_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'), [
        'zero' => 'Zero',
    ]);
    // Check that string values with special characters can be used.
    $input = [
        'field_storage[subform][settings][allowed_values][table][0][item][key]' => 'zero',
        'field_storage[subform][settings][allowed_values][table][0][item][label]' => 'Zero',
        'field_storage[subform][settings][allowed_values][table][1][item][key]' => '.example #example',
        'field_storage[subform][settings][allowed_values][table][1][item][label]' => 'Example',
    ];
    $array = [
        'zero' => 'Zero',
        '.example #example' => 'Example',
    ];
    $this->assertAllowedValuesInput($input, $array, '');
    // Check that the same key can only be used once.
    $input = [
        'field_storage[subform][settings][allowed_values][table][0][item][key]' => 'zero',
        'field_storage[subform][settings][allowed_values][table][0][item][label]' => 'Zero',
        'field_storage[subform][settings][allowed_values][table][1][item][key]' => 'zero',
        'field_storage[subform][settings][allowed_values][table][1][item][label]' => 'One',
    ];
    $array = [
        'zero' => '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.