From c503bb6a93ae1a568f435abe779edc95dbfce52d Mon Sep 17 00:00:00 2001 From: Alex Pott Date: Fri, 23 Nov 2018 21:08:40 +0000 Subject: [PATCH] Issue #3014950 by goodboy, mondrake, alexpott: Replace all db calls to file_managed table with Entity APIs --- core/modules/file/src/Tests/FileFieldTestBase.php | 2 +- .../file/tests/src/Functional/FileFieldRevisionTest.php | 6 ++++-- .../modules/file/tests/src/Functional/FileFieldTestBase.php | 2 +- .../file/tests/src/Functional/SaveUploadFormTest.php | 6 +++--- core/modules/file/tests/src/Functional/SaveUploadTest.php | 6 +++--- .../src/FunctionalJavascript/FileManagedFileElementTest.php | 3 +-- core/modules/file/tests/src/Kernel/DeleteTest.php | 3 ++- core/modules/image/src/Tests/ImageFieldTestBase.php | 2 +- .../image/tests/src/Functional/ImageFieldTestBase.php | 2 +- core/modules/user/tests/src/Functional/UserPictureTest.php | 3 ++- 10 files changed, 19 insertions(+), 16 deletions(-) diff --git a/core/modules/file/src/Tests/FileFieldTestBase.php b/core/modules/file/src/Tests/FileFieldTestBase.php index 0f82af3eac67..6d53cd4eae25 100644 --- a/core/modules/file/src/Tests/FileFieldTestBase.php +++ b/core/modules/file/src/Tests/FileFieldTestBase.php @@ -59,7 +59,7 @@ abstract class FileFieldTestBase extends WebTestBase { * Retrieves the fid of the last inserted file. */ public function getLastFileId() { - return (int) db_query('SELECT MAX(fid) FROM {file_managed}')->fetchField(); + return (int) \Drupal::entityQueryAggregate('file')->aggregate('fid', 'max')->execute()[0]['fid_max']; } /** diff --git a/core/modules/file/tests/src/Functional/FileFieldRevisionTest.php b/core/modules/file/tests/src/Functional/FileFieldRevisionTest.php index 3335623be972..332afc9ea7ed 100644 --- a/core/modules/file/tests/src/Functional/FileFieldRevisionTest.php +++ b/core/modules/file/tests/src/Functional/FileFieldRevisionTest.php @@ -118,7 +118,8 @@ class FileFieldRevisionTest extends FileFieldTestBase { // Call file_cron() to clean up the file. Make sure the changed timestamp // of the file is older than the system.file.temporary_maximum_age - // configuration value. + // configuration value. We use an UPDATE statement because using the API + // would set the timestamp. $connection = Database::getConnection(); $connection->update('file_managed') ->fields([ @@ -135,7 +136,8 @@ class FileFieldRevisionTest extends FileFieldTestBase { $this->drupalPostForm('node/' . $nid . '/delete', [], t('Delete')); // Call file_cron() to clean up the file. Make sure the changed timestamp // of the file is older than the system.file.temporary_maximum_age - // configuration value. + // configuration value. We use an UPDATE statement because using the API + // would set the timestamp. $connection->update('file_managed') ->fields([ 'changed' => REQUEST_TIME - ($this->config('system.file')->get('temporary_maximum_age') + 1), diff --git a/core/modules/file/tests/src/Functional/FileFieldTestBase.php b/core/modules/file/tests/src/Functional/FileFieldTestBase.php index dc2e8becc139..13f4d84148ec 100644 --- a/core/modules/file/tests/src/Functional/FileFieldTestBase.php +++ b/core/modules/file/tests/src/Functional/FileFieldTestBase.php @@ -60,7 +60,7 @@ abstract class FileFieldTestBase extends BrowserTestBase { * Retrieves the fid of the last inserted file. */ public function getLastFileId() { - return (int) db_query('SELECT MAX(fid) FROM {file_managed}')->fetchField(); + return (int) \Drupal::entityQueryAggregate('file')->aggregate('fid', 'max')->execute()[0]['fid_max']; } /** diff --git a/core/modules/file/tests/src/Functional/SaveUploadFormTest.php b/core/modules/file/tests/src/Functional/SaveUploadFormTest.php index e8b14e6ba120..2a43a9997f9e 100644 --- a/core/modules/file/tests/src/Functional/SaveUploadFormTest.php +++ b/core/modules/file/tests/src/Functional/SaveUploadFormTest.php @@ -66,7 +66,7 @@ class SaveUploadFormTest extends FileManagedTestBase { $this->phpfile = current($this->drupalGetTestFiles('php')); $this->assertTrue(is_file($this->phpfile->uri), 'The PHP file we are going to upload exists.'); - $this->maxFidBefore = db_query('SELECT MAX(fid) AS fid FROM {file_managed}')->fetchField(); + $this->maxFidBefore = (int) \Drupal::entityQueryAggregate('file')->aggregate('fid', 'max')->execute()[0]['fid_max']; /** @var \Drupal\Core\File\FileSystemInterface $file_system */ $file_system = \Drupal::service('file_system'); @@ -89,7 +89,7 @@ class SaveUploadFormTest extends FileManagedTestBase { * Tests the _file_save_upload_from_form() function. */ public function testNormal() { - $max_fid_after = db_query('SELECT MAX(fid) AS fid FROM {file_managed}')->fetchField(); + $max_fid_after = (int) \Drupal::entityQueryAggregate('file')->aggregate('fid', 'max')->execute()[0]['fid_max']; $this->assertTrue($max_fid_after > $this->maxFidBefore, 'A new file was created.'); $file1 = File::load($max_fid_after); $this->assertTrue($file1, 'Loaded the file.'); @@ -107,7 +107,7 @@ class SaveUploadFormTest extends FileManagedTestBase { $this->drupalPostForm('file-test/save_upload_from_form_test', $edit, t('Submit')); $this->assertResponse(200, 'Received a 200 response for posted test file.'); $this->assertRaw(t('You WIN!')); - $max_fid_after = db_query('SELECT MAX(fid) AS fid FROM {file_managed}')->fetchField(); + $max_fid_after = (int) \Drupal::entityQueryAggregate('file')->aggregate('fid', 'max')->execute()[0]['fid_max']; // Check that the correct hooks were called. $this->assertFileHooksCalled(['validate', 'insert']); diff --git a/core/modules/file/tests/src/Functional/SaveUploadTest.php b/core/modules/file/tests/src/Functional/SaveUploadTest.php index bebf47e076e2..552cfc628e69 100644 --- a/core/modules/file/tests/src/Functional/SaveUploadTest.php +++ b/core/modules/file/tests/src/Functional/SaveUploadTest.php @@ -61,7 +61,7 @@ class SaveUploadTest extends FileManagedTestBase { $this->phpfile = current($this->drupalGetTestFiles('php')); $this->assertTrue(is_file($this->phpfile->uri), 'The PHP file we are going to upload exists.'); - $this->maxFidBefore = db_query('SELECT MAX(fid) AS fid FROM {file_managed}')->fetchField(); + $this->maxFidBefore = (int) \Drupal::entityQueryAggregate('file')->aggregate('fid', 'max')->execute()[0]['fid_max']; // Upload with replace to guarantee there's something there. $edit = [ @@ -82,7 +82,7 @@ class SaveUploadTest extends FileManagedTestBase { * Test the file_save_upload() function. */ public function testNormal() { - $max_fid_after = db_query('SELECT MAX(fid) AS fid FROM {file_managed}')->fetchField(); + $max_fid_after = (int) \Drupal::entityQueryAggregate('file')->aggregate('fid', 'max')->execute()[0]['fid_max']; $this->assertTrue($max_fid_after > $this->maxFidBefore, 'A new file was created.'); $file1 = File::load($max_fid_after); $this->assertTrue($file1, 'Loaded the file.'); @@ -98,7 +98,7 @@ class SaveUploadTest extends FileManagedTestBase { $this->drupalPostForm('file-test/upload', $edit, t('Submit')); $this->assertResponse(200, 'Received a 200 response for posted test file.'); $this->assertRaw(t('You WIN!')); - $max_fid_after = db_query('SELECT MAX(fid) AS fid FROM {file_managed}')->fetchField(); + $max_fid_after = (int) \Drupal::entityQueryAggregate('file')->aggregate('fid', 'max')->execute()[0]['fid_max']; // Check that the correct hooks were called. $this->assertFileHooksCalled(['validate', 'insert']); diff --git a/core/modules/file/tests/src/FunctionalJavascript/FileManagedFileElementTest.php b/core/modules/file/tests/src/FunctionalJavascript/FileManagedFileElementTest.php index 8c4217811404..5ef877b4d048 100644 --- a/core/modules/file/tests/src/FunctionalJavascript/FileManagedFileElementTest.php +++ b/core/modules/file/tests/src/FunctionalJavascript/FileManagedFileElementTest.php @@ -2,7 +2,6 @@ namespace Drupal\Tests\file\FunctionalJavascript; -use Drupal\Core\Database\Database; use Drupal\FunctionalJavascriptTests\WebDriverTestBase; /** @@ -95,7 +94,7 @@ class FileManagedFileElementTest extends WebDriverTestBase { * Retrieves the fid of the last inserted file. */ protected function getLastFileId() { - return (int) Database::getConnection()->query('SELECT MAX(fid) FROM {file_managed}')->fetchField(); + return (int) \Drupal::entityQueryAggregate('file')->aggregate('fid', 'max')->execute()[0]['fid_max']; } } diff --git a/core/modules/file/tests/src/Kernel/DeleteTest.php b/core/modules/file/tests/src/Kernel/DeleteTest.php index f6e649bbf3fe..a82e009eb1c8 100644 --- a/core/modules/file/tests/src/Kernel/DeleteTest.php +++ b/core/modules/file/tests/src/Kernel/DeleteTest.php @@ -61,7 +61,8 @@ class DeleteTest extends FileManagedUnitTestBase { // Call file_cron() to clean up the file. Make sure the changed timestamp // of the file is older than the system.file.temporary_maximum_age - // configuration value. + // configuration value. We use an UPDATE statement because using the API + // would set the timestamp. Database::getConnection()->update('file_managed') ->fields([ 'changed' => REQUEST_TIME - ($this->config('system.file')->get('temporary_maximum_age') + 1), diff --git a/core/modules/image/src/Tests/ImageFieldTestBase.php b/core/modules/image/src/Tests/ImageFieldTestBase.php index a1611cb3522d..980b9e619c93 100644 --- a/core/modules/image/src/Tests/ImageFieldTestBase.php +++ b/core/modules/image/src/Tests/ImageFieldTestBase.php @@ -109,7 +109,7 @@ abstract class ImageFieldTestBase extends WebTestBase { * Retrieves the fid of the last inserted file. */ protected function getLastFileId() { - return (int) db_query('SELECT MAX(fid) FROM {file_managed}')->fetchField(); + return (int) \Drupal::entityQueryAggregate('file')->aggregate('fid', 'max')->execute()[0]['fid_max']; } } diff --git a/core/modules/image/tests/src/Functional/ImageFieldTestBase.php b/core/modules/image/tests/src/Functional/ImageFieldTestBase.php index ef79de850372..b736e8f46614 100644 --- a/core/modules/image/tests/src/Functional/ImageFieldTestBase.php +++ b/core/modules/image/tests/src/Functional/ImageFieldTestBase.php @@ -104,7 +104,7 @@ abstract class ImageFieldTestBase extends BrowserTestBase { * Retrieves the fid of the last inserted file. */ protected function getLastFileId() { - return (int) db_query('SELECT MAX(fid) FROM {file_managed}')->fetchField(); + return (int) \Drupal::entityQueryAggregate('file')->aggregate('fid', 'max')->execute()[0]['fid_max']; } } diff --git a/core/modules/user/tests/src/Functional/UserPictureTest.php b/core/modules/user/tests/src/Functional/UserPictureTest.php index e8941019e863..d24d788684c2 100644 --- a/core/modules/user/tests/src/Functional/UserPictureTest.php +++ b/core/modules/user/tests/src/Functional/UserPictureTest.php @@ -74,7 +74,8 @@ class UserPictureTest extends BrowserTestBase { // Call file_cron() to clean up the file. Make sure the timestamp // of the file is older than the system.file.temporary_maximum_age - // configuration value. + // configuration value. We use an UPDATE statement because using the API + // would set the timestamp. Database::getConnection()->update('file_managed') ->fields([ 'changed' => REQUEST_TIME - ($this->config('system.file')->get('temporary_maximum_age') + 1),