You are here

function UpdateComplexTest::testSubSelectUpdate in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/modules/system/src/Tests/Database/UpdateComplexTest.php \Drupal\system\Tests\Database\UpdateComplexTest::testSubSelectUpdate()

Test UPDATE with a subselect value.

File

core/modules/system/src/Tests/Database/UpdateComplexTest.php, line 133
Contains \Drupal\system\Tests\Database\UpdateComplexTest.

Class

UpdateComplexTest
Tests the Update query builder, complex queries.

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 number of rows that updated for assertion later.
  $num_updated = $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);
  $this
    ->assertEqual(1, $num_updated, t('Expected 1 row to be updated in subselect update query.'));
}