From de052f01c7e4d5d3fc03b1bc18902edccbd08466 Mon Sep 17 00:00:00 2001 From: catch Date: Mon, 27 Apr 2020 10:31:21 +0100 Subject: [PATCH] Issue #3130396 by longwave, jungle, dww, mondrake: Replace assertions involving calls to is_file with assertFileExists()/assertFileNotExists() --- .../file/tests/src/Functional/FileManagedTestBase.php | 2 +- .../file/tests/src/Functional/SaveUploadFormTest.php | 8 ++++---- core/modules/file/tests/src/Functional/SaveUploadTest.php | 6 +++--- core/modules/file/tests/src/Kernel/DeleteTest.php | 2 +- .../file/tests/src/Kernel/FileManagedUnitTestBase.php | 2 +- .../src/Functional/Form/StateValuesCleanAdvancedTest.php | 2 +- .../tests/src/Functional/System/RetrieveFileTest.php | 4 ++-- .../modules/user/tests/src/Functional/UserPictureTest.php | 2 +- .../Asset/ResolvedLibraryDefinitionsFilesMatchTest.php | 2 +- core/tests/Drupal/KernelTests/Core/File/DirectoryTest.php | 8 ++++---- core/tests/Drupal/KernelTests/Core/File/FileTestBase.php | 2 +- 11 files changed, 20 insertions(+), 20 deletions(-) diff --git a/core/modules/file/tests/src/Functional/FileManagedTestBase.php b/core/modules/file/tests/src/Functional/FileManagedTestBase.php index cc4bb9f05c1..db3b4fda5a4 100644 --- a/core/modules/file/tests/src/Functional/FileManagedTestBase.php +++ b/core/modules/file/tests/src/Functional/FileManagedTestBase.php @@ -194,7 +194,7 @@ abstract class FileManagedTestBase extends BrowserTestBase { } file_put_contents($filepath, $contents); - $this->assertTrue(is_file($filepath), t('The test file exists on the disk.'), 'Create test file'); + $this->assertFileExists($filepath); return $filepath; } diff --git a/core/modules/file/tests/src/Functional/SaveUploadFormTest.php b/core/modules/file/tests/src/Functional/SaveUploadFormTest.php index 92ce2d332e3..08fc8b31699 100644 --- a/core/modules/file/tests/src/Functional/SaveUploadFormTest.php +++ b/core/modules/file/tests/src/Functional/SaveUploadFormTest.php @@ -71,10 +71,10 @@ class SaveUploadFormTest extends FileManagedTestBase { $this->image = File::create((array) current($image_files)); list(, $this->imageExtension) = explode('.', $this->image->getFilename()); - $this->assertTrue(is_file($this->image->getFileUri()), "The image file we're going to upload exists."); + $this->assertFileExists($this->image->getFileUri()); $this->phpfile = current($this->drupalGetTestFiles('php')); - $this->assertTrue(is_file($this->phpfile->uri), 'The PHP file we are going to upload exists.'); + $this->assertFileExists($this->phpfile->uri); $this->maxFidBefore = (int) \Drupal::entityQueryAggregate('file')->aggregate('fid', 'max')->execute()[0]['fid_max']; @@ -143,7 +143,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!')); - $this->assertTrue(is_file('temporary://' . $dir . '/' . trim(\Drupal::service('file_system')->basename($image3_realpath)))); + $this->assertFileExists('temporary://' . $dir . '/' . trim(\Drupal::service('file_system')->basename($image3_realpath))); } /** @@ -456,7 +456,7 @@ class SaveUploadFormTest extends FileManagedTestBase { */ public function testCombinedErrorMessages() { $textfile = current($this->drupalGetTestFiles('text')); - $this->assertTrue(is_file($textfile->uri), 'The text file we are going to upload exists.'); + $this->assertFileExists($textfile->uri); /** @var \Drupal\Core\File\FileSystemInterface $file_system */ $file_system = \Drupal::service('file_system'); diff --git a/core/modules/file/tests/src/Functional/SaveUploadTest.php b/core/modules/file/tests/src/Functional/SaveUploadTest.php index 891de3b7121..ff27b289292 100644 --- a/core/modules/file/tests/src/Functional/SaveUploadTest.php +++ b/core/modules/file/tests/src/Functional/SaveUploadTest.php @@ -68,10 +68,10 @@ class SaveUploadTest extends FileManagedTestBase { $this->image = File::create((array) current($image_files)); list(, $this->imageExtension) = explode('.', $this->image->getFilename()); - $this->assertTrue(is_file($this->image->getFileUri()), "The image file we're going to upload exists."); + $this->assertFileExists($this->image->getFileUri()); $this->phpfile = current($this->drupalGetTestFiles('php')); - $this->assertTrue(is_file($this->phpfile->uri), 'The PHP file we are going to upload exists.'); + $this->assertFileExists($this->phpfile->uri); $this->maxFidBefore = (int) \Drupal::entityQueryAggregate('file')->aggregate('fid', 'max')->execute()[0]['fid_max']; @@ -136,7 +136,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!')); - $this->assertTrue(is_file('temporary://' . $dir . '/' . trim(\Drupal::service('file_system')->basename($image3_realpath)))); + $this->assertFileExists('temporary://' . $dir . '/' . trim(\Drupal::service('file_system')->basename($image3_realpath))); } /** diff --git a/core/modules/file/tests/src/Kernel/DeleteTest.php b/core/modules/file/tests/src/Kernel/DeleteTest.php index e8be244534b..23fd376bea0 100644 --- a/core/modules/file/tests/src/Kernel/DeleteTest.php +++ b/core/modules/file/tests/src/Kernel/DeleteTest.php @@ -19,7 +19,7 @@ class DeleteTest extends FileManagedUnitTestBase { $file = $this->createFile(); // Check that deletion removes the file and database record. - $this->assertTrue(is_file($file->getFileUri()), 'File exists.'); + $this->assertFileExists($file->getFileUri()); $file->delete(); $this->assertFileHooksCalled(['delete']); $this->assertFalse(file_exists($file->getFileUri()), 'Test file has actually been deleted.'); diff --git a/core/modules/file/tests/src/Kernel/FileManagedUnitTestBase.php b/core/modules/file/tests/src/Kernel/FileManagedUnitTestBase.php index 76ca34563f1..da56402fe0c 100644 --- a/core/modules/file/tests/src/Kernel/FileManagedUnitTestBase.php +++ b/core/modules/file/tests/src/Kernel/FileManagedUnitTestBase.php @@ -207,7 +207,7 @@ abstract class FileManagedUnitTestBase extends KernelTestBase { } file_put_contents($filepath, $contents); - $this->assertTrue(is_file($filepath), t('The test file exists on the disk.'), 'Create test file'); + $this->assertFileExists($filepath); return $filepath; } diff --git a/core/modules/system/tests/src/Functional/Form/StateValuesCleanAdvancedTest.php b/core/modules/system/tests/src/Functional/Form/StateValuesCleanAdvancedTest.php index 59cda249780..672312c0ad5 100644 --- a/core/modules/system/tests/src/Functional/Form/StateValuesCleanAdvancedTest.php +++ b/core/modules/system/tests/src/Functional/Form/StateValuesCleanAdvancedTest.php @@ -47,7 +47,7 @@ class StateValuesCleanAdvancedTest extends BrowserTestBase { $this->image = current($image_files); // Check if the physical file is there. - $this->assertTrue(is_file($this->image->uri), "The image file we're going to upload exists."); + $this->assertFileExists($this->image->uri); // "Browse" for the desired file. $edit = ['files[image]' => \Drupal::service('file_system')->realpath($this->image->uri)]; diff --git a/core/modules/system/tests/src/Functional/System/RetrieveFileTest.php b/core/modules/system/tests/src/Functional/System/RetrieveFileTest.php index 25db399666f..bda8c2571dc 100644 --- a/core/modules/system/tests/src/Functional/System/RetrieveFileTest.php +++ b/core/modules/system/tests/src/Functional/System/RetrieveFileTest.php @@ -38,7 +38,7 @@ class RetrieveFileTest extends BrowserTestBase { $encoded_filename = rawurlencode($filename); $this->assertEqual($retrieved_file, 'public://' . $encoded_filename, 'Sane path for downloaded file returned (public:// scheme).'); - $this->assertTrue(is_file($retrieved_file), 'Downloaded file does exist (public:// scheme).'); + $this->assertFileExists($retrieved_file); $this->assertEqual(filesize($retrieved_file), 7, 'File size of downloaded file is correct (public:// scheme).'); /** @var \Drupal\Core\File\FileSystemInterface $file_system */ $file_system = \Drupal::service('file_system'); @@ -48,7 +48,7 @@ class RetrieveFileTest extends BrowserTestBase { $file_system->mkdir($targetdir = 'temporary://' . $this->randomMachineName()); $retrieved_file = system_retrieve_file($url, $targetdir); $this->assertEqual($retrieved_file, "$targetdir/$encoded_filename", 'Sane path for downloaded file returned (temporary:// scheme).'); - $this->assertTrue(is_file($retrieved_file), 'Downloaded file does exist (temporary:// scheme).'); + $this->assertFileExists($retrieved_file); $this->assertEqual(filesize($retrieved_file), 7, 'File size of downloaded file is correct (temporary:// scheme).'); $file_system->delete($retrieved_file); diff --git a/core/modules/user/tests/src/Functional/UserPictureTest.php b/core/modules/user/tests/src/Functional/UserPictureTest.php index e4f65ee99cd..dff036b5baf 100644 --- a/core/modules/user/tests/src/Functional/UserPictureTest.php +++ b/core/modules/user/tests/src/Functional/UserPictureTest.php @@ -94,7 +94,7 @@ class UserPictureTest extends BrowserTestBase { $this->assertNull(File::load($file->id()), 'File was removed from the database.'); // Clear out PHP's file stat cache so we see the current value. clearstatcache(TRUE, $file->getFileUri()); - $this->assertFalse(is_file($file->getFileUri()), 'File was removed from the file system.'); + $this->assertFileNotExists($file->getFileUri(), 'File was removed from the file system.'); } /** diff --git a/core/tests/Drupal/KernelTests/Core/Asset/ResolvedLibraryDefinitionsFilesMatchTest.php b/core/tests/Drupal/KernelTests/Core/Asset/ResolvedLibraryDefinitionsFilesMatchTest.php index 7a2e28de201..00fde59aa93 100644 --- a/core/tests/Drupal/KernelTests/Core/Asset/ResolvedLibraryDefinitionsFilesMatchTest.php +++ b/core/tests/Drupal/KernelTests/Core/Asset/ResolvedLibraryDefinitionsFilesMatchTest.php @@ -173,7 +173,7 @@ class ResolvedLibraryDefinitionsFilesMatchTest extends KernelTestBase { $path = $this->root . '/' . $file; // Only check and assert each file path once. if (!isset($this->pathsChecked[$path])) { - $this->assertTrue(is_file($path), "$file file referenced from the $extension/$library_name library exists."); + $this->assertFileExists($path, "$file file referenced from the $extension/$library_name library does not exist."); $this->pathsChecked[$path] = TRUE; } } diff --git a/core/tests/Drupal/KernelTests/Core/File/DirectoryTest.php b/core/tests/Drupal/KernelTests/Core/File/DirectoryTest.php index 7047c1f09bd..8b2af3ff52b 100644 --- a/core/tests/Drupal/KernelTests/Core/File/DirectoryTest.php +++ b/core/tests/Drupal/KernelTests/Core/File/DirectoryTest.php @@ -110,15 +110,15 @@ class DirectoryTest extends FileTestBase { // Remove .htaccess file to then test that it gets re-created. @$file_system->unlink($default_scheme . '://.htaccess'); - $this->assertFalse(is_file($default_scheme . '://.htaccess'), 'Successfully removed the .htaccess file in the files directory.', 'File'); + $this->assertFileNotExists($default_scheme . '://.htaccess'); $this->container->get('file.htaccess_writer')->ensure(); - $this->assertTrue(is_file($default_scheme . '://.htaccess'), 'Successfully re-created the .htaccess file in the files directory.', 'File'); + $this->assertFileExists($default_scheme . '://.htaccess'); // Remove .htaccess file again to test that it is re-created by a cron run. @$file_system->unlink($default_scheme . '://.htaccess'); - $this->assertFalse(is_file($default_scheme . '://.htaccess'), 'Successfully removed the .htaccess file in the files directory.', 'File'); + $this->assertFileNotExists($default_scheme . '://.htaccess'); system_cron(); - $this->assertTrue(is_file($default_scheme . '://.htaccess'), 'Successfully re-created the .htaccess file in the files directory.', 'File'); + $this->assertFileExists($default_scheme . '://.htaccess'); // Verify contents of .htaccess file. $file = file_get_contents($default_scheme . '://.htaccess'); diff --git a/core/tests/Drupal/KernelTests/Core/File/FileTestBase.php b/core/tests/Drupal/KernelTests/Core/File/FileTestBase.php index 58ed88bb797..af5fe3e054e 100644 --- a/core/tests/Drupal/KernelTests/Core/File/FileTestBase.php +++ b/core/tests/Drupal/KernelTests/Core/File/FileTestBase.php @@ -196,7 +196,7 @@ abstract class FileTestBase extends KernelTestBase { } file_put_contents($filepath, $contents); - $this->assertTrue(is_file($filepath), t('The test file exists on the disk.'), 'Create test file'); + $this->assertFileExists($filepath); return $filepath; }