You are here

public function WebformBlockTest::testBlockAccess in Webform 8.5

Same name and namespace in other branches
  1. 6.x tests/src/Unit/Plugin/Block/WebformBlockTest.php \Drupal\Tests\webform\Unit\Plugin\Block\WebformBlockTest::testBlockAccess()

Tests the access of a webform block.

File

tests/src/Unit/Plugin/Block/WebformBlockTest.php, line 53

Class

WebformBlockTest
Tests webform submission bulk form actions.

Namespace

Drupal\Tests\webform\Unit\Plugin\Block

Code

public function testBlockAccess() {
  $account = $this
    ->getMockBuilder(AccountInterface::class)
    ->disableOriginalConstructor()
    ->getMock();
  $cache_contexts = [
    'dummy_cache_context',
  ];
  $cache_contexts_manager = $this
    ->getMockBuilder(CacheContextsManager::class)
    ->disableOriginalConstructor()
    ->getMock();
  $cache_contexts_manager
    ->method('assertValidTokens')
    ->willReturnMap([
    [
      $cache_contexts,
      TRUE,
    ],
  ]);
  $container = $this
    ->getMockBuilder(ContainerInterface::class)
    ->disableOriginalConstructor()
    ->getMock();
  $container
    ->method('get')
    ->willReturnMap([
    [
      'cache_contexts_manager',
      ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
      $cache_contexts_manager,
    ],
  ]);
  \Drupal::setContainer($container);
  $access_result = AccessResult::allowed();
  $access_result
    ->setCacheMaxAge(1);
  $access_result
    ->addCacheTags([
    'dummy_cache_tag',
  ]);
  $access_result
    ->addCacheContexts($cache_contexts);

  // Create mock webform and webform block.
  $webform = $this
    ->getMockBuilder(WebformInterface::class)
    ->disableOriginalConstructor()
    ->getMock();
  $webform
    ->method('id')
    ->willReturn($this
    ->randomMachineName());
  $webform
    ->method('access')
    ->willReturnMap([
    [
      'submission_create',
      $account,
      TRUE,
      $access_result,
    ],
  ]);
  $block = $this
    ->mockWebformBlock($webform);
  $result = $block
    ->access($account, TRUE);

  // Make sure the block transparently follows the webform access logic.
  $this
    ->assertSame($access_result
    ->isAllowed(), $result
    ->isAllowed(), 'Block access yields the same result as the access of the webform.');
  $this
    ->assertEquals($access_result
    ->getCacheContexts(), $result
    ->getCacheContexts(), 'Block access has the same cache contexts as the access of the webform.');
  $this
    ->assertEquals($access_result
    ->getCacheTags(), $result
    ->getCacheTags(), 'Block access has the same cache tags as the access of the webform.');
  $this
    ->assertEquals($access_result
    ->getCacheMaxAge(), $result
    ->getCacheMaxAge(), 'Block access has the same cache max age as the access of the webform.');
}