You are here

public function SqlQueryTest::testExecuteMetadata in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/views/src/Tests/Plugin/SqlQueryTest.php \Drupal\views\Tests\Plugin\SqlQueryTest::testExecuteMetadata()

Tests adding some metadata/tags to the views query.

File

core/modules/views/src/Tests/Plugin/SqlQueryTest.php, line 42
Contains \Drupal\views\Tests\Plugin\SqlQueryTest.

Class

SqlQueryTest
Tests the sql query plugin.

Namespace

Drupal\views\Tests\Plugin

Code

public function testExecuteMetadata() {
  $view = Views::getView('test_view');
  $view
    ->setDisplay();
  $view
    ->initQuery();
  $view
    ->execute();

  /** @var \Drupal\Core\Database\Query\Select $query */
  $main_query = $view->build_info['query'];

  /** @var \Drupal\Core\Database\Query\Select $count_query */
  $count_query = $view->build_info['count_query'];
  foreach (array(
    $main_query,
    $count_query,
  ) as $query) {

    // Check query access tags.
    $this
      ->assertTrue($query
      ->hasTag('test_tag'));

    // Check metadata.
    $this
      ->assertIdentical($query
      ->getMetaData('key1'), 'test_metadata');
    $this
      ->assertIdentical($query
      ->getMetaData('key2'), 'test_metadata2');
  }
  $query_options = $view->display_handler
    ->getOption('query');
  $query_options['options']['disable_sql_rewrite'] = TRUE;
  $view->display_handler
    ->setOption('query', $query_options);
  $view
    ->save();
  $view
    ->destroy();
  $view = Views::getView('test_view');
  $view
    ->setDisplay();
  $view
    ->initQuery();
  $view
    ->execute();

  /** @var \Drupal\Core\Database\Query\Select $query */
  $main_query = $view->build_info['query'];

  /** @var \Drupal\Core\Database\Query\Select $count_query */
  $count_query = $view->build_info['count_query'];
  foreach (array(
    $main_query,
    $count_query,
  ) as $query) {

    // Check query access tags.
    $this
      ->assertFalse($query
      ->hasTag('test_tag'));

    // Check metadata.
    $this
      ->assertIdentical($query
      ->getMetaData('key1'), NULL);
    $this
      ->assertIdentical($query
      ->getMetaData('key2'), NULL);
  }
}