You are here

class WebformBlockTest in Webform 8.5

Same name in this branch
  1. 8.5 tests/src/Functional/Block/WebformBlockTest.php \Drupal\Tests\webform\Functional\Block\WebformBlockTest
  2. 8.5 tests/src/Unit/Plugin/Block/WebformBlockTest.php \Drupal\Tests\webform\Unit\Plugin\Block\WebformBlockTest
Same name and namespace in other branches
  1. 6.x tests/src/Unit/Plugin/Block/WebformBlockTest.php \Drupal\Tests\webform\Unit\Plugin\Block\WebformBlockTest

Tests webform submission bulk form actions.

@coversDefaultClass \Drupal\webform\Plugin\Block\WebformBlock

@group webform

Hierarchy

Expanded class hierarchy of WebformBlockTest

File

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

Namespace

Drupal\Tests\webform\Unit\Plugin\Block
View source
class WebformBlockTest extends UnitTestCase {

  /**
   * Tests the dependencies of a webform block.
   */
  public function testCalculateDependencies() {

    // Create mock webform and webform block.
    $webform = $this
      ->getMockBuilder(WebformInterface::class)
      ->disableOriginalConstructor()
      ->getMock();
    $webform
      ->method('id')
      ->willReturn($this
      ->randomMachineName());
    $webform
      ->method('getConfigDependencyKey')
      ->willReturn('config');
    $webform
      ->method('getConfigDependencyName')
      ->willReturn('config.webform.' . $webform
      ->id());
    $block = $this
      ->mockWebformBlock($webform);
    $dependencies = $block
      ->calculateDependencies();
    $expected = [
      $webform
        ->getConfigDependencyKey() => [
        $webform
          ->getConfigDependencyName(),
      ],
    ];
    $this
      ->assertEquals($expected, $dependencies, 'WebformBlock reports proper dependencies.');
  }

  /**
   * Tests the access of a webform block.
   */
  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.');
  }

  /**
   * Create a mock webform block.
   *
   * @param \Drupal\webform\WebformInterface $webform
   *   A webform.
   *
   * @return \Drupal\webform\Plugin\Block\WebformBlock
   *   A mock webform block.
   */
  protected function mockWebformBlock(WebformInterface $webform) {
    $request_stack = $this
      ->getMockBuilder(RequestStack::class)
      ->disableOriginalConstructor()
      ->getMock();
    $entity_type_manager = $this
      ->getMockBuilder(EntityTypeManagerInterface::class)
      ->disableOriginalConstructor()
      ->getMock();
    $storage = $this
      ->getMockBuilder(EntityStorageInterface::class)
      ->disableOriginalConstructor()
      ->getMock();
    $storage
      ->method('load')
      ->willReturnMap([
      [
        $webform
          ->id(),
        $webform,
      ],
    ]);
    $entity_type_manager
      ->method('getStorage')
      ->willReturnMap([
      [
        'webform',
        $storage,
      ],
    ]);
    $token_manager = $this
      ->getMockBuilder(WebformTokenManagerInterface::class)
      ->disableOriginalConstructor()
      ->getMock();
    $route_match = $this
      ->getMockBuilder(RouteMatchInterface::class)
      ->disableOriginalConstructor()
      ->getMock();
    $configuration = [
      'webform_id' => $webform
        ->id(),
    ];
    $plugin_id = 'webform_block';
    $plugin_definition = [
      'provider' => 'unit_test',
    ];
    return new WebformBlock($configuration, $plugin_id, $plugin_definition, $request_stack, $entity_type_manager, $token_manager, $route_match);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 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::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.
UnitTestCase::setUp protected function 340
WebformBlockTest::mockWebformBlock protected function Create a mock webform block.
WebformBlockTest::testBlockAccess public function Tests the access of a webform block.
WebformBlockTest::testCalculateDependencies public function Tests the dependencies of a webform block.