Upgraded drupal core with security updates
[yaffs-website] / web / core / modules / taxonomy / tests / src / Functional / TermEntityReferenceTest.php
1 <?php
2
3 namespace Drupal\Tests\taxonomy\Functional;
4
5 use Drupal\field\Entity\FieldStorageConfig;
6 use Drupal\field\Entity\FieldConfig;
7
8 /**
9  * Tests the settings of restricting term selection to a single vocabulary.
10  *
11  * @group taxonomy
12  */
13 class TermEntityReferenceTest extends TaxonomyTestBase {
14
15   /**
16    * Modules to enable.
17    *
18    * @var array
19    */
20   public static $modules = ['entity_reference_test', 'entity_test'];
21
22   /**
23    * Tests an entity reference field restricted to a single vocabulary.
24    *
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.
28    */
29   public function testSelectionTestVocabularyRestriction() {
30
31     // Create two vocabularies.
32     $vocabulary = $this->createVocabulary();
33     $vocabulary2 = $this->createVocabulary();
34
35     $term = $this->createTerm($vocabulary);
36     $term2 = $this->createTerm($vocabulary2);
37
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,
44       'settings' => [
45         'target_type' => 'taxonomy_term',
46       ],
47       'type' => 'entity_reference',
48       'cardinality' => 1,
49     ]);
50     $field_storage->save();
51     $field = FieldConfig::create([
52       'field_storage' => $field_storage,
53       'entity_type' => 'entity_test',
54       'bundle' => 'test_bundle',
55       'settings' => [
56         'handler' => 'default',
57         'handler_settings' => [
58           // Restrict selection of terms to a single vocabulary.
59           'target_bundles' => [
60             $vocabulary->id() => $vocabulary->id(),
61           ],
62         ],
63       ],
64     ]);
65     $field->save();
66
67     $handler = $this->container->get('plugin.manager.entity_reference_selection')->getSelectionHandler($field);
68     $result = $handler->getReferenceableEntities();
69
70     $expected_result = [
71       $vocabulary->id() => [
72         $term->id() => $term->getName(),
73       ],
74     ];
75
76     $this->assertIdentical($result, $expected_result, 'Terms selection restricted to a single vocabulary.');
77   }
78
79 }