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