Skip to content
Snippets Groups Projects
Verified Commit 9909596e authored by Alex Pott's avatar Alex Pott
Browse files

Issue #2464041 by Mile23, quietone, andrewsuth: Test unit behavior of...

Issue #2464041 by Mile23, quietone, andrewsuth: Test unit behavior of EntityStorageBase::load(), loadMultiple() with invalid ID, UUID

(cherry picked from commit 50ddf37f)
parent ff2599d2
24 merge requests!8506Draft: Issue #3456536 by ibrahim tameme,!5646Issue #3350972 by nod_: [random test failure]...,!5600Issue #3350972 by nod_: [random test failure]...,!5343Issue #3305066 by quietone, Rename RedirectLeadingSlashesSubscriber,!4350Issue #3307718: Implement xxHash for non-cryptographic use-cases,!3603#ISSUE 3346218 Add a different message on edit comment,!3555Issue #2473873: Views entity operations lack cacheability support, resulting in incorrect dropbuttons,!3494Issue #3327018 by Spokje, longwave, xjm, mondrake: Update PHPStan to 1.9.3 and...,!3410Issue #3340128: UserLoginForm::submitForm has some dead code,!3389Issue #3325184 by Spokje, andypost, xjm, smustgrave: $this->configFactory is...,!3381Issue #3332363: Refactor Claro's menus-and-lists stylesheet,!3307Issue #3326193: CKEditor 5 can grow past the viewport when there is a lot of content,!3236Issue #3332419: Refactor Claro's messages stylesheet,!3231Draft: Issue #3049525 by longwave, fougere, larowlan, kim.pepper, AaronBauman, Wim...,!3212Issue #3294003: Refactor Claro's entity-meta stylesheet,!3194Issue #3330981: Fix PHPStan L1 error "Relying on entity queries to check access by default is deprecated...",!3143Issue #3313342: [PHP 8.1] Deprecated function: strpos(): Passing null to parameter #1 LayoutBuilderUiCacheContext.php on line 28,!3024Issue #3307509: Empty option for views bulk form,!2972Issue #1845004: Replace custom password hashing library with PHP 5.5 password_hash(),!2719Issue #3110137: Remove Classy from core.,!2688Issue #3261452: [PP-1] Remove tracker module from core,!2437Issue #3238257 by hooroomoo, Wim Leers: Fragment link pointing to <textarea>...,!2296Issue #3100732: Allow specifying `meta` data on JSON:API objects,!1626Issue #3256642: Make life better for database drivers that extend another database driver
<?php
namespace Drupal\Tests\Core\Entity;
use Drupal\Tests\UnitTestCase;
/**
* @coversDefaultClass \Drupal\Core\Entity\EntityStorageBase
* @group Entity
*/
class EntityStorageBaseTest extends UnitTestCase {
/**
* Generate a mocked entity object.
*
* @param string $id
* ID value for this entity.
*
* @return \Drupal\Core\Entity\EntityInterface|\PHPUnit\Framework\MockObject\MockObject
* The mocked entity.
*/
public function generateEntityInterface($id) {
$mock_entity = $this->getMockBuilder('\Drupal\Core\Entity\EntityInterface')
->onlyMethods(['id'])
->getMockForAbstractClass();
$mock_entity->expects($this->any())
->method('id')
->willReturn((string) $id);
return $mock_entity;
}
/**
* Data provider for testLoad().
*
* @return array
* - Expected output of load().
* - A fixture of entities to query against. Suitable return value for
* loadMultiple().
* - The ID we'll query.
*/
public function providerLoad() {
$data = [];
// Data set for a matching value.
$entity = $this->generateEntityInterface('1');
$data['matching-value'] = [$entity, ['1' => $entity], '1'];
// Data set for no matching value.
$data['no-matching-value'] = [NULL, [], '0'];
return $data;
}
/**
* @covers ::load
*
* @dataProvider providerLoad
*/
public function testLoad($expected, $entity_fixture, $query) {
$mock_base = $this->getMockBuilder('\Drupal\Core\Entity\EntityStorageBase')
->disableOriginalConstructor()
->onlyMethods(['loadMultiple'])
->getMockForAbstractClass();
// load() always calls loadMultiple().
$mock_base->expects($this->once())
->method('loadMultiple')
->with([$query])
->willReturn($entity_fixture);
$this->assertEquals($expected, $mock_base->load($query));
}
/**
* Data provider for testLoadMultiple.
*
* @return array
* - The expected result.
* - Results for doLoadMultiple(), called internally by loadMultiple().
* - The query, an array of IDs.
*/
public function providerLoadMultiple() {
// Create a fixture of entity objects.
$fixture = [];
foreach (range(1, 10) as $index) {
$fixture[(string) $index] = $this->generateEntityInterface($index);
}
$data = [];
// Data set for NULL ID parameter.
$data['null-id-parameter'] = [$fixture, $fixture, NULL];
// Data set for no results.
$data['no-results'] = [[], [], ['11']];
// Data set for 0 results for multiple IDs.
$data['no-results-multiple-ids'] = [[], [], ['11', '12', '13']];
// Data set for 1 result for 1 ID.
$data['1-result-for-1-id'] = [
['1' => $fixture['1']],
['1' => $fixture['1']],
['1'],
];
// Data set for results for all IDs.
$ids = ['1', '2', '3'];
foreach ($ids as $id) {
$expectation[$id] = $fixture[$id];
$load_multiple[$id] = $fixture[$id];
}
$data['results-for-all-ids'] = [$expectation, $load_multiple, $ids];
// Data set for partial results for multiple IDs.
$ids = ['1', '2', '3'];
foreach ($ids as $id) {
$expectation[$id] = $fixture[$id];
$load_multiple[$id] = $fixture[$id];
}
$ids = array_merge($ids, ['11', '12']);
$data['partial-results-for-multiple-ids'] = [
$expectation,
$load_multiple,
$ids,
];
return $data;
}
/**
* Test loadMultiple().
*
* Does not cover statically-cached results.
*
* @covers ::loadMultiple
*
* @dataProvider providerLoadMultiple
*/
public function testLoadMultiple($expected, $load_multiple, $query) {
// Make our EntityStorageBase mock.
$mock_base = $this->getMockBuilder('\Drupal\Core\Entity\EntityStorageBase')
->disableOriginalConstructor()
->onlyMethods(['doLoadMultiple', 'postLoad'])
->getMockForAbstractClass();
// For all non-cached queries, we call doLoadMultiple().
$mock_base->expects($this->once())
->method('doLoadMultiple')
->with($query)
->willReturn($load_multiple);
// Make our EntityTypeInterface mock so that we can turn off static caching.
$mock_entity_type = $this->getMockBuilder('\Drupal\Core\Entity\EntityTypeInterface')
->onlyMethods(['isStaticallyCacheable'])
->getMockForAbstractClass();
// Disallow caching.
$mock_entity_type->expects($this->any())
->method('isStaticallyCacheable')
->willReturn(FALSE);
// Add the EntityTypeInterface to the storage object.
$ref_entity_type = new \ReflectionProperty($mock_base, 'entityType');
$ref_entity_type->setAccessible(TRUE);
$ref_entity_type->setValue($mock_base, $mock_entity_type);
// Set up expectations for postLoad(), which we only call if there are
// results from loadMultiple().
$mock_base->expects($this->exactly(empty($load_multiple) ? 0 : 1))
->method('postLoad');
$this->assertEquals($expected, $mock_base->loadMultiple($query));
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment