3 namespace Drupal\Tests\dblog\Functional;
5 use Drupal\Core\Database\Database;
6 use Drupal\Tests\BrowserTestBase;
9 * Tests logging of connection failures.
13 class ConnectionFailureTest extends BrowserTestBase {
15 public static $modules = ['dblog'];
18 * Tests logging of connection failures.
20 public function testConnectionFailureLogging() {
21 $logger = \Drupal::service('logger.factory');
23 // MySQL errors like "1153 - Got a packet bigger than 'max_allowed_packet'
24 // bytes" or "1100 - Table 'xyz' was not locked with LOCK TABLES" lead to a
25 // database connection unusable for further requests. All further request
26 // will result in an "2006 - MySQL server had gone away" error. As a
27 // consequence it's impossible to use this connection to log the causing
28 // initial error itself. Using Database::closeConnection() we simulate such
29 // a corrupted connection. In this case dblog has to establish a different
30 // connection by itself to be able to write the log entry.
31 Database::closeConnection();
33 // Create a log entry.
34 $logger->get('php')->error('testConnectionFailureLogging');
36 // Re-establish the default database connection.
37 Database::getConnection();
39 $wid = db_query("SELECT MAX(wid) FROM {watchdog} WHERE message = 'testConnectionFailureLogging'")->fetchField();
40 $this->assertTrue($wid, 'Watchdog entry has been stored in database.');