You are here

function SelectComplexTest::testJoinSubquery in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/system/src/Tests/Database/SelectComplexTest.php \Drupal\system\Tests\Database\SelectComplexTest::testJoinSubquery()

Tests that we can join on a query.

File

core/modules/system/src/Tests/Database/SelectComplexTest.php, line 328
Contains \Drupal\system\Tests\Database\SelectComplexTest.

Class

SelectComplexTest
Tests the Select query builder with more complex queries.

Namespace

Drupal\system\Tests\Database

Code

function testJoinSubquery() {
  $this
    ->installSchema('system', 'sequences');
  $account = entity_create('user', array(
    'name' => $this
      ->randomMachineName(),
    'mail' => $this
      ->randomMachineName() . '@example.com',
  ));
  $query = db_select('test_task', 'tt', array(
    'target' => 'replica',
  ));
  $query
    ->addExpression('tt.pid + 1', 'abc');
  $query
    ->condition('priority', 1, '>');
  $query
    ->condition('priority', 100, '<');
  $subquery = db_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.');
}