public function DomainRedirectRequestSubscriberTest::testDomainRedirect in Redirect 8
Tests redirect between domains.
@dataProvider providerDomains
File
- modules/
redirect_domain/ tests/ src/ Unit/ DomainRedirectRequestSubscriberTest.php, line 30
Class
- DomainRedirectRequestSubscriberTest
- Tests the redirect logic.
Namespace
Drupal\Tests\redirect_domain\UnitCode
public function testDomainRedirect($request_url, $response_url) {
$data = [
'redirect_domain.domains' => [
'domain_redirects' => [
'foo:com' => [
[
'sub_path' => '/fixedredirect',
'destination' => 'bar.com/fixedredirect',
],
[
'sub_path' => '/*',
'destination' => 'bar.com/example',
],
],
'example:com' => [
[
'sub_path' => '/foo/*/bar',
'destination' => 'example.com/bar/foo',
],
],
'simpleexample:com' => [
[
'sub_path' => '/redirect',
'destination' => 'redirected.com/redirect',
],
],
'wildcardtest:com' => [
[
'sub_path' => '/some/path',
'destination' => 'somedomain.com/path',
],
[
'sub_path' => '/*',
'destination' => 'wildcardredirect.com',
],
[
'sub_path' => '/other/path',
'destination' => 'otherdomain.com/path',
],
],
],
],
'redirect.settings' => [
'default_status_code' => 301,
],
'system.site' => [
'page.front' => '/',
],
];
// Create a mock redirect checker.
$checker = $this
->getMockBuilder(RedirectChecker::class)
->disableOriginalConstructor()
->getMock();
$checker
->expects($this
->any())
->method('canRedirect')
->will($this
->returnValue(TRUE));
// Set up the configuration for the requested domain.
$config_factory = $this
->getConfigFactoryStub($data);
// Create a mock path matcher.
$route_match = $this
->createMock(RouteMatchInterface::class);
$path_matcher = new PathMatcher($config_factory, $route_match);
$subscriber = new DomainRedirectRequestSubscriber($config_factory, $checker, $path_matcher);
// Make a request to the urls from the data provider and get the response.
$event = $this
->getGetResponseEventStub($request_url, http_build_query([]));
// Run the main redirect method.
$subscriber
->onKernelRequestCheckDomainRedirect($event);
// Assert the expected response from the data provider.
if ($response_url) {
$this
->assertTrue($event
->getResponse() instanceof RedirectResponse);
$response = $event
->getResponse();
// Make sure that the response is properly redirected.
$this
->assertEquals($response_url, $response
->getTargetUrl());
$this
->assertEquals($config_factory
->get('redirect.settings')
->get('default_status_code'), $response
->getStatusCode());
}
else {
$this
->assertNull($event
->getResponse());
}
}