public function EntityAutocompleteElementFormTest::testValidEntityAutocompleteElement in Drupal 9
Same name and namespace in other branches
- 8 core/tests/Drupal/KernelTests/Core/Entity/Element/EntityAutocompleteElementFormTest.php \Drupal\KernelTests\Core\Entity\Element\EntityAutocompleteElementFormTest::testValidEntityAutocompleteElement()
- 10 core/tests/Drupal/KernelTests/Core/Entity/Element/EntityAutocompleteElementFormTest.php \Drupal\KernelTests\Core\Entity\Element\EntityAutocompleteElementFormTest::testValidEntityAutocompleteElement()
Tests valid entries in the EntityAutocomplete Form API element.
File
- core/
tests/ Drupal/ KernelTests/ Core/ Entity/ Element/ EntityAutocompleteElementFormTest.php, line 203
Class
- EntityAutocompleteElementFormTest
- Tests the EntityAutocomplete Form API element.
Namespace
Drupal\KernelTests\Core\Entity\ElementCode
public function testValidEntityAutocompleteElement() {
$form_state = (new FormState())
->setValues([
'single' => $this
->getAutocompleteInput($this->referencedEntities[0]),
'single_autocreate' => 'single - autocreated entity label',
'single_autocreate_specific_uid' => 'single - autocreated entity label with specific uid',
'tags' => $this
->getAutocompleteInput($this->referencedEntities[0]) . ', ' . $this
->getAutocompleteInput($this->referencedEntities[1]),
'tags_autocreate' => $this
->getAutocompleteInput($this->referencedEntities[0]) . ', tags - autocreated entity label, ' . $this
->getAutocompleteInput($this->referencedEntities[1]),
'tags_autocreate_specific_uid' => $this
->getAutocompleteInput($this->referencedEntities[0]) . ', tags - autocreated entity label with specific uid, ' . $this
->getAutocompleteInput($this->referencedEntities[1]),
'single_string_id' => $this
->getAutocompleteInput($this->referencedEntities[2]),
'tags_string_id' => $this
->getAutocompleteInput($this->referencedEntities[2]) . ', ' . $this
->getAutocompleteInput($this->referencedEntities[3]),
]);
$form_builder = $this->container
->get('form_builder');
$form_builder
->submitForm($this, $form_state);
// Valid form state.
$this
->assertCount(0, $form_state
->getErrors());
// Test the 'single' element.
$this
->assertEquals($this->referencedEntities[0]
->id(), $form_state
->getValue('single'));
// Test the 'single_autocreate' element.
$value = $form_state
->getValue('single_autocreate');
$this
->assertEquals('single - autocreated entity label', $value['entity']
->label());
$this
->assertEquals('entity_test', $value['entity']
->bundle());
$this
->assertEquals($this->testUser
->id(), $value['entity']
->getOwnerId());
// Test the 'single_autocreate_specific_uid' element.
$value = $form_state
->getValue('single_autocreate_specific_uid');
$this
->assertEquals('single - autocreated entity label with specific uid', $value['entity']
->label());
$this
->assertEquals('entity_test', $value['entity']
->bundle());
$this
->assertEquals($this->testAutocreateUser
->id(), $value['entity']
->getOwnerId());
// Test the 'tags' element.
$expected = [
[
'target_id' => $this->referencedEntities[0]
->id(),
],
[
'target_id' => $this->referencedEntities[1]
->id(),
],
];
$this
->assertEquals($expected, $form_state
->getValue('tags'));
// Test the 'single_autocreate' element.
$value = $form_state
->getValue('tags_autocreate');
// First value is an existing entity.
$this
->assertEquals($this->referencedEntities[0]
->id(), $value[0]['target_id']);
// Second value is an autocreated entity.
$this
->assertTrue(!isset($value[1]['target_id']));
$this
->assertEquals('tags - autocreated entity label', $value[1]['entity']
->label());
$this
->assertEquals($this->testUser
->id(), $value[1]['entity']
->getOwnerId());
// Third value is an existing entity.
$this
->assertEquals($this->referencedEntities[1]
->id(), $value[2]['target_id']);
// Test the 'tags_autocreate_specific_uid' element.
$value = $form_state
->getValue('tags_autocreate_specific_uid');
// First value is an existing entity.
$this
->assertEquals($this->referencedEntities[0]
->id(), $value[0]['target_id']);
// Second value is an autocreated entity.
$this
->assertTrue(!isset($value[1]['target_id']));
$this
->assertEquals('tags - autocreated entity label with specific uid', $value[1]['entity']
->label());
$this
->assertEquals($this->testAutocreateUser
->id(), $value[1]['entity']
->getOwnerId());
// Third value is an existing entity.
$this
->assertEquals($this->referencedEntities[1]
->id(), $value[2]['target_id']);
// Test the 'single_string_id' element.
$this
->assertEquals($this->referencedEntities[2]
->id(), $form_state
->getValue('single_string_id'));
// Test the 'tags_string_id' element.
$expected = [
[
'target_id' => $this->referencedEntities[2]
->id(),
],
[
'target_id' => $this->referencedEntities[3]
->id(),
],
];
$this
->assertEquals($expected, $form_state
->getValue('tags_string_id'));
}