Skip to content
Snippets Groups Projects
Commit 669ed1aa authored by Rico Van de Vin's avatar Rico Van de Vin Committed by Elliot Ward
Browse files

Issue #3360180 by ricovandevin, Eli-T, Gold: Drupal 10 compatibility

parent 3a461787
No related branches found
No related tags found
1 merge request!5Resolve #3360180 "Drupal 10 compatibility"
Pipeline #13978 failed
...@@ -3,6 +3,6 @@ type: module ...@@ -3,6 +3,6 @@ type: module
description: Change the maximum number of items in a response for a given route. description: Change the maximum number of items in a response for a given route.
package: Web services package: Web services
core: 8.x core: 8.x
core_version_requirement: ^8 || ^9 core_version_requirement: ^8 || ^9 || ^10
dependencies: dependencies:
- drupal:jsonapi - drupal:jsonapi
...@@ -42,7 +42,7 @@ class EntityResource extends \Drupal\jsonapi\Controller\EntityResource { ...@@ -42,7 +42,7 @@ class EntityResource extends \Drupal\jsonapi\Controller\EntityResource {
*/ */
private $pathMatcher; private $pathMatcher;
public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $field_manager, ResourceTypeRepositoryInterface $resource_type_repository, RendererInterface $renderer, EntityRepositoryInterface $entity_repository, IncludeResolver $include_resolver, EntityAccessChecker $entity_access_checker, FieldResolver $field_resolver, SerializerInterface $serializer, TimeInterface $time, AccountInterface $user, RequestContext $request_context, PathMatcherInterface $path_matcher, array $size_max) { public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $field_manager, ResourceTypeRepositoryInterface $resource_type_repository, RendererInterface $renderer, EntityRepositoryInterface $entity_repository, IncludeResolver $include_resolver, EntityAccessChecker $entity_access_checker, FieldResolver $field_resolver, SerializerInterface $serializer, TimeInterface $time, AccountInterface $user, RequestContext $request_context, PathMatcherInterface $path_matcher, ?array $size_max) {
parent::__construct($entity_type_manager, $field_manager, $resource_type_repository, $renderer, $entity_repository, $include_resolver, $entity_access_checker, $field_resolver, $serializer, $time, $user); parent::__construct($entity_type_manager, $field_manager, $resource_type_repository, $renderer, $entity_repository, $include_resolver, $entity_access_checker, $field_resolver, $serializer, $time, $user);
$this->sizeMax = $size_max; $this->sizeMax = $size_max;
$this->requestContext = $request_context; $this->requestContext = $request_context;
...@@ -54,7 +54,7 @@ class EntityResource extends \Drupal\jsonapi\Controller\EntityResource { ...@@ -54,7 +54,7 @@ class EntityResource extends \Drupal\jsonapi\Controller\EntityResource {
*/ */
protected function getJsonApiParams(Request $request, ResourceType $resource_type) { protected function getJsonApiParams(Request $request, ResourceType $resource_type) {
$params = parent::getJsonApiParams($request, $resource_type); $params = parent::getJsonApiParams($request, $resource_type);
$page_params = $request->query->get(OffsetPage::KEY_NAME); $page_params = $request->query->all(OffsetPage::KEY_NAME);
// Only handle requests where a ?page[limit] has been specified. // Only handle requests where a ?page[limit] has been specified.
if (is_array($page_params) && isset($page_params[OffsetPage::SIZE_KEY])) { if (is_array($page_params) && isset($page_params[OffsetPage::SIZE_KEY])) {
......
<?php
declare(strict_types = 1);
namespace Drupal\Tests\jsonapi_page_limit\Functional;
use Drupal\Tests\BrowserTestBase;
/**
* Test description.
*
* @group jsonapi_page_limit
*/
final class LimitTest extends BrowserTestBase {
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* {@inheritdoc}
*/
protected static $modules = [
'jsonapi',
'jsonapi_page_limit',
'node',
];
/**
* The name of the test content type.
*
* @var string
*/
protected string $testContentType = 'test_content_type';
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
// Create a content type to test with.
$this->contentType = $this->drupalCreateContentType(['type' => $this->testContentType]);
// Rebuild the routes before performing any tests to ensure the JSON:API
// dynamic routes function correctly.
\Drupal::service('router.builder')->rebuild();
// Create lots of test content.
for ($nodeCount = 0; $nodeCount < 100; $nodeCount++) {
$this->drupalCreateNode(['type' => $this->testContentType]);
}
}
/**
* Sets Drupal to have a configured JSON:API Page Limit.
*
* @param int $pageLimit
* Page limit.
*/
protected function setJsonApiPageLimit(int $pageLimit) : void {
$parameters = $this->container->getParameter('jsonapi_page_limit.size_max');
$parameters['/jsonapi/*'] = $pageLimit;
$this->setContainerParameter('jsonapi_page_limit.size_max', $parameters);
$this->rebuildContainer();
$this->resetAll();
}
/**
* Makes a JSON:API request and assert the results have the correct size.
*
* @param int $expectedResultCount
* Expected count of results in the JSON:API response.
* @param int $requestLimit
* Limit specified in the HTTP request.
* @param int $drupalLimit
* Limit specified at a system level in Drupal.
*
* @throws \Behat\Mink\Exception\ExpectationException
*/
protected function testRequest(int $expectedResultCount, int $requestLimit = 0, int $drupalLimit = 0) : void {
// Set the system-wide JSON:API page limit if passed.
if ($drupalLimit) {
$this->setJsonApiPageLimit($drupalLimit);
}
$options = [];
if ($requestLimit) {
$options = ['query' => ['page[limit]' => $requestLimit]];
}
$response = $this->drupalGet("jsonapi/node/$this->testContentType", $options);
$this->assertSession()->statusCodeEquals(200);
$response = json_decode($response);
$this->assertNotEmpty($response->data);
$this->assertCount($expectedResultCount, $response->data);
}
/**
* Test we get 50 items back on a normal request with no limit configured.
*
* @throws \Behat\Mink\Exception\ExpectationException
*/
public function testUnlimitedRequest(): void {
$this->testRequest(50);
}
/**
* Test we get 75 items back on a request limit and config limit of 75.
*
* @throws \Behat\Mink\Exception\ExpectationException
*/
public function testLimitedRequestWithConfiguredLimit(): void {
$this->testRequest(75, 75, 75);
}
/**
* Test we get 55 items back on a request limit of 55 and config limit of 75.
*
* @throws \Behat\Mink\Exception\ExpectationException
*/
public function testLimitedRequestLessThanConfiguredLimit(): void {
$this->testRequest(55, 55, 75);
}
/**
* Test we get 75 items back on a request limit of 100 and config limit of 75.
*
* @throws \Behat\Mink\Exception\ExpectationException
*/
public function testLimitedRequestWithMoreThanConfiguredLimit(): void {
$this->testRequest(75, 100, 75);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment