Skip to content

Commit

Permalink
Merge pull request #17063 from colemanw/api4limit
Browse files Browse the repository at this point in the history
APIv4 - Fix setting offset with no limit
  • Loading branch information
eileenmcnaughton authored Apr 13, 2020
2 parents 3d06904 + f8bf8e2 commit 9a05c57
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Civi/Api4/Query/Api4SelectQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,8 @@ protected function buildOrderBy() {
*/
protected function buildLimit() {
if (!empty($this->limit) || !empty($this->offset)) {
$this->query->limit($this->limit, $this->offset);
// If limit is 0, mysql will actually return 0 results. Instead set to maximum possible.
$this->query->limit($this->limit ?: '18446744073709551615', $this->offset);
}
}

Expand Down
21 changes: 21 additions & 0 deletions tests/phpunit/api/v4/Action/ContactGetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,25 @@ public function testGetDeletedContacts() {
$this->assertContains($del['id'], $contacts->column('id'));
}

public function testGetWithLimit() {
$last_name = uniqid('getWithLimitTest');

$bob = Contact::create()
->setValues(['first_name' => 'Bob', 'last_name' => $last_name])
->execute()->first();

$jan = Contact::create()
->setValues(['first_name' => 'Jan', 'last_name' => $last_name])
->execute()->first();

$dan = Contact::create()
->setValues(['first_name' => 'Dan', 'last_name' => $last_name])
->execute()->first();

$num = Contact::get()->setCheckPermissions(FALSE)->selectRowCount()->execute()->count();
$this->assertCount($num - 1, Contact::get()->setCheckPermissions(FALSE)->setLimit(0)->setOffset(1)->execute());
$this->assertCount($num - 2, Contact::get()->setCheckPermissions(FALSE)->setLimit(0)->setOffset(2)->execute());
$this->assertCount(2, Contact::get()->setCheckPermissions(FALSE)->setLimit(2)->setOffset(0)->execute());
}

}

0 comments on commit 9a05c57

Please sign in to comment.