-
Notifications
You must be signed in to change notification settings - Fork 19
IBX-12042: Deprecated filename-less download route in favor of filename-validated variant #775
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
wiewiurdp
wants to merge
3
commits into
5.0
Choose a base branch
from
IBX-12042-Deprecate-filename-less-download-route-content-download-contentId-fieldId-and-replace-it-with-a-filename-validated-variant
base: 5.0
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
187 changes: 187 additions & 0 deletions
187
tests/lib/MVC/Symfony/Controller/Controller/Content/DownloadControllerTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,187 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
| * @license For full copyright and license information view LICENSE file distributed with this source code. | ||
| */ | ||
| declare(strict_types=1); | ||
|
|
||
| namespace Ibexa\Tests\Core\MVC\Symfony\Controller\Controller\Content; | ||
|
|
||
| use DateTime; | ||
| use Ibexa\Contracts\Core\Repository\ContentService; | ||
| use Ibexa\Contracts\Core\Repository\Values\Content\ContentInfo; | ||
| use Ibexa\Contracts\Core\Repository\Values\Content\Field; | ||
| use Ibexa\Core\Base\Exceptions\NotFoundException; | ||
| use Ibexa\Core\FieldType\BinaryFile\Value as BinaryFileValue; | ||
| use Ibexa\Core\Helper\TranslationHelper; | ||
| use Ibexa\Core\IO\IOServiceInterface; | ||
| use Ibexa\Core\IO\Values\BinaryFile; | ||
| use Ibexa\Core\MVC\Symfony\Controller\Content\DownloadController; | ||
| use Ibexa\Core\Repository\Values\Content\Content; | ||
| use Ibexa\Core\Repository\Values\Content\VersionInfo; | ||
| use PHPUnit\Framework\MockObject\MockObject; | ||
| use PHPUnit\Framework\TestCase; | ||
| use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait; | ||
| use Symfony\Component\DependencyInjection\ContainerInterface; | ||
| use Symfony\Component\HttpFoundation\Request; | ||
|
|
||
| /** | ||
| * @covers \Ibexa\Core\MVC\Symfony\Controller\Content\DownloadController | ||
| */ | ||
| final class DownloadControllerTest extends TestCase | ||
| { | ||
| use ExpectDeprecationTrait; | ||
|
|
||
| private const string FILENAME = 'Test-file.pdf'; | ||
|
|
||
| private ContentService & MockObject $contentService; | ||
|
|
||
| private IOServiceInterface & MockObject $ioService; | ||
|
|
||
| private TranslationHelper & MockObject $translationHelper; | ||
|
|
||
| protected function setUp(): void | ||
| { | ||
| parent::setUp(); | ||
|
|
||
| $this->contentService = $this->createMock(ContentService::class); | ||
| $this->ioService = $this->createMock(IOServiceInterface::class); | ||
| $this->translationHelper = $this->createMock(TranslationHelper::class); | ||
| } | ||
|
|
||
| public function testDownloadBinaryFileByIdActionReturnsFileWhenFilenameMatches(): void | ||
| { | ||
| $content = $this->createContent(); | ||
| $field = $content->getField('file'); | ||
| self::assertInstanceOf(Field::class, $field); | ||
|
|
||
| $this->contentService | ||
| ->method('loadContent') | ||
| ->willReturn($content); | ||
| $this->translationHelper | ||
| ->expects(self::once()) | ||
| ->method('getTranslatedField') | ||
| ->with($content, 'file') | ||
| ->willReturn($field); | ||
| $this->ioService | ||
| ->expects(self::once()) | ||
| ->method('loadBinaryFile') | ||
| ->with('binary-file-id') | ||
| ->willReturn($this->createBinaryFile()); | ||
|
|
||
| $response = $this->createController()->downloadBinaryFileByIdAction(new Request(), 42, 7, self::FILENAME); | ||
|
|
||
| self::assertStringContainsString('attachment;', (string)$response->headers->get('Content-Disposition')); | ||
| self::assertStringContainsString(self::FILENAME, (string)$response->headers->get('Content-Disposition')); | ||
| } | ||
|
|
||
| /** | ||
| * @dataProvider provideNotFoundCases | ||
| */ | ||
| public function testDownloadBinaryFileByIdActionReturnsNotFound(int $fieldId, string $filename): void | ||
| { | ||
| $content = $this->createContent(); | ||
|
|
||
| $this->contentService | ||
| ->expects(self::once()) | ||
| ->method('loadContent') | ||
| ->with(42, null, null) | ||
| ->willReturn($content); | ||
| $this->translationHelper | ||
| ->expects(self::never()) | ||
| ->method('getTranslatedField'); | ||
| $this->ioService | ||
| ->expects(self::never()) | ||
| ->method('loadBinaryFile'); | ||
|
|
||
| $this->expectException(NotFoundException::class); | ||
|
|
||
| $this->createController()->downloadBinaryFileByIdAction(new Request(), 42, $fieldId, $filename); | ||
| } | ||
|
|
||
| /** | ||
| * @return iterable<string, array{int, string}> | ||
| */ | ||
| public static function provideNotFoundCases(): iterable | ||
| { | ||
| yield 'filename does not match the field value' => [7, 'SomeRandomText.txt']; | ||
| yield 'field id does not exist in content' => [123, self::FILENAME]; | ||
| } | ||
|
|
||
| /** | ||
| * @group legacy | ||
| */ | ||
| public function testDownloadBinaryFileByIdActionTriggersDeprecationWithoutFilename(): void | ||
| { | ||
| $content = $this->createContent(); | ||
| $field = $content->getField('file'); | ||
| self::assertInstanceOf(Field::class, $field); | ||
|
|
||
| $this->contentService | ||
| ->method('loadContent') | ||
| ->willReturn($content); | ||
| $this->translationHelper | ||
| ->method('getTranslatedField') | ||
| ->willReturn($field); | ||
| $this->ioService | ||
| ->method('loadBinaryFile') | ||
| ->willReturn($this->createBinaryFile()); | ||
|
|
||
| $this->expectDeprecation( | ||
| 'Since ibexa/core 5.0: The "ibexa.content.download.field_id" route (/content/download/{contentId}/{fieldId})' | ||
| . ' is deprecated and will be removed in 6.0.' | ||
| . ' Use the "ibexa.content.download.field_id.filename" route' | ||
| . ' (/content/download/{contentId}/{fieldId}/{filename}) instead.' | ||
| ); | ||
|
|
||
| $response = $this->createController()->downloadBinaryFileByIdAction(new Request(), 42, 7); | ||
|
|
||
| self::assertStringContainsString(self::FILENAME, (string)$response->headers->get('Content-Disposition')); | ||
| } | ||
|
|
||
| private function createController(): DownloadController | ||
| { | ||
| return new DownloadController( | ||
| $this->createMock(ContainerInterface::class), | ||
| $this->contentService, | ||
| $this->ioService, | ||
| $this->translationHelper | ||
| ); | ||
| } | ||
|
|
||
| private function createContent(): Content | ||
| { | ||
| return new Content([ | ||
| 'internalFields' => [ | ||
| new Field([ | ||
| 'id' => 7, | ||
| 'fieldDefIdentifier' => 'file', | ||
| 'languageCode' => 'eng-GB', | ||
| 'value' => new BinaryFileValue([ | ||
| 'id' => 'binary-file-id', | ||
| 'fileName' => self::FILENAME, | ||
| ]), | ||
| ]), | ||
| ], | ||
| 'versionInfo' => new VersionInfo([ | ||
| 'contentInfo' => new ContentInfo([ | ||
| 'id' => 42, | ||
| 'mainLanguageCode' => 'eng-GB', | ||
| 'name' => 'Test content', | ||
| 'status' => ContentInfo::STATUS_PUBLISHED, | ||
| ]), | ||
| ]), | ||
| ]); | ||
| } | ||
|
|
||
| private function createBinaryFile(): BinaryFile | ||
| { | ||
| return new BinaryFile([ | ||
| 'id' => 'binary-file-id', | ||
| 'mtime' => new DateTime(), | ||
| 'size' => 123, | ||
| 'uri' => 'binary-file-uri', | ||
| ]); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.