mirror of
https://github.com/BookStackApp/BookStack.git
synced 2025-05-10 02:50:39 +00:00
Sorting: Fixes during testing of sort rules
- Fixed name numeric sorting not working as expected due to bad comparison. - Added name numeric desc operation option. - Added test to ensure each operating has a comparison function.
This commit is contained in:
parent
d7ccb3ce6a
commit
dca14feaaa
3 changed files with 20 additions and 5 deletions
|
@ -10,6 +10,7 @@ enum SortRuleOperation: string
|
||||||
case NameAsc = 'name_asc';
|
case NameAsc = 'name_asc';
|
||||||
case NameDesc = 'name_desc';
|
case NameDesc = 'name_desc';
|
||||||
case NameNumericAsc = 'name_numeric_asc';
|
case NameNumericAsc = 'name_numeric_asc';
|
||||||
|
case NameNumericDesc = 'name_numeric_desc';
|
||||||
case CreatedDateAsc = 'created_date_asc';
|
case CreatedDateAsc = 'created_date_asc';
|
||||||
case CreatedDateDesc = 'created_date_desc';
|
case CreatedDateDesc = 'created_date_desc';
|
||||||
case UpdateDateAsc = 'updated_date_asc';
|
case UpdateDateAsc = 'updated_date_asc';
|
||||||
|
|
|
@ -8,7 +8,6 @@ use BookStack\Entities\Models\Entity;
|
||||||
/**
|
/**
|
||||||
* Sort comparison function for each of the possible SortSetOperation values.
|
* Sort comparison function for each of the possible SortSetOperation values.
|
||||||
* Method names should be camelCase names for the SortSetOperation enum value.
|
* Method names should be camelCase names for the SortSetOperation enum value.
|
||||||
* TODO - Test to cover each SortSetOperation enum value is covered.
|
|
||||||
*/
|
*/
|
||||||
class SortSetOperationComparisons
|
class SortSetOperationComparisons
|
||||||
{
|
{
|
||||||
|
@ -27,9 +26,12 @@ class SortSetOperationComparisons
|
||||||
$numRegex = '/^\d+(\.\d+)?/';
|
$numRegex = '/^\d+(\.\d+)?/';
|
||||||
$aMatches = [];
|
$aMatches = [];
|
||||||
$bMatches = [];
|
$bMatches = [];
|
||||||
preg_match($numRegex, $a, $aMatches);
|
preg_match($numRegex, $a->name, $aMatches);
|
||||||
preg_match($numRegex, $b, $bMatches);
|
preg_match($numRegex, $b->name, $bMatches);
|
||||||
return ($aMatches[0] ?? 0) <=> ($bMatches[0] ?? 0);
|
$aVal = floatval(($aMatches[0] ?? 0));
|
||||||
|
$bVal = floatval(($bMatches[0] ?? 0));
|
||||||
|
|
||||||
|
return $aVal <=> $bVal;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function nameNumericDesc(Entity $a, Entity $b): int
|
public static function nameNumericDesc(Entity $a, Entity $b): int
|
||||||
|
|
|
@ -5,6 +5,7 @@ namespace Tests\Sorting;
|
||||||
use BookStack\Activity\ActivityType;
|
use BookStack\Activity\ActivityType;
|
||||||
use BookStack\Entities\Models\Book;
|
use BookStack\Entities\Models\Book;
|
||||||
use BookStack\Sorting\SortRule;
|
use BookStack\Sorting\SortRule;
|
||||||
|
use BookStack\Sorting\SortRuleOperation;
|
||||||
use Tests\Api\TestsApi;
|
use Tests\Api\TestsApi;
|
||||||
use Tests\TestCase;
|
use Tests\TestCase;
|
||||||
|
|
||||||
|
@ -202,7 +203,8 @@ class SortRuleTest extends TestCase
|
||||||
"20 - Milk",
|
"20 - Milk",
|
||||||
];
|
];
|
||||||
|
|
||||||
foreach ($namesToAdd as $name) {
|
$reverseNamesToAdd = array_reverse($namesToAdd);
|
||||||
|
foreach ($reverseNamesToAdd as $name) {
|
||||||
$this->actingAsApiEditor()->post("/api/pages", [
|
$this->actingAsApiEditor()->post("/api/pages", [
|
||||||
'book_id' => $book->id,
|
'book_id' => $book->id,
|
||||||
'name' => $name,
|
'name' => $name,
|
||||||
|
@ -218,4 +220,14 @@ class SortRuleTest extends TestCase
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function test_each_sort_rule_operation_has_a_comparison_function()
|
||||||
|
{
|
||||||
|
$operations = SortRuleOperation::cases();
|
||||||
|
|
||||||
|
foreach ($operations as $operation) {
|
||||||
|
$comparisonFunc = $operation->getSortFunction();
|
||||||
|
$this->assertIsCallable($comparisonFunc);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue