You are here

public function EntityDefinitionUpdateTest::testBaseFieldCreateUpdateDeleteWithoutData in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/modules/system/src/Tests/Entity/EntityDefinitionUpdateTest.php \Drupal\system\Tests\Entity\EntityDefinitionUpdateTest::testBaseFieldCreateUpdateDeleteWithoutData()

Tests creating, updating, and deleting a base field if no entities exist.

File

core/modules/system/src/Tests/Entity/EntityDefinitionUpdateTest.php, line 140
Contains \Drupal\system\Tests\Entity\EntityDefinitionUpdateTest.

Class

EntityDefinitionUpdateTest
Tests EntityDefinitionUpdateManager functionality.

Namespace

Drupal\system\Tests\Entity

Code

public function testBaseFieldCreateUpdateDeleteWithoutData() {

  // Add a base field, ensure the update manager reports it, and the update
  // creates its schema.
  $this
    ->addBaseField();
  $this
    ->assertTrue($this->entityDefinitionUpdateManager
    ->needsUpdates(), 'EntityDefinitionUpdateManager reports that updates are needed.');
  $expected = array(
    'entity_test_update' => array(
      t('Create the %field_name field.', array(
        '%field_name' => t('A new base field'),
      )),
    ),
  );
  $this
    ->assertEqual($this->entityDefinitionUpdateManager
    ->getChangeSummary(), $expected, 'EntityDefinitionUpdateManager reports the expected change summary.');
  $this->entityDefinitionUpdateManager
    ->applyUpdates();
  $this
    ->assertTrue($this->database
    ->schema()
    ->fieldExists('entity_test_update', 'new_base_field'), 'Column created in shared table for new_base_field.');

  // Add an index on the base field, ensure the update manager reports it,
  // and the update creates it.
  $this
    ->addBaseFieldIndex();
  $this
    ->assertTrue($this->entityDefinitionUpdateManager
    ->needsUpdates(), 'EntityDefinitionUpdateManager reports that updates are needed.');
  $expected = array(
    'entity_test_update' => array(
      t('Update the %field_name field.', array(
        '%field_name' => t('A new base field'),
      )),
    ),
  );
  $this
    ->assertEqual($this->entityDefinitionUpdateManager
    ->getChangeSummary(), $expected, 'EntityDefinitionUpdateManager reports the expected change summary.');
  $this->entityDefinitionUpdateManager
    ->applyUpdates();
  $this
    ->assertTrue($this->database
    ->schema()
    ->indexExists('entity_test_update', 'entity_test_update_field__new_base_field'), 'Index created.');

  // Remove the above index, ensure the update manager reports it, and the
  // update deletes it.
  $this
    ->removeBaseFieldIndex();
  $this
    ->assertTrue($this->entityDefinitionUpdateManager
    ->needsUpdates(), 'EntityDefinitionUpdateManager reports that updates are needed.');
  $expected = array(
    'entity_test_update' => array(
      t('Update the %field_name field.', array(
        '%field_name' => t('A new base field'),
      )),
    ),
  );
  $this
    ->assertEqual($this->entityDefinitionUpdateManager
    ->getChangeSummary(), $expected, 'EntityDefinitionUpdateManager reports the expected change summary.');
  $this->entityDefinitionUpdateManager
    ->applyUpdates();
  $this
    ->assertFalse($this->database
    ->schema()
    ->indexExists('entity_test_update', 'entity_test_update_field__new_base_field'), 'Index deleted.');

  // Update the type of the base field from 'string' to 'text', ensure the
  // update manager reports it, and the update adjusts the schema
  // accordingly.
  $this
    ->modifyBaseField();
  $this
    ->assertTrue($this->entityDefinitionUpdateManager
    ->needsUpdates(), 'EntityDefinitionUpdateManager reports that updates are needed.');
  $expected = array(
    'entity_test_update' => array(
      t('Update the %field_name field.', array(
        '%field_name' => t('A new base field'),
      )),
    ),
  );
  $this
    ->assertEqual($this->entityDefinitionUpdateManager
    ->getChangeSummary(), $expected, 'EntityDefinitionUpdateManager reports the expected change summary.');
  $this->entityDefinitionUpdateManager
    ->applyUpdates();
  $this
    ->assertFalse($this->database
    ->schema()
    ->fieldExists('entity_test_update', 'new_base_field'), 'Original column deleted in shared table for new_base_field.');
  $this
    ->assertTrue($this->database
    ->schema()
    ->fieldExists('entity_test_update', 'new_base_field__value'), 'Value column created in shared table for new_base_field.');
  $this
    ->assertTrue($this->database
    ->schema()
    ->fieldExists('entity_test_update', 'new_base_field__format'), 'Format column created in shared table for new_base_field.');

  // Remove the base field, ensure the update manager reports it, and the
  // update deletes the schema.
  $this
    ->removeBaseField();
  $this
    ->assertTrue($this->entityDefinitionUpdateManager
    ->needsUpdates(), 'EntityDefinitionUpdateManager reports that updates are needed.');
  $expected = array(
    'entity_test_update' => array(
      t('Delete the %field_name field.', array(
        '%field_name' => t('A new base field'),
      )),
    ),
  );
  $this
    ->assertEqual($this->entityDefinitionUpdateManager
    ->getChangeSummary(), $expected, 'EntityDefinitionUpdateManager reports the expected change summary.');
  $this->entityDefinitionUpdateManager
    ->applyUpdates();
  $this
    ->assertFalse($this->database
    ->schema()
    ->fieldExists('entity_test_update', 'new_base_field_value'), 'Value column deleted from shared table for new_base_field.');
  $this
    ->assertFalse($this->database
    ->schema()
    ->fieldExists('entity_test_update', 'new_base_field_format'), 'Format column deleted from shared table for new_base_field.');
}