View source
<?php
namespace Drupal\Tests\entity_update\Functional;
error_reporting(0);
use Drupal\entity_update\EntityUpdatePrint;
use Drupal\Tests\BrowserTestBase;
use Drupal\entity_update\EntityUpdate;
use Drupal\entity_update_tests\EntityUpdateTestHelper;
use Drupal\entity_update_tests\Entity\EntityUpdateTestsContentEntity;
class EntityUpdateFunctionsTest extends BrowserTestBase {
public static $modules = [
'entity_update',
'entity_update_tests',
];
protected $defaultTheme = 'stark';
protected function setUp() {
parent::setUp();
if ($entity = EntityUpdateTestsContentEntity::load(100)) {
$entity
->delete();
}
EntityUpdateTestHelper::fieldDisable('name');
EntityUpdateTestHelper::fieldDisable('description');
EntityUpdateTestHelper::fieldSetType('type', NULL);
EntityUpdatePrint::setEchoPrintEnable(FALSE);
}
public function testEntityUpdateBasic() {
$list = EntityUpdate::getEntityTypesToUpdate();
$this
->assertEquals(count($list), 0, 'Every entities are up to date.');
EntityUpdateTestHelper::fieldEnable('name');
$list = EntityUpdate::getEntityTypesToUpdate();
$this
->assertTrue(count($list) === 1, 'Has only one entity type 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.');
$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.');
$res = EntityUpdate::basicUpdate();
$this
->assertTrue($res, 'Entity schema has been updated (Field Add).');
$list = EntityUpdate::getEntityTypesToUpdate();
$this
->assertEquals(count($list), 0, 'Every entities are up to date.');
$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) . ']');
EntityUpdateTestHelper::fieldSetType('type', 'string');
$list = EntityUpdate::getEntityTypesToUpdate();
$this
->assertEquals(count($list), 1, 'Has one update.');
$res = EntityUpdate::basicUpdate();
$this
->assertTrue($res, 'Entity schema has been updated (Field Add).');
$list = EntityUpdate::getEntityTypesToUpdate();
$this
->assertEquals(count($list), 0, 'Every entities are up to date.');
$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) . ']');
EntityUpdateTestHelper::fieldDisable('name');
$list = EntityUpdate::getEntityTypesToUpdate();
$this
->assertEquals(count($list), 1, 'Has one entity type to update.');
$res = EntityUpdate::basicUpdate();
$this
->assertTrue($res, 'Entity schema has been updated (Field Remove).');
$list = EntityUpdate::getEntityTypesToUpdate();
$this
->assertEquals(count($list), 0, 'Every entities are up to date.');
EntityUpdateTestHelper::fieldSetType('type', 'integer');
$list = EntityUpdate::getEntityTypesToUpdate();
$this
->assertEquals(count($list), 1, 'Has one entity type to update.');
$res = EntityUpdate::basicUpdate();
$this
->assertTrue($res, 'Entity schema has been updated (Field Remove).');
$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) . ']');
}
public function testEntityUpdateBasicForce() {
$entity = EntityUpdateTestsContentEntity::create([
'id' => 1,
]);
$entity
->save();
$ids = \Drupal::entityQuery('entity_update_tests_cnt')
->execute();
$this
->assertEquals(count($ids), 1, 'Has one entity.');
EntityUpdateTestHelper::fieldEnable('name');
EntityUpdateTestHelper::fieldEnable('description');
$res = EntityUpdate::basicUpdate(TRUE);
$this
->assertTrue($res, 'Entity schema has been updated (Field Add).');
EntityUpdateTestHelper::fieldDisable('name');
$res = EntityUpdate::basicUpdate(TRUE);
$this
->assertTrue($res, 'Entity schema has been updated (Field Remove).');
$entity = EntityUpdateTestsContentEntity::load(1);
$entity
->set('description', 'a description');
$entity
->save();
EntityUpdateTestHelper::fieldDisable('description');
$res = EntityUpdate::basicUpdate(TRUE);
$this
->assertTrue($res, 'Entity schema has updated (Uninstall + data).');
}
public function testEntityUpdateAll() {
$entity = EntityUpdateTestsContentEntity::create([
'id' => 1,
]);
$entity
->save();
$ids = \Drupal::entityQuery('entity_update_tests_cnt')
->execute();
$this
->assertEquals(count($ids), 1, 'Has one entity.');
EntityUpdateTestHelper::fieldEnable('name');
EntityUpdateTestHelper::fieldSetType('type', 'string');
$this
->assertEquals(count(EntityUpdate::getEntityTypesToUpdate()), 1, 'Has one entity type to update.');
$res = EntityUpdate::safeUpdateMain();
$this
->assertTrue($res, 'Entity schema has been updated (Field Add).');
$this
->assertEquals(count(EntityUpdate::getEntityTypesToUpdate()), 0, 'Entity type updated.');
$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) . ']');
$entity = EntityUpdateTestsContentEntity::load(1);
$entity
->set('name', 'value');
$entity
->save();
EntityUpdateTestHelper::fieldDisable('name');
$this
->assertEquals(count(EntityUpdate::getEntityTypesToUpdate()), 1, 'Has one entity type to update.');
$res = EntityUpdate::safeUpdateMain();
$this
->assertTrue($res, 'Entity schema has been updated (Field Remove).');
$this
->assertEquals(count(EntityUpdate::getEntityTypesToUpdate()), 0, 'Entity type updated.');
$ids = \Drupal::entityQuery('entity_update_tests_cnt')
->execute();
$this
->assertEquals(count($ids), 1, 'Has one entity.');
$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) . ']');
}
public function testEntityUpdateSel() {
$entity = EntityUpdateTestsContentEntity::create([
'id' => 1,
]);
$entity
->save();
$ids = \Drupal::entityQuery('entity_update_tests_cnt')
->execute();
$this
->assertEquals(count($ids), 1, 'Has one entity.');
EntityUpdateTestHelper::fieldEnable('name');
EntityUpdateTestHelper::fieldEnable('city');
$this
->assertEquals(count(EntityUpdate::getEntityTypesToUpdate()), 2, 'Has one entity type to update.');
$entity_type = \Drupal::entityTypeManager()
->getStorage('entity_update_tests_cnt');
$res = EntityUpdate::safeUpdateMain($entity_type
->getEntityType());
$this
->assertTrue($res, 'Entity schema has been updated (Field Add & Remove).');
$this
->assertEquals(count(EntityUpdate::getEntityTypesToUpdate()), 1, 'Entity type updated.');
$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) . ']');
$entity = EntityUpdateTestsContentEntity::load(1);
$entity
->set('name', 'value');
$entity
->save();
EntityUpdateTestHelper::fieldDisable('name');
$this
->assertEquals(count(EntityUpdate::getEntityTypesToUpdate()), 2, 'Has one entity type to update.');
$res = EntityUpdate::safeUpdateMain($entity_type
->getEntityType());
$this
->assertTrue($res, 'Entity schema has been updated (Field Remove).');
$this
->assertEquals(count(EntityUpdate::getEntityTypesToUpdate()), 1, 'Entity type updated.');
$ids = \Drupal::entityQuery('entity_update_tests_cnt')
->execute();
$this
->assertEquals(count($ids), 1, 'After updates, Hase one entity.');
$ids = \Drupal::entityQuery('entity_update_tests_cnt')
->execute();
$this
->assertEquals(count($ids), 1, 'Has one entity.');
$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) . ']');
}
public function testEntityUpdateAdvanced() {
EntityUpdateTestHelper::fieldEnable('name');
EntityUpdateTestHelper::fieldEnable('description');
$res = EntityUpdate::basicUpdate();
$this
->assertTrue($res, 'Entity schema has been updated (Field Add).');
$data = [
'id' => 1,
'name' => 'name',
'description' => 'description',
];
$entity = EntityUpdateTestsContentEntity::create($data);
$entity
->save();
$ids = \Drupal::entityQuery('entity_update_tests_cnt')
->execute();
$this
->assertEquals(count($ids), 1, 'Has one entity.');
EntityUpdateTestHelper::fieldDisable('name');
$res = EntityUpdate::basicUpdate();
$this
->assertTrue(!$res, 'Entity schema is NOT updated (Uninstall + data).');
$res = EntityUpdate::basicUpdate(TRUE);
$this
->assertTrue($res, 'Entity schema is updated (Uninstall + data).');
EntityUpdateTestHelper::fieldEnable('name');
$res = EntityUpdate::basicUpdate(TRUE);
$this
->assertTrue($res, 'Entity schema is updated by advanced (Uninstall + data).');
$res = EntityUpdate::cleanupEntityBackup();
$type = 'entity_update_tests_cnt';
$res = EntityUpdate::entityUpdateDataBackupDel(EntityUpdate::getEntityTypesToUpdate($type), $type);
$this
->assertTrue($res, 'Entity data backup.');
EntityUpdateTestHelper::fieldDisable('name');
$res = EntityUpdate::basicUpdate();
$this
->assertTrue($res, 'Entity schema has updated (without data).');
$res = EntityUpdate::entityUpdateDataRestore();
$this
->assertTrue($res, 'Entity data Restore.');
$ids = \Drupal::entityQuery('entity_update_tests_cnt')
->execute();
$this
->assertEquals(count($ids), 1, 'Has one entity.');
$res = EntityUpdate::cleanupEntityBackup();
}
public function testEntityUpdateClean() {
$entity = EntityUpdateTestsContentEntity::create([
'id' => 1,
]);
$entity
->save();
$ids = \Drupal::entityQuery('entity_update_tests_cnt')
->execute();
$this
->assertEquals(count($ids), 1, 'Has one entity.');
EntityUpdateTestHelper::fieldEnable('name');
$res = EntityUpdate::safeUpdateMain();
$this
->assertTrue($res, 'Entity schema has been updated (Field Add).');
$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) . ']');
EntityUpdateTestHelper::fieldDisable('name');
$res = EntityUpdate::safeUpdateMain();
$this
->assertTrue($res, 'Entity schema has been updated (Field Remove).');
$res = EntityUpdate::cleanupEntityBackup();
$this
->assertTrue($res, 'Table cleanup END.');
$ids = \Drupal::entityQuery('entity_update_tests_cnt')
->execute();
$this
->assertEquals(count($ids), 1, 'Has one entity.');
$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) . ']');
}
}