You are here

public function UpdateApiEntityDefinitionUpdateTest::testMultipleUpdates in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/system/src/Tests/Entity/Update/UpdateApiEntityDefinitionUpdateTest.php \Drupal\system\Tests\Entity\Update\UpdateApiEntityDefinitionUpdateTest::testMultipleUpdates()

Tests that multiple updates applied in bulk work as expected.

File

core/modules/system/src/Tests/Entity/Update/UpdateApiEntityDefinitionUpdateTest.php, line 108
Contains \Drupal\system\Tests\Entity\Update\UpdateApiEntityDefinitionUpdateTest.

Class

UpdateApiEntityDefinitionUpdateTest
Tests performing entity updates through the Update API.

Namespace

Drupal\system\Tests\Entity\Update

Code

public function testMultipleUpdates() {

  // 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 and then single again by applying updates.
  $this
    ->enableUpdates('entity_test', 'entity_definition_updates', 8002);
  $this
    ->applyUpdates();

  // Check that data was correctly migrated back and forth.
  $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]);
}