diff --git a/core/lib/Drupal/Core/Database/Connection.php b/core/lib/Drupal/Core/Database/Connection.php index c170814..7d12118 100644 --- a/core/lib/Drupal/Core/Database/Connection.php +++ b/core/lib/Drupal/Core/Database/Connection.php @@ -1249,6 +1249,8 @@ public function quote($string, $parameter_type = \PDO::PARAM_STR) { * {@inheritdoc} */ public function serialize() { + // Just store the target and the key of the connection because this is all + // what drupal needs to get a proper connection running again. return serialize(array($this->target, $this->key)); } diff --git a/core/modules/system/lib/Drupal/system/Tests/Database/ConnectionTest.php b/core/modules/system/lib/Drupal/system/Tests/Database/ConnectionTest.php index 983a539..18f4c86 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Database/ConnectionTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Database/ConnectionTest.php @@ -125,14 +125,14 @@ function testConnectionOptions() { } /** - * Tests the serialization and deserialization of a database connection. + * Tests the serialization and unserialization of a database connection. */ - function testConnectionSerialization() { + public function testConnectionSerialization() { $db = Database::getConnection('default', 'default'); $serialized = serialize($db); - $deserialized = unserialize($serialized); + $unserialized = unserialize($serialized); - $this->assertIdenticalObject($db, $deserialized, 'Serializing and deserializing a connection returns an identical object.'); + $this->assertIdenticalObject($db, $unserialized, 'Serializing and unserializing a connection returns an identical object.'); } } diff --git a/core/modules/system/lib/Drupal/system/Tests/Form/FormTest.php b/core/modules/system/lib/Drupal/system/Tests/Form/FormTest.php index c662ec7..ef498e1 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Form/FormTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Form/FormTest.php @@ -664,4 +664,13 @@ function testMultiFormSameNameErrorClass() { $this->assertFieldByXpath('//input[@id="edit-name" and contains(@class, "error")]', NULL, 'Error input form element class found for first element.'); $this->assertNoFieldByXpath('//input[@id="edit-name--2" and contains(@class, "error")]', NULL, 'No error input form element class found for second element.'); } + + /** + * Tests a form with a form state storing a database connection. + */ + public function testFormStateDatabaseConnection() { + $this->assertNoText(t('Database connection found')); + $this->drupalPost('form-test/form_state-database', array(), t('Submit')); + $this->assertText(t('Database connection found')); + } } diff --git a/core/modules/system/tests/modules/form_test/form_test.module b/core/modules/system/tests/modules/form_test/form_test.module index c7a85a3..afcc3f9 100644 --- a/core/modules/system/tests/modules/form_test/form_test.module +++ b/core/modules/system/tests/modules/form_test/form_test.module @@ -5,6 +5,8 @@ * Helper module for the form API tests. */ +use Drupal\Core\Database\Connection; +use Drupal\Core\Database\Database; use Drupal\form_test\Callbacks; use Drupal\form_test\FormTestObject; use Drupal\form_test\SystemConfigFormTestForm; @@ -353,6 +355,13 @@ function form_test_menu() { 'access callback' => TRUE, ); + $items['form-test/form_state-database'] = array( + 'title' => t('Form state with a database connection'), + 'page callback' => 'drupal_get_form', + 'page arguments' => array('form_test_form_state_database'), + 'access callback' => TRUE, + ); + return $items; } @@ -2466,3 +2475,36 @@ function form_test_group_vertical_tabs() { ); return $form; } + +/** + * Builds a form which gets the database connection stored in the form state. + */ +function form_test_form_state_database($form, &$form_state) { + + $form['text'] = array( + '#type' => 'textfield', + '#title' => t('Text field'), + ); + + $form['test_submit'] = array( + '#type' => 'submit', + '#value' => t('Submit'), + ); + + if (!isset($form_state['storage']['database'])) { + $form_state['storage']['database'] = Database::getConnection('default'); + } + elseif ($form_state['storage']['database'] instanceof Connection) { + $form['database']['#markup'] = t('Database connection found'); + } + + return $form; +} + +/** + * Form submit handler for database form_state test. + */ +function form_test_form_state_database_submit($form, &$form_state) { + $form_state['cache'] = TRUE; + $form_state['rebuild'] = FALSE; +}