You are here

public function SelectComplexTest::testJoinSubquery in Drupal 8

Same name and namespace in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Database/SelectComplexTest.php \Drupal\KernelTests\Core\Database\SelectComplexTest::testJoinSubquery()

Tests that we can join on a query.

File

core/tests/Drupal/KernelTests/Core/Database/SelectComplexTest.php, line 339

Class

SelectComplexTest
Tests the Select query builder with more complex queries.

Namespace

Drupal\KernelTests\Core\Database

Code

public function testJoinSubquery() {
  $this
    ->installSchema('system', 'sequences');
  $account = User::create([
    'name' => $this
      ->randomMachineName(),
    'mail' => $this
      ->randomMachineName() . '@example.com',
  ]);
  $query = Database::getConnection('replica')
    ->select('test_task', 'tt');
  $query
    ->addExpression('tt.pid + 1', 'abc');
  $query
    ->condition('priority', 1, '>');
  $query
    ->condition('priority', 100, '<');
  $subquery = $this->connection
    ->select('test', 'tp');
  $subquery
    ->join('test_one_blob', 'tpb', 'tp.id = tpb.id');
  $subquery
    ->join('node', 'n', 'tp.id = n.nid');
  $subquery
    ->addTag('node_access');
  $subquery
    ->addMetaData('account', $account);
  $subquery
    ->addField('tp', 'id');
  $subquery
    ->condition('age', 5, '>');
  $subquery
    ->condition('age', 500, '<');
  $query
    ->leftJoin($subquery, 'sq', 'tt.pid = sq.id');
  $query
    ->join('test_one_blob', 'tb3', 'tt.pid = tb3.id');

  // Construct the query string.
  // This is the same sequence that SelectQuery::execute() goes through.
  $query
    ->preExecute();
  $query
    ->getArguments();
  $str = (string) $query;

  // Verify that the string only has one copy of condition placeholder 0.
  $pos = strpos($str, 'db_condition_placeholder_0', 0);
  $pos2 = strpos($str, 'db_condition_placeholder_0', $pos + 1);
  $this
    ->assertFalse($pos2, 'Condition placeholder is not repeated.');
}