Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions src/Controller/Component/ModulesComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -344,17 +344,20 @@ public function upload(array &$requestData): void

/** @var \Laminas\Diactoros\UploadedFile $file */
$file = $requestData['file'];
$filepath = $file->getStream()->getMetadata('uri');
$content = file_get_contents($filepath);

// upload file
$filename = basename($file->getClientFileName());
$filepath = $file->getStream()->getMetadata('uri');
$headers = ['Content-Type' => $file->getClientMediaType()];
$apiClient = ApiClientProvider::getApiClient();
$response = $apiClient->upload($filename, $filepath, $headers);

// assoc stream to media
$streamId = $response['data']['id'];
$requestData['id'] = $this->assocStreamToMedia($streamId, $requestData, $filename);
$type = $this->getController()->getRequest()->getParam('object_type');
$response = $apiClient->post(
sprintf('/%s/upload/%s', $type, $filename),
$content,
$headers
);
$requestData['id'] = Hash::get($response, 'data.id');
}
unset($requestData['file'], $requestData['remote_url']);
}
Expand Down Expand Up @@ -391,6 +394,8 @@ public function removeStream(array $requestData): bool
* @param array $requestData The request data
* @param string $defaultTitle The default title for media
* @return string The media ID
* @deprecated 5.15.4 This method is no longer used and will be removed in a future version.
* @codeCoverageIgnore
*/
public function assocStreamToMedia(string $streamId, array &$requestData, string $defaultTitle): string
{
Expand Down
22 changes: 16 additions & 6 deletions tests/TestCase/Controller/Component/ModulesComponentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -858,9 +858,9 @@ public function testBeforeRender($userId, $modules, ?string $currentModule, arra
public function uploadProvider(): array
{
$filename = sprintf('%s/tests/files/%s', getcwd(), 'test.png');
$file = new UploadedFile($filename, filesize($filename), 0, $filename);
$fileErr = new UploadedFile($filename, filesize($filename), 1, $filename);
$fileEmpty = new UploadedFile($filename, filesize($filename), 4, $filename);
$file = new UploadedFile($filename, filesize($filename), 0, $filename, 'image/png');
$fileErr = new UploadedFile($filename, filesize($filename), 1, $filename, 'image/png');
$fileEmpty = new UploadedFile($filename, filesize($filename), 4, $filename, 'image/png');

return [
'no file' => [
Expand All @@ -870,6 +870,7 @@ public function uploadProvider(): array
],
null,
false,
null,
],
'model-type empty' => [
[
Expand All @@ -878,6 +879,7 @@ public function uploadProvider(): array
],
new InternalErrorException('Invalid form data: model-type'),
false,
null,
],
'model-type not a string' => [
[
Expand All @@ -887,6 +889,7 @@ public function uploadProvider(): array
],
new InternalErrorException('Invalid form data: model-type'),
false,
null,
],
'upload ok' => [
[
Expand All @@ -896,6 +899,7 @@ public function uploadProvider(): array
],
null,
true,
'image/png',
],
'generic upload error' => [
[
Expand All @@ -905,6 +909,7 @@ public function uploadProvider(): array
],
new UploadException(null, 1), // !UPLOAD_ERR_OK
true,
'image/png',
],
'save with empty file' => [
[
Expand All @@ -914,6 +919,7 @@ public function uploadProvider(): array
],
null,
false,
null,
],
'upload remote url' => [
[
Expand All @@ -926,6 +932,7 @@ public function uploadProvider(): array
'provider' => 'YouTube',
'provider_uid' => 'v=fE50xrnJnR8',
],
null,
],
];
}
Expand All @@ -936,14 +943,14 @@ public function uploadProvider(): array
* @param array $requestData The request data
* @param \Exception|null $expectedException The exception expected
* @param array|bool $uploaded The upload result (boolean or expected requestdata)
* @param string|null $contentType The content type of the uploaded file
* @return void
* @covers ::upload()
* @covers ::removeStream()
* @covers ::assocStreamToMedia()
* @covers ::checkRequestForUpload()
* @dataProvider uploadProvider()
*/
public function testUpload(array $requestData, $expectedException, $uploaded): void
public function testUpload(array $requestData, $expectedException, $uploaded, ?string $contentType): void
{
// if upload failed, verify exception
if ($expectedException != null) {
Expand All @@ -957,6 +964,9 @@ public function testUpload(array $requestData, $expectedException, $uploaded): v

if ($requestData['upload_behavior'] === 'file') {
// do component call
if (array_key_exists('model-type', $requestData)) {
$this->Modules->getController()->setRequest($this->Modules->getController()->getRequest()->withParam('object_type', $requestData['model-type']));
}
$this->Modules->upload($requestData);
} else {
// mock for ModulesComponent
Expand Down Expand Up @@ -991,7 +1001,7 @@ public function objectTypes(?bool $abstract = null): array

// test upload of another file to change stream
$filename = sprintf('%s/tests/files/%s', getcwd(), 'test2.png');
$file = new UploadedFile($filename, filesize($filename), 0, $filename);
$file = new UploadedFile($filename, filesize($filename), 0, $filename, $contentType);
$requestData = [
'file' => $file,
'model-type' => 'images',
Expand Down
Loading