function UpdateComplexTest::testSubSelectUpdate

Test UPDATE with a subselect value.

File

drupal/core/modules/system/lib/Drupal/system/Tests/Database/UpdateComplexTest.php, line 139
Definition of Drupal\system\Tests\Database\UpdateComplexTest.

Class

UpdateComplexTest
Tests for more complex update statements.

Namespace

Drupal\system\Tests\Database

Code

function testSubSelectUpdate() {
  $subselect = db_select('test_task', 't');
  $subselect
    ->addExpression('MAX(priority) + :increment', 'max_priority', array(
    ':increment' => 30,
  ));

  // Clone this to make sure we are running a different query when
  // asserting.
  $select = clone $subselect;
  $query = db_update('test')
    ->expression('age', $subselect)
    ->condition('name', 'Ringo');

  // Save the query for a __toString test later.
  $string_test = $query;
  $query
    ->execute();
  $after_age = db_query('SELECT age FROM {test} WHERE name = :name', array(
    ':name' => 'Ringo',
  ))
    ->fetchField();
  $expected_age = $select
    ->execute()
    ->fetchField();
  $this
    ->assertEqual($after_age, $expected_age);

  // Replace whitespace with a single space.
  $query_string = preg_replace('/\\s+/', ' ', $string_test);
  $this
    ->assertIdentical('UPDATE {test} SET age= (SELECT MAX(priority) + :increment AS max_priority FROM {test_task} t) WHERE (name = :db_condition_placeholder_0)', trim($query_string));
}