View source
<?php
namespace Drupal\Tests\webform\Kernel\Breadcrumb;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Tests\UnitTestCase;
use Drupal\Core\Link;
use Symfony\Component\DependencyInjection\Container;
class WebformBreadcrumbBuilderTest extends UnitTestCase {
protected $moduleHandler;
protected $configFactory;
protected $requestHandler;
protected $translationManager;
protected $breadcrumbBuilder;
protected $node;
protected $nodeAccess;
protected $webform;
protected $webformAccess;
protected $webformTemplate;
protected $webformSubmission;
protected $webformSubmissionAccess;
protected function setUp() {
parent::setUp();
$this
->setUpMockEntities();
$this->moduleHandler = $this
->createMock('Drupal\\Core\\Extension\\ModuleHandlerInterface');
$this->configFactory = $this
->getConfigFactoryStub([
'webform.settings' => [
'ui' => [
'toolbar_item' => FALSE,
],
],
]);
$this->requestHandler = $this
->createMock('Drupal\\webform\\WebformRequestInterface');
$this->translationManager = $this
->createMock('Drupal\\Core\\StringTranslation\\TranslationInterface');
$this->breadcrumbBuilder = $this
->getMockBuilder('Drupal\\webform\\Breadcrumb\\WebformBreadcrumbBuilder')
->setConstructorArgs([
$this->moduleHandler,
$this->requestHandler,
$this->translationManager,
$this->configFactory,
])
->setMethods(NULL)
->getMock();
$this->moduleHandler
->expects($this
->any())
->method('moduleExists')
->with('webform_templates')
->will($this
->returnValue(TRUE));
$translation_manager = $this
->getStringTranslationStub();
$property = new \ReflectionProperty('Drupal\\webform\\Breadcrumb\\WebformBreadcrumbBuilder', 'stringTranslation');
$property
->setAccessible(TRUE);
$property
->setValue($this->breadcrumbBuilder, $translation_manager);
$cache_contexts_manager = $this
->getMockBuilder('Drupal\\Core\\Cache\\Context\\CacheContextsManager')
->disableOriginalConstructor()
->getMock();
$cache_contexts_manager
->method('assertValidTokens')
->willReturn(TRUE);
$container = new Container();
$container
->set('cache_contexts_manager', $cache_contexts_manager);
\Drupal::setContainer($container);
}
public function testApplies($expected, $route_name = NULL, array $parameter_map = []) {
$route_match = $this
->getMockRouteMatch($route_name, $parameter_map);
$this
->assertEquals($expected, $this->breadcrumbBuilder
->applies($route_match));
}
public function providerTestApplies() {
$this
->setUpMockEntities();
$tests = [
[
FALSE,
],
[
FALSE,
'not',
],
[
FALSE,
'webform',
],
[
FALSE,
'entity.webform',
],
[
TRUE,
'entity.webform.handler.',
],
[
TRUE,
'entity.webform_ui.element',
],
[
TRUE,
'entity.webform.user.submissions',
],
[
TRUE,
'entity.{source_entity}.webform',
],
[
TRUE,
'entity.{source_entity}.webform_submission',
],
[
TRUE,
'entity.node.webform',
],
[
TRUE,
'entity.node.webform_submission',
],
[
TRUE,
'entity.node.webform.user.submissions',
],
[
FALSE,
'entity.webform.user.submission',
],
[
TRUE,
'entity.webform.user.submission',
[
[
'webform_submission',
$this->webformSubmissionAccess,
],
],
],
[
TRUE,
'webform',
[
[
'webform_submission',
$this->webformSubmissionAccess,
],
],
],
[
FALSE,
'entity.webform.config_translation_overview',
],
[
TRUE,
'entity.webform.config_translation_overview',
[
[
'webform',
$this->webformAccess,
],
],
],
];
return $tests;
}
public function testType($expected, $route_name = NULL, array $parameter_map = []) {
$route_match = $this
->getMockRouteMatch($route_name, $parameter_map);
$this->breadcrumbBuilder
->applies($route_match);
$this
->assertEquals($expected, $this->breadcrumbBuilder
->getType());
}
public function providerTestType() {
$this
->setUpMockEntities();
$tests = [
[
NULL,
],
[
'webform_source_entity',
'entity.{source_entity}.webform',
],
[
'webform_source_entity',
'entity.{source_entity}.webform_submission',
],
[
'webform_source_entity',
'entity.node.webform',
],
[
'webform_source_entity',
'entity.node.webform_submission',
],
[
'webform_element',
'entity.webform_ui.element',
],
[
'webform_handler',
'entity.webform.handler.',
],
[
'webform_user_submissions',
'entity.webform.user.submissions',
],
[
'webform_source_entity',
'entity.{source_entity}.webform.user.submissions',
],
[
'webform_source_entity',
'entity.node.webform.user.submissions',
],
[
'webform_user_submission',
'entity.webform.user.submission',
[
[
'webform_submission',
$this->webformSubmission,
],
],
],
[
NULL,
'entity.webform_submission.canonical',
[
[
'webform_submission',
$this->webformSubmission,
],
],
],
[
'webform_submission',
'entity.webform_submission.canonical',
[
[
'webform_submission',
$this->webformSubmissionAccess,
],
],
],
[
NULL,
'entity.webform.canonical',
[
[
'webform',
$this->webform,
],
],
],
[
'webform',
'entity.webform.canonical',
[
[
'webform',
$this->webformAccess,
],
],
],
[
'webform_template',
'entity.webform.canonical',
[
[
'webform',
$this->webformTemplate,
],
],
],
];
return $tests;
}
public function testBuildSourceEntity() {
$this
->setSourceEntity($this->nodeAccess);
$route_match = $this
->getMockRouteMatch('entity.node.webform', [
[
'webform',
$this->webformAccess,
],
[
'node',
$this->nodeAccess,
],
]);
$links = [
Link::createFromRoute('Home', '<front>'),
$this->node
->toLink(),
];
$this
->assertLinks($route_match, $links);
}
public function testBuildSourceEntitySubmissions() {
$this
->setSourceEntity($this->nodeAccess);
$route_match = $this
->getMockRouteMatch('entity.node.webform.user.submission', [
[
'webform_submission',
$this->webformSubmissionAccess,
],
[
'webform',
$this->webform,
],
[
'node',
$this->node,
],
]);
$links = [
Link::createFromRoute('Home', '<front>'),
$this->node
->toLink(),
Link::createFromRoute('Submissions', 'entity.node.webform.user.submissions', [
'node' => 1,
]),
];
$this
->assertLinks($route_match, $links);
}
public function testBuildSourceEntityResults() {
$this
->setSourceEntity($this->nodeAccess);
$route_match = $this
->getMockRouteMatch('entity.node.webform_submission.canonical', [
[
'webform_submission',
$this->webformSubmissionAccess,
],
[
'webform',
$this->webform,
],
[
'node',
$this->node,
],
]);
$links = [
Link::createFromRoute('Home', '<front>'),
$this->node
->toLink(),
Link::createFromRoute('Results', 'entity.node.webform.results_submissions', [
'node' => 1,
]),
];
$this
->assertLinks($route_match, $links);
}
public function testBuildSourceEntityUserResults() {
$this
->setSourceEntity($this->node);
$webform_submission_access = $this
->getMockBuilder('Drupal\\webform\\WebformSubmissionInterface')
->disableOriginalConstructor()
->getMock();
$webform_submission_access
->expects($this
->any())
->method('access')
->will($this
->returnCallback(function ($operation) {
return $operation === 'view_own';
}));
$route_match = $this
->getMockRouteMatch('entity.node.webform_submission.canonical', [
[
'webform_submission',
$webform_submission_access,
],
[
'webform',
$this->webform,
],
[
'node',
$this->node,
],
]);
$links = [
Link::createFromRoute('Home', '<front>'),
$this->node
->toLink(),
Link::createFromRoute('Results', 'entity.node.webform.user.submissions', [
'node' => 1,
]),
];
$this
->assertLinks($route_match, $links);
}
public function testBuildTemplates() {
$route_match = $this
->getMockRouteMatch('entity.webform.canonical', [
[
'webform',
$this->webformTemplate,
],
]);
$links = [
Link::createFromRoute('Home', '<front>'),
Link::createFromRoute('Administration', 'system.admin'),
Link::createFromRoute('Structure', 'system.admin_structure'),
Link::createFromRoute('Webforms', 'entity.webform.collection'),
Link::createFromRoute('Templates', 'entity.webform.templates'),
];
$this
->assertLinks($route_match, $links);
}
public function testBuildElements() {
$route_match = $this
->getMockRouteMatch('entity.webform_ui.element', [
[
'webform',
$this->webform,
],
]);
$links = [
Link::createFromRoute('Home', '<front>'),
Link::createFromRoute('Administration', 'system.admin'),
Link::createFromRoute('Structure', 'system.admin_structure'),
Link::createFromRoute('Webforms', 'entity.webform.collection'),
Link::createFromRoute($this->webform
->label(), 'entity.webform.canonical', [
'webform' => $this->webform
->id(),
]),
Link::createFromRoute('Elements', 'entity.webform.edit_form', [
'webform' => $this->webform
->id(),
]),
];
$this
->assertLinks($route_match, $links);
}
public function testBuildHandlers() {
$route_match = $this
->getMockRouteMatch('entity.webform.handler.add_form', [
[
'webform',
$this->webform,
],
]);
$links = [
Link::createFromRoute('Home', '<front>'),
Link::createFromRoute('Administration', 'system.admin'),
Link::createFromRoute('Structure', 'system.admin_structure'),
Link::createFromRoute('Webforms', 'entity.webform.collection'),
Link::createFromRoute($this->webform
->label(), 'entity.webform.canonical', [
'webform' => $this->webform
->id(),
]),
Link::createFromRoute('Emails / Handlers', 'entity.webform.handlers', [
'webform' => $this->webform
->id(),
]),
];
$this
->assertLinks($route_match, $links);
}
public function testBuildSubmissions() {
$route_match = $this
->getMockRouteMatch('entity.webform_submission.canonical', [
[
'webform_submission',
$this->webformSubmissionAccess,
],
]);
$links = [
Link::createFromRoute('Home', '<front>'),
Link::createFromRoute('Administration', 'system.admin'),
Link::createFromRoute('Structure', 'system.admin_structure'),
Link::createFromRoute('Webforms', 'entity.webform.collection'),
Link::createFromRoute($this->webform
->label(), 'entity.webform.canonical', [
'webform' => $this->webform
->id(),
]),
Link::createFromRoute('Results', 'entity.webform.results_submissions', [
'webform' => $this->webform
->id(),
]),
];
$this
->assertLinks($route_match, $links);
}
public function testBuildUserSubmissions() {
$route_match = $this
->getMockRouteMatch('entity.webform.user.submission', [
[
'webform_submission',
$this->webformSubmission,
],
]);
$links = [
Link::createFromRoute($this->webform
->label(), 'entity.webform.canonical', [
'webform' => $this->webform
->id(),
]),
];
$this
->assertLinks($route_match, $links);
$route_match = $this
->getMockRouteMatch('entity.webform.user.submission', [
[
'webform_submission',
$this->webformSubmissionAccess,
],
]);
$links = [
Link::createFromRoute($this->webform
->label(), 'entity.webform.canonical', [
'webform' => $this->webform
->id(),
]),
Link::createFromRoute('Submissions', 'entity.webform.user.submissions', [
'webform' => $this->webform
->id(),
]),
];
$this
->assertLinks($route_match, $links);
}
public function testBuildUserSubmission() {
$route_match = $this
->getMockRouteMatch('entity.webform.user.submissions', [
[
'webform',
$this->webform,
],
]);
$links = [
Link::createFromRoute($this->webform
->label(), 'entity.webform.canonical', [
'webform' => $this->webform
->id(),
]),
];
$this
->assertLinks($route_match, $links);
}
protected function assertLinks(RouteMatchInterface $route_match, array $links) {
$this->breadcrumbBuilder
->applies($route_match);
$breadcrumb = $this->breadcrumbBuilder
->build($route_match);
$this
->assertEquals($links, $breadcrumb
->getLinks());
}
protected function setSourceEntity(EntityInterface $entity) {
$this->requestHandler
->expects($this
->any())
->method('getCurrentSourceEntity')
->will($this
->returnValue($entity));
}
protected function getMockRouteMatch($route_name = NULL, array $parameter_map = []) {
$route_match = $this
->createMock('Drupal\\Core\\Routing\\RouteMatchInterface');
$route_match
->expects($this
->any())
->method('getRouteName')
->will($this
->returnValue($route_name));
$route_match
->expects($this
->any())
->method('getParameter')
->will($this
->returnValueMap($parameter_map));
return $route_match;
}
protected function setUpMockEntities() {
if (isset($this->node)) {
return;
}
$this->node = $this
->getMockBuilder('Drupal\\node\\NodeInterface')
->disableOriginalConstructor()
->getMock();
$this->node
->expects($this
->any())
->method('label')
->will($this
->returnValue('{node}'));
$this->node
->expects($this
->any())
->method('getEntityTypeId')
->will($this
->returnValue('node'));
$this->node
->expects($this
->any())
->method('id')
->will($this
->returnValue('1'));
$this->node
->expects($this
->any())
->method('toLink')
->will($this
->returnValue(Link::createFromRoute('{node}', 'entity.node.canonical', [
'node' => 1,
])));
$this->nodeAccess = clone $this->node;
$this->nodeAccess
->expects($this
->any())
->method('access')
->will($this
->returnValue(TRUE));
$this->webform = $this
->getMockBuilder('Drupal\\webform\\WebformInterface')
->disableOriginalConstructor()
->getMock();
$this->webform
->expects($this
->any())
->method('label')
->will($this
->returnValue('{webform}'));
$this->webform
->expects($this
->any())
->method('id')
->will($this
->returnValue(1));
$this->webformAccess = clone $this->webform;
$this->webformAccess
->expects($this
->any())
->method('access')
->will($this
->returnValue(TRUE));
$this->webformTemplate = clone $this->webformAccess;
$this->webformTemplate
->expects($this
->any())
->method('isTemplate')
->will($this
->returnValue(TRUE));
$this->webformSubmission = $this
->getMockBuilder('Drupal\\webform\\WebformSubmissionInterface')
->disableOriginalConstructor()
->getMock();
$this->webformSubmission
->expects($this
->any())
->method('getWebform')
->will($this
->returnValue($this->webform));
$this->webformSubmission
->expects($this
->any())
->method('label')
->will($this
->returnValue('{webform_submission}'));
$this->webformSubmission
->expects($this
->any())
->method('id')
->will($this
->returnValue(1));
$this->webformSubmissionAccess = clone $this->webformSubmission;
$this->webformSubmissionAccess
->expects($this
->any())
->method('access')
->will($this
->returnValue(TRUE));
}
}
if (!function_exists('base_path')) {
function base_path() {
return '/';
}
}