3 namespace Drupal\KernelTests\Core\Database;
5 use Drupal\Component\Utility\Environment;
6 use Drupal\Core\Database\Database;
7 use Drupal\Core\Database\DatabaseException;
10 * Tests handling of large queries.
14 class LargeQueryTest extends DatabaseTestBase {
17 * Tests truncation of messages when max_allowed_packet exception occurs.
19 public function testMaxAllowedPacketQueryTruncating() {
20 // This test only makes sense if we are running on a MySQL database.
22 $database = Database::getConnectionInfo('default');
23 if ($database['default']['driver'] == 'mysql') {
24 // The max_allowed_packet value is configured per database instance.
25 // Retrieve the max_allowed_packet value from the current instance and
26 // check if PHP is configured with sufficient allowed memory to be able
27 // to generate a query larger than max_allowed_packet.
28 $max_allowed_packet = db_query('SELECT @@global.max_allowed_packet')->fetchField();
29 if (Environment::checkMemoryLimit($max_allowed_packet + (16 * 1024 * 1024))) {
30 $long_name = str_repeat('a', $max_allowed_packet + 1);
32 db_query('SELECT name FROM {test} WHERE name = :name', [':name' => $long_name]);
33 $this->fail("An exception should be thrown for queries larger than 'max_allowed_packet'");
35 catch (DatabaseException $e) {
36 // Close and re-open the connection. Otherwise we will run into error
37 // 2006 "MySQL server had gone away" afterwards.
38 Database::closeConnection();
39 Database::getConnection();
40 $this->assertEqual($e->getPrevious()->errorInfo[1], 1153, "Got a packet bigger than 'max_allowed_packet' bytes exception thrown.");
41 // Use strlen() to count the bytes exactly, not the unicode chars.
42 $this->assertTrue(strlen($e->getMessage()) <= $max_allowed_packet, "'max_allowed_packet' exception message truncated.");
46 $this->verbose('The configured max_allowed_packet exceeds the php memory limit. Therefore the test is skipped.');
50 $this->verbose('The test requires MySQL. Therefore the test is skipped.');