You are here

class DuplicateContentPreventionMiddlewareTest in CDN 8.3

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

Hierarchy

Expanded class hierarchy of DuplicateContentPreventionMiddlewareTest

File

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

Namespace

Drupal\Tests\cdn\Unit\StackMiddleware
View source
class DuplicateContentPreventionMiddlewareTest extends UnitTestCase {

  /**
   * @covers ::handle
   * @covers ::getRedirectUrl
   * @dataProvider duplicateContentPreventionProvider
   */
  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);
    }
  }
  public function duplicateContentPreventionProvider() {

    // @codingStandardsIgnoreStart
    return [
      // HTML requested: the response is a redirect when requested by a CDN.
      [
        '/',
        'Mozilla',
        [],
        FALSE,
      ],
      [
        '/node/1',
        'Mozilla',
        [],
        FALSE,
      ],
      [
        '/node/1.html',
        'Mozilla',
        [],
        FALSE,
      ],
      [
        '/node/1.htm',
        'Mozilla',
        [],
        FALSE,
      ],
      [
        '/node/1.php',
        'Mozilla',
        [],
        FALSE,
      ],
      [
        '/',
        'Amazon CloudFront',
        [],
        'http://๐Ÿท.com/',
      ],
      [
        '/node/1',
        'Amazon CloudFront',
        [],
        'http://๐Ÿท.com/node/1',
      ],
      [
        '/node/1.html',
        'Amazon CloudFront',
        [],
        'http://๐Ÿท.com/node/1.html',
      ],
      [
        '/node/1.htm',
        'Amazon CloudFront',
        [],
        'http://๐Ÿท.com/node/1.htm',
      ],
      [
        '/node/1.php',
        'Amazon CloudFront',
        [],
        'http://๐Ÿท.com/node/1.php',
      ],
      [
        '/',
        'Mozilla',
        [
          'CF-RAY' => $this
            ->randomMachineName(),
        ],
        'http://๐Ÿท.com/',
      ],
      [
        '/node/1',
        'Mozilla',
        [
          'CF-RAY' => $this
            ->randomMachineName(),
        ],
        'http://๐Ÿท.com/node/1',
      ],
      [
        '/node/1.html',
        'Mozilla',
        [
          'CF-RAY' => $this
            ->randomMachineName(),
        ],
        'http://๐Ÿท.com/node/1.html',
      ],
      [
        '/node/1.htm',
        'Mozilla',
        [
          'CF-RAY' => $this
            ->randomMachineName(),
        ],
        'http://๐Ÿท.com/node/1.htm',
      ],
      [
        '/node/1.php',
        'Mozilla',
        [
          'CF-RAY' => $this
            ->randomMachineName(),
        ],
        'http://๐Ÿท.com/node/1.php',
      ],
      // File requested: the response is never a redirect.
      [
        '/misc/jquery.js',
        'Mozilla',
        [],
        FALSE,
      ],
      [
        '/misc/jquery.js',
        'Amazon CloudFront',
        [],
        FALSE,
      ],
      // Generated file requested: the response is never a redirect.
      [
        '/sites/default/files/styles/thumbnail/foobar.png',
        'Mozilla',
        [],
        FALSE,
      ],
      [
        '/sites/default/files/styles/thumbnail/foobar.png',
        'Amazon CloudFront',
        [],
        FALSE,
      ],
    ];

    // @codingStandardsIgnoreEnd
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DuplicateContentPreventionMiddlewareTest::duplicateContentPreventionProvider public function
DuplicateContentPreventionMiddlewareTest::testDuplicateContentPrevention public function @covers ::handle @covers ::getRedirectUrl @dataProvider duplicateContentPreventionProvider
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