Unverified Commit 64344760 authored by alexpott's avatar alexpott

Issue #2942569 by seanB, idebr, amateescu, kristiaanvandeneynde: Sorting...

Issue #2942569 by seanB, idebr, amateescu, kristiaanvandeneynde: Sorting nested properties of config entity queries does not work

(cherry picked from commit a07a4ba0)
parent ed21c9b4
...@@ -89,7 +89,14 @@ public function execute() { ...@@ -89,7 +89,14 @@ public function execute() {
$direction = $sort['direction'] == 'ASC' ? -1 : 1; $direction = $sort['direction'] == 'ASC' ? -1 : 1;
$field = $sort['field']; $field = $sort['field'];
uasort($result, function ($a, $b) use ($field, $direction) { uasort($result, function ($a, $b) use ($field, $direction) {
return ($a[$field] <= $b[$field]) ? $direction : -$direction; $properties = explode('.', $field);
foreach ($properties as $property) {
if (isset($a[$property]) || isset($b[$property])) {
$a = isset($a[$property]) ? $a[$property] : NULL;
$b = isset($b[$property]) ? $b[$property] : NULL;
}
}
return ($a <= $b) ? $direction : -$direction;
}); });
} }
......
...@@ -114,6 +114,27 @@ protected function setUp(): void { ...@@ -114,6 +114,27 @@ protected function setUp(): void {
$this->entities[] = $entity; $this->entities[] = $entity;
$entity->enforceIsNew(); $entity->enforceIsNew();
$entity->save(); $entity->save();
$array['level1'] = [];
$entity = ConfigQueryTest::create([
'label' => $this->randomMachineName(),
'id' => '6',
'array' => $array,
]);
$this->entities[] = $entity;
$entity->enforceIsNew();
$entity->save();
$array['level1']['level2'] = 4;
$entity = ConfigQueryTest::create([
'label' => $this->randomMachineName(),
'id' => '7',
'number' => 70,
'array' => $array,
]);
$this->entities[] = $entity;
$entity->enforceIsNew();
$entity->save();
} }
/** /**
...@@ -123,11 +144,11 @@ public function testConfigEntityQuery() { ...@@ -123,11 +144,11 @@ public function testConfigEntityQuery() {
// Run a test without any condition. // Run a test without any condition.
$this->queryResults = $this->entityStorage->getQuery() $this->queryResults = $this->entityStorage->getQuery()
->execute(); ->execute();
$this->assertResults(['1', '2', '3', '4', '5']); $this->assertResults(['1', '2', '3', '4', '5', '6', '7']);
// No conditions, OR. // No conditions, OR.
$this->queryResults = $this->entityStorage->getQuery('OR') $this->queryResults = $this->entityStorage->getQuery('OR')
->execute(); ->execute();
$this->assertResults(['1', '2', '3', '4', '5']); $this->assertResults(['1', '2', '3', '4', '5', '6', '7']);
// Filter by ID with equality. // Filter by ID with equality.
$this->queryResults = $this->entityStorage->getQuery() $this->queryResults = $this->entityStorage->getQuery()
...@@ -169,19 +190,19 @@ public function testConfigEntityQuery() { ...@@ -169,19 +190,19 @@ public function testConfigEntityQuery() {
$this->queryResults = $this->entityStorage->getQuery() $this->queryResults = $this->entityStorage->getQuery()
->condition('id', '3', '>') ->condition('id', '3', '>')
->execute(); ->execute();
$this->assertResults(['4', '5']); $this->assertResults(['4', '5', '6', '7']);
// Filter by ID with the >= operator. // Filter by ID with the >= operator.
$this->queryResults = $this->entityStorage->getQuery() $this->queryResults = $this->entityStorage->getQuery()
->condition('id', '3', '>=') ->condition('id', '3', '>=')
->execute(); ->execute();
$this->assertResults(['3', '4', '5']); $this->assertResults(['3', '4', '5', '6', '7']);
// Filter by ID with the <> operator. // Filter by ID with the <> operator.
$this->queryResults = $this->entityStorage->getQuery() $this->queryResults = $this->entityStorage->getQuery()
->condition('id', '3', '<>') ->condition('id', '3', '<>')
->execute(); ->execute();
$this->assertResults(['1', '2', '4', '5']); $this->assertResults(['1', '2', '4', '5', '6', '7']);
// Filter by ID with the < operator. // Filter by ID with the < operator.
$this->queryResults = $this->entityStorage->getQuery() $this->queryResults = $this->entityStorage->getQuery()
...@@ -226,7 +247,7 @@ public function testConfigEntityQuery() { ...@@ -226,7 +247,7 @@ public function testConfigEntityQuery() {
->condition('number', 10, '>=') ->condition('number', 10, '>=')
->condition('number', 50, '>=') ->condition('number', 50, '>=')
->execute(); ->execute();
$this->assertResults(['3', '5']); $this->assertResults(['3', '5', '7']);
// Filter with an OR condition group. // Filter with an OR condition group.
$this->queryResults = $this->entityStorage->getQuery('OR') $this->queryResults = $this->entityStorage->getQuery('OR')
...@@ -249,7 +270,7 @@ public function testConfigEntityQuery() { ...@@ -249,7 +270,7 @@ public function testConfigEntityQuery() {
$this->queryResults = $this->entityStorage->getQuery() $this->queryResults = $this->entityStorage->getQuery()
->condition('id', ['1', '2'], 'NOT IN') ->condition('id', ['1', '2'], 'NOT IN')
->execute(); ->execute();
$this->assertResults(['3', '4', '5']); $this->assertResults(['3', '4', '5', '6', '7']);
// Filter with an OR condition group on different fields. // Filter with an OR condition group on different fields.
$this->queryResults = $this->entityStorage->getQuery('OR') $this->queryResults = $this->entityStorage->getQuery('OR')
...@@ -328,7 +349,7 @@ public function testConfigEntityQuery() { ...@@ -328,7 +349,7 @@ public function testConfigEntityQuery() {
$this->queryResults = $this->entityStorage->getQuery() $this->queryResults = $this->entityStorage->getQuery()
->exists('id') ->exists('id')
->execute(); ->execute();
$this->assertResults(['1', '2', '3', '4', '5']); $this->assertResults(['1', '2', '3', '4', '5', '6', '7']);
$this->queryResults = $this->entityStorage->getQuery() $this->queryResults = $this->entityStorage->getQuery()
->exists('non-existent') ->exists('non-existent')
...@@ -343,7 +364,7 @@ public function testConfigEntityQuery() { ...@@ -343,7 +364,7 @@ public function testConfigEntityQuery() {
$this->queryResults = $this->entityStorage->getQuery() $this->queryResults = $this->entityStorage->getQuery()
->notExists('non-existent') ->notExists('non-existent')
->execute(); ->execute();
$this->assertResults(['1', '2', '3', '4', '5']); $this->assertResults(['1', '2', '3', '4', '5', '6', '7']);
} }
/** /**
...@@ -436,38 +457,38 @@ public function testSortRange() { ...@@ -436,38 +457,38 @@ public function testSortRange() {
$this->queryResults = $this->entityStorage->getQuery() $this->queryResults = $this->entityStorage->getQuery()
->sort('number', 'DESC') ->sort('number', 'DESC')
->execute(); ->execute();
$this->assertIdentical(array_values($this->queryResults), ['3', '5', '2', '1', '4']); $this->assertIdentical(array_values($this->queryResults), ['7', '3', '5', '2', '1', '4', '6']);
$this->queryResults = $this->entityStorage->getQuery() $this->queryResults = $this->entityStorage->getQuery()
->sort('number', 'ASC') ->sort('number', 'ASC')
->execute(); ->execute();
$this->assertIdentical(array_values($this->queryResults), ['4', '1', '2', '5', '3']); $this->assertIdentical(array_values($this->queryResults), ['6', '4', '1', '2', '5', '3', '7']);
// Apply some filters and sort. // Apply some filters and sort.
$this->queryResults = $this->entityStorage->getQuery() $this->queryResults = $this->entityStorage->getQuery()
->condition('id', '3', '>') ->condition('id', '3', '>')
->sort('number', 'DESC') ->sort('number', 'DESC')
->execute(); ->execute();
$this->assertIdentical(array_values($this->queryResults), ['5', '4']); $this->assertIdentical(array_values($this->queryResults), ['7', '5', '4', '6']);
$this->queryResults = $this->entityStorage->getQuery() $this->queryResults = $this->entityStorage->getQuery()
->condition('id', '3', '>') ->condition('id', '3', '>')
->sort('number', 'ASC') ->sort('number', 'ASC')
->execute(); ->execute();
$this->assertIdentical(array_values($this->queryResults), ['4', '5']); $this->assertIdentical(array_values($this->queryResults), ['6', '4', '5', '7']);
// Apply a pager and sort. // Apply a pager and sort.
$this->queryResults = $this->entityStorage->getQuery() $this->queryResults = $this->entityStorage->getQuery()
->sort('number', 'DESC') ->sort('number', 'DESC')
->range('2', '2') ->range('2', '2')
->execute(); ->execute();
$this->assertIdentical(array_values($this->queryResults), ['2', '1']); $this->assertIdentical(array_values($this->queryResults), ['5', '2']);
$this->queryResults = $this->entityStorage->getQuery() $this->queryResults = $this->entityStorage->getQuery()
->sort('number', 'ASC') ->sort('number', 'ASC')
->range('2', '2') ->range('2', '2')
->execute(); ->execute();
$this->assertIdentical(array_values($this->queryResults), ['2', '5']); $this->assertIdentical(array_values($this->queryResults), ['1', '2']);
// Add a range to a query without a start parameter. // Add a range to a query without a start parameter.
$this->queryResults = $this->entityStorage->getQuery() $this->queryResults = $this->entityStorage->getQuery()
...@@ -499,28 +520,28 @@ public function testTableSort() { ...@@ -499,28 +520,28 @@ public function testTableSort() {
->tableSort($header) ->tableSort($header)
->sort('id', 'DESC') ->sort('id', 'DESC')
->execute(); ->execute();
$this->assertIdentical(array_values($this->queryResults), ['5', '4', '3', '2', '1']); $this->assertIdentical(array_values($this->queryResults), ['7', '6', '5', '4', '3', '2', '1']);
// Sorting with 'ASC' upper case // Sorting with 'ASC' upper case
$this->queryResults = $this->entityStorage->getQuery() $this->queryResults = $this->entityStorage->getQuery()
->tableSort($header) ->tableSort($header)
->sort('id', 'ASC') ->sort('id', 'ASC')
->execute(); ->execute();
$this->assertIdentical(array_values($this->queryResults), ['1', '2', '3', '4', '5']); $this->assertIdentical(array_values($this->queryResults), ['1', '2', '3', '4', '5', '6', '7']);
// Sorting with 'desc' lower case // Sorting with 'desc' lower case
$this->queryResults = $this->entityStorage->getQuery() $this->queryResults = $this->entityStorage->getQuery()
->tableSort($header) ->tableSort($header)
->sort('id', 'desc') ->sort('id', 'desc')
->execute(); ->execute();
$this->assertIdentical(array_values($this->queryResults), ['5', '4', '3', '2', '1']); $this->assertIdentical(array_values($this->queryResults), ['7', '6', '5', '4', '3', '2', '1']);
// Sorting with 'asc' lower case // Sorting with 'asc' lower case
$this->queryResults = $this->entityStorage->getQuery() $this->queryResults = $this->entityStorage->getQuery()
->tableSort($header) ->tableSort($header)
->sort('id', 'asc') ->sort('id', 'asc')
->execute(); ->execute();
$this->assertIdentical(array_values($this->queryResults), ['1', '2', '3', '4', '5']); $this->assertIdentical(array_values($this->queryResults), ['1', '2', '3', '4', '5', '6', '7']);
// Sort key: number // Sort key: number
// Sorting with 'DeSc' mixed upper and lower case // Sorting with 'DeSc' mixed upper and lower case
...@@ -528,28 +549,28 @@ public function testTableSort() { ...@@ -528,28 +549,28 @@ public function testTableSort() {
->tableSort($header) ->tableSort($header)
->sort('number', 'DeSc') ->sort('number', 'DeSc')
->execute(); ->execute();
$this->assertIdentical(array_values($this->queryResults), ['3', '5', '2', '1', '4']); $this->assertIdentical(array_values($this->queryResults), ['7', '3', '5', '2', '1', '4', '6']);
// Sorting with 'AsC' mixed upper and lower case // Sorting with 'AsC' mixed upper and lower case
$this->queryResults = $this->entityStorage->getQuery() $this->queryResults = $this->entityStorage->getQuery()
->tableSort($header) ->tableSort($header)
->sort('number', 'AsC') ->sort('number', 'AsC')
->execute(); ->execute();
$this->assertIdentical(array_values($this->queryResults), ['4', '1', '2', '5', '3']); $this->assertIdentical(array_values($this->queryResults), ['6', '4', '1', '2', '5', '3', '7']);
// Sorting with 'dEsC' mixed upper and lower case // Sorting with 'dEsC' mixed upper and lower case
$this->queryResults = $this->entityStorage->getQuery() $this->queryResults = $this->entityStorage->getQuery()
->tableSort($header) ->tableSort($header)
->sort('number', 'dEsC') ->sort('number', 'dEsC')
->execute(); ->execute();
$this->assertIdentical(array_values($this->queryResults), ['3', '5', '2', '1', '4']); $this->assertIdentical(array_values($this->queryResults), ['7', '3', '5', '2', '1', '4', '6']);
// Sorting with 'aSc' mixed upper and lower case // Sorting with 'aSc' mixed upper and lower case
$this->queryResults = $this->entityStorage->getQuery() $this->queryResults = $this->entityStorage->getQuery()
->tableSort($header) ->tableSort($header)
->sort('number', 'aSc') ->sort('number', 'aSc')
->execute(); ->execute();
$this->assertIdentical(array_values($this->queryResults), ['4', '1', '2', '5', '3']); $this->assertIdentical(array_values($this->queryResults), ['6', '4', '1', '2', '5', '3', '7']);
} }
/** /**
...@@ -572,6 +593,15 @@ public function testDotted() { ...@@ -572,6 +593,15 @@ public function testDotted() {
->condition('array.level1.level2', 3) ->condition('array.level1.level2', 3)
->execute(); ->execute();
$this->assertResults(['5']); $this->assertResults(['5']);
// Test dotted sorting.
$this->queryResults = $this->entityStorage->getQuery()
->sort('array.level1.level2')
->execute();
$this->assertResults(['6', '1', '3', '2', '4', '5', '7']);
$this->queryResults = $this->entityStorage->getQuery()
->sort('array.level1.level2', 'DESC')
->execute();
$this->assertResults(['7', '5', '2', '4', '1', '3', '6']);
// Make sure that values on the wildcard level do not match if there are // Make sure that values on the wildcard level do not match if there are
// sub-keys defined. This must not find anything even if entity 2 has a // sub-keys defined. This must not find anything even if entity 2 has a
// top-level key number with value 41. // top-level key number with value 41.
...@@ -581,23 +611,22 @@ public function testDotted() { ...@@ -581,23 +611,22 @@ public function testDotted() {
$this->assertResults([]); $this->assertResults([]);
// Make sure that "IS NULL" and "IS NOT NULL" work correctly with // Make sure that "IS NULL" and "IS NOT NULL" work correctly with
// array-valued fields/keys. // array-valued fields/keys.
$all = ['1', '2', '3', '4', '5'];
$this->queryResults = $this->entityStorage->getQuery() $this->queryResults = $this->entityStorage->getQuery()
->exists('array.level1.level2') ->exists('array.level1.level2')
->execute(); ->execute();
$this->assertResults($all); $this->assertResults(['1', '2', '3', '4', '5', '7']);
$this->queryResults = $this->entityStorage->getQuery() $this->queryResults = $this->entityStorage->getQuery()
->exists('array.level1') ->exists('array.level1')
->execute(); ->execute();
$this->assertResults($all); $this->assertResults(['1', '2', '3', '4', '5', '6', '7']);
$this->queryResults = $this->entityStorage->getQuery() $this->queryResults = $this->entityStorage->getQuery()
->exists('array') ->exists('array')
->execute(); ->execute();
$this->assertResults($all); $this->assertResults(['1', '2', '3', '4', '5', '6', '7']);
$this->queryResults = $this->entityStorage->getQuery() $this->queryResults = $this->entityStorage->getQuery()
->notExists('array.level1.level2') ->notExists('array.level1.level2')
->execute(); ->execute();
$this->assertResults([]); $this->assertResults(['6']);
$this->queryResults = $this->entityStorage->getQuery() $this->queryResults = $this->entityStorage->getQuery()
->notExists('array.level1') ->notExists('array.level1')
->execute(); ->execute();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment