ENH: Respect sort and limit arguments (#158)

These parameters are defined in the PHPDocs for `Report` and are technically part of the method signature. They should be respected and in the case of the new default limit in silverstripe/silverstripe-reports#139 this could have performance ramifications for large datasets.
This commit is contained in:
Guy Sartorelli 2022-03-11 09:14:06 +13:00 committed by GitHub
parent 46a637a6a8
commit 141e8d0bf3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 16 deletions

View File

@ -148,16 +148,26 @@ class PagesDueForReviewReport extends Report
/** /**
* @param array $params * @param array $params
* @param array|string|null $sort
* @param int|null $limit
* *
* @return SS_List * @return SS_List
*/ */
public function sourceRecords($params = []) public function sourceRecords($params = [], $sort = null, $limit = null)
{ {
Versioned::set_stage(Versioned::DRAFT); Versioned::set_stage(Versioned::DRAFT);
$records = SiteTree::get(); $records = SiteTree::get();
$compatibility = ContentReviewCompatability::start(); $compatibility = ContentReviewCompatability::start();
// Apply sort and limit if appropriate.
if ($sort !== null) {
$records = $records->sort($sort);
}
if ($limit !== null) {
$records = $records->limit($limit);
}
if (empty($params['ReviewDateBefore']) && empty($params['ReviewDateAfter'])) { if (empty($params['ReviewDateBefore']) && empty($params['ReviewDateAfter'])) {
// If there's no review dates set, default to all pages due for review now // If there's no review dates set, default to all pages due for review now
$records = $records->where( $records = $records->where(

View File

@ -104,10 +104,12 @@ class PagesWithoutReviewScheduleReport extends Report
/** /**
* @param array $params * @param array $params
* @param array|string|null $sort
* @param int|null $limit
* *
* @return SS_List * @return SS_List
*/ */
public function sourceRecords($params = []) public function sourceRecords($params = [], $sort = null, $limit = null)
{ {
Versioned::set_stage(Versioned::DRAFT); Versioned::set_stage(Versioned::DRAFT);
@ -125,16 +127,18 @@ class PagesWithoutReviewScheduleReport extends Report
)); ));
} }
$records->sort("ParentID"); // Apply sort and limit if appropriate.
$records = $records->toArray(); if ($sort !== null) {
$records = $records->sort($sort);
}
if ($limit !== null) {
$records = $records->limit($limit);
}
// Trim out calculated values // Trim out calculated values
$list = ArrayList::create(); $list = $records->filterByCallback(function ($record) {
foreach ($records as $record) { return !$this->hasReviewSchedule($record);
if (!$this->hasReviewSchedule($record)) { });
$list->push($record);
}
}
ContentReviewCompatability::done($compatibility); ContentReviewCompatability::done($compatibility);

View File

@ -87,11 +87,11 @@ class ContentReviewReportTest extends FunctionalTest
$results = $report->sourceRecords(); $results = $report->sourceRecords();
$this->assertEquals([ $this->assertListEquals([
"Home", ['Title' => 'Home'],
"About Us", ['Title' => 'About Us'],
"Page without review date", ['Title' => 'Page without review date'],
"Page owned by group", ['Title' => 'Page owned by group'],
], $results->column("Title")); ], $results);
} }
} }