public function EntityUpdateFunctionsTest::testEntityUpdateBasic in Entity Update 8
Entity update function : basic.
File
- modules/
entity_update_tests/ src/ Tests/ EntityUpdateFunctionsTest.php, line 44
Class
- EntityUpdateFunctionsTest
- Test Entity Update functions.
Namespace
Drupal\entity_update_tests\TestsCode
public function testEntityUpdateBasic() {
// Disable the field by default => No need to update.
$list = EntityUpdate::getEntityTypesToUpdate();
$this
->assertEqual(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
->assert(count($list) === 1, 'Has only one entity type to update.');
// Analyse Entity to update.
$first_item = reset($list);
$first_key = key($list);
$this
->assertEqual($first_key, 'entity_update_tests_cnt', 'The first key is "entity_update_tests_cnt".');
$this
->assertEqual(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
->assertEqual($temp, 'The Name field needs to be installed.', 'Summary text is correct.');
// Make Update.
$res = EntityUpdate::basicUpdate();
$this
->assert($res, 'Entity schema has been updated (Field Add).');
// Get updates list and check.
$list = EntityUpdate::getEntityTypesToUpdate();
$this
->assertEqual(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
->assert($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
->assertEqual(count($list), 1, 'Has one update.');
// Make Update.
$res = EntityUpdate::basicUpdate();
$this
->assert($res, 'Entity schema has been updated (Field Add).');
// Get updates list and check.
$list = EntityUpdate::getEntityTypesToUpdate();
$this
->assertEqual(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
->assert($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
->assertEqual(count($list), 1, 'Has one entity type to update.');
// Make Update.
$res = EntityUpdate::basicUpdate();
$this
->assert($res, 'Entity schema has been updated (Field Remove).');
// Has one field to update.
$list = EntityUpdate::getEntityTypesToUpdate();
$this
->assertEqual(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
->assertEqual(count($list), 1, 'Has one entity type to update.');
// Make Update.
$res = EntityUpdate::basicUpdate();
$this
->assert($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
->assert($res === TRUE, 'Entity schema database has correct fields [' . print_r($res, TRUE) . ']');
}