Version 1
[yaffs-website] / web / core / modules / field / tests / src / Kernel / EntityReference / EntityReferenceSettingsTest.php
1 <?php
2
3 namespace Drupal\Tests\field\Kernel\EntityReference;
4
5 use Drupal\Component\Utility\Unicode;
6 use Drupal\field\Entity\FieldConfig;
7 use Drupal\field\Tests\EntityReference\EntityReferenceTestTrait;
8 use Drupal\node\Entity\NodeType;
9 use Drupal\KernelTests\KernelTestBase;
10 use Drupal\taxonomy\Entity\Vocabulary;
11
12 /**
13  * Tests entity reference field settings.
14  *
15  * @group field
16  */
17 class EntityReferenceSettingsTest extends KernelTestBase {
18
19   use EntityReferenceTestTrait;
20
21   /**
22    * {@inheritdoc}
23    */
24   public static $modules = ['node', 'taxonomy', 'field', 'user', 'text', 'entity_reference', 'entity_test', 'system'];
25
26   /**
27    * Testing node type.
28    *
29    * @var \Drupal\node\Entity\NodeType
30    */
31   protected $nodeType;
32
33   /**
34    * Testing vocabulary.
35    *
36    * @var \Drupal\taxonomy\Entity\Vocabulary
37    */
38   protected $vocabulary;
39
40   /**
41    * An entity bundle that is not stored as a configuration entity.
42    *
43    * @var string
44    */
45   protected $customBundle;
46
47   /**
48    * {@inheritdoc}
49    */
50   protected function setUp() {
51     parent::setup();
52
53     $this->installEntitySchema('node');
54     $this->installEntitySchema('taxonomy_term');
55     $this->installEntitySchema('entity_test');
56
57     $this->nodeType = NodeType::create([
58       'type' => Unicode::strtolower($this->randomMachineName()),
59       'name' => $this->randomString(),
60     ]);
61     $this->nodeType->save();
62
63     $this->vocabulary = Vocabulary::create([
64       'vid' => Unicode::strtolower($this->randomMachineName()),
65       'name' => $this->randomString(),
66     ]);
67     $this->vocabulary->save();
68
69     // Create a custom bundle.
70     $this->customBundle = 'test_bundle_' . Unicode::strtolower($this->randomMachineName());
71     entity_test_create_bundle($this->customBundle, NULL, 'entity_test');
72   }
73
74   /**
75    * Tests that config bundle deletions are mirrored in field config settings.
76    */
77   public function testConfigTargetBundleDeletion() {
78     // Attach an entity reference field to $this->nodeType.
79     $name = Unicode::strtolower($this->randomMachineName());
80     $label = $this->randomString();
81     $vid = $this->vocabulary->id();
82     $handler_settings = ['target_bundles' => [$vid => $vid]];
83     $this->createEntityReferenceField('node', $this->nodeType->id(), $name, $label, 'taxonomy_term', 'default', $handler_settings);
84
85     // Check that the 'target_bundle' setting contains the vocabulary.
86     $field_config = FieldConfig::loadByName('node', $this->nodeType->id(), $name);
87     $actual_handler_settings = $field_config->getSetting('handler_settings');
88     $this->assertEqual($handler_settings, $actual_handler_settings);
89
90     // Delete the vocabulary.
91     $this->vocabulary->delete();
92
93     // Check that the deleted vocabulary is no longer present in the
94     // 'target_bundles' field setting.
95     $field_config = FieldConfig::loadByName('node', $this->nodeType->id(), $name);
96     $handler_settings = $field_config->getSetting('handler_settings');
97     $this->assertTrue(empty($handler_settings['target_bundles']));
98   }
99
100   /**
101    * Tests that deletions of custom bundles are mirrored in field settings.
102    */
103   public function testCustomTargetBundleDeletion() {
104     // Attach an entity reference field to $this->nodeType.
105     $name = Unicode::strtolower($this->randomMachineName());
106     $label = $this->randomString();
107     $handler_settings = ['target_bundles' => [$this->customBundle => $this->customBundle]];
108     $this->createEntityReferenceField('node', $this->nodeType->id(), $name, $label, 'entity_test', 'default', $handler_settings);
109
110     // Check that the 'target_bundle' setting contains the custom bundle.
111     $field_config = FieldConfig::loadByName('node', $this->nodeType->id(), $name);
112     $actual_handler_settings = $field_config->getSetting('handler_settings');
113     $this->assertEqual($handler_settings, $actual_handler_settings);
114
115     // Delete the custom bundle.
116     entity_test_delete_bundle($this->customBundle, 'entity_test');
117
118     // Check that the deleted bundle is no longer present in the
119     // 'target_bundles' field setting.
120     $field_config = FieldConfig::loadByName('node', $this->nodeType->id(), $name);
121     $handler_settings = $field_config->getSetting('handler_settings');
122     $this->assertTrue(empty($handler_settings['target_bundles']));
123   }
124
125 }