View source
<?php
namespace Drupal\Tests\name\Functional;
use Drupal\taxonomy\Entity\Term;
use Drupal\taxonomy\Entity\Vocabulary;
class NameOptionsProviderTest extends NameTestBase {
use NameTestTrait;
public static $modules = [
'field',
'name',
'taxonomy',
'entity_test',
'text',
'user',
];
protected $entityListener;
protected $optionsProvider;
public function setUp() {
parent::setUp();
$this->entityListener = \Drupal::service('entity_type.listener');
$this->entityListener
->onEntityTypeCreate(\Drupal::entityTypeManager()
->getDefinition('taxonomy_term'));
$this->optionsProvider = \Drupal::service('name.options_provider');
}
public function testTitleOptionsFromField() {
$field = $this
->createNameField('field_name_test', 'entity_test', 'entity_test');
$settings = $field
->getSettings();
$settings['title_options'] = [
'-- --',
'b',
'a',
'c',
];
$field
->getConfig('entity_test')
->setSettings($settings)
->save();
$expected = [
'' => '--',
'b' => 'b',
'a' => 'a',
'c' => 'c',
];
$this
->assertEquals($expected, $this->optionsProvider
->getOptions($field, 'title'));
$settings['sort_options']['title'] = TRUE;
$field
->getConfig('entity_test')
->setSettings($settings)
->save();
$expected = [
'' => '--',
'a' => 'a',
'b' => 'b',
'c' => 'c',
];
$this
->assertEquals($expected, $this->optionsProvider
->getOptions($field, 'title'));
}
public function testTitleOptionsFromTaxonomy() {
$field = $this
->createNameField('field_name_test', 'entity_test', 'entity_test');
$vocabulary = Vocabulary::create([
'vid' => 'title_options',
'name' => 'Title options',
]);
$vocabulary
->save();
foreach ([
'foo',
'bar',
'baz',
] as $name) {
$term = Term::create([
'name' => $name,
'vid' => $vocabulary
->id(),
]);
$term
->save();
}
$settings = $field
->getSettings();
$settings['title_options'] = [
'-- --',
'[vocabulary:title_options]',
];
$settings['sort_options']['title'] = TRUE;
$field
->getConfig('entity_test')
->setSettings($settings)
->save();
$expected = [
'' => '--',
'bar' => 'bar',
'baz' => 'baz',
'foo' => 'foo',
];
$this
->assertEquals($expected, $this->optionsProvider
->getOptions($field, 'title'));
}
}