3 namespace Drupal\Tests\taxonomy\Functional;
5 use Drupal\field\Entity\FieldStorageConfig;
6 use Drupal\field\Entity\FieldConfig;
9 * Tests the settings of restricting term selection to a single vocabulary.
13 class TermEntityReferenceTest extends TaxonomyTestBase {
20 public static $modules = ['entity_reference_test', 'entity_test'];
23 * Tests an entity reference field restricted to a single vocabulary.
25 * Creates two vocabularies with a term, then set up the entity reference
26 * field to limit the target vocabulary to one of them, ensuring that
27 * the restriction applies.
29 public function testSelectionTestVocabularyRestriction() {
31 // Create two vocabularies.
32 $vocabulary = $this->createVocabulary();
33 $vocabulary2 = $this->createVocabulary();
35 $term = $this->createTerm($vocabulary);
36 $term2 = $this->createTerm($vocabulary2);
38 // Create an entity reference field.
39 $field_name = 'taxonomy_' . $vocabulary->id();
40 $field_storage = FieldStorageConfig::create([
41 'field_name' => $field_name,
42 'entity_type' => 'entity_test',
43 'translatable' => FALSE,
45 'target_type' => 'taxonomy_term',
47 'type' => 'entity_reference',
50 $field_storage->save();
51 $field = FieldConfig::create([
52 'field_storage' => $field_storage,
53 'entity_type' => 'entity_test',
54 'bundle' => 'test_bundle',
56 'handler' => 'default',
57 'handler_settings' => [
58 // Restrict selection of terms to a single vocabulary.
60 $vocabulary->id() => $vocabulary->id(),
67 $handler = $this->container->get('plugin.manager.entity_reference_selection')->getSelectionHandler($field);
68 $result = $handler->getReferenceableEntities();
71 $vocabulary->id() => [
72 $term->id() => $term->getName(),
76 $this->assertIdentical($result, $expected_result, 'Terms selection restricted to a single vocabulary.');