Chris@0: drupalPlaceBlock('system_breadcrumb_block'); Chris@0: $this->drupalPlaceBlock('local_tasks_block'); Chris@0: $this->drupalPlaceBlock('page_title_block'); Chris@0: Chris@0: // Create a test user. Chris@0: $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']); Chris@0: $this->drupalLogin($admin_user); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests that deletion removes field storages and fields as expected. Chris@0: */ Chris@0: public function testDeleteField() { Chris@0: $field_label = $this->randomMachineName(); Chris@0: $field_name_input = 'test'; Chris@0: $field_name = 'field_test'; Chris@0: Chris@0: // Create an additional node type. Chris@0: $type_name1 = strtolower($this->randomMachineName(8)) . '_test'; Chris@0: $type1 = $this->drupalCreateContentType(['name' => $type_name1, 'type' => $type_name1]); Chris@0: $type_name1 = $type1->id(); Chris@0: Chris@0: // Create a new field. Chris@0: $bundle_path1 = 'admin/structure/types/manage/' . $type_name1; Chris@0: $this->fieldUIAddNewField($bundle_path1, $field_name_input, $field_label); Chris@0: Chris@0: // Create an additional node type. Chris@0: $type_name2 = strtolower($this->randomMachineName(8)) . '_test'; Chris@0: $type2 = $this->drupalCreateContentType(['name' => $type_name2, 'type' => $type_name2]); Chris@0: $type_name2 = $type2->id(); Chris@0: Chris@0: // Add a field to the second node type. Chris@0: $bundle_path2 = 'admin/structure/types/manage/' . $type_name2; Chris@0: $this->fieldUIAddExistingField($bundle_path2, $field_name, $field_label); Chris@0: Chris@0: \Drupal::service('module_installer')->install(['views']); Chris@0: ViewTestData::createTestViews(get_class($this), ['field_test_views']); Chris@0: Chris@0: $view = View::load('test_view_field_delete'); Chris@0: $this->assertNotNull($view); Chris@0: $this->assertTrue($view->status()); Chris@0: // Test that the View depends on the field. Chris@0: $dependencies = $view->getDependencies() + ['config' => []]; Chris@0: $this->assertTrue(in_array("field.storage.node.$field_name", $dependencies['config'])); Chris@0: Chris@0: // Check the config dependencies of the first field, the field storage must Chris@0: // not be shown as being deleted yet. Chris@0: $this->drupalGet("$bundle_path1/fields/node.$type_name1.$field_name/delete"); Chris@0: $this->assertNoText(t('The listed configuration will be deleted.')); Chris@0: $this->assertNoText(t('View')); Chris@0: $this->assertNoText('test_view_field_delete'); Chris@0: Chris@0: // Delete the first field. Chris@0: $this->fieldUIDeleteField($bundle_path1, "node.$type_name1.$field_name", $field_label, $type_name1); Chris@0: Chris@0: // Check that the field was deleted. Chris@0: $this->assertNull(FieldConfig::loadByName('node', $type_name1, $field_name), 'Field was deleted.'); Chris@0: // Check that the field storage was not deleted Chris@0: $this->assertNotNull(FieldStorageConfig::loadByName('node', $field_name), 'Field storage was not deleted.'); Chris@0: Chris@0: // Check the config dependencies of the first field. Chris@0: $this->drupalGet("$bundle_path2/fields/node.$type_name2.$field_name/delete"); Chris@0: $this->assertText(t('The listed configuration will be updated.')); Chris@0: $this->assertText(t('View')); Chris@0: $this->assertText('test_view_field_delete'); Chris@0: Chris@0: $xml = $this->cssSelect('#edit-entity-deletes'); Chris@0: // Test that nothing is scheduled for deletion. Chris@0: $this->assertFalse(isset($xml[0]), 'The field currently being deleted is not shown in the entity deletions.'); Chris@0: Chris@0: // Delete the second field. Chris@0: $this->fieldUIDeleteField($bundle_path2, "node.$type_name2.$field_name", $field_label, $type_name2); Chris@0: Chris@0: // Check that the field was deleted. Chris@0: $this->assertNull(FieldConfig::loadByName('node', $type_name2, $field_name), 'Field was deleted.'); Chris@0: // Check that the field storage was deleted too. Chris@0: $this->assertNull(FieldStorageConfig::loadByName('node', $field_name), 'Field storage was deleted.'); Chris@0: Chris@0: // Test that the View isn't deleted and has been disabled. Chris@0: $view = View::load('test_view_field_delete'); Chris@0: $this->assertNotNull($view); Chris@0: $this->assertFalse($view->status()); Chris@0: // Test that the View no longer depends on the deleted field. Chris@0: $dependencies = $view->getDependencies() + ['config' => []]; Chris@0: $this->assertFalse(in_array("field.storage.node.$field_name", $dependencies['config'])); Chris@0: } Chris@0: Chris@0: }