Skip to content

Commit

Permalink
Issue #3131088 by Spokje, longwave: Replace assertions involving call…
Browse files Browse the repository at this point in the history
…s to file_exists with assertFileExists()/assertFileNotExists() or assertDirectoryExists()/assertDirectoryNotExists()
  • Loading branch information
catch committed Apr 28, 2020
1 parent 925bce6 commit 2b77fa0
Show file tree
Hide file tree
Showing 30 changed files with 136 additions and 140 deletions.
4 changes: 2 additions & 2 deletions modules/file/tests/src/Kernel/CopyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ public function testNormal() {

$this->assertDifferentFile($source, $result);
$this->assertEqual($result->getFileUri(), $desired_uri, 'The copied file entity has the desired filepath.');
$this->assertTrue(file_exists($source->getFileUri()), 'The original file still exists.');
$this->assertTrue(file_exists($result->getFileUri()), 'The copied file exists.');
$this->assertFileExists($source->getFileUri());
$this->assertFileExists($result->getFileUri());

// Reload the file from the database and check that the changes were
// actually saved.
Expand Down
10 changes: 5 additions & 5 deletions modules/file/tests/src/Kernel/DeleteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function testUnused() {
$this->assertFileExists($file->getFileUri());
$file->delete();
$this->assertFileHooksCalled(['delete']);
$this->assertFalse(file_exists($file->getFileUri()), 'Test file has actually been deleted.');
$this->assertFileNotExists($file->getFileUri());
$this->assertNull(File::load($file->id()), 'File was removed from the database.');
}

Expand All @@ -43,7 +43,7 @@ public function testInUse() {
$file_usage->delete($file, 'testing', 'test', 1);
$usage = $file_usage->listUsage($file);
$this->assertEqual($usage['testing']['test'], [1 => 1], 'Test file is still in use.');
$this->assertTrue(file_exists($file->getFileUri()), 'File still exists on the disk.');
$this->assertFileExists($file->getFileUri());
$this->assertNotEmpty(File::load($file->id()), 'File still exists in the database.');

// Clear out the call to hook_file_load().
Expand All @@ -53,7 +53,7 @@ public function testInUse() {
$usage = $file_usage->listUsage($file);
$this->assertFileHooksCalled(['load', 'update']);
$this->assertTrue(empty($usage), 'File usage data was removed.');
$this->assertTrue(file_exists($file->getFileUri()), 'File still exists on the disk.');
$this->assertFileExists($file->getFileUri());
$file = File::load($file->id());
$this->assertNotEmpty($file, 'File still exists in the database.');
$this->assertTrue($file->isTemporary(), 'File is temporary.');
Expand All @@ -73,7 +73,7 @@ public function testInUse() {

// file_cron() loads
$this->assertFileHooksCalled(['delete']);
$this->assertFalse(file_exists($file->getFileUri()), 'File has been deleted after its last usage was removed.');
$this->assertFileNotExists($file->getFileUri());
$this->assertNull(File::load($file->id()), 'File was removed from the database.');
}

Expand All @@ -84,7 +84,7 @@ public function testCronDeleteNonExistingTemporary() {
$file = $this->createFile();
// Delete the file, but leave it in the file_managed table.
\Drupal::service('file_system')->delete($file->getFileUri());
$this->assertFalse(file_exists($file->getFileUri()), 'File is deleted from the filesystem.');
$this->assertFileNotExists($file->getFileUri());
$this->assertInstanceOf(File::class, File::load($file->id()));

// Call file_cron() to clean up the file. Make sure the changed timestamp
Expand Down
8 changes: 4 additions & 4 deletions modules/file/tests/src/Kernel/MoveTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function testNormal() {

// Check the return status and that the contents changed.
$this->assertNotFalse($result, 'File moved successfully.');
$this->assertFalse(file_exists($source->getFileUri()));
$this->assertFileNotExists($source->getFileUri());
$this->assertEqual($contents, file_get_contents($result->getFileUri()), 'Contents of file correctly written.');

// Check that the correct hooks were called.
Expand Down Expand Up @@ -59,7 +59,7 @@ public function testExistingRename() {

// Check the return status and that the contents changed.
$this->assertNotFalse($result, 'File moved successfully.');
$this->assertFalse(file_exists($source->getFileUri()));
$this->assertFileNotExists($source->getFileUri());
$this->assertEqual($contents, file_get_contents($result->getFileUri()), 'Contents of file correctly written.');

// Check that the correct hooks were called.
Expand Down Expand Up @@ -94,7 +94,7 @@ public function testExistingReplace() {

// Look at the results.
$this->assertEqual($contents, file_get_contents($result->getFileUri()), 'Contents of file were overwritten.');
$this->assertFalse(file_exists($source->getFileUri()));
$this->assertFileNotExists($source->getFileUri());
$this->assertNotEmpty($result, 'File moved successfully.');

// Check that the correct hooks were called.
Expand Down Expand Up @@ -147,7 +147,7 @@ public function testExistingError() {

// Check the return status and that the contents did not change.
$this->assertFalse($result, 'File move failed.');
$this->assertTrue(file_exists($source->getFileUri()));
$this->assertFileExists($source->getFileUri());
$this->assertEqual($contents, file_get_contents($target->getFileUri()), 'Contents of file were not altered.');

// Check that no hooks were called while failing.
Expand Down
32 changes: 16 additions & 16 deletions modules/file/tests/src/Kernel/UsageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,27 +163,27 @@ public function createTempFiles() {
])
->condition('fid', $temp_old->id())
->execute();
$this->assertTrue(file_exists($temp_old->getFileUri()), 'Old temp file was created correctly.');
$this->assertFileExists($temp_old->getFileUri());

// Temporary file that is new.
$temp_new = file_save_data('');
$connection->update('file_managed')
->fields(['status' => 0])
->condition('fid', $temp_new->id())
->execute();
$this->assertTrue(file_exists($temp_new->getFileUri()), 'New temp file was created correctly.');
$this->assertFileExists($temp_new->getFileUri());

// Permanent file that is old.
$perm_old = file_save_data('');
$connection->update('file_managed')
->fields(['changed' => REQUEST_TIME - $this->config('system.file')->get('temporary_maximum_age') - 1])
->condition('fid', $temp_old->id())
->execute();
$this->assertTrue(file_exists($perm_old->getFileUri()), 'Old permanent file was created correctly.');
$this->assertFileExists($perm_old->getFileUri());

// Permanent file that is new.
$perm_new = file_save_data('');
$this->assertTrue(file_exists($perm_new->getFileUri()), 'New permanent file was created correctly.');
$this->assertFileExists($perm_new->getFileUri());
return [$temp_old, $temp_new, $perm_old, $perm_new];
}

Expand All @@ -195,10 +195,10 @@ public function testTempFileCleanupDefault() {

// Run cron and then ensure that only the old, temp file was deleted.
$this->container->get('cron')->run();
$this->assertFalse(file_exists($temp_old->getFileUri()), 'Old temp file was correctly removed.');
$this->assertTrue(file_exists($temp_new->getFileUri()), 'New temp file was correctly ignored.');
$this->assertTrue(file_exists($perm_old->getFileUri()), 'Old permanent file was correctly ignored.');
$this->assertTrue(file_exists($perm_new->getFileUri()), 'New permanent file was correctly ignored.');
$this->assertFileNotExists($temp_old->getFileUri());
$this->assertFileExists($temp_new->getFileUri());
$this->assertFileExists($perm_old->getFileUri());
$this->assertFileExists($perm_new->getFileUri());
}

/**
Expand All @@ -214,10 +214,10 @@ public function testTempFileNoCleanup() {

// Run cron and then ensure that no file was deleted.
$this->container->get('cron')->run();
$this->assertTrue(file_exists($temp_old->getFileUri()), 'Old temp file was correctly ignored.');
$this->assertTrue(file_exists($temp_new->getFileUri()), 'New temp file was correctly ignored.');
$this->assertTrue(file_exists($perm_old->getFileUri()), 'Old permanent file was correctly ignored.');
$this->assertTrue(file_exists($perm_new->getFileUri()), 'New permanent file was correctly ignored.');
$this->assertFileExists($temp_old->getFileUri());
$this->assertFileExists($temp_new->getFileUri());
$this->assertFileExists($perm_old->getFileUri());
$this->assertFileExists($perm_new->getFileUri());
}

/**
Expand All @@ -233,10 +233,10 @@ public function testTempFileCustomCleanup() {

// Run cron and then ensure that more files were deleted.
$this->container->get('cron')->run();
$this->assertTrue(file_exists($temp_old->getFileUri()), 'Old temp file was correctly ignored.');
$this->assertTrue(file_exists($temp_new->getFileUri()), 'New temp file was correctly ignored.');
$this->assertTrue(file_exists($perm_old->getFileUri()), 'Old permanent file was correctly ignored.');
$this->assertTrue(file_exists($perm_new->getFileUri()), 'New permanent file was correctly ignored.');
$this->assertFileExists($temp_old->getFileUri());
$this->assertFileExists($temp_new->getFileUri());
$this->assertFileExists($perm_old->getFileUri());
$this->assertFileExists($perm_new->getFileUri());
}

/**
Expand Down
4 changes: 2 additions & 2 deletions modules/file/tests/src/Kernel/ValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ public function testFileValidateExtensions() {
* This ensures a specific file is actually an image.
*/
public function testFileValidateIsImage() {
$this->assertTrue(file_exists($this->image->getFileUri()), 'The image being tested exists.', 'File');
$this->assertFileExists($this->image->getFileUri());
$errors = file_validate_is_image($this->image);
$this->assertEqual(count($errors), 0, 'No error reported for our image file.', 'File');

$this->assertTrue(file_exists($this->nonImage->getFileUri()), 'The non-image being tested exists.', 'File');
$this->assertFileExists($this->nonImage->getFileUri());
$errors = file_validate_is_image($this->nonImage);
$this->assertEqual(count($errors), 1, 'An error reported for our non-image file.', 'File');
}
Expand Down
6 changes: 3 additions & 3 deletions modules/image/tests/src/Functional/FileMoveTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,18 @@ public function testNormal() {
$style->createDerivative($original_uri, $derivative_uri);

// Check if derivative image exists.
$this->assertTrue(file_exists($derivative_uri), 'Make sure derivative image is generated successfully.');
$this->assertFileExists($derivative_uri);

// Clone the object so we don't have to worry about the function changing
// our reference copy.
$desired_filepath = 'public://' . $this->randomMachineName();
$result = file_move(clone $file, $desired_filepath, FileSystemInterface::EXISTS_ERROR);

// Check if image has been moved.
$this->assertTrue(file_exists($result->getFileUri()), 'Make sure image is moved successfully.');
$this->assertFileExists($result->getFileUri());

// Check if derivative image has been flushed.
$this->assertFalse(file_exists($derivative_uri), 'Make sure derivative image has been flushed.');
$this->assertFileNotExists($derivative_uri);
}

}
40 changes: 20 additions & 20 deletions modules/image/tests/src/Functional/ImageDimensionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ public function testImageDimensions() {
$style->addImageEffect($effect);
$style->save();
$this->assertEqual($this->getImageTag($variables), '<img src="' . $url . '" width="120" height="60" alt="" class="image-style-test" />');
$this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.');
$this->assertFileNotExists($generated_uri);
$this->drupalGet($this->getAbsoluteUrl($url));
$this->assertResponse(200, 'Image was generated at the URL.');
$this->assertTrue(file_exists($generated_uri), 'Generated file does exist after we accessed it.');
$this->assertFileExists($generated_uri);
$image_file = $image_factory->get($generated_uri);
$this->assertEqual($image_file->getWidth(), 120);
$this->assertEqual($image_file->getHeight(), 60);
Expand All @@ -99,10 +99,10 @@ public function testImageDimensions() {
$style->addImageEffect($effect);
$style->save();
$this->assertEqual($this->getImageTag($variables), '<img src="' . $url . '" width="60" height="120" alt="" class="image-style-test" />');
$this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.');
$this->assertFileNotExists($generated_uri);
$this->drupalGet($this->getAbsoluteUrl($url));
$this->assertResponse(200, 'Image was generated at the URL.');
$this->assertTrue(file_exists($generated_uri), 'Generated file does exist after we accessed it.');
$this->assertFileExists($generated_uri);
$image_file = $image_factory->get($generated_uri);
$this->assertEqual($image_file->getWidth(), 60);
$this->assertEqual($image_file->getHeight(), 120);
Expand All @@ -121,10 +121,10 @@ public function testImageDimensions() {
$style->addImageEffect($effect);
$style->save();
$this->assertEqual($this->getImageTag($variables), '<img src="' . $url . '" width="45" height="90" alt="" class="image-style-test" />');
$this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.');
$this->assertFileNotExists($generated_uri);
$this->drupalGet($this->getAbsoluteUrl($url));
$this->assertResponse(200, 'Image was generated at the URL.');
$this->assertTrue(file_exists($generated_uri), 'Generated file does exist after we accessed it.');
$this->assertFileExists($generated_uri);
$image_file = $image_factory->get($generated_uri);
$this->assertEqual($image_file->getWidth(), 45);
$this->assertEqual($image_file->getHeight(), 90);
Expand All @@ -143,10 +143,10 @@ public function testImageDimensions() {
$style->addImageEffect($effect);
$style->save();
$this->assertEqual($this->getImageTag($variables), '<img src="' . $url . '" width="45" height="90" alt="" class="image-style-test" />');
$this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.');
$this->assertFileNotExists($generated_uri);
$this->drupalGet($this->getAbsoluteUrl($url));
$this->assertResponse(200, 'Image was generated at the URL.');
$this->assertTrue(file_exists($generated_uri), 'Generated file does exist after we accessed it.');
$this->assertFileExists($generated_uri);
$image_file = $image_factory->get($generated_uri);
$this->assertEqual($image_file->getWidth(), 45);
$this->assertEqual($image_file->getHeight(), 90);
Expand All @@ -161,10 +161,10 @@ public function testImageDimensions() {
$style->addImageEffect($effect);
$style->save();
$this->assertEqual($this->getImageTag($variables), '<img src="' . $url . '" width="45" height="90" alt="" class="image-style-test" />');
$this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.');
$this->assertFileNotExists($generated_uri);
$this->drupalGet($this->getAbsoluteUrl($url));
$this->assertResponse(200, 'Image was generated at the URL.');
$this->assertTrue(file_exists($generated_uri), 'Generated file does exist after we accessed it.');
$this->assertFileExists($generated_uri);
$image_file = $image_factory->get($generated_uri);
$this->assertEqual($image_file->getWidth(), 45);
$this->assertEqual($image_file->getHeight(), 90);
Expand All @@ -182,10 +182,10 @@ public function testImageDimensions() {
$style->addImageEffect($effect);
$style->save();
$this->assertEqual($this->getImageTag($variables), '<img src="' . $url . '" alt="" class="image-style-test" />');
$this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.');
$this->assertFileNotExists($generated_uri);
$this->drupalGet($this->getAbsoluteUrl($url));
$this->assertResponse(200, 'Image was generated at the URL.');
$this->assertTrue(file_exists($generated_uri), 'Generated file does exist after we accessed it.');
$this->assertFileExists($generated_uri);

// Add a crop effect.
$effect = [
Expand All @@ -201,10 +201,10 @@ public function testImageDimensions() {
$style->addImageEffect($effect);
$style->save();
$this->assertEqual($this->getImageTag($variables), '<img src="' . $url . '" width="30" height="30" alt="" class="image-style-test" />');
$this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.');
$this->assertFileNotExists($generated_uri);
$this->drupalGet($this->getAbsoluteUrl($url));
$this->assertResponse(200, 'Image was generated at the URL.');
$this->assertTrue(file_exists($generated_uri), 'Generated file does exist after we accessed it.');
$this->assertFileExists($generated_uri);
$image_file = $image_factory->get($generated_uri);
$this->assertEqual($image_file->getWidth(), 30);
$this->assertEqual($image_file->getHeight(), 30);
Expand All @@ -224,10 +224,10 @@ public function testImageDimensions() {
// @todo Uncomment this once
// https://www.drupal.org/project/drupal/issues/2670966 is resolved.
// $this->assertEqual($this->getImageTag($variables), '<img src="' . $url . '" width="41" height="41" alt="" class="image-style-test" />');
$this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.');
$this->assertFileNotExists($generated_uri);
$this->drupalGet($this->getAbsoluteUrl($url));
$this->assertResponse(200, 'Image was generated at the URL.');
$this->assertTrue(file_exists($generated_uri), 'Generated file does exist after we accessed it.');
$this->assertFileExists($generated_uri);
$image_file = $image_factory->get($generated_uri);
// @todo Uncomment this once
// https://www.drupal.org/project/drupal/issues/2670966 is resolved.
Expand Down Expand Up @@ -268,10 +268,10 @@ public function testImageDimensions() {
$generated_uri = 'public://styles/test_uri/public/' . $file_system->basename($original_uri);
$url = file_url_transform_relative($style->buildUrl($original_uri));
$this->assertEqual($this->getImageTag($variables), '<img src="' . $url . '" width="100" height="100" alt="" class="image-style-test-uri" />');
$this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.');
$this->assertFileNotExists($generated_uri);
$this->drupalGet($this->getAbsoluteUrl($url));
$this->assertResponse(200, 'Image was generated at the URL.');
$this->assertTrue(file_exists($generated_uri), 'Generated file does exist after we accessed it.');
$this->assertFileExists($generated_uri);
$image_file = $image_factory->get($generated_uri);
$this->assertEqual($image_file->getWidth(), 100);
$this->assertEqual($image_file->getHeight(), 100);
Expand All @@ -282,10 +282,10 @@ public function testImageDimensions() {
$url = file_url_transform_relative($style->buildUrl($original_uri));
$variables['#uri'] = $original_uri;
$this->assertEqual($this->getImageTag($variables), '<img src="' . $url . '" width="50" height="50" alt="" class="image-style-test-uri" />');
$this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.');
$this->assertFileNotExists($generated_uri);
$this->drupalGet($this->getAbsoluteUrl($url));
$this->assertResponse(200, 'Image was generated at the URL.');
$this->assertTrue(file_exists($generated_uri), 'Generated file does exist after we accessed it.');
$this->assertFileExists($generated_uri);
$image_file = $image_factory->get($generated_uri);
$this->assertEqual($image_file->getWidth(), 50);
$this->assertEqual($image_file->getHeight(), 50);
Expand Down

0 comments on commit 2b77fa0

Please sign in to comment.