public function NameAutocompleteTest::testAutocomplete in Name Field 8
Tests the service.
File
- tests/
src/ Kernel/ NameAutocompleteTest.php, line 89
Class
- NameAutocompleteTest
- Tests name autocomplete.
Namespace
Drupal\Tests\name\KernelCode
public function testAutocomplete() {
$autocomplete = \Drupal::service('name.autocomplete');
// Title component.
$matches = $autocomplete
->getMatches($this->field, 'title', 'M');
$this
->assertEquals($matches, $this
->mapAssoc([
'Mr.',
'Mrs.',
'Miss',
'Ms.',
]));
$matches = $autocomplete
->getMatches($this->field, 'title', 'Mr');
$this
->assertEquals($matches, $this
->mapAssoc([
'Mr.',
'Mrs.',
]));
$matches = $autocomplete
->getMatches($this->field, 'title', 'Pr');
$this
->assertEquals($matches, $this
->mapAssoc([
'Prof.',
]));
$matches = $autocomplete
->getMatches($this->field, 'title', 'X');
$this
->assertEquals($matches, []);
// First name component.
$names = [
'SpongeBob SquarePants',
'Patrick Star',
'Squidward Tentacles',
'Eugene Krabs',
'Sandy Cheeks',
'Gary Snail',
];
foreach ($names as $name) {
$name = explode(' ', $name);
$entity = $this->container
->get('entity_type.manager')
->getStorage('entity_test')
->create([
'bundle' => 'entity_test',
'field_name_test' => [
'given' => $name[0],
'family' => $name[1],
],
]);
$entity
->save();
}
$matches = $autocomplete
->getMatches($this->field, 'name', 'S');
}