function DrupalDataApiTest::testDrupalWriteRecord in SimpleTest 7
Test the drupal_write_record() API function.
File
- tests/
common.test, line 1421 - Tests for common.inc functionality.
Class
- DrupalDataApiTest
- Tests for CRUD API functions.
Code
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.
$node_access = new stdClass();
$node_access->nid = mt_rand();
$node_access->gid = mt_rand();
$node_access->realm = $this
->randomName();
$insert_result = drupal_write_record('node_access', $node_access);
$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('node_access', $node_access, array(
'nid',
'gid',
'realm',
));
$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.'));
}