View source
<?php
namespace Drupal\Tests\log\Kernel;
use Drupal\Tests\log\Traits\LogCreationTrait;
use Drupal\Tests\views\Kernel\ViewsKernelTestBase;
use Drupal\views\Tests\ViewTestData;
use Drupal\views\Views;
class SortTimestampIdTest extends ViewsKernelTestBase {
use LogCreationTrait;
public static $modules = [
'log',
'log_test',
'datetime',
'state_machine',
];
public static $testViews = [
'log_test_view',
];
protected $expectedResultASC = [];
protected $expectedResultDESC = [];
protected function setUp($import_test_views = TRUE) {
parent::setUp();
$this
->installEntitySchema('log');
$this
->installConfig([
'log',
'log_test',
]);
ViewTestData::createTestViews(get_class($this), [
'log_test',
]);
$first_timestamp = 376185600;
$second_timestamp = 386121600;
$first_entity = $this
->createLogEntity([
'name' => 'First',
'timestamp' => $first_timestamp,
]);
$second_entity = $this
->createLogEntity([
'name' => 'Second',
'timestamp' => $first_timestamp,
]);
$third_entity = $this
->createLogEntity([
'name' => 'Third',
'timestamp' => $second_timestamp,
]);
$this->expectedResultASC = [
[
'name' => $first_entity
->get('name')->value,
'id' => $first_entity
->id(),
],
[
'name' => $second_entity
->get('name')->value,
'id' => $second_entity
->id(),
],
[
'name' => $third_entity
->get('name')->value,
'id' => $third_entity
->id(),
],
];
$this->expectedResultDESC = [
[
'name' => $third_entity
->get('name')->value,
'id' => $third_entity
->id(),
],
[
'name' => $second_entity
->get('name')->value,
'id' => $second_entity
->id(),
],
[
'name' => $first_entity
->get('name')->value,
'id' => $first_entity
->id(),
],
];
}
public function testLogTimestampIdAscSort() {
$view = Views::getView('log_test_view');
$view
->setDisplay();
$view->displayHandlers
->get('default')
->overrideOption('sorts', [
'timestamp' => [
'id' => 'timestamp',
'table' => 'log_field_data',
'field' => 'timestamp',
'relationship' => 'none',
'order' => 'ASC',
'plugin_id' => 'log_standard',
],
]);
$this
->executeView($view);
$this
->assertEqual(3, count($view->result), 'The number of returned rows match.');
$this
->assertIdenticalResultset($view, $this->expectedResultASC, [
'name' => 'name',
'id' => 'id',
], 'ASC sort displays as expected');
$view
->destroy();
unset($view);
}
public function testLogTimestampIdDescSort() {
$view = Views::getView('log_test_view');
$view
->setDisplay();
$view->displayHandlers
->get('default')
->overrideOption('sorts', [
'timestamp' => [
'id' => 'timestamp',
'table' => 'log_field_data',
'field' => 'timestamp',
'relationship' => 'none',
'order' => 'DESC',
'plugin_id' => 'log_standard',
],
]);
$this
->executeView($view);
$this
->assertEqual(3, count($view->result), 'The number of returned rows match.');
$this
->assertIdenticalResultset($view, $this->expectedResultDESC, [
'name' => 'name',
'id' => 'id',
], 'DESC sort displays as expected');
$view
->destroy();
unset($view);
}
}