You are here

class DomainRedirectRequestSubscriberTest in Redirect 8

Tests the redirect logic.

@group redirect_domain

@coversDefaultClass Drupal\redirect_domain\EventSubscriber\DomainRedirectRequestSubscriber

Hierarchy

Expanded class hierarchy of DomainRedirectRequestSubscriberTest

File

modules/redirect_domain/tests/src/Unit/DomainRedirectRequestSubscriberTest.php, line 23

Namespace

Drupal\Tests\redirect_domain\Unit
View source
class DomainRedirectRequestSubscriberTest extends UnitTestCase {

  /**
   * Tests redirect between domains.
   *
   * @dataProvider providerDomains
   */
  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());
    }
  }

  /**
   * Gets response event object.
   *
   * @param $path_info
   *   The path info.
   * @param $query_string
   *   The query string in the url.
   *
   * @return GetResponseEvent
   *   The response for the request.
   */
  protected function getGetResponseEventStub($path_info, $query_string) {
    $request = Request::create($path_info . '?' . $query_string, 'GET', [], [], [], [
      'SCRIPT_NAME' => 'index.php',
    ]);
    $http_kernel = $this
      ->getMockBuilder(HttpKernelInterface::class)
      ->getMock();
    return new GetResponseEvent($http_kernel, $request, HttpKernelInterface::MASTER_REQUEST);
  }

  /**
   * Data provider for the domain redirects.
   *
   * @return array
   *   An array of requests and expected responses for the redirect domains.
   */
  public function providerDomains() {
    $datasets = [];
    $datasets[] = [
      'http://foo.com/example',
      'http://bar.com/example',
    ];
    $datasets[] = [
      'http://example.com/foo/test/bar',
      'http://example.com/bar/foo',
    ];
    $datasets[] = [
      'http://simpleexample.com/redirect',
      'http://redirected.com/redirect',
    ];
    $datasets[] = [
      'http://nonexisting.com',
      NULL,
    ];
    $datasets[] = [
      'http://simpleexample.com/wrongpath',
      NULL,
    ];
    $datasets[] = [
      'http://foo.com/fixedredirect',
      'http://bar.com/fixedredirect',
    ];
    $datasets[] = [
      'http://wildcardtest.com/some/path',
      'http://somedomain.com/path',
    ];
    $datasets[] = [
      'http://wildcardtest.com/other/path',
      'http://wildcardredirect.com',
    ];
    $datasets[] = [
      'http://wildcardtest.com/does-not-exist',
      'http://wildcardredirect.com',
    ];
    return $datasets;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DomainRedirectRequestSubscriberTest::getGetResponseEventStub protected function Gets response event object.
DomainRedirectRequestSubscriberTest::providerDomains public function Data provider for the domain redirects.
DomainRedirectRequestSubscriberTest::testDomainRedirect public function Tests redirect between domains.
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