You are here

public function EntityDefinitionUpdateTest::testBundleFieldCreateDeleteWithExistingEntities 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::testBundleFieldCreateDeleteWithExistingEntities()

Tests creating and deleting a bundle field if entities exist.

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

See also

testBundleFieldDeleteWithExistingData()

File

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

Class

EntityDefinitionUpdateTest
Tests EntityDefinitionUpdateManager functionality.

Namespace

Drupal\system\Tests\Entity

Code

public function testBundleFieldCreateDeleteWithExistingEntities() {

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

  // Add a bundle field and run the update. Ensure the bundle field's table
  // is created and the prior saved entity data is still there.
  $this
    ->addBundleField();
  $this->entityDefinitionUpdateManager
    ->applyUpdates();
  $schema_handler = $this->database
    ->schema();
  $this
    ->assertTrue($schema_handler
    ->tableExists('entity_test_update__new_bundle_field'), 'Dedicated table created for new_bundle_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 bundle field's
  // table is deleted and the prior saved entity data is still there.
  $this
    ->removeBundleField();
  $this->entityDefinitionUpdateManager
    ->applyUpdates();
  $this
    ->assertFalse($schema_handler
    ->tableExists('entity_test_update__new_bundle_field'), 'Dedicated table deleted for new_bundle_field.');
  $entity = $this->entityManager
    ->getStorage('entity_test_update')
    ->load($entity
    ->id());
  $this
    ->assertIdentical($entity->name->value, $name, 'Entity data preserved during field deletion.');

  // Test that required columns are created as 'not null'.
  $this
    ->addBundleField('shape_required');
  $this->entityDefinitionUpdateManager
    ->applyUpdates();
  $message = 'The new_bundle_field_shape column is not nullable.';
  $values = array(
    'bundle' => $entity
      ->bundle(),
    'deleted' => 0,
    'entity_id' => $entity
      ->id(),
    'revision_id' => $entity
      ->id(),
    'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
    'delta' => 0,
    'new_bundle_field_color' => $this
      ->randomString(),
  );
  try {

    // Try to insert a record without providing a value for the 'not null'
    // column. This should fail.
    $this->database
      ->insert('entity_test_update__new_bundle_field')
      ->fields($values)
      ->execute();
    $this
      ->fail($message);
  } catch (\RuntimeException $e) {
    if ($e instanceof DatabaseExceptionWrapper || $e instanceof IntegrityConstraintViolationException) {

      // Now provide a value for the 'not null' column. This is expected to
      // succeed.
      $values['new_bundle_field_shape'] = $this
        ->randomString();
      $this->database
        ->insert('entity_test_update__new_bundle_field')
        ->fields($values)
        ->execute();
      $this
        ->pass($message);
    }
    else {

      // Keep throwing it.
      throw $e;
    }
  }
}