comparison core/modules/taxonomy/tests/src/Kernel/TermEntityReferenceTest.php @ 5:12f9dff5fda9 tip

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:34:47 +0100
parents
children
comparison
equal deleted inserted replaced
4:a9cd425dd02b 5:12f9dff5fda9
1 <?php
2
3 namespace Drupal\Tests\taxonomy\Kernel;
4
5 use Drupal\field\Entity\FieldStorageConfig;
6 use Drupal\field\Entity\FieldConfig;
7 use Drupal\KernelTests\KernelTestBase;
8 use Drupal\taxonomy\Entity\Term;
9 use Drupal\taxonomy\Entity\Vocabulary;
10
11 /**
12 * Tests the settings of restricting term selection to a single vocabulary.
13 *
14 * @group taxonomy
15 */
16 class TermEntityReferenceTest extends KernelTestBase {
17
18 /**
19 * {@inheritdoc}
20 */
21 public static $modules = [
22 'entity_test',
23 'field',
24 'system',
25 'taxonomy',
26 'text',
27 'user',
28 ];
29
30 /**
31 * {@inheritdoc}
32 */
33 protected function setUp() {
34 parent::setUp();
35
36 $this->installEntitySchema('entity_test');
37 $this->installEntitySchema('taxonomy_term');
38 $this->installEntitySchema('user');
39 }
40
41 /**
42 * Tests an entity reference field restricted to a single vocabulary.
43 *
44 * Creates two vocabularies with a term, then set up the entity reference
45 * field to limit the target vocabulary to one of them, ensuring that
46 * the restriction applies.
47 */
48 public function testSelectionTestVocabularyRestriction() {
49 // Create two vocabularies.
50 $vocabulary = Vocabulary::create([
51 'name' => 'test1',
52 'vid' => 'test1',
53 ]);
54 $vocabulary->save();
55 $vocabulary2 = Vocabulary::create([
56 'name' => 'test2',
57 'vid' => 'test2',
58 ]);
59 $vocabulary2->save();
60
61 $term = Term::create([
62 'name' => 'term1',
63 'vid' => $vocabulary->id(),
64 ]);
65 $term->save();
66 $term2 = Term::create([
67 'name' => 'term2',
68 'vid' => $vocabulary2->id(),
69 ]);
70 $term2->save();
71
72 // Create an entity reference field.
73 $field_name = 'taxonomy_' . $vocabulary->id();
74 $field_storage = FieldStorageConfig::create([
75 'field_name' => $field_name,
76 'entity_type' => 'entity_test',
77 'translatable' => FALSE,
78 'settings' => [
79 'target_type' => 'taxonomy_term',
80 ],
81 'type' => 'entity_reference',
82 'cardinality' => 1,
83 ]);
84 $field_storage->save();
85 $field = FieldConfig::create([
86 'field_storage' => $field_storage,
87 'entity_type' => 'entity_test',
88 'bundle' => 'test_bundle',
89 'settings' => [
90 'handler' => 'default',
91 'handler_settings' => [
92 // Restrict selection of terms to a single vocabulary.
93 'target_bundles' => [
94 $vocabulary->id() => $vocabulary->id(),
95 ],
96 ],
97 ],
98 ]);
99 $field->save();
100
101 $handler = $this->container->get('plugin.manager.entity_reference_selection')->getSelectionHandler($field);
102 $result = $handler->getReferenceableEntities();
103
104 $expected_result = [
105 $vocabulary->id() => [
106 $term->id() => $term->getName(),
107 ],
108 ];
109
110 $this->assertIdentical($result, $expected_result, 'Terms selection restricted to a single vocabulary.');
111 }
112
113 }