1212use Ibexa \Contracts \Core \Repository \ContentService ;
1313use Ibexa \Contracts \Core \Repository \Values \Content \ContentInfo ;
1414use Ibexa \Contracts \Core \Repository \Values \Content \Field ;
15- use Ibexa \Core \Base \Exceptions \NotFoundException ;
15+ use Ibexa \Core \Base \Exceptions \NotFoundException as BaseNotFoundException ;
1616use Ibexa \Core \FieldType \BinaryFile \Value as BinaryFileValue ;
1717use Ibexa \Core \Helper \TranslationHelper ;
1818use Ibexa \Core \IO \IOServiceInterface ;
2222use Ibexa \Core \Repository \Values \Content \VersionInfo ;
2323use PHPUnit \Framework \TestCase ;
2424use Symfony \Component \HttpFoundation \Request ;
25+ use Symfony \Component \HttpKernel \Exception \NotFoundHttpException ;
2526
2627/**
2728 * @covers \Ibexa\Core\MVC\Symfony\Controller\Content\DownloadController
@@ -104,8 +105,76 @@ public function testDownloadBinaryFileActionReturnsNotFoundWhenFilenameDoesNotMa
104105 ->expects ($ this ->never ())
105106 ->method ('loadBinaryFile ' );
106107
107- $ this ->expectException (NotFoundException::class);
108- $ this ->createController ()->downloadBinaryFileAction (42 , 'file ' , 'SomeRandomText.txt ' , $ request );
108+ $ this ->assertFileNotFound (function () use ($ request ): void {
109+ $ this ->createController ()->downloadBinaryFileAction (42 , 'file ' , 'SomeRandomText.txt ' , $ request );
110+ });
111+ }
112+
113+ public function testDownloadBinaryFileActionReturnsNotFoundWhenFieldIdentifierDoesNotMatch (): void
114+ {
115+ $ content = $ this ->createContent (393 , 'New file ' );
116+ $ request = new Request (['inLanguage ' => 'eng-GB ' ]);
117+
118+ $ this ->contentService
119+ ->expects (self ::once ())
120+ ->method ('loadContent ' )
121+ ->with (393 )
122+ ->willReturn ($ content );
123+ $ this ->translationHelper
124+ ->expects (self ::once ())
125+ ->method ('getTranslatedField ' )
126+ ->with ($ content , 'file5 ' , 'eng-GB ' )
127+ ->willReturn (null );
128+ $ this ->ioService
129+ ->expects ($ this ->never ())
130+ ->method ('loadBinaryFile ' );
131+
132+ $ this ->assertFileNotFound (function () use ($ request ): void {
133+ $ this ->createController ()->downloadBinaryFileAction (393 , 'file5 ' , 'snorelax_snooze.png ' , $ request );
134+ });
135+ }
136+
137+ public function testDownloadBinaryFileActionReturnsNotFoundWhenContentDoesNotExist (): void
138+ {
139+ $ request = new Request (['inLanguage ' => 'eng-GB ' ]);
140+
141+ $ this ->contentService
142+ ->expects (self ::once ())
143+ ->method ('loadContent ' )
144+ ->with (393 )
145+ ->willThrowException (new BaseNotFoundException ('Content ' , 393 ));
146+ $ this ->translationHelper
147+ ->expects ($ this ->never ())
148+ ->method ('getTranslatedField ' );
149+ $ this ->ioService
150+ ->expects ($ this ->never ())
151+ ->method ('loadBinaryFile ' );
152+
153+ $ this ->assertFileNotFound (function () use ($ request ): void {
154+ $ this ->createController ()->downloadBinaryFileAction (393 , 'file ' , 'snorelax_snooze.png ' , $ request );
155+ });
156+ }
157+
158+ public function testDownloadBinaryFileByIdActionReturnsNotFoundWhenFieldIdDoesNotMatch (): void
159+ {
160+ $ content = $ this ->createContent ();
161+ $ request = new Request ();
162+
163+ $ this ->contentService
164+ ->expects (self ::once ())
165+ ->method ('loadContent ' )
166+ ->with (42 , null , null )
167+ ->willReturn ($ content );
168+ $ this ->translationHelper
169+ ->expects ($ this ->never ())
170+ ->method ('getTranslatedField ' );
171+ $ this ->ioService
172+ ->expects ($ this ->never ())
173+ ->method ('loadBinaryFile ' );
174+
175+ $ this ->assertFileNotFound (function () use ($ request ): void {
176+ $ this ->createController ()->downloadBinaryFileByIdAction ($ request , 42 , 123 );
177+ });
109178 }
110179
111180 private function createController (): DownloadController
@@ -117,11 +186,22 @@ private function createController(): DownloadController
117186 );
118187 }
119188
120- private function createContent (): Content
189+ private function assertFileNotFound (callable $ callback ): void
190+ {
191+ try {
192+ $ callback ();
193+ self ::fail (sprintf ('Expected %s to be thrown. ' , NotFoundHttpException::class));
194+ } catch (NotFoundHttpException $ e ) {
195+ self ::assertSame ('File not found ' , $ e ->getMessage ());
196+ }
197+ }
198+
199+ private function createContent (int $ contentId = 42 , string $ contentName = 'Test content ' ): Content
121200 {
122201 return new Content ([
123202 'internalFields ' => [
124203 new Field ([
204+ 'id ' => 7 ,
125205 'fieldDefIdentifier ' => 'file ' ,
126206 'languageCode ' => 'eng-GB ' ,
127207 'value ' => new BinaryFileValue ([
@@ -132,9 +212,9 @@ private function createContent(): Content
132212 ],
133213 'versionInfo ' => new VersionInfo ([
134214 'contentInfo ' => new ContentInfo ([
135- 'id ' => 42 ,
215+ 'id ' => $ contentId ,
136216 'mainLanguageCode ' => 'eng-GB ' ,
137- 'name ' => ' Test content ' ,
217+ 'name ' => $ contentName ,
138218 'status ' => ContentInfo::STATUS_PUBLISHED ,
139219 ]),
140220 ]),
0 commit comments