BUG Update DataQuery::exists to return false when limit causes no result to be returned (#9946)

* BUG Update DataQuery::exists to return false when limit causes no result to be returned

* Update comment

* Fixing linting issue
This commit is contained in:
Maxime Rainville 2021-05-31 16:50:58 +12:00 committed by GitHub
parent e6aeff6468
commit 472fc4ebb4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 5 deletions

View File

@ -465,10 +465,9 @@ class DataQuery
// statement anyway
$statement = $this->getFinalisedQuery();
// Clear limit, distinct, and order as it's not relevant for an exists query
// Clear distinct, and order as it's not relevant for an exists query
$statement->setDistinct(false);
$statement->setOrderBy(null);
$statement->setLimit(null);
// We can remove grouping if there's no "having" that might be relying on an aggregate
// Additionally, the columns being selected no longer matter

View File

@ -466,8 +466,50 @@ class DataQueryTest extends SapphireTest
public function testExistsCreatesFunctionalQueries()
{
$this->assertTrue(ObjectE::get()->exists());
$this->assertFalse(ObjectE::get()->where(['"Title" = ?' => 'Foo'])->exists());
$this->assertTrue(ObjectE::get()->dataQuery()->groupby('"SortOrder"')->exists());
$this->assertTrue(
ObjectE::get()->exists(),
'Query for ObjectE exists because there\'s more than 1 record'
);
$this->assertFalse(
ObjectE::get()->where(['"Title" = ?' => 'Foo'])->exists(),
'Query for ObjectE with Title Foo does NOT exists because there\'s no matching record'
);
$this->assertTrue(
ObjectE::get()->dataQuery()->groupby('"SortOrder"')->exists(),
'Existence of query for ObjectE is not affected by group by'
);
$this->assertTrue(
ObjectE::get()->limit(1)->exists(),
'Existence of query for ObjectE is not affected by limit if records are returned'
);
$this->assertFalse(
ObjectE::get()->limit(4, 9999)->exists(),
'Existence of query for ObjectE is affected by limit if no records are returned'
);
$query = new DataQuery(ObjectE::class);
$this->assertTrue(
$query->exists(),
'exist returns true if query return results'
);
$query = new DataQuery(ObjectE::class);
$this->assertFalse(
$query->where(['"Title" = ?' => 'Foo'])->exists(),
'exist returns false if there\'s no results'
);
$query = new DataQuery(ObjectE::class);
$this->assertTrue(
$query->groupby('"SortOrder"')->exists(),
'exist is unaffected by group by'
);
$query = new DataQuery(ObjectE::class);
$this->assertTrue(
$query->limit(1)->exists(),
'exist is unaffected by limit as long as one recard is returned'
);
$this->assertFalse(
$query->limit(1, 9999)->exists(),
'exist is false when a limit returns no results'
);
}
}