View source
<?php
namespace Drupal\Tests\field_ui\Functional;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\field_ui\Traits\FieldUiTestTrait;
use Drupal\views\Entity\View;
use Drupal\views\Tests\ViewTestData;
class FieldUIDeleteTest extends BrowserTestBase {
use FieldUiTestTrait;
public static $modules = [
'node',
'field_ui',
'field_test',
'block',
'field_test_views',
];
protected $defaultTheme = 'stark';
public static $testViews = [
'test_view_field_delete',
];
protected function setUp() {
parent::setUp();
$this
->drupalPlaceBlock('system_breadcrumb_block');
$this
->drupalPlaceBlock('local_tasks_block');
$this
->drupalPlaceBlock('page_title_block');
$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',
]);
$this
->drupalLogin($admin_user);
}
public function testDeleteField() {
$field_label = $this
->randomMachineName();
$field_name_input = 'test';
$field_name = 'field_test';
$type_name1 = strtolower($this
->randomMachineName(8)) . '_test';
$type1 = $this
->drupalCreateContentType([
'name' => $type_name1,
'type' => $type_name1,
]);
$type_name1 = $type1
->id();
$bundle_path1 = 'admin/structure/types/manage/' . $type_name1;
$this
->fieldUIAddNewField($bundle_path1, $field_name_input, $field_label);
$type_name2 = strtolower($this
->randomMachineName(8)) . '_test';
$type2 = $this
->drupalCreateContentType([
'name' => $type_name2,
'type' => $type_name2,
]);
$type_name2 = $type2
->id();
$bundle_path2 = 'admin/structure/types/manage/' . $type_name2;
$this
->fieldUIAddExistingField($bundle_path2, $field_name, $field_label);
\Drupal::service('module_installer')
->install([
'views',
]);
ViewTestData::createTestViews(get_class($this), [
'field_test_views',
]);
$view = View::load('test_view_field_delete');
$this
->assertNotNull($view);
$this
->assertTrue($view
->status());
$dependencies = $view
->getDependencies() + [
'config' => [],
];
$this
->assertContains("field.storage.node.{$field_name}", $dependencies['config']);
$this
->drupalGet("{$bundle_path1}/fields/node.{$type_name1}.{$field_name}/delete");
$this
->assertNoText(t('The listed configuration will be deleted.'));
$this
->assertNoText(t('View'));
$this
->assertNoText('test_view_field_delete');
$this
->fieldUIDeleteField($bundle_path1, "node.{$type_name1}.{$field_name}", $field_label, $type_name1);
$this
->assertNull(FieldConfig::loadByName('node', $type_name1, $field_name), 'Field was deleted.');
$this
->assertNotNull(FieldStorageConfig::loadByName('node', $field_name), 'Field storage was not deleted.');
$this
->drupalGet("{$bundle_path2}/fields/node.{$type_name2}.{$field_name}/delete");
$this
->assertText(t('The listed configuration will be updated.'));
$this
->assertText(t('View'));
$this
->assertText('test_view_field_delete');
$xml = $this
->cssSelect('#edit-entity-deletes');
$this
->assertFalse(isset($xml[0]), 'The field currently being deleted is not shown in the entity deletions.');
$this
->fieldUIDeleteField($bundle_path2, "node.{$type_name2}.{$field_name}", $field_label, $type_name2);
$this
->assertNull(FieldConfig::loadByName('node', $type_name2, $field_name), 'Field was deleted.');
$this
->assertNull(FieldStorageConfig::loadByName('node', $field_name), 'Field storage was deleted.');
$view = View::load('test_view_field_delete');
$this
->assertNotNull($view);
$this
->assertFalse($view
->status());
$dependencies = $view
->getDependencies() + [
'config' => [],
];
$this
->assertNotContains("field.storage.node.{$field_name}", $dependencies['config']);
}
}