You are here

public function ContentHubInternalRequestTest::testGetEntityCdfByInternalRequest in Acquia Content Hub 8

Test the getEntityCdfByInternalRequest method.

This test only verifies that we are passing cookies and server variables to the child subrequest created by this service.

@covers ::getEntityCdfByInternalRequest

File

tests/src/Unit/ContentHubInternalRequestTest.php, line 131

Class

ContentHubInternalRequestTest
PHPUnit for the ContentHubInternalRequest class.

Namespace

Drupal\Tests\acquia_contenthub\Unit

Code

public function testGetEntityCdfByInternalRequest() {

  // Setting up services.
  $GLOBALS['base_path'] = '/';
  $url_generator = $this
    ->createMock('Drupal\\Core\\Routing\\UrlGeneratorInterface');
  $url_generator
    ->method('generateFromRoute')
    ->willReturnCallback(function ($name, $parameters, $options, $bubbleable_metadata) {
    if ($name == 'acquia_contenthub.entity.node.GET.acquia_contenthub_cdf') {
      return '/acquia-contenthub-cdf/' . $parameters['entity_type'] . '/' . $parameters['entity_id'] . '?_format=' . $parameters['_format'];
    }
    return NULL;
  });
  $this->container
    ->expects($this
    ->once())
    ->method('get')
    ->with('url_generator')
    ->willReturn($url_generator);
  $this->clientManager
    ->expects($this
    ->once())
    ->method('isConnected')
    ->willReturn(TRUE);
  $this->clientManager
    ->expects($this
    ->once())
    ->method('getRequestSignature')
    ->withAnyParameters()
    ->willReturn('testSignature');
  $session = new Session(new MockArraySessionStorage());
  $request = new Request();
  $request
    ->setSession($session);

  // Set cookies and server variables to the main request that are expected
  // to be carried over to internal subrequests.
  $request->cookies
    ->set('test', 'cookie_test_value');
  $request->server
    ->set('test', 'server_test_value');

  // Assign request to request stack.
  $this->kernel
    ->method('handle')
    ->willReturnCallback(function (Request $request, $type) {
    $output = [
      'type' => $type,
      'uri' => $request
        ->getRequestUri(),
      'headers' => $request->headers
        ->all(),
      'cookies' => $request->cookies
        ->all(),
      'server' => $request->server
        ->all(),
    ];
    $response = new JsonResponse($output);
    return $response;
  });
  $this->requestStack
    ->expects($this
    ->once())
    ->method('getCurrentRequest')
    ->willReturn($request);

  // Executing the test.
  $entity_type = 'node';
  $entity_id = 1;
  $output = $this->contentHubInternalRequest
    ->getEntityCdfByInternalRequest($entity_type, $entity_id, FALSE);
  unset($GLOBALS['base_path']);

  // Verify that we obtained the cookies and server variables passed to the
  // subrequest.
  $this
    ->assertEquals(HttpKernelInterface::SUB_REQUEST, $output['type']);
  $this
    ->assertTrue(isset($output['headers']['authorization']));
  $this
    ->assertEquals('Acquia ContentHub:testSignature', $output['headers']['authorization'][0]);
  $this
    ->assertTrue(isset($output['cookies']['test']));
  $this
    ->assertEquals('cookie_test_value', $output['cookies']['test']);
  $this
    ->assertTrue(isset($output['server']['test']));
  $this
    ->assertEquals('server_test_value', $output['server']['test']);
}