Unverified Commit 3954b06d authored by alexpott's avatar alexpott

Issue #3031539 by mondrake, amateescu, larowlan, Berdir: FileFieldTestBase...

Issue #3031539 by mondrake, amateescu, larowlan, Berdir: FileFieldTestBase tests fail under PHPUnit 7
parent 1546b4b8
...@@ -88,7 +88,7 @@ public function testAnonymousNodeWithFile() { ...@@ -88,7 +88,7 @@ public function testAnonymousNodeWithFile() {
$this->assertNotEqual($nid, 0, 'The node ID was extracted from the URL.'); $this->assertNotEqual($nid, 0, 'The node ID was extracted from the URL.');
$node = Node::load($nid); $node = Node::load($nid);
$this->assertNotEqual($node, NULL, 'The node was loaded successfully.'); $this->assertNotEqual($node, NULL, 'The node was loaded successfully.');
$this->assertFileExists(File::load($node->field_image->target_id), 'The image was uploaded successfully.'); $this->assertFileExists(File::load($node->field_image->target_id)->getFileUri(), 'The image was uploaded successfully.');
} }
} }
...@@ -162,7 +162,7 @@ protected function doTestNodeWithFileWithoutTitle() { ...@@ -162,7 +162,7 @@ protected function doTestNodeWithFileWithoutTitle() {
$this->assertNotEqual($nid, 0, 'The node ID was extracted from the URL.'); $this->assertNotEqual($nid, 0, 'The node ID was extracted from the URL.');
$node = Node::load($nid); $node = Node::load($nid);
$this->assertNotEqual($node, NULL, 'The node was loaded successfully.'); $this->assertNotEqual($node, NULL, 'The node was loaded successfully.');
$this->assertFileExists(File::load($node->field_image->target_id), 'The image was uploaded successfully.'); $this->assertFileExists(File::load($node->field_image->target_id)->getFileUri(), 'The image was uploaded successfully.');
} }
} }
......
...@@ -46,7 +46,7 @@ public function testRevisions() { ...@@ -46,7 +46,7 @@ public function testRevisions() {
$node = $node_storage->load($nid); $node = $node_storage->load($nid);
$node_file_r1 = File::load($node->{$field_name}->target_id); $node_file_r1 = File::load($node->{$field_name}->target_id);
$node_vid_r1 = $node->getRevisionId(); $node_vid_r1 = $node->getRevisionId();
$this->assertFileExists($node_file_r1, 'New file saved to disk on node creation.'); $this->assertFileExists($node_file_r1->getFileUri(), 'New file saved to disk on node creation.');
$this->assertFileEntryExists($node_file_r1, 'File entry exists in database on node creation.'); $this->assertFileEntryExists($node_file_r1, 'File entry exists in database on node creation.');
$this->assertFileIsPermanent($node_file_r1, 'File is permanent.'); $this->assertFileIsPermanent($node_file_r1, 'File is permanent.');
...@@ -56,7 +56,7 @@ public function testRevisions() { ...@@ -56,7 +56,7 @@ public function testRevisions() {
$node = $node_storage->load($nid); $node = $node_storage->load($nid);
$node_file_r2 = File::load($node->{$field_name}->target_id); $node_file_r2 = File::load($node->{$field_name}->target_id);
$node_vid_r2 = $node->getRevisionId(); $node_vid_r2 = $node->getRevisionId();
$this->assertFileExists($node_file_r2, 'Replacement file exists on disk after creating new revision.'); $this->assertFileExists($node_file_r2->getFileUri(), 'Replacement file exists on disk after creating new revision.');
$this->assertFileEntryExists($node_file_r2, 'Replacement file entry exists in database after creating new revision.'); $this->assertFileEntryExists($node_file_r2, 'Replacement file entry exists in database after creating new revision.');
$this->assertFileIsPermanent($node_file_r2, 'Replacement file is permanent.'); $this->assertFileIsPermanent($node_file_r2, 'Replacement file is permanent.');
...@@ -64,7 +64,7 @@ public function testRevisions() { ...@@ -64,7 +64,7 @@ public function testRevisions() {
$node = node_revision_load($node_vid_r1); $node = node_revision_load($node_vid_r1);
$current_file = File::load($node->{$field_name}->target_id); $current_file = File::load($node->{$field_name}->target_id);
$this->assertEqual($node_file_r1->id(), $current_file->id(), 'Original file still in place after replacing file in new revision.'); $this->assertEqual($node_file_r1->id(), $current_file->id(), 'Original file still in place after replacing file in new revision.');
$this->assertFileExists($node_file_r1, 'Original file still in place after replacing file in new revision.'); $this->assertFileExists($node_file_r1->getFileUri(), 'Original file still in place after replacing file in new revision.');
$this->assertFileEntryExists($node_file_r1, 'Original file entry still in place after replacing file in new revision'); $this->assertFileEntryExists($node_file_r1, 'Original file entry still in place after replacing file in new revision');
$this->assertFileIsPermanent($node_file_r1, 'Original file is still permanent.'); $this->assertFileIsPermanent($node_file_r1, 'Original file is still permanent.');
...@@ -89,7 +89,7 @@ public function testRevisions() { ...@@ -89,7 +89,7 @@ public function testRevisions() {
// Delete the second revision and check that the file is kept (since it is // Delete the second revision and check that the file is kept (since it is
// still being used by the third revision). // still being used by the third revision).
$this->drupalPostForm('node/' . $nid . '/revisions/' . $node_vid_r2 . '/delete', [], t('Delete')); $this->drupalPostForm('node/' . $nid . '/revisions/' . $node_vid_r2 . '/delete', [], t('Delete'));
$this->assertFileExists($node_file_r3, 'Second file is still available after deleting second revision, since it is being used by the third revision.'); $this->assertFileExists($node_file_r3->getFileUri(), 'Second file is still available after deleting second revision, since it is being used by the third revision.');
$this->assertFileEntryExists($node_file_r3, 'Second file entry is still available after deleting second revision, since it is being used by the third revision.'); $this->assertFileEntryExists($node_file_r3, 'Second file entry is still available after deleting second revision, since it is being used by the third revision.');
$this->assertFileIsPermanent($node_file_r3, 'Second file entry is still permanent after deleting second revision, since it is being used by the third revision.'); $this->assertFileIsPermanent($node_file_r3, 'Second file entry is still permanent after deleting second revision, since it is being used by the third revision.');
...@@ -102,7 +102,7 @@ public function testRevisions() { ...@@ -102,7 +102,7 @@ public function testRevisions() {
// Delete the third revision and check that the file is not deleted yet. // Delete the third revision and check that the file is not deleted yet.
$this->drupalPostForm('node/' . $nid . '/revisions/' . $node_vid_r3 . '/delete', [], t('Delete')); $this->drupalPostForm('node/' . $nid . '/revisions/' . $node_vid_r3 . '/delete', [], t('Delete'));
$this->assertFileExists($node_file_r3, 'Second file is still available after deleting third revision, since it is being used by the user.'); $this->assertFileExists($node_file_r3->getFileUri(), 'Second file is still available after deleting third revision, since it is being used by the user.');
$this->assertFileEntryExists($node_file_r3, 'Second file entry is still available after deleting third revision, since it is being used by the user.'); $this->assertFileEntryExists($node_file_r3, 'Second file entry is still available after deleting third revision, since it is being used by the user.');
$this->assertFileIsPermanent($node_file_r3, 'Second file entry is still permanent after deleting third revision, since it is being used by the user.'); $this->assertFileIsPermanent($node_file_r3, 'Second file entry is still permanent after deleting third revision, since it is being used by the user.');
...@@ -129,7 +129,7 @@ public function testRevisions() { ...@@ -129,7 +129,7 @@ public function testRevisions() {
->execute(); ->execute();
\Drupal::service('cron')->run(); \Drupal::service('cron')->run();
$this->assertFileNotExists($node_file_r3, 'Second file is now deleted after deleting third revision, since it is no longer being used by any other nodes.'); $this->assertFileNotExists($node_file_r3->getFileUri(), 'Second file is now deleted after deleting third revision, since it is no longer being used by any other nodes.');
$this->assertFileEntryNotExists($node_file_r3, 'Second file entry is now deleted after deleting third revision, since it is no longer being used by any other nodes.'); $this->assertFileEntryNotExists($node_file_r3, 'Second file entry is now deleted after deleting third revision, since it is no longer being used by any other nodes.');
// Delete the entire node and check that the original file is deleted. // Delete the entire node and check that the original file is deleted.
...@@ -145,7 +145,7 @@ public function testRevisions() { ...@@ -145,7 +145,7 @@ public function testRevisions() {
->condition('fid', $node_file_r1->id()) ->condition('fid', $node_file_r1->id())
->execute(); ->execute();
\Drupal::service('cron')->run(); \Drupal::service('cron')->run();
$this->assertFileNotExists($node_file_r1, 'Original file is deleted after deleting the entire node with two revisions remaining.'); $this->assertFileNotExists($node_file_r1->getFileUri(), 'Original file is deleted after deleting the entire node with two revisions remaining.');
$this->assertFileEntryNotExists($node_file_r1, 'Original file entry is deleted after deleting the entire node with two revisions remaining.'); $this->assertFileEntryNotExists($node_file_r1, 'Original file entry is deleted after deleting the entire node with two revisions remaining.');
} }
......
...@@ -209,11 +209,16 @@ public function replaceNodeFile($file, $field_name, $nid, $new_revision = TRUE) ...@@ -209,11 +209,16 @@ public function replaceNodeFile($file, $field_name, $nid, $new_revision = TRUE)
* Either the file entity or the file URI. * Either the file entity or the file URI.
* @param string $message * @param string $message
* (optional) A message to display with the assertion. * (optional) A message to display with the assertion.
*
* @see https://www.drupal.org/node/3057326
*/ */
public static function assertFileExists($file, $message = NULL) { public static function assertFileExists($file, $message = NULL) {
$message = isset($message) ? $message : format_string('File %file exists on the disk.', ['%file' => $file->getFileUri()]); if ($file instanceof FileInterface) {
$filename = $file instanceof FileInterface ? $file->getFileUri() : $file; @trigger_error('Passing a File entity as $file argument to FileFieldTestBase::assertFileExists is deprecated in drupal:8.8.0. It will be removed from drupal:9.0.0. Instead, pass the File entity URI via File::getFileUri(). See https://www.drupal.org/node/3057326', E_USER_DEPRECATED);
parent::assertFileExists($filename, $message); $file = $file->getFileUri();
}
$message = isset($message) ? $message : format_string('File %file exists on the disk.', ['%file' => $file]);
parent::assertFileExists($file, $message);
} }
/** /**
...@@ -229,18 +234,23 @@ public function assertFileEntryExists($file, $message = NULL) { ...@@ -229,18 +234,23 @@ public function assertFileEntryExists($file, $message = NULL) {
/** /**
* Asserts that a file does not exist on disk. * Asserts that a file does not exist on disk.
* *
* Overrides PHPUnit\Framework\Assert::assertFileExists() to also work with * Overrides PHPUnit\Framework\Assert::assertFileNotExists() to also work
* file entities. * with file entities.
* *
* @param \Drupal\File\FileInterface|string $file * @param \Drupal\File\FileInterface|string $file
* Either the file entity or the file URI. * Either the file entity or the file URI.
* @param string $message * @param string $message
* (optional) A message to display with the assertion. * (optional) A message to display with the assertion.
*
* @see https://www.drupal.org/node/3057326
*/ */
public static function assertFileNotExists($file, $message = NULL) { public static function assertFileNotExists($file, $message = NULL) {
$message = isset($message) ? $message : format_string('File %file exists on the disk.', ['%file' => $file->getFileUri()]); if ($file instanceof FileInterface) {
$filename = $file instanceof FileInterface ? $file->getFileUri() : $file; @trigger_error('Passing a File entity as $file argument to FileFieldTestBase::assertFileNotExists is deprecated in drupal:8.8.0. It will be removed from drupal:9.0.0. Instead, pass the File entity URI via File::getFileUri(). See https://www.drupal.org/node/3057326', E_USER_DEPRECATED);
parent::assertFileNotExists($filename, $message); $file = $file->getFileUri();
}
$message = isset($message) ? $message : format_string('File %file exists on the disk.', ['%file' => $file]);
parent::assertFileNotExists($file, $message);
} }
/** /**
......
...@@ -40,7 +40,7 @@ public function testRequired() { ...@@ -40,7 +40,7 @@ public function testRequired() {
$node = $node_storage->load($nid); $node = $node_storage->load($nid);
$node_file = File::load($node->{$field_name}->target_id); $node_file = File::load($node->{$field_name}->target_id);
$this->assertFileExists($node_file, 'File exists after uploading to the required field.'); $this->assertFileExists($node_file->getFileUri(), 'File exists after uploading to the required field.');
$this->assertFileEntryExists($node_file, 'File entry exists after uploading to the required field.'); $this->assertFileEntryExists($node_file, 'File entry exists after uploading to the required field.');
// Try again with a multiple value field. // Try again with a multiple value field.
...@@ -58,7 +58,7 @@ public function testRequired() { ...@@ -58,7 +58,7 @@ public function testRequired() {
$node_storage->resetCache([$nid]); $node_storage->resetCache([$nid]);
$node = $node_storage->load($nid); $node = $node_storage->load($nid);
$node_file = File::load($node->{$field_name}->target_id); $node_file = File::load($node->{$field_name}->target_id);
$this->assertFileExists($node_file, 'File exists after uploading to the required multiple value field.'); $this->assertFileExists($node_file->getFileUri(), 'File exists after uploading to the required multiple value field.');
$this->assertFileEntryExists($node_file, 'File entry exists after uploading to the required multiple value field.'); $this->assertFileEntryExists($node_file, 'File entry exists after uploading to the required multiple value field.');
} }
...@@ -92,7 +92,7 @@ public function testFileMaxSize() { ...@@ -92,7 +92,7 @@ public function testFileMaxSize() {
$node_storage->resetCache([$nid]); $node_storage->resetCache([$nid]);
$node = $node_storage->load($nid); $node = $node_storage->load($nid);
$node_file = File::load($node->{$field_name}->target_id); $node_file = File::load($node->{$field_name}->target_id);
$this->assertFileExists($node_file, format_string('File exists after uploading a file (%filesize) under the max limit (%maxsize).', ['%filesize' => format_size($small_file->getSize()), '%maxsize' => $max_filesize])); $this->assertFileExists($node_file->getFileUri(), format_string('File exists after uploading a file (%filesize) under the max limit (%maxsize).', ['%filesize' => format_size($small_file->getSize()), '%maxsize' => $max_filesize]));
$this->assertFileEntryExists($node_file, format_string('File entry exists after uploading a file (%filesize) under the max limit (%maxsize).', ['%filesize' => format_size($small_file->getSize()), '%maxsize' => $max_filesize])); $this->assertFileEntryExists($node_file, format_string('File entry exists after uploading a file (%filesize) under the max limit (%maxsize).', ['%filesize' => format_size($small_file->getSize()), '%maxsize' => $max_filesize]));
// Check that uploading the large file fails (1M limit). // Check that uploading the large file fails (1M limit).
...@@ -109,7 +109,7 @@ public function testFileMaxSize() { ...@@ -109,7 +109,7 @@ public function testFileMaxSize() {
$node_storage->resetCache([$nid]); $node_storage->resetCache([$nid]);
$node = $node_storage->load($nid); $node = $node_storage->load($nid);
$node_file = File::load($node->{$field_name}->target_id); $node_file = File::load($node->{$field_name}->target_id);
$this->assertFileExists($node_file, format_string('File exists after uploading a file (%filesize) with no max limit.', ['%filesize' => format_size($large_file->getSize())])); $this->assertFileExists($node_file->getFileUri(), format_string('File exists after uploading a file (%filesize) with no max limit.', ['%filesize' => format_size($large_file->getSize())]));
$this->assertFileEntryExists($node_file, format_string('File entry exists after uploading a file (%filesize) with no max limit.', ['%filesize' => format_size($large_file->getSize())])); $this->assertFileEntryExists($node_file, format_string('File entry exists after uploading a file (%filesize) with no max limit.', ['%filesize' => format_size($large_file->getSize())]));
} }
...@@ -133,7 +133,7 @@ public function testFileExtension() { ...@@ -133,7 +133,7 @@ public function testFileExtension() {
$node_storage->resetCache([$nid]); $node_storage->resetCache([$nid]);
$node = $node_storage->load($nid); $node = $node_storage->load($nid);
$node_file = File::load($node->{$field_name}->target_id); $node_file = File::load($node->{$field_name}->target_id);
$this->assertFileExists($node_file, 'File exists after uploading a file with no extension checking.'); $this->assertFileExists($node_file->getFileUri(), 'File exists after uploading a file with no extension checking.');
$this->assertFileEntryExists($node_file, 'File entry exists after uploading a file with no extension checking.'); $this->assertFileEntryExists($node_file, 'File entry exists after uploading a file with no extension checking.');
// Enable extension checking for text files. // Enable extension checking for text files.
...@@ -152,7 +152,7 @@ public function testFileExtension() { ...@@ -152,7 +152,7 @@ public function testFileExtension() {
$node_storage->resetCache([$nid]); $node_storage->resetCache([$nid]);
$node = $node_storage->load($nid); $node = $node_storage->load($nid);
$node_file = File::load($node->{$field_name}->target_id); $node_file = File::load($node->{$field_name}->target_id);
$this->assertFileExists($node_file, 'File exists after uploading a file with extension checking.'); $this->assertFileExists($node_file->getFileUri(), 'File exists after uploading a file with extension checking.');
$this->assertFileEntryExists($node_file, 'File entry exists after uploading a file with extension checking.'); $this->assertFileEntryExists($node_file, 'File entry exists after uploading a file with extension checking.');
} }
...@@ -175,7 +175,7 @@ public function testFileRemoval() { ...@@ -175,7 +175,7 @@ public function testFileRemoval() {
$node_storage->resetCache([$nid]); $node_storage->resetCache([$nid]);
$node = $node_storage->load($nid); $node = $node_storage->load($nid);
$node_file = File::load($node->{$field_name}->target_id); $node_file = File::load($node->{$field_name}->target_id);
$this->assertFileExists($node_file, 'File exists after uploading a file with no extension checking.'); $this->assertFileExists($node_file->getFileUri(), 'File exists after uploading a file with no extension checking.');
$this->assertFileEntryExists($node_file, 'File entry exists after uploading a file with no extension checking.'); $this->assertFileEntryExists($node_file, 'File entry exists after uploading a file with no extension checking.');
// Enable extension checking for text files. // Enable extension checking for text files.
...@@ -187,4 +187,33 @@ public function testFileRemoval() { ...@@ -187,4 +187,33 @@ public function testFileRemoval() {
$this->assertText('Article ' . $node->getTitle() . ' has been updated.'); $this->assertText('Article ' . $node->getTitle() . ' has been updated.');
} }
/**
* Tests deprecation of the assertFileExists and assertFileNotExists methods.
*
* @group legacy
*
* @expectedDeprecation Passing a File entity as $file argument to FileFieldTestBase::assertFileExists is deprecated in drupal:8.8.0. It will be removed from drupal:9.0.0. Instead, pass the File entity URI via File::getFileUri(). See https://www.drupal.org/node/3057326
* @expectedDeprecation Passing a File entity as $file argument to FileFieldTestBase::assertFileNotExists is deprecated in drupal:8.8.0. It will be removed from drupal:9.0.0. Instead, pass the File entity URI via File::getFileUri(). See https://www.drupal.org/node/3057326
*/
public function testAssertFileExistsDeprecation() {
$node_storage = $this->container->get('entity.manager')->getStorage('node');
$type_name = 'article';
$field_name = 'file_test';
$this->createFileField($field_name, 'node', $type_name);
$test_file = $this->getTestFile('image');
// Disable extension checking.
$this->updateFileField($field_name, $type_name, ['file_extensions' => '']);
// Check that the file can be uploaded with no extension checking.
$nid = $this->uploadNodeFile($test_file, $field_name, $type_name);
$node_storage->resetCache([$nid]);
$node = $node_storage->load($nid);
$node_file = File::load($node->{$field_name}->target_id);
$this->assertFileExists($node_file, 'File exists after uploading a file with no extension checking.');
unlink($node_file->getFileUri());
$this->assertFileNotExists($node_file, 'File does not exists after having been deleted.');
}
} }
...@@ -83,7 +83,7 @@ public function testSingleValuedWidget() { ...@@ -83,7 +83,7 @@ public function testSingleValuedWidget() {
$nid = $this->uploadNodeFile($test_file, $field_name, $type_name); $nid = $this->uploadNodeFile($test_file, $field_name, $type_name);
$node = $node_storage->loadUnchanged($nid); $node = $node_storage->loadUnchanged($nid);
$node_file = File::load($node->{$field_name}->target_id); $node_file = File::load($node->{$field_name}->target_id);
$this->assertFileExists($node_file, 'New file saved to disk on node creation.'); $this->assertFileExists($node_file->getFileUri(), 'New file saved to disk on node creation.');
// Ensure the file can be downloaded. // Ensure the file can be downloaded.
$this->drupalGet($node_file->createFileUrl()); $this->drupalGet($node_file->createFileUrl());
...@@ -257,7 +257,7 @@ public function testPrivateFileSetting() { ...@@ -257,7 +257,7 @@ public function testPrivateFileSetting() {
$nid = $this->uploadNodeFile($test_file, $field_name, $type_name); $nid = $this->uploadNodeFile($test_file, $field_name, $type_name);
$node = $node_storage->loadUnchanged($nid); $node = $node_storage->loadUnchanged($nid);
$node_file = File::load($node->{$field_name}->target_id); $node_file = File::load($node->{$field_name}->target_id);
$this->assertFileExists($node_file, 'New file saved to disk on node creation.'); $this->assertFileExists($node_file->getFileUri(), 'New file saved to disk on node creation.');
// Ensure the private file is available to the user who uploaded it. // Ensure the private file is available to the user who uploaded it.
$this->drupalGet($node_file->createFileUrl()); $this->drupalGet($node_file->createFileUrl());
...@@ -324,7 +324,7 @@ public function testPrivateFileComment() { ...@@ -324,7 +324,7 @@ public function testPrivateFileComment() {
$comment = Comment::load($cid); $comment = Comment::load($cid);
$comment_file = $comment->{'field_' . $name}->entity; $comment_file = $comment->{'field_' . $name}->entity;
$this->assertFileExists($comment_file, 'New file saved to disk on node creation.'); $this->assertFileExists($comment_file->getFileUri(), 'New file saved to disk on node creation.');
// Test authenticated file download. // Test authenticated file download.
$url = $comment_file->createFileUrl(); $url = $comment_file->createFileUrl();
$this->assertNotEqual($url, NULL, 'Confirmed that the URL is valid'); $this->assertNotEqual($url, NULL, 'Confirmed that the URL is valid');
...@@ -501,7 +501,7 @@ protected function doTestTemporaryFileRemovalExploit(UserInterface $victim_user, ...@@ -501,7 +501,7 @@ protected function doTestTemporaryFileRemovalExploit(UserInterface $victim_user,
/** @var \Drupal\file\FileInterface $node_file */ /** @var \Drupal\file\FileInterface $node_file */
$node_file = File::load($node->{$field_name}->target_id); $node_file = File::load($node->{$field_name}->target_id);
$this->assertFileExists($node_file, 'A file was saved to disk on node creation'); $this->assertFileExists($node_file->getFileUri(), 'A file was saved to disk on node creation');
$this->assertEqual($attacker_user->id(), $node_file->getOwnerId(), 'New file belongs to the attacker.'); $this->assertEqual($attacker_user->id(), $node_file->getOwnerId(), 'New file belongs to the attacker.');
// Ensure the file can be downloaded. // Ensure the file can be downloaded.
...@@ -519,7 +519,7 @@ protected function doTestTemporaryFileRemovalExploit(UserInterface $victim_user, ...@@ -519,7 +519,7 @@ protected function doTestTemporaryFileRemovalExploit(UserInterface $victim_user,
// The victim's temporary file should not be removed by the attacker's // The victim's temporary file should not be removed by the attacker's
// POST request. // POST request.
$this->assertFileExists($victim_tmp_file); $this->assertFileExists($victim_tmp_file->getFileUri());
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment