class LayoutBuilderIsActiveCacheContextTest

Same name in other branches
  1. 9 core/modules/layout_builder/tests/src/Unit/LayoutBuilderIsActiveCacheContextTest.php \Drupal\Tests\layout_builder\Unit\LayoutBuilderIsActiveCacheContextTest
  2. 10 core/modules/layout_builder/tests/src/Unit/LayoutBuilderIsActiveCacheContextTest.php \Drupal\Tests\layout_builder\Unit\LayoutBuilderIsActiveCacheContextTest

@coversDefaultClass \Drupal\layout_builder\Cache\LayoutBuilderIsActiveCacheContext

@group layout_builder

Hierarchy

Expanded class hierarchy of LayoutBuilderIsActiveCacheContextTest

File

core/modules/layout_builder/tests/src/Unit/LayoutBuilderIsActiveCacheContextTest.php, line 24

Namespace

Drupal\Tests\layout_builder\Unit
View source
class LayoutBuilderIsActiveCacheContextTest extends UnitTestCase {
    
    /**
     * @covers ::getContext
     */
    public function testGetContextMissingEntityTypeId() : void {
        $route_match = $this->prophesize(RouteMatchInterface::class);
        $cache_context = new LayoutBuilderIsActiveCacheContext($route_match->reveal());
        $this->expectException(\LogicException::class);
        $this->expectExceptionMessage('Missing entity type ID');
        $cache_context->getContext();
    }
    
    /**
     * @covers ::getContext
     * @covers ::getDisplay
     */
    public function testGetContextNonFieldableEntity() : void {
        $route_match = $this->prophesize(RouteMatchInterface::class);
        $route_match->getParameter('not_a_fieldable_entity')
            ->willReturn('something that is not a fieldable entity');
        $cache_context = new LayoutBuilderIsActiveCacheContext($route_match->reveal());
        $expected = '0';
        $actual = $cache_context->getContext('not_a_fieldable_entity');
        $this->assertSame($expected, $actual);
    }
    
    /**
     * @covers ::getContext
     * @covers ::getDisplay
     *
     * @dataProvider providerTestGetContext
     */
    public function testGetContext($is_overridden, $expected) : void {
        $entity_display = $this->prophesize(LayoutEntityDisplayInterface::class);
        $entity_display->isOverridable()
            ->willReturn($is_overridden);
        $entity_type_id = 'a_fieldable_entity_type';
        $fieldable_entity = $this->prophesize(FieldableEntityInterface::class);
        $fieldable_entity->getEntityTypeId()
            ->willReturn($entity_type_id);
        $fieldable_entity->bundle()
            ->willReturn('the_bundle_id');
        $route_match = $this->prophesize(RouteMatchInterface::class);
        $route_match->getParameter($entity_type_id)
            ->willReturn($fieldable_entity->reveal());
        // \Drupal\Core\Entity\Entity\EntityViewDisplay::collectRenderDisplay() is a
        // static method and can not be mocked on its own. All of the expectations
        // of that method are mocked in the next code block.
        $entity_query = $this->prophesize(QueryInterface::class);
        $entity_query->condition(Argument::cetera())
            ->willReturn($entity_query);
        $entity_query->execute()
            ->willReturn([
            'a_fieldable_entity_type.the_bundle_id.full' => 'a_fieldable_entity_type.the_bundle_id.full',
        ]);
        $entity_storage = $this->prophesize(EntityStorageInterface::class);
        $entity_storage->getQuery('AND')
            ->willReturn($entity_query->reveal());
        $entity_storage->loadMultiple(Argument::type('array'))
            ->willReturn([
            'a_fieldable_entity_type.the_bundle_id.full' => $entity_display->reveal(),
        ]);
        $entity_type_manager = $this->prophesize(EntityTypeManagerInterface::class);
        $entity_type_manager->getStorage('entity_view_display')
            ->willReturn($entity_storage->reveal());
        $module_handler = $this->prophesize(ModuleHandlerInterface::class);
        $container = new ContainerBuilder();
        $container->set('entity_type.manager', $entity_type_manager->reveal());
        $container->set('module_handler', $module_handler->reveal());
        \Drupal::setContainer($container);
        $cache_context = new LayoutBuilderIsActiveCacheContext($route_match->reveal());
        $actual = $cache_context->getContext($entity_type_id);
        $this->assertSame($expected, $actual);
    }
    
    /**
     * Provides test data for ::testGetContext().
     */
    public static function providerTestGetContext() {
        $data = [];
        $data['overridden'] = [
            TRUE,
            '1',
        ];
        $data['not overridden'] = [
            FALSE,
            '0',
        ];
        return $data;
    }

}

Members

Title Sort descending Modifiers Object type Summary Overrides
ExpectDeprecationTrait::expectDeprecation public function Adds an expected deprecation.
ExpectDeprecationTrait::getCallableName private static function Returns a callable as a string suitable for inclusion in a message.
ExpectDeprecationTrait::setUpErrorHandler public function Sets up the test error handler.
ExpectDeprecationTrait::tearDownErrorHandler public function Tears down the test error handler.
LayoutBuilderIsActiveCacheContextTest::providerTestGetContext public static function Provides test data for ::testGetContext().
LayoutBuilderIsActiveCacheContextTest::testGetContext public function @covers ::getContext
@covers ::getDisplay
LayoutBuilderIsActiveCacheContextTest::testGetContextMissingEntityTypeId public function @covers ::getContext
LayoutBuilderIsActiveCacheContextTest::testGetContextNonFieldableEntity public function @covers ::getContext
@covers ::getDisplay
RandomGeneratorTrait::getRandomGenerator protected function Gets the random generator for the utility methods.
RandomGeneratorTrait::randomMachineName protected function Generates a unique random string containing letters and numbers.
RandomGeneratorTrait::randomObject public function Generates a random PHP object.
RandomGeneratorTrait::randomString public function Generates a pseudo-random string of ASCII characters of codes 32 to 126.
UnitTestCase::$root protected property The app root.
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::setUp protected function 367
UnitTestCase::setUpBeforeClass public static function

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.