View source
<?php
namespace Drupal\KernelTests\Core\Entity\Element;
use Drupal\Core\Entity\Element\EntityAutocomplete;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormInterface;
use Drupal\Core\Form\FormState;
use Drupal\Core\Form\FormStateInterface;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\entity_test\Entity\EntityTestStringId;
use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
use Drupal\user\Entity\Role;
use Drupal\user\Entity\User;
class EntityAutocompleteElementFormTest extends EntityKernelTestBase implements FormInterface {
protected $testUser;
protected $testAutocreateUser;
protected $referencedEntities;
protected function setUp() : void {
parent::setUp();
$this
->installEntitySchema('entity_test_string_id');
User::create([
'uid' => 1,
'name' => 'user1',
])
->save();
Role::create([
'id' => 'test_role',
'label' => 'Can view test entities',
'permissions' => [
'view test entity',
],
])
->save();
$this->testUser = User::create([
'name' => 'foobar1',
'mail' => 'foobar1@example.com',
'roles' => [
'test_role',
],
]);
$this->testUser
->save();
\Drupal::service('current_user')
->setAccount($this->testUser);
$this->testAutocreateUser = User::create([
'name' => 'foobar2',
'mail' => 'foobar2@example.com',
]);
$this->testAutocreateUser
->save();
for ($i = 1; $i < 3; $i++) {
$entity = EntityTest::create([
'name' => $this
->randomMachineName(),
]);
$entity
->save();
$this->referencedEntities[] = $entity;
}
for ($i = 0; $i < 2; $i++) {
$entity = EntityTestStringId::create([
'name' => $this
->randomMachineName(),
'id' => $this
->randomMachineName() . '&</\\:?',
]);
$entity
->save();
$this->referencedEntities[] = $entity;
}
}
public function getFormId() {
return 'test_entity_autocomplete';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form['single'] = [
'#type' => 'entity_autocomplete',
'#target_type' => 'entity_test',
];
$form['single_autocreate'] = [
'#type' => 'entity_autocomplete',
'#target_type' => 'entity_test',
'#autocreate' => [
'bundle' => 'entity_test',
],
];
$form['single_autocreate_specific_uid'] = [
'#type' => 'entity_autocomplete',
'#target_type' => 'entity_test',
'#autocreate' => [
'bundle' => 'entity_test',
'uid' => $this->testAutocreateUser
->id(),
],
];
$form['tags'] = [
'#type' => 'entity_autocomplete',
'#target_type' => 'entity_test',
'#tags' => TRUE,
];
$form['tags_autocreate'] = [
'#type' => 'entity_autocomplete',
'#target_type' => 'entity_test',
'#tags' => TRUE,
'#autocreate' => [
'bundle' => 'entity_test',
],
];
$form['tags_autocreate_specific_uid'] = [
'#type' => 'entity_autocomplete',
'#target_type' => 'entity_test',
'#tags' => TRUE,
'#autocreate' => [
'bundle' => 'entity_test',
'uid' => $this->testAutocreateUser
->id(),
],
];
$form['single_no_validate'] = [
'#type' => 'entity_autocomplete',
'#target_type' => 'entity_test',
'#validate_reference' => FALSE,
];
$form['single_autocreate_no_validate'] = [
'#type' => 'entity_autocomplete',
'#target_type' => 'entity_test',
'#validate_reference' => FALSE,
'#autocreate' => [
'bundle' => 'entity_test',
],
];
$form['single_access'] = [
'#type' => 'entity_autocomplete',
'#target_type' => 'entity_test',
'#default_value' => $this->referencedEntities[0],
];
$form['tags_access'] = [
'#type' => 'entity_autocomplete',
'#target_type' => 'entity_test',
'#tags' => TRUE,
'#default_value' => [
$this->referencedEntities[0],
$this->referencedEntities[1],
],
];
$form['single_string_id'] = [
'#type' => 'entity_autocomplete',
'#target_type' => 'entity_test_string_id',
];
$form['tags_string_id'] = [
'#type' => 'entity_autocomplete',
'#target_type' => 'entity_test_string_id',
'#tags' => TRUE,
];
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
}
public function validateForm(array &$form, FormStateInterface $form_state) {
}
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);
$this
->assertCount(0, $form_state
->getErrors());
$this
->assertEquals($this->referencedEntities[0]
->id(), $form_state
->getValue('single'));
$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());
$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());
$expected = [
[
'target_id' => $this->referencedEntities[0]
->id(),
],
[
'target_id' => $this->referencedEntities[1]
->id(),
],
];
$this
->assertEquals($expected, $form_state
->getValue('tags'));
$value = $form_state
->getValue('tags_autocreate');
$this
->assertEquals($this->referencedEntities[0]
->id(), $value[0]['target_id']);
$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());
$this
->assertEquals($this->referencedEntities[1]
->id(), $value[2]['target_id']);
$value = $form_state
->getValue('tags_autocreate_specific_uid');
$this
->assertEquals($this->referencedEntities[0]
->id(), $value[0]['target_id']);
$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());
$this
->assertEquals($this->referencedEntities[1]
->id(), $value[2]['target_id']);
$this
->assertEquals($this->referencedEntities[2]
->id(), $form_state
->getValue('single_string_id'));
$expected = [
[
'target_id' => $this->referencedEntities[2]
->id(),
],
[
'target_id' => $this->referencedEntities[3]
->id(),
],
];
$this
->assertEquals($expected, $form_state
->getValue('tags_string_id'));
}
public function testInvalidEntityAutocompleteElement() {
$form_builder = $this->container
->get('form_builder');
$form_state = (new FormState())
->setValues([
'single' => 'single - non-existent label',
]);
$form_builder
->submitForm($this, $form_state);
$this
->assertCount(1, $form_state
->getErrors());
$this
->assertEquals(t('There are no test entity entities matching "%value".', [
'%value' => 'single - non-existent label',
]), $form_state
->getErrors()['single']);
$form_state = (new FormState())
->setValues([
'single' => 'single - non-existent label (42)',
]);
$form_builder
->submitForm($this, $form_state);
$this
->assertCount(1, $form_state
->getErrors());
$this
->assertEquals(t('The referenced entity (%type: %id) does not exist.', [
'%type' => 'entity_test',
'%id' => 42,
]), $form_state
->getErrors()['single']);
$form_state = (new FormState())
->setValues([
'single_no_validate' => 'single - non-existent label',
'single_autocreate_no_validate' => 'single - autocreate non-existent label',
]);
$form_builder
->submitForm($this, $form_state);
$this
->assertCount(1, $form_state
->getErrors());
$this
->assertEquals(t('There are no test entity entities matching "%value".', [
'%value' => 'single - non-existent label',
]), $form_state
->getErrors()['single_no_validate']);
$form_state = (new FormState())
->setValues([
'single_no_validate' => 'single - non-existent label (42)',
'single_autocreate_no_validate' => 'single - autocreate non-existent label (43)',
]);
$form_builder
->submitForm($this, $form_state);
$this
->assertCount(0, $form_state
->getErrors());
}
public function testEntityAutocompleteAccess() {
$form_builder = $this->container
->get('form_builder');
$form = $form_builder
->getForm($this);
$expected = $this->referencedEntities[0]
->label() . ' (' . $this->referencedEntities[0]
->id() . ')';
$this
->assertEquals($expected, $form['single_access']['#value']);
$expected .= ', ' . $this->referencedEntities[1]
->label() . ' (' . $this->referencedEntities[1]
->id() . ')';
$this
->assertEquals($expected, $form['tags_access']['#value']);
\Drupal::currentUser()
->setAccount($this
->createUser([], []));
$form = $form_builder
->getForm($this);
$expected = '- Restricted access - (' . $this->referencedEntities[0]
->id() . ')';
$this
->assertEquals($expected, $form['single_access']['#value']);
$expected .= ', - Restricted access - (' . $this->referencedEntities[1]
->id() . ')';
$this
->assertEquals($expected, $form['tags_access']['#value']);
}
public function testEntityAutocompleteIdInput() {
$form_builder = $this->container
->get('form_builder');
$form_state = (new FormState())
->setMethod('GET')
->setValues([
'single' => [
[
'target_id' => $this->referencedEntities[0]
->id(),
],
],
'single_no_validate' => [
[
'target_id' => $this->referencedEntities[0]
->id(),
],
],
]);
$form_builder
->submitForm($this, $form_state);
$form = $form_state
->getCompleteForm();
$expected_label = $this
->getAutocompleteInput($this->referencedEntities[0]);
$this
->assertSame($expected_label, $form['single']['#value']);
$this
->assertSame($expected_label, $form['single_no_validate']['#value']);
}
protected function getAutocompleteInput(EntityInterface $entity) {
return EntityAutocomplete::getEntityLabels([
$entity,
]);
}
}