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