Commit 9b50597e authored by webchick's avatar webchick

#369423 by nedjo: Fix drupal_write_record() bug with multi-field primary keys (with tests).

parent 535db9d0
......@@ -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;
}
......
......@@ -782,6 +782,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.
*/
......@@ -853,3 +899,4 @@ class DrupalErrorCollectionUnitTest extends DrupalWebTestCase {
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment