You are here

public function FieldUpdateTest::testFieldUpdate8500 in Drupal 8

Tests field_update_8500().

See also

field_update_8500()

File

core/modules/field/tests/src/Functional/Update/FieldUpdateTest.php, line 167

Class

FieldUpdateTest
Tests that field settings are properly updated during database updates.

Namespace

Drupal\Tests\field\Functional\Update

Code

public function testFieldUpdate8500() {
  $field_name = 'field_test';
  $field_uuid = '5d0d9870-560b-46c4-b838-0dcded0502dd';
  $field_storage_uuid = 'ce93d7c2-1da7-4a2c-9e6d-b4925e3b129f';

  // Check that we have pre-existing entries for 'field.field.deleted' and
  // 'field.storage.deleted'.
  $deleted_fields = $this->state
    ->get('field.field.deleted');
  $this
    ->assertCount(1, $deleted_fields);
  $this
    ->assertArrayHasKey($field_uuid, $deleted_fields);
  $deleted_field_storages = $this->state
    ->get('field.storage.deleted');
  $this
    ->assertCount(1, $deleted_field_storages);
  $this
    ->assertArrayHasKey($field_storage_uuid, $deleted_field_storages);

  // Ensure that cron does not run automatically after running the updates.
  $this->state
    ->set('system.cron_last', REQUEST_TIME + 100);

  // Run updates.
  $this
    ->runUpdates();
  $deleted_fields_repository = \Drupal::service('entity_field.deleted_fields_repository');

  // Now that we can use the API, check that the "delete fields" state entries
  // have been converted to proper field definition objects.
  $deleted_fields = $deleted_fields_repository
    ->getFieldDefinitions();
  $this
    ->assertCount(1, $deleted_fields);
  $this
    ->assertArrayHasKey($field_uuid, $deleted_fields);
  $this
    ->assertInstanceOf(FieldDefinitionInterface::class, $deleted_fields[$field_uuid]);
  $this
    ->assertEquals($field_name, $deleted_fields[$field_uuid]
    ->getName());
  $deleted_field_storages = $deleted_fields_repository
    ->getFieldStorageDefinitions();
  $this
    ->assertCount(1, $deleted_field_storages);
  $this
    ->assertArrayHasKey($field_storage_uuid, $deleted_field_storages);
  $this
    ->assertInstanceOf(FieldStorageDefinitionInterface::class, $deleted_field_storages[$field_storage_uuid]);
  $this
    ->assertEquals($field_name, $deleted_field_storages[$field_storage_uuid]
    ->getName());

  // Check that the installed storage schema still exists.
  $this
    ->assertNotNull($this->installedStorageSchema
    ->get("node.field_schema_data.{$field_name}"));

  // Check that the deleted field tables exist.

  /** @var \Drupal\Core\Entity\Sql\DefaultTableMapping $table_mapping */
  $table_mapping = \Drupal::entityTypeManager()
    ->getStorage('node')
    ->getTableMapping();
  $deleted_field_data_table_name = $table_mapping
    ->getDedicatedDataTableName($deleted_field_storages[$field_storage_uuid], TRUE);
  $this
    ->assertTrue($this->database
    ->schema()
    ->tableExists($deleted_field_data_table_name));
  $deleted_field_revision_table_name = $table_mapping
    ->getDedicatedRevisionTableName($deleted_field_storages[$field_storage_uuid], TRUE);
  $this
    ->assertTrue($this->database
    ->schema()
    ->tableExists($deleted_field_revision_table_name));

  // Run cron and repeat the checks above.
  $this
    ->cronRun();
  $deleted_fields = $deleted_fields_repository
    ->getFieldDefinitions();
  $this
    ->assertCount(0, $deleted_fields);
  $deleted_field_storages = $deleted_fields_repository
    ->getFieldStorageDefinitions();
  $this
    ->assertCount(0, $deleted_field_storages);

  // Check that the installed storage schema has been deleted.
  $this
    ->assertNull($this->installedStorageSchema
    ->get("node.field_schema_data.{$field_name}"));

  // Check that the deleted field tables have been deleted.
  $this
    ->assertFalse($this->database
    ->schema()
    ->tableExists($deleted_field_data_table_name));
  $this
    ->assertFalse($this->database
    ->schema()
    ->tableExists($deleted_field_revision_table_name));
}