class ContentHubInternalRequestTest in Acquia Content Hub 8
PHPUnit for the ContentHubInternalRequest class.
@coversDefaultClass \Drupal\acquia_contenthub\ContentHubInternalRequest
@group acquia_contenthub
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\acquia_contenthub\Unit\ContentHubInternalRequestTest
Expanded class hierarchy of ContentHubInternalRequestTest
File
- tests/
src/ Unit/ ContentHubInternalRequestTest.php, line 27
Namespace
Drupal\Tests\acquia_contenthub\UnitView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ContentHubInternalRequestTest:: |
protected | property | The account switcher service. | |
ContentHubInternalRequestTest:: |
protected | property | Content Hub Client Manager. | |
ContentHubInternalRequestTest:: |
protected | property | The dependency injection container. | |
ContentHubInternalRequestTest:: |
protected | property | The Content Hub Internal Request. | |
ContentHubInternalRequestTest:: |
protected | property | Content Hub Subscription. | |
ContentHubInternalRequestTest:: |
protected | property | The Basic HTTP Kernel to make requests. | |
ContentHubInternalRequestTest:: |
protected | property | Logger. | |
ContentHubInternalRequestTest:: |
protected | property | The Request Stack Service. | |
ContentHubInternalRequestTest:: |
protected | function |
Overrides UnitTestCase:: |
|
ContentHubInternalRequestTest:: |
public | function | Test the getEntityCdfByInternalRequest method. | |
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. |