3 namespace Drupal\field_ui\Tests;
5 use Drupal\field\Entity\FieldConfig;
6 use Drupal\field\Entity\FieldStorageConfig;
7 use Drupal\simpletest\WebTestBase;
8 use Drupal\views\Tests\ViewTestData;
11 * Tests deletion of a field and their dependencies in the UI.
15 class FieldUIDeleteTest extends WebTestBase {
24 public static $modules = ['node', 'field_ui', 'field_test', 'block', 'field_test_views'];
27 * Test views to enable
31 public static $testViews = ['test_view_field_delete'];
36 protected function setUp() {
39 $this->drupalPlaceBlock('system_breadcrumb_block');
40 $this->drupalPlaceBlock('local_tasks_block');
41 $this->drupalPlaceBlock('page_title_block');
43 // Create a test user.
44 $admin_user = $this->drupalCreateUser(['access content', 'administer content types', 'administer node fields', 'administer node form display', 'administer node display', 'administer users', 'administer account settings', 'administer user display', 'bypass node access']);
45 $this->drupalLogin($admin_user);
49 * Tests that deletion removes field storages and fields as expected.
51 public function testDeleteField() {
52 $field_label = $this->randomMachineName();
53 $field_name_input = 'test';
54 $field_name = 'field_test';
56 // Create an additional node type.
57 $type_name1 = strtolower($this->randomMachineName(8)) . '_test';
58 $type1 = $this->drupalCreateContentType(['name' => $type_name1, 'type' => $type_name1]);
59 $type_name1 = $type1->id();
61 // Create a new field.
62 $bundle_path1 = 'admin/structure/types/manage/' . $type_name1;
63 $this->fieldUIAddNewField($bundle_path1, $field_name_input, $field_label);
65 // Create an additional node type.
66 $type_name2 = strtolower($this->randomMachineName(8)) . '_test';
67 $type2 = $this->drupalCreateContentType(['name' => $type_name2, 'type' => $type_name2]);
68 $type_name2 = $type2->id();
70 // Add a field to the second node type.
71 $bundle_path2 = 'admin/structure/types/manage/' . $type_name2;
72 $this->fieldUIAddExistingField($bundle_path2, $field_name, $field_label);
74 \Drupal::service('module_installer')->install(['views']);
75 ViewTestData::createTestViews(get_class($this), ['field_test_views']);
77 // Check the config dependencies of the first field, the field storage must
78 // not be shown as being deleted yet.
79 $this->drupalGet("$bundle_path1/fields/node.$type_name1.$field_name/delete");
80 $this->assertNoText(t('The listed configuration will be deleted.'));
81 $this->assertNoText(t('View'));
82 $this->assertNoText('test_view_field_delete');
84 // Delete the first field.
85 $this->fieldUIDeleteField($bundle_path1, "node.$type_name1.$field_name", $field_label, $type_name1);
87 // Check that the field was deleted.
88 $this->assertNull(FieldConfig::loadByName('node', $type_name1, $field_name), 'Field was deleted.');
89 // Check that the field storage was not deleted
90 $this->assertNotNull(FieldStorageConfig::loadByName('node', $field_name), 'Field storage was not deleted.');
92 // Check the config dependencies of the first field.
93 $this->drupalGet("$bundle_path2/fields/node.$type_name2.$field_name/delete");
94 $this->assertText(t('The listed configuration will be deleted.'));
95 $this->assertText(t('View'));
96 $this->assertText('test_view_field_delete');
98 $xml = $this->cssSelect('#edit-entity-deletes');
99 // Remove the wrapping HTML.
100 $this->assertIdentical(FALSE, strpos($xml[0]->asXml(), $field_label), 'The currently being deleted field is not shown in the entity deletions.');
102 // Delete the second field.
103 $this->fieldUIDeleteField($bundle_path2, "node.$type_name2.$field_name", $field_label, $type_name2);
105 // Check that the field was deleted.
106 $this->assertNull(FieldConfig::loadByName('node', $type_name2, $field_name), 'Field was deleted.');
107 // Check that the field storage was deleted too.
108 $this->assertNull(FieldStorageConfig::loadByName('node', $field_name), 'Field storage was deleted.');