diff --git a/core/modules/file/tests/src/Functional/DownloadTest.php b/core/modules/file/tests/src/Functional/DownloadTest.php index 10f99d7dbe..f563daeb62 100644 --- a/core/modules/file/tests/src/Functional/DownloadTest.php +++ b/core/modules/file/tests/src/Functional/DownloadTest.php @@ -3,6 +3,7 @@ namespace Drupal\Tests\file\Functional; use Drupal\Core\Site\Settings; +use GuzzleHttp\Exception\RequestException; /** * Tests for download/file transfer functions. @@ -77,13 +78,23 @@ protected function doPrivateFileTransferTest() { // Deny access to all downloads via a -1 header. file_test_set_return('download', -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 { + $http_client->head($url); + $this->fail('Not correctly denied access to a file when file_test sets the header to -1.'); + } + catch (RequestException $e) { + $this->assertSame(403, $e->getCode()); + } // Try non-existent file. $url = file_create_url('private://' . $this->randomMachineName()); - $response = $http_client->head($url); - $this->assertEquals(404, $response->getStatusCode(), 'Correctly returned 404 response for a non-existent file.'); + try { + $http_client->head($url); + $this->fail('Not correctly returned 404 response for a non-existent file.'); + } + catch (RequestException $e) { + $this->assertSame(404, $e->getCode()); + } } /** diff --git a/core/modules/file/tests/src/Functional/FileFieldDisplayTest.php b/core/modules/file/tests/src/Functional/FileFieldDisplayTest.php index 04af759a6e..f12b7edf58 100644 --- a/core/modules/file/tests/src/Functional/FileFieldDisplayTest.php +++ b/core/modules/file/tests/src/Functional/FileFieldDisplayTest.php @@ -4,6 +4,7 @@ use Drupal\Core\Field\FieldStorageDefinitionInterface; use Drupal\file\Entity\File; +use Drupal\Tests\Traits\DrupalPostFormHack; /** * Tests the display of file fields in node and views. @@ -12,6 +13,8 @@ */ class FileFieldDisplayTest extends FileFieldTestBase { + use DrupalPostFormHack; + /** * Tests normal formatter display on node display. */ @@ -101,8 +104,8 @@ public function testNodeDisplay() { // Uncheck the display checkboxes and go to the preview. $edit[$field_name . '[0][display]'] = FALSE; $edit[$field_name . '[1][display]'] = FALSE; - $this->drupalPostForm("node/$nid/edit", $edit, t('Preview')); - $this->clickLink(t('Back to content editing')); + $this->drupalPostFormHack("node/$nid/edit", $edit, t('Preview')); + $this->drupalGet("node/$nid/edit"); $this->assertRaw($field_name . '[0][display]', 'First file appears as expected.'); $this->assertRaw($field_name . '[1][display]', 'Second file appears as expected.'); } diff --git a/core/modules/file/tests/src/Functional/FilePrivateTest.php b/core/modules/file/tests/src/Functional/FilePrivateTest.php index 1e335eaf43..82cae97540 100644 --- a/core/modules/file/tests/src/Functional/FilePrivateTest.php +++ b/core/modules/file/tests/src/Functional/FilePrivateTest.php @@ -6,6 +6,7 @@ use Drupal\Component\Utility\SafeMarkup; use Drupal\file\Entity\File; use Drupal\node\Entity\NodeType; +use Drupal\Tests\Traits\DrupalPostFormHack; use Drupal\user\RoleInterface; /** @@ -15,6 +16,8 @@ */ class FilePrivateTest extends FileFieldTestBase { + use DrupalPostFormHack; + /** * Modules to enable. * @@ -80,22 +83,19 @@ public function testPrivateFile() { $this->drupalPostForm('node/add/' . $type_name, $edit, t('Save')); $new_node = $this->drupalGetNodeByTitle($edit['title[0][value]']); $edit[$field_name . '[0][fids]'] = $node_file->id(); - $this->drupalPostForm('node/' . $new_node->id() . '/edit', $edit, t('Save')); - // Make sure the form submit failed - we stayed on the edit form. - $this->assertUrl('node/' . $new_node->id() . '/edit'); + $this->drupalPostFormHack('node/' . $new_node->id() . '/edit', $edit, t('Save')); // Check that we got the expected constraint form error. $constraint = new ReferenceAccessConstraint(); - $this->assertRaw(SafeMarkup::format($constraint->message, ['%type' => 'file', '%id' => $node_file->id()])); + $this->assertContains(SafeMarkup::format($constraint->message, ['%type' => 'file', '%id' => $node_file->id()])->jsonSerialize(), (string) $this->postResponse->getBody()); // Attempt to reuse the existing file when creating a new node, and confirm // that access is still denied. $edit = []; $edit['title[0][value]'] = $this->randomMachineName(); $edit[$field_name . '[0][fids]'] = $node_file->id(); - $this->drupalPostForm('node/add/' . $type_name, $edit, t('Save')); + $this->drupalPostFormHack('node/add/' . $type_name, $edit, t('Save')); $new_node = $this->drupalGetNodeByTitle($edit['title[0][value]']); $this->assertTrue(empty($new_node), 'Node was not created.'); - $this->assertUrl('node/add/' . $type_name); - $this->assertRaw(SafeMarkup::format($constraint->message, ['%type' => 'file', '%id' => $node_file->id()])); + $this->assertContains(SafeMarkup::format($constraint->message, ['%type' => 'file', '%id' => $node_file->id()])->jsonSerialize(), (string) $this->postResponse->getBody()); // Now make file_test_file_download() return everything. \Drupal::state()->set('file_test.allow_all', TRUE); diff --git a/core/modules/file/tests/src/Functional/SaveUploadFormTest.php b/core/modules/file/tests/src/Functional/SaveUploadFormTest.php index 6ac441770e..ab178707f5 100644 --- a/core/modules/file/tests/src/Functional/SaveUploadFormTest.php +++ b/core/modules/file/tests/src/Functional/SaveUploadFormTest.php @@ -361,7 +361,7 @@ public function testDrupalMovingUploadedFileError() { \Drupal::state()->set('file_test.disable_error_collection', TRUE); $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.'); + $this->assertContains(t('Epic upload FAIL!')->render(), (string) $this->postResponse->getBody(), 'Found the failure message.'); // Uploading failed. Now check the log. $this->drupalGet('admin/reports/dblog');