X-Git-Url: http://www.aleph1.co.uk/gitweb/?a=blobdiff_plain;ds=sidebyside;f=web%2Fcore%2Fmodules%2Fblock_content%2Ftests%2Fsrc%2FKernel%2FBlockContentEntityReferenceSelectionTest.php;fp=web%2Fcore%2Fmodules%2Fblock_content%2Ftests%2Fsrc%2FKernel%2FBlockContentEntityReferenceSelectionTest.php;h=e593336fa3a1eff8526203a199679efb5bca1b56;hb=0bf8d09d2542548982e81a441b1f16e75873a04f;hp=0000000000000000000000000000000000000000;hpb=74df008bdbb3a11eeea356744f39b802369bda3c;p=yaffs-website diff --git a/web/core/modules/block_content/tests/src/Kernel/BlockContentEntityReferenceSelectionTest.php b/web/core/modules/block_content/tests/src/Kernel/BlockContentEntityReferenceSelectionTest.php new file mode 100644 index 000000000..e593336fa --- /dev/null +++ b/web/core/modules/block_content/tests/src/Kernel/BlockContentEntityReferenceSelectionTest.php @@ -0,0 +1,189 @@ +installSchema('system', ['sequence']); + $this->installSchema('system', ['sequences']); + $this->installEntitySchema('user'); + $this->installEntitySchema('block_content'); + + // Create a block content type. + $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'); + + // And reusable block content entities. + $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.manager'), $this->container->get('module_handler'), \Drupal::currentUser()); + + // Setup the 3 expectation cases. + $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()]], + ]; + } + + /** + * Tests to make sure queries without the expected tags are not altered. + * + * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException + * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException + */ + public function testQueriesNotAltered() { + // Ensure that queries without all the tags are not altered. + $query = $this->entityTypeManager->getStorage('block_content')->getQuery(); + $this->assertCount(2, $query->execute()); + + $query = $this->entityTypeManager->getStorage('block_content')->getQuery(); + $query->addTag('block_content_access'); + $this->assertCount(2, $query->execute()); + + $query = $this->entityTypeManager->getStorage('block_content')->getQuery(); + $query->addTag('entity_query_block_content'); + $this->assertCount(2, $query->execute()); + } + + /** + * Test with no conditions set. + * + * @throws \Drupal\Core\Entity\EntityStorageException + */ + public function testNoConditions() { + $this->assertEquals( + $this->expectations['block_reusable'], + $this->selectionHandler->getReferenceableEntities() + ); + + $this->blockNonReusable->setReusable(); + $this->blockNonReusable->save(); + + // Ensure that the block is now returned as a referenceable entity. + $this->assertEquals( + $this->expectations['both_blocks'], + $this->selectionHandler->getReferenceableEntities() + ); + } + + /** + * Tests setting 'reusable' condition on different levels. + * + * @dataProvider fieldConditionProvider + * + * @throws \Exception + */ + 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() + ); + } + + /** + * Provides possible fields and condition types. + */ + public 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; + } + +}