function ConfigEntityQueryTest::testTableSort

Same name in other branches
  1. 8.9.x core/tests/Drupal/KernelTests/Core/Entity/ConfigEntityQueryTest.php \Drupal\KernelTests\Core\Entity\ConfigEntityQueryTest::testTableSort()
  2. 10 core/tests/Drupal/KernelTests/Core/Entity/ConfigEntityQueryTest.php \Drupal\KernelTests\Core\Entity\ConfigEntityQueryTest::testTableSort()
  3. 11.x core/tests/Drupal/KernelTests/Core/Entity/ConfigEntityQueryTest.php \Drupal\KernelTests\Core\Entity\ConfigEntityQueryTest::testTableSort()

Tests sorting with tableSort on config entity queries.

File

core/tests/Drupal/KernelTests/Core/Entity/ConfigEntityQueryTest.php, line 514

Class

ConfigEntityQueryTest
Tests Config Entity Query functionality.

Namespace

Drupal\KernelTests\Core\Entity

Code

public function testTableSort() {
    $header = [
        [
            'data' => 'ID',
            'specifier' => 'id',
        ],
        [
            'data' => 'Number',
            'specifier' => 'number',
        ],
    ];
    // Sort key: id
    // Sorting with 'DESC' upper case
    $this->queryResults = $this->entityStorage
        ->getQuery()
        ->tableSort($header)
        ->sort('id', 'DESC')
        ->execute();
    $this->assertSame([
        '7',
        '6',
        '5',
        '4',
        '3',
        '2',
        '1',
    ], array_values($this->queryResults));
    // Sorting with 'ASC' upper case
    $this->queryResults = $this->entityStorage
        ->getQuery()
        ->tableSort($header)
        ->sort('id', 'ASC')
        ->execute();
    $this->assertSame([
        '1',
        '2',
        '3',
        '4',
        '5',
        '6',
        '7',
    ], array_values($this->queryResults));
    // Sorting with 'desc' lower case
    $this->queryResults = $this->entityStorage
        ->getQuery()
        ->tableSort($header)
        ->sort('id', 'desc')
        ->execute();
    $this->assertSame([
        '7',
        '6',
        '5',
        '4',
        '3',
        '2',
        '1',
    ], array_values($this->queryResults));
    // Sorting with 'asc' lower case
    $this->queryResults = $this->entityStorage
        ->getQuery()
        ->tableSort($header)
        ->sort('id', 'asc')
        ->execute();
    $this->assertSame([
        '1',
        '2',
        '3',
        '4',
        '5',
        '6',
        '7',
    ], array_values($this->queryResults));
    // Sort key: number
    // Sorting with 'DeSc' mixed upper and lower case
    $this->queryResults = $this->entityStorage
        ->getQuery()
        ->tableSort($header)
        ->sort('number', 'DeSc')
        ->execute();
    $this->assertSame([
        '7',
        '3',
        '5',
        '2',
        '1',
        '4',
        '6',
    ], array_values($this->queryResults));
    // Sorting with 'AsC' mixed upper and lower case
    $this->queryResults = $this->entityStorage
        ->getQuery()
        ->tableSort($header)
        ->sort('number', 'AsC')
        ->execute();
    $this->assertSame([
        '6',
        '4',
        '1',
        '2',
        '5',
        '3',
        '7',
    ], array_values($this->queryResults));
    // Sorting with 'dEsC' mixed upper and lower case
    $this->queryResults = $this->entityStorage
        ->getQuery()
        ->tableSort($header)
        ->sort('number', 'dEsC')
        ->execute();
    $this->assertSame([
        '7',
        '3',
        '5',
        '2',
        '1',
        '4',
        '6',
    ], array_values($this->queryResults));
    // Sorting with 'aSc' mixed upper and lower case
    $this->queryResults = $this->entityStorage
        ->getQuery()
        ->tableSort($header)
        ->sort('number', 'aSc')
        ->execute();
    $this->assertSame([
        '6',
        '4',
        '1',
        '2',
        '5',
        '3',
        '7',
    ], array_values($this->queryResults));
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.