You are here

class ContentHubInternalRequestTest in Acquia Content Hub 8

PHPUnit for the ContentHubInternalRequest class.

@coversDefaultClass \Drupal\acquia_contenthub\ContentHubInternalRequest

@group acquia_contenthub

Hierarchy

Expanded class hierarchy of ContentHubInternalRequestTest

File

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

Namespace

Drupal\Tests\acquia_contenthub\Unit
View source
class ContentHubInternalRequestTest extends UnitTestCase {

  /**
   * The dependency injection container.
   *
   * @var \Symfony\Component\DependencyInjection\ContainerBuilder|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $container;

  /**
   * Content Hub Client Manager.
   *
   * @var \Drupal\acquia_contenthub\Client\ClientManager|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $clientManager;

  /**
   * Logger.
   *
   * @var \Drupal\Core\Logger\LoggerChannelFactoryInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $loggerFactory;

  /**
   * The Basic HTTP Kernel to make requests.
   *
   * @var \Symfony\Component\HttpKernel\HttpKernelInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $kernel;

  /**
   * Content Hub Subscription.
   *
   * @var \Drupal\acquia_contenthub\ContentHubSubscription|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $contentHubSubscription;

  /**
   * The account switcher service.
   *
   * @var \Drupal\Core\Session\AccountSwitcherInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $accountSwitcher;

  /**
   * The Request Stack Service.
   *
   * @var \Symfony\Component\HttpFoundation\RequestStack|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $requestStack;

  /**
   * The Content Hub Internal Request.
   *
   * @var \Drupal\acquia_contenthub\ContentHubInternalRequest
   */
  protected $contentHubInternalRequest;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();

    // Setting up Drupal container.
    $this->container = $this
      ->createMock('Drupal\\Core\\DependencyInjection\\Container');
    \Drupal::setContainer($this->container);
    $prophecy = $this
      ->prophesize(Config::class);
    $prophecy
      ->get('user_role')
      ->willReturn(AccountInterface::ANONYMOUS_ROLE);
    $config = $prophecy
      ->reveal();
    $config_factory = $this
      ->prophesize(ConfigFactoryInterface::class);
    $config_factory
      ->get('acquia_contenthub.entity_config')
      ->willReturn($config);
    $config_factory
      ->getEditable('acquia_contenthub.admin_settings')
      ->willReturn($config);
    $this->loggerFactory = $this
      ->prophesize(LoggerChannelFactoryInterface::class)
      ->reveal();
    $this->kernel = $this
      ->createMock('Symfony\\Component\\HttpKernel\\HttpKernelInterface');
    $this->accountSwitcher = $this
      ->prophesize(AccountSwitcherInterface::class)
      ->reveal();
    $this->requestStack = $this
      ->getMockBuilder('Symfony\\Component\\HttpFoundation\\RequestStack')
      ->disableOriginalConstructor()
      ->getMock();
    $this->clientManager = $this
      ->getMockBuilder('Drupal\\acquia_contenthub\\Client\\ClientManager')
      ->disableOriginalConstructor()
      ->getMock();
    $state = $this
      ->prophesize(StateInterface::class);
    $state
      ->get('acquia_contenthub.shared_secret')
      ->willReturn('shared_secret');
    $this->contentHubSubscription = new ContentHubSubscription($this->loggerFactory, $config_factory
      ->reveal(), $this->clientManager, $state
      ->reveal());

    // Initializing service.
    $this->contentHubInternalRequest = new ContentHubInternalRequest($this->kernel, $this->contentHubSubscription, $this->accountSwitcher, $config_factory
      ->reveal(), $this->loggerFactory, $this->requestStack);
  }

  /**
   * 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
   */
  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']);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ContentHubInternalRequestTest::$accountSwitcher protected property The account switcher service.
ContentHubInternalRequestTest::$clientManager protected property Content Hub Client Manager.
ContentHubInternalRequestTest::$container protected property The dependency injection container.
ContentHubInternalRequestTest::$contentHubInternalRequest protected property The Content Hub Internal Request.
ContentHubInternalRequestTest::$contentHubSubscription protected property Content Hub Subscription.
ContentHubInternalRequestTest::$kernel protected property The Basic HTTP Kernel to make requests.
ContentHubInternalRequestTest::$loggerFactory protected property Logger.
ContentHubInternalRequestTest::$requestStack protected property The Request Stack Service.
ContentHubInternalRequestTest::setUp protected function Overrides UnitTestCase::setUp
ContentHubInternalRequestTest::testGetEntityCdfByInternalRequest public function Test the getEntityCdfByInternalRequest method.
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.