diff --git a/core/modules/file/src/Tests/FileFieldTestBase.php b/core/modules/file/src/Tests/FileFieldTestBase.php index b7b90d7d37..91bb58ca7d 100644 --- a/core/modules/file/src/Tests/FileFieldTestBase.php +++ b/core/modules/file/src/Tests/FileFieldTestBase.php @@ -2,6 +2,8 @@ namespace Drupal\file\Tests; +@trigger_error('The ' . __NAMESPACE__ . '\FileFieldTestBase is scheduled for removal in Drupal 9.0.0. Instead, use \Drupal\Tests\file\Functional\FileFieldTestBase', E_USER_DEPRECATED); + use Drupal\field\Entity\FieldStorageConfig; use Drupal\field\Entity\FieldConfig; use Drupal\file\FileInterface; diff --git a/core/modules/file/src/Tests/FileManagedTestBase.php b/core/modules/file/src/Tests/FileManagedTestBase.php index 879e1c4e94..fde651dca6 100644 --- a/core/modules/file/src/Tests/FileManagedTestBase.php +++ b/core/modules/file/src/Tests/FileManagedTestBase.php @@ -2,6 +2,8 @@ namespace Drupal\file\Tests; +@trigger_error('The ' . __NAMESPACE__ . '\FileManagedTestBase is scheduled for removal in Drupal 9.0.0. Instead, use \Drupal\Tests\file\Functional\FileManagedTestBase', E_USER_DEPRECATED); + use Drupal\file\Entity\File; use Drupal\file\FileInterface; use Drupal\simpletest\WebTestBase; diff --git a/core/modules/file/src/Tests/DownloadTest.php b/core/modules/file/tests/src/Functional/DownloadTest.php similarity index 84% rename from core/modules/file/src/Tests/DownloadTest.php rename to core/modules/file/tests/src/Functional/DownloadTest.php index 239155cf40..10f99d7dbe 100644 --- a/core/modules/file/src/Tests/DownloadTest.php +++ b/core/modules/file/tests/src/Functional/DownloadTest.php @@ -1,6 +1,8 @@ getViaScheme('public')->getDirectoryPath() . '/' . rawurlencode($file->getFilename()); $this->assertEqual($filename, $url, 'Correctly generated a URL for a created file.'); - $this->drupalHead($url); - $this->assertResponse(200, 'Confirmed that the generated URL is correct by downloading the created file.'); - + $http_client = \Drupal::httpClient(); + $response = $http_client->head($url); + $this->assertEquals(200, $response->getStatusCode(), 'Confirmed that the generated URL is correct by downloading the created file.'); // Test generating a URL to a shipped file (i.e. a file that is part of // Drupal core, a module or a theme, for example a JavaScript file). $filepath = 'core/assets/vendor/jquery/jquery.min.js'; $url = file_create_url($filepath); $this->assertEqual($GLOBALS['base_url'] . '/' . $filepath, $url, 'Correctly generated a URL for a shipped file.'); - $this->drupalHead($url); - $this->assertResponse(200, 'Confirmed that the generated URL is correct by downloading the shipped file.'); + $response = $http_client->head($url); + $this->assertEquals(200, $response->getStatusCode(), 'Confirmed that the generated URL is correct by downloading the shipped file.'); } /** @@ -70,17 +72,18 @@ protected function doPrivateFileTransferTest() { $this->assertResponse(200, 'Correctly allowed access to a file when file_test provides headers.'); // Test that the file transferred correctly. - $this->assertEqual($contents, $this->content, 'Contents of the file are correct.'); + $this->assertSession()->responseContains($contents); // Deny access to all downloads via a -1 header. file_test_set_return('download', -1); - $this->drupalHead($url); - $this->assertResponse(403, 'Correctly denied access to a file when file_test sets the header to -1.'); + $http_client = \Drupal::httpClient(); + $response = $http_client->head($url); + $this->assertEquals(403, $response->getStatusCode(), 'Correctly denied access to a file when file_test sets the header to -1.'); // Try non-existent file. $url = file_create_url('private://' . $this->randomMachineName()); - $this->drupalHead($url); - $this->assertResponse(404, 'Correctly returned 404 response for a non-existent file.'); + $response = $http_client->head($url); + $this->assertEquals(404, $response->getStatusCode(), 'Correctly returned 404 response for a non-existent file.'); } /** @@ -172,4 +175,20 @@ private function checkUrl($scheme, $directory, $filename, $expected_url) { $file->delete(); } + /** + * Changes in memory settings. + * + * @param $name + * The name of the setting to return. + * @param $value + * The value of the setting. + * + * @see \Drupal\Core\Site\Settings::get() + */ + protected function settingsSet($name, $value) { + $settings = Settings::getAll(); + $settings[$name] = $value; + new Settings($settings); + } + } diff --git a/core/modules/file/src/Tests/FileFieldAnonymousSubmissionTest.php b/core/modules/file/tests/src/Functional/FileFieldAnonymousSubmissionTest.php similarity index 95% rename from core/modules/file/src/Tests/FileFieldAnonymousSubmissionTest.php rename to core/modules/file/tests/src/Functional/FileFieldAnonymousSubmissionTest.php index 66d87cd7e8..0bed72fbdc 100644 --- a/core/modules/file/src/Tests/FileFieldAnonymousSubmissionTest.php +++ b/core/modules/file/tests/src/Functional/FileFieldAnonymousSubmissionTest.php @@ -1,6 +1,6 @@ assertNotEqual($nid, 0, 'The node ID was extracted from the URL.'); $node = Node::load($nid); $this->assertNotEqual($node, NULL, 'The node was loaded successfully.'); - $this->assertFileExists(File::load($node->field_image->target_id), 'The image was uploaded successfully.'); + $this->assertFileExistsOnDisk(File::load($node->field_image->target_id), 'The image was uploaded successfully.'); } } @@ -162,7 +162,7 @@ protected function doTestNodeWithFileWithoutTitle() { $this->assertNotEqual($nid, 0, 'The node ID was extracted from the URL.'); $node = Node::load($nid); $this->assertNotEqual($node, NULL, 'The node was loaded successfully.'); - $this->assertFileExists(File::load($node->field_image->target_id), 'The image was uploaded successfully.'); + $this->assertFileExistsOnDisk(File::load($node->field_image->target_id), 'The image was uploaded successfully.'); } } diff --git a/core/modules/file/src/Tests/FileFieldDisplayTest.php b/core/modules/file/tests/src/Functional/FileFieldDisplayTest.php similarity index 99% rename from core/modules/file/src/Tests/FileFieldDisplayTest.php rename to core/modules/file/tests/src/Functional/FileFieldDisplayTest.php index db9b7dc91b..04af759a6e 100644 --- a/core/modules/file/src/Tests/FileFieldDisplayTest.php +++ b/core/modules/file/tests/src/Functional/FileFieldDisplayTest.php @@ -1,6 +1,6 @@ drupalGet('rss.xml'); $uploaded_filename = str_replace('public://', '', $node_file->getFileUri()); $selector = sprintf( - 'enclosure[url="%s"][length="%s"][type="%s"]', + 'enclosure[@url="%s"][@length="%s"][@type="%s"]', file_create_url("public://$uploaded_filename", ['absolute' => TRUE]), $node_file->getSize(), $node_file->getMimeType() ); - $this->assertTrue(!empty($this->cssSelect($selector)), 'File field RSS enclosure is displayed when viewing the RSS feed.'); + $this->assertNotNull($this->getSession()->getDriver()->find('xpath', $selector), 'File field RSS enclosure is displayed when viewing the RSS feed.'); } } diff --git a/core/modules/file/src/Tests/FileFieldRevisionTest.php b/core/modules/file/tests/src/Functional/FileFieldRevisionTest.php similarity index 88% rename from core/modules/file/src/Tests/FileFieldRevisionTest.php rename to core/modules/file/tests/src/Functional/FileFieldRevisionTest.php index 04fbcb8767..7b4bd66965 100644 --- a/core/modules/file/src/Tests/FileFieldRevisionTest.php +++ b/core/modules/file/tests/src/Functional/FileFieldRevisionTest.php @@ -1,6 +1,6 @@ load($nid); $node_file_r1 = File::load($node->{$field_name}->target_id); $node_vid_r1 = $node->getRevisionId(); - $this->assertFileExists($node_file_r1, 'New file saved to disk on node creation.'); + $this->assertFileExistsOnDisk($node_file_r1, 'New file saved to disk on node creation.'); $this->assertFileEntryExists($node_file_r1, 'File entry exists in database on node creation.'); $this->assertFileIsPermanent($node_file_r1, 'File is permanent.'); @@ -54,7 +54,7 @@ public function testRevisions() { $node = $node_storage->load($nid); $node_file_r2 = File::load($node->{$field_name}->target_id); $node_vid_r2 = $node->getRevisionId(); - $this->assertFileExists($node_file_r2, 'Replacement file exists on disk after creating new revision.'); + $this->assertFileExistsOnDisk($node_file_r2, '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->assertFileIsPermanent($node_file_r2, 'Replacement file is permanent.'); @@ -62,7 +62,7 @@ public function testRevisions() { $node = node_revision_load($node_vid_r1); $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->assertFileExists($node_file_r1, 'Original file still in place after replacing file in new revision.'); + $this->assertFileExistsOnDisk($node_file_r1, '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->assertFileIsPermanent($node_file_r1, 'Original file is still permanent.'); @@ -87,7 +87,7 @@ public function testRevisions() { // Delete the second revision and check that the file is kept (since it is // still being used by the third revision). $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->assertFileExistsOnDisk($node_file_r3, '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->assertFileIsPermanent($node_file_r3, 'Second file entry is still permanent after deleting second revision, since it is being used by the third revision.'); @@ -100,7 +100,7 @@ public function testRevisions() { // 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->assertFileExists($node_file_r3, 'Second file is still available after deleting third revision, since it is being used by the user.'); + $this->assertFileExistsOnDisk($node_file_r3, '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->assertFileIsPermanent($node_file_r3, 'Second file entry is still permanent after deleting third revision, since it is being used by the user.'); @@ -125,7 +125,7 @@ public function testRevisions() { ->execute(); \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->assertFileNotExistsOnDisk($node_file_r3, '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.'); // Delete the entire node and check that the original file is deleted. @@ -140,7 +140,7 @@ public function testRevisions() { ->condition('fid', $node_file_r1->id()) ->execute(); \Drupal::service('cron')->run(); - $this->assertFileNotExists($node_file_r1, 'Original file is deleted after deleting the entire node with two revisions remaining.'); + $this->assertFileNotExistsOnDisk($node_file_r1, '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.'); } diff --git a/core/modules/file/tests/src/Functional/FileFieldTestBase.php b/core/modules/file/tests/src/Functional/FileFieldTestBase.php index d6096874d6..e8f6569f74 100644 --- a/core/modules/file/tests/src/Functional/FileFieldTestBase.php +++ b/core/modules/file/tests/src/Functional/FileFieldTestBase.php @@ -7,12 +7,17 @@ use Drupal\file\FileInterface; use Drupal\Tests\BrowserTestBase; use Drupal\file\Entity\File; +use Drupal\Tests\TestFileCreationTrait; /** * Provides methods specifically for testing File module's field handling. */ abstract class FileFieldTestBase extends BrowserTestBase { + use TestFileCreationTrait { + getTestFiles as drupalGetTestFiles; + } + /** * Modules to enable. * @@ -224,7 +229,7 @@ public function uploadNodeFiles(array $files, $field_name, $nid_or_type, $new_re $edit[$name][] = $file_path; } } - $this->drupalPostForm("node/$nid/edit", $edit, t('Save and keep published')); + $this->drupalPostForm("node/$nid/edit", $edit, t('Save')); return $nid; } @@ -240,7 +245,7 @@ public function removeNodeFile($nid, $new_revision = TRUE) { ]; $this->drupalPostForm('node/' . $nid . '/edit', [], t('Remove')); - $this->drupalPostForm(NULL, $edit, t('Save and keep published')); + $this->drupalPostForm(NULL, $edit, t('Save')); } /** @@ -253,24 +258,15 @@ public function replaceNodeFile($file, $field_name, $nid, $new_revision = TRUE) ]; $this->drupalPostForm('node/' . $nid . '/edit', [], t('Remove')); - $this->drupalPostForm(NULL, $edit, t('Save and keep published')); + $this->drupalPostForm(NULL, $edit, t('Save')); } /** * Asserts that a file exists physically on disk. - * - * Overrides PHPUnit\Framework\Assert::assertFileExists() to also work with - * file entities. - * - * @param \Drupal\File\FileInterface|string $file - * Either the file entity or the file URI. - * @param string $message - * (optional) A message to display with the assertion. */ - public static function assertFileExists($file, $message = NULL) { + public function assertFileExistsOnDisk($file, $message = NULL) { $message = isset($message) ? $message : format_string('File %file exists on the disk.', ['%file' => $file->getFileUri()]); - $filename = $file instanceof FileInterface ? $file->getFileUri() : $file; - parent::assertFileExists($filename, $message); + $this->assertTrue(is_file($file->getFileUri()), $message); } /** @@ -285,19 +281,10 @@ public function assertFileEntryExists($file, $message = NULL) { /** * Asserts that a file does not exist on disk. - * - * Overrides PHPUnit\Framework\Assert::assertFileExists() to also work with - * file entities. - * - * @param \Drupal\File\FileInterface|string $file - * Either the file entity or the file URI. - * @param string $message - * (optional) A message to display with the assertion. */ - public static function assertFileNotExists($file, $message = NULL) { + public function assertFileNotExistsOnDisk($file, $message = NULL) { $message = isset($message) ? $message : format_string('File %file exists on the disk.', ['%file' => $file->getFileUri()]); - $filename = $file instanceof FileInterface ? $file->getFileUri() : $file; - parent::assertFileNotExists($filename, $message); + $this->assertFalse(is_file($file->getFileUri()), $message); } /** diff --git a/core/modules/file/src/Tests/FileFieldValidateTest.php b/core/modules/file/tests/src/Functional/FileFieldValidateTest.php similarity index 89% rename from core/modules/file/src/Tests/FileFieldValidateTest.php rename to core/modules/file/tests/src/Functional/FileFieldValidateTest.php index 4698185c4f..8847e87dd3 100644 --- a/core/modules/file/src/Tests/FileFieldValidateTest.php +++ b/core/modules/file/tests/src/Functional/FileFieldValidateTest.php @@ -1,6 +1,6 @@ load($nid); $node_file = File::load($node->{$field_name}->target_id); - $this->assertFileExists($node_file, 'File exists after uploading to the required field.'); + $this->assertFileExistsOnDisk($node_file, 'File 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. @@ -58,7 +58,7 @@ public function testRequired() { $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 to the required multiple value field.'); + $this->assertFileExistsOnDisk($node_file, '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.'); } @@ -92,7 +92,7 @@ public function testFileMaxSize() { $node_storage->resetCache([$nid]); $node = $node_storage->load($nid); $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->assertFileExistsOnDisk($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->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). @@ -109,7 +109,7 @@ public function testFileMaxSize() { $node_storage->resetCache([$nid]); $node = $node_storage->load($nid); $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->assertFileExistsOnDisk($node_file, 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())])); } @@ -133,7 +133,7 @@ public function testFileExtension() { $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.'); + $this->assertFileExistsOnDisk($node_file, '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.'); // Enable extension checking for text files. @@ -152,7 +152,7 @@ public function testFileExtension() { $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 extension checking.'); + $this->assertFileExistsOnDisk($node_file, 'File 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() { $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.'); + $this->assertFileExistsOnDisk($node_file, '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.'); // Enable extension checking for text files. diff --git a/core/modules/file/src/Tests/FileListingTest.php b/core/modules/file/tests/src/Functional/FileListingTest.php similarity index 98% rename from core/modules/file/src/Tests/FileListingTest.php rename to core/modules/file/tests/src/Functional/FileListingTest.php index cca21c5a4c..0468f96666 100644 --- a/core/modules/file/src/Tests/FileListingTest.php +++ b/core/modules/file/tests/src/Functional/FileListingTest.php @@ -1,6 +1,6 @@ sumUsages($file_usage->listUsage($file)); $this->assertRaw('admin/content/files/usage/' . $file->id() . '">' . $usage); - $result = $this->xpath("//td[contains(@class, 'views-field-status') and contains(text(), :value)]", [':value' => t('Temporary')]); + $result = $this->xpath("//td[contains(@class, 'views-field-status') and contains(text(), :value)]", [':value' => 'Temporary']); $this->assertEqual(1, count($result), 'Unused file marked as temporary.'); // Test file usage page. diff --git a/core/modules/file/src/Tests/FileOnTranslatedEntityTest.php b/core/modules/file/tests/src/Functional/FileOnTranslatedEntityTest.php similarity index 99% rename from core/modules/file/src/Tests/FileOnTranslatedEntityTest.php rename to core/modules/file/tests/src/Functional/FileOnTranslatedEntityTest.php index b8a22ae01b..508ead4742 100644 --- a/core/modules/file/src/Tests/FileOnTranslatedEntityTest.php +++ b/core/modules/file/tests/src/Functional/FileOnTranslatedEntityTest.php @@ -1,6 +1,6 @@ drupalGet($file_url); $this->assertResponse(200, 'Confirmed that the anonymous uploader has access to the temporary file.'); // Close the prior connection and remove the session cookie. - $this->curlClose(); - $this->curlCookies = []; - $this->cookies = []; + $this->getSession()->reset(); $this->drupalGet($file_url); $this->assertResponse(403, 'Confirmed that another anonymous user cannot access the temporary file.'); @@ -172,9 +170,7 @@ public function testPrivateFile() { $this->drupalGet($file_url); $this->assertResponse(200, 'Confirmed that the anonymous uploader has access to the file whose references were removed.'); // Close the prior connection and remove the session cookie. - $this->curlClose(); - $this->curlCookies = []; - $this->cookies = []; + $this->getSession()->reset(); $this->drupalGet($file_url); $this->assertResponse(403, 'Confirmed that another anonymous user cannot access the file whose references were removed.'); @@ -195,9 +191,7 @@ public function testPrivateFile() { $this->drupalGet($file_url); $this->assertResponse(200, 'Confirmed that the anonymous uploader has access to the permanent file that is referenced by a published node.'); // Close the prior connection and remove the session cookie. - $this->curlClose(); - $this->curlCookies = []; - $this->cookies = []; + $this->getSession()->reset(); $this->drupalGet($file_url); $this->assertResponse(200, 'Confirmed that another anonymous user also has access to the permanent file that is referenced by a published node.'); @@ -222,9 +216,7 @@ public function testPrivateFile() { $this->drupalGet($file_url); $this->assertResponse(403, 'Confirmed that the anonymous uploader cannot access the permanent file when it is referenced by an unpublished node.'); // Close the prior connection and remove the session cookie. - $this->curlClose(); - $this->curlCookies = []; - $this->cookies = []; + $this->getSession()->reset(); $this->drupalGet($file_url); $this->assertResponse(403, 'Confirmed that another anonymous user cannot access the permanent file when it is referenced by an unpublished node.'); } diff --git a/core/modules/file/src/Tests/FileTokenReplaceTest.php b/core/modules/file/tests/src/Functional/FileTokenReplaceTest.php similarity index 99% rename from core/modules/file/src/Tests/FileTokenReplaceTest.php rename to core/modules/file/tests/src/Functional/FileTokenReplaceTest.php index 5bfa07b9ab..fd955913b9 100644 --- a/core/modules/file/src/Tests/FileTokenReplaceTest.php +++ b/core/modules/file/tests/src/Functional/FileTokenReplaceTest.php @@ -1,6 +1,6 @@ set('file_test.disable_error_collection', TRUE); - $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('File upload error. Could not move uploaded file.'), 'Found the failure message.'); - $this->assertRaw(t('Epic upload FAIL!'), 'Found the failure message.'); + $this->drupalPostFormHack('file-test/save_upload_from_form_test', $edit, t('Submit')); + $this->assertContains(t('File upload error. Could not move uploaded file.')->render(), (string) $this->postResponse->getBody(), 'Found the failure message.'); + $this->assertContains(t('Epic upload FAIL!')->render(), $this->postResponse->getBody(), 'Found the failure message.'); // Uploading failed. Now check the log. $this->drupalGet('admin/reports/dblog'); @@ -433,12 +440,11 @@ public function testCombinedErrorMessages() { 'extensions' => 'jpeg', ]; - $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('Epic upload FAIL!'), 'Found the failure message.'); + $this->drupalPostFormHack('file-test/save_upload_from_form_test', $edit, t('Submit')); + $this->assertContains(t('Epic upload FAIL!')->render(), (string) $this->postResponse->getBody(), 'Found the failure message.'); // Search for combined error message followed by a formatted list of messages. - $this->assertRaw(t('One or more files could not be uploaded.') . '
', 'Error message contains combined list of validation errors.'); + $this->assertContains(t('One or more files could not be uploaded.') . '
', (string) $this->postResponse->getBody()); } /** diff --git a/core/modules/file/src/Tests/SaveUploadTest.php b/core/modules/file/tests/src/Functional/SaveUploadTest.php similarity index 98% rename from core/modules/file/src/Tests/SaveUploadTest.php rename to core/modules/file/tests/src/Functional/SaveUploadTest.php index a9f69c2fb7..9382c8b4a6 100644 --- a/core/modules/file/src/Tests/SaveUploadTest.php +++ b/core/modules/file/tests/src/Functional/SaveUploadTest.php @@ -1,8 +1,9 @@ castSafeStrings($edit); + } + + if (isset($path)) { + $this->drupalGet($path, $options); + } + + $edit_save = $edit; + // Let's iterate over all the forms. + $xpath = "//form"; + if (!empty($form_html_id)) { + $xpath .= "[@id='" . $form_html_id . "']"; + } + $forms = $this->xpath($xpath); + foreach ($forms as $form) { + // We try to set the fields of this form as specified in $edit. + $edit = $edit_save; + $post = []; + $upload = []; + $submit_matches = $this->handleForm($post, $edit, $upload, $submit, $form); + $action = $form->hasAttribute('action') ? $this->getAbsoluteUrl($form->getAttribute('action')) : $this->getUrl(); + // We post only if we managed to handle every field in edit and the + // submit button matches. + if (!$edit && ($submit_matches || !isset($submit))) { + $post_multipart = []; + foreach ($post as $key => $value) { + $post_multipart[] = [ + 'name' => $key, + 'contents' => $value, + ]; + } + + if ($upload) { + foreach ($upload as $key => $file) { + if (is_array($file) && count($file)) { + // There seems to be no way via php's API to cURL to upload + // several files with the same post field name. However, Drupal + // still sees array-index syntax in a similar way. + for ($i = 0; $i < count($file); $i++) { + $postfield = str_replace('[]', '', $key) . '[' . $i . ']'; + $file_path = $this->container->get('file_system') + ->realpath($file[$i]); + $post_multipart[] = [ + 'name' => $postfield, + 'contents' => fopen($file_path, 'r'), + ]; + } + } + else { + $file = $this->container->get('file_system')->realpath($file); + if ($file && is_file($file)) { + $post_multipart[] = [ + 'name' => $key, + 'contents' => fopen($file, 'r'), + ]; + } + } + } + } + + $domain = parse_url($this->getUrl(), PHP_URL_HOST); + $session_id = $this->getSession()->getCookie($this->getSessionName()); + $cookies = CookieJar::fromArray([$this->getSessionName() => $session_id], $domain); + + /** @var \GuzzleHttp\Client $client */ + $client = $this->getSession()->getDriver()->getClient()->getClient(); + + $response = $client->post($action, [ + 'multipart' => $post_multipart, + 'cookies' => $cookies, + 'http_errors' => FALSE, + ]); + $this->postResponse = $response; + } + } + } + + /** + * Handles form input related to drupalPostFormHack(). + * + * Ensure that the specified fields exist and attempt to create POST data in + * the correct manner for the particular field type. + * + * @param $post + * Reference to array of post values. + * @param $edit + * Reference to array of edit values to be checked against the form. + * @param $submit + * Form submit button value. + * @param \Behat\Mink\Element\NodeElement $form + * Array of form elements. + * + * @return + * Submit value matches a valid submit input in the form. + */ + protected function handleForm(&$post, &$edit, &$upload, $submit, $form) { + // Retrieve the form elements. + $elements = $form->findAll('xpath', './/input[not(@disabled)]|.//textarea[not(@disabled)]|.//select[not(@disabled)]'); + $submit_matches = FALSE; + /** @var \Behat\Mink\Element\NodeElement $element */ + foreach ($elements as $element) { + // SimpleXML objects need string casting all the time. + $name = $element->getAttribute('name'); + // This can either be the type of or the name of the tag itself + // for