diff --git a/core/tests/Drupal/KernelTests/Core/Database/Driver/mysql/LargeQueryTest.php b/core/tests/Drupal/KernelTests/Core/Database/Driver/mysql/LargeQueryTest.php new file mode 100644 index 0000000000000000000000000000000000000000..e9a0f922138bafd429a5e2f013e6e8a0948d72ac --- /dev/null +++ b/core/tests/Drupal/KernelTests/Core/Database/Driver/mysql/LargeQueryTest.php @@ -0,0 +1,47 @@ +<?php + +namespace Drupal\KernelTests\Core\Database\Driver\mysql; + +use Drupal\Component\Utility\Environment; +use Drupal\Core\Database\Database; +use Drupal\Core\Database\DatabaseException; + +/** + * Tests handling of large queries. + * + * @group Database + */ +class LargeQueryTest extends MySqlDriverTestBase { + + /** + * Tests truncation of messages when max_allowed_packet exception occurs. + */ + public function testMaxAllowedPacketQueryTruncating() { + // The max_allowed_packet value is configured per database instance. + // Retrieve the max_allowed_packet value from the current instance and + // check if PHP is configured with sufficient allowed memory to be able + // to generate a query larger than max_allowed_packet. + $max_allowed_packet = $this->connection->query('SELECT @@global.max_allowed_packet')->fetchField(); + if (!Environment::checkMemoryLimit($max_allowed_packet + (16 * 1024 * 1024))) { + $this->markTestSkipped('The configured max_allowed_packet exceeds the php memory limit. Therefore the test is skipped.'); + } + + $long_name = str_repeat('a', $max_allowed_packet + 1); + try { + $this->connection->query('SELECT name FROM {test} WHERE name = :name', [':name' => $long_name]); + $this->fail("An exception should be thrown for queries larger than 'max_allowed_packet'"); + } + catch (DatabaseException $e) { + // Close and re-open the connection. Otherwise we will run into error + // 2006 "MySQL server had gone away" afterwards. + Database::closeConnection(); + Database::getConnection(); + // Got a packet bigger than 'max_allowed_packet' bytes exception thrown. + $this->assertEquals(1153, $e->getPrevious()->errorInfo[1]); + // 'max_allowed_packet' exception message truncated. + // Use strlen() to count the bytes exactly, not the unicode chars. + $this->assertLessThanOrEqual($max_allowed_packet, strlen($e->getMessage())); + } + } + +} diff --git a/core/tests/Drupal/KernelTests/Core/Database/Driver/mysql/MySqlDriverTestBase.php b/core/tests/Drupal/KernelTests/Core/Database/Driver/mysql/MySqlDriverTestBase.php new file mode 100644 index 0000000000000000000000000000000000000000..abff7d07ab7cedeff1476d0bc804df311f8a6318 --- /dev/null +++ b/core/tests/Drupal/KernelTests/Core/Database/Driver/mysql/MySqlDriverTestBase.php @@ -0,0 +1,25 @@ +<?php + +namespace Drupal\KernelTests\Core\Database\Driver\mysql; + +use Drupal\KernelTests\Core\Database\DatabaseTestBase; + +/** + * Base class for MySql driver-specific database tests. + */ +class MySqlDriverTestBase extends DatabaseTestBase { + + /** + * {@inheritdoc} + */ + protected function setUp() { + parent::setUp(); + + // Only run this test for the 'mysql' driver. + $driver = $this->connection->driver(); + if ($driver !== 'mysql') { + $this->markTestSkipped("MySql tests can not run for driver '$driver'."); + } + } + +} diff --git a/core/tests/Drupal/KernelTests/Core/Database/Driver/mysql/PrefixInfoTest.php b/core/tests/Drupal/KernelTests/Core/Database/Driver/mysql/PrefixInfoTest.php new file mode 100644 index 0000000000000000000000000000000000000000..5bb9a497a5a8fa95d4ce236f61a9bb65ed016228 --- /dev/null +++ b/core/tests/Drupal/KernelTests/Core/Database/Driver/mysql/PrefixInfoTest.php @@ -0,0 +1,67 @@ +<?php + +namespace Drupal\KernelTests\Core\Database\Driver\mysql; + +use Drupal\Core\Database\Database; + +/** + * Tests that the prefix info for a database schema is correct. + * + * @group Database + */ +class PrefixInfoTest extends MySqlDriverTestBase { + + /** + * Tests that DatabaseSchema::getPrefixInfo() returns the right database. + * + * We are testing if the return array of the method + * \Drupal\Core\Database\Driver\mysql\Schema::getPrefixInfo(). This return + * array is a keyed array with info about amongst other things the database. + * The other two by Drupal core supported databases do not have this variable + * set in the return array. + */ + public function testGetPrefixInfo() { + $connection_info = Database::getConnectionInfo('default'); + + // Copy the default connection info to the 'extra' key. + Database::addConnectionInfo('extra', 'default', $connection_info['default']); + + $db1_connection = Database::getConnection('default', 'default'); + $db1_schema = $db1_connection->schema(); + $db2_connection = Database::getConnection('default', 'extra'); + + // Get the prefix info for the first database. + $method = new \ReflectionMethod($db1_schema, 'getPrefixInfo'); + $method->setAccessible(TRUE); + $db1_info = $method->invoke($db1_schema); + + // We change the database after opening the connection, so as to prevent + // connecting to a non-existent database. + $reflection = new \ReflectionObject($db2_connection); + $property = $reflection->getProperty('connectionOptions'); + $property->setAccessible(TRUE); + $connection_info['default']['database'] = 'foobar'; + $property->setValue($db2_connection, $connection_info['default']); + + // For testing purposes, we also change the database info. + $reflection_class = new \ReflectionClass(Database::class); + $property = $reflection_class->getProperty('databaseInfo'); + $property->setAccessible(TRUE); + $info = $property->getValue(); + $info['extra']['default']['database'] = 'foobar'; + $property->setValue(NULL, $info); + + $extra_info = Database::getConnectionInfo('extra'); + $this->assertSame('foobar', $extra_info['default']['database']); + $db2_schema = $db2_connection->schema(); + $db2_info = $method->invoke($db2_schema); + + // Each target connection has a different database. + $this->assertNotSame($db2_info['database'], $db1_info['database']); + // The new profile has a different database. + $this->assertSame('foobar', $db2_info['database']); + + Database::removeConnection('extra'); + } + +} diff --git a/core/tests/Drupal/KernelTests/Core/Database/LargeQueryTest.php b/core/tests/Drupal/KernelTests/Core/Database/LargeQueryTest.php deleted file mode 100644 index 86888e11a2d0b223ae6f6d425d93e398e1fc43a7..0000000000000000000000000000000000000000 --- a/core/tests/Drupal/KernelTests/Core/Database/LargeQueryTest.php +++ /dev/null @@ -1,54 +0,0 @@ -<?php - -namespace Drupal\KernelTests\Core\Database; - -use Drupal\Component\Utility\Environment; -use Drupal\Core\Database\Database; -use Drupal\Core\Database\DatabaseException; - -/** - * Tests handling of large queries. - * - * @group Database - */ -class LargeQueryTest extends DatabaseTestBase { - - /** - * Tests truncation of messages when max_allowed_packet exception occurs. - */ - public function testMaxAllowedPacketQueryTruncating() { - // This test only makes sense if we are running on a MySQL database. - // Test if we are. - $database = Database::getConnectionInfo('default'); - if ($database['default']['driver'] == 'mysql') { - // The max_allowed_packet value is configured per database instance. - // Retrieve the max_allowed_packet value from the current instance and - // check if PHP is configured with sufficient allowed memory to be able - // to generate a query larger than max_allowed_packet. - $max_allowed_packet = $this->connection->query('SELECT @@global.max_allowed_packet')->fetchField(); - if (Environment::checkMemoryLimit($max_allowed_packet + (16 * 1024 * 1024))) { - $long_name = str_repeat('a', $max_allowed_packet + 1); - try { - $this->connection->query('SELECT name FROM {test} WHERE name = :name', [':name' => $long_name]); - $this->fail("An exception should be thrown for queries larger than 'max_allowed_packet'"); - } - catch (DatabaseException $e) { - // Close and re-open the connection. Otherwise we will run into error - // 2006 "MySQL server had gone away" afterwards. - Database::closeConnection(); - Database::getConnection(); - $this->assertEqual($e->getPrevious()->errorInfo[1], 1153, "Got a packet bigger than 'max_allowed_packet' bytes exception thrown."); - // Use strlen() to count the bytes exactly, not the unicode chars. - $this->assertTrue(strlen($e->getMessage()) <= $max_allowed_packet, "'max_allowed_packet' exception message truncated."); - } - } - else { - $this->verbose('The configured max_allowed_packet exceeds the php memory limit. Therefore the test is skipped.'); - } - } - else { - $this->verbose('The test requires MySQL. Therefore the test is skipped.'); - } - } - -} diff --git a/core/tests/Drupal/KernelTests/Core/Database/PrefixInfoTest.php b/core/tests/Drupal/KernelTests/Core/Database/PrefixInfoTest.php deleted file mode 100644 index ea5e8a9edd226a6623f0dbec4373ed55f6edc53a..0000000000000000000000000000000000000000 --- a/core/tests/Drupal/KernelTests/Core/Database/PrefixInfoTest.php +++ /dev/null @@ -1,66 +0,0 @@ -<?php - -namespace Drupal\KernelTests\Core\Database; - -use Drupal\Core\Database\Database; - -/** - * Tests that the prefix info for a database schema is correct. - * - * @group Database - */ -class PrefixInfoTest extends DatabaseTestBase { - - /** - * Tests that DatabaseSchema::getPrefixInfo() returns the right database. - * - * We are testing if the return array of the method - * \Drupal\Core\Database\Driver\mysql\Schema::getPrefixInfo(). This return - * array is a keyed array with info about amongst other things the database. - * The other two by Drupal core supported databases do not have this variable - * set in the return array. - */ - public function testGetPrefixInfo() { - $connection_info = Database::getConnectionInfo('default'); - if ($connection_info['default']['driver'] == 'mysql') { - // Copy the default connection info to the 'extra' key. - Database::addConnectionInfo('extra', 'default', $connection_info['default']); - - $db1_connection = Database::getConnection('default', 'default'); - $db1_schema = $db1_connection->schema(); - $db2_connection = Database::getConnection('default', 'extra'); - - // Get the prefix info for the first database. - $method = new \ReflectionMethod($db1_schema, 'getPrefixInfo'); - $method->setAccessible(TRUE); - $db1_info = $method->invoke($db1_schema); - - // We change the database after opening the connection, so as to prevent - // connecting to a non-existent database. - $reflection = new \ReflectionObject($db2_connection); - $property = $reflection->getProperty('connectionOptions'); - $property->setAccessible(TRUE); - $connection_info['default']['database'] = 'foobar'; - $property->setValue($db2_connection, $connection_info['default']); - - // For testing purposes, we also change the database info. - $reflection_class = new \ReflectionClass('Drupal\Core\Database\Database'); - $property = $reflection_class->getProperty('databaseInfo'); - $property->setAccessible(TRUE); - $info = $property->getValue(); - $info['extra']['default']['database'] = 'foobar'; - $property->setValue(NULL, $info); - - $extra_info = Database::getConnectionInfo('extra'); - $this->assertSame($extra_info['default']['database'], 'foobar'); - $db2_schema = $db2_connection->schema(); - $db2_info = $method->invoke($db2_schema); - - $this->assertNotSame($db2_info['database'], $db1_info['database'], 'Each target connection has a different database.'); - $this->assertSame($db2_info['database'], 'foobar', 'The new profile has a different database.'); - - Database::removeConnection('extra'); - } - } - -}