View source
<?php
namespace Drupal\Tests\block_field\Kernel;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
use Drupal\Tests\block_field\Traits\BlockFieldTestTrait;
class BlockFieldFormatterTest extends EntityKernelTestBase {
use BlockFieldTestTrait;
public static $modules = [
'block',
'block_field',
'block_field_test',
];
protected function setUp() {
parent::setUp();
$this
->installConfig([
'user',
]);
$this
->installEntitySchema('entity_test');
$this
->createBlockField('entity_test', 'entity_test', 'field_test', 'Field test', 'default', [], FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
}
public function testBlockAccessCacheMetadata() {
$renderer = $this->container
->get('renderer');
$referencing_entity = $this->container
->get('entity_type.manager')
->getStorage('entity_test')
->create([
'name' => $this
->randomMachineName(),
]);
$referencing_entity->field_test->plugin_id = 'block_field_test_access';
$referencing_entity->field_test->settings = [
'label' => 'Custom access.',
'label_display' => TRUE,
'access' => TRUE,
];
$referencing_entity
->save();
$items = $referencing_entity
->get('field_test');
$formatter_manager = $this->container
->get('plugin.manager.field.formatter');
foreach ($formatter_manager
->getOptions('block_field') as $formatter => $name) {
$build = $items
->view([
'type' => $formatter,
'settings' => [],
]);
$markup = $renderer
->renderRoot($build);
$this
->assertEquals($build['#cache']['tags'], [
'custom_tag',
], sprintf('The formatter %s does not propagate tags correctly.', $name));
$this
->assertStringContainsString('Custom access.', (string) $markup, sprintf('The contents of the block are missing when using the %s formatter', $name));
}
$referencing_entity->field_test->settings = [
'label' => 'Custom access.',
'label_display' => TRUE,
'access' => FALSE,
];
$referencing_entity
->save();
foreach ($formatter_manager
->getOptions('block_field') as $formatter => $name) {
$build = $items
->view([
'type' => $formatter,
'settings' => [],
]);
$markup = $renderer
->renderRoot($build);
$this
->assertEquals($build['#cache']['tags'], [
'custom_tag',
], sprintf('The formatter %s does not propagate tags correctly.', $name));
$this
->assertStringNotContainsString('Custom access.', $markup, sprintf('The block contents show even when we denied access when using the %s formatter', $name));
}
}
}