You are here

public function EntityUpdateFunctionsTest::testEntityUpdateClean in Entity Update 2.0.x

Entity update function : clean.

File

tests/src/Functional/EntityUpdateFunctionsTest.php, line 336

Class

EntityUpdateFunctionsTest
Test Entity Update functions.

Namespace

Drupal\Tests\entity_update\Functional

Code

public function testEntityUpdateClean() {

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

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

  // Make Update.
  $res = EntityUpdate::safeUpdateMain();
  $this
    ->assertTrue($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
    ->assertTrue($res === TRUE, 'Entity schema database has correct fields [' . print_r($res, TRUE) . ']');

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

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

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

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

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