Version 1
[yaffs-website] / web / core / tests / Drupal / KernelTests / Core / Database / LargeQueryTest.php
1 <?php
2
3 namespace Drupal\KernelTests\Core\Database;
4
5 use Drupal\Component\Utility\Environment;
6 use Drupal\Core\Database\Database;
7 use Drupal\Core\Database\DatabaseException;
8
9 /**
10  * Tests handling of large queries.
11  *
12  * @group Database
13  */
14 class LargeQueryTest extends DatabaseTestBase {
15
16   /**
17    * Tests truncation of messages when max_allowed_packet exception occurs.
18    */
19   public function testMaxAllowedPacketQueryTruncating() {
20     // This test only makes sense if we are running on a MySQL database.
21     // Test if we are.
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);
31         try {
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'");
34         }
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.");
43         }
44       }
45       else {
46         $this->verbose('The configured max_allowed_packet exceeds the php memory limit. Therefore the test is skipped.');
47       }
48     }
49     else {
50       $this->verbose('The test requires MySQL. Therefore the test is skipped.');
51     }
52   }
53
54 }