function EntityOperationsCacheabilityTest::testEntityOperationsCacheability
Same name and namespace in other branches
- 11.x core/modules/system/tests/src/Kernel/Entity/EntityOperationsCacheabilityTest.php \Drupal\Tests\system\Kernel\Entity\EntityOperationsCacheabilityTest::testEntityOperationsCacheability()
Test cacheability is added via entity operations functions and hooks.
Attributes
#[DataProvider('providerEntityOperationsCacheability')]
See also
\Drupal\Core\Entity\EntityListBuilder::getOperations()
\Drupal\Core\Entity\EntityListBuilder::getDefaultOperations()
\Drupal\entity_test\Hook\EntityTestHooks::entityOperation()
\Drupal\entity_test\Hook\EntityTestHooks::entityOperationAlter()
File
-
core/
modules/ system/ tests/ src/ Kernel/ Entity/ EntityOperationsCacheabilityTest.php, line 28
Class
- EntityOperationsCacheabilityTest
- Tests cacheability added by entity operations.
Namespace
Drupal\Tests\system\Kernel\EntityCode
public function testEntityOperationsCacheability(array $modules, string $entityType, array $properties, array $expectedTags, array $expectedContexts, bool $createBundle = FALSE, bool $setId = FALSE) : void {
if ($entityType === 'block') {
\Drupal::service('theme_installer')->install([
'stark',
]);
}
elseif (str_contains($entityType, 'image')) {
\Drupal::configFactory()->getEditable('system.image')
->set('toolkit', 'gd')
->save();
}
\Drupal::service('module_installer')->install($modules);
$entityTypeManager = \Drupal::entityTypeManager();
$storage = $entityTypeManager->getStorage($entityType);
$entityTypeDef = $entityTypeManager->getDefinition($entityType);
if ($createBundle) {
$bundleType = $entityTypeDef->getBundleEntityType();
$bundleTypeDef = $entityTypeManager->getDefinition($bundleType);
$entityTypeManager->getStorage($bundleType)
->create([
$bundleTypeDef->getKey('id') => 'test',
$bundleTypeDef->getKey('label') => 'test',
])
->save();
}
if ($entityTypeDef->getKey('label')) {
$properties += [
$entityTypeDef->getKey('label') => 'Test entity',
];
}
if ($setId) {
$properties += [
$entityTypeDef->getKey('id') => 'test',
];
}
if ($createBundle) {
$properties += [
$entityTypeDef->getKey('bundle') => 'test',
];
}
$entity = $storage->create($properties);
$entity->save();
$listBuilder = $entityTypeManager->getListBuilder($entityType);
$cacheability = new CacheableMetadata();
$listBuilder->getOperations($entity, $cacheability);
$this->assertSame($expectedTags, $cacheability->getCacheTags());
$this->assertEquals($expectedContexts, $cacheability->getCacheContexts());
$this->assertEquals(-1, $cacheability->getCacheMaxAge());
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.