Issue #1774104 by sun: Replace all trivial drupal_write_record() calls with db_merge().
parent
dd4931b012
commit
b07fb6b076
|
@ -161,9 +161,11 @@ abstract class FileManagedTestBase extends WebTestBase {
|
|||
$file->status = 0;
|
||||
// Write the record directly rather than using the API so we don't invoke
|
||||
// the hooks.
|
||||
$this->assertNotIdentical(drupal_write_record('file_managed', $file), FALSE, 'The file was added to the database.', 'Create test file');
|
||||
|
||||
return entity_create('file', (array) $file);
|
||||
$file = (array) $file;
|
||||
$file['fid'] = db_insert('file_managed')
|
||||
->fields($file)
|
||||
->execute();
|
||||
return entity_create('file', $file);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -24,19 +24,19 @@ class SpaceUsedTest extends FileManagedUnitTestBase {
|
|||
|
||||
// Create records for a couple of users with different sizes.
|
||||
$file = array('uid' => 2, 'uri' => 'public://example1.txt', 'filesize' => 50, 'status' => FILE_STATUS_PERMANENT);
|
||||
drupal_write_record('file_managed', $file);
|
||||
db_insert('file_managed')->fields($file)->execute();
|
||||
$file = array('uid' => 2, 'uri' => 'public://example2.txt', 'filesize' => 20, 'status' => FILE_STATUS_PERMANENT);
|
||||
drupal_write_record('file_managed', $file);
|
||||
db_insert('file_managed')->fields($file)->execute();
|
||||
$file = array('uid' => 3, 'uri' => 'public://example3.txt', 'filesize' => 100, 'status' => FILE_STATUS_PERMANENT);
|
||||
drupal_write_record('file_managed', $file);
|
||||
db_insert('file_managed')->fields($file)->execute();
|
||||
$file = array('uid' => 3, 'uri' => 'public://example4.txt', 'filesize' => 200, 'status' => FILE_STATUS_PERMANENT);
|
||||
drupal_write_record('file_managed', $file);
|
||||
db_insert('file_managed')->fields($file)->execute();
|
||||
|
||||
// Now create some non-permanent files.
|
||||
$file = array('uid' => 2, 'uri' => 'public://example5.txt', 'filesize' => 1, 'status' => 0);
|
||||
drupal_write_record('file_managed', $file);
|
||||
db_insert('file_managed')->fields($file)->execute();
|
||||
$file = array('uid' => 3, 'uri' => 'public://example6.txt', 'filesize' => 3, 'status' => 0);
|
||||
drupal_write_record('file_managed', $file);
|
||||
db_insert('file_managed')->fields($file)->execute();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -270,8 +270,8 @@ EOF;
|
|||
'version' => '',
|
||||
);
|
||||
foreach ($data as $file) {
|
||||
$file = (object) array_merge($default, $file);
|
||||
drupal_write_record('locale_file', $file);
|
||||
$file = array_merge($default, $file);
|
||||
db_insert('locale_file')->fields($file)->execute();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -866,22 +866,23 @@ function locale_translation_get_file_history() {
|
|||
*
|
||||
* @return integer
|
||||
* FALSE on failure. Otherwise SAVED_NEW or SAVED_UPDATED.
|
||||
*
|
||||
* @see drupal_write_record()
|
||||
*/
|
||||
function locale_translation_update_file_history($file) {
|
||||
// Update or write new record.
|
||||
if (db_query("SELECT project FROM {locale_file} WHERE project = :project AND langcode = :langcode", array(':project' => $file->project, ':langcode' => $file->langcode))->fetchField()) {
|
||||
$update = array('project', 'langcode');
|
||||
}
|
||||
else {
|
||||
$update = array();
|
||||
}
|
||||
$result = drupal_write_record('locale_file', $file, $update);
|
||||
$status = db_merge('locale_file')
|
||||
->key(array(
|
||||
'project' => $file->project,
|
||||
'langcode' => $file->langcode,
|
||||
))
|
||||
->fields(array(
|
||||
'version' => $file->version,
|
||||
'timestamp' => $file->timestamp,
|
||||
'last_checked' => $file->last_checked,
|
||||
))
|
||||
->execute();
|
||||
// The file history has changed, flush the static cache now.
|
||||
// @todo Can we make this more fine grained?
|
||||
drupal_static_reset('locale_translation_get_file_history');
|
||||
return $result;
|
||||
return $status;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -111,7 +111,7 @@ function hook_menu_link_insert(\Drupal\menu_link\Entity\MenuLink $menu_link) {
|
|||
$record['mlid'] = $menu_link->id();
|
||||
$record['menu_name'] = $menu_link->menu_name;
|
||||
$record['status'] = 0;
|
||||
drupal_write_record('menu_example', $record);
|
||||
db_insert('menu_example')->fields($record)->execute();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -144,7 +144,7 @@ class NodeQueryAlterTest extends NodeTestBase {
|
|||
'grant_update' => 0,
|
||||
'grant_delete' => 0,
|
||||
);
|
||||
drupal_write_record('node_access', $record);
|
||||
db_insert('node_access')->fields($record)->execute();
|
||||
|
||||
// Test that the noAccessUser still doesn't have the 'view'
|
||||
// privilege after adding the node_access record.
|
||||
|
|
|
@ -150,7 +150,7 @@ use Drupal\Component\Utility\Xss;
|
|||
* 'grant_update' => 0,
|
||||
* 'grant_delete' => 0,
|
||||
* );
|
||||
* drupal_write_record('node_access', $record);
|
||||
* db_insert('node_access')->fields($record)->execute();
|
||||
* @endcode
|
||||
* And then in its hook_node_grants() implementation, it would need to return:
|
||||
* @code
|
||||
|
|
|
@ -381,12 +381,10 @@ function hook_entity_predelete(Drupal\Core\Entity\EntityInterface $entity) {
|
|||
->fetchField();
|
||||
|
||||
// Log the count in a table that records this statistic for deleted entities.
|
||||
$ref_count_record = (object) array(
|
||||
'count' => $count,
|
||||
'type' => $type,
|
||||
'id' => $id,
|
||||
);
|
||||
drupal_write_record('example_deleted_entity_statistics', $ref_count_record);
|
||||
db_merge('example_deleted_entity_statistics')
|
||||
->key(array('type' => $type, 'id' => $id))
|
||||
->fields(array('count' => $count))
|
||||
->execute();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -31,22 +31,12 @@ class InstallTest extends WebTestBase {
|
|||
}
|
||||
|
||||
/**
|
||||
* Test that calls to drupal_write_record() work during module installation.
|
||||
*
|
||||
* This is a useful function to test because modules often use it to insert
|
||||
* initial data in their database tables when they are being installed or
|
||||
* enabled. Furthermore, drupal_write_record() relies on the module schema
|
||||
* information being available, so this also checks that the data from one of
|
||||
* the module's hook implementations, in particular hook_schema(), is
|
||||
* properly available during this time. Therefore, this test helps ensure
|
||||
* that modules are fully functional while Drupal is installing and enabling
|
||||
* them.
|
||||
* Verify that drupal_get_schema() can be used during module installation.
|
||||
*/
|
||||
public function testDrupalWriteRecord() {
|
||||
// Check for data that was inserted using drupal_write_record() while the
|
||||
// 'module_test' module was being installed and enabled.
|
||||
$data = db_query("SELECT data FROM {module_test}")->fetchCol();
|
||||
$this->assertTrue(in_array('Data inserted in hook_install()', $data), 'Data inserted using drupal_write_record() in hook_install() is correctly saved.');
|
||||
public function testGetSchemaAtInstallTime() {
|
||||
// @see module_test_install()
|
||||
$value = db_query("SELECT data FROM {module_test}")->fetchField();
|
||||
$this->assertIdentical($value, 'varchar');
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -28,6 +28,10 @@ function module_test_schema() {
|
|||
* Implements hook_install().
|
||||
*/
|
||||
function module_test_install() {
|
||||
$record = array('data' => 'Data inserted in hook_install()');
|
||||
drupal_write_record('module_test', $record);
|
||||
$schema = drupal_get_schema('module_test');
|
||||
db_insert('module_test')
|
||||
->fields(array(
|
||||
'data' => $schema['fields']['data']['type'],
|
||||
))
|
||||
->execute();
|
||||
}
|
||||
|
|
|
@ -64,7 +64,7 @@ class CacheTest extends PluginTestBase {
|
|||
'age' => 29,
|
||||
'job' => 'Banjo',
|
||||
);
|
||||
drupal_write_record('views_test_data', $record);
|
||||
db_insert('views_test_data')->fields($record)->execute();
|
||||
|
||||
// The Result should be the same as before, because of the caching.
|
||||
$view = views_get_view('test_cache');
|
||||
|
@ -106,8 +106,7 @@ class CacheTest extends PluginTestBase {
|
|||
'age' => 29,
|
||||
'job' => 'Banjo',
|
||||
);
|
||||
|
||||
drupal_write_record('views_test_data', $record);
|
||||
db_insert('views_test_data')->fields($record)->execute();
|
||||
|
||||
// The Result changes, because the view is not cached.
|
||||
$view = views_get_view('test_cache');
|
||||
|
|
Loading…
Reference in New Issue