/** * {@inheritdoc} */ protected function setUp() { $this->configFactory = $this->getConfigFactoryStub(['system.site' => ['page.403' => '/access-denied-page', 'page.404' => '/not-found-page']]); $this->kernel = $this->getMock('Symfony\\Component\\HttpKernel\\HttpKernelInterface'); $this->logger = $this->getMock('Psr\\Log\\LoggerInterface'); $this->redirectDestination = $this->getMock('\\Drupal\\Core\\Routing\\RedirectDestinationInterface'); $this->redirectDestination->expects($this->any())->method('getAsArray')->willReturn(['destination' => 'test']); $this->accessUnawareRouter = $this->getMock('Symfony\\Component\\Routing\\Matcher\\UrlMatcherInterface'); $this->accessUnawareRouter->expects($this->any())->method('match')->willReturn(['_controller' => 'mocked']); $this->customPageSubscriber = new CustomPageExceptionHtmlSubscriber($this->configFactory, $this->kernel, $this->logger, $this->redirectDestination, $this->accessUnawareRouter); // You can't create an exception in PHP without throwing it. Store the // current error_log, and disable it temporarily. $this->errorLog = ini_set('error_log', file_exists('/dev/null') ? '/dev/null' : 'nul'); }
/** * Tests the isValid() method with a not existing path. * * @covers ::isValid */ public function testIsValidWithNotExistingPath() { $this->account->expects($this->once())->method('hasPermission')->with('link to any page')->willReturn(FALSE); $this->accessUnawareRouter->expects($this->never())->method('match'); $this->accessAwareRouter->expects($this->once())->method('match')->with('/not-existing-path')->willThrowException(new ResourceNotFoundException()); $this->pathProcessor->expects($this->once())->method('processInbound')->willReturnArgument(0); $this->assertFalse($this->pathValidator->isValid('not-existing-path')); }
/** * Tests the getUrlIfValidWithoutAccessCheck() method. * * @covers ::getUrlIfValidWithoutAccessCheck */ public function testGetUrlIfValidWithoutAccessCheck() { $this->account->expects($this->never())->method('hasPermission')->with('link to any page'); $this->accessAwareRouter->expects($this->never())->method('match'); $this->accessUnawareRouter->expects($this->once())->method('match')->with('/test-path')->willReturn([RouteObjectInterface::ROUTE_NAME => 'test_route', '_raw_variables' => new ParameterBag(['key' => 'value'])]); $this->pathProcessor->expects($this->once())->method('processInbound')->willReturnArgument(0); $url = $this->pathValidator->getUrlIfValidWithoutAccessCheck('test-path'); $this->assertInstanceOf('Drupal\\Core\\Url', $url); $this->assertEquals('test_route', $url->getRouteName()); $this->assertEquals(['key' => 'value'], $url->getRouteParameters()); }
/** * @param string $method * @dataProvider getLinkMethods */ public function testProducedLinks($method) { $listener = $this->getListener(); $event = $this->getEvent(); $urlParams = ['foo' => 'bar']; $this->urlMatcher->expects($this->exactly(3))->method('match')->willReturn($urlParams); $event->getRequest()->setMethod($method); $event->getRequest()->headers->set('Link', 'link1,link2, link3'); $this->context->method('getMethod')->willReturn('METHOD'); $this->context->expects($this->exactly(2))->method('setMethod')->withConsecutive(['GET'], ['METHOD']); $listener->onKernelRequest($event); $this->assertTrue($event->getRequest()->attributes->has('links')); /** @var LinkHeader[] $links */ $links = $event->getRequest()->attributes->get('links'); $this->assertEquals(3, count($links)); $this->assertEquals('link1', $links[0]->getOriginalHeader()); $this->assertEquals($urlParams, $links[0]->getUrlParameters()); $this->assertEquals('link2', $links[1]->getOriginalHeader()); $this->assertEquals('link3', $links[2]->getOriginalHeader()); }
/** * {@inheritdoc} */ protected function setUp() { $this->configFactory = $this->getConfigFactoryStub(['system.site' => ['page.403' => '/access-denied-page', 'page.404' => '/not-found-page']]); $this->kernel = $this->getMock('Symfony\\Component\\HttpKernel\\HttpKernelInterface'); $this->logger = $this->getMock('Psr\\Log\\LoggerInterface'); $this->redirectDestination = $this->getMock('\\Drupal\\Core\\Routing\\RedirectDestinationInterface'); $this->redirectDestination->expects($this->any())->method('getAsArray')->willReturn(['destination' => 'test']); $this->accessUnawareRouter = $this->getMock('Symfony\\Component\\Routing\\Matcher\\UrlMatcherInterface'); $this->accessUnawareRouter->expects($this->any())->method('match')->willReturn(['_controller' => 'mocked']); $this->accessManager = $this->getMock('Drupal\\Core\\Access\\AccessManagerInterface'); $this->accessManager->expects($this->any())->method('checkNamedRoute')->willReturn(AccessResult::allowed()->addCacheTags(['foo', 'bar'])); $this->customPageSubscriber = new CustomPageExceptionHtmlSubscriber($this->configFactory, $this->kernel, $this->logger, $this->redirectDestination, $this->accessUnawareRouter, $this->accessManager); $path_validator = $this->getMock('Drupal\\Core\\Path\\PathValidatorInterface'); $path_validator->expects($this->any())->method('getUrlIfValidWithoutAccessCheck')->willReturn(Url::fromRoute('foo', ['foo' => 'bar'])); $container = new ContainerBuilder(); $container->set('path.validator', $path_validator); \Drupal::setContainer($container); // You can't create an exception in PHP without throwing it. Store the // current error_log, and disable it temporarily. $this->errorLog = ini_set('error_log', file_exists('/dev/null') ? '/dev/null' : 'nul'); }
/** * Tests onHandleException with a GET request. */ public function testHandleWithGetRequest() { $request = Request::create('/test', 'GET', array('name' => 'druplicon', 'pass' => '12345')); $request->attributes->set(AccessAwareRouterInterface::ACCESS_RESULT, AccessResult::forbidden()->addCacheTags(['druplicon'])); $request_context = new RequestContext(); $request_context->fromRequest($request); $this->accessUnawareRouter->expects($this->any())->method('getContext')->willReturn($request_context); $this->kernel->expects($this->once())->method('handle')->will($this->returnCallback(function (Request $request) { return new Response($request->getMethod() . ' ' . UrlHelper::buildQuery($request->query->all())); })); $event = new GetResponseForExceptionEvent($this->kernel, $request, 'foo', new NotFoundHttpException('foo')); $this->customPageSubscriber->onException($event); $response = $event->getResponse(); $result = $response->getContent() . " " . UrlHelper::buildQuery($request->request->all()); $this->assertEquals('GET name=druplicon&pass=12345&destination=test&_exception_statuscode=404 ', $result); $this->assertEquals(AccessResult::forbidden()->addCacheTags(['druplicon', 'foo', 'bar']), $request->attributes->get(AccessAwareRouterInterface::ACCESS_RESULT)); }