From 2ff34bf0550db3e576ca6720e527976c4df15a1d Mon Sep 17 00:00:00 2001 From: Gunnstein Lye Date: Tue, 24 Sep 2019 17:28:32 +0200 Subject: [PATCH] EZP-30956: Added raw URL encoding of download file name --- .../ContentDownloadRouteReferenceListener.php | 2 +- ...tentDownloadRouteReferenceListenerTest.php | 48 ++++++++++++++++++- 2 files changed, 47 insertions(+), 3 deletions(-) diff --git a/eZ/Bundle/EzPublishCoreBundle/EventListener/ContentDownloadRouteReferenceListener.php b/eZ/Bundle/EzPublishCoreBundle/EventListener/ContentDownloadRouteReferenceListener.php index ca20002d5f4..70750abc15d 100644 --- a/eZ/Bundle/EzPublishCoreBundle/EventListener/ContentDownloadRouteReferenceListener.php +++ b/eZ/Bundle/EzPublishCoreBundle/EventListener/ContentDownloadRouteReferenceListener.php @@ -67,7 +67,7 @@ public function onRouteReferenceGeneration(RouteReferenceGenerationEvent $event) } $routeReference->set(self::OPT_CONTENT_ID, $options[self::OPT_CONTENT_ID]); - $routeReference->set(self::OPT_DOWNLOAD_NAME, $options[self::OPT_DOWNLOAD_NAME]); + $routeReference->set(self::OPT_DOWNLOAD_NAME, rawurlencode($options[self::OPT_DOWNLOAD_NAME])); } /** diff --git a/eZ/Bundle/EzPublishCoreBundle/Tests/EventListener/ContentDownloadRouteReferenceListenerTest.php b/eZ/Bundle/EzPublishCoreBundle/Tests/EventListener/ContentDownloadRouteReferenceListenerTest.php index 18b25759f6c..ec7ba5179ab 100644 --- a/eZ/Bundle/EzPublishCoreBundle/Tests/EventListener/ContentDownloadRouteReferenceListenerTest.php +++ b/eZ/Bundle/EzPublishCoreBundle/Tests/EventListener/ContentDownloadRouteReferenceListenerTest.php @@ -134,10 +134,54 @@ public function testDownloadNameOverrideWorks() self::assertEquals('My-custom-filename.pdf', $routeReference->get(ContentDownloadRouteReferenceListener::OPT_DOWNLOAD_NAME)); } + public function testFilenameParenthesesAreEncoded() + { + $content = $this->getCompleteContent('(Parentheses)-filename.pdf'); + + $routeReference = new RouteReference( + ContentDownloadRouteReferenceListener::ROUTE_NAME, + [ + ContentDownloadRouteReferenceListener::OPT_CONTENT => $content, + ContentDownloadRouteReferenceListener::OPT_FIELD_IDENTIFIER => 'file', + ] + ); + $event = new RouteReferenceGenerationEvent($routeReference, new Request()); + $eventListener = $this->getListener(); + + $this + ->translationHelperMock + ->expects($this->once()) + ->method('getTranslatedField') + ->will($this->returnValue($content->getField('file', 'eng-GB'))); + $eventListener->onRouteReferenceGeneration($event); + + self::assertEquals('%28Parentheses%29-filename.pdf', $routeReference->get(ContentDownloadRouteReferenceListener::OPT_DOWNLOAD_NAME)); + } + + public function testOverrideFilenameParenthesesAreEncoded() + { + $content = $this->getCompleteContent(); + + $routeReference = new RouteReference( + ContentDownloadRouteReferenceListener::ROUTE_NAME, + [ + ContentDownloadRouteReferenceListener::OPT_CONTENT => $content, + ContentDownloadRouteReferenceListener::OPT_FIELD_IDENTIFIER => 'file', + ContentDownloadRouteReferenceListener::OPT_DOWNLOAD_NAME => '(Override)-filename.pdf', + ] + ); + $event = new RouteReferenceGenerationEvent($routeReference, new Request()); + $eventListener = $this->getListener(); + + $eventListener->onRouteReferenceGeneration($event); + + self::assertEquals('%28Override%29-filename.pdf', $routeReference->get(ContentDownloadRouteReferenceListener::OPT_DOWNLOAD_NAME)); + } + /** * @return \eZ\Publish\Core\Repository\Values\Content\Content */ - protected function getCompleteContent() + protected function getCompleteContent($filename = 'Test-file.pdf') { return new Content( [ @@ -146,7 +190,7 @@ protected function getCompleteContent() [ 'fieldDefIdentifier' => 'file', 'languageCode' => 'eng-GB', - 'value' => new BinaryFileValue(['fileName' => 'Test-file.pdf']), + 'value' => new BinaryFileValue(['fileName' => $filename]), ] ), ],