You are here

public function OptionsFieldUITest::testOptionsAllowedValuesInteger in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/options/tests/src/Functional/OptionsFieldUITest.php \Drupal\Tests\options\Functional\OptionsFieldUITest::testOptionsAllowedValuesInteger()
  2. 10 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 88

Class

OptionsFieldUITest
Tests the Options field UI functionality.

Namespace

Drupal\Tests\options\Functional

Code

public function testOptionsAllowedValuesInteger() {
  $this->fieldName = 'field_options_integer';
  $this
    ->createOptionsField('list_integer');

  // Flat list of textual values.
  $string = "Zero\nOne";
  $array = [
    '0' => 'Zero',
    '1' => 'One',
  ];
  $this
    ->assertAllowedValuesInput($string, $array, 'Unkeyed lists are accepted.');

  // Explicit integer keys.
  $string = "0|Zero\n2|Two";
  $array = [
    '0' => 'Zero',
    '2' => 'Two',
  ];
  $this
    ->assertAllowedValuesInput($string, $array, 'Integer keys are accepted.');

  // Check that values can be added and removed.
  $string = "0|Zero\n1|One";
  $array = [
    '0' => 'Zero',
    '1' => 'One',
  ];
  $this
    ->assertAllowedValuesInput($string, $array, 'Values can be added and removed.');

  // Non-integer keys.
  $this
    ->assertAllowedValuesInput("1.1|One", 'keys must be integers', 'Non integer keys are rejected.');
  $this
    ->assertAllowedValuesInput("abc|abc", 'keys must be integers', 'Non integer keys are rejected.');

  // Mixed list of keyed and unkeyed values.
  $this
    ->assertAllowedValuesInput("Zero\n1|One", 'invalid input', 'Mixed lists are rejected.');

  // Create a node with actual data for the field.
  $settings = [
    'type' => $this->type,
    $this->fieldName => [
      [
        'value' => 1,
      ],
    ],
  ];
  $node = $this
    ->drupalCreateNode($settings);

  // Check that a flat list of values is rejected once the field has data.
  $this
    ->assertAllowedValuesInput("Zero\nOne", 'invalid input', 'Unkeyed lists are rejected once the field has data.');

  // Check that values can be added but values in use cannot be removed.
  $string = "0|Zero\n1|One\n2|Two";
  $array = [
    '0' => 'Zero',
    '1' => 'One',
    '2' => 'Two',
  ];
  $this
    ->assertAllowedValuesInput($string, $array, 'Values can be added.');
  $string = "0|Zero\n1|One";
  $array = [
    '0' => 'Zero',
    '1' => 'One',
  ];
  $this
    ->assertAllowedValuesInput($string, $array, 'Values not in use can be removed.');
  $this
    ->assertAllowedValuesInput("0|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 = "0|Zero";
  $array = [
    '0' => 'Zero',
  ];
  $this
    ->assertAllowedValuesInput($string, $array, 'Values not in use can be removed.');

  // Check that the same key can only be used once.
  $string = "0|Zero\n0|One";
  $array = [
    '0' => 'One',
  ];
  $this
    ->assertAllowedValuesInput($string, $array, 'Same value cannot be used multiple times.');
}