You are here

public function EntityDefinitionUpdateTest::testBaseFieldCreateDeleteWithExistingEntities in Zircon Profile 8

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

Tests creating and deleting a base field if entities exist.

This tests deletion when there are existing entities, but not existing data for the field being deleted.

See also

testBaseFieldDeleteWithExistingData()

File

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

Class

EntityDefinitionUpdateTest
Tests EntityDefinitionUpdateManager functionality.

Namespace

Drupal\system\Tests\Entity

Code

public function testBaseFieldCreateDeleteWithExistingEntities() {

  // Save an entity.
  $name = $this
    ->randomString();
  $storage = $this->entityManager
    ->getStorage('entity_test_update');
  $entity = $storage
    ->create(array(
    'name' => $name,
  ));
  $entity
    ->save();

  // Add a base field and run the update. Ensure the base field's column is
  // created and the prior saved entity data is still there.
  $this
    ->addBaseField();
  $this->entityDefinitionUpdateManager
    ->applyUpdates();
  $schema_handler = $this->database
    ->schema();
  $this
    ->assertTrue($schema_handler
    ->fieldExists('entity_test_update', 'new_base_field'), 'Column created in shared table for new_base_field.');
  $entity = $this->entityManager
    ->getStorage('entity_test_update')
    ->load($entity
    ->id());
  $this
    ->assertIdentical($entity->name->value, $name, 'Entity data preserved during field creation.');

  // Remove the base field and run the update. Ensure the base field's column
  // is deleted and the prior saved entity data is still there.
  $this
    ->removeBaseField();
  $this->entityDefinitionUpdateManager
    ->applyUpdates();
  $this
    ->assertFalse($schema_handler
    ->fieldExists('entity_test_update', 'new_base_field'), 'Column deleted from shared table for new_base_field.');
  $entity = $this->entityManager
    ->getStorage('entity_test_update')
    ->load($entity
    ->id());
  $this
    ->assertIdentical($entity->name->value, $name, 'Entity data preserved during field deletion.');

  // Add a base field with a required property and run the update. Ensure
  // 'not null' is not applied and thus no exception is thrown.
  $this
    ->addBaseField('shape_required');
  $this->entityDefinitionUpdateManager
    ->applyUpdates();
  $assert = $schema_handler
    ->fieldExists('entity_test_update', 'new_base_field__shape') && $schema_handler
    ->fieldExists('entity_test_update', 'new_base_field__color');
  $this
    ->assertTrue($assert, 'Columns created in shared table for new_base_field.');

  // Recreate the field after emptying the base table and check that its
  // columns are not 'not null'.
  // @todo Revisit this test when allowing for required storage field
  //   definitions. See https://www.drupal.org/node/2390495.
  $entity
    ->delete();
  $this
    ->removeBaseField();
  $this->entityDefinitionUpdateManager
    ->applyUpdates();
  $assert = !$schema_handler
    ->fieldExists('entity_test_update', 'new_base_field__shape') && !$schema_handler
    ->fieldExists('entity_test_update', 'new_base_field__color');
  $this
    ->assert($assert, 'Columns removed from the shared table for new_base_field.');
  $this
    ->addBaseField('shape_required');
  $this->entityDefinitionUpdateManager
    ->applyUpdates();
  $assert = $schema_handler
    ->fieldExists('entity_test_update', 'new_base_field__shape') && $schema_handler
    ->fieldExists('entity_test_update', 'new_base_field__color');
  $this
    ->assertTrue($assert, 'Columns created again in shared table for new_base_field.');
  $entity = $storage
    ->create(array(
    'name' => $name,
  ));
  $entity
    ->save();
  $this
    ->pass('The new_base_field columns are still nullable');
}