class TemporaryQueryTest
Same name and namespace in other branches
- 11.x core/modules/sqlite/tests/src/Kernel/sqlite/TemporaryQueryTest.php \Drupal\Tests\sqlite\Kernel\sqlite\TemporaryQueryTest
- 11.x core/modules/mysql/tests/src/Kernel/mysql/TemporaryQueryTest.php \Drupal\Tests\mysql\Kernel\mysql\TemporaryQueryTest
- 11.x core/modules/pgsql/tests/src/Kernel/pgsql/TemporaryQueryTest.php \Drupal\Tests\pgsql\Kernel\pgsql\TemporaryQueryTest
- 11.x core/modules/mysqli/tests/src/Kernel/mysqli/TemporaryQueryTest.php \Drupal\Tests\mysqli\Kernel\mysqli\TemporaryQueryTest
- 10 core/modules/sqlite/tests/src/Kernel/sqlite/TemporaryQueryTest.php \Drupal\Tests\sqlite\Kernel\sqlite\TemporaryQueryTest
- 10 core/modules/mysql/tests/src/Kernel/mysql/TemporaryQueryTest.php \Drupal\Tests\mysql\Kernel\mysql\TemporaryQueryTest
- 10 core/modules/pgsql/tests/src/Kernel/pgsql/TemporaryQueryTest.php \Drupal\Tests\pgsql\Kernel\pgsql\TemporaryQueryTest
- 8.9.x core/modules/system/tests/src/Functional/Database/TemporaryQueryTest.php \Drupal\Tests\system\Functional\Database\TemporaryQueryTest
Tests the temporary query functionality.
@group Database @group legacy
Hierarchy
- class \Drupal\Tests\BrowserTestBase uses \Drupal\Core\Test\FunctionalTestSetupTrait, \Drupal\Tests\UiHelperTrait, \Drupal\Core\Test\TestSetupTrait, \Drupal\Tests\block\Traits\BlockCreationTrait, \Drupal\FunctionalTests\AssertLegacyTrait, \Drupal\Tests\RandomGeneratorTrait, \Drupal\Tests\node\Traits\NodeCreationTrait, \Drupal\Tests\node\Traits\ContentTypeCreationTrait, \Drupal\Tests\ConfigTestTrait, \Drupal\Tests\TestRequirementsTrait, \Drupal\Tests\user\Traits\UserCreationTrait, \Drupal\Tests\XdebugRequestTrait, \Drupal\Tests\Traits\PhpUnitWarnings, \Drupal\Tests\PhpUnitCompatibilityTrait, \Symfony\Bridge\PhpUnit\ExpectDeprecationTrait, \Drupal\Tests\ExtensionListTestTrait extends \PHPUnit\Framework\TestCase
- class \Drupal\Tests\system\Functional\Database\DatabaseTestBase uses \Drupal\KernelTests\Core\Database\DatabaseTestSchemaDataTrait extends \Drupal\Tests\BrowserTestBase
- class \Drupal\Tests\system\Functional\Database\TemporaryQueryTest extends \Drupal\Tests\system\Functional\Database\DatabaseTestBase
- class \Drupal\Tests\system\Functional\Database\DatabaseTestBase uses \Drupal\KernelTests\Core\Database\DatabaseTestSchemaDataTrait extends \Drupal\Tests\BrowserTestBase
Expanded class hierarchy of TemporaryQueryTest
File
-
core/
modules/ system/ tests/ src/ Functional/ Database/ TemporaryQueryTest.php, line 13
Namespace
Drupal\Tests\system\Functional\DatabaseView source
class TemporaryQueryTest extends DatabaseTestBase {
/**
* {@inheritdoc}
*/
protected static $modules = [
'database_test',
];
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* Returns the number of rows of a table.
*/
public function countTableRows($table_name) {
return Database::getConnection()->select($table_name)
->countQuery()
->execute()
->fetchField();
}
/**
* Confirms that temporary tables work and are limited to one request.
*/
public function testTemporaryQuery() {
$this->expectDeprecation('Connection::generateTemporaryTableName() is deprecated in drupal:9.3.0 and is removed from drupal:10.0.0. There is no replacement. See https://www.drupal.org/node/3211781');
$connection = Database::getConnection();
$this->drupalGet('database_test/db_query_temporary');
$data = json_decode($this->getSession()
->getPage()
->getContent());
if ($data) {
$this->assertEquals($this->countTableRows('test'), $data->row_count, 'The temporary table contains the correct amount of rows.');
$this->assertFalse($connection->schema()
->tableExists($data->table_name), 'The temporary table is, indeed, temporary.');
}
else {
$this->fail('The creation of the temporary table failed.');
}
// Now try to run two temporary queries in the same request.
$table_name_test = $connection->queryTemporary('SELECT [name] FROM {test}', []);
$table_name_task = $connection->queryTemporary('SELECT [pid] FROM {test_task}', []);
$this->assertEquals($this->countTableRows('test'), $this->countTableRows($table_name_test), 'A temporary table was created successfully in this request.');
$this->assertEquals($this->countTableRows('test_task'), $this->countTableRows($table_name_task), 'A second temporary table was created successfully in this request.');
// Check that leading whitespace and comments do not cause problems
// in the modified query.
$sql = "\n -- Let's select some rows into a temporary table\n SELECT [name] FROM {test}\n ";
$table_name_test = $connection->queryTemporary($sql, []);
$this->assertEquals($this->countTableRows('test'), $this->countTableRows($table_name_test), 'Leading white space and comments do not interfere with temporary table creation.');
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.