You are here

public function EntityUpdateFunctionsTest::testEntityUpdateSel in Entity Update 8

Update a selected entity type.

File

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

Class

EntityUpdateFunctionsTest
Test Entity Update functions.

Namespace

Drupal\entity_update_tests\Tests

Code

public function testEntityUpdateSel() {

  // 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');
  EntityUpdateTestHelper::fieldEnable('city');

  // Check update.
  $this
    ->assertEqual(count(EntityUpdate::getEntityTypesToUpdate()), 2, 'Has one entity type to update.');
  $entity_type = \Drupal::entityTypeManager()
    ->getStorage('entity_update_tests_cnt');

  // Make Update of the type.
  $res = EntityUpdate::safeUpdateMain($entity_type
    ->getEntityType());
  $this
    ->assert($res, 'Entity schema has been updated (Field Add & Remove).');

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

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

  // 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()), 2, 'Has one entity type to update.');

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

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

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

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