function EntityAutocompleteTest::testEntityReferenceAutocompletion
Same name in other branches
- 9 core/tests/Drupal/KernelTests/Core/Entity/EntityAutocompleteTest.php \Drupal\KernelTests\Core\Entity\EntityAutocompleteTest::testEntityReferenceAutocompletion()
- 8.9.x core/tests/Drupal/KernelTests/Core/Entity/EntityAutocompleteTest.php \Drupal\KernelTests\Core\Entity\EntityAutocompleteTest::testEntityReferenceAutocompletion()
- 11.x core/tests/Drupal/KernelTests/Core/Entity/EntityAutocompleteTest.php \Drupal\KernelTests\Core\Entity\EntityAutocompleteTest::testEntityReferenceAutocompletion()
Tests autocompletion edge cases with slashes in the names.
File
-
core/
tests/ Drupal/ KernelTests/ Core/ Entity/ EntityAutocompleteTest.php, line 47
Class
- EntityAutocompleteTest
- Tests the autocomplete functionality.
Namespace
Drupal\KernelTests\Core\EntityCode
public function testEntityReferenceAutocompletion() : void {
// Add an entity with a slash in its name.
$entity_1 = $this->container
->get('entity_type.manager')
->getStorage($this->entityType)
->create([
'name' => '10/16/2011',
]);
$entity_1->save();
// Add another entity that differs after the slash character.
$entity_2 = $this->container
->get('entity_type.manager')
->getStorage($this->entityType)
->create([
'name' => '10/17/2011',
]);
$entity_2->save();
// Add another entity that has both a comma, a slash and markup.
$entity_3 = $this->container
->get('entity_type.manager')
->getStorage($this->entityType)
->create([
'name' => 'label with, and / test',
]);
$entity_3->save();
// Try to autocomplete an entity label that matches both entities.
// We should get both entities in a JSON encoded string.
$input = '10/';
$data = $this->getAutocompleteResult($input);
$this->assertSame(Html::escape($entity_1->name->value), $data[0]['label'], 'Autocomplete returned the first matching entity');
$this->assertSame(Html::escape($entity_2->name->value), $data[1]['label'], 'Autocomplete returned the second matching entity');
// Try to autocomplete an entity label that matches the first entity.
// We should only get the first entity in a JSON encoded string.
$input = '10/16';
$data = $this->getAutocompleteResult($input);
$target = [
'value' => $entity_1->name->value . ' (1)',
'label' => Html::escape($entity_1->name->value),
];
$this->assertSame($target, reset($data), 'Autocomplete returns only the expected matching entity.');
// Pass the first entity to the request.
// We should get empty results.
// First we need to have permission to pass entity.
$user = $this->drupalCreateUser([
'administer entity_test content',
]);
$this->drupalSetCurrentUser($user);
$data = $this->getAutocompleteResult($input, $entity_1->id());
$this->assertSame([], $data, 'Autocomplete returns empty results as first entity is passed to autocomplete request.');
// Try to autocomplete an entity label that matches the second entity, and
// the first entity is already typed in the autocomplete (tags) widget.
$input = $entity_1->name->value . ' (1), 10/17';
$data = $this->getAutocompleteResult($input);
$this->assertSame(Html::escape($entity_2->name->value), $data[0]['label'], 'Autocomplete returned the second matching entity');
// Try to autocomplete an entity label with both comma, slash and markup.
$input = '"label with, and /"';
$data = $this->getAutocompleteResult($input);
$n = $entity_3->name->value . ' (3)';
// Entity labels containing commas or quotes must be wrapped in quotes.
$n = Tags::encode($n);
$target = [
'value' => $n,
'label' => Html::escape($entity_3->name->value),
];
$this->assertSame($target, reset($data), 'Autocomplete returns an entity label containing a comma and a slash.');
// Test empty input.
foreach ([
'',
NULL,
FALSE,
0,
0.0,
] as $input) {
$data = $this->getAutocompleteResult($input);
$this->assertSame([], $data, 'Autocomplete of empty input returns empty result');
}
$input = ',';
$data = $this->getAutocompleteResult($input);
$this->assertSame(Html::escape($entity_1->name->value), $data[0]['label'], 'Autocomplete returned the first matching entity');
$this->assertSame(Html::escape($entity_2->name->value), $data[1]['label'], 'Autocomplete returned the second matching entity');
$this->assertSame(Html::escape($entity_3->name->value), $data[2]['label'], 'Autocomplete returned the third matching entity');
// Pass the first entity to the request.
// We should not get the first entity in the results.
$data = $this->getAutocompleteResult($input, $entity_1->id());
$this->assertCount(2, $data, 'Autocomplete returned only 2 entities');
$this->assertSame(Html::escape($entity_2->name->value), $data[0]['label'], 'Autocomplete returned the second matching entity');
$this->assertSame(Html::escape($entity_3->name->value), $data[1]['label'], 'Autocomplete returned the third matching entity');
// Strange input that is mangled by
// \Drupal\Component\Utility\Tags::explode().
$input = '"l!J>&Tw';
$data = $this->getAutocompleteResult($input);
$this->assertSame(Html::escape($entity_1->name->value), $data[0]['label'], 'Autocomplete returned the first matching entity');
$this->assertSame(Html::escape($entity_2->name->value), $data[1]['label'], 'Autocomplete returned the second matching entity');
$this->assertSame(Html::escape($entity_3->name->value), $data[2]['label'], 'Autocomplete returned the third matching entity');
// Try to autocomplete an entity label with the '0' character.
$input = '0';
$data = $this->getAutocompleteResult($input);
$this->assertSame(Html::escape($entity_1->name->value), $data[0]['label'], 'Autocomplete returned the first matching entity');
$this->assertSame(Html::escape($entity_2->name->value), $data[1]['label'], 'Autocomplete returned the second matching entity');
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.