Skip to content

Commit

Permalink
Fix Doctrine query with a negative length (DataTables option bPaginat…
Browse files Browse the repository at this point in the history
…e with false value)
  • Loading branch information
webeweb committed Oct 8, 2018
1 parent 6004771 commit a91069d
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
6 changes: 4 additions & 2 deletions Repository/DefaultDataTablesRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,10 @@ protected function buildDataTablesFindAll(DataTablesWrapper $dtWrapper) {

// Create a query builder.
$qb = $this->createQueryBuilder($prefix)
->setFirstResult($dtWrapper->getRequest()->getStart())
->setMaxResults($dtWrapper->getRequest()->getLength());
->setFirstResult($dtWrapper->getRequest()->getStart());
if (0 < $dtWrapper->getRequest()->getLength()) {
$qb->setMaxResults($dtWrapper->getRequest()->getLength());
}

// Build the where and order clauses.
DataTablesRepositoryHelper::appendWhere($qb, $dtWrapper);
Expand Down
53 changes: 53 additions & 0 deletions Tests/Controller/DataTablesControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,59 @@ public function testIndexActionWithLength() {
$this->assertEquals("Gavin Joyce", $res["data"][19]["name"]);
}

/**
* Tests the indexAction() method.
*
* @return void
* @depends testIndexAction
*/
public function testIndexActionWithNegativeLength() {

// Get the POST data.
$parameters = TestFixtures::getPOSTData();

// Change the length.
$parameters["length"] = "-1";

// Create a client.
$client = static::createClient();

// Make a POST request with XML HTTP request.
$client->request("POST", "/datatables/employee/index", $parameters, [], ["HTTP_X-Requested-With" => "XMLHttpRequest"]);
$this->assertEquals(200, $client->getResponse()->getStatusCode());
$this->assertEquals("application/json", $client->getResponse()->headers->get("Content-Type"));

// Check the JSON response.
$res = json_decode($client->getResponse()->getContent(), true);

$this->assertCount(57, $res["data"]);

$this->assertArrayHasKey("DT_RowId", $res["data"][0]);
$this->assertArrayHasKey("DT_RowClass", $res["data"][0]);
$this->assertArrayHasKey("DT_RowData", $res["data"][0]);

$this->assertEquals("Airi Satou", $res["data"][0]["name"]);
$this->assertEquals("Angelica Ramos", $res["data"][1]["name"]);
$this->assertEquals("Ashton Cox", $res["data"][2]["name"]);
$this->assertEquals("Bradley Greer", $res["data"][3]["name"]);
$this->assertEquals("Brenden Wagner", $res["data"][4]["name"]);
$this->assertEquals("Brielle Williamson", $res["data"][5]["name"]);
$this->assertEquals("Bruno Nash", $res["data"][6]["name"]);
$this->assertEquals("Caesar Vance", $res["data"][7]["name"]);
$this->assertEquals("Cara Stevens", $res["data"][8]["name"]);
$this->assertEquals("Cedric Kelly", $res["data"][9]["name"]);

// [...]

$this->assertEquals("Tiger Nixon", $res["data"][50]["name"]);
$this->assertEquals("Timothy Mooney", $res["data"][51]["name"]);
$this->assertEquals("Unity Butler", $res["data"][52]["name"]);
$this->assertEquals("Vivian Harrell", $res["data"][53]["name"]);
$this->assertEquals("Yuri Berry", $res["data"][54]["name"]);
$this->assertEquals("Zenaida Frank", $res["data"][55]["name"]);
$this->assertEquals("Zorita Serrano", $res["data"][56]["name"]);
}

/**
* Tests the indexAction() method.
*
Expand Down

0 comments on commit a91069d

Please sign in to comment.