ContentHubInternalRequestTest.php in Acquia Content Hub 8
File
tests/src/Unit/ContentHubInternalRequestTest.php
View source
<?php
namespace Drupal\Tests\acquia_contenthub\Unit;
use Drupal\acquia_contenthub\ContentHubInternalRequest;
use Drupal\acquia_contenthub\ContentHubSubscription;
use Drupal\Core\Config\Config;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Session\AccountSwitcherInterface;
use Drupal\Core\State\StateInterface;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
use Symfony\Component\HttpKernel\HttpKernelInterface;
class ContentHubInternalRequestTest extends UnitTestCase {
protected $container;
protected $clientManager;
protected $loggerFactory;
protected $kernel;
protected $contentHubSubscription;
protected $accountSwitcher;
protected $requestStack;
protected $contentHubInternalRequest;
protected function setUp() {
parent::setUp();
$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());
$this->contentHubInternalRequest = new ContentHubInternalRequest($this->kernel, $this->contentHubSubscription, $this->accountSwitcher, $config_factory
->reveal(), $this->loggerFactory, $this->requestStack);
}
public function testGetEntityCdfByInternalRequest() {
$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);
$request->cookies
->set('test', 'cookie_test_value');
$request->server
->set('test', 'server_test_value');
$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);
$entity_type = 'node';
$entity_id = 1;
$output = $this->contentHubInternalRequest
->getEntityCdfByInternalRequest($entity_type, $entity_id, FALSE);
unset($GLOBALS['base_path']);
$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']);
}
}