Unverified Commit 5470a059 authored by alexpott's avatar alexpott
Browse files

Issue #3019030 by Upchuk: Ensure loadMultipleRevisions() returns the revisions...

Issue #3019030 by Upchuk: Ensure loadMultipleRevisions() returns the revisions in the same order as the passed IDs
parent f9e7921b
......@@ -539,6 +539,13 @@ public function loadMultipleRevisions(array $revision_ids) {
$this->postLoad($entities);
}
// Ensure that the returned array is ordered the same as the original
// $ids array if this was passed in and remove any invalid IDs.
if ($revision_ids) {
$flipped_ids = array_intersect_key(array_flip($revision_ids), $revisions);
$revisions = array_replace($flipped_ids, $revisions);
}
return $revisions;
}
......
......@@ -134,6 +134,65 @@ protected function assertCRUD($entity_type, UserInterface $user1) {
}
}
/**
* Tests that the Entity storage loads the entities in the correct order.
*
* Entities should be returned in the same order as the passed IDs.
*/
public function testLoadMultiple() {
// Entity load.
$storage = $this->container->get('entity_type.manager')->getStorage('entity_test');
$ids = [];
$entity = $storage->create(['name' => 'test']);
$entity->save();
$ids[] = $entity->id();
$entity = $storage->create(['name' => 'test2']);
$entity->save();
$ids[] = $entity->id();
// We load the entities in an initial and reverse order, with both static
// cache in place and reset, to ensure we always get the same result.
$entities = $storage->loadMultiple($ids);
$this->assertEquals($ids, array_keys($entities));
// Reverse the order and load again.
$ids = array_reverse($ids);
$entities = $storage->loadMultiple($ids);
$this->assertEquals($ids, array_keys($entities));
// Reverse the order again, reset the cache and load again.
$storage->resetCache();
$ids = array_reverse($ids);
$entities = $storage->loadMultiple($ids);
$this->assertEquals($ids, array_keys($entities));
// Entity revision load.
$storage = $this->container->get('entity_type.manager')->getStorage('entity_test_rev');
$ids = [];
$entity = $storage->create(['name' => 'test_rev']);
$entity->save();
$ids[] = $entity->getRevisionId();
$revision = $storage->createRevision($entity, TRUE);
$revision->save();
$ids[] = $revision->getRevisionId();
$entities = $storage->loadMultipleRevisions($ids);
$this->assertEquals($ids, array_keys($entities));
// Reverse the order and load again.
$ids = array_reverse($ids);
$entities = $storage->loadMultipleRevisions($ids);
$this->assertEquals($ids, array_keys($entities));
// Reverse the order again, reset the cache and load again.
$ids = array_reverse($ids);
$storage->resetCache();
$entities = $storage->loadMultipleRevisions($ids);
$this->assertEquals($ids, array_keys($entities));
}
/**
* Tests that exceptions are thrown when saving or deleting an entity.
*/
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment