From fec7c654aa4896d0ffacd6a001e48dd7a56bb105 Mon Sep 17 00:00:00 2001 From: Lauri Eskola <lauri.eskola@acquia.com> Date: Thu, 28 Sep 2023 20:48:35 +0300 Subject: [PATCH] Issue #3389957 by catch: Consolidate methods in jsonapi FileUploadTest --- .../tests/src/Functional/FileUploadTest.php | 61 ++++++------------- 1 file changed, 17 insertions(+), 44 deletions(-) diff --git a/core/modules/jsonapi/tests/src/Functional/FileUploadTest.php b/core/modules/jsonapi/tests/src/Functional/FileUploadTest.php index 0d23e93d4b02..7ccd20b2ac40 100644 --- a/core/modules/jsonapi/tests/src/Functional/FileUploadTest.php +++ b/core/modules/jsonapi/tests/src/Functional/FileUploadTest.php @@ -383,10 +383,7 @@ protected function getPostDocument() { /** * Tests using the file upload POST route with invalid headers. */ - public function testPostFileUploadInvalidHeaders() { - $this->setUpAuthorization('POST'); - $this->config('jsonapi.settings')->set('read_only', FALSE)->save(TRUE); - + protected function testPostFileUploadInvalidHeaders() { $uri = Url::fromUri('base:' . static::$postUri); // The wrong content type header should return a 415 code. @@ -445,24 +442,6 @@ public function testPostFileUploadDuplicateFile() { // Check the actual file data. $this->assertSame($this->testFileData, file_get_contents('public://foobar/example_0.txt')); - } - - /** - * Tests using the file upload POST route twice, simulating a race condition. - * - * A validation error should occur when the filenames are not unique. - */ - public function testPostFileUploadDuplicateFileRaceCondition() { - $this->setUpAuthorization('POST'); - $this->config('jsonapi.settings')->set('read_only', FALSE)->save(TRUE); - - $uri = Url::fromUri('base:' . static::$postUri); - - // This request will have the default 'application/octet-stream' content - // type header. - $response = $this->fileRequest($uri, $this->testFileData); - - $this->assertSame(201, $response->getStatusCode()); // Simulate a race condition where two files are uploaded at almost the same // time, by removing the first uploaded file from disk (leaving the entry in @@ -523,6 +502,17 @@ public function testFileUploadStrippedFilePath() { $this->assertSame($this->testFileData, file_get_contents('public://foobar/passwd')); } + /** + * Tests invalid file uploads. + */ + public function testInvalidFileUploads() { + $this->setUpAuthorization('POST'); + $this->config('jsonapi.settings')->set('read_only', FALSE)->save(TRUE); + $this->testPostFileUploadInvalidHeaders(); + $this->testFileUploadLargerFileSize(); + $this->testFileUploadMaliciousExtension(); + } + /** * Tests using the file upload route with a unicode file name. */ @@ -583,15 +573,12 @@ public function testFileUploadInvalidFileType() { /** * Tests using the file upload route with a file size larger than allowed. */ - public function testFileUploadLargerFileSize() { + protected function testFileUploadLargerFileSize() { // Set a limit of 50 bytes. $this->field->setSetting('max_filesize', 50) ->save(); $this->rebuildAll(); - $this->setUpAuthorization('POST'); - $this->config('jsonapi.settings')->set('read_only', FALSE)->save(TRUE); - $uri = Url::fromUri('base:' . static::$postUri); // Generate a string larger than the 50 byte limit set. @@ -606,14 +593,11 @@ public function testFileUploadLargerFileSize() { /** * Tests using the file upload POST route with malicious extensions. */ - public function testFileUploadMaliciousExtension() { + protected function testFileUploadMaliciousExtension() { // Allow all file uploads but system.file::allow_insecure_uploads is set to // FALSE. $this->field->setSetting('file_extensions', '')->save(); - $this->setUpAuthorization('POST'); - $this->config('jsonapi.settings')->set('read_only', FALSE)->save(TRUE); - $uri = Url::fromUri('base:' . static::$postUri); $php_string = '<?php print "Drupal"; ?>'; @@ -723,9 +707,9 @@ public function testFileUploadMaliciousExtension() { } /** - * Tests using the file upload POST route no extension configured. + * Tests using the file upload POST route no configuration. */ - public function testFileUploadNoExtensionSetting() { + public function testFileUploadNoConfiguration() { $this->setUpAuthorization('POST'); $this->config('jsonapi.settings')->set('read_only', FALSE)->save(TRUE); @@ -740,22 +724,11 @@ public function testFileUploadNoExtensionSetting() { $this->assertResponseData($expected, $response); $this->assertFileExists('public://foobar/example.txt'); - } - - /** - * Tests using the file upload POST route no directory configured. - */ - public function testFileUploadNoDirectorySetting() { - $this->setUpAuthorization('POST'); - $this->config('jsonapi.settings')->set('read_only', FALSE)->save(TRUE); - - $uri = Url::fromUri('base:' . static::$postUri); - $this->field->setSetting('file_directory', '') ->save(); $response = $this->fileRequest($uri, $this->testFileData, ['Content-Disposition' => 'filename="example.txt"']); - $expected = $this->getExpectedDocument(1, 'example.txt', TRUE); + $expected = $this->getExpectedDocument(2, 'example.txt', TRUE); $expected['data']['attributes']['uri']['value'] = 'public://example.txt'; $expected['data']['attributes']['uri']['url'] = base_path() . $this->siteDirectory . '/files/example.txt'; -- GitLab