diff --git a/src/Tags/Concerns/GetsQueryResults.php b/src/Tags/Concerns/GetsQueryResults.php index d57ec00c34..766b8b2290 100644 --- a/src/Tags/Concerns/GetsQueryResults.php +++ b/src/Tags/Concerns/GetsQueryResults.php @@ -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); @@ -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) diff --git a/tests/Tags/Collection/EntriesTest.php b/tests/Tags/Collection/EntriesTest.php index c32a4c4720..4306ce487c 100644 --- a/tests/Tags/Collection/EntriesTest.php +++ b/tests/Tags/Collection/EntriesTest.php @@ -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])); @@ -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() {