View source
<?php
namespace Drupal\Tests\options\Functional;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\Tests\field\Functional\FieldTestBase;
class OptionsWidgetsTest extends FieldTestBase {
protected static $modules = [
'node',
'options',
'entity_test',
'options_test',
'taxonomy',
'field_ui',
];
protected $defaultTheme = 'stark';
protected $card1;
protected $card2;
protected $float;
protected function setUp() : void {
parent::setUp();
$this->card1 = FieldStorageConfig::create([
'field_name' => 'card_1',
'entity_type' => 'entity_test',
'type' => 'list_integer',
'cardinality' => 1,
'settings' => [
'allowed_values' => [
0 => 'Zero',
1 => 'One',
2 => 'Some <script>dangerous</script> & unescaped <strong>markup</strong>',
3 => 'Some HTML encoded markup with < & >',
],
],
]);
$this->card1
->save();
$this->card2 = FieldStorageConfig::create([
'field_name' => 'card_2',
'entity_type' => 'entity_test',
'type' => 'list_integer',
'cardinality' => 2,
'settings' => [
'allowed_values' => [
0 => 'Zero',
1 => 'One',
2 => 'Some <script>dangerous</script> & unescaped <strong>markup</strong>',
],
],
]);
$this->card2
->save();
$this->float = FieldStorageConfig::create([
'field_name' => 'float',
'entity_type' => 'entity_test',
'type' => 'list_float',
'cardinality' => 1,
'settings' => [
'allowed_values' => [
'0.0' => '0.0',
'1.5' => '1.5',
'2.0' => '2.0',
],
],
]);
$this->float
->save();
$this
->drupalLogin($this
->drupalCreateUser([
'view test entity',
'administer entity_test content',
]));
}
public function testRadioButtons() {
$field = FieldConfig::create([
'field_storage' => $this->card1,
'bundle' => 'entity_test',
]);
$field
->save();
\Drupal::service('entity_display.repository')
->getFormDisplay('entity_test', 'entity_test')
->setComponent($this->card1
->getName(), [
'type' => 'options_buttons',
])
->save();
$entity = EntityTest::create([
'user_id' => 1,
'name' => $this
->randomMachineName(),
]);
$entity
->save();
$entity_init = clone $entity;
$this
->drupalGet('entity_test/manage/' . $entity
->id() . '/edit');
$this
->assertSession()
->checkboxNotChecked('edit-card-1-0');
$this
->assertSession()
->checkboxNotChecked('edit-card-1-1');
$this
->assertSession()
->checkboxNotChecked('edit-card-1-2');
$this
->assertSession()
->responseContains('Some dangerous & unescaped <strong>markup</strong>');
$this
->assertSession()
->responseContains('Some HTML encoded markup with < & >');
$edit = [
'card_1' => 0,
];
$this
->submitForm($edit, 'Save');
$this
->assertFieldValues($entity_init, 'card_1', [
0,
]);
$this
->drupalGet('entity_test/manage/' . $entity
->id() . '/edit');
$this
->assertSession()
->checkboxChecked('edit-card-1-0');
$this
->assertSession()
->checkboxNotChecked('edit-card-1-1');
$this
->assertSession()
->checkboxNotChecked('edit-card-1-2');
$edit = [
'card_1' => '_none',
];
$this
->submitForm($edit, 'Save');
$this
->assertFieldValues($entity_init, 'card_1', []);
$this->card1
->setSetting('allowed_values', [
99 => 'Only allowed value',
]);
$this->card1
->save();
$field
->setRequired(TRUE);
$field
->save();
$this
->drupalGet('entity_test/manage/' . $entity
->id() . '/edit');
$this
->assertSession()
->checkboxChecked('edit-card-1-99');
}
public function testCheckBoxes() {
$field = FieldConfig::create([
'field_storage' => $this->card2,
'bundle' => 'entity_test',
]);
$field
->save();
\Drupal::service('entity_display.repository')
->getFormDisplay('entity_test', 'entity_test')
->setComponent($this->card2
->getName(), [
'type' => 'options_buttons',
])
->save();
$entity = EntityTest::create([
'user_id' => 1,
'name' => $this
->randomMachineName(),
]);
$entity
->save();
$entity_init = clone $entity;
$this
->drupalGet('entity_test/manage/' . $entity
->id() . '/edit');
$this
->assertSession()
->checkboxNotChecked('edit-card-2-0');
$this
->assertSession()
->checkboxNotChecked('edit-card-2-1');
$this
->assertSession()
->checkboxNotChecked('edit-card-2-2');
$this
->assertSession()
->responseContains('Some dangerous & unescaped <strong>markup</strong>');
$edit = [
'card_2[0]' => TRUE,
'card_2[1]' => FALSE,
'card_2[2]' => TRUE,
];
$this
->submitForm($edit, 'Save');
$this
->assertFieldValues($entity_init, 'card_2', [
0,
2,
]);
$this
->drupalGet('entity_test/manage/' . $entity
->id() . '/edit');
$this
->assertSession()
->checkboxChecked('edit-card-2-0');
$this
->assertSession()
->checkboxNotChecked('edit-card-2-1');
$this
->assertSession()
->checkboxChecked('edit-card-2-2');
$edit = [
'card_2[0]' => TRUE,
'card_2[1]' => FALSE,
'card_2[2]' => FALSE,
];
$this
->submitForm($edit, 'Save');
$this
->assertFieldValues($entity_init, 'card_2', [
0,
]);
$this
->drupalGet('entity_test/manage/' . $entity
->id() . '/edit');
$this
->assertSession()
->checkboxChecked('edit-card-2-0');
$this
->assertSession()
->checkboxNotChecked('edit-card-2-1');
$this
->assertSession()
->checkboxNotChecked('edit-card-2-2');
$edit = [
'card_2[0]' => TRUE,
'card_2[1]' => TRUE,
'card_2[2]' => TRUE,
];
$this
->submitForm($edit, 'Save');
$this
->assertSession()
->pageTextContains('this field cannot hold more than 2 values');
$edit = [
'card_2[0]' => FALSE,
'card_2[1]' => FALSE,
'card_2[2]' => FALSE,
];
$this
->submitForm($edit, 'Save');
$this
->assertFieldValues($entity_init, 'card_2', []);
$this->card2
->setSetting('allowed_values', [
99 => 'Only allowed value',
]);
$this->card2
->save();
$field
->setRequired(TRUE);
$field
->save();
$this
->drupalGet('entity_test/manage/' . $entity
->id() . '/edit');
$this
->assertSession()
->checkboxChecked('edit-card-2-99');
}
public function testSelectListSingle() {
$field = FieldConfig::create([
'field_storage' => $this->card1,
'bundle' => 'entity_test',
'required' => TRUE,
]);
$field
->save();
\Drupal::service('entity_display.repository')
->getFormDisplay('entity_test', 'entity_test')
->setComponent($this->card1
->getName(), [
'type' => 'options_select',
])
->save();
$entity = EntityTest::create([
'user_id' => 1,
'name' => $this
->randomMachineName(),
]);
$entity
->save();
$entity_init = clone $entity;
$this
->drupalGet('entity_test/manage/' . $entity
->id() . '/edit');
$option = $this
->assertSession()
->optionExists('edit-card-1', '_none');
$this
->assertSame('- Select a value -', $option
->getText());
$this
->assertTrue($this
->assertSession()
->optionExists('card_1', '_none')
->isSelected());
$this
->assertFalse($this
->assertSession()
->optionExists('card_1', 0)
->isSelected());
$this
->assertFalse($this
->assertSession()
->optionExists('card_1', 1)
->isSelected());
$this
->assertFalse($this
->assertSession()
->optionExists('card_1', 2)
->isSelected());
$this
->assertSession()
->responseContains('Some dangerous & unescaped markup');
$edit = [
'card_1' => '_none',
];
$this
->submitForm($edit, 'Save');
$this
->assertSession()
->pageTextContains("{$field->getName()} field is required.");
$edit = [
'card_1' => 0,
];
$this
->submitForm($edit, 'Save');
$this
->assertFieldValues($entity_init, 'card_1', [
0,
]);
$this
->drupalGet('entity_test/manage/' . $entity
->id() . '/edit');
$this
->assertSession()
->optionNotExists('edit-card-1', '_none');
$this
->assertTrue($this
->assertSession()
->optionExists('card_1', 0)
->isSelected());
$this
->assertFalse($this
->assertSession()
->optionExists('card_1', 1)
->isSelected());
$this
->assertFalse($this
->assertSession()
->optionExists('card_1', 2)
->isSelected());
$field
->setRequired(FALSE);
$field
->save();
$this
->drupalGet('entity_test/manage/' . $entity
->id() . '/edit');
$option = $this
->assertSession()
->optionExists('edit-card-1', '_none');
$this
->assertSame('- None -', $option
->getText());
$edit = [
'card_1' => '_none',
];
$this
->drupalGet('entity_test/manage/' . $entity
->id() . '/edit');
$this
->submitForm($edit, 'Save');
$this
->assertFieldValues($entity_init, 'card_1', []);
$this->card1
->setSetting('allowed_values', []);
$this->card1
->setSetting('allowed_values_function', 'options_test_allowed_values_callback');
$this->card1
->save();
$this
->drupalGet('entity_test/manage/' . $entity
->id() . '/edit');
$this
->assertFalse($this
->assertSession()
->optionExists('card_1', 0)
->isSelected());
$this
->assertFalse($this
->assertSession()
->optionExists('card_1', 1)
->isSelected());
$this
->assertFalse($this
->assertSession()
->optionExists('card_1', 2)
->isSelected());
$this
->assertSession()
->responseContains('Some dangerous & unescaped markup');
$this
->assertSession()
->responseContains('More <script>dangerous</script> markup');
$this
->assertSession()
->responseContains('Group 1');
$edit = [
'card_1' => 0,
];
$this
->submitForm($edit, 'Save');
$this
->assertFieldValues($entity_init, 'card_1', [
0,
]);
$this
->drupalGet('entity_test/manage/' . $entity
->id() . '/edit');
$this
->assertTrue($this
->assertSession()
->optionExists('card_1', 0)
->isSelected());
$this
->assertFalse($this
->assertSession()
->optionExists('card_1', 1)
->isSelected());
$this
->assertFalse($this
->assertSession()
->optionExists('card_1', 2)
->isSelected());
$edit = [
'card_1' => '_none',
];
$this
->drupalGet('entity_test/manage/' . $entity
->id() . '/edit');
$this
->submitForm($edit, 'Save');
$this
->assertFieldValues($entity_init, 'card_1', []);
}
public function testSelectListMultiple() {
$field = FieldConfig::create([
'field_storage' => $this->card2,
'bundle' => 'entity_test',
]);
$field
->save();
\Drupal::service('entity_display.repository')
->getFormDisplay('entity_test', 'entity_test')
->setComponent($this->card2
->getName(), [
'type' => 'options_select',
])
->save();
$entity = EntityTest::create([
'user_id' => 1,
'name' => $this
->randomMachineName(),
]);
$entity
->save();
$entity_init = clone $entity;
$this
->drupalGet('entity_test/manage/' . $entity
->id() . '/edit');
$this
->assertTrue($this
->assertSession()
->optionExists('card_2', '_none')
->isSelected());
$this
->assertFalse($this
->assertSession()
->optionExists('card_2', 0)
->isSelected());
$this
->assertFalse($this
->assertSession()
->optionExists('card_2', 1)
->isSelected());
$this
->assertFalse($this
->assertSession()
->optionExists('card_2', 2)
->isSelected());
$this
->assertSession()
->responseContains('Some dangerous & unescaped markup');
$edit = [
'card_2[]' => [
0 => 0,
2 => 2,
],
];
$this
->submitForm($edit, 'Save');
$this
->assertFieldValues($entity_init, 'card_2', [
0,
2,
]);
$this
->drupalGet('entity_test/manage/' . $entity
->id() . '/edit');
$this
->assertTrue($this
->assertSession()
->optionExists('card_2', 0)
->isSelected());
$this
->assertFalse($this
->assertSession()
->optionExists('card_2', 1)
->isSelected());
$this
->assertTrue($this
->assertSession()
->optionExists('card_2', 2)
->isSelected());
$edit = [
'card_2[]' => [
0 => 0,
],
];
$this
->submitForm($edit, 'Save');
$this
->assertFieldValues($entity_init, 'card_2', [
0,
]);
$this
->drupalGet('entity_test/manage/' . $entity
->id() . '/edit');
$this
->assertTrue($this
->assertSession()
->optionExists('card_2', 0)
->isSelected());
$this
->assertFalse($this
->assertSession()
->optionExists('card_2', 1)
->isSelected());
$this
->assertFalse($this
->assertSession()
->optionExists('card_2', 2)
->isSelected());
$edit = [
'card_2[]' => [
0 => 0,
1 => 1,
2 => 2,
],
];
$this
->submitForm($edit, 'Save');
$this
->assertSession()
->pageTextContains('this field cannot hold more than 2 values');
$edit = [
'card_2[]' => [],
];
$this
->submitForm($edit, 'Save');
$this
->assertFieldValues($entity_init, 'card_2', []);
$edit = [
'card_2[]' => [
'_none' => '_none',
0 => 0,
],
];
$this
->drupalGet('entity_test/manage/' . $entity
->id() . '/edit');
$this
->submitForm($edit, 'Save');
$this
->assertFieldValues($entity_init, 'card_2', [
0,
]);
$edit = [
'card_2[]' => [
'_none' => '_none',
],
];
$this
->drupalGet('entity_test/manage/' . $entity
->id() . '/edit');
$this
->submitForm($edit, 'Save');
$this
->assertFieldValues($entity_init, 'card_2', []);
$field
->setRequired(TRUE);
$field
->save();
$this
->drupalGet('entity_test/manage/' . $entity
->id() . '/edit');
$this
->assertEmpty($this
->assertSession()
->selectExists('edit-card-2')
->find('xpath', 'option[@value=""]'));
$this->card2
->setSetting('allowed_values', []);
$this->card2
->setSetting('allowed_values_function', 'options_test_allowed_values_callback');
$this->card2
->save();
$field
->setRequired(FALSE);
$field
->save();
$this
->drupalGet('entity_test/manage/' . $entity
->id() . '/edit');
$this
->assertFalse($this
->assertSession()
->optionExists('card_2', 0)
->isSelected());
$this
->assertFalse($this
->assertSession()
->optionExists('card_2', 1)
->isSelected());
$this
->assertFalse($this
->assertSession()
->optionExists('card_2', 2)
->isSelected());
$this
->assertSession()
->responseContains('Some dangerous & unescaped markup');
$this
->assertSession()
->responseContains('More <script>dangerous</script> markup');
$this
->assertSession()
->responseContains('Group 1');
$edit = [
'card_2[]' => [
0 => 0,
],
];
$this
->submitForm($edit, 'Save');
$this
->assertFieldValues($entity_init, 'card_2', [
0,
]);
$this
->drupalGet('entity_test/manage/' . $entity
->id() . '/edit');
$this
->assertTrue($this
->assertSession()
->optionExists('card_2', 0)
->isSelected());
$this
->assertFalse($this
->assertSession()
->optionExists('card_2', 1)
->isSelected());
$this
->assertFalse($this
->assertSession()
->optionExists('card_2', 2)
->isSelected());
$edit = [
'card_2[]' => [
'_none' => '_none',
],
];
$this
->drupalGet('entity_test/manage/' . $entity
->id() . '/edit');
$this
->submitForm($edit, 'Save');
$this
->assertFieldValues($entity_init, 'card_2', []);
}
public function testSelectListFloat() {
$field = FieldConfig::create([
'field_storage' => $this->float,
'bundle' => 'entity_test',
'required' => TRUE,
]);
$field
->save();
$this->container
->get('entity_type.manager')
->getStorage('entity_form_display')
->load('entity_test.entity_test.default')
->setComponent($this->float
->getName(), [
'type' => 'options_select',
])
->save();
$entity = EntityTest::create([
'user_id' => 1,
'name' => $this
->randomMachineName(),
]);
$entity
->save();
$this
->drupalGet('entity_test/manage/' . $entity
->id() . '/edit');
$this
->assertFalse($this
->assertSession()
->optionExists('float', 0)
->isSelected());
$this
->assertFalse($this
->assertSession()
->optionExists('float', 1.5)
->isSelected());
$this
->assertFalse($this
->assertSession()
->optionExists('float', 2)
->isSelected());
$edit = [
'float' => 1.5,
];
$this
->submitForm($edit, 'Save');
$this
->assertFieldValues($entity, 'float', [
1.5,
]);
$this
->drupalGet('entity_test/manage/' . $entity
->id() . '/edit');
$this
->assertFalse($this
->assertSession()
->optionExists('float', 0)
->isSelected());
$this
->assertTrue($this
->assertSession()
->optionExists('float', 1.5)
->isSelected());
$this
->assertFalse($this
->assertSession()
->optionExists('float', 2)
->isSelected());
}
public function testEmptyValue() {
$field = FieldConfig::create([
'field_storage' => $this->card1,
'bundle' => 'entity_test',
]);
$field
->save();
$display_repository = \Drupal::service('entity_display.repository');
$display_repository
->getFormDisplay('entity_test', 'entity_test')
->setComponent($this->card1
->getName(), [
'type' => 'options_buttons',
])
->save();
$entity = EntityTest::create([
'user_id' => 1,
'name' => $this
->randomMachineName(),
]);
$entity
->save();
$this
->drupalGet('entity_test/manage/' . $entity
->id() . '/edit');
$this
->assertSession()
->elementExists('xpath', '//div[@id="edit-card-1"]//input[@value="_none"]');
$this
->assertSession()
->elementExists('xpath', '//div[@id="edit-card-1"]//label[@for="edit-card-1-none"]');
$this
->assertSession()
->elementTextEquals('xpath', '//div[@id="edit-card-1"]//label[@for="edit-card-1-none"]', "N/A");
$display_repository
->getFormDisplay('entity_test', 'entity_test')
->setComponent($this->card1
->getName(), [
'type' => 'options_select',
])
->save();
$this
->drupalGet('entity_test/manage/' . $entity
->id() . '/edit');
$option = $this
->assertSession()
->optionExists('edit-card-1', '_none');
$this
->assertSame('- None -', $option
->getText());
}
}