FieldImportDeleteUninstallUiTest.php 4.53 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php

/**
 * @file
 * Contains \Drupal\field\Tests\FieldImportDeleteUninstallUiTest.
 */

namespace Drupal\field\Tests;

10
use Drupal\entity_test\Entity\EntityTest;
11
use Drupal\field\Entity\FieldConfig;
12 13
use Drupal\field\Entity\FieldStorageConfig;

14
/**
15 16
 * Delete field storages and fields during config synchronization and uninstall
 * module that provides the field type through the UI.
17
 *
18
 * @group field
19 20 21 22 23 24 25 26 27 28 29
 * @see \Drupal\field\ConfigImporterFieldPurger
 * @see field_config_import_steps_alter()
 * @see field_form_config_admin_import_form_alter()
 */
class FieldImportDeleteUninstallUiTest extends FieldTestBase {

  /**
   * Modules to enable.
   *
   * @var array
   */
30
  public static $modules = array('entity_test', 'telephone', 'config', 'filter', 'datetime');
31

32
  protected function setUp() {
33 34
    parent::setUp();

35
    $this->drupalLogin($this->drupalCreateUser(array('synchronize configuration')));
36 37 38
  }

  /**
39
   * Tests deleting field storages and fields as part of config import.
40 41
   */
  public function testImportDeleteUninstall() {
42
    // Create a telephone field.
43
    $field_storage = FieldStorageConfig::create(array(
44
      'field_name' => 'field_tel',
45 46 47
      'entity_type' => 'entity_test',
      'type' => 'telephone',
    ));
48
    $field_storage->save();
49
    FieldConfig::create([
50
      'field_storage' => $field_storage,
51
      'bundle' => 'entity_test',
52
    ])->save();
53

54
    // Create a text field.
55
    $date_field_storage = FieldStorageConfig::create(array(
56
      'field_name' => 'field_date',
57
      'entity_type' => 'entity_test',
58
      'type' => 'datetime',
59
    ));
60
    $date_field_storage->save();
61
    FieldConfig::create([
62
      'field_storage' => $date_field_storage,
63
      'bundle' => 'entity_test',
64
    ])->save();
65 66

    // Create an entity which has values for the telephone and text field.
67
    $entity = EntityTest::create();
68 69
    $value = '+0123456789';
    $entity->field_tel = $value;
70
    $entity->field_date = time();
71
    $entity->name->value = $this->randomMachineName();
72 73 74 75 76
    $entity->save();

    // Delete the text field before exporting configuration so that we can test
    // that deleted fields that are provided by modules that will be uninstalled
    // are also purged and that the UI message includes such fields.
77
    $date_field_storage->delete();
78 79 80 81 82 83 84 85

    // Verify entity has been created properly.
    $id = $entity->id();
    $entity = entity_load('entity_test', $id);
    $this->assertEqual($entity->field_tel->value, $value);
    $this->assertEqual($entity->field_tel[0]->value, $value);

    $active = $this->container->get('config.storage');
86 87
    $sync = $this->container->get('config.storage.sync');
    $this->copyConfig($active, $sync);
88 89

    // Stage uninstall of the Telephone module.
90
    $core_extension = $this->config('core.extension')->get();
91
    unset($core_extension['module']['telephone']);
92
    $sync->write('core.extension', $core_extension);
93 94

    // Stage the field deletion
95 96
    $sync->delete('field.storage.entity_test.field_tel');
    $sync->delete('field.field.entity_test.entity_test.field_tel');
97 98 99 100 101
    $this->drupalGet('admin/config/development/configuration');
    // Test that the message for one field being purged during a configuration
    // synchronization is correct.
    $this->assertText('This synchronization will delete data from the field entity_test.field_tel.');

102 103 104
    // Stage an uninstall of the datetime module to test the message for
    // multiple fields.
    unset($core_extension['module']['datetime']);
105
    $sync->write('core.extension', $core_extension);
106

107
    $this->drupalGet('admin/config/development/configuration');
108
    $this->assertText('This synchronization will delete data from the fields: entity_test.field_tel, entity_test.field_date.');
109 110 111 112 113 114 115

    // This will purge all the data, delete the field and uninstall the
    // Telephone and Text modules.
    $this->drupalPostForm(NULL, array(), t('Import all'));
    $this->assertNoText('Field data will be deleted by this synchronization.');
    $this->rebuildContainer();
    $this->assertFalse(\Drupal::moduleHandler()->moduleExists('telephone'));
116
    $this->assertFalse(\Drupal::entityManager()->loadEntityByUuid('field_storage_config', $field_storage->uuid()), 'The telephone field has been deleted by the configuration synchronization');
117 118 119
    $deleted_storages = \Drupal::state()->get('field.storage.deleted') ?: array();
    $this->assertFalse(isset($deleted_storages[$field_storage->uuid()]), 'Telephone field has been completed removed from the system.');
    $this->assertFalse(isset($deleted_storages[$field_storage->uuid()]), 'Text field has been completed removed from the system.');
120 121 122
  }

}