diff --git a/core/modules/file/src/Controller/FileUploadController.php b/core/modules/file/src/Controller/FileUploadController.php index 0c9c6545b2..0bbc8e00e5 100644 --- a/core/modules/file/src/Controller/FileUploadController.php +++ b/core/modules/file/src/Controller/FileUploadController.php @@ -56,6 +56,9 @@ public function upload(Request $request, FileInterface $file) { $this->streamUploadData($file); + // Save the file so file size is re-calculated. + $file->save(); + return new Response('OK'); } diff --git a/core/modules/file/tests/src/Kernel/FileUploadTest.php b/core/modules/file/tests/src/Kernel/FileUploadTest.php new file mode 100644 index 0000000000..1e136c225e --- /dev/null +++ b/core/modules/file/tests/src/Kernel/FileUploadTest.php @@ -0,0 +1,115 @@ +installEntitySchema('file'); + $this->installEntitySchema('user'); + $this->installSchema('file', ['file_usage']); + $this->installSchema('system', 'sequences'); + + // Create a file so binary data uploading can be used on it. + file_put_contents('public://example.txt', 'TEST'); + + // Create a user to use as the file owner. + $this->user = User::create([ + 'name' => 'user1', + 'status' => 1, + ]); + $this->user->save(); + + $this->file = File::create([ + 'uid' => $this->user->id(), + 'uri' => 'public://example.txt', + ]); + + $this->file->save(); + } + + /** + * Tests using the file upload route. + */ + public function testFileUploadString() { + $new_file_data = 'TEST WITH NEW DATA'; + $uri = '/file/upload/' . $this->file->id(); + + $request = Request::create($uri, 'POST', [], [], [], [], $new_file_data); + + // Set the file owner as the current user. + $this->container->get('current_user')->setAccount($this->user); + + $response = $this->container->get('http_kernel')->handle($request); + + $this->assertSame(415, $response->getStatusCode()); + + // Now set the correct Content-Type header. + $request->headers->set('Content-Type', 'application/octet-stream'); + + $response = $this->container->get('http_kernel')->handle($request); + + $this->assertSame(200, $response->getStatusCode()); + $this->assertSame('OK', $response->getContent()); + $this->assertSame($new_file_data, file_get_contents('public://example.txt')); + } + + /** + * Tests using the file upload route. + */ + public function testFileUploadBinary() { + $uri = '/file/upload/' . $this->file->id(); + + $new_binary_data = ''; + + $request = Request::create($uri, 'POST', [], [], [], [], $new_binary_data); + + // Set the file owner as the current user. + $this->container->get('current_user')->setAccount($this->user); + + $request->headers->set('Content-Type', 'application/octet-stream'); + + $response = $this->container->get('http_kernel')->handle($request); + + $this->assertSame(200, $response->getStatusCode()); + $this->assertSame('OK', $response->getContent()); + $this->assertSame($new_binary_data, file_get_contents('public://example.txt')); + } + +}