function SortDateTimeTest::testDateTimeSort

Same name in other branches
  1. 9 core/modules/datetime/tests/src/Kernel/Views/SortDateTimeTest.php \Drupal\Tests\datetime\Kernel\Views\SortDateTimeTest::testDateTimeSort()
  2. 8.9.x core/modules/datetime/tests/src/Kernel/Views/SortDateTimeTest.php \Drupal\Tests\datetime\Kernel\Views\SortDateTimeTest::testDateTimeSort()
  3. 11.x core/modules/datetime/tests/src/Kernel/Views/SortDateTimeTest.php \Drupal\Tests\datetime\Kernel\Views\SortDateTimeTest::testDateTimeSort()

Tests the datetime sort handler.

File

core/modules/datetime/tests/src/Kernel/Views/SortDateTimeTest.php, line 54

Class

SortDateTimeTest
Tests for core Drupal\datetime\Plugin\views\sort\Date handler.

Namespace

Drupal\Tests\datetime\Kernel\Views

Code

public function testDateTimeSort() : void {
    $field = static::$fieldName . '_value';
    $view = Views::getView('test_sort_datetime');
    // Set granularity to 'minute', and the secondary node ID order should
    // define the order of nodes with the same minute.
    $view->initHandlers();
    $view->sort[$field]->options['granularity'] = 'minute';
    $view->setDisplay('default');
    $this->executeView($view);
    $expected_result = [
        [
            'nid' => $this->nodes[0]
                ->id(),
        ],
        [
            'nid' => $this->nodes[3]
                ->id(),
        ],
        [
            'nid' => $this->nodes[4]
                ->id(),
        ],
        [
            'nid' => $this->nodes[5]
                ->id(),
        ],
        [
            'nid' => $this->nodes[6]
                ->id(),
        ],
        [
            'nid' => $this->nodes[2]
                ->id(),
        ],
        [
            'nid' => $this->nodes[1]
                ->id(),
        ],
    ];
    $this->assertIdenticalResultset($view, $expected_result, $this->map);
    $view->destroy();
    // Check ASC.
    $view->initHandlers();
    $field = static::$fieldName . '_value';
    $view->sort[$field]->options['order'] = 'ASC';
    $view->setDisplay('default');
    $this->executeView($view);
    $expected_result = [
        [
            'nid' => $this->nodes[1]
                ->id(),
        ],
        [
            'nid' => $this->nodes[2]
                ->id(),
        ],
        [
            'nid' => $this->nodes[3]
                ->id(),
        ],
        [
            'nid' => $this->nodes[5]
                ->id(),
        ],
        [
            'nid' => $this->nodes[4]
                ->id(),
        ],
        [
            'nid' => $this->nodes[6]
                ->id(),
        ],
        [
            'nid' => $this->nodes[0]
                ->id(),
        ],
    ];
    $this->assertIdenticalResultset($view, $expected_result, $this->map);
    $view->destroy();
    // Change granularity to 'year', and the secondary node ID order should
    // define the order of nodes with the same year.
    $view->initHandlers();
    $view->sort[$field]->options['granularity'] = 'year';
    $view->sort[$field]->options['order'] = 'DESC';
    $view->setDisplay('default');
    $this->executeView($view);
    $expected_result = [
        [
            'nid' => $this->nodes[0]
                ->id(),
        ],
        [
            'nid' => $this->nodes[1]
                ->id(),
        ],
        [
            'nid' => $this->nodes[2]
                ->id(),
        ],
        [
            'nid' => $this->nodes[3]
                ->id(),
        ],
        [
            'nid' => $this->nodes[4]
                ->id(),
        ],
        [
            'nid' => $this->nodes[5]
                ->id(),
        ],
        [
            'nid' => $this->nodes[6]
                ->id(),
        ],
    ];
    $this->assertIdenticalResultset($view, $expected_result, $this->map);
    $view->destroy();
    // Change granularity to 'second'.
    $view->initHandlers();
    $view->sort[$field]->options['granularity'] = 'second';
    $view->sort[$field]->options['order'] = 'DESC';
    $view->setDisplay('default');
    $this->executeView($view);
    $expected_result = [
        [
            'nid' => $this->nodes[0]
                ->id(),
        ],
        [
            'nid' => $this->nodes[6]
                ->id(),
        ],
        [
            'nid' => $this->nodes[4]
                ->id(),
        ],
        [
            'nid' => $this->nodes[5]
                ->id(),
        ],
        [
            'nid' => $this->nodes[3]
                ->id(),
        ],
        [
            'nid' => $this->nodes[2]
                ->id(),
        ],
        [
            'nid' => $this->nodes[1]
                ->id(),
        ],
    ];
    $this->assertIdenticalResultset($view, $expected_result, $this->map);
    $view->destroy();
}

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