2f8f13df8db51c3dad28b32f820e546d35aa76ce
[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\Core\DependencyInjection\ContainerBuilder;
7 use Drupal\Core\Logger\RfcLogLevel;
8 use Drupal\field\Entity\FieldConfig;
9 use Drupal\field\Tests\EntityReference\EntityReferenceTestTrait;
10 use Drupal\node\Entity\NodeType;
11 use Drupal\KernelTests\KernelTestBase;
12 use Drupal\taxonomy\Entity\Vocabulary;
13 use Symfony\Component\Debug\BufferingLogger;
14
15 /**
16  * Tests entity reference field settings.
17  *
18  * @group field
19  */
20 class EntityReferenceSettingsTest extends KernelTestBase {
21
22   use EntityReferenceTestTrait;
23
24   /**
25    * {@inheritdoc}
26    */
27   public static $modules = ['node', 'taxonomy', 'field', 'user', 'text', 'entity_reference', 'entity_test', 'system'];
28
29   /**
30    * Testing node type.
31    *
32    * @var \Drupal\node\Entity\NodeType
33    */
34   protected $nodeType;
35
36   /**
37    * Testing vocabulary.
38    *
39    * @var \Drupal\taxonomy\Entity\Vocabulary
40    */
41   protected $vocabulary;
42
43   /**
44    * An entity bundle that is not stored as a configuration entity.
45    *
46    * @var string
47    */
48   protected $customBundle;
49
50   /**
51    * The service name for a logger implementation that collects anything logged.
52    *
53    * @var string
54    */
55   protected $testLogServiceName = 'entity_reference_settings_test.logger';
56
57   /**
58    * {@inheritdoc}
59    */
60   protected function setUp() {
61     parent::setup();
62
63     $this->installEntitySchema('node');
64     $this->installEntitySchema('taxonomy_term');
65     $this->installEntitySchema('entity_test');
66
67     $this->nodeType = NodeType::create([
68       'type' => Unicode::strtolower($this->randomMachineName()),
69       'name' => $this->randomString(),
70     ]);
71     $this->nodeType->save();
72
73     // Create a custom bundle.
74     $this->customBundle = 'test_bundle_' . Unicode::strtolower($this->randomMachineName());
75     entity_test_create_bundle($this->customBundle, NULL, 'entity_test');
76
77     // Prepare the logger for collecting the expected critical error.
78     $this->container->get($this->testLogServiceName)->cleanLogs();
79   }
80
81   /**
82    * Tests that config bundle deletions are mirrored in field config settings.
83    */
84   public function testConfigTargetBundleDeletion() {
85     // Create two vocabularies.
86     /** @var \Drupal\taxonomy\Entity\Vocabulary[] $vocabularies */
87     $vocabularies = [];
88     for ($i = 0; $i < 2; $i++) {
89       $vid = Unicode::strtolower($this->randomMachineName());
90       $vocabularies[$i] = Vocabulary::create([
91         'name' => $this->randomString(),
92         'vid' => $vid,
93       ]);
94       $vocabularies[$i]->save();
95     }
96     // Attach an entity reference field to $this->nodeType.
97     $name = Unicode::strtolower($this->randomMachineName());
98     $label = $this->randomString();
99     $handler_settings = [
100       'target_bundles' => [
101         $vocabularies[0]->id() => $vocabularies[0]->id(),
102         $vocabularies[1]->id() => $vocabularies[1]->id(),
103       ],
104     ];
105     $this->createEntityReferenceField('node', $this->nodeType->id(), $name, $label, 'taxonomy_term', 'default', $handler_settings);
106
107     // Check that the 'target_bundle' setting contains the vocabulary.
108     $field_config = FieldConfig::loadByName('node', $this->nodeType->id(), $name);
109     $actual_handler_settings = $field_config->getSetting('handler_settings');
110     $this->assertEqual($handler_settings, $actual_handler_settings);
111
112     // Delete the vocabulary.
113     $vocabularies[0]->delete();
114     // Ensure that noting is logged.
115     $this->assertEmpty($this->container->get($this->testLogServiceName)->cleanLogs());
116
117     // Check that the deleted vocabulary is no longer present in the
118     // 'target_bundles' field setting.
119     $field_config = FieldConfig::loadByName('node', $this->nodeType->id(), $name);
120     $handler_settings = $field_config->getSetting('handler_settings');
121     $this->assertEquals([$vocabularies[1]->id() => $vocabularies[1]->id()], $handler_settings['target_bundles']);
122
123     // Delete the other vocabulary.
124     $vocabularies[1]->delete();
125     // Ensure that field_field_config_presave() logs the expected critical
126     // error.
127     $log_message = $this->container->get($this->testLogServiceName)->cleanLogs()[0];
128     $this->assertEquals(RfcLogLevel::CRITICAL, $log_message[0]);
129     $this->assertEquals('The %field_name entity reference field (entity_type: %entity_type, bundle: %bundle) no longer has any valid bundle it can reference. The field is not working correctly anymore and has to be adjusted.', $log_message[1]);
130     $this->assertEquals($field_config->getName(), $log_message[2]['%field_name']);
131     $this->assertEquals('node', $log_message[2]['%entity_type']);
132     $this->assertEquals($this->nodeType->id(), $log_message[2]['%bundle']);
133
134     // Check that the deleted bundle is no longer present in the
135     // 'target_bundles' field setting.
136     $field_config = FieldConfig::loadByName('node', $this->nodeType->id(), $name);
137     $handler_settings = $field_config->getSetting('handler_settings');
138     $this->assertEquals([], $handler_settings['target_bundles']);
139   }
140
141   /**
142    * Tests that deletions of custom bundles are mirrored in field settings.
143    */
144   public function testCustomTargetBundleDeletion() {
145     // Attach an entity reference field to $this->nodeType.
146     $name = Unicode::strtolower($this->randomMachineName());
147     $label = $this->randomString();
148     $handler_settings = ['target_bundles' => [$this->customBundle => $this->customBundle]];
149     $this->createEntityReferenceField('node', $this->nodeType->id(), $name, $label, 'entity_test', 'default', $handler_settings);
150
151     // Check that the 'target_bundle' setting contains the custom bundle.
152     $field_config = FieldConfig::loadByName('node', $this->nodeType->id(), $name);
153     $actual_handler_settings = $field_config->getSetting('handler_settings');
154     $this->assertEqual($handler_settings, $actual_handler_settings);
155
156     // Delete the custom bundle.
157     entity_test_delete_bundle($this->customBundle, 'entity_test');
158
159     // Ensure that field_field_config_presave() logs the expected critical
160     // error.
161     $log_message = $this->container->get($this->testLogServiceName)->cleanLogs()[0];
162     $this->assertEquals(RfcLogLevel::CRITICAL, $log_message[0]);
163     $this->assertEquals('The %field_name entity reference field (entity_type: %entity_type, bundle: %bundle) no longer has any valid bundle it can reference. The field is not working correctly anymore and has to be adjusted.', $log_message[1]);
164     $this->assertEquals($field_config->getName(), $log_message[2]['%field_name']);
165     $this->assertEquals('node', $log_message[2]['%entity_type']);
166     $this->assertEquals($this->nodeType->id(), $log_message[2]['%bundle']);
167
168     // Check that the deleted bundle is no longer present in the
169     // 'target_bundles' field setting.
170     $field_config = FieldConfig::loadByName('node', $this->nodeType->id(), $name);
171     $handler_settings = $field_config->getSetting('handler_settings');
172     $this->assertTrue(empty($handler_settings['target_bundles']));
173   }
174
175   /**
176    * {@inheritdoc}
177    */
178   public function register(ContainerBuilder $container) {
179     parent::register($container);
180     $container
181       ->register($this->testLogServiceName, BufferingLogger::class)
182       ->addTag('logger');
183   }
184
185 }