comparison core/modules/field/tests/src/Kernel/BulkDeleteTest.php @ 17:129ea1e6d783

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:21:36 +0000
parents 4c8ae668cc8c
children af1871eacc83
comparison
equal deleted inserted replaced
16:c2387f117808 17:129ea1e6d783
103 // Create two field storages. 103 // Create two field storages.
104 $field_storage = FieldStorageConfig::create([ 104 $field_storage = FieldStorageConfig::create([
105 'field_name' => 'bf_1', 105 'field_name' => 'bf_1',
106 'entity_type' => $this->entityTypeId, 106 'entity_type' => $this->entityTypeId,
107 'type' => 'test_field', 107 'type' => 'test_field',
108 'cardinality' => 1 108 'cardinality' => 1,
109 ]); 109 ]);
110 $field_storage->save(); 110 $field_storage->save();
111 $this->fieldStorages[] = $field_storage; 111 $this->fieldStorages[] = $field_storage;
112 $field_storage = FieldStorageConfig::create([ 112 $field_storage = FieldStorageConfig::create([
113 'field_name' => 'bf_2', 113 'field_name' => 'bf_2',
114 'entity_type' => $this->entityTypeId, 114 'entity_type' => $this->entityTypeId,
115 'type' => 'test_field', 115 'type' => 'test_field',
116 'cardinality' => 4 116 'cardinality' => 4,
117 ]); 117 ]);
118 $field_storage->save(); 118 $field_storage->save();
119 $this->fieldStorages[] = $field_storage; 119 $this->fieldStorages[] = $field_storage;
120 120
121 // For each bundle, create each field, and 10 entities with values for the 121 // For each bundle, create each field, and 10 entities with values for the
160 */ 160 */
161 public function testDeleteField() { 161 public function testDeleteField() {
162 $bundle = reset($this->bundles); 162 $bundle = reset($this->bundles);
163 $field_storage = reset($this->fieldStorages); 163 $field_storage = reset($this->fieldStorages);
164 $field_name = $field_storage->getName(); 164 $field_name = $field_storage->getName();
165 $factory = \Drupal::service('entity.query'); 165 $storage = \Drupal::entityTypeManager()->getStorage('entity_test');
166 166
167 // There are 10 entities of this bundle. 167 // There are 10 entities of this bundle.
168 $found = $factory->get('entity_test') 168 $found = $storage
169 ->getQuery()
169 ->condition('type', $bundle) 170 ->condition('type', $bundle)
170 ->execute(); 171 ->execute();
171 $this->assertEqual(count($found), 10, 'Correct number of entities found before deleting'); 172 $this->assertEqual(count($found), 10, 'Correct number of entities found before deleting');
172 173
173 // Delete the field. 174 // Delete the field.
179 $this->assertEqual(count($fields), 1, 'There is one deleted field'); 180 $this->assertEqual(count($fields), 1, 'There is one deleted field');
180 $field = $fields[$field->uuid()]; 181 $field = $fields[$field->uuid()];
181 $this->assertEqual($field->getTargetBundle(), $bundle, 'The deleted field is for the correct bundle'); 182 $this->assertEqual($field->getTargetBundle(), $bundle, 'The deleted field is for the correct bundle');
182 183
183 // Check that the actual stored content did not change during delete. 184 // Check that the actual stored content did not change during delete.
184 $storage = \Drupal::entityManager()->getStorage($this->entityTypeId);
185 /** @var \Drupal\Core\Entity\Sql\DefaultTableMapping $table_mapping */ 185 /** @var \Drupal\Core\Entity\Sql\DefaultTableMapping $table_mapping */
186 $table_mapping = $storage->getTableMapping(); 186 $table_mapping = $storage->getTableMapping();
187 $table = $table_mapping->getDedicatedDataTableName($field_storage); 187 $table = $table_mapping->getDedicatedDataTableName($field_storage);
188 $column = $table_mapping->getFieldColumnName($field_storage, 'value'); 188 $column = $table_mapping->getFieldColumnName($field_storage, 'value');
189 $result = db_select($table, 't') 189 $result = db_select($table, 't')
192 foreach ($result as $row) { 192 foreach ($result as $row) {
193 $this->assertEqual($this->entities[$row->entity_id]->{$field_name}->value, $row->$column); 193 $this->assertEqual($this->entities[$row->entity_id]->{$field_name}->value, $row->$column);
194 } 194 }
195 195
196 // There are 0 entities of this bundle with non-deleted data. 196 // There are 0 entities of this bundle with non-deleted data.
197 $found = $factory->get('entity_test') 197 $found = $storage
198 ->getQuery()
198 ->condition('type', $bundle) 199 ->condition('type', $bundle)
199 ->condition("$field_name.deleted", 0) 200 ->condition("$field_name.deleted", 0)
200 ->execute(); 201 ->execute();
201 $this->assertFalse($found, 'No entities found after deleting'); 202 $this->assertFalse($found, 'No entities found after deleting');
202 203
203 // There are 10 entities of this bundle when deleted fields are allowed, and 204 // There are 10 entities of this bundle when deleted fields are allowed, and
204 // their values are correct. 205 // their values are correct.
205 $found = $factory->get('entity_test') 206 $found = $storage
207 ->getQuery()
206 ->condition('type', $bundle) 208 ->condition('type', $bundle)
207 ->condition("$field_name.deleted", 1) 209 ->condition("$field_name.deleted", 1)
208 ->sort('id') 210 ->sort('id')
209 ->execute(); 211 ->execute();
210 $this->assertEqual(count($found), 10, 'Correct number of entities found after deleting'); 212 $this->assertEqual(count($found), 10, 'Correct number of entities found after deleting');
220 $field_name = 'bf_3'; 222 $field_name = 'bf_3';
221 $deleted_field_storage = FieldStorageConfig::create([ 223 $deleted_field_storage = FieldStorageConfig::create([
222 'field_name' => $field_name, 224 'field_name' => $field_name,
223 'entity_type' => $this->entityTypeId, 225 'entity_type' => $this->entityTypeId,
224 'type' => 'test_field', 226 'type' => 'test_field',
225 'cardinality' => 1 227 'cardinality' => 1,
226 ]); 228 ]);
227 $deleted_field_storage->save(); 229 $deleted_field_storage->save();
228 // Create the field. 230 // Create the field.
229 FieldConfig::create([ 231 FieldConfig::create([
230 'field_storage' => $deleted_field_storage, 232 'field_storage' => $deleted_field_storage,
251 // Create the field again. 253 // Create the field again.
252 $field_storage = FieldStorageConfig::create([ 254 $field_storage = FieldStorageConfig::create([
253 'field_name' => $field_name, 255 'field_name' => $field_name,
254 'entity_type' => $this->entityTypeId, 256 'entity_type' => $this->entityTypeId,
255 'type' => 'test_field', 257 'type' => 'test_field',
256 'cardinality' => 1 258 'cardinality' => 1,
257 ]); 259 ]);
258 $field_storage->save(); 260 $field_storage->save();
259 FieldConfig::create([ 261 FieldConfig::create([
260 'field_storage' => $field_storage, 262 'field_storage' => $field_storage,
261 'bundle' => $bundle, 263 'bundle' => $bundle,