Skip to content

Commit

Permalink
[5.x] Prevent incompatible pagination parameter combinations (#10415)
Browse files Browse the repository at this point in the history
Co-authored-by: Jason Varga <jason@pixelfear.com>
  • Loading branch information
jesseleite and jasonvarga authored Jul 10, 2024
1 parent a39cadd commit 65e0c5b
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 4 deletions.
22 changes: 20 additions & 2 deletions src/Tags/Concerns/GetsQueryResults.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ trait GetsQueryResults
{
protected function results($query)
{
$this->setPaginationParameterPrecedence();
$this
->allowLegacyStylePaginationLimiting()
->preventIncompatiblePaginationParameters();

if ($paginate = $this->params->int('paginate')) {
return $this->paginatedResults($query, $paginate);
Expand All @@ -30,11 +32,27 @@ protected function results($query)
return $query->get();
}

protected function setPaginationParameterPrecedence()
protected function allowLegacyStylePaginationLimiting()
{
if ($this->params->get('paginate') === true) {
$this->params->put('paginate', $this->params->get('limit'));
$this->params->forget('limit');
}

return $this;
}

protected function preventIncompatiblePaginationParameters()
{
if ($this->params->int('paginate') && $this->params->has('limit')) {
throw new \Exception('Cannot use [paginate] integer in combination with [limit] param.');
}

if ($this->params->has('paginate') && $this->params->has('chunk')) {
throw new \Exception('Cannot use [paginate] in combination with [chunk] param.');
}

return $this;
}

protected function paginatedResults($query, $perPage)
Expand Down
48 changes: 46 additions & 2 deletions tests/Tags/Collection/EntriesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,7 @@ public function it_gets_paginated_entries_in_a_collection()

$this->assertCount(5, $this->getEntries());
$this->assertCount(3, $this->getEntries(['paginate' => 3])); // recommended v3 style
$this->assertCount(4, $this->getEntries(['paginate' => true, 'limit' => 4])); // v2 style
$this->assertCount(3, $this->getEntries(['paginate' => 3, 'limit' => 4])); // precedence test
$this->assertCount(4, $this->getEntries(['paginate' => true, 'limit' => 4])); // v2 style pagination limiting
$this->assertCount(5, $this->getEntries(['paginate' => true])); // ignore if no perPage set

$this->assertEquals(['a', 'b', 'c'], $this->getEntryIds(['paginate' => 3]));
Expand Down Expand Up @@ -128,6 +127,51 @@ public function it_gets_offset_paginated_entries_in_a_collection()
$this->assertEquals(['f', 'g'], $this->getEntryIds(['paginate' => 3, 'offset' => 2]));
}

#[Test]
public function it_should_throw_exception_if_trying_to_paginate_and_limit_at_same_time()
{
$this->makeEntry('a')->save();
$this->makeEntry('b')->save();
$this->makeEntry('c')->save();
$this->makeEntry('d')->save();
$this->makeEntry('e')->save();

$this->expectException(\Exception::class);
$this->expectExceptionMessage('Cannot use [paginate] integer in combination with [limit] param.');

$this->assertCount(3, $this->getEntries(['paginate' => 3, 'limit' => 4]));
}

#[Test]
public function it_should_throw_exception_if_trying_to_paginate_and_chunk_at_same_time()
{
$this->makeEntry('a')->save();
$this->makeEntry('b')->save();
$this->makeEntry('c')->save();
$this->makeEntry('d')->save();
$this->makeEntry('e')->save();

$this->expectException(\Exception::class);
$this->expectExceptionMessage('Cannot use [paginate] in combination with [chunk] param.');

$this->assertCount(3, $this->getEntries(['paginate' => true, 'chunk' => 2]));
}

#[Test]
public function it_should_throw_exception_if_trying_to_paginate_with_integer_and_chunk_at_same_time()
{
$this->makeEntry('a')->save();
$this->makeEntry('b')->save();
$this->makeEntry('c')->save();
$this->makeEntry('d')->save();
$this->makeEntry('e')->save();

$this->expectException(\Exception::class);
$this->expectExceptionMessage('Cannot use [paginate] in combination with [chunk] param.');

$this->assertCount(3, $this->getEntries(['paginate' => 3, 'chunk' => 2]));
}

#[Test]
public function it_gets_localized_site_entries_in_a_collection()
{
Expand Down

0 comments on commit 65e0c5b

Please sign in to comment.