You are here

public function SelectComplexTest::testJoinConditionObject 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::testJoinConditionObject()

Test that join conditions can use Condition objects.

File

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

Class

SelectComplexTest
Tests the Select query builder with more complex queries.

Namespace

Drupal\KernelTests\Core\Database

Code

public function testJoinConditionObject() {

  // Same test as testDefaultJoin, but with a Condition object.
  $query = $this->connection
    ->select('test_task', 't');
  $join_cond = (new Condition('AND'))
    ->where('t.pid = p.id');
  $people_alias = $query
    ->join('test', 'p', $join_cond);
  $name_field = $query
    ->addField($people_alias, 'name', 'name');
  $query
    ->addField('t', 'task', 'task');
  $priority_field = $query
    ->addField('t', 'priority', 'priority');
  $query
    ->orderBy($priority_field);
  $result = $query
    ->execute();
  $num_records = 0;
  $last_priority = 0;
  foreach ($result as $record) {
    $num_records++;
    $this
      ->assertTrue($record->{$priority_field} >= $last_priority, 'Results returned in correct order.');
    $this
      ->assertNotEqual($record->{$name_field}, 'Ringo', 'Taskless person not selected.');
    $last_priority = $record->{$priority_field};
  }
  $this
    ->assertEqual($num_records, 7, 'Returned the correct number of rows.');

  // Test a condition object that creates placeholders.
  $t1_name = 'John';
  $t2_name = 'George';
  $join_cond = (new Condition('AND'))
    ->condition('t1.name', $t1_name)
    ->condition('t2.name', $t2_name);
  $query = $this->connection
    ->select('test', 't1');
  $query
    ->innerJoin('test', 't2', $join_cond);
  $query
    ->addField('t1', 'name', 't1_name');
  $query
    ->addField('t2', 'name', 't2_name');
  $num_records = $query
    ->countQuery()
    ->execute()
    ->fetchField();
  $this
    ->assertEqual($num_records, 1, 'Query expected to return 1 row. Actual: ' . $num_records);
  if ($num_records == 1) {
    $record = $query
      ->execute()
      ->fetchObject();
    $this
      ->assertEqual($record->t1_name, $t1_name, 'Query expected to retrieve name ' . $t1_name . ' from table t1. Actual: ' . $record->t1_name);
    $this
      ->assertEqual($record->t2_name, $t2_name, 'Query expected to retrieve name ' . $t2_name . ' from table t2. Actual: ' . $record->t2_name);
  }
}