Unverified Commit 1ebf06db authored by Lee Rowlands's avatar Lee Rowlands Committed by Lee Rowlands
Browse files

Issue #3270038 by larowlan, mohit_aghera:...

Issue #3270038 by larowlan, mohit_aghera: Drupal\lb_default_blocks\Plugin\Block\DefaultBlock::getPosition() can return NULL if the layout position has been deleted
parent 52f68612
Loading
Loading
Loading
Loading
+15 −4
Original line number Diff line number Diff line
@@ -176,12 +176,12 @@ class DefaultBlock extends BlockBase implements ContainerFactoryPluginInterface
  /**
   * Loads the layout position associated with this block.
   *
   * @return \Drupal\lb_default_blocks\Entity\LayoutPositionInterface
   * @return \Drupal\lb_default_blocks\Entity\LayoutPositionInterface|null
   *   The layout position
   */
  protected function getPosition(): LayoutPositionInterface {
  protected function getPosition(): ?LayoutPositionInterface {
    $position = $this->entityTypeManager->getStorage('lb_default_blocks_position')->load($this->configuration['position']);
    assert($position instanceof LayoutPositionInterface);
    assert($position instanceof LayoutPositionInterface || is_null($position));
    return $position;
  }

@@ -222,6 +222,9 @@ class DefaultBlock extends BlockBase implements ContainerFactoryPluginInterface
  protected function blockAccess(AccountInterface $account) {
    $context = $this->getContextValue('entity');
    $position = $this->getPosition();
    if (!$position) {
      return AccessResult::forbidden('Missing layout position')->addCacheTags(['lb_default_blocks_position_list']);
    }
    $lb_default_blocks = $this->doDefaultLookup($position, $context);
    if (empty($lb_default_blocks)) {
      if (empty($position->getDefault())) {
@@ -238,7 +241,15 @@ class DefaultBlock extends BlockBase implements ContainerFactoryPluginInterface
   */
  public function build() {
    $entity = $this->getContextValue('entity');
    return $this->buildForEntityPositionAndVocabularies($entity, $this->getPosition());
    $position = $this->getPosition();
    if (!$position) {
      return [
        '#cache' => [
          'tags' => ['lb_default_blocks_position_list'],
        ]
      ];
    }
    return $this->buildForEntityPositionAndVocabularies($entity, $position);
  }

  /**
+52 −0
Original line number Diff line number Diff line
<?php

declare(strict_types=1);

namespace Drupal\Tests\lb_default_blocks\Kernel;

use Drupal\Core\Block\BlockManager;
use Drupal\Core\Render\Element;
use Drupal\Core\Session\AnonymousUserSession;
use Drupal\lb_default_blocks\Plugin\Block\DefaultBlock;
use Drupal\Tests\node\Traits\NodeCreationTrait;

/**
 * Defines a class for testing missing position.
 *
 * @group lb_default_blocks
 */
final class MissingPositionTest extends LbDefaultBlocksKernelTestBase {

  use NodeCreationTrait;

  /**
   * Tests missing position.
   */
  public function testMissingPositionBuild() {
    $manager = \Drupal::service('plugin.manager.block');
    assert($manager instanceof BlockManager);
    $block = $manager->createInstance('lb_default_blocks', [
      'position' => 'yeah_this_position_does_not_exist',
    ]);
    assert($block instanceof DefaultBlock);
    $block->setContextValue('entity', $this->createNode());
    $build = $block->build();
    $this->assertTrue(!empty($build['#cache']));
    $this->assertTrue(Element::isEmpty($build));
  }

  /**
   * Tests missing position.
   */
  public function testMissingPositionAccess() {
    $manager = \Drupal::service('plugin.manager.block');
    assert($manager instanceof BlockManager);
    $block = $manager->createInstance('lb_default_blocks', [
      'position' => 'yeah_this_position_does_not_exist',
    ]);
    assert($block instanceof DefaultBlock);
    $block->setContextValue('entity', $this->createNode());
    $this->assertFalse($block->access(new AnonymousUserSession()));
  }

}