3 namespace Drupal\Tests\block_content\Kernel;
5 use Drupal\block_content\Entity\BlockContent;
6 use Drupal\block_content\Entity\BlockContentType;
7 use Drupal\block_content_test\Plugin\EntityReferenceSelection\TestSelection;
8 use Drupal\KernelTests\KernelTestBase;
11 * Tests EntityReference selection handlers don't return non-reusable blocks.
13 * @see block_content_query_entity_reference_alter()
15 * @group block_content
17 class BlockContentEntityReferenceSelectionTest extends KernelTestBase {
22 public static $modules = [
31 * The entity type manager.
33 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
35 protected $entityTypeManager;
38 * Test reusable block.
40 * @var \Drupal\block_content\BlockContentInterface
42 protected $blockReusable;
45 * Test non-reusable block.
47 * @var \Drupal\block_content\BlockContentInterface
49 protected $blockNonReusable;
52 * Test selection handler.
54 * @var \Drupal\block_content_test\Plugin\EntityReferenceSelection\TestSelection
56 protected $selectionHandler;
59 * Test block expectations.
63 protected $expectations;
68 public function setUp() {
70 $this->installSchema('system', ['sequence']);
71 $this->installSchema('system', ['sequences']);
72 $this->installEntitySchema('user');
73 $this->installEntitySchema('block_content');
75 // Create a block content type.
76 $block_content_type = BlockContentType::create([
78 'label' => 'Mucho spiffy',
79 'description' => "Provides a block type that increases your site's spiffiness by up to 11%",
81 $block_content_type->save();
82 $this->entityTypeManager = $this->container->get('entity_type.manager');
84 // And reusable block content entities.
85 $this->blockReusable = BlockContent::create([
86 'info' => 'Reusable Block',
89 $this->blockReusable->save();
90 $this->blockNonReusable = BlockContent::create([
91 'info' => 'Non-reusable Block',
95 $this->blockNonReusable->save();
98 'target_type' => 'block_content',
99 'target_bundles' => ['spiffy' => 'spiffy'],
100 'sort' => ['field' => '_none'],
102 $this->selectionHandler = new TestSelection($configuration, '', '', $this->container->get('entity.manager'), $this->container->get('module_handler'), \Drupal::currentUser());
104 // Setup the 3 expectation cases.
105 $this->expectations = [
108 $this->blockReusable->id() => $this->blockReusable->label(),
109 $this->blockNonReusable->id() => $this->blockNonReusable->label(),
112 'block_reusable' => ['spiffy' => [$this->blockReusable->id() => $this->blockReusable->label()]],
113 'block_non_reusable' => ['spiffy' => [$this->blockNonReusable->id() => $this->blockNonReusable->label()]],
118 * Tests to make sure queries without the expected tags are not altered.
120 * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
121 * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
123 public function testQueriesNotAltered() {
124 // Ensure that queries without all the tags are not altered.
125 $query = $this->entityTypeManager->getStorage('block_content')->getQuery();
126 $this->assertCount(2, $query->execute());
128 $query = $this->entityTypeManager->getStorage('block_content')->getQuery();
129 $query->addTag('block_content_access');
130 $this->assertCount(2, $query->execute());
132 $query = $this->entityTypeManager->getStorage('block_content')->getQuery();
133 $query->addTag('entity_query_block_content');
134 $this->assertCount(2, $query->execute());
138 * Test with no conditions set.
140 * @throws \Drupal\Core\Entity\EntityStorageException
142 public function testNoConditions() {
144 $this->expectations['block_reusable'],
145 $this->selectionHandler->getReferenceableEntities()
148 $this->blockNonReusable->setReusable();
149 $this->blockNonReusable->save();
151 // Ensure that the block is now returned as a referenceable entity.
153 $this->expectations['both_blocks'],
154 $this->selectionHandler->getReferenceableEntities()
159 * Tests setting 'reusable' condition on different levels.
161 * @dataProvider fieldConditionProvider
165 public function testFieldConditions($condition_type, $is_reusable) {
166 $this->selectionHandler->setTestMode($condition_type, $is_reusable);
168 $is_reusable ? $this->expectations['block_reusable'] : $this->expectations['block_non_reusable'],
169 $this->selectionHandler->getReferenceableEntities()
174 * Provides possible fields and condition types.
176 public function fieldConditionProvider() {
178 foreach (['base', 'group', 'nested_group'] as $condition_type) {
179 foreach ([TRUE, FALSE] as $reusable) {
180 $cases["$condition_type:" . ($reusable ? 'reusable' : 'non-reusable')] = [