function DatabaseAlterTestCase::testSimpleAlterSubquery

7.x database_test.test DatabaseAlterTestCase::testSimpleAlterSubquery()

Test that we can do basic alters on subqueries.

File

drupal-7.x/modules/simpletest/tests/database_test.test, line 2783

Class

DatabaseAlterTestCase
Select alter tests.

Code

function testSimpleAlterSubquery() {
  // Create a sub-query with an alter tag.
  $subquery = db_select('test', 'p');
  $subquery->addField('p', 'name');
  $subquery->addField('p', 'id');
  // Pick out George.
  $subquery->condition('age', 27);
  $subquery->addExpression("age*2", 'double_age');
  // This query alter should change it to age * 3.
  $subquery->addTag('database_test_alter_change_expressions');

  // Create a main query and join to sub-query.
  $query = db_select('test_task', 'tt');
  $query->join($subquery, 'pq', 'pq.id = tt.pid');
  $age_field = $query->addField('pq', 'double_age');
  $name_field = $query->addField('pq', 'name');

  $record = $query->execute()->fetch();
  $this->assertEqual($record->$name_field, 'George', 'Fetched name is correct.');
  $this->assertEqual($record->$age_field, 27 * 3, 'Fetched age expression is correct.');
}