diff --git a/core/modules/file/src/Plugin/rest/resource/FileUploadResource.php b/core/modules/file/src/Plugin/rest/resource/FileUploadResource.php index 7f840fb988..e4e44b6152 100644 --- a/core/modules/file/src/Plugin/rest/resource/FileUploadResource.php +++ b/core/modules/file/src/Plugin/rest/resource/FileUploadResource.php @@ -181,7 +181,7 @@ public function post(Request $request, $entity_type_id, $bundle, $field_name) { 'uri' => $file_uri, // Set the size. This is done in File::preSave() but we validate the file // before it is saved. - 'size' => @filesize($file_uri), + 'filesize' => @filesize($file_uri), ]; $values['filemime'] = $this->mimeTypeGuesser->guess($values['filename']); $file = File::create($values); diff --git a/core/modules/rest/tests/src/Functional/FileUploadResourceTestBase.php b/core/modules/rest/tests/src/Functional/FileUploadResourceTestBase.php index b6cb7c04c7..42a956ffcd 100644 --- a/core/modules/rest/tests/src/Functional/FileUploadResourceTestBase.php +++ b/core/modules/rest/tests/src/Functional/FileUploadResourceTestBase.php @@ -152,16 +152,62 @@ public function testPostFileUpload() { $this->assertSame(201, $response->getStatusCode()); $expected = $this->getExpectedNormalizedEntity(); - static::recursiveKSort($expected); - $actual = $this->serializer->decode((string) $response->getBody(), static::$format); - static::recursiveKSort($actual); - $this->assertSame($expected, $actual); + $this->assertResponseData($expected, $response); // Check the actual file data. $this->assertSame($this->testFileData, file_get_contents('public://foobar/example.txt')); } + /** + * Tests using the file upload route with a zero byte file. + */ + public function testFileUploadZeroByteFile() { + $this->initAuthentication(); + + $this->provisionResource([static::$format], static::$auth ? [static::$auth] : [], ['POST']); + + $this->setUpAuthorization('POST'); + + $uri = Url::fromUri('base:' . static::$postUri); + + // Test with a zero byte file. + $response = $this->fileRequest($uri, NULL); + + $this->assertSame(201, $response->getStatusCode()); + + $expected = $this->getExpectedNormalizedEntity(); + // Modify the default expected data to account for the 0 byte file. + $expected['filesize'][0]['value'] = 0; + + $this->assertResponseData($expected, $response); + + // Check the actual file data. + $this->assertSame('', file_get_contents('public://foobar/example.txt')); + } + + /** + * Tests using the file upload route with an invalid file type. + */ + public function testFileUploadInvalidFileType() { + $this->initAuthentication(); + + $this->provisionResource([static::$format], static::$auth ? [static::$auth] : [], ['POST']); + + $this->setUpAuthorization('POST'); + + $uri = Url::fromUri('base:' . static::$postUri); + + // Test with a JSON file. + $response = $this->fileRequest($uri, '{"test":123}', ['Content-Disposition' => 'filename="example.json"']); + + $this->assertSame(422, $response->getStatusCode()); + + // Make sure that no file was saved. + $this->assertEmpty(File::load(1)); + $this->assertFalse(file_exists('public://foobar/example.txt')); + } + /** * {@inheritdoc} */ @@ -297,4 +343,20 @@ protected function setUpAuthorization($method) { } } + /** + * Asserts expected normalized data matches response data. + * + * @param array $expected + * The expected data. + * @param \Psr\Http\Message\ResponseInterface $response + * The file upload response. + */ + protected function assertResponseData(array $expected, ResponseInterface $response) { + static::recursiveKSort($expected); + $actual = $this->serializer->decode((string) $response->getBody(), static::$format); + static::recursiveKSort($actual); + + $this->assertSame($expected, $actual); + } + }