case NameAsc = 'name_asc';
case NameDesc = 'name_desc';
case NameNumericAsc = 'name_numeric_asc';
+ case NameNumericDesc = 'name_numeric_desc';
case CreatedDateAsc = 'created_date_asc';
case CreatedDateDesc = 'created_date_desc';
case UpdateDateAsc = 'updated_date_asc';
/**
* Sort comparison function for each of the possible SortSetOperation values.
* Method names should be camelCase names for the SortSetOperation enum value.
- * TODO - Test to cover each SortSetOperation enum value is covered.
*/
class SortSetOperationComparisons
{
$numRegex = '/^\d+(\.\d+)?/';
$aMatches = [];
$bMatches = [];
- preg_match($numRegex, $a, $aMatches);
- preg_match($numRegex, $b, $bMatches);
- return ($aMatches[0] ?? 0) <=> ($bMatches[0] ?? 0);
+ preg_match($numRegex, $a->name, $aMatches);
+ preg_match($numRegex, $b->name, $bMatches);
+ $aVal = floatval(($aMatches[0] ?? 0));
+ $bVal = floatval(($bMatches[0] ?? 0));
+
+ return $aVal <=> $bVal;
}
public static function nameNumericDesc(Entity $a, Entity $b): int
use BookStack\Activity\ActivityType;
use BookStack\Entities\Models\Book;
use BookStack\Sorting\SortRule;
+use BookStack\Sorting\SortRuleOperation;
use Tests\Api\TestsApi;
use Tests\TestCase;
"20 - Milk",
];
- foreach ($namesToAdd as $name) {
+ $reverseNamesToAdd = array_reverse($namesToAdd);
+ foreach ($reverseNamesToAdd as $name) {
$this->actingAsApiEditor()->post("/api/pages", [
'book_id' => $book->id,
'name' => $name,
]);
}
}
+
+ public function test_each_sort_rule_operation_has_a_comparison_function()
+ {
+ $operations = SortRuleOperation::cases();
+
+ foreach ($operations as $operation) {
+ $comparisonFunc = $operation->getSortFunction();
+ $this->assertIsCallable($comparisonFunc);
+ }
+ }
}