BlockContentEntityReferenceSelectionTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. namespace Drupal\Tests\block_content\Kernel;
  3. use Drupal\block_content\Entity\BlockContent;
  4. use Drupal\block_content\Entity\BlockContentType;
  5. use Drupal\block_content_test\Plugin\EntityReferenceSelection\TestSelection;
  6. use Drupal\KernelTests\KernelTestBase;
  7. /**
  8. * Tests EntityReference selection handlers don't return non-reusable blocks.
  9. *
  10. * @see block_content_query_entity_reference_alter()
  11. *
  12. * @group block_content
  13. */
  14. class BlockContentEntityReferenceSelectionTest extends KernelTestBase {
  15. /**
  16. * {@inheritdoc}
  17. */
  18. public static $modules = [
  19. 'block',
  20. 'block_content',
  21. 'block_content_test',
  22. 'system',
  23. 'user',
  24. ];
  25. /**
  26. * The entity type manager.
  27. *
  28. * @var \Drupal\Core\Entity\EntityTypeManagerInterface
  29. */
  30. protected $entityTypeManager;
  31. /**
  32. * Test reusable block.
  33. *
  34. * @var \Drupal\block_content\BlockContentInterface
  35. */
  36. protected $blockReusable;
  37. /**
  38. * Test non-reusable block.
  39. *
  40. * @var \Drupal\block_content\BlockContentInterface
  41. */
  42. protected $blockNonReusable;
  43. /**
  44. * Test selection handler.
  45. *
  46. * @var \Drupal\block_content_test\Plugin\EntityReferenceSelection\TestSelection
  47. */
  48. protected $selectionHandler;
  49. /**
  50. * Test block expectations.
  51. *
  52. * @var array
  53. */
  54. protected $expectations;
  55. /**
  56. * {@inheritdoc}
  57. */
  58. public function setUp() {
  59. parent::setUp();
  60. $this->installSchema('system', ['sequence']);
  61. $this->installSchema('system', ['sequences']);
  62. $this->installEntitySchema('user');
  63. $this->installEntitySchema('block_content');
  64. // Create a block content type.
  65. $block_content_type = BlockContentType::create([
  66. 'id' => 'spiffy',
  67. 'label' => 'Mucho spiffy',
  68. 'description' => "Provides a block type that increases your site's spiffiness by up to 11%",
  69. ]);
  70. $block_content_type->save();
  71. $this->entityTypeManager = $this->container->get('entity_type.manager');
  72. // And reusable block content entities.
  73. $this->blockReusable = BlockContent::create([
  74. 'info' => 'Reusable Block',
  75. 'type' => 'spiffy',
  76. ]);
  77. $this->blockReusable->save();
  78. $this->blockNonReusable = BlockContent::create([
  79. 'info' => 'Non-reusable Block',
  80. 'type' => 'spiffy',
  81. 'reusable' => FALSE,
  82. ]);
  83. $this->blockNonReusable->save();
  84. $configuration = [
  85. 'target_type' => 'block_content',
  86. 'target_bundles' => ['spiffy' => 'spiffy'],
  87. 'sort' => ['field' => '_none'],
  88. ];
  89. $this->selectionHandler = new TestSelection($configuration, '', '', $this->container->get('entity.manager'), $this->container->get('module_handler'), \Drupal::currentUser());
  90. // Setup the 3 expectation cases.
  91. $this->expectations = [
  92. 'both_blocks' => [
  93. 'spiffy' => [
  94. $this->blockReusable->id() => $this->blockReusable->label(),
  95. $this->blockNonReusable->id() => $this->blockNonReusable->label(),
  96. ],
  97. ],
  98. 'block_reusable' => ['spiffy' => [$this->blockReusable->id() => $this->blockReusable->label()]],
  99. 'block_non_reusable' => ['spiffy' => [$this->blockNonReusable->id() => $this->blockNonReusable->label()]],
  100. ];
  101. }
  102. /**
  103. * Tests to make sure queries without the expected tags are not altered.
  104. *
  105. * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
  106. * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
  107. */
  108. public function testQueriesNotAltered() {
  109. // Ensure that queries without all the tags are not altered.
  110. $query = $this->entityTypeManager->getStorage('block_content')->getQuery();
  111. $this->assertCount(2, $query->execute());
  112. $query = $this->entityTypeManager->getStorage('block_content')->getQuery();
  113. $query->addTag('block_content_access');
  114. $this->assertCount(2, $query->execute());
  115. $query = $this->entityTypeManager->getStorage('block_content')->getQuery();
  116. $query->addTag('entity_query_block_content');
  117. $this->assertCount(2, $query->execute());
  118. }
  119. /**
  120. * Test with no conditions set.
  121. *
  122. * @throws \Drupal\Core\Entity\EntityStorageException
  123. */
  124. public function testNoConditions() {
  125. $this->assertEquals(
  126. $this->expectations['block_reusable'],
  127. $this->selectionHandler->getReferenceableEntities()
  128. );
  129. $this->blockNonReusable->setReusable();
  130. $this->blockNonReusable->save();
  131. // Ensure that the block is now returned as a referenceable entity.
  132. $this->assertEquals(
  133. $this->expectations['both_blocks'],
  134. $this->selectionHandler->getReferenceableEntities()
  135. );
  136. }
  137. /**
  138. * Tests setting 'reusable' condition on different levels.
  139. *
  140. * @dataProvider fieldConditionProvider
  141. *
  142. * @throws \Exception
  143. */
  144. public function testFieldConditions($condition_type, $is_reusable) {
  145. $this->selectionHandler->setTestMode($condition_type, $is_reusable);
  146. $this->assertEquals(
  147. $is_reusable ? $this->expectations['block_reusable'] : $this->expectations['block_non_reusable'],
  148. $this->selectionHandler->getReferenceableEntities()
  149. );
  150. }
  151. /**
  152. * Provides possible fields and condition types.
  153. */
  154. public function fieldConditionProvider() {
  155. $cases = [];
  156. foreach (['base', 'group', 'nested_group'] as $condition_type) {
  157. foreach ([TRUE, FALSE] as $reusable) {
  158. $cases["$condition_type:" . ($reusable ? 'reusable' : 'non-reusable')] = [
  159. $condition_type,
  160. $reusable,
  161. ];
  162. }
  163. }
  164. return $cases;
  165. }
  166. }