diff --git a/includes/bootstrap.inc b/includes/bootstrap.inc index decb911..bff43ea 100644 --- a/includes/bootstrap.inc +++ b/includes/bootstrap.inc @@ -918,7 +918,25 @@ function variable_initialize($conf = array()) { } else { // Proceed with variable rebuild. - $variables = array_map('unserialize', db_query('SELECT name, value FROM {variable}')->fetchAllKeyed()); + $variables = array(); + // We can run into unparsable values + $err_variables = array(); + $rows = db_query('SELECT name, value FROM {variable}')->fetchAllKeyed(); + foreach ($rows as $name => $db_value) { + // Suppress the notice. + $value = @unserialize($db_value); + // If the unserialize call returned FALSE and the stored value is NOT a + // boolean false, list it in the report. + if ($value === FALSE && $db_value != 'b:0;') { + $err_variables[] = $name; + } + $variables[$name] = $value; + } + if (count($err_variables)) { + $err_variables = join (", ", $err_variables); + // We cannot call watchdog as modules are not loaded so resort to drupal_set_message + drupal_set_message(t("There are errors with your variables %variables.", array('%variables' => $err_variables))); + } cache_set('variables', $variables, 'cache_bootstrap'); lock_release($name); } diff --git a/modules/simpletest/tests/bootstrap.test b/modules/simpletest/tests/bootstrap.test index cb4fe8e..07be7ac 100644 --- a/modules/simpletest/tests/bootstrap.test +++ b/modules/simpletest/tests/bootstrap.test @@ -267,6 +267,27 @@ class BootstrapVariableTestCase extends DrupalWebTestCase { $this->assertIdentical(5, variable_get('simpletest_bootstrap_variable_test', 5), t('The default variable parameter is passed through correctly.')); } + /** + * Test for a corrupted variable. + */ + function testFaultyVariable() { + $name = 'simpletest_bootstrap_wrong_variable_test'; + $test_value = 'unserialized_value'; + + // We write a value directly to the database bypassing the serialization + // We cannot use drupal_write_record as that serializes the value + db_merge('variable')->key(array('name' => $name))->fields(array('value' => $test_value))->execute(); + + // Clear the variable cache to reread the new value + // next line fails with a PDOException: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'drupal_d7.simpletest992395' doesn't exist: DELETE FROM {} WHERE (cid = :db_condition_placeholder_0) ; Array ( [:db_condition_placeholder_0] => variables ) in cache_clear_all() (line 169 of /Users/clemens/Sites/drupal/d7/www/includes/cache.inc). + cache_clear_all('variables', 'cache_bootstrap'); + // Reseed the variables. This should load $name + variable_initialize(); + // Value should be FALSE + $value = variable_get($name, 'default_value'); + $this->assertEqual($value, $test_value, "Are equal? variable_get: '" . $value . "' set : '" . $test_value . "' ... " . gettype($value)); + $this->assertValue($ok, t('Wrong serialized variable renders to FALSE.')); + } } /**