Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Tests\field\Kernel;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Core\Entity\EntityInterface;
|
Chris@0
|
6 use Drupal\field\Entity\FieldConfig;
|
Chris@0
|
7 use Drupal\field\Entity\FieldStorageConfig;
|
Chris@0
|
8
|
Chris@0
|
9 /**
|
Chris@0
|
10 * Bulk delete storages and fields, and clean up afterwards.
|
Chris@0
|
11 *
|
Chris@0
|
12 * @group field
|
Chris@0
|
13 */
|
Chris@0
|
14 class BulkDeleteTest extends FieldKernelTestBase {
|
Chris@0
|
15
|
Chris@0
|
16 /**
|
Chris@0
|
17 * The fields to use in this test.
|
Chris@0
|
18 *
|
Chris@0
|
19 * @var array
|
Chris@0
|
20 */
|
Chris@0
|
21 protected $fieldStorages;
|
Chris@0
|
22
|
Chris@0
|
23 /**
|
Chris@0
|
24 * The entities to use in this test.
|
Chris@0
|
25 *
|
Chris@0
|
26 * @var array
|
Chris@0
|
27 */
|
Chris@0
|
28 protected $entities;
|
Chris@0
|
29
|
Chris@0
|
30 /**
|
Chris@0
|
31 * The entities to use in this test, keyed by bundle.
|
Chris@0
|
32 *
|
Chris@0
|
33 * @var array
|
Chris@0
|
34 */
|
Chris@0
|
35 protected $entitiesByBundles;
|
Chris@0
|
36
|
Chris@0
|
37 /**
|
Chris@0
|
38 * The bundles for the entities used in this test.
|
Chris@0
|
39 *
|
Chris@0
|
40 * @var array
|
Chris@0
|
41 */
|
Chris@0
|
42 protected $bundles;
|
Chris@0
|
43
|
Chris@0
|
44 /**
|
Chris@0
|
45 * The entity type to be used in the test classes.
|
Chris@0
|
46 *
|
Chris@0
|
47 * @var string
|
Chris@0
|
48 */
|
Chris@0
|
49 protected $entityTypeId = 'entity_test';
|
Chris@0
|
50
|
Chris@0
|
51 /**
|
Chris@0
|
52 * Tests that the expected hooks have been invoked on the expected entities.
|
Chris@0
|
53 *
|
Chris@0
|
54 * @param $expected_hooks
|
Chris@0
|
55 * An array keyed by hook name, with one entry per expected invocation.
|
Chris@0
|
56 * Each entry is the value of the "$entity" parameter the hook is expected
|
Chris@0
|
57 * to have been passed.
|
Chris@0
|
58 * @param $actual_hooks
|
Chris@0
|
59 * The array of actual hook invocations recorded by field_test_memorize().
|
Chris@0
|
60 */
|
Chris@0
|
61 public function checkHooksInvocations($expected_hooks, $actual_hooks) {
|
Chris@0
|
62 foreach ($expected_hooks as $hook => $invocations) {
|
Chris@0
|
63 $actual_invocations = $actual_hooks[$hook];
|
Chris@0
|
64
|
Chris@0
|
65 // Check that the number of invocations is correct.
|
Chris@0
|
66 $this->assertEqual(count($actual_invocations), count($invocations), "$hook() was called the expected number of times.");
|
Chris@0
|
67
|
Chris@0
|
68 // Check that the hook was called for each expected argument.
|
Chris@0
|
69 foreach ($invocations as $argument) {
|
Chris@0
|
70 $found = FALSE;
|
Chris@0
|
71 foreach ($actual_invocations as $actual_arguments) {
|
Chris@0
|
72 // The argument we are looking for is either an array of entities as
|
Chris@0
|
73 // the second argument or a single entity object as the first.
|
Chris@0
|
74 if ($argument instanceof EntityInterface && $actual_arguments[0]->id() == $argument->id()) {
|
Chris@0
|
75 $found = TRUE;
|
Chris@0
|
76 break;
|
Chris@0
|
77 }
|
Chris@0
|
78 // In case of an array, compare the array size and make sure it
|
Chris@0
|
79 // contains the same elements.
|
Chris@0
|
80 elseif (is_array($argument) && count($actual_arguments[1]) == count($argument) && count(array_diff_key($actual_arguments[1], $argument)) == 0) {
|
Chris@0
|
81 $found = TRUE;
|
Chris@0
|
82 break;
|
Chris@0
|
83 }
|
Chris@0
|
84 }
|
Chris@0
|
85 $this->assertTrue($found, "$hook() was called on expected argument");
|
Chris@0
|
86 }
|
Chris@0
|
87 }
|
Chris@0
|
88 }
|
Chris@0
|
89
|
Chris@0
|
90 protected function setUp() {
|
Chris@0
|
91 parent::setUp();
|
Chris@0
|
92
|
Chris@0
|
93 $this->fieldStorages = [];
|
Chris@0
|
94 $this->entities = [];
|
Chris@0
|
95 $this->entitiesByBundles = [];
|
Chris@0
|
96
|
Chris@0
|
97 // Create two bundles.
|
Chris@0
|
98 $this->bundles = ['bb_1' => 'bb_1', 'bb_2' => 'bb_2'];
|
Chris@0
|
99 foreach ($this->bundles as $name => $desc) {
|
Chris@0
|
100 entity_test_create_bundle($name, $desc);
|
Chris@0
|
101 }
|
Chris@0
|
102
|
Chris@0
|
103 // Create two field storages.
|
Chris@0
|
104 $field_storage = FieldStorageConfig::create([
|
Chris@0
|
105 'field_name' => 'bf_1',
|
Chris@0
|
106 'entity_type' => $this->entityTypeId,
|
Chris@0
|
107 'type' => 'test_field',
|
Chris@0
|
108 'cardinality' => 1
|
Chris@0
|
109 ]);
|
Chris@0
|
110 $field_storage->save();
|
Chris@0
|
111 $this->fieldStorages[] = $field_storage;
|
Chris@0
|
112 $field_storage = FieldStorageConfig::create([
|
Chris@0
|
113 'field_name' => 'bf_2',
|
Chris@0
|
114 'entity_type' => $this->entityTypeId,
|
Chris@0
|
115 'type' => 'test_field',
|
Chris@0
|
116 'cardinality' => 4
|
Chris@0
|
117 ]);
|
Chris@0
|
118 $field_storage->save();
|
Chris@0
|
119 $this->fieldStorages[] = $field_storage;
|
Chris@0
|
120
|
Chris@0
|
121 // For each bundle, create each field, and 10 entities with values for the
|
Chris@0
|
122 // fields.
|
Chris@0
|
123 foreach ($this->bundles as $bundle) {
|
Chris@0
|
124 foreach ($this->fieldStorages as $field_storage) {
|
Chris@0
|
125 FieldConfig::create([
|
Chris@0
|
126 'field_storage' => $field_storage,
|
Chris@0
|
127 'bundle' => $bundle,
|
Chris@0
|
128 ])->save();
|
Chris@0
|
129 }
|
Chris@0
|
130 for ($i = 0; $i < 10; $i++) {
|
Chris@0
|
131 $entity = $this->container->get('entity_type.manager')
|
Chris@0
|
132 ->getStorage($this->entityTypeId)
|
Chris@0
|
133 ->create(['type' => $bundle]);
|
Chris@0
|
134 foreach ($this->fieldStorages as $field_storage) {
|
Chris@0
|
135 $entity->{$field_storage->getName()}->setValue($this->_generateTestFieldValues($field_storage->getCardinality()));
|
Chris@0
|
136 }
|
Chris@0
|
137 $entity->save();
|
Chris@0
|
138 }
|
Chris@0
|
139 }
|
Chris@0
|
140 $this->entities = $this->container->get('entity_type.manager')
|
Chris@0
|
141 ->getStorage($this->entityTypeId)->loadMultiple();
|
Chris@0
|
142 foreach ($this->entities as $entity) {
|
Chris@0
|
143 // This test relies on the entities having stale field definitions
|
Chris@0
|
144 // so that the deleted field can be accessed on them. Access the field
|
Chris@0
|
145 // now, so that they are always loaded.
|
Chris@0
|
146 $entity->bf_1->value;
|
Chris@0
|
147
|
Chris@0
|
148 // Also keep track of the entities per bundle.
|
Chris@0
|
149 $this->entitiesByBundles[$entity->bundle()][$entity->id()] = $entity;
|
Chris@0
|
150 }
|
Chris@0
|
151 }
|
Chris@0
|
152
|
Chris@0
|
153 /**
|
Chris@0
|
154 * Verify that deleting a field leaves the field data items in the database
|
Chris@0
|
155 * and that the appropriate Field API functions can operate on the deleted
|
Chris@0
|
156 * data and field definition.
|
Chris@0
|
157 *
|
Chris@0
|
158 * This tests how EntityFieldQuery interacts with field deletion and could be
|
Chris@0
|
159 * moved to FieldCrudTestCase, but depends on this class's setUp().
|
Chris@0
|
160 */
|
Chris@0
|
161 public function testDeleteField() {
|
Chris@0
|
162 $bundle = reset($this->bundles);
|
Chris@0
|
163 $field_storage = reset($this->fieldStorages);
|
Chris@0
|
164 $field_name = $field_storage->getName();
|
Chris@0
|
165 $factory = \Drupal::service('entity.query');
|
Chris@0
|
166
|
Chris@0
|
167 // There are 10 entities of this bundle.
|
Chris@0
|
168 $found = $factory->get('entity_test')
|
Chris@0
|
169 ->condition('type', $bundle)
|
Chris@0
|
170 ->execute();
|
Chris@0
|
171 $this->assertEqual(count($found), 10, 'Correct number of entities found before deleting');
|
Chris@0
|
172
|
Chris@0
|
173 // Delete the field.
|
Chris@0
|
174 $field = FieldConfig::loadByName($this->entityTypeId, $bundle, $field_name);
|
Chris@0
|
175 $field->delete();
|
Chris@0
|
176
|
Chris@0
|
177 // The field still exists, deleted.
|
Chris@0
|
178 $fields = entity_load_multiple_by_properties('field_config', ['field_storage_uuid' => $field_storage->uuid(), 'deleted' => TRUE, 'include_deleted' => TRUE]);
|
Chris@0
|
179 $this->assertEqual(count($fields), 1, 'There is one deleted field');
|
Chris@0
|
180 $field = $fields[$field->uuid()];
|
Chris@0
|
181 $this->assertEqual($field->getTargetBundle(), $bundle, 'The deleted field is for the correct bundle');
|
Chris@0
|
182
|
Chris@0
|
183 // Check that the actual stored content did not change during delete.
|
Chris@0
|
184 $storage = \Drupal::entityManager()->getStorage($this->entityTypeId);
|
Chris@0
|
185 /** @var \Drupal\Core\Entity\Sql\DefaultTableMapping $table_mapping */
|
Chris@0
|
186 $table_mapping = $storage->getTableMapping();
|
Chris@0
|
187 $table = $table_mapping->getDedicatedDataTableName($field_storage);
|
Chris@0
|
188 $column = $table_mapping->getFieldColumnName($field_storage, 'value');
|
Chris@0
|
189 $result = db_select($table, 't')
|
Chris@0
|
190 ->fields('t')
|
Chris@0
|
191 ->execute();
|
Chris@0
|
192 foreach ($result as $row) {
|
Chris@0
|
193 $this->assertEqual($this->entities[$row->entity_id]->{$field_name}->value, $row->$column);
|
Chris@0
|
194 }
|
Chris@0
|
195
|
Chris@0
|
196 // There are 0 entities of this bundle with non-deleted data.
|
Chris@0
|
197 $found = $factory->get('entity_test')
|
Chris@0
|
198 ->condition('type', $bundle)
|
Chris@0
|
199 ->condition("$field_name.deleted", 0)
|
Chris@0
|
200 ->execute();
|
Chris@0
|
201 $this->assertFalse($found, 'No entities found after deleting');
|
Chris@0
|
202
|
Chris@0
|
203 // There are 10 entities of this bundle when deleted fields are allowed, and
|
Chris@0
|
204 // their values are correct.
|
Chris@0
|
205 $found = $factory->get('entity_test')
|
Chris@0
|
206 ->condition('type', $bundle)
|
Chris@0
|
207 ->condition("$field_name.deleted", 1)
|
Chris@0
|
208 ->sort('id')
|
Chris@0
|
209 ->execute();
|
Chris@0
|
210 $this->assertEqual(count($found), 10, 'Correct number of entities found after deleting');
|
Chris@0
|
211 $this->assertFalse(array_diff($found, array_keys($this->entities)));
|
Chris@0
|
212 }
|
Chris@0
|
213
|
Chris@0
|
214 /**
|
Chris@0
|
215 * Tests that recreating a field with the name as a deleted field works.
|
Chris@0
|
216 */
|
Chris@0
|
217 public function testPurgeWithDeletedAndActiveField() {
|
Chris@0
|
218 $bundle = reset($this->bundles);
|
Chris@0
|
219 // Create another field storage.
|
Chris@0
|
220 $field_name = 'bf_3';
|
Chris@0
|
221 $deleted_field_storage = FieldStorageConfig::create([
|
Chris@0
|
222 'field_name' => $field_name,
|
Chris@0
|
223 'entity_type' => $this->entityTypeId,
|
Chris@0
|
224 'type' => 'test_field',
|
Chris@0
|
225 'cardinality' => 1
|
Chris@0
|
226 ]);
|
Chris@0
|
227 $deleted_field_storage->save();
|
Chris@0
|
228 // Create the field.
|
Chris@0
|
229 FieldConfig::create([
|
Chris@0
|
230 'field_storage' => $deleted_field_storage,
|
Chris@0
|
231 'bundle' => $bundle,
|
Chris@0
|
232 ])->save();
|
Chris@0
|
233
|
Chris@0
|
234 for ($i = 0; $i < 20; $i++) {
|
Chris@0
|
235 $entity = $this->container->get('entity_type.manager')
|
Chris@0
|
236 ->getStorage($this->entityTypeId)
|
Chris@0
|
237 ->create(['type' => $bundle]);
|
Chris@0
|
238 $entity->{$field_name}->setValue($this->_generateTestFieldValues(1));
|
Chris@0
|
239 $entity->save();
|
Chris@0
|
240 }
|
Chris@0
|
241
|
Chris@0
|
242 // Delete the field.
|
Chris@0
|
243 $deleted_field = FieldConfig::loadByName($this->entityTypeId, $bundle, $field_name);
|
Chris@0
|
244 $deleted_field->delete();
|
Chris@0
|
245 $deleted_field_uuid = $deleted_field->uuid();
|
Chris@0
|
246
|
Chris@0
|
247 // Reload the field storage.
|
Chris@0
|
248 $field_storages = entity_load_multiple_by_properties('field_storage_config', ['uuid' => $deleted_field_storage->uuid(), 'include_deleted' => TRUE]);
|
Chris@0
|
249 $deleted_field_storage = reset($field_storages);
|
Chris@0
|
250
|
Chris@0
|
251 // Create the field again.
|
Chris@0
|
252 $field_storage = FieldStorageConfig::create([
|
Chris@0
|
253 'field_name' => $field_name,
|
Chris@0
|
254 'entity_type' => $this->entityTypeId,
|
Chris@0
|
255 'type' => 'test_field',
|
Chris@0
|
256 'cardinality' => 1
|
Chris@0
|
257 ]);
|
Chris@0
|
258 $field_storage->save();
|
Chris@0
|
259 FieldConfig::create([
|
Chris@0
|
260 'field_storage' => $field_storage,
|
Chris@0
|
261 'bundle' => $bundle,
|
Chris@0
|
262 ])->save();
|
Chris@0
|
263
|
Chris@0
|
264 // The field still exists, deleted, with the same field name.
|
Chris@0
|
265 $fields = entity_load_multiple_by_properties('field_config', ['uuid' => $deleted_field_uuid, 'include_deleted' => TRUE]);
|
Chris@0
|
266 $this->assertTrue(isset($fields[$deleted_field_uuid]) && $fields[$deleted_field_uuid]->isDeleted(), 'The field exists and is deleted');
|
Chris@0
|
267 $this->assertTrue(isset($fields[$deleted_field_uuid]) && $fields[$deleted_field_uuid]->getName() == $field_name);
|
Chris@0
|
268
|
Chris@0
|
269 for ($i = 0; $i < 10; $i++) {
|
Chris@0
|
270 $entity = $this->container->get('entity_type.manager')
|
Chris@0
|
271 ->getStorage($this->entityTypeId)
|
Chris@0
|
272 ->create(['type' => $bundle]);
|
Chris@0
|
273 $entity->{$field_name}->setValue($this->_generateTestFieldValues(1));
|
Chris@0
|
274 $entity->save();
|
Chris@0
|
275 }
|
Chris@0
|
276
|
Chris@0
|
277 // Check that the two field storages have different tables.
|
Chris@0
|
278 $storage = \Drupal::entityManager()->getStorage($this->entityTypeId);
|
Chris@0
|
279 /** @var \Drupal\Core\Entity\Sql\DefaultTableMapping $table_mapping */
|
Chris@0
|
280 $table_mapping = $storage->getTableMapping();
|
Chris@0
|
281 $deleted_table_name = $table_mapping->getDedicatedDataTableName($deleted_field_storage, TRUE);
|
Chris@0
|
282 $active_table_name = $table_mapping->getDedicatedDataTableName($field_storage);
|
Chris@0
|
283
|
Chris@0
|
284 field_purge_batch(50);
|
Chris@0
|
285
|
Chris@0
|
286 // Ensure the new field still has its table and the deleted one has been
|
Chris@0
|
287 // removed.
|
Chris@0
|
288 $this->assertTrue(\Drupal::database()->schema()->tableExists($active_table_name));
|
Chris@0
|
289 $this->assertFalse(\Drupal::database()->schema()->tableExists($deleted_table_name));
|
Chris@0
|
290
|
Chris@0
|
291 // The field has been removed from the system.
|
Chris@0
|
292 $fields = entity_load_multiple_by_properties('field_config', ['field_storage_uuid' => $deleted_field_storage->uuid(), 'deleted' => TRUE, 'include_deleted' => TRUE]);
|
Chris@0
|
293 $this->assertEqual(count($fields), 0, 'The field is gone');
|
Chris@0
|
294
|
Chris@0
|
295 // Verify there are still 10 entries in the main table.
|
Chris@0
|
296 $count = \Drupal::database()
|
Chris@0
|
297 ->select('entity_test__' . $field_name, 'f')
|
Chris@0
|
298 ->fields('f', ['entity_id'])
|
Chris@0
|
299 ->condition('bundle', $bundle)
|
Chris@0
|
300 ->countQuery()
|
Chris@0
|
301 ->execute()
|
Chris@0
|
302 ->fetchField();
|
Chris@0
|
303 $this->assertEqual($count, 10);
|
Chris@0
|
304 }
|
Chris@0
|
305
|
Chris@0
|
306 /**
|
Chris@0
|
307 * Verify that field data items and fields are purged when a field storage is
|
Chris@0
|
308 * deleted.
|
Chris@0
|
309 */
|
Chris@0
|
310 public function testPurgeField() {
|
Chris@0
|
311 // Start recording hook invocations.
|
Chris@0
|
312 field_test_memorize();
|
Chris@0
|
313
|
Chris@0
|
314 $bundle = reset($this->bundles);
|
Chris@0
|
315 $field_storage = reset($this->fieldStorages);
|
Chris@0
|
316 $field_name = $field_storage->getName();
|
Chris@0
|
317
|
Chris@0
|
318 // Delete the field.
|
Chris@0
|
319 $field = FieldConfig::loadByName($this->entityTypeId, $bundle, $field_name);
|
Chris@0
|
320 $field->delete();
|
Chris@0
|
321
|
Chris@0
|
322 // No field hooks were called.
|
Chris@0
|
323 $mem = field_test_memorize();
|
Chris@0
|
324 $this->assertEqual(count($mem), 0, 'No field hooks were called');
|
Chris@0
|
325
|
Chris@0
|
326 $batch_size = 2;
|
Chris@0
|
327 for ($count = 8; $count >= 0; $count -= $batch_size) {
|
Chris@0
|
328 // Purge two entities.
|
Chris@0
|
329 field_purge_batch($batch_size);
|
Chris@0
|
330
|
Chris@0
|
331 // There are $count deleted entities left.
|
Chris@0
|
332 $found = \Drupal::entityQuery('entity_test')
|
Chris@0
|
333 ->condition('type', $bundle)
|
Chris@0
|
334 ->condition($field_name . '.deleted', 1)
|
Chris@0
|
335 ->execute();
|
Chris@0
|
336 $this->assertEqual(count($found), $count, 'Correct number of entities found after purging 2');
|
Chris@0
|
337 }
|
Chris@0
|
338
|
Chris@0
|
339 // Check hooks invocations.
|
Chris@0
|
340 // FieldItemInterface::delete() should have been called once for each entity in the
|
Chris@0
|
341 // bundle.
|
Chris@0
|
342 $actual_hooks = field_test_memorize();
|
Chris@0
|
343 $hooks = [];
|
Chris@0
|
344 $entities = $this->entitiesByBundles[$bundle];
|
Chris@0
|
345 foreach ($entities as $id => $entity) {
|
Chris@0
|
346 $hooks['field_test_field_delete'][] = $entity;
|
Chris@0
|
347 }
|
Chris@0
|
348 $this->checkHooksInvocations($hooks, $actual_hooks);
|
Chris@0
|
349
|
Chris@0
|
350 // The field still exists, deleted.
|
Chris@0
|
351 $fields = entity_load_multiple_by_properties('field_config', ['field_storage_uuid' => $field_storage->uuid(), 'deleted' => TRUE, 'include_deleted' => TRUE]);
|
Chris@0
|
352 $this->assertEqual(count($fields), 1, 'There is one deleted field');
|
Chris@0
|
353
|
Chris@0
|
354 // Purge the field.
|
Chris@0
|
355 field_purge_batch($batch_size);
|
Chris@0
|
356
|
Chris@0
|
357 // The field is gone.
|
Chris@0
|
358 $fields = entity_load_multiple_by_properties('field_config', ['field_storage_uuid' => $field_storage->uuid(), 'deleted' => TRUE, 'include_deleted' => TRUE]);
|
Chris@0
|
359 $this->assertEqual(count($fields), 0, 'The field is gone');
|
Chris@0
|
360
|
Chris@0
|
361 // The field storage still exists, not deleted, because it has a second
|
Chris@0
|
362 // field.
|
Chris@0
|
363 $storages = entity_load_multiple_by_properties('field_storage_config', ['uuid' => $field_storage->uuid(), 'include_deleted' => TRUE]);
|
Chris@0
|
364 $this->assertTrue(isset($storages[$field_storage->uuid()]), 'The field storage exists and is not deleted');
|
Chris@0
|
365 }
|
Chris@0
|
366
|
Chris@0
|
367 /**
|
Chris@0
|
368 * Verify that field storages are preserved and purged correctly as multiple
|
Chris@0
|
369 * fields are deleted and purged.
|
Chris@0
|
370 */
|
Chris@0
|
371 public function testPurgeFieldStorage() {
|
Chris@0
|
372 // Start recording hook invocations.
|
Chris@0
|
373 field_test_memorize();
|
Chris@0
|
374
|
Chris@0
|
375 $field_storage = reset($this->fieldStorages);
|
Chris@0
|
376 $field_name = $field_storage->getName();
|
Chris@0
|
377
|
Chris@0
|
378 // Delete the first field.
|
Chris@0
|
379 $bundle = reset($this->bundles);
|
Chris@0
|
380 $field = FieldConfig::loadByName($this->entityTypeId, $bundle, $field_name);
|
Chris@0
|
381 $field->delete();
|
Chris@0
|
382
|
Chris@0
|
383 // Assert that FieldItemInterface::delete() was not called yet.
|
Chris@0
|
384 $mem = field_test_memorize();
|
Chris@0
|
385 $this->assertEqual(count($mem), 0, 'No field hooks were called.');
|
Chris@0
|
386
|
Chris@0
|
387 // Purge the data.
|
Chris@0
|
388 field_purge_batch(10);
|
Chris@0
|
389
|
Chris@0
|
390 // Check hooks invocations.
|
Chris@0
|
391 // FieldItemInterface::delete() should have been called once for each entity in the
|
Chris@0
|
392 // bundle.
|
Chris@0
|
393 $actual_hooks = field_test_memorize();
|
Chris@0
|
394 $hooks = [];
|
Chris@0
|
395 $entities = $this->entitiesByBundles[$bundle];
|
Chris@0
|
396 foreach ($entities as $id => $entity) {
|
Chris@0
|
397 $hooks['field_test_field_delete'][] = $entity;
|
Chris@0
|
398 }
|
Chris@0
|
399 $this->checkHooksInvocations($hooks, $actual_hooks);
|
Chris@0
|
400
|
Chris@0
|
401 // The field still exists, deleted.
|
Chris@0
|
402 $fields = entity_load_multiple_by_properties('field_config', ['uuid' => $field->uuid(), 'include_deleted' => TRUE]);
|
Chris@0
|
403 $this->assertTrue(isset($fields[$field->uuid()]) && $fields[$field->uuid()]->isDeleted(), 'The field exists and is deleted');
|
Chris@0
|
404
|
Chris@0
|
405 // Purge again to purge the field.
|
Chris@0
|
406 field_purge_batch(0);
|
Chris@0
|
407
|
Chris@0
|
408 // The field is gone.
|
Chris@0
|
409 $fields = entity_load_multiple_by_properties('field_config', ['uuid' => $field->uuid(), 'include_deleted' => TRUE]);
|
Chris@0
|
410 $this->assertEqual(count($fields), 0, 'The field is purged.');
|
Chris@0
|
411 // The field storage still exists, not deleted.
|
Chris@0
|
412 $storages = entity_load_multiple_by_properties('field_storage_config', ['uuid' => $field_storage->uuid(), 'include_deleted' => TRUE]);
|
Chris@0
|
413 $this->assertTrue(isset($storages[$field_storage->uuid()]) && !$storages[$field_storage->uuid()]->isDeleted(), 'The field storage exists and is not deleted');
|
Chris@0
|
414
|
Chris@0
|
415 // Delete the second field.
|
Chris@0
|
416 $bundle = next($this->bundles);
|
Chris@0
|
417 $field = FieldConfig::loadByName($this->entityTypeId, $bundle, $field_name);
|
Chris@0
|
418 $field->delete();
|
Chris@0
|
419
|
Chris@0
|
420 // Assert that FieldItemInterface::delete() was not called yet.
|
Chris@0
|
421 $mem = field_test_memorize();
|
Chris@0
|
422 $this->assertEqual(count($mem), 0, 'No field hooks were called.');
|
Chris@0
|
423
|
Chris@0
|
424 // Purge the data.
|
Chris@0
|
425 field_purge_batch(10);
|
Chris@0
|
426
|
Chris@0
|
427 // Check hooks invocations (same as above, for the 2nd bundle).
|
Chris@0
|
428 $actual_hooks = field_test_memorize();
|
Chris@0
|
429 $hooks = [];
|
Chris@0
|
430 $entities = $this->entitiesByBundles[$bundle];
|
Chris@0
|
431 foreach ($entities as $id => $entity) {
|
Chris@0
|
432 $hooks['field_test_field_delete'][] = $entity;
|
Chris@0
|
433 }
|
Chris@0
|
434 $this->checkHooksInvocations($hooks, $actual_hooks);
|
Chris@0
|
435
|
Chris@0
|
436 // The field and the storage still exist, deleted.
|
Chris@0
|
437 $fields = entity_load_multiple_by_properties('field_config', ['uuid' => $field->uuid(), 'include_deleted' => TRUE]);
|
Chris@0
|
438 $this->assertTrue(isset($fields[$field->uuid()]) && $fields[$field->uuid()]->isDeleted(), 'The field exists and is deleted');
|
Chris@0
|
439 $storages = entity_load_multiple_by_properties('field_storage_config', ['uuid' => $field_storage->uuid(), 'include_deleted' => TRUE]);
|
Chris@0
|
440 $this->assertTrue(isset($storages[$field_storage->uuid()]) && $storages[$field_storage->uuid()]->isDeleted(), 'The field storage exists and is deleted');
|
Chris@0
|
441
|
Chris@0
|
442 // Purge again to purge the field and the storage.
|
Chris@0
|
443 field_purge_batch(0);
|
Chris@0
|
444
|
Chris@0
|
445 // The field and the storage are gone.
|
Chris@0
|
446 $fields = entity_load_multiple_by_properties('field_config', ['uuid' => $field->uuid(), 'include_deleted' => TRUE]);
|
Chris@0
|
447 $this->assertEqual(count($fields), 0, 'The field is purged.');
|
Chris@0
|
448 $storages = entity_load_multiple_by_properties('field_storage_config', ['uuid' => $field_storage->uuid(), 'include_deleted' => TRUE]);
|
Chris@0
|
449 $this->assertEqual(count($storages), 0, 'The field storage is purged.');
|
Chris@0
|
450 }
|
Chris@0
|
451
|
Chris@0
|
452 }
|