You are here

public function EntityUpdateFunctionsTest::testEntityUpdateAll in Entity Update 8

Entity update function : all.

File

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

Class

EntityUpdateFunctionsTest
Test Entity Update functions.

Namespace

Drupal\entity_update_tests\Tests

Code

public function testEntityUpdateAll() {

  // 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');

  // Enable Type Field and set to 'string'.
  EntityUpdateTestHelper::fieldSetType('type', 'string');

  // Check update.
  $this
    ->assertEqual(count(EntityUpdate::getEntityTypesToUpdate()), 1, 'Has one entity type to update.');

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

  // Check update.
  $this
    ->assertEqual(count(EntityUpdate::getEntityTypesToUpdate()), 0, 'Entity type updated.');

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

  // Load and update entity (after entity type update).
  $entity = EntityUpdateTestsContentEntity::load(1);
  $entity
    ->set('name', 'value');
  $entity
    ->save();

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

  // Check update.
  $this
    ->assertEqual(count(EntityUpdate::getEntityTypesToUpdate()), 1, 'Has one entity type to update.');

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

  // Check update.
  $this
    ->assertEqual(count(EntityUpdate::getEntityTypesToUpdate()), 0, 'Entity type updated.');

  // 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',
    'type',
  ];
  $res = EntityUpdateTestHelper::checkFieldList('entity_update_tests_cnt', $fields);
  $this
    ->assert($res === TRUE, 'Entity schema database has correct fields [' . print_r($res, TRUE) . ']');
}