function OptionsFieldUITest::testOptionsAllowedValuesText in Zircon Profile 8
Same name and namespace in other branches
- 8.0 core/modules/options/src/Tests/OptionsFieldUITest.php \Drupal\options\Tests\OptionsFieldUITest::testOptionsAllowedValuesText()
Options (text) : test 'allowed values' input.
File
- core/
modules/ options/ src/ Tests/ OptionsFieldUITest.php, line 187 - Contains \Drupal\options\Tests\OptionsFieldUITest.
Class
- OptionsFieldUITest
- Tests the Options field UI functionality.
Namespace
Drupal\options\TestsCode
function testOptionsAllowedValuesText() {
$this->fieldName = 'field_options_text';
$this
->createOptionsField('list_string');
// Flat list of textual values.
$string = "Zero\nOne";
$array = array(
'Zero' => 'Zero',
'One' => 'One',
);
$this
->assertAllowedValuesInput($string, $array, 'Unkeyed lists are accepted.');
// Explicit keys.
$string = "zero|Zero\none|One";
$array = 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 = 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 = 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 = array(
'type' => $this->type,
$this->fieldName => array(
array(
'value' => 'One',
),
),
);
$node = $this
->drupalCreateNode($settings);
// Check that flat lists of values are still accepted once the field has
// data.
$string = "Zero\nOne";
$array = 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 = array(
'Zero' => 'Zero',
'One' => 'One',
'Two' => 'Two',
);
$this
->assertAllowedValuesInput($string, $array, 'Values can be added.');
$string = "Zero\nOne";
$array = 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 = 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 = 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 = array(
'zero' => 'One',
);
$this
->assertAllowedValuesInput($string, $array, 'Same value cannot be used multiple times.');
}