Issue #2992017 by tim.plunkett, neclimdul, tedbow, johndevman: \Drupal\Core\Plugin\Context\EntityContextDefinition::getSampleValues() needlessly generates full sample entities
parent
4ed41f4e8e
commit
033f2a2649
|
@ -57,13 +57,15 @@ class EntityContextDefinition extends ContextDefinition {
|
||||||
$constraints = $this->getConstraintObjects();
|
$constraints = $this->getConstraintObjects();
|
||||||
$entity_type_manager = \Drupal::entityTypeManager();
|
$entity_type_manager = \Drupal::entityTypeManager();
|
||||||
$entity_type_id = $this->getEntityTypeId();
|
$entity_type_id = $this->getEntityTypeId();
|
||||||
|
$entity_type = $entity_type_manager->getDefinition($entity_type_id);
|
||||||
$storage = $entity_type_manager->getStorage($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 the storage can generate a sample entity we might delegate to that.
|
||||||
if ($storage instanceof ContentEntityStorageInterface) {
|
if ($storage instanceof ContentEntityStorageInterface) {
|
||||||
if (!empty($constraints['Bundle']) && $constraints['Bundle'] instanceof BundleConstraint) {
|
if (!empty($constraints['Bundle']) && $constraints['Bundle'] instanceof BundleConstraint) {
|
||||||
foreach ($constraints['Bundle']->getBundleOption() as $bundle) {
|
foreach ($constraints['Bundle']->getBundleOption() as $bundle) {
|
||||||
// We have a bundle, we are bundleable and we can generate a sample.
|
// 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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
|
@ -112,10 +112,16 @@ class EntityContextDefinitionIsSatisfiedTest extends UnitTestCase {
|
||||||
$content_entity_storage = $this->prophesize(ContentEntityStorageInterface::class);
|
$content_entity_storage = $this->prophesize(ContentEntityStorageInterface::class);
|
||||||
$this->entityTypeManager->getStorage('test_config')->willReturn($entity_storage->reveal());
|
$this->entityTypeManager->getStorage('test_config')->willReturn($entity_storage->reveal());
|
||||||
$this->entityTypeManager->getStorage('test_content')->willReturn($content_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([
|
$this->entityManager->getDefinitions()->willReturn([
|
||||||
'test_config' => new EntityType(['id' => 'test_config']),
|
'test_config' => $config_entity_type,
|
||||||
'test_content' => new EntityType(['id' => 'test_content']),
|
'test_content' => $content_entity_type,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$this->entityTypeBundleInfo->getBundleInfo('test_config')->willReturn([
|
$this->entityTypeBundleInfo->getBundleInfo('test_config')->willReturn([
|
||||||
'test_config' => ['label' => 'test_config'],
|
'test_config' => ['label' => 'test_config'],
|
||||||
]);
|
]);
|
||||||
|
@ -186,14 +192,25 @@ class EntityContextDefinitionIsSatisfiedTest extends UnitTestCase {
|
||||||
$entity->getEntityTypeId()->willReturn('test_content');
|
$entity->getEntityTypeId()->willReturn('test_content');
|
||||||
$entity->getIterator()->willReturn(new \ArrayIterator([]));
|
$entity->getIterator()->willReturn(new \ArrayIterator([]));
|
||||||
$entity->bundle()->willReturn($bundle);
|
$entity->bundle()->willReturn($bundle);
|
||||||
$content_entity_storage->createWithSampleValues($bundle)
|
$content_entity_storage->create(['the_bundle_key' => $bundle])
|
||||||
->willReturn($entity->reveal())
|
->willReturn($entity->reveal())
|
||||||
->shouldBeCalled();
|
->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->getStorage('test_content')->willReturn($content_entity_storage->reveal());
|
||||||
|
|
||||||
|
$this->entityTypeManager->getDefinition('test_content')->willReturn($entity_type);
|
||||||
$this->entityManager->getDefinitions()->willReturn([
|
$this->entityManager->getDefinitions()->willReturn([
|
||||||
'test_content' => $entity_type,
|
'test_content' => $entity_type,
|
||||||
]);
|
]);
|
||||||
|
|
Loading…
Reference in New Issue