class SectionRenderTest

Same name in other branches
  1. 9 core/modules/layout_builder/tests/src/Unit/SectionRenderTest.php \Drupal\Tests\layout_builder\Unit\SectionRenderTest
  2. 8.9.x core/modules/layout_builder/tests/src/Unit/SectionRenderTest.php \Drupal\Tests\layout_builder\Unit\SectionRenderTest
  3. 11.x core/modules/layout_builder/tests/src/Unit/SectionRenderTest.php \Drupal\Tests\layout_builder\Unit\SectionRenderTest

@coversDefaultClass \Drupal\layout_builder\Section @group layout_builder

Hierarchy

Expanded class hierarchy of SectionRenderTest

File

core/modules/layout_builder/tests/src/Unit/SectionRenderTest.php, line 32

Namespace

Drupal\Tests\layout_builder\Unit
View source
class SectionRenderTest extends UnitTestCase {
    
    /**
     * The current user.
     *
     * @var \Drupal\Core\Session\AccountInterface
     */
    protected $account;
    
    /**
     * The block plugin manager.
     *
     * @var \Drupal\Core\Block\BlockManagerInterface
     */
    protected $blockManager;
    
    /**
     * The plugin context handler.
     *
     * @var \Drupal\Core\Plugin\Context\ContextHandlerInterface
     */
    protected $contextHandler;
    
    /**
     * The context manager service.
     *
     * @var \Drupal\Core\Plugin\Context\ContextRepositoryInterface
     */
    protected $contextRepository;
    
    /**
     * The event dispatcher.
     *
     * @var \Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher
     */
    protected $eventDispatcher;
    
    /**
     * {@inheritdoc}
     */
    protected function setUp() : void {
        parent::setUp();
        $layout_plugin_manager = $this->prophesize(LayoutPluginManagerInterface::class);
        $this->blockManager = $this->prophesize(BlockManagerInterface::class);
        $this->contextHandler = $this->prophesize(ContextHandlerInterface::class);
        $this->contextRepository = $this->prophesize(ContextRepositoryInterface::class);
        // @todo Refactor this into some better tests in https://www.drupal.org/node/2942605.
        $this->eventDispatcher = (new \ReflectionClass(ContainerAwareEventDispatcher::class))->newInstanceWithoutConstructor();
        $this->account = $this->prophesize(AccountInterface::class);
        $subscriber = new BlockComponentRenderArray($this->account
            ->reveal());
        $this->eventDispatcher
            ->addSubscriber($subscriber);
        $layout = $this->prophesize(LayoutInterface::class);
        $layout->getPluginDefinition()
            ->willReturn(new LayoutDefinition([]));
        $layout->build(Argument::type('array'))
            ->willReturnArgument(0);
        $layout_plugin_manager->createInstance('layout_onecol', [])
            ->willReturn($layout->reveal());
        $container = new ContainerBuilder();
        $container->set('current_user', $this->account
            ->reveal());
        $container->set('plugin.manager.block', $this->blockManager
            ->reveal());
        $container->set('plugin.manager.core.layout', $layout_plugin_manager->reveal());
        $container->set('context.handler', $this->contextHandler
            ->reveal());
        $container->set('context.repository', $this->contextRepository
            ->reveal());
        $container->set('event_dispatcher', $this->eventDispatcher);
        \Drupal::setContainer($container);
    }
    
    /**
     * @covers ::toRenderArray
     */
    public function testToRenderArray() : void {
        $block_content = [
            '#markup' => 'The block content.',
        ];
        $placeholder_label = 'Placeholder Label';
        $render_array = [
            '#theme' => 'block',
            '#weight' => 0,
            '#configuration' => [],
            '#plugin_id' => 'block_plugin_id',
            '#base_plugin_id' => 'block_plugin_id',
            '#derivative_plugin_id' => NULL,
            'content' => $block_content,
            '#cache' => [
                'contexts' => [],
                'tags' => [],
                'max-age' => -1,
            ],
            '#in_preview' => FALSE,
        ];
        $block = $this->prophesize(BlockPluginInterface::class)
            ->willImplement(PreviewFallbackInterface::class);
        $this->blockManager
            ->createInstance('block_plugin_id', [
            'id' => 'block_plugin_id',
        ])
            ->willReturn($block->reveal());
        $access_result = AccessResult::allowed();
        $block->access($this->account
            ->reveal(), TRUE)
            ->willReturn($access_result);
        $block->build()
            ->willReturn($block_content);
        $block->getCacheContexts()
            ->willReturn([]);
        $block->getCacheTags()
            ->willReturn([]);
        $block->getCacheMaxAge()
            ->willReturn(Cache::PERMANENT);
        $block->getPluginId()
            ->willReturn('block_plugin_id');
        $block->getBaseId()
            ->willReturn('block_plugin_id');
        $block->getDerivativeId()
            ->willReturn(NULL);
        $block->getConfiguration()
            ->willReturn([]);
        $block->getPreviewFallbackString()
            ->willReturn($placeholder_label);
        $section = [
            new SectionComponent('some_uuid', 'content', [
                'id' => 'block_plugin_id',
            ]),
        ];
        $expected = [
            'content' => [
                'some_uuid' => $render_array,
            ],
        ];
        $result = (new Section('layout_onecol', [], $section))->toRenderArray();
        $this->assertEquals($expected, $result);
    }
    
    /**
     * @covers ::toRenderArray
     */
    public function testToRenderArrayAccessDenied() : void {
        $block = $this->prophesize(BlockPluginInterface::class);
        $this->blockManager
            ->createInstance('block_plugin_id', [
            'id' => 'block_plugin_id',
        ])
            ->willReturn($block->reveal());
        $access_result = AccessResult::forbidden();
        $block->access($this->account
            ->reveal(), TRUE)
            ->willReturn($access_result);
        $block->build()
            ->shouldNotBeCalled();
        $block->getCacheContexts()
            ->willReturn([]);
        $block->getCacheTags()
            ->willReturn([]);
        $block->getCacheMaxAge()
            ->willReturn(Cache::PERMANENT);
        $section = [
            new SectionComponent('some_uuid', 'content', [
                'id' => 'block_plugin_id',
            ]),
        ];
        $expected = [
            'content' => [
                'some_uuid' => [
                    '#cache' => [
                        'contexts' => [],
                        'tags' => [],
                        'max-age' => -1,
                    ],
                ],
            ],
        ];
        $result = (new Section('layout_onecol', [], $section))->toRenderArray();
        $this->assertEquals($expected, $result);
    }
    
    /**
     * @covers ::toRenderArray
     */
    public function testToRenderArrayPreview() : void {
        $block_content = [
            '#markup' => 'The block content.',
        ];
        $placeholder_label = 'Placeholder Label';
        $render_array = [
            '#theme' => 'block',
            '#weight' => 0,
            '#configuration' => [],
            '#plugin_id' => 'block_plugin_id',
            '#base_plugin_id' => 'block_plugin_id',
            '#derivative_plugin_id' => NULL,
            'content' => $block_content,
            '#attributes' => [
                'data-layout-content-preview-placeholder-label' => $placeholder_label,
            ],
            '#cache' => [
                'contexts' => [],
                'tags' => [],
                'max-age' => 0,
            ],
            '#in_preview' => TRUE,
        ];
        $block = $this->prophesize(BlockPluginInterface::class)
            ->willImplement(PreviewFallbackInterface::class);
        $this->blockManager
            ->createInstance('block_plugin_id', [
            'id' => 'block_plugin_id',
        ])
            ->willReturn($block->reveal());
        $block->access($this->account
            ->reveal(), TRUE)
            ->shouldNotBeCalled();
        $block->build()
            ->willReturn($block_content);
        $block->getCacheContexts()
            ->willReturn([]);
        $block->getCacheTags()
            ->willReturn([]);
        $block->getCacheMaxAge()
            ->willReturn(Cache::PERMANENT);
        $block->getConfiguration()
            ->willReturn([]);
        $block->getPluginId()
            ->willReturn('block_plugin_id');
        $block->getBaseId()
            ->willReturn('block_plugin_id');
        $block->getDerivativeId()
            ->willReturn(NULL);
        $block->getPreviewFallbackString()
            ->willReturn($placeholder_label);
        $section = [
            new SectionComponent('some_uuid', 'content', [
                'id' => 'block_plugin_id',
            ]),
        ];
        $expected = [
            'content' => [
                'some_uuid' => $render_array,
            ],
        ];
        $result = (new Section('layout_onecol', [], $section))->toRenderArray([], TRUE);
        $this->assertEquals($expected, $result);
    }
    
    /**
     * @covers ::toRenderArray
     */
    public function testToRenderArrayEmpty() : void {
        $section = [];
        $expected = [];
        $result = (new Section('layout_onecol', [], $section))->toRenderArray();
        $this->assertEquals($expected, $result);
    }
    
    /**
     * @covers ::toRenderArray
     */
    public function testContextAwareBlock() : void {
        $block_content = [
            '#markup' => 'The block content.',
        ];
        $placeholder_label = 'Placeholder Label';
        $render_array = [
            '#theme' => 'block',
            '#weight' => 0,
            '#configuration' => [],
            '#plugin_id' => 'block_plugin_id',
            '#base_plugin_id' => 'block_plugin_id',
            '#derivative_plugin_id' => NULL,
            'content' => $block_content,
            '#cache' => [
                'contexts' => [],
                'tags' => [],
                'max-age' => -1,
            ],
            '#in_preview' => FALSE,
        ];
        $block = $this->prophesize(BlockPluginInterface::class)
            ->willImplement(ContextAwarePluginInterface::class)
            ->willImplement(PreviewFallbackInterface::class);
        $this->blockManager
            ->createInstance('block_plugin_id', [
            'id' => 'block_plugin_id',
        ])
            ->willReturn($block->reveal());
        $access_result = AccessResult::allowed();
        $block->access($this->account
            ->reveal(), TRUE)
            ->willReturn($access_result);
        $block->build()
            ->willReturn($block_content);
        $block->getCacheContexts()
            ->willReturn([]);
        $block->getCacheTags()
            ->willReturn([]);
        $block->getCacheMaxAge()
            ->willReturn(Cache::PERMANENT);
        $block->getContextMapping()
            ->willReturn([]);
        $block->getPluginId()
            ->willReturn('block_plugin_id');
        $block->getBaseId()
            ->willReturn('block_plugin_id');
        $block->getDerivativeId()
            ->willReturn(NULL);
        $block->getConfiguration()
            ->willReturn([]);
        $block->getPreviewFallbackString()
            ->willReturn($placeholder_label);
        $section = [
            new SectionComponent('some_uuid', 'content', [
                'id' => 'block_plugin_id',
            ]),
        ];
        $expected = [
            'content' => [
                'some_uuid' => $render_array,
            ],
        ];
        $result = (new Section('layout_onecol', [], $section))->toRenderArray();
        $this->assertEquals($expected, $result);
    }
    
    /**
     * @covers ::toRenderArray
     */
    public function testToRenderArrayMissingPluginId() : void {
        $this->expectException(PluginException::class);
        $this->expectExceptionMessage('No plugin ID specified for component with "some_uuid" UUID');
        (new Section('layout_onecol', [], [
            new SectionComponent('some_uuid', 'content'),
        ]))->toRenderArray();
    }

}

Members

Title Sort descending Deprecated Modifiers Object type Summary Overriden Title Overrides
PhpUnitWarnings::$deprecationWarnings private static property Deprecation warnings from PHPUnit to raise with @trigger_error().
PhpUnitWarnings::addWarning public function Converts PHPUnit deprecation warnings to E_USER_DEPRECATED.
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.
RandomGeneratorTrait::randomStringValidate Deprecated public function Callback for random string validation.
SectionRenderTest::$account protected property The current user.
SectionRenderTest::$blockManager protected property The block plugin manager.
SectionRenderTest::$contextHandler protected property The plugin context handler.
SectionRenderTest::$contextRepository protected property The context manager service.
SectionRenderTest::$eventDispatcher protected property The event dispatcher.
SectionRenderTest::setUp protected function Overrides UnitTestCase::setUp
SectionRenderTest::testContextAwareBlock public function @covers ::toRenderArray
SectionRenderTest::testToRenderArray public function @covers ::toRenderArray
SectionRenderTest::testToRenderArrayAccessDenied public function @covers ::toRenderArray
SectionRenderTest::testToRenderArrayEmpty public function @covers ::toRenderArray
SectionRenderTest::testToRenderArrayMissingPluginId public function @covers ::toRenderArray
SectionRenderTest::testToRenderArrayPreview public function @covers ::toRenderArray
UnitTestCase::$root protected property The app root. 1
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::setUpBeforeClass public static function
UnitTestCase::__get public function

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