Commit 0f77f652 authored by Dries's avatar Dries

- Patch #394524 by Berdir, Damien Tournoud: converted to the new database abstraction layer.

parent 83631203
......@@ -121,7 +121,9 @@ protected function assert($status, $message = '', $group = 'Other', array $calle
);
// Store assertion for display after the test has completed.
db_insert('simpletest')->fields($assertion)->execute();
db_insert('simpletest')
->fields($assertion)
->execute();
// Return to testing prefix.
$db_prefix = $current_db_prefix;
......@@ -679,7 +681,10 @@ protected function drupalCreateNode($settings = array()) {
node_save($node);
// small hack to link revisions to our test user
db_query('UPDATE {node_revision} SET uid = %d WHERE vid = %d', $node->uid, $node->vid);
db_update('node_revision')
->fields(array('uid' => $node->uid))
->condition('vid', $node->vid)
->execute();
return $node;
}
......@@ -832,15 +837,22 @@ protected function drupalCreateRole(array $permissions) {
// Create new role.
$role_name = $this->randomName();
db_query("INSERT INTO {role} (name) VALUES ('%s')", $role_name);
$role = db_fetch_object(db_query("SELECT * FROM {role} WHERE name = '%s'", $role_name));
db_insert('role')
->fields(array('name' => $role_name))
->execute();
$role = db_query('SELECT * FROM {role} WHERE name = :name', array(':name' => $role_name))->fetchObject();
$this->assertTrue($role, t('Created role of name: @role_name, id: @rid', array('@role_name' => $role_name, '@rid' => (isset($role->rid) ? $role->rid : t('-n/a-')))), t('Role'));
if ($role && !empty($role->rid)) {
// Assign permissions to role and mark it for clean-up.
$query = db_insert('role_permission')->fields(array('rid', 'permission'));
foreach ($permissions as $permission_string) {
db_query("INSERT INTO {role_permission} (rid, permission) VALUES (%d, '%s')", $role->rid, $permission_string);
$query->values(array(
'rid' => $role->rid,
'permission' => $permission_string,
));
}
$count = db_result(db_query("SELECT COUNT(*) FROM {role_permission} WHERE rid = %d", $role->rid));
$query->execute();
$count = db_query('SELECT COUNT(*) FROM {role_permission} WHERE rid = :rid', array(':rid' => $role->rid))->fetchField();
$this->assertTrue($count == count($permissions), t('Created permissions: @perms', array('@perms' => implode(', ', $permissions))), t('Role'));
return $role->rid;
}
......
......@@ -102,7 +102,9 @@ function _simpletest_format_summary_line($summary) {
*/
function simpletest_run_tests($test_list, $reporter = 'drupal') {
cache_clear_all();
$test_id = db_insert('simpletest_test_id')->useDefaults(array('test_id'))->execute();
$test_id = db_insert('simpletest_test_id')
->useDefaults(array('test_id'))
->execute();
// Get the info for the first test being run.
$first_test = array_shift($test_list);
......@@ -325,7 +327,7 @@ function simpletest_clean_temporary_directories() {
function simpletest_clean_results_table($test_id = NULL) {
if (variable_get('simpletest_clear_results', TRUE)) {
if ($test_id) {
$count = db_result(db_query('SELECT COUNT(test_id) FROM {simpletest_test_id} WHERE test_id = :test_id', array(':test_id' => $test_id)));
$count = db_query('SELECT COUNT(test_id) FROM {simpletest_test_id} WHERE test_id = :test_id', array(':test_id' => $test_id))->fetchField();
db_delete("simpletest")
->condition('test_id', $test_id)
......@@ -335,7 +337,7 @@ function simpletest_clean_results_table($test_id = NULL) {
->execute();
}
else {
$count = db_result(db_query('SELECT COUNT(test_id) FROM {simpletest_test_id}'));
$count = db_query('SELECT COUNT(test_id) FROM {simpletest_test_id}')->fetchField();
// Clear test results.
db_delete("simpletest")->execute();
......
......@@ -57,7 +57,7 @@ class ActionsConfigurationTestCase extends DrupalWebTestCase {
$this->assertRaw(t('Action %action was deleted', array('%action' => $new_action_description)), t('Make sure that we get a delete confirmation message.'));
$this->drupalGet('admin/settings/actions/manage');
$this->assertNoText($new_action_description, t("Make sure the action description does not appear on the overview page after we've deleted the action."));
$exists = db_result(db_query("SELECT aid FROM {actions} WHERE callback = 'drupal_goto_action'"));
$exists = db_query('SELECT aid FROM {actions} WHERE callback = :callback', array(':callback' => 'drupal_goto_action'))->fetchField();
$this->assertFalse($exists, t('Make sure the action is gone from the database after being deleted.'));
}
}
......@@ -251,31 +251,31 @@ class HookBootExitTestCase extends DrupalWebTestCase {
variable_set('cache', CACHE_DISABLED);
$this->drupalGet('');
$calls = 1;
$this->assertEqual(db_query("SELECT COUNT(*) FROM {watchdog} WHERE type = 'system_test' AND message = 'hook_boot'")->fetchField(), $calls, t('hook_boot called with disabled cache.'));
$this->assertEqual(db_query("SELECT COUNT(*) FROM {watchdog} WHERE type = 'system_test' AND message = 'hook_exit'")->fetchField(), $calls, t('hook_exit called with disabled cache.'));
$this->assertEqual(db_query('SELECT COUNT(*) FROM {watchdog} WHERE type = :type AND message = :message', array(':type' => 'system_test', ':message' => 'hook_boot'))->fetchField(), $calls, t('hook_boot called with disabled cache.'));
$this->assertEqual(db_query('SELECT COUNT(*) FROM {watchdog} WHERE type = :type AND message = :message', array(':type' => 'system_test', ':message' => 'hook_exit'))->fetchField(), $calls, t('hook_exit called with disabled cache.'));
// Test with normal cache. Boot and exit should be called.
variable_set('cache', CACHE_NORMAL);
$this->drupalGet('');
$calls++;
$this->assertEqual(db_query("SELECT COUNT(*) FROM {watchdog} WHERE type = 'system_test' AND message = 'hook_boot'")->fetchField(), $calls, t('hook_boot called with normal cache.'));
$this->assertEqual(db_query("SELECT COUNT(*) FROM {watchdog} WHERE type = 'system_test' AND message = 'hook_exit'")->fetchField(), $calls, t('hook_exit called with normal cache.'));
$this->assertEqual(db_query('SELECT COUNT(*) FROM {watchdog} WHERE type = :type AND message = :message', array(':type' => 'system_test', ':message' => 'hook_boot'))->fetchField(), $calls, t('hook_boot called with normal cache.'));
$this->assertEqual(db_query('SELECT COUNT(*) FROM {watchdog} WHERE type = :type AND message = :message', array(':type' => 'system_test', ':message' => 'hook_exit'))->fetchField(), $calls, t('hook_exit called with normal cache.'));
// Test with aggressive cache. Boot and exit should not fire since the
// page is cached.
variable_set('cache', CACHE_AGGRESSIVE);
$this->assertTrue(cache_get(url('', array('absolute' => TRUE)), 'cache_page'), t('Page has been cached.'));
$this->drupalGet('');
$this->assertEqual(db_query("SELECT COUNT(*) FROM {watchdog} WHERE type = 'system_test' AND message = 'hook_boot'")->fetchField(), $calls, t('hook_boot not called with agressive cache and a cached page.'));
$this->assertEqual(db_query("SELECT COUNT(*) FROM {watchdog} WHERE type = 'system_test' AND message = 'hook_exit'")->fetchField(), $calls, t('hook_exit not called with agressive cache and a cached page.'));
$this->assertEqual(db_query('SELECT COUNT(*) FROM {watchdog} WHERE type = :type AND message = :message', array(':type' => 'system_test', ':message' => 'hook_boot'))->fetchField(), $calls, t('hook_boot not called with agressive cache and a cached page.'));
$this->assertEqual(db_query('SELECT COUNT(*) FROM {watchdog} WHERE type = :type AND message = :message', array(':type' => 'system_test', ':message' => 'hook_exit'))->fetchField(), $calls, t('hook_exit not called with agressive cache and a cached page.'));
// Test with aggressive cache and page cache cleared. Boot and exit should
// be called.
$this->assertTrue(db_delete('cache_page')->execute(), t('Page cache cleared.'));
$this->drupalGet('');
$calls++;
$this->assertEqual(db_query("SELECT COUNT(*) FROM {watchdog} WHERE type = 'system_test' AND message = 'hook_boot'")->fetchField(), $calls, t('hook_boot called with agressive cache and no cached page.'));
$this->assertEqual(db_query("SELECT COUNT(*) FROM {watchdog} WHERE type = 'system_test' AND message = 'hook_exit'")->fetchField(), $calls, t('hook_exit called with agressive cache and no cached page.'));
$this->assertEqual(db_query('SELECT COUNT(*) FROM {watchdog} WHERE type = :type AND message = :message', array(':type' => 'system_test', ':message' => 'hook_boot'))->fetchField(), $calls, t('hook_boot called with agressive cache and no cached page.'));
$this->assertEqual(db_query('SELECT COUNT(*) FROM {watchdog} WHERE type = :type AND message = :message', array(':type' => 'system_test', ':message' => 'hook_exit'))->fetchField(), $calls, t('hook_exit called with agressive cache and no cached page.'));
}
}
......@@ -252,7 +252,7 @@ class DatabaseFetchTestCase extends DatabaseTestCase {
*/
function testQueryFetchDefault() {
$records = array();
$result = db_query("SELECT name FROM {test} WHERE age = :age", array(':age' => 25));
$result = db_query('SELECT name FROM {test} WHERE age = :age', array(':age' => 25));
$this->assertTrue($result instanceof DatabaseStatementInterface, t('Result set is a Drupal statement object.'));
foreach ($result as $record) {
$records[] = $record;
......@@ -268,7 +268,7 @@ class DatabaseFetchTestCase extends DatabaseTestCase {
*/
function testQueryFetchObject() {
$records = array();
$result = db_query("SELECT name FROM {test} WHERE age = :age", array(':age' => 25), array('fetch' => PDO::FETCH_OBJ));
$result = db_query('SELECT name FROM {test} WHERE age = :age', array(':age' => 25), array('fetch' => PDO::FETCH_OBJ));
foreach ($result as $record) {
$records[] = $record;
$this->assertTrue(is_object($record), t('Record is an object.'));
......@@ -283,7 +283,7 @@ class DatabaseFetchTestCase extends DatabaseTestCase {
*/
function testQueryFetchArray() {
$records = array();
$result = db_query("SELECT name FROM {test} WHERE age = :age", array(':age' => 25), array('fetch' => PDO::FETCH_ASSOC));
$result = db_query('SELECT name FROM {test} WHERE age = :age', array(':age' => 25), array('fetch' => PDO::FETCH_ASSOC));
foreach ($result as $record) {
$records[] = $record;
if ($this->assertTrue(is_array($record), t('Record is an array.'))) {
......@@ -301,7 +301,7 @@ class DatabaseFetchTestCase extends DatabaseTestCase {
*/
function testQueryFetchClass() {
$records = array();
$result = db_query("SELECT name FROM {test} WHERE age = :age", array(':age' => 25), array('fetch' => 'FakeRecord'));
$result = db_query('SELECT name FROM {test} WHERE age = :age', array(':age' => 25), array('fetch' => 'FakeRecord'));
foreach ($result as $record) {
$records[] = $record;
if ($this->assertTrue($record instanceof FakeRecord, t('Record is an object of class FakeRecord.'))) {
......@@ -335,7 +335,7 @@ class DatabaseFetch2TestCase extends DatabaseTestCase {
// Confirm that we can fetch a record into an indexed array explicitly.
function testQueryFetchNum() {
$records = array();
$result = db_query("SELECT name FROM {test} WHERE age = :age", array(':age' => 25), array('fetch' => PDO::FETCH_NUM));
$result = db_query('SELECT name FROM {test} WHERE age = :age', array(':age' => 25), array('fetch' => PDO::FETCH_NUM));
foreach ($result as $record) {
$records[] = $record;
if ($this->assertTrue(is_array($record), t('Record is an array.'))) {
......@@ -351,7 +351,7 @@ class DatabaseFetch2TestCase extends DatabaseTestCase {
*/
function testQueryFetchBoth() {
$records = array();
$result = db_query("SELECT name FROM {test} WHERE age = :age", array(':age' => 25), array('fetch' => PDO::FETCH_BOTH));
$result = db_query('SELECT name FROM {test} WHERE age = :age', array(':age' => 25), array('fetch' => PDO::FETCH_BOTH));
foreach ($result as $record) {
$records[] = $record;
if ($this->assertTrue(is_array($record), t('Record is an array.'))) {
......@@ -368,11 +368,11 @@ class DatabaseFetch2TestCase extends DatabaseTestCase {
*/
function testQueryFetchCol() {
$records = array();
$result = db_query("SELECT name FROM {test} WHERE age > :age", array(':age' => 25));
$result = db_query('SELECT name FROM {test} WHERE age > :age', array(':age' => 25));
$column = $result->fetchCol();
$this->assertIdentical(count($column), 3, t('fetchCol() returns the right number of records.'));
$result = db_query("SELECT name FROM {test} WHERE age > :age", array(':age' => 25));
$result = db_query('SELECT name FROM {test} WHERE age > :age', array(':age' => 25));
$i = 0;
foreach ($result as $record) {
$this->assertIdentical($record->name, $column[$i++], t('Column matches direct accesss.'));
......@@ -397,7 +397,7 @@ class DatabaseInsertTestCase extends DatabaseTestCase {
* Test the very basic insert functionality.
*/
function testSimpleInsert() {
$num_records_before = db_query("SELECT COUNT(*) FROM {test}")->fetchField();
$num_records_before = db_query('SELECT COUNT(*) FROM {test}')->fetchField();
$query = db_insert('test');
$query->fields(array(
......@@ -406,9 +406,9 @@ class DatabaseInsertTestCase extends DatabaseTestCase {
));
$query->execute();
$num_records_after = db_query("SELECT COUNT(*) FROM {test}")->fetchField();
$num_records_after = db_query('SELECT COUNT(*) FROM {test}')->fetchField();
$this->assertIdentical($num_records_before + 1, (int)$num_records_after, t('Record inserts correctly.'));
$saved_age = db_query("SELECT age FROM {test} WHERE name = :name", array(':name' => 'Yoko'))->fetchField();
$saved_age = db_query('SELECT age FROM {test} WHERE name = :name', array(':name' => 'Yoko'))->fetchField();
$this->assertIdentical($saved_age, '29', t('Can retrieve after inserting.'));
}
......@@ -416,7 +416,7 @@ class DatabaseInsertTestCase extends DatabaseTestCase {
* Test that we can insert multiple records in one query object.
*/
function testMultiInsert() {
$num_records_before = (int) db_query("SELECT COUNT(*) FROM {test}")->fetchField();
$num_records_before = (int) db_query('SELECT COUNT(*) FROM {test}')->fetchField();
$query = db_insert('test');
$query->fields(array(
......@@ -435,13 +435,13 @@ class DatabaseInsertTestCase extends DatabaseTestCase {
$query->values(array('Moe', '32'));
$query->execute();
$num_records_after = (int) db_query("SELECT COUNT(*) FROM {test}")->fetchField();
$num_records_after = (int) db_query('SELECT COUNT(*) FROM {test}')->fetchField();
$this->assertIdentical($num_records_before + 3, $num_records_after, t('Record inserts correctly.'));
$saved_age = db_query("SELECT age FROM {test} WHERE name = :name", array(':name' => 'Larry'))->fetchField();
$saved_age = db_query('SELECT age FROM {test} WHERE name = :name', array(':name' => 'Larry'))->fetchField();
$this->assertIdentical($saved_age, '30', t('Can retrieve after inserting.'));
$saved_age = db_query("SELECT age FROM {test} WHERE name = :name", array(':name' => 'Curly'))->fetchField();
$saved_age = db_query('SELECT age FROM {test} WHERE name = :name', array(':name' => 'Curly'))->fetchField();
$this->assertIdentical($saved_age, '31', t('Can retrieve after inserting.'));
$saved_age = db_query("SELECT age FROM {test} WHERE name = :name", array(':name' => 'Moe'))->fetchField();
$saved_age = db_query('SELECT age FROM {test} WHERE name = :name', array(':name' => 'Moe'))->fetchField();
$this->assertIdentical($saved_age, '32', t('Can retrieve after inserting.'));
}
......@@ -449,7 +449,7 @@ class DatabaseInsertTestCase extends DatabaseTestCase {
* Test that an insert object can be reused with new data after it executes.
*/
function testRepeatedInsert() {
$num_records_before = db_query("SELECT COUNT(*) FROM {test}")->fetchField();
$num_records_before = db_query('SELECT COUNT(*) FROM {test}')->fetchField();
$query = db_insert('test');
......@@ -470,13 +470,13 @@ class DatabaseInsertTestCase extends DatabaseTestCase {
$query->values(array('Moe', '32'));
$query->execute();
$num_records_after = db_query("SELECT COUNT(*) FROM {test}")->fetchField();
$num_records_after = db_query('SELECT COUNT(*) FROM {test}')->fetchField();
$this->assertIdentical((int) $num_records_before + 3, (int) $num_records_after, t('Record inserts correctly.'));
$saved_age = db_query("SELECT age FROM {test} WHERE name = :name", array(':name' => 'Larry'))->fetchField();
$saved_age = db_query('SELECT age FROM {test} WHERE name = :name', array(':name' => 'Larry'))->fetchField();
$this->assertIdentical($saved_age, '30', t('Can retrieve after inserting.'));
$saved_age = db_query("SELECT age FROM {test} WHERE name = :name", array(':name' => 'Curly'))->fetchField();
$saved_age = db_query('SELECT age FROM {test} WHERE name = :name', array(':name' => 'Curly'))->fetchField();
$this->assertIdentical($saved_age, '31', t('Can retrieve after inserting.'));
$saved_age = db_query("SELECT age FROM {test} WHERE name = :name", array(':name' => 'Moe'))->fetchField();
$saved_age = db_query('SELECT age FROM {test} WHERE name = :name', array(':name' => 'Moe'))->fetchField();
$this->assertIdentical($saved_age, '32', t('Can retrieve after inserting.'));
}
......@@ -492,11 +492,11 @@ class DatabaseInsertTestCase extends DatabaseTestCase {
->values(array('Curly', '31'))
->values(array('Moe', '32'))
->execute();
$saved_age = db_query("SELECT age FROM {test} WHERE name = :name", array(':name' => 'Larry'))->fetchField();
$saved_age = db_query('SELECT age FROM {test} WHERE name = :name', array(':name' => 'Larry'))->fetchField();
$this->assertIdentical($saved_age, '30', t('Can retrieve after inserting.'));
$saved_age = db_query("SELECT age FROM {test} WHERE name = :name", array(':name' => 'Curly'))->fetchField();
$saved_age = db_query('SELECT age FROM {test} WHERE name = :name', array(':name' => 'Curly'))->fetchField();
$this->assertIdentical($saved_age, '31', t('Can retrieve after inserting.'));
$saved_age = db_query("SELECT age FROM {test} WHERE name = :name", array(':name' => 'Moe'))->fetchField();
$saved_age = db_query('SELECT age FROM {test} WHERE name = :name', array(':name' => 'Moe'))->fetchField();
$this->assertIdentical($saved_age, '32', t('Can retrieve after inserting.'));
}
......@@ -504,7 +504,8 @@ class DatabaseInsertTestCase extends DatabaseTestCase {
* Test that inserts return the proper auto-increment ID.
*/
function testInsertLastInsertID() {
$id = db_insert('test')->fields(array(
$id = db_insert('test')
->fields(array(
'name' => 'Larry',
'age' => '30',
))
......@@ -533,9 +534,10 @@ class DatabaseInsertLOBTestCase extends DatabaseTestCase {
function testInsertOneBlob() {
$data = "This is\000a test.";
$this->assertTrue(strlen($data) === 15, t('Test data contains a NULL.'));
$id = db_insert('test_one_blob')->fields(array('blob1' => $data))->execute();
$res = db_query('SELECT * FROM {test_one_blob} WHERE id = :id', array(':id' => $id));
$r = db_fetch_array($res);
$id = db_insert('test_one_blob')
->fields(array('blob1' => $data))
->execute();
$r = db_query('SELECT * FROM {test_one_blob} WHERE id = :id', array(':id' => $id))->fetchAssoc();
$this->assertTrue($r['blob1'] === $data, t('Can insert a blob: id @id, @data.', array('@id' => $id, '@data' => serialize($r))));
}
......@@ -543,13 +545,13 @@ class DatabaseInsertLOBTestCase extends DatabaseTestCase {
* Test that we can insert multiple blob fields in the same query.
*/
function testInsertMultipleBlob() {
$id = db_insert('test_two_blobs')->fields(array(
'blob1' => 'This is',
'blob2' => 'a test',
$id = db_insert('test_two_blobs')
->fields(array(
'blob1' => 'This is',
'blob2' => 'a test',
))
->execute();
$res = db_query('SELECT * FROM {test_two_blobs} WHERE id = %d', $id);
$r = db_fetch_array($res);
->execute();
$r = db_query('SELECT * FROM {test_two_blobs} WHERE id = :id', array(':id' => $id))->fetchAssoc();
$this->assertTrue($r['blob1'] === 'This is' && $r['blob2'] === 'a test', t('Can insert multiple blobs per row.'));
}
}
......@@ -576,7 +578,7 @@ class DatabaseInsertDefaultsTestCase extends DatabaseTestCase {
$schema = drupal_get_schema('test');
$job = db_query("SELECT job FROM {test} WHERE id = :id", array(':id' => $id))->fetchField();
$job = db_query('SELECT job FROM {test} WHERE id = :id', array(':id' => $id))->fetchField();
$this->assertEqual($job, $schema['fields']['job']['default'], t('Default field value is set.'));
}
......@@ -584,12 +586,12 @@ class DatabaseInsertDefaultsTestCase extends DatabaseTestCase {
* Test that no action will be preformed if no fields are specified.
*/
function testDefaultEmptyInsert() {
$num_records_before = (int) db_query("SELECT COUNT(*) FROM {test}")->fetchField();
$num_records_before = (int) db_query('SELECT COUNT(*) FROM {test}')->fetchField();
$result = db_insert('test')->execute();
$this->assertNull($result, t('Return NULL as no fields are specified.'));
$num_records_after = (int) db_query("SELECT COUNT(*) FROM {test}")->fetchField();
$num_records_after = (int) db_query('SELECT COUNT(*) FROM {test}')->fetchField();
$this->assertIdentical($num_records_before, $num_records_after, t('Do nothing as no fields are specified.'));
}
......@@ -597,12 +599,14 @@ class DatabaseInsertDefaultsTestCase extends DatabaseTestCase {
* Test that we can insert fields with values and defaults in the same query.
*/
function testDefaultInsertWithFields() {
$query = db_insert('test')->fields(array('name' => 'Bob'))->useDefaults(array('job'));
$query = db_insert('test')
->fields(array('name' => 'Bob'))
->useDefaults(array('job'));
$id = $query->execute();
$schema = drupal_get_schema('test');
$job = db_query("SELECT job FROM {test} WHERE id = :id", array(':id' => $id))->fetchField();
$job = db_query('SELECT job FROM {test} WHERE id = :id', array(':id' => $id))->fetchField();
$this->assertEqual($job, $schema['fields']['job']['default'], t('Default field value is set.'));
}
}
......@@ -624,10 +628,13 @@ class DatabaseUpdateTestCase extends DatabaseTestCase {
* Confirm that we can update a single record successfully.
*/
function testSimpleUpdate() {
$num_updated = db_update('test')->fields(array('name' => 'Tiffany'))->condition('id', 1)->execute();
$num_updated = db_update('test')
->fields(array('name' => 'Tiffany'))
->condition('id', 1)
->execute();
$this->assertIdentical($num_updated, 1, t('Updated 1 record.'));
$saved_name = db_query("SELECT name FROM {test} WHERE id = :id", array(':id' => 1))->fetchField();
$saved_name = db_query('SELECT name FROM {test} WHERE id = :id', array(':id' => 1))->fetchField();
$this->assertIdentical($saved_name, 'Tiffany', t('Updated name successfully.'));
}
......@@ -635,10 +642,13 @@ class DatabaseUpdateTestCase extends DatabaseTestCase {
* Confirm that we can update a multiple records successfully.
*/
function testMultiUpdate() {
$num_updated = db_update('test')->fields(array('job' => 'Musician'))->condition('job', 'Singer')->execute();
$num_updated = db_update('test')
->fields(array('job' => 'Musician'))
->condition('job', 'Singer')
->execute();
$this->assertIdentical($num_updated, 2, t('Updated 2 records.'));
$num_matches = db_query("SELECT COUNT(*) FROM {test} WHERE job = :job", array(':job' => 'Musician'))->fetchField();
$num_matches = db_query('SELECT COUNT(*) FROM {test} WHERE job = :job', array(':job' => 'Musician'))->fetchField();
$this->assertIdentical($num_matches, '2', t('Updated fields successfully.'));
}
......@@ -646,10 +656,13 @@ class DatabaseUpdateTestCase extends DatabaseTestCase {
* Confirm that we can update a multiple records with a non-equality condition.
*/
function testMultiGTUpdate() {
$num_updated = db_update('test')->fields(array('job' => 'Musician'))->condition('age', 26, '>')->execute();
$num_updated = db_update('test')
->fields(array('job' => 'Musician'))
->condition('age', 26, '>')
->execute();
$this->assertIdentical($num_updated, 2, t('Updated 2 records.'));
$num_matches = db_query("SELECT COUNT(*) FROM {test} WHERE job = :job", array(':job' => 'Musician'))->fetchField();
$num_matches = db_query('SELECT COUNT(*) FROM {test} WHERE job = :job', array(':job' => 'Musician'))->fetchField();
$this->assertIdentical($num_matches, '2', t('Updated fields successfully.'));
}
......@@ -657,10 +670,13 @@ class DatabaseUpdateTestCase extends DatabaseTestCase {
* Confirm that we can update a multiple records with a where call.
*/
function testWhereUpdate() {
$num_updated = db_update('test')->fields(array('job' => 'Musician'))->where('age > :age', array(':age' => 26))->execute();
$num_updated = db_update('test')
->fields(array('job' => 'Musician'))
->where('age > :age', array(':age' => 26))
->execute();
$this->assertIdentical($num_updated, 2, t('Updated 2 records.'));
$num_matches = db_query("SELECT COUNT(*) FROM {test} WHERE job = :job", array(':job' => 'Musician'))->fetchField();
$num_matches = db_query('SELECT COUNT(*) FROM {test} WHERE job = :job', array(':job' => 'Musician'))->fetchField();
$this->assertIdentical($num_matches, '2', t('Updated fields successfully.'));
}
......@@ -668,11 +684,14 @@ class DatabaseUpdateTestCase extends DatabaseTestCase {
* Confirm that we can stack condition and where calls.
*/
function testWhereAndConditionUpdate() {
$update = db_update('test')->fields(array('job' => 'Musician'))->where('age > :age', array(':age' => 26))->condition('name', 'Ringo');
$update = db_update('test')
->fields(array('job' => 'Musician'))
->where('age > :age', array(':age' => 26))
->condition('name', 'Ringo');
$num_updated = $update->execute();
$this->assertIdentical($num_updated, 1, t('Updated 1 record.'));
$num_matches = db_query("SELECT COUNT(*) FROM {test} WHERE job = :job", array(':job' => 'Musician'))->fetchField();
$num_matches = db_query('SELECT COUNT(*) FROM {test} WHERE job = :job', array(':job' => 'Musician'))->fetchField();
$this->assertIdentical($num_matches, '1', t('Updated fields successfully.'));
}
......@@ -704,7 +723,7 @@ class DatabaseUpdateComplexTestCase extends DatabaseTestCase {
$num_updated = $update->execute();
$this->assertIdentical($num_updated, 2, t('Updated 2 records.'));
$num_matches = db_query("SELECT COUNT(*) FROM {test} WHERE job = :job", array(':job' => 'Musician'))->fetchField();
$num_matches = db_query('SELECT COUNT(*) FROM {test} WHERE job = :job', array(':job' => 'Musician'))->fetchField();
$this->assertIdentical($num_matches, '2', t('Updated fields successfully.'));
}
......@@ -712,12 +731,13 @@ class DatabaseUpdateComplexTestCase extends DatabaseTestCase {
* Test WHERE IN clauses.
*/
function testInConditionUpdate() {
$num_updated = db_update('test')->fields(array('job' => 'Musician'))
$num_updated = db_update('test')
->fields(array('job' => 'Musician'))
->condition('name', array('John', 'Paul'), 'IN')
->execute();
$this->assertIdentical($num_updated, 2, t('Updated 2 records.'));
$num_matches = db_query("SELECT COUNT(*) FROM {test} WHERE job = :job", array(':job' => 'Musician'))->fetchField();
$num_matches = db_query('SELECT COUNT(*) FROM {test} WHERE job = :job', array(':job' => 'Musician'))->fetchField();
$this->assertIdentical($num_matches, '2', t('Updated fields successfully.'));
}
......@@ -725,12 +745,13 @@ class DatabaseUpdateComplexTestCase extends DatabaseTestCase {
* Test WHERE NOT IN clauses.
*/
function testNotInConditionUpdate() {
$num_updated = db_update('test')->fields(array('job' => 'Musician'))
$num_updated = db_update('test')
->fields(array('job' => 'Musician'))
->condition('name', array('John', 'Paul', 'George'), 'NOT IN')
->execute();
$this->assertIdentical($num_updated, 1, t('Updated 1 record.'));
$num_matches = db_query("SELECT COUNT(*) FROM {test} WHERE job = :job", array(':job' => 'Musician'))->fetchField();
$num_matches = db_query('SELECT COUNT(*) FROM {test} WHERE job = :job', array(':job' => 'Musician'))->fetchField();
$this->assertIdentical($num_matches, '1', t('Updated fields successfully.'));
}
......@@ -738,12 +759,13 @@ class DatabaseUpdateComplexTestCase extends DatabaseTestCase {
* Test BETWEEN conditional clauses.
*/
function testBetweenConditionUpdate() {
$num_updated = db_update('test')->fields(array('job' => 'Musician'))
$num_updated = db_update('test')
->fields(array('job' => 'Musician'))
->condition('age', array(25, 26), 'BETWEEN')
->execute();
$this->assertIdentical($num_updated, 2, t('Updated 2 records.'));
$num_matches = db_query("SELECT COUNT(*) FROM {test} WHERE job = :job", array(':job' => 'Musician'))->fetchField();
$num_matches = db_query('SELECT COUNT(*) FROM {test} WHERE job = :job', array(':job' => 'Musician'))->fetchField();
$this->assertIdentical($num_matches, '2', t('Updated fields successfully.'));
}
......@@ -751,12 +773,13 @@ class DatabaseUpdateComplexTestCase extends DatabaseTestCase {
* Test LIKE conditionals.
*/
function testLikeConditionUpdate() {
$num_updated = db_update('test')->fields(array('job' => 'Musician'))
$num_updated = db_update('test')
->fields(array('job' => 'Musician'))
->condition('name', '%ge%', 'LIKE')
->execute();
$this->assertIdentical($num_updated, 1, t('Updated 1 record.'));
$num_matches = db_query("SELECT COUNT(*) FROM {test} WHERE job = :job", array(':job' => 'Musician'))->fetchField();
$num_matches = db_query('SELECT COUNT(*) FROM {test} WHERE job = :job', array(':job' => 'Musician'))->fetchField();
$this->assertIdentical($num_matches, '1', t('Updated fields successfully.'));
}
......@@ -764,7 +787,7 @@ class DatabaseUpdateComplexTestCase extends DatabaseTestCase {
* Test update with expression values.
*/
function testUpdateExpression() {
$before_age = db_query("SELECT age FROM {test} WHERE name = 'Ringo'")->fetchField();
$before_age = db_query('SELECT age FROM {test} WHERE name = :name', array(':name' => 'Ringo'))->fetchField();
$GLOBALS['larry_test'] = 1;
$num_updated = db_update('test')
->condition('name', 'Ringo')
......@@ -773,10 +796,10 @@ class DatabaseUpdateComplexTestCase extends DatabaseTestCase {
->execute();
$this->assertIdentical($num_updated, 1, t('Updated 1 record.'));
$num_matches = db_query("SELECT COUNT(*) FROM {test} WHERE job = :job", array(':job' => 'Musician'))->fetchField();
$num_matches = db_query('SELECT COUNT(*) FROM {test} WHERE job = :job', array(':job' => 'Musician'))->fetchField();
$this->assertIdentical($num_matches, '1', t('Updated fields successfully.'));
$person = db_query("SELECT * FROM {test} WHERE name = :name", array(':name' => 'Ringo'))->fetch();
$person = db_query('SELECT * FROM {test} WHERE name = :name', array(':name' => 'Ringo'))->fetch();
$this->assertEqual($person->name, 'Ringo', t('Name set correctly.'));
$this->assertEqual($person->age, $before_age + 4, t('Age set correctly.'));
$this->assertEqual($person->job, 'Musician', t('Job set correctly.'));
......@@ -787,14 +810,14 @@ class DatabaseUpdateComplexTestCase extends DatabaseTestCase {
* Test update with only expression values.
*/
function testUpdateOnlyExpression() {
$before_age = db_query("SELECT age FROM {test} WHERE name = 'Ringo'")->fetchField();
$before_age = db_query('SELECT age FROM {test} WHERE name = :name', array(':name' => 'Ringo'))->fetchField();
$num_updated = db_update('test')
->condition('name', 'Ringo')
->expression('age', 'age + :age', array(':age' => 4))
->execute();
$this->assertIdentical($num_updated, 1, t('Updated 1 record.'));
$after_age = db_query("SELECT age FROM {test} WHERE name = 'Ringo'")->fetchField();
$after_age = db_query('SELECT age FROM {test} WHERE name = :name', array(':name' => 'Ringo'))->fetchField();
$this->assertEqual($before_age + 4, $after_age, t('Age updated correctly'));
}
}
......@@ -818,13 +841,17 @@ class DatabaseUpdateLOBTestCase extends DatabaseTestCase {
function testUpdateOneBlob() {
$data = "This is\000a test.";
$this->assertTrue(strlen($data) === 15, t('Test data contains a NULL.'));
$id = db_insert('test_one_blob')->fields(array('blob1' => $data))->execute();
$id = db_insert('test_one_blob')
->fields(array('blob1' => $data))
->execute();
$data .= $data;
db_update('test_one_blob')->condition('id', $id)->fields(array('blob1' => $data))->execute();
db_update('test_one_blob')
->condition('id', $id)
->fields(array('blob1' => $data))
->execute();
$res = db_query('SELECT * FROM {test_one_blob} WHERE id = %d', $id);
$r = db_fetch_array($res);
$r = db_query('SELECT * FROM {test_one_blob} WHERE id = :id', array(':id' => $id))->fetchAssoc();
$this->assertTrue($r['blob1'] === $data, t('Can update a blob: id @id, @data.', array('@id' => $id, '@data' => serialize($r))));
}
......@@ -832,16 +859,19 @@ class DatabaseUpdateLOBTestCase extends DatabaseTestCase {
* Confirm that we can update two blob columns in the same table.
*/
function testUpdateMultipleBlob() {
$id = db_insert('test_two_blobs')->fields(array(
$id = db_insert('test_two_blobs')
->fields(array(
'blob1' => 'This is',
'blob2' => 'a test')
)
'blob2' => 'a test',
))
->execute();
db_update('test_two_blobs')->condition('id', $id)->fields(array('blob1' => 'and so', 'blob2' => 'is this'))->execute();
db_update('test_two_blobs')
->condition('id', $id)
->fields(array('blob1' => 'and so', 'blob2' => 'is this'))
->execute();
$res = db_query('SELECT * FROM {test_two_blobs} WHERE id = %d', $id);
$r = db_fetch_array($res);
$r = db_query('SELECT * FROM {test_two_blobs} WHERE id = :id', array(':id' => $id))->fetchAssoc();
$this->assertTrue($r['blob1'] === 'and so' && $r['blob2'] === 'is this', t('Can update multiple blobs per row.'));
}
}
......@@ -871,12 +901,14 @@ class DatabaseDeleteTruncateTestCase extends DatabaseTestCase {
* Confirm that we can delete a single record successfully.
*/
function testSimpleDelete() {
$num_records_before = db_query("SELECT COUNT(*) FROM {test}")->fetchField();
$num_records_before = db_query('SELECT COUNT(*) FROM {test}')->fetchField();
$num_deleted = db_delete('test')->condition('id', 1)->execute();
$num_deleted = db_delete('test')
->condition('id', 1)
->execute();
$this->assertIdentical($num_deleted, 1, t('Deleted 1 record.'));
$num_records_after = db_query("SELECT COUNT(*) FROM {test}")->fetchField();
$num_records_after = db_query('SELECT COUNT(*) FROM {test}')->fetchField();
$this->assertEqual($num_records_before, $num_records_after + $num_deleted, t('Deletion adds up.'));
}
......@@ -911,7 +943,7 @@ class DatabaseMergeTestCase extends DatabaseTestCase {
* Confirm that we can merge-insert a record successfully.
*/
function testMergeInsert() {
$num_records_before = db_query("SELECT COUNT(*) FROM {test_people}")->fetchField();
$num_records_before = db_query('SELECT COUNT(*) FROM {test_people}')->fetchField();
db_merge('test_people')
->key(array('job' => 'Presenter'))
......@@ -921,10 +953,10 @@ class DatabaseMergeTestCase extends DatabaseTestCase {
))
->execute();
$num_records_after = db_query("SELECT COUNT(*) FROM {test_people}")->fetchField();
$num_records_after = db_query('SELECT COUNT(*) FROM {test_people}')->fetchField();
$this->assertEqual($num_records_before + 1, $num_records_after, t('Merge inserted properly.'));
$person = db_query("SELECT * FROM {test_people} WHERE job = :job", array(':job' => 'Presenter'))->fetch();
$person = db_query('SELECT * FROM {test_people} WHERE job = :job', array(':job' => 'Presenter'))->fetch();
$this->assertEqual($person->name, 'Tiffany', t('Name set correctly.'));
$this->assertEqual($person->age, 31, t('Age set correctly.'));
$this->assertEqual($person->job, 'Presenter', t('Job set correctly.'));
......@@ -934,14 +966,20 @@ class DatabaseMergeTestCase extends DatabaseTestCase {
* Confirm that we can merge-update a record successfully.
*/
function testMergeUpdate() {
$num_records_before = db_query("SELECT COUNT(*) FROM {test_people}")->fetchField();
$num_records_before = db_query('SELECT COUNT(*) FROM {test_people}')->fetchField();
db_merge('test_people')->key(array('job' => 'Speaker'))->fields(array('age' => 31, 'name' => 'Tiffany'))->execute();
db_merge('test_people')
->key(array('job' => 'Speaker'))
->fields(array(
'age' => 31,
'name' => 'Tiffany',
))
->execute();
$num_records_after = db_query("SELECT COUNT(*) FROM {test_people}")->fetchField();
$num_records_after = db_query('SELECT COUNT(*) FROM {test_people}')->fetchField();
$this->assertEqual($num_records_before, $num_records_after, t('Merge updated properly.'));
$person = db_query("SELECT * FROM {test_people} WHERE job = :job", array(':job' => 'Speaker'))->fetch();
$person = db_query('SELECT * FROM {test_people} WHERE job = :job', array(':job' => 'Speaker'))->fetch();
$this->assertEqual($person->name, 'Tiffany', t('Name set correctly.'));
$this->assertEqual($person->age, 31, t('Age set correctly.'));
$this->assertEqual($person->job, 'Speaker', t('Job set correctly.'));
......@@ -951,14 +989,21 @@ class DatabaseMergeTestCase extends DatabaseTestCase {
* Confirm that we can merge-update a record successfully, with exclusion.
*/
function testMergeUpdateExcept() {
$num_records_before = db_query("SELECT COUNT(*) FROM {test_people}")->fetchField();
$num_records_before = db_query('SELECT COUNT(*) FROM {test_people}')->fetchField();
db_merge('test_people')->key(array('job' => 'Speaker'))->fields(array('age' => 31, 'name' => 'Tiffany'))->updateExcept('age')->execute();
db_merge('test_people')