public function UpdateApiEntityDefinitionUpdateTest::testSingleUpdates in Zircon Profile 8
Same name and namespace in other branches
- 8.0 core/modules/system/src/Tests/Entity/Update/UpdateApiEntityDefinitionUpdateTest.php \Drupal\system\Tests\Entity\Update\UpdateApiEntityDefinitionUpdateTest::testSingleUpdates()
Tests that individual updates applied sequentially work as expected.
File
- core/
modules/ system/ src/ Tests/ Entity/ Update/ UpdateApiEntityDefinitionUpdateTest.php, line 60 - Contains \Drupal\system\Tests\Entity\Update\UpdateApiEntityDefinitionUpdateTest.
Class
- UpdateApiEntityDefinitionUpdateTest
- Tests performing entity updates through the Update API.
Namespace
Drupal\system\Tests\Entity\UpdateCode
public function testSingleUpdates() {
// Create a test entity.
$user_ids = [
mt_rand(),
mt_rand(),
];
$entity = EntityTest::create([
'name' => $this
->randomString(),
'user_id' => $user_ids,
]);
$entity
->save();
// Check that only a single value is stored for 'user_id'.
$entity = $this
->reloadEntity($entity);
$this
->assertEqual(count($entity->user_id), 1);
$this
->assertEqual($entity->user_id->target_id, $user_ids[0]);
// Make 'user_id' multiple by applying updates.
$this
->enableUpdates('entity_test', 'entity_definition_updates', 8001);
$this
->applyUpdates();
// Check that data was correctly migrated.
$entity = $this
->reloadEntity($entity);
$this
->assertEqual(count($entity->user_id), 1);
$this
->assertEqual($entity->user_id->target_id, $user_ids[0]);
// Store multiple data and check it is correctly stored.
$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]);
// Make 'user_id' single again by applying updates.
$this
->enableUpdates('entity_test', 'entity_definition_updates', 8002);
$this
->applyUpdates();
// Check that data was correctly migrated/dropped.
$entity = $this
->reloadEntity($entity);
$this
->assertEqual(count($entity->user_id), 1);
$this
->assertEqual($entity->user_id->target_id, $user_ids[0]);
// Check that only a single value is stored for 'user_id' again.
$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]);
}