diff options
author | Dries Buytaert <dries@buytaert.net> | 2010-03-03 08:40:25 +0000 |
---|---|---|
committer | Dries Buytaert <dries@buytaert.net> | 2010-03-03 08:40:25 +0000 |
commit | c4fd5818999670bf754c24a88fc718e6b502a85b (patch) | |
tree | 44d49eeb9a53b22a3a8b1ba698863291e850d916 | |
parent | a3134cc24a396c526ad6285337d3419d8d888f47 (diff) | |
download | brdo-c4fd5818999670bf754c24a88fc718e6b502a85b.tar.gz brdo-c4fd5818999670bf754c24a88fc718e6b502a85b.tar.bz2 |
- Patch #353918 by Dave Reid, yched: drupal_write_record() writes empty string instead of empty serialized array.
-rw-r--r-- | includes/common.inc | 2 | ||||
-rw-r--r-- | modules/simpletest/tests/common.test | 20 | ||||
-rw-r--r-- | modules/simpletest/tests/database_test.install | 30 |
3 files changed, 50 insertions, 2 deletions
diff --git a/includes/common.inc b/includes/common.inc index 7f140b815..c9458dbc1 100644 --- a/includes/common.inc +++ b/includes/common.inc @@ -5741,7 +5741,7 @@ function drupal_write_record($table, &$object, $primary_keys = array()) { if (empty($info['serialize'])) { $fields[$field] = $object->$field; } - elseif (!empty($object->$field)) { + elseif (isset($object->$field)) { $fields[$field] = serialize($object->$field); } else { diff --git a/modules/simpletest/tests/common.test b/modules/simpletest/tests/common.test index aa8f9d422..de4861389 100644 --- a/modules/simpletest/tests/common.test +++ b/modules/simpletest/tests/common.test @@ -1561,6 +1561,26 @@ class DrupalDataApiTest extends DrupalWebTestCase { $this->assertIdentical($result->name, 'Mary', t('Name field set.')); $this->assertIdentical($result->age, NULL, t('Age field set.')); + // Insert a record - the "data" column should be serialized. + $person = new stdClass(); + $person->name = 'Dave'; + $update_result = drupal_write_record('test_serialized', $person); + $result = db_query("SELECT * FROM {test_serialized} WHERE id = :id", array(':id' => $person->id))->fetchObject(); + $this->assertIdentical($result->name, 'Dave', t('Name field set.')); + $this->assertIdentical($result->info, NULL, t('Info field set.')); + + $person->info = array(); + $update_result = drupal_write_record('test_serialized', $person, array('id')); + $result = db_query("SELECT * FROM {test_serialized} WHERE id = :id", array(':id' => $person->id))->fetchObject(); + $this->assertIdentical(unserialize($result->info), array(), t('Info field updated.')); + + // Update the serialized record. + $data = array('foo' => 'bar', 1 => 2, 'empty' => '', 'null' => NULL); + $person->info = $data; + $update_result = drupal_write_record('test_serialized', $person, array('id')); + $result = db_query("SELECT * FROM {test_serialized} WHERE id = :id", array(':id' => $person->id))->fetchObject(); + $this->assertIdentical(unserialize($result->info), $data, t('Info field updated.')); + // Run an update query where no field values are changed. The database // layer should return zero for number of affected rows, but // db_write_record() should still return SAVED_UPDATED. diff --git a/modules/simpletest/tests/database_test.install b/modules/simpletest/tests/database_test.install index 4ecf74bc2..98a0c8403 100644 --- a/modules/simpletest/tests/database_test.install +++ b/modules/simpletest/tests/database_test.install @@ -35,7 +35,8 @@ function database_test_schema() { 'type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, - 'default' => 0), + 'default' => 0, + ), 'job' => array( 'description' => "The person's job", 'type' => 'varchar', @@ -202,5 +203,32 @@ function database_test_schema() { ), ); + $schema['test_serialized'] = array( + 'description' => 'Basic test table for NULL value handling.', + 'fields' => array( + 'id' => array( + 'type' => 'serial', + 'unsigned' => TRUE, + 'not null' => TRUE, + ), + 'name' => array( + 'description' => "A person's name.", + 'type' => 'varchar', + 'length' => 255, + 'not null' => FALSE, + 'default' => '', + ), + 'info' => array( + 'description' => "The person's data in serialized form.", + 'type' => 'text', + 'serialize' => TRUE, + ), + ), + 'primary key' => array('id'), + 'unique keys' => array( + 'name' => array('name') + ), + ); + return $schema; } |