annotate core/modules/field_ui/src/Tests/FieldUIDeleteTest.php @ 6:875880e46745

Styling
author Chris Cannam
date Fri, 08 Dec 2017 13:21:27 +0000
parents 4c8ae668cc8c
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\field_ui\Tests;
Chris@0 4
Chris@0 5 use Drupal\field\Entity\FieldConfig;
Chris@0 6 use Drupal\field\Entity\FieldStorageConfig;
Chris@0 7 use Drupal\simpletest\WebTestBase;
Chris@0 8 use Drupal\views\Entity\View;
Chris@0 9 use Drupal\views\Tests\ViewTestData;
Chris@0 10
Chris@0 11 /**
Chris@0 12 * Tests deletion of a field and their dependencies in the UI.
Chris@0 13 *
Chris@0 14 * @group field_ui
Chris@0 15 */
Chris@0 16 class FieldUIDeleteTest extends WebTestBase {
Chris@0 17
Chris@0 18 use FieldUiTestTrait;
Chris@0 19
Chris@0 20 /**
Chris@0 21 * Modules to install.
Chris@0 22 *
Chris@0 23 * @var array
Chris@0 24 */
Chris@0 25 public static $modules = ['node', 'field_ui', 'field_test', 'block', 'field_test_views'];
Chris@0 26
Chris@0 27 /**
Chris@0 28 * Test views to enable
Chris@0 29 *
Chris@0 30 * @var string[]
Chris@0 31 */
Chris@0 32 public static $testViews = ['test_view_field_delete'];
Chris@0 33
Chris@0 34 /**
Chris@0 35 * {@inheritdoc}
Chris@0 36 */
Chris@0 37 protected function setUp() {
Chris@0 38 parent::setUp();
Chris@0 39
Chris@0 40 $this->drupalPlaceBlock('system_breadcrumb_block');
Chris@0 41 $this->drupalPlaceBlock('local_tasks_block');
Chris@0 42 $this->drupalPlaceBlock('page_title_block');
Chris@0 43
Chris@0 44 // Create a test user.
Chris@0 45 $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 46 $this->drupalLogin($admin_user);
Chris@0 47 }
Chris@0 48
Chris@0 49 /**
Chris@0 50 * Tests that deletion removes field storages and fields as expected.
Chris@0 51 */
Chris@0 52 public function testDeleteField() {
Chris@0 53 $field_label = $this->randomMachineName();
Chris@0 54 $field_name_input = 'test';
Chris@0 55 $field_name = 'field_test';
Chris@0 56
Chris@0 57 // Create an additional node type.
Chris@0 58 $type_name1 = strtolower($this->randomMachineName(8)) . '_test';
Chris@0 59 $type1 = $this->drupalCreateContentType(['name' => $type_name1, 'type' => $type_name1]);
Chris@0 60 $type_name1 = $type1->id();
Chris@0 61
Chris@0 62 // Create a new field.
Chris@0 63 $bundle_path1 = 'admin/structure/types/manage/' . $type_name1;
Chris@0 64 $this->fieldUIAddNewField($bundle_path1, $field_name_input, $field_label);
Chris@0 65
Chris@0 66 // Create an additional node type.
Chris@0 67 $type_name2 = strtolower($this->randomMachineName(8)) . '_test';
Chris@0 68 $type2 = $this->drupalCreateContentType(['name' => $type_name2, 'type' => $type_name2]);
Chris@0 69 $type_name2 = $type2->id();
Chris@0 70
Chris@0 71 // Add a field to the second node type.
Chris@0 72 $bundle_path2 = 'admin/structure/types/manage/' . $type_name2;
Chris@0 73 $this->fieldUIAddExistingField($bundle_path2, $field_name, $field_label);
Chris@0 74
Chris@0 75 \Drupal::service('module_installer')->install(['views']);
Chris@0 76 ViewTestData::createTestViews(get_class($this), ['field_test_views']);
Chris@0 77
Chris@0 78 $view = View::load('test_view_field_delete');
Chris@0 79 $this->assertNotNull($view);
Chris@0 80 $this->assertTrue($view->status());
Chris@0 81 // Test that the View depends on the field.
Chris@0 82 $dependencies = $view->getDependencies() + ['config' => []];
Chris@0 83 $this->assertTrue(in_array("field.storage.node.$field_name", $dependencies['config']));
Chris@0 84
Chris@0 85 // Check the config dependencies of the first field, the field storage must
Chris@0 86 // not be shown as being deleted yet.
Chris@0 87 $this->drupalGet("$bundle_path1/fields/node.$type_name1.$field_name/delete");
Chris@0 88 $this->assertNoText(t('The listed configuration will be deleted.'));
Chris@0 89 $this->assertNoText(t('View'));
Chris@0 90 $this->assertNoText('test_view_field_delete');
Chris@0 91
Chris@0 92 // Delete the first field.
Chris@0 93 $this->fieldUIDeleteField($bundle_path1, "node.$type_name1.$field_name", $field_label, $type_name1);
Chris@0 94
Chris@0 95 // Check that the field was deleted.
Chris@0 96 $this->assertNull(FieldConfig::loadByName('node', $type_name1, $field_name), 'Field was deleted.');
Chris@0 97 // Check that the field storage was not deleted
Chris@0 98 $this->assertNotNull(FieldStorageConfig::loadByName('node', $field_name), 'Field storage was not deleted.');
Chris@0 99
Chris@0 100 // Check the config dependencies of the first field.
Chris@0 101 $this->drupalGet("$bundle_path2/fields/node.$type_name2.$field_name/delete");
Chris@0 102 $this->assertText(t('The listed configuration will be updated.'));
Chris@0 103 $this->assertText(t('View'));
Chris@0 104 $this->assertText('test_view_field_delete');
Chris@0 105
Chris@0 106 $xml = $this->cssSelect('#edit-entity-deletes');
Chris@0 107 // Test that nothing is scheduled for deletion.
Chris@0 108 $this->assertFalse(isset($xml[0]), 'The field currently being deleted is not shown in the entity deletions.');
Chris@0 109
Chris@0 110 // Delete the second field.
Chris@0 111 $this->fieldUIDeleteField($bundle_path2, "node.$type_name2.$field_name", $field_label, $type_name2);
Chris@0 112
Chris@0 113 // Check that the field was deleted.
Chris@0 114 $this->assertNull(FieldConfig::loadByName('node', $type_name2, $field_name), 'Field was deleted.');
Chris@0 115 // Check that the field storage was deleted too.
Chris@0 116 $this->assertNull(FieldStorageConfig::loadByName('node', $field_name), 'Field storage was deleted.');
Chris@0 117
Chris@0 118 // Test that the View isn't deleted and has been disabled.
Chris@0 119 $view = View::load('test_view_field_delete');
Chris@0 120 $this->assertNotNull($view);
Chris@0 121 $this->assertFalse($view->status());
Chris@0 122 // Test that the View no longer depends on the deleted field.
Chris@0 123 $dependencies = $view->getDependencies() + ['config' => []];
Chris@0 124 $this->assertFalse(in_array("field.storage.node.$field_name", $dependencies['config']));
Chris@0 125 }
Chris@0 126
Chris@0 127 }