function SqlQueryTest::testExecuteMetadata

Same name in other branches
  1. 9 core/modules/views/tests/src/Kernel/Plugin/SqlQueryTest.php \Drupal\Tests\views\Kernel\Plugin\SqlQueryTest::testExecuteMetadata()
  2. 8.9.x core/modules/views/tests/src/Kernel/Plugin/SqlQueryTest.php \Drupal\Tests\views\Kernel\Plugin\SqlQueryTest::testExecuteMetadata()
  3. 10 core/modules/views/tests/src/Kernel/Plugin/SqlQueryTest.php \Drupal\Tests\views\Kernel\Plugin\SqlQueryTest::testExecuteMetadata()

Tests adding some metadata/tags to the views query.

File

core/modules/views/tests/src/Kernel/Plugin/SqlQueryTest.php, line 40

Class

SqlQueryTest
Tests the sql query plugin.

Namespace

Drupal\Tests\views\Kernel\Plugin

Code

public function testExecuteMetadata() : void {
    $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 ([
        $main_query,
        $count_query,
    ] as $query) {
        // Check query access tags.
        $this->assertTrue($query->hasTag('test_tag'));
        // Check metadata.
        $this->assertSame('test_metadata', $query->getMetaData('key1'));
        $this->assertSame('test_metadata2', $query->getMetaData('key2'));
    }
    $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 ([
        $main_query,
        $count_query,
    ] as $query) {
        // Check query access tags.
        $this->assertFalse($query->hasTag('test_tag'));
        // Check metadata.
        $this->assertNull($query->getMetaData('key1'));
        $this->assertNull($query->getMetaData('key2'));
    }
}

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