View source
<?php
namespace Drupal\Tests\jsonapi\Unit\EventSubscriber;
use Drupal\jsonapi\EventSubscriber\ResourceResponseValidator;
use Drupal\jsonapi\ResourceType\ResourceType;
use Drupal\jsonapi\Routing\Routes;
use JsonSchema\Validator;
use Drupal\Core\Extension\Extension;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\rest\ResourceResponse;
use Drupal\Tests\UnitTestCase;
use Prophecy\Argument;
use Psr\Log\LoggerInterface;
use Drupal\Core\Routing\RouteObjectInterface;
use Symfony\Component\HttpFoundation\Request;
class ResourceResponseValidatorTest extends UnitTestCase {
protected $subscriber;
public function setUp() : void {
parent::setUp();
if (!class_exists("\\JsonSchema\\Validator")) {
$this
->fail('The JSON Schema validator is missing. You can install it with `composer require justinrainbow/json-schema`.');
}
$module_handler = $this
->prophesize(ModuleHandlerInterface::class);
$module = $this
->prophesize(Extension::class);
$module_path = dirname(__DIR__, 4);
$module
->getPath()
->willReturn($module_path);
$module_handler
->getModule('jsonapi')
->willReturn($module
->reveal());
$subscriber = new ResourceResponseValidator($this
->prophesize(LoggerInterface::class)
->reveal(), $module_handler
->reveal(), '');
$subscriber
->setValidator();
$this->subscriber = $subscriber;
}
public function testDoValidateResponse() {
$request = $this
->createRequest('jsonapi.node--article.individual', new ResourceType('node', 'article', NULL));
$response = $this
->createResponse('{"data":null}');
$zend_assertions_default = ini_get('zend.assertions');
$assert_active_default = assert_options(ASSERT_ACTIVE);
$validator = $this
->prophesize(Validator::class);
$validator
->check(Argument::any(), Argument::any())
->shouldBeCalled('Validation should be run when asserts are active.');
$validator
->isValid()
->willReturn(TRUE);
$this->subscriber
->setValidator($validator
->reveal());
ini_set('zend.assertions', 1);
assert_options(ASSERT_ACTIVE, 1);
$this->subscriber
->doValidateResponse($response, $request);
$validator = $this
->prophesize(Validator::class);
$validator
->check(Argument::any(), Argument::any())
->shouldNotBeCalled('Validation should not be run when asserts are not active.');
$this->subscriber
->setValidator($validator
->reveal());
ini_set('zend.assertions', 0);
assert_options(ASSERT_ACTIVE, 0);
$this->subscriber
->doValidateResponse($response, $request);
ini_set('zend.assertions', $zend_assertions_default);
assert_options(ASSERT_ACTIVE, $assert_active_default);
}
public function testValidateResponse($request, $response, $expected, $description) {
$object = new \ReflectionObject($this->subscriber);
$method = $object
->getMethod('validateResponse');
$method
->setAccessible(TRUE);
$this
->assertSame($expected, $method
->invoke($this->subscriber, $response, $request), $description);
}
public function validateResponseProvider() {
$defaults = [
'route_name' => 'jsonapi.node--article.individual',
'resource_type' => new ResourceType('node', 'article', NULL),
];
$test_data = [
[
'json' => <<<'EOD'
{
"data": {
"type": "node--article",
"id": "4f342419-e668-4b76-9f87-7ce20c436169",
"attributes": {
"nid": "1",
"uuid": "4f342419-e668-4b76-9f87-7ce20c436169"
}
}
}
EOD
,
'expected' => TRUE,
'description' => 'Response validation flagged a valid response.',
],
[
'json' => <<<'EOD'
{
"data": {
"id": "4f342419-e668-4b76-9f87-7ce20c436169",
"attributes": {
"nid": "1",
"uuid": "4f342419-e668-4b76-9f87-7ce20c436169"
}
}
}
EOD
,
'expected' => FALSE,
'description' => 'Response validation failed to flag an invalid response.',
],
[
'json' => <<<'EOD'
{
"data": {
"type": "node--article",
"id": "4f342419-e668-4b76-9f87-7ce20c436169",
"attributes": {
"nid": "1",
"uuid": "4f342419-e668-4b76-9f87-7ce20c436169"
}
},
"errors": [{}]
}
EOD
,
'expected' => FALSE,
'description' => 'Response validation failed to flag an invalid response.',
],
[
'json' => NULL,
'expected' => TRUE,
'description' => 'Response validation flagged a valid empty response.',
],
[
'json' => '{}',
'expected' => FALSE,
'description' => 'Response validation flags empty array as invalid.',
],
];
$test_cases = array_map(function ($input) use ($defaults) {
list($json, $expected, $description, $route_name, $resource_type) = array_values($input + $defaults);
return [
$this
->createRequest($route_name, $resource_type),
$this
->createResponse($json),
$expected,
$description,
];
}, $test_data);
return $test_cases;
}
protected function createRequest($route_name, ResourceType $resource_type) {
$request = new Request();
$request->attributes
->set(RouteObjectInterface::ROUTE_NAME, $route_name);
$request->attributes
->set(Routes::RESOURCE_TYPE_KEY, $resource_type);
return $request;
}
protected function createResponse($json = NULL) {
$response = new ResourceResponse();
if ($json) {
$response
->setContent($json);
}
return $response;
}
}