class WebformBlockTest in Webform 8.5
Same name in this branch
- 8.5 tests/src/Functional/Block/WebformBlockTest.php \Drupal\Tests\webform\Functional\Block\WebformBlockTest
- 8.5 tests/src/Unit/Plugin/Block/WebformBlockTest.php \Drupal\Tests\webform\Unit\Plugin\Block\WebformBlockTest
Same name and namespace in other branches
- 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
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\webform\Unit\Plugin\Block\WebformBlockTest
Expanded class hierarchy of WebformBlockTest
File
- tests/
src/ Unit/ Plugin/ Block/ WebformBlockTest.php, line 25
Namespace
Drupal\Tests\webform\Unit\Plugin\BlockView 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
Name![]() |
Modifiers | Type | Description | Overrides |
---|---|---|---|---|
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. | |
UnitTestCase:: |
protected | function | 340 | |
WebformBlockTest:: |
protected | function | Create a mock webform block. | |
WebformBlockTest:: |
public | function | Tests the access of a webform block. | |
WebformBlockTest:: |
public | function | Tests the dependencies of a webform block. |