You are here

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

Entity advanced update simulation.

File

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

Class

EntityUpdateFunctionsTest
Test Entity Update functions.

Namespace

Drupal\Tests\entity_update\Functional

Code

public function testEntityUpdateAdvanced() {

  /* TODO : Review this test */

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

  // Update entity type.
  $res = EntityUpdate::basicUpdate();
  $this
    ->assertTrue($res, 'Entity schema has been updated (Field Add).');

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

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

  // Try update without --force.
  $res = EntityUpdate::basicUpdate();
  $this
    ->assertTrue(!$res, 'Entity schema is NOT updated (Uninstall + data).');

  // Try update using --force (After D8.5 this will update).
  $res = EntityUpdate::basicUpdate(TRUE);
  $this
    ->assertTrue($res, 'Entity schema is updated (Uninstall + data).');

  // Simulate advanced update. Re enable field.
  EntityUpdateTestHelper::fieldEnable('name');
  $res = EntityUpdate::basicUpdate(TRUE);
  $this
    ->assertTrue($res, 'Entity schema is updated by advanced (Uninstall + data).');

  // Cleanup backup DB.
  $res = EntityUpdate::cleanupEntityBackup();

  // Copy to backup DB.
  $type = 'entity_update_tests_cnt';
  $res = EntityUpdate::entityUpdateDataBackupDel(EntityUpdate::getEntityTypesToUpdate($type), $type);
  $this
    ->assertTrue($res, 'Entity data backup.');

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

  // Update entity types.
  $res = EntityUpdate::basicUpdate();
  $this
    ->assertTrue($res, 'Entity schema has updated (without data).');

  // Restore from backup DB.
  $res = EntityUpdate::entityUpdateDataRestore();
  $this
    ->assertTrue($res, 'Entity data Restore.');

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

  // Cleanup backup DB.
  $res = EntityUpdate::cleanupEntityBackup();
}