You are here

public function EntityUpdateFunctionsTest::testEntityUpdateClean in Entity Update 8

Entity update function : clean.

File

modules/entity_update_tests/src/Tests/EntityUpdateFunctionsTest.php, line 324

Class

EntityUpdateFunctionsTest
Test Entity Update functions.

Namespace

Drupal\entity_update_tests\Tests

Code

public function testEntityUpdateClean() {

  // Create an entity.
  $entity = EntityUpdateTestsContentEntity::create([
    'id' => 1,
  ]);
  $entity
    ->save();
  $ids = \Drupal::entityQuery('entity_update_tests_cnt')
    ->execute();
  $this
    ->assertEqual(count($ids), 1, 'Has one entity.');

  // Enable the field.
  EntityUpdateTestHelper::fieldEnable('name');

  // Make Update.
  $res = EntityUpdate::safeUpdateMain();
  $this
    ->assert($res, 'Entity schema has been updated (Field Add).');

  // Check fields list on database.
  $fields = [
    'fix',
    'id',
    'name',
  ];
  $res = EntityUpdateTestHelper::checkFieldList('entity_update_tests_cnt', $fields);
  $this
    ->assert($res === TRUE, 'Entity schema database has correct fields [' . print_r($res, TRUE) . ']');

  // Disable the field.
  EntityUpdateTestHelper::fieldDisable('name');

  // Make Update.
  $res = EntityUpdate::safeUpdateMain();
  $this
    ->assert($res, 'Entity schema has been updated (Field Remove).');

  // Cleanup function.
  $res = EntityUpdate::cleanupEntityBackup();
  $this
    ->assert($res, 'Table cleanup END.');

  // Check entity count.
  $ids = \Drupal::entityQuery('entity_update_tests_cnt')
    ->execute();
  $this
    ->assertEqual(count($ids), 1, 'Has one entity.');

  // Check fields list on database.
  $fields = [
    'fix',
    'id',
  ];
  $res = EntityUpdateTestHelper::checkFieldList('entity_update_tests_cnt', $fields);
  $this
    ->assert($res === TRUE, 'Entity schema database has correct fields [' . print_r($res, TRUE) . ']');
}