You are here

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

Entity update function : basic.

File

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

Class

EntityUpdateFunctionsTest
Test Entity Update functions.

Namespace

Drupal\Tests\entity_update\Functional

Code

public function testEntityUpdateBasic() {

  // Disable the field by default => No need to update.
  $list = EntityUpdate::getEntityTypesToUpdate();
  $this
    ->assertEquals(count($list), 0, 'Every entities are up to date.');

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

  // Get updates list.
  $list = EntityUpdate::getEntityTypesToUpdate();

  // Has one field to update.
  $this
    ->assertTrue(count($list) === 1, 'Has only one entity type to update.');

  // Analyse Entity to update.
  $first_item = reset($list);
  $first_key = key($list);
  $this
    ->assertEquals($first_key, 'entity_update_tests_cnt', 'The first key is "entity_update_tests_cnt".');
  $this
    ->assertEquals(count($first_item), 1, 'The "entity_update_tests_cnt" has one change.');

  // Get first change.
  $entity_change_summ = reset($first_item);
  $temp = strip_tags($entity_change_summ);
  $this
    ->assertEquals($temp, 'The Name field needs to be installed.', 'Summary text is correct.');

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

  // Get updates list and check.
  $list = EntityUpdate::getEntityTypesToUpdate();
  $this
    ->assertEquals(count($list), 0, 'Every entities are up to date.');

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

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

  // Get updates list and check.
  $list = EntityUpdate::getEntityTypesToUpdate();
  $this
    ->assertEquals(count($list), 1, 'Has one update.');

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

  // Get updates list and check.
  $list = EntityUpdate::getEntityTypesToUpdate();
  $this
    ->assertEquals(count($list), 0, 'Every entities are up to date.');

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

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

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

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

  // Has one field to update.
  $list = EntityUpdate::getEntityTypesToUpdate();
  $this
    ->assertEquals(count($list), 0, 'Every entities are up to date.');

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

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

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

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