You are here

public function DuplicateContentPreventionMiddlewareTest::testDuplicateContentPrevention in CDN 8.3

@covers ::handle @covers ::getRedirectUrl @dataProvider duplicateContentPreventionProvider

File

tests/src/Unit/StackMiddleware/DuplicateContentPreventionMiddlewareTest.php, line 29

Class

DuplicateContentPreventionMiddlewareTest
@coversDefaultClass \Drupal\cdn\StackMiddleware\DuplicateContentPreventionMiddleware @group StackMiddleware @group cdn

Namespace

Drupal\Tests\cdn\Unit\StackMiddleware

Code

public function testDuplicateContentPrevention($path, $user_agent, array $other_request_headers, $expected_redirect) {

  // The incoming request for the given path from the given user agent.
  $request_prophecy = $this
    ->prophesize(Request::class);
  $request_prophecy
    ->getPathInfo()
    ->willReturn($path);
  $request = $request_prophecy
    ->reveal();
  $request->headers = new HeaderBag([
    'User-Agent' => $user_agent,
  ] + $other_request_headers);

  // Simulate the logic of the unrouted URL assembler.
  $container = new ContainerBuilder();
  $url_assembler_prophecy = $this
    ->prophesize(UnroutedUrlAssemblerInterface::class);
  $url_assembler_prophecy
    ->assemble(Argument::type('string'), [
    'absolute' => TRUE,
  ], FALSE)
    ->will(function ($args) {
    return str_replace('base:', 'http://🐷.com', $args[0]);
  });
  $container
    ->set('unrouted_url_assembler', $url_assembler_prophecy
    ->reveal());
  \Drupal::setContainer($container);

  // Mock the kernel to decorate.
  $kernel = $this
    ->prophesize(HttpKernelInterface::class);
  $kernel
    ->handle($request, HttpKernelInterface::MASTER_REQUEST, TRUE)
    ->willReturn(new Response());
  $middleware = new DuplicateContentPreventionMiddleware($kernel
    ->reveal(), new RequestStack());
  $response = $middleware
    ->handle($request);
  if ($expected_redirect !== FALSE) {
    $this
      ->assertInstanceOf(RedirectResponse::class, $response);
    $this
      ->assertSame(301, $response
      ->getStatusCode());
    $this
      ->assertSame('<' . $expected_redirect . '>; rel="canonical"', $response->headers
      ->get('Link'));
  }
  else {
    $this
      ->assertNotInstanceOf(RedirectResponse::class, $response);
  }
}