You are here

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

Entity update function : all.

File

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

Class

EntityUpdateFunctionsTest
Test Entity Update functions.

Namespace

Drupal\Tests\entity_update\Functional

Code

public function testEntityUpdateAll() {

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

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

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

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

  // Check update.
  $this
    ->assertEquals(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
    ->assertTrue($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
    ->assertEquals(count(EntityUpdate::getEntityTypesToUpdate()), 1, 'Has one entity type to update.');

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

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

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