Skip to content

Commit

Permalink
fix: Prevent query->count from changing query internals (#511)
Browse files Browse the repository at this point in the history
  • Loading branch information
dplewis authored May 13, 2023
1 parent 8b17fa9 commit 26db002
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 7 deletions.
7 changes: 4 additions & 3 deletions src/Parse/ParseQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -586,9 +586,10 @@ public function count($useMasterKey = false)
if (ParseUser::getCurrentUser()) {
$sessionToken = ParseUser::getCurrentUser()->getSessionToken();
}
$this->limit = 0;
$this->count = 1;
$queryString = $this->buildQueryString($this->_getOptions());
$queryParams = $this->_getOptions();
$queryParams['limit'] = 0;
$queryParams['count'] = 1;
$queryString = $this->buildQueryString($queryParams);
$result = ParseClient::_request(
'GET',
'classes/'.$this->className.'?'.$queryString,
Expand Down
45 changes: 41 additions & 4 deletions tests/Parse/ParseQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2654,8 +2654,8 @@ public function testGetAndSetConditions()
$query->select(['select1','select2']);
$query->skip(24);

// sets count = 1 and limit = 0
$query->count();
// sets count = 1
$query->withCount();
// reset limit up to 42
$query->limit(42);

Expand Down Expand Up @@ -2695,6 +2695,45 @@ public function testGetAndSetConditions()
);
}

/**
* @group query-count-conditions
*/
public function testCountDoesNotOverrideConditions()
{
$obj = new ParseObject('TestObject');
$obj->set('name', 'John');
$obj->set('country', 'US');
$obj->save();

$obj = new ParseObject('TestObject');
$obj->set('name', 'Bob');
$obj->set('country', 'US');
$obj->save();

$obj = new ParseObject('TestObject');
$obj->set('name', 'Mike');
$obj->set('country', 'CA');
$obj->save();

$query = new ParseQuery('TestObject');
$query->equalTo('country', 'US');
$query->limit(1);
$count = $query->count();
$results = $query->find();

$this->assertEquals(1, count($results));
$this->assertEquals(2, $count);

$this->assertSame([
'where' => [
'country' => [
'$eq' => 'US'
]
],
'limit' => 1,
], $query->_getOptions());
}

public function testNotArrayConditions()
{
$this->expectException(
Expand Down Expand Up @@ -2770,8 +2809,6 @@ public function testEqualToWithSameKeyDoesNotOverrideOtherConditions()
'$eq' => 'bar',
]
],
'limit' => 0,
'count' => 1,
], $query->_getOptions());
}
}

0 comments on commit 26db002

Please sign in to comment.