Skip to content
Snippets Groups Projects
Commit 4e316ff1 authored by Daniel Cothran's avatar Daniel Cothran
Browse files

Issue #3479470 by andileco, nikathone: Ensure that table sort option works

parent fc4f7f82
No related branches found
No related tags found
1 merge request!12Update 2 files
Pipeline #306837 passed with warnings
......@@ -45,15 +45,3 @@ include:
# - '/includes/include.drupalci.main-d7.yml'
- '/includes/include.drupalci.variables.yml'
- '/includes/include.drupalci.workflows.yml'
################
# Pipeline configuration variables
#
# These are the variables provided to the Run Pipeline form that a user may want to override.
#
# Docs at https://git.drupalcode.org/project/gitlab_templates/-/blob/1.0.x/includes/include.drupalci.variables.yml
################
variables:
# Disables the default phpunit job as we have no tests on this module.
SKIP_PHPUNIT: 1
......@@ -512,7 +512,7 @@ class Select {
if ($order_by = $this->getObjectItem('order_by')) {
foreach ($order_by as $sort) {
$stmt = $stmt->orderBy(function (array $a, array $b) use ($sort): int {
$direction = $sort->direction;
$direction = strtoupper($sort->direction);
return match ($direction) {
self::DIRECTION_ASC => strnatcasecmp($a[$sort->column] ?? '', $b[$sort->column] ?? ''),
self::DIRECTION_DESC => -strnatcasecmp($a[$sort->column] ?? '', $b[$sort->column] ?? ''),
......
......@@ -186,6 +186,28 @@ class SelectTest extends UnitTestCase {
], $records[0]);
}
/**
* @covers ::orderBy
* @covers ::execute
* @covers ::applyOrderBy
* @covers ::getRecords
*
* @dataProvider orderByDataProvider
*/
public function testOrderBy(string $order, string $expected_first, string $expected_last) {
$select = $this->getSelectQuery()
->addField('Geography name', 'group_by')
->orderBy('Geography name', $order);
$records = $select->execute();
$records = array_values(iterator_to_array($records));
$this->assertCount(7, $records);
$this->assertEquals(['Geography name' => $expected_first], $records[0]);
$this->assertEquals(['Geography name' => $expected_last], $records[6]);
}
/**
* @covers ::groupBy
* @covers ::orderBy
......@@ -348,6 +370,28 @@ class SelectTest extends UnitTestCase {
return $conditions;
}
/**
* Data provider for ::testOrderBy().
*/
public function orderByDataProvider(): array {
$asc_data = [
Select::DIRECTION_ASC,
'Alabama',
'Kansas',
];
$desc_data = [
Select::DIRECTION_DESC,
'Kansas',
'Alabama',
];
return [
'select_direction_asc' => $asc_data,
'select_direction_desc' => $desc_data,
'lowercase_asc' => [0 => 'asc'] + $asc_data,
'lowercase_desc' => [0 => 'desc'] + $desc_data,
];
}
/**
* Gets the select query object.
*
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment