You are here

function DatabaseInsertTestCase::testInsertSelectFields in Drupal 7

Test that the INSERT INTO ... SELECT (fields) ... syntax works.

File

modules/simpletest/tests/database_test.test, line 615

Class

DatabaseInsertTestCase
Test the insert builder.

Code

function testInsertSelectFields() {
  $query = db_select('test_people', 'tp');

  // The query builder will always append expressions after fields.
  // Add the expression first to test that the insert fields are correctly
  // re-ordered.
  $query
    ->addExpression('tp.age', 'age');
  $query
    ->fields('tp', array(
    'name',
    'job',
  ))
    ->condition('tp.name', 'Meredith');

  // The resulting query should be equivalent to:
  // INSERT INTO test (age, name, job)
  // SELECT tp.age AS age, tp.name AS name, tp.job AS job
  // FROM test_people tp
  // WHERE tp.name = 'Meredith'
  db_insert('test')
    ->from($query)
    ->execute();
  $saved_age = db_query('SELECT age FROM {test} WHERE name = :name', array(
    ':name' => 'Meredith',
  ))
    ->fetchField();
  $this
    ->assertIdentical($saved_age, '30', 'Can retrieve after inserting.');
}