Commit 38139005 authored by webchick's avatar webchick

Issue #2992017 by tim.plunkett, neclimdul, tedbow, johndevman:...

Issue #2992017 by tim.plunkett, neclimdul, tedbow, johndevman: \Drupal\Core\Plugin\Context\EntityContextDefinition::getSampleValues() needlessly generates full sample entities

(cherry picked from commit 033f2a26)
parent fb03eec3
......@@ -57,13 +57,15 @@ protected function getSampleValues() {
$constraints = $this->getConstraintObjects();
$entity_type_manager = \Drupal::entityTypeManager();
$entity_type_id = $this->getEntityTypeId();
$entity_type = $entity_type_manager->getDefinition($entity_type_id);
$storage = $entity_type_manager->getStorage($entity_type_id);
// If the storage can generate a sample entity we might delegate to that.
if ($storage instanceof ContentEntityStorageInterface) {
if (!empty($constraints['Bundle']) && $constraints['Bundle'] instanceof BundleConstraint) {
foreach ($constraints['Bundle']->getBundleOption() as $bundle) {
// We have a bundle, we are bundleable and we can generate a sample.
yield EntityAdapter::createFromEntity($storage->createWithSampleValues($bundle));
$values = [$entity_type->getKey('bundle') => $bundle];
yield EntityAdapter::createFromEntity($storage->create($values));
}
return;
}
......
......@@ -112,10 +112,16 @@ public function testIsSatisfiedBy($expected, EntityContextDefinition $requiremen
$content_entity_storage = $this->prophesize(ContentEntityStorageInterface::class);
$this->entityTypeManager->getStorage('test_config')->willReturn($entity_storage->reveal());
$this->entityTypeManager->getStorage('test_content')->willReturn($content_entity_storage->reveal());
$config_entity_type = new EntityType(['id' => 'test_config']);
$content_entity_type = new EntityType(['id' => 'test_content']);
$this->entityTypeManager->getDefinition('test_config')->willReturn($config_entity_type);
$this->entityTypeManager->getDefinition('test_content')->willReturn($content_entity_type);
$this->entityManager->getDefinitions()->willReturn([
'test_config' => new EntityType(['id' => 'test_config']),
'test_content' => new EntityType(['id' => 'test_content']),
'test_config' => $config_entity_type,
'test_content' => $content_entity_type,
]);
$this->entityTypeBundleInfo->getBundleInfo('test_config')->willReturn([
'test_config' => ['label' => 'test_config'],
]);
......@@ -186,14 +192,25 @@ public function testIsSatisfiedByGenerateBundledEntity($expected, array $require
$entity->getEntityTypeId()->willReturn('test_content');
$entity->getIterator()->willReturn(new \ArrayIterator([]));
$entity->bundle()->willReturn($bundle);
$content_entity_storage->createWithSampleValues($bundle)
$content_entity_storage->create(['the_bundle_key' => $bundle])
->willReturn($entity->reveal())
->shouldBeCalled();
}
$entity_type = new EntityType(['id' => 'test_content']);
// Creating entities with sample values can lead to performance issues when
// called many times. Ensure that createWithSampleValues() is not called.
$content_entity_storage->createWithSampleValues(Argument::any())->shouldNotBeCalled();
$entity_type = new EntityType([
'id' => 'test_content',
'entity_keys' => [
'bundle' => 'the_bundle_key',
],
]);
$this->entityTypeManager->getStorage('test_content')->willReturn($content_entity_storage->reveal());
$this->entityTypeManager->getDefinition('test_content')->willReturn($entity_type);
$this->entityManager->getDefinitions()->willReturn([
'test_content' => $entity_type,
]);
......
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