summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAngie Byron2009-02-13 02:25:59 (GMT)
committerAngie Byron2009-02-13 02:25:59 (GMT)
commit9b50597eb243c0447eed6d9f41c9ad2e4de10b0d (patch)
tree08aea2b41249f374c0261c85b358da984aad3e8d
parent535db9d024393d89a4843eea638056452b4c687d (diff)
#369423 by nedjo: Fix drupal_write_record() bug with multi-field primary keys (with tests).
-rw-r--r--includes/common.inc4
-rw-r--r--modules/simpletest/tests/common.test47
2 files changed, 50 insertions, 1 deletions
diff --git a/includes/common.inc b/includes/common.inc
index 9c4bb54..c1dc6f8 100644
--- a/includes/common.inc
+++ b/includes/common.inc
@@ -3967,7 +3967,9 @@ function drupal_write_record($table, &$object, $primary_keys = array()) {
$object->$serial = $last_insert_id;
}
}
- else {
+ // If we have a single-field primary key but got no insert ID, the
+ // query failed.
+ elseif (count($primary_keys) == 1) {
$return = FALSE;
}
diff --git a/modules/simpletest/tests/common.test b/modules/simpletest/tests/common.test
index 9d8c1d2..209b450 100644
--- a/modules/simpletest/tests/common.test
+++ b/modules/simpletest/tests/common.test
@@ -783,6 +783,52 @@ class ValidUrlTestCase extends DrupalWebTestCase {
}
/**
+ * Tests for CRUD API functions.
+ */
+class DrupalDataApiTest extends DrupalWebTestCase {
+ function getInfo() {
+ return array(
+ 'name' => t('Data API functions'),
+ 'description' => t('Tests the performance of CRUD APIs.'),
+ 'group' => t('System'),
+ );
+ }
+
+ function setUp() {
+ parent::setUp('taxonomy');
+ }
+
+ /**
+ * Test the drupal_write_record() API function.
+ */
+ function testDrupalWriteRecord() {
+ // Insert an object record for a table with a single-field primary key.
+ $vocabulary = new StdClass();
+ $vocabulary->name = 'test';
+ $insert_result = drupal_write_record('taxonomy_vocabulary', $vocabulary);
+ $this->assertTrue($insert_result == SAVED_NEW, t('Correct value returned when a record is inserted with drupal_write_record() for a table with a single-field primary key.'));
+ $this->assertTrue(isset($vocabulary->vid), t('Primary key is set on record created with drupal_write_record().'));
+
+ // Update the initial record after changing a property.
+ $vocabulary->name = 'testing';
+ $update_result = drupal_write_record('taxonomy_vocabulary', $vocabulary, array('vid'));
+ $this->assertTrue($update_result == SAVED_UPDATED, t('Correct value returned when a record updated with drupal_write_record() for table with single-field primary key.'));
+
+ // Insert an object record for a table with a multi-field primary key.
+ $vocabulary_node_type = new StdClass();
+ $vocabulary_node_type->vid = $vocabulary->vid;
+ $vocabulary_node_type->type = 'page';
+ $insert_result = drupal_write_record('taxonomy_vocabulary_node_type', $vocabulary_node_type);
+ $this->assertTrue($insert_result == SAVED_NEW, t('Correct value returned when a record is inserted with drupal_write_record() for a table with a multi-field primary key.'));
+
+ // Update the record.
+ $update_result = drupal_write_record('taxonomy_vocabulary_node_type', $vocabulary_node_type, array('vid', 'type'));
+ $this->assertTrue($update_result == SAVED_UPDATED, t('Correct value returned when a record is updated with drupal_write_record() for a table with a multi-field primary key.'));
+ }
+
+}
+
+/**
* Tests Simpletest error and exception collecter.
*/
class DrupalErrorCollectionUnitTest extends DrupalWebTestCase {
@@ -853,3 +899,4 @@ class DrupalErrorCollectionUnitTest extends DrupalWebTestCase {
}
}
}
+