Merge pull request #4203 from fiveai/fix-api-search-with-pagination

Fix API search with pagination when specifying page
This commit is contained in:
James Brooks
2021-03-29 11:54:58 +01:00
committed by GitHub
2 changed files with 46 additions and 2 deletions

View File

@@ -34,10 +34,11 @@ trait SearchableTrait
return $query;
}
if (!array_intersect(array_keys($search), $this->searchable)) {
$allowed_search = array_intersect_key($search, array_flip($this->searchable));
if (!$allowed_search) {
return $query;
}
return $query->where($search);
return $query->where($allowed_search);
}
}

View File

@@ -30,6 +30,49 @@ class IncidentTest extends AbstractApiTestCase
$this->assertResponseOk();
}
public function testGetIncidentsPaginated()
{
$incidents = factory('CachetHQ\Cachet\Models\Incident', 3)->create();
$this->get('/api/v1/incidents?per_page=1&page=1');
$this->seeJson(['id' => $incidents[0]->id]);
$this->assertResponseOk();
$this->get('/api/v1/incidents?per_page=1&page=2');
$this->seeJson(['id' => $incidents[1]->id]);
$this->assertResponseOk();
}
public function testGetIncidentsBySearch()
{
factory('CachetHQ\Cachet\Models\Incident', 3)->create([
'status' => 0,
]);
$incidents = factory('CachetHQ\Cachet\Models\Incident', 2)->create([
'status' => 1,
]);
$this->get('/api/v1/incidents?status=1');
$this->seeJson(['id' => $incidents[0]->id]);
$this->assertResponseOk();
}
public function testGetIncidentsPaginatedBySearch()
{
factory('CachetHQ\Cachet\Models\Incident', 3)->create([
'status' => 0,
]);
$incidents = factory('CachetHQ\Cachet\Models\Incident', 2)->create([
'status' => 1,
]);
$this->get('/api/v1/incidents?status=1&per_page=1&page=1');
$this->seeJson(['id' => $incidents[0]->id]);
$this->assertResponseOk();
}
public function testGetInvalidIncident()
{
$this->get('/api/v1/incidents/0');