View source
<?php
namespace Drupal\system\Tests\Entity\Update;
use Drupal\Core\Entity\Exception\FieldStorageDefinitionUpdateForbiddenException;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\simpletest\WebTestBase;
use Drupal\system\Tests\Update\DbUpdatesTrait;
class UpdateApiEntityDefinitionUpdateTest extends WebTestBase {
use DbUpdatesTrait;
protected static $modules = [
'entity_test',
];
protected $entityManager;
protected $updatesManager;
public function setUp() {
parent::setUp();
$this->entityManager = $this->container
->get('entity.manager');
$this->updatesManager = $this->container
->get('entity.definition_update_manager');
$admin = $this
->drupalCreateUser([], FALSE, TRUE);
$this
->drupalLogin($admin);
}
public function testSingleUpdates() {
$user_ids = [
mt_rand(),
mt_rand(),
];
$entity = EntityTest::create([
'name' => $this
->randomString(),
'user_id' => $user_ids,
]);
$entity
->save();
$entity = $this
->reloadEntity($entity);
$this
->assertEqual(count($entity->user_id), 1);
$this
->assertEqual($entity->user_id->target_id, $user_ids[0]);
$this
->enableUpdates('entity_test', 'entity_definition_updates', 8001);
$this
->applyUpdates();
$entity = $this
->reloadEntity($entity);
$this
->assertEqual(count($entity->user_id), 1);
$this
->assertEqual($entity->user_id->target_id, $user_ids[0]);
$entity->user_id = $user_ids;
$entity
->save();
$entity = $this
->reloadEntity($entity);
$this
->assertEqual(count($entity->user_id), 2);
$this
->assertEqual($entity->user_id[0]->target_id, $user_ids[0]);
$this
->assertEqual($entity->user_id[1]->target_id, $user_ids[1]);
$this
->enableUpdates('entity_test', 'entity_definition_updates', 8002);
$this
->applyUpdates();
$entity = $this
->reloadEntity($entity);
$this
->assertEqual(count($entity->user_id), 1);
$this
->assertEqual($entity->user_id->target_id, $user_ids[0]);
$entity->user_id = $user_ids;
$entity
->save();
$entity = $this
->reloadEntity($entity);
$this
->assertEqual(count($entity->user_id), 1);
$this
->assertEqual($entity->user_id[0]->target_id, $user_ids[0]);
}
public function testMultipleUpdates() {
$user_ids = [
mt_rand(),
mt_rand(),
];
$entity = EntityTest::create([
'name' => $this
->randomString(),
'user_id' => $user_ids,
]);
$entity
->save();
$entity = $this
->reloadEntity($entity);
$this
->assertEqual(count($entity->user_id), 1);
$this
->assertEqual($entity->user_id->target_id, $user_ids[0]);
$this
->enableUpdates('entity_test', 'entity_definition_updates', 8002);
$this
->applyUpdates();
$entity = $this
->reloadEntity($entity);
$this
->assertEqual(count($entity->user_id), 1);
$this
->assertEqual($entity->user_id->target_id, $user_ids[0]);
$entity->user_id = $user_ids;
$entity
->save();
$entity = $this
->reloadEntity($entity);
$this
->assertEqual(count($entity->user_id), 1);
$this
->assertEqual($entity->user_id[0]->target_id, $user_ids[0]);
}
function testStatusReport() {
$entity = EntityTest::create([
'name' => $this
->randomString(),
'user_id' => mt_rand(),
]);
$entity
->save();
$this
->drupalGet('admin/reports/status');
$this
->assertNoRaw('Out of date');
$this
->assertNoRaw('Mismatched entity and/or field definitions');
$this->container
->get('state')
->set('entity_test.remove_name_field', TRUE);
$this
->drupalGet('admin/reports/status');
$this
->assertNoRaw('Out of date');
$this
->assertRaw('Mismatched entity and/or field definitions');
$this
->enableUpdates('entity_test', 'status_report', 8001);
$this
->drupalGet('admin/reports/status');
$this
->assertRaw('Out of date');
$this
->assertRaw('Mismatched entity and/or field definitions');
$this
->applyUpdates();
$this
->drupalGet('admin/reports/status');
$this
->assertNoRaw('Out of date');
$this
->assertRaw('Mismatched entity and/or field definitions');
$message = 'Entity updates cannot run if entity data exists.';
try {
$this->updatesManager
->applyUpdates();
$this
->fail($message);
} catch (FieldStorageDefinitionUpdateForbiddenException $e) {
$this
->pass($message);
}
$this
->drupalGet('admin/reports/status');
$this
->assertNoRaw('Out of date');
$this
->assertRaw('Mismatched entity and/or field definitions');
$entity
->delete();
$this
->enableUpdates('entity_test', 'status_report', 8002);
$this
->applyUpdates();
$this
->drupalGet('admin/reports/status');
$this
->assertNoRaw('Out of date');
$this
->assertRaw('Mismatched entity and/or field definitions');
}
protected function reloadEntity(EntityTest $entity) {
$this->entityManager
->useCaches(FALSE);
$this->entityManager
->getStorage('entity_test')
->resetCache([
$entity
->id(),
]);
return EntityTest::load($entity
->id());
}
}