You are here

public function FieldKernelTest::testQuery in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/views/tests/src/Kernel/Handler/FieldKernelTest.php \Drupal\Tests\views\Kernel\Handler\FieldKernelTest::testQuery()
  2. 10 core/modules/views/tests/src/Kernel/Handler/FieldKernelTest.php \Drupal\Tests\views\Kernel\Handler\FieldKernelTest::testQuery()

Tests all things related to the query.

File

core/modules/views/tests/src/Kernel/Handler/FieldKernelTest.php, line 69

Class

FieldKernelTest
Tests the generic field handler.

Namespace

Drupal\Tests\views\Kernel\Handler

Code

public function testQuery() {

  // Tests adding additional fields to the query.
  $view = Views::getView('test_view');
  $view
    ->initHandlers();
  $id_field = $view->field['id'];
  $id_field->additional_fields['job'] = 'job';

  // Choose also a field alias key which doesn't match to the table field.
  $id_field->additional_fields['created_test'] = [
    'table' => 'views_test_data',
    'field' => 'created',
  ];
  $view
    ->build();

  // Make sure the field aliases have the expected value.
  $this
    ->assertEqual($id_field->aliases['job'], 'views_test_data_job');
  $this
    ->assertEqual($id_field->aliases['created_test'], 'views_test_data_created');
  $this
    ->executeView($view);

  // Tests the getValue method with and without a field aliases.
  foreach ($this
    ->dataSet() as $key => $row) {
    $id = $key + 1;
    $result = $view->result[$key];
    $this
      ->assertEqual($id_field
      ->getValue($result), $id);
    $this
      ->assertEqual($id_field
      ->getValue($result, 'job'), $row['job']);
    $this
      ->assertEqual($id_field
      ->getValue($result, 'created_test'), $row['created']);
  }
}