View source
<?php
namespace Drupal\Tests\options\Functional;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\Tests\field\Functional\FieldTestBase;
class OptionsFieldUITest extends FieldTestBase {
protected static $modules = [
'node',
'options',
'field_test',
'taxonomy',
'field_ui',
];
protected $defaultTheme = 'stark';
protected $typeName;
protected $type;
protected $fieldName;
protected $adminPath;
protected function setUp() : void {
parent::setUp();
$admin_user = $this
->drupalCreateUser([
'access content',
'administer taxonomy',
'access administration pages',
'administer site configuration',
'administer content types',
'administer nodes',
'bypass node access',
'administer node fields',
'administer node display',
]);
$this
->drupalLogin($admin_user);
$this->typeName = 'test_' . strtolower($this
->randomMachineName());
$type = $this
->drupalCreateContentType([
'name' => $this->typeName,
'type' => $this->typeName,
]);
$this->type = $type
->id();
}
public function testOptionsAllowedValuesInteger() {
$this->fieldName = 'field_options_integer';
$this
->createOptionsField('list_integer');
$string = "Zero\nOne";
$array = [
'0' => 'Zero',
'1' => 'One',
];
$this
->assertAllowedValuesInput($string, $array, 'Unkeyed lists are accepted.');
$string = "0|Zero\n2|Two";
$array = [
'0' => 'Zero',
'2' => 'Two',
];
$this
->assertAllowedValuesInput($string, $array, 'Integer keys are accepted.');
$string = "0|Zero\n1|One";
$array = [
'0' => 'Zero',
'1' => 'One',
];
$this
->assertAllowedValuesInput($string, $array, 'Values can be added and removed.');
$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.');
$this
->assertAllowedValuesInput("Zero\n1|One", 'invalid input', 'Mixed lists are rejected.');
$settings = [
'type' => $this->type,
$this->fieldName => [
[
'value' => 1,
],
],
];
$node = $this
->drupalCreateNode($settings);
$this
->assertAllowedValuesInput("Zero\nOne", 'invalid input', 'Unkeyed lists are rejected once the field has data.');
$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.');
$node
->delete();
$string = "0|Zero";
$array = [
'0' => 'Zero',
];
$this
->assertAllowedValuesInput($string, $array, 'Values not in use can be removed.');
$string = "0|Zero\n0|One";
$array = [
'0' => 'One',
];
$this
->assertAllowedValuesInput($string, $array, 'Same value cannot be used multiple times.');
}
public function testOptionsAllowedValuesFloat() {
$this->fieldName = 'field_options_float';
$this
->createOptionsField('list_float');
$string = "Zero\nOne";
$array = [
'0' => 'Zero',
'1' => 'One',
];
$this
->assertAllowedValuesInput($string, $array, 'Unkeyed lists are accepted.');
$string = "0|Zero\n.5|Point five";
$array = [
'0' => 'Zero',
'0.5' => 'Point five',
];
$this
->assertAllowedValuesInput($string, $array, 'Integer keys are accepted.');
$string = "0|Zero\n.5|Point five\n1.0|One";
$array = [
'0' => 'Zero',
'0.5' => 'Point five',
'1' => 'One',
];
$this
->assertAllowedValuesInput($string, $array, 'Values can be added and removed.');
$this
->assertAllowedValuesInput("abc|abc\n", 'each key must be a valid integer or decimal', 'Non numeric keys are rejected.');
$this
->assertAllowedValuesInput("Zero\n1|One\n", 'invalid input', 'Mixed lists are rejected.');
$settings = [
'type' => $this->type,
$this->fieldName => [
[
'value' => 0.5,
],
],
];
$node = $this
->drupalCreateNode($settings);
$this
->assertAllowedValuesInput("Zero\nOne", 'invalid input', 'Unkeyed lists are rejected once the field has data.');
$string = "0|Zero\n.5|Point five\n2|Two";
$array = [
'0' => 'Zero',
'0.5' => 'Point five',
'2' => 'Two',
];
$this
->assertAllowedValuesInput($string, $array, 'Values can be added.');
$string = "0|Zero\n.5|Point five";
$array = [
'0' => 'Zero',
'0.5' => 'Point five',
];
$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.');
$node
->delete();
$string = "0|Zero";
$array = [
'0' => 'Zero',
];
$this
->assertAllowedValuesInput($string, $array, 'Values not in use can be removed.');
$string = "0.5|Point five\n0.5|Half";
$array = [
'0.5' => 'Half',
];
$this
->assertAllowedValuesInput($string, $array, 'Same value cannot be used multiple times.');
$string = "0|Zero\n.5|Point five\n0.5|Half";
$array = [
'0' => 'Zero',
'0.5' => 'Half',
];
$this
->assertAllowedValuesInput($string, $array, 'Different forms of the same value cannot be used.');
}
public function testOptionsAllowedValuesText() {
$this->fieldName = 'field_options_text';
$this
->createOptionsField('list_string');
$string = "Zero\nOne";
$array = [
'Zero' => 'Zero',
'One' => 'One',
];
$this
->assertAllowedValuesInput($string, $array, 'Unkeyed lists are accepted.');
$string = "zero|Zero\none|One";
$array = [
'zero' => 'Zero',
'one' => 'One',
];
$this
->assertAllowedValuesInput($string, $array, 'Explicit keys are accepted.');
$string = "zero|Zero\ntwo|Two";
$array = [
'zero' => 'Zero',
'two' => 'Two',
];
$this
->assertAllowedValuesInput($string, $array, 'Values can be added and removed.');
$string = "zero|Zero\nOne\n";
$array = [
'zero' => 'Zero',
'One' => 'One',
];
$this
->assertAllowedValuesInput($string, $array, 'Mixed lists are accepted.');
$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.');
$settings = [
'type' => $this->type,
$this->fieldName => [
[
'value' => 'One',
],
],
];
$node = $this
->drupalCreateNode($settings);
$string = "Zero\nOne";
$array = [
'Zero' => 'Zero',
'One' => 'One',
];
$this
->assertAllowedValuesInput($string, $array, 'Unkeyed lists are still accepted once the field has data.');
$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.');
$node
->delete();
$string = "Zero";
$array = [
'Zero' => 'Zero',
];
$this
->assertAllowedValuesInput($string, $array, 'Values not in use can be removed.');
$string = "Zero\nexample.com|Example";
$array = [
'Zero' => 'Zero',
'example.com' => 'Example',
];
$this
->assertAllowedValuesInput($string, $array, 'String value with dot is supported.');
$string = "zero|Zero\nzero|One";
$array = [
'zero' => 'One',
];
$this
->assertAllowedValuesInput($string, $array, 'Same value cannot be used multiple times.');
}
public function testOptionsTrimmedValuesText() {
$this->fieldName = 'field_options_trimmed_text';
$this
->createOptionsField('list_string');
$string = "zero |Zero\none | One";
$array = [
'zero' => 'Zero',
'one' => 'One',
];
$this
->assertAllowedValuesInput($string, $array, 'Explicit keys are accepted and trimmed.');
}
protected function createOptionsField($type) {
FieldStorageConfig::create([
'field_name' => $this->fieldName,
'entity_type' => 'node',
'type' => $type,
])
->save();
FieldConfig::create([
'field_name' => $this->fieldName,
'entity_type' => 'node',
'bundle' => $this->type,
])
->save();
\Drupal::service('entity_display.repository')
->getFormDisplay('node', $this->type)
->setComponent($this->fieldName)
->save();
$this->adminPath = 'admin/structure/types/manage/' . $this->type . '/fields/node.' . $this->type . '.' . $this->fieldName . '/storage';
}
public function assertAllowedValuesInput(string $input_string, $result, string $message) : void {
$edit = [
'settings[allowed_values]' => $input_string,
];
$this
->drupalGet($this->adminPath);
$this
->submitForm($edit, 'Save field settings');
$this
->assertSession()
->responseNotContains('&lt;');
if (is_string($result)) {
$this
->assertSession()
->pageTextContains($result);
}
else {
$field_storage = FieldStorageConfig::loadByName('node', $this->fieldName);
$this
->assertSame($field_storage
->getSetting('allowed_values'), $result, $message);
}
}
public function testNodeDisplay() {
$this->fieldName = strtolower($this
->randomMachineName());
$this
->createOptionsField('list_integer');
$node = $this
->drupalCreateNode([
'type' => $this->type,
]);
$on = $this
->randomMachineName();
$off = $this
->randomMachineName();
$edit = [
'settings[allowed_values]' => "1|{$on}\n 0|{$off}",
];
$this
->drupalGet($this->adminPath);
$this
->submitForm($edit, 'Save field settings');
$this
->assertSession()
->pageTextContains('Updated field ' . $this->fieldName . ' field settings.');
$edit = [
$this->fieldName => '1',
];
$this
->drupalGet('node/' . $node
->id() . '/edit');
$this
->submitForm($edit, 'Save');
$file_formatters = [
'list_default',
'list_key',
];
foreach ($file_formatters as $formatter) {
$edit = [
"fields[{$this->fieldName}][type]" => $formatter,
"fields[{$this->fieldName}][region]" => 'content',
];
$this
->drupalGet('admin/structure/types/manage/' . $this->typeName . '/display');
$this
->submitForm($edit, 'Save');
$this
->drupalGet('node/' . $node
->id());
if ($formatter == 'list_default') {
$output = $on;
}
else {
$output = '1';
}
$this
->assertSession()
->elementsCount('xpath', '//div[text()="' . $output . '"]', 1);
}
}
}