function AlterTest::testSimpleAlterSubquery

Same name in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Database/AlterTest.php \Drupal\KernelTests\Core\Database\AlterTest::testSimpleAlterSubquery()
  2. 8.9.x core/tests/Drupal/KernelTests/Core/Database/AlterTest.php \Drupal\KernelTests\Core\Database\AlterTest::testSimpleAlterSubquery()
  3. 10 core/tests/Drupal/KernelTests/Core/Database/AlterTest.php \Drupal\KernelTests\Core\Database\AlterTest::testSimpleAlterSubquery()

Tests that we can do basic alters on subqueries.

File

core/tests/Drupal/KernelTests/Core/Database/AlterTest.php, line 131

Class

AlterTest
Tests the hook_query_alter capabilities of the Select builder.

Namespace

Drupal\KernelTests\Core\Database

Code

public function testSimpleAlterSubquery() : void {
    // Create a sub-query with an alter tag.
    $subquery = $this->connection
        ->select('test', 'p');
    $subquery->addField('p', 'name');
    $subquery->addField('p', 'id');
    // Pick out George.
    $subquery->condition('age', 27);
    $subquery->addExpression("[age]*2", 'double_age');
    // This query alter should change it to age * 3.
    $subquery->addTag('database_test_alter_change_expressions');
    // Create a main query and join to sub-query.
    $query = $this->connection
        ->select('test_task', 'tt');
    $query->join($subquery, 'pq', '[pq].[id] = [tt].[pid]');
    $age_field = $query->addField('pq', 'double_age');
    $name_field = $query->addField('pq', 'name');
    $record = $query->execute()
        ->fetch();
    $this->assertEquals('George', $record->{$name_field}, 'Fetched name is correct.');
    $this->assertEquals(27 * 3, $record->{$age_field}, 'Fetched age expression is correct.');
}

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