You are here

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

Update a selected entity type.

File

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

Class

EntityUpdateFunctionsTest
Test Entity Update functions.

Namespace

Drupal\Tests\entity_update\Functional

Code

public function testEntityUpdateSel() {

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

  // Check update.
  $this
    ->assertEquals(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
    ->assertTrue($res, 'Entity schema has been updated (Field Add & Remove).');

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

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

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

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

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