View source
<?php
namespace Drupal\KernelTests\Core\Render\Element;
use Drupal\Core\Utility\TableSort;
use Drupal\KernelTests\KernelTestBase;
use Symfony\Component\HttpFoundation\Request;
class TableSortExtenderTest extends KernelTestBase {
public function testTableSortInit() {
$headers = [
'foo',
'bar',
'baz',
];
$expected_ts = [
'name' => 'foo',
'sql' => '',
'sort' => 'asc',
'query' => [],
];
$request = Request::createFromGlobals();
$request->query
->replace([]);
\Drupal::getContainer()
->get('request_stack')
->push($request);
$ts = TableSort::getContextFromRequest($headers, $request);
$this
->assertEquals($expected_ts, $ts, 'Simple table headers sorted correctly.');
$request = Request::createFromGlobals();
$request->query
->replace([
'order' => 'bar',
]);
\Drupal::getContainer()
->get('request_stack')
->push($request);
$ts = TableSort::getContextFromRequest($headers, $request);
$this
->assertEquals($expected_ts, $ts, 'Simple table headers plus non-overriding $_GET parameters sorted correctly.');
$request = Request::createFromGlobals();
$request->query
->replace([
'sort' => 'DESC',
'alpha' => 'beta',
]);
\Drupal::getContainer()
->get('request_stack')
->push($request);
$expected_ts['sort'] = 'desc';
$expected_ts['query'] = [
'alpha' => 'beta',
];
$ts = TableSort::getContextFromRequest($headers, $request);
$this
->assertEquals($expected_ts, $ts, 'Simple table headers plus $_GET parameters sorted correctly.');
$headers = [
'foo',
[
'data' => '1',
'field' => 'one',
'sort' => 'asc',
'colspan' => 1,
],
[
'data' => '2',
'field' => 'two',
'sort' => 'desc',
],
];
$request = Request::createFromGlobals();
$request->query
->replace([
'order' => '2',
]);
\Drupal::getContainer()
->get('request_stack')
->push($request);
$ts = TableSort::getContextFromRequest($headers, $request);
$expected_ts = [
'name' => '2',
'sql' => 'two',
'sort' => 'desc',
'query' => [],
];
$this
->assertEquals($expected_ts, $ts, 'Complex table headers sorted correctly.');
$request = Request::createFromGlobals();
$request->query
->replace([
'order' => 'bar',
]);
\Drupal::getContainer()
->get('request_stack')
->push($request);
$ts = TableSort::getContextFromRequest($headers, $request);
$expected_ts = [
'name' => '1',
'sql' => 'one',
'sort' => 'asc',
'query' => [],
];
$this
->assertEquals($expected_ts, $ts, 'Complex table headers plus non-overriding $_GET parameters sorted correctly.');
$request = Request::createFromGlobals();
$request->query
->replace([
'order' => '1',
'sort' => 'ASC',
'alpha' => 'beta',
]);
\Drupal::getContainer()
->get('request_stack')
->push($request);
$expected_ts = [
'name' => '1',
'sql' => 'one',
'sort' => 'asc',
'query' => [
'alpha' => 'beta',
],
];
$ts = TableSort::getContextFromRequest($headers, $request);
$this
->assertEquals($expected_ts, $ts, 'Complex table headers plus $_GET parameters sorted correctly.');
$headers = [
'foo',
[
'data' => '1',
'field' => 'one',
'initial_click_sort' => 'desc',
'colspan' => 1,
],
[
'data' => '2',
'field' => 'two',
],
[
'data' => '3',
'field' => 'three',
'initial_click_sort' => 'desc',
'sort' => 'asc',
],
[
'data' => '4',
'field' => 'four',
'initial_click_sort' => 'asc',
],
[
'data' => '5',
'field' => 'five',
'initial_click_sort' => 'foo',
],
];
$request = Request::createFromGlobals();
$request->query
->replace([
'order' => '1',
]);
\Drupal::getContainer()
->get('request_stack')
->push($request);
$ts = TableSort::getContextFromRequest($headers, $request);
$expected_ts = [
'name' => '1',
'sql' => 'one',
'sort' => 'desc',
'query' => [],
];
$this
->assertEquals($expected_ts, $ts, 'Complex table headers using the initial_click_sort parameter are sorted correctly.');
$request = Request::createFromGlobals();
$request->query
->replace([
'order' => '2',
]);
\Drupal::getContainer()
->get('request_stack')
->push($request);
$ts = TableSort::getContextFromRequest($headers, $request);
$expected_ts = [
'name' => '2',
'sql' => 'two',
'sort' => 'asc',
'query' => [],
];
$this
->assertEquals($expected_ts, $ts, 'Complex table headers without using the initial_click_sort parameter are sorted correctly.');
$request = Request::createFromGlobals();
$request->query
->replace([
'order' => '3',
]);
\Drupal::getContainer()
->get('request_stack')
->push($request);
$ts = TableSort::getContextFromRequest($headers, $request);
$expected_ts = [
'name' => '3',
'sql' => 'three',
'sort' => 'asc',
'query' => [],
];
$this
->assertEquals($expected_ts, $ts, 'Complex table headers using the initial_click_sort and sort parameters are sorted correctly.');
$request = Request::createFromGlobals();
$request->query
->replace([
'order' => '4',
]);
\Drupal::getContainer()
->get('request_stack')
->push($request);
$ts = TableSort::getContextFromRequest($headers, $request);
$expected_ts = [
'name' => '4',
'sql' => 'four',
'sort' => 'asc',
'query' => [],
];
$this
->assertEquals($expected_ts, $ts, 'Complex table headers with the initial_click_sort set as ASC are sorted correctly.');
$request = Request::createFromGlobals();
$request->query
->replace([
'order' => '5',
]);
\Drupal::getContainer()
->get('request_stack')
->push($request);
$ts = TableSort::getContextFromRequest($headers, $request);
$expected_ts = [
'name' => '5',
'sql' => 'five',
'sort' => 'foo',
'query' => [],
];
$this
->assertEquals($expected_ts, $ts, 'Complex table headers with the initial_click_sort set as foo are sorted correctly.');
}
}