function OptionsFieldUITest::testOptionsAllowedValuesText

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

Class

OptionsFieldUITest
Tests the Options field UI functionality.

Namespace

Drupal\Tests\options\Functional

Code

public function testOptionsAllowedValuesText() {
    $this->fieldName = 'field_options_text';
    $this->createOptionsField('list_string');
    // Flat list of textual values.
    $string = "Zero\nOne";
    $array = [
        'Zero' => 'Zero',
        'One' => 'One',
    ];
    $this->assertAllowedValuesInput($string, $array, 'Unkeyed lists are accepted.');
    // Explicit keys.
    $string = "zero|Zero\none|One";
    $array = [
        'zero' => 'Zero',
        'one' => 'One',
    ];
    $this->assertAllowedValuesInput($string, $array, 'Explicit keys are accepted.');
    // Check that values can be added and removed.
    $string = "zero|Zero\ntwo|Two";
    $array = [
        'zero' => 'Zero',
        'two' => 'Two',
    ];
    $this->assertAllowedValuesInput($string, $array, 'Values can be added and removed.');
    // Mixed list of keyed and unkeyed values.
    $string = "zero|Zero\nOne\n";
    $array = [
        'zero' => 'Zero',
        'One' => 'One',
    ];
    $this->assertAllowedValuesInput($string, $array, 'Mixed lists are accepted.');
    // Overly long keys.
    $this->assertAllowedValuesInput("zero|Zero\n" . $this->randomMachineName(256) . "|One", 'each key must be a string at most 255 characters long', 'Overly long keys are rejected.');
    // Create a node with actual data for the field.
    $settings = [
        'type' => $this->type,
        $this->fieldName => [
            [
                'value' => 'One',
            ],
        ],
    ];
    $node = $this->drupalCreateNode($settings);
    // Check that flat lists of values are still accepted once the field has
    // data.
    $string = "Zero\nOne";
    $array = [
        'Zero' => 'Zero',
        'One' => 'One',
    ];
    $this->assertAllowedValuesInput($string, $array, 'Unkeyed lists are still accepted once the field has data.');
    // Check that values can be added but values in use cannot be removed.
    $string = "Zero\nOne\nTwo";
    $array = [
        'Zero' => 'Zero',
        'One' => 'One',
        'Two' => 'Two',
    ];
    $this->assertAllowedValuesInput($string, $array, 'Values can be added.');
    $string = "Zero\nOne";
    $array = [
        'Zero' => 'Zero',
        'One' => 'One',
    ];
    $this->assertAllowedValuesInput($string, $array, 'Values not in use can be removed.');
    $this->assertAllowedValuesInput("Zero", 'some values are being removed while currently in use', 'Values in use cannot be removed.');
    // Delete the node, remove the value.
    $node->delete();
    $string = "Zero";
    $array = [
        'Zero' => 'Zero',
    ];
    $this->assertAllowedValuesInput($string, $array, 'Values not in use can be removed.');
    // Check that string values with dots can be used.
    $string = "Zero\nexample.com|Example";
    $array = [
        'Zero' => 'Zero',
        'example.com' => 'Example',
    ];
    $this->assertAllowedValuesInput($string, $array, 'String value with dot is supported.');
    // Check that the same key can only be used once.
    $string = "zero|Zero\nzero|One";
    $array = [
        'zero' => 'One',
    ];
    $this->assertAllowedValuesInput($string, $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.