Same filename and directory in other branches
- 8.9.x core/modules/block_content/tests/src/Kernel/BlockContentEntityReferenceSelectionTest.php
- 9 core/modules/block_content/tests/src/Kernel/BlockContentEntityReferenceSelectionTest.php
Namespace
Drupal\Tests\block_content\Kernel
File
core/modules/block_content/tests/src/Kernel/BlockContentEntityReferenceSelectionTest.php
View source
<?php
declare (strict_types=1);
namespace Drupal\Tests\block_content\Kernel;
use Drupal\block_content\Entity\BlockContent;
use Drupal\block_content\Entity\BlockContentType;
use Drupal\block_content_test\Plugin\EntityReferenceSelection\TestSelection;
use Drupal\KernelTests\KernelTestBase;
class BlockContentEntityReferenceSelectionTest extends KernelTestBase {
protected static $modules = [
'block',
'block_content',
'block_content_test',
'system',
'user',
];
protected $entityTypeManager;
protected $blockReusable;
protected $blockNonReusable;
protected $selectionHandler;
protected $expectations;
protected function setUp() : void {
parent::setUp();
$this
->installEntitySchema('user');
$this
->installEntitySchema('block_content');
$block_content_type = BlockContentType::create([
'id' => 'spiffy',
'label' => 'Mucho spiffy',
'description' => "Provides a block type that increases your site's spiffiness by up to 11%",
]);
$block_content_type
->save();
$this->entityTypeManager = $this->container
->get('entity_type.manager');
$this->blockReusable = BlockContent::create([
'info' => 'Reusable Block',
'type' => 'spiffy',
]);
$this->blockReusable
->save();
$this->blockNonReusable = BlockContent::create([
'info' => 'Non-reusable Block',
'type' => 'spiffy',
'reusable' => FALSE,
]);
$this->blockNonReusable
->save();
$configuration = [
'target_type' => 'block_content',
'target_bundles' => [
'spiffy' => 'spiffy',
],
'sort' => [
'field' => '_none',
],
];
$this->selectionHandler = new TestSelection($configuration, '', '', $this->container
->get('entity_type.manager'), $this->container
->get('module_handler'), \Drupal::currentUser(), \Drupal::service('entity_field.manager'), \Drupal::service('entity_type.bundle.info'), \Drupal::service('entity.repository'));
$this->expectations = [
'both_blocks' => [
'spiffy' => [
$this->blockReusable
->id() => $this->blockReusable
->label(),
$this->blockNonReusable
->id() => $this->blockNonReusable
->label(),
],
],
'block_reusable' => [
'spiffy' => [
$this->blockReusable
->id() => $this->blockReusable
->label(),
],
],
'block_non_reusable' => [
'spiffy' => [
$this->blockNonReusable
->id() => $this->blockNonReusable
->label(),
],
],
];
}
public function testQueriesNotAltered() {
$query = $this->entityTypeManager
->getStorage('block_content')
->getQuery()
->accessCheck(FALSE);
$this
->assertCount(2, $query
->execute());
$query = $this->entityTypeManager
->getStorage('block_content')
->getQuery()
->accessCheck(FALSE);
$query
->addTag('block_content_access');
$this
->assertCount(2, $query
->execute());
$query = $this->entityTypeManager
->getStorage('block_content')
->getQuery()
->accessCheck(FALSE);
$query
->addTag('entity_query_block_content');
$this
->assertCount(2, $query
->execute());
}
public function testNoConditions() {
$this
->assertEquals($this->expectations['block_reusable'], $this->selectionHandler
->getReferenceableEntities());
$this->blockNonReusable
->setReusable();
$this->blockNonReusable
->save();
$this
->assertEquals($this->expectations['both_blocks'], $this->selectionHandler
->getReferenceableEntities());
}
public function testFieldConditions($condition_type, $is_reusable) {
$this->selectionHandler
->setTestMode($condition_type, $is_reusable);
$this
->assertEquals($is_reusable ? $this->expectations['block_reusable'] : $this->expectations['block_non_reusable'], $this->selectionHandler
->getReferenceableEntities());
}
public static function fieldConditionProvider() {
$cases = [];
foreach ([
'base',
'group',
'nested_group',
] as $condition_type) {
foreach ([
TRUE,
FALSE,
] as $reusable) {
$cases["{$condition_type}:" . ($reusable ? 'reusable' : 'non-reusable')] = [
$condition_type,
$reusable,
];
}
}
return $cases;
}
}
Classes