function FieldKernelTest::testQuery

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. 8.9.x core/modules/views/tests/src/Kernel/Handler/FieldKernelTest.php \Drupal\Tests\views\Kernel\Handler\FieldKernelTest::testQuery()
  3. 11.x 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 74

Class

FieldKernelTest
Tests the generic field handler.

Namespace

Drupal\Tests\views\Kernel\Handler

Code

public function testQuery() : void {
  // 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->assertEquals('views_test_data_job', $id_field->aliases['job']);
  $this->assertEquals('views_test_data_created', $id_field->aliases['created_test']);
  $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->assertEquals($id, $id_field->getValue($result));
    $this->assertEquals($row['job'], $id_field->getValue($result, 'job'));
    $this->assertEquals($row['created'], $id_field->getValue($result, 'created_test'));
  }
}

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