Version 1
[yaffs-website] / web / core / modules / field_ui / src / Tests / FieldUIDeleteTest.php
1 <?php
2
3 namespace Drupal\field_ui\Tests;
4
5 use Drupal\field\Entity\FieldConfig;
6 use Drupal\field\Entity\FieldStorageConfig;
7 use Drupal\simpletest\WebTestBase;
8 use Drupal\views\Tests\ViewTestData;
9
10 /**
11  * Tests deletion of a field and their dependencies in the UI.
12  *
13  * @group field_ui
14  */
15 class FieldUIDeleteTest extends WebTestBase {
16
17   use FieldUiTestTrait;
18
19   /**
20    * Modules to install.
21    *
22    * @var array
23    */
24   public static $modules = ['node', 'field_ui', 'field_test', 'block', 'field_test_views'];
25
26   /**
27    * Test views to enable
28    *
29    * @var string[]
30    */
31   public static $testViews = ['test_view_field_delete'];
32
33   /**
34    * {@inheritdoc}
35    */
36   protected function setUp() {
37     parent::setUp();
38
39     $this->drupalPlaceBlock('system_breadcrumb_block');
40     $this->drupalPlaceBlock('local_tasks_block');
41     $this->drupalPlaceBlock('page_title_block');
42
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);
46   }
47
48   /**
49    * Tests that deletion removes field storages and fields as expected.
50    */
51   public function testDeleteField() {
52     $field_label = $this->randomMachineName();
53     $field_name_input = 'test';
54     $field_name = 'field_test';
55
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();
60
61     // Create a new field.
62     $bundle_path1 = 'admin/structure/types/manage/' . $type_name1;
63     $this->fieldUIAddNewField($bundle_path1, $field_name_input, $field_label);
64
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();
69
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);
73
74     \Drupal::service('module_installer')->install(['views']);
75     ViewTestData::createTestViews(get_class($this), ['field_test_views']);
76
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');
83
84     // Delete the first field.
85     $this->fieldUIDeleteField($bundle_path1, "node.$type_name1.$field_name", $field_label, $type_name1);
86
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.');
91
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');
97
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.');
101
102     // Delete the second field.
103     $this->fieldUIDeleteField($bundle_path2, "node.$type_name2.$field_name", $field_label, $type_name2);
104
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.');
109   }
110
111 }