View source
<?php
namespace Drupal\Tests\system\Functional\Entity\Update;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\system\Functional\Entity\Traits\EntityDefinitionTestTrait;
use Drupal\Tests\system\Functional\Update\DbUpdatesTrait;
class UpdateApiEntityDefinitionUpdateTest extends BrowserTestBase {
use DbUpdatesTrait;
use EntityDefinitionTestTrait;
protected static $modules = [
'entity_test',
];
protected $defaultTheme = 'stark';
protected function setUp() {
parent::setUp();
$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
->assertCount(1, $entity->user_id);
$this
->assertEqual($entity->user_id->target_id, $user_ids[0]);
$this
->enableUpdates('entity_test', 'entity_definition_updates', 8001);
$this
->applyUpdates();
$this
->assertTrue(\Drupal::database()
->schema()
->tableExists('entity_test__user_id'));
$entity = $this
->reloadEntity($entity);
$this
->assertCount(1, $entity->user_id);
$this
->assertEqual($entity->user_id->target_id, $user_ids[0]);
$entity->user_id = $user_ids;
$entity
->save();
$entity = $this
->reloadEntity($entity);
$this
->assertCount(2, $entity->user_id);
$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
->assertCount(1, $entity->user_id);
$this
->assertEqual($entity->user_id->target_id, $user_ids[0]);
$entity->user_id = $user_ids;
$entity
->save();
$entity = $this
->reloadEntity($entity);
$this
->assertCount(1, $entity->user_id);
$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
->assertCount(1, $entity->user_id);
$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
->assertCount(1, $entity->user_id);
$this
->assertEqual($entity->user_id->target_id, $user_ids[0]);
$entity->user_id = $user_ids;
$entity
->save();
$entity = $this
->reloadEntity($entity);
$this
->assertCount(1, $entity->user_id);
$this
->assertEqual($entity->user_id[0]->target_id, $user_ids[0]);
}
public 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');
$this
->applyEntityUpdates();
$this
->drupalGet('admin/reports/status');
$this
->assertNoRaw('Out of date');
$this
->assertNoRaw('Mismatched entity and/or field definitions');
}
protected function reloadEntity(EntityTest $entity) {
\Drupal::entityTypeManager()
->useCaches(FALSE);
\Drupal::service('entity_field.manager')
->useCaches(FALSE);
return EntityTest::load($entity
->id());
}
}