You are here

public function AccessTest::testDrupalRegion in Twig Tweak 8.2

Same name and namespace in other branches
  1. 8 tests/src/Kernel/AccessTest.php \Drupal\Tests\twig_tweak\Kernel\AccessTest::testDrupalRegion()

Test callback.

File

tests/src/Kernel/AccessTest.php, line 340

Class

AccessTest
Tests for the Twig Tweak access control.

Namespace

Drupal\Tests\twig_tweak\Kernel

Code

public function testDrupalRegion() {

  // @codingStandardsIgnoreStart
  $create_block = function ($id) {
    return new class([
      'id' => $id,
    ], 'block') extends Block {
      public function access($operation, AccountInterface $account = NULL, $return_as_object = FALSE) {
        $result = AccessResult::allowedIf($this->id == 'block_1');
        $result
          ->cachePerUser();
        $result
          ->addCacheTags([
          'tag_for_' . $this->id,
        ]);
        $result
          ->setCacheMaxAge(123);
        return $return_as_object ? $result : $result
          ->isAllowed();
      }
      public function getPlugin() {
        return NULL;
      }

    };
  };

  // @codingStandardsIgnoreEnd
  $storage = $this
    ->createMock(EntityStorageInterface::class);
  $blocks = [
    'block_1' => $create_block('block_1'),
    'block_2' => $create_block('block_2'),
  ];
  $storage
    ->expects($this
    ->any())
    ->method('loadByProperties')
    ->willReturn($blocks);
  $view_builder = $this
    ->createMock(BlockViewBuilder::class);
  $content = [
    '#markup' => 'foo',
    '#cache' => [
      'tags' => [
        'tag_from_view',
      ],
    ],
  ];
  $view_builder
    ->expects($this
    ->any())
    ->method('view')
    ->willReturn($content);
  $entity_type = $this
    ->createMock(EntityTypeInterface::class);
  $entity_type
    ->expects($this
    ->any())
    ->method('getListCacheTags')
    ->willReturn([]);
  $entity_type
    ->expects($this
    ->any())
    ->method('getListCacheContexts')
    ->willReturn([]);
  $entity_type_manager = $this
    ->createMock(EntityTypeManagerInterface::class);
  $entity_type_manager
    ->expects($this
    ->any())
    ->method('getStorage')
    ->willReturn($storage);
  $entity_type_manager
    ->expects($this
    ->any())
    ->method('getViewBuilder')
    ->willReturn($view_builder);
  $entity_type_manager
    ->expects($this
    ->any())
    ->method('getDefinition')
    ->willReturn($entity_type);
  $this->container
    ->set('entity_type.manager', $entity_type_manager);
  $build = $this->twigExtension
    ->drupalRegion('bar');
  $expected_build = [
    'block_1' => [
      '#markup' => 'foo',
      '#cache' => [
        'tags' => [
          'tag_from_view',
        ],
      ],
    ],
    '#region' => 'bar',
    '#theme_wrappers' => [
      'region',
    ],
    '#cache' => [
      'contexts' => [
        'user',
      ],
      'tags' => [
        'tag_for_block_1',
        'tag_for_block_2',
      ],
      'max-age' => 123,
    ],
  ];
  self::assertSame($expected_build, $build);
}