function AlterTest::testSimpleAlterSubquery in Zircon Profile 8
Same name and namespace in other branches
- 8.0 core/modules/system/src/Tests/Database/AlterTest.php \Drupal\system\Tests\Database\AlterTest::testSimpleAlterSubquery()
Tests that we can do basic alters on subqueries.
File
- core/
modules/ system/ src/ Tests/ Database/ AlterTest.php, line 134 - Contains \Drupal\system\Tests\Database\AlterTest.
Class
- AlterTest
- Tests the hook_query_alter capabilities of the Select builder.
Namespace
Drupal\system\Tests\DatabaseCode
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.');
}