‪TYPO3CMS  ‪main
FileViewHelperTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use PHPUnit\Framework\Attributes\Test;
31 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
32 use TYPO3Fluid\Fluid\Core\ViewHelper\Exception;
33 use TYPO3Fluid\Fluid\View\TemplateView;
34 
35 final class ‪FileViewHelperTest extends FunctionalTestCase
36 {
37  private const ‪TEMPLATE_PATH = 'EXT:fluid/Tests/Functional/Fixtures/ViewHelpers/Link/FileViewHelper/Template.html';
38 
39  protected array ‪$additionalFoldersToCreate = [
40  '/fileadmin/user_upload',
41  '/fileadmin/_processed_',
42  ];
43 
45  'typo3/sysext/fluid/Tests/Functional/Fixtures/ViewHelpers/Link/FileViewHelper/Folders/fileadmin/user_upload/typo3_image2.jpg' => 'fileadmin/user_upload/typo3_image2.jpg',
46  'typo3/sysext/fluid/Tests/Functional/Fixtures/ViewHelpers/Link/FileViewHelper/Folders/fileadmin/_processed_/csm_typo3_image2_5c2670fd59.jpg' => 'fileadmin/_processed_/csm_typo3_image2_5c2670fd59.jpg',
47  ];
48 
49  protected function ‪setUp(): void
50  {
51  parent::setUp();
52  $this->importCSVDataSet(__DIR__ . '/../../Fixtures/ViewHelpers/Link/FileViewHelper/DatabaseImport.csv');
53  $this->importCSVDataSet(__DIR__ . '/../../Fixtures/be_users.csv');
54  $this->setUpBackendUser(1);
55  }
56 
57  protected function ‪tearDown(): void
58  {
59  $filesToDelete = [
60  ‪Environment::getPublicPath() . '/fileadmin/user_upload/typo3_image2.jpg',
61  ‪Environment::getPublicPath() . '/fileadmin/_processed_/csm_typo3_image2_5c2670fd59.jpg',
62  ];
63 
64  foreach ($filesToDelete as $file) {
65  if (file_exists($file)) {
66  unlink($file);
67  }
68  }
69 
70  parent::tearDown();
71  }
72 
73  #[Test]
74  public function ‪throwsExceptionOnMissingFile(): void
75  {
76  $this->expectException(Exception::class);
77  $this->expectExceptionCode(1621511632);
78 
79  $context = $this->get(RenderingContextFactory::class)->create();
80  $context->getTemplatePaths()->setTemplatePathAndFilename(self::TEMPLATE_PATH);
81  (new TemplateView($context))->render();
82  }
83 
84  #[Test]
85  public function ‪renderTagsForPublicFileTest(): void
86  {
87  $context = $this->get(RenderingContextFactory::class)->create();
88  $context->getTemplatePaths()->setTemplatePathAndFilename(self::TEMPLATE_PATH);
89  $view = new TemplateView($context);
90  $view->assign('file', $this->‪getFile(1));
91 
92  self::assertEquals(
93  [
94  '<a title="uid-1" href="fileadmin/user_upload/typo3_image2.jpg">typo3_image2.jpg</a>',
95  '<a title="uid-1" target="_blank" href="fileadmin/user_upload/typo3_image2.jpg">Link file</a>',
96  '<a title="uid-1" href="fileadmin/user_upload/typo3_image2.jpg">Link file - alt-name.jpg</a>',
97  '<a title="uid-1" download="" href="fileadmin/user_upload/typo3_image2.jpg">Download file</a>',
98  '<a title="uid-1" download="" href="fileadmin/user_upload/typo3_image2.jpg">Download file - alt name not valid</a>',
99  '<a title="uid-1" download="" href="fileadmin/user_upload/typo3_image2.jpg">Download file - alt name wrong extension</a>',
100  '<a title="uid-1" download="alt-name.jpg" href="fileadmin/user_upload/typo3_image2.jpg">Download file - alt-name.jpg</a>',
101  '<a title="uid-1" download="alt-name.jpg" href="fileadmin/user_upload/typo3_image2.jpg">Download file - extension is appended</a>',
102  ],
103  array_values(array_filter(explode(LF, $view->render())))
104  );
105  }
106 
107  #[Test]
108  public function ‪renderTagsForNonPublicFileTest(): void
109  {
110  // Set storage to non-public
111  $connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable('sys_file_storage');
112  $connection->update('sys_file_storage', ['is_public' => 0], ['uid' => 1]);
113 
114  $context = $this->get(RenderingContextFactory::class)->create();
115  $context->getTemplatePaths()->setTemplatePathAndFilename(self::TEMPLATE_PATH);
116  $view = new TemplateView($context);
117  $view->assign('file', $this->‪getFile(1));
118 
119  $expected = [
120  'index.php?eID=dumpFile&amp;t=f&amp;f=1&amp;token=',
121  'index.php?eID=dumpFile&amp;t=f&amp;f=1&amp;token=',
122  'index.php?eID=dumpFile&amp;t=f&amp;f=1&amp;fn=alt-name.jpg&amp;token=',
123  'index.php?eID=dumpFile&amp;t=f&amp;f=1&amp;dl=1&amp;token=',
124  'index.php?eID=dumpFile&amp;t=f&amp;f=1&amp;dl=1&amp;token=',
125  'index.php?eID=dumpFile&amp;t=f&amp;f=1&amp;dl=1&amp;token=',
126  'index.php?eID=dumpFile&amp;t=f&amp;f=1&amp;dl=1&amp;fn=alt-name.jpg&amp;token=',
127  'index.php?eID=dumpFile&amp;t=f&amp;f=1&amp;dl=1&amp;fn=alt-name.jpg&amp;token=',
128  ];
129 
130  foreach (array_values(array_filter(explode(LF, $view->render()))) as $key => $tag) {
131  self::assertStringContainsString($expected[$key], $tag);
132  }
133  }
134 
135  #[Test]
137  {
138  $context = $this->get(RenderingContextFactory::class)->create();
139  $context->getTemplatePaths()->setTemplatePathAndFilename(self::TEMPLATE_PATH);
140  $view = new TemplateView($context);
141  $view->assign('file', $this->‪getFileReference(2));
142 
143  self::assertEquals(
144  [
145  '<a title="uid-2" href="fileadmin/user_upload/typo3_image2.jpg">typo3_image2.jpg</a>',
146  '<a title="uid-2" target="_blank" href="fileadmin/user_upload/typo3_image2.jpg">Link file</a>',
147  '<a title="uid-2" href="fileadmin/user_upload/typo3_image2.jpg">Link file - alt-name.jpg</a>',
148  '<a title="uid-2" download="" href="fileadmin/user_upload/typo3_image2.jpg">Download file</a>',
149  '<a title="uid-2" download="" href="fileadmin/user_upload/typo3_image2.jpg">Download file - alt name not valid</a>',
150  '<a title="uid-2" download="" href="fileadmin/user_upload/typo3_image2.jpg">Download file - alt name wrong extension</a>',
151  '<a title="uid-2" download="alt-name.jpg" href="fileadmin/user_upload/typo3_image2.jpg">Download file - alt-name.jpg</a>',
152  '<a title="uid-2" download="alt-name.jpg" href="fileadmin/user_upload/typo3_image2.jpg">Download file - extension is appended</a>',
153  ],
154  array_values(array_filter(explode(LF, $view->render())))
155  );
156  }
157 
158  #[Test]
160  {
161  // Set storage to non-public
162  $connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable('sys_file_storage');
163  $connection->update('sys_file_storage', ['is_public' => 0], ['uid' => 1]);
164 
165  $context = $this->get(RenderingContextFactory::class)->create();
166  $context->getTemplatePaths()->setTemplatePathAndFilename(self::TEMPLATE_PATH);
167  $view = new TemplateView($context);
168  $view->assign('file', $this->‪getFileReference(2));
169 
170  $expected = [
171  'index.php?eID=dumpFile&amp;t=r&amp;r=2&amp;token=',
172  'index.php?eID=dumpFile&amp;t=r&amp;r=2&amp;token=',
173  'index.php?eID=dumpFile&amp;t=r&amp;r=2&amp;fn=alt-name.jpg&amp;token=',
174  'index.php?eID=dumpFile&amp;t=r&amp;r=2&amp;dl=1&amp;token=',
175  'index.php?eID=dumpFile&amp;t=r&amp;r=2&amp;dl=1&amp;token=',
176  'index.php?eID=dumpFile&amp;t=r&amp;r=2&amp;dl=1&amp;token=',
177  'index.php?eID=dumpFile&amp;t=r&amp;r=2&amp;dl=1&amp;fn=alt-name.jpg&amp;token=',
178  'index.php?eID=dumpFile&amp;t=r&amp;r=2&amp;dl=1&amp;fn=alt-name.jpg&amp;token=',
179  ];
180 
181  foreach (array_values(array_filter(explode(LF, $view->render()))) as $key => $tag) {
182  self::assertStringContainsString($expected[$key], $tag);
183  }
184  }
185 
186  #[Test]
188  {
189  $context = $this->get(RenderingContextFactory::class)->create();
190  $context->getTemplatePaths()->setTemplatePathAndFilename(self::TEMPLATE_PATH);
191  $view = new TemplateView($context);
192  $view->assign('file', $this->‪getProcessedFile(3));
193 
194  self::assertEquals(
195  [
196  '<a title="uid-3" href="fileadmin/_processed_/csm_typo3_image2_5c2670fd59.jpg">csm_typo3_image2_5c2670fd59.jpg</a>',
197  '<a title="uid-3" target="_blank" href="fileadmin/_processed_/csm_typo3_image2_5c2670fd59.jpg">Link file</a>',
198  '<a title="uid-3" href="fileadmin/_processed_/csm_typo3_image2_5c2670fd59.jpg">Link file - alt-name.jpg</a>',
199  '<a title="uid-3" download="" href="fileadmin/_processed_/csm_typo3_image2_5c2670fd59.jpg">Download file</a>',
200  '<a title="uid-3" download="" href="fileadmin/_processed_/csm_typo3_image2_5c2670fd59.jpg">Download file - alt name not valid</a>',
201  '<a title="uid-3" download="" href="fileadmin/_processed_/csm_typo3_image2_5c2670fd59.jpg">Download file - alt name wrong extension</a>',
202  '<a title="uid-3" download="alt-name.jpg" href="fileadmin/_processed_/csm_typo3_image2_5c2670fd59.jpg">Download file - alt-name.jpg</a>',
203  '<a title="uid-3" download="alt-name.jpg" href="fileadmin/_processed_/csm_typo3_image2_5c2670fd59.jpg">Download file - extension is appended</a>',
204  ],
205  array_values(array_filter(explode(LF, $view->render())))
206  );
207  }
208 
209  #[Test]
211  {
212  // Set storage to non-public
213  $connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable('sys_file_storage');
214  $connection->update('sys_file_storage', ['is_public' => 0], ['uid' => 1]);
215 
216  $context = $this->get(RenderingContextFactory::class)->create();
217  $context->getTemplatePaths()->setTemplatePathAndFilename(self::TEMPLATE_PATH);
218  $view = new TemplateView($context);
219  $view->assign('file', $this->‪getProcessedFile(3));
220 
221  $expected = [
222  'index.php?eID=dumpFile&amp;t=p&amp;p=3&amp;token=',
223  'index.php?eID=dumpFile&amp;t=p&amp;p=3&amp;token=',
224  'index.php?eID=dumpFile&amp;t=p&amp;p=3&amp;fn=alt-name.jpg&amp;token=',
225  'index.php?eID=dumpFile&amp;t=p&amp;p=3&amp;dl=1&amp;token=',
226  'index.php?eID=dumpFile&amp;t=p&amp;p=3&amp;dl=1&amp;token=',
227  'index.php?eID=dumpFile&amp;t=p&amp;p=3&amp;dl=1&amp;token=',
228  'index.php?eID=dumpFile&amp;t=p&amp;p=3&amp;dl=1&amp;fn=alt-name.jpg&amp;token=',
229  'index.php?eID=dumpFile&amp;t=p&amp;p=3&amp;dl=1&amp;fn=alt-name.jpg&amp;token=',
230  ];
231 
232  foreach (array_values(array_filter(explode(LF, $view->render()))) as $key => $tag) {
233  self::assertStringContainsString($expected[$key], $tag);
234  }
235  }
236 
237  protected function ‪getFile(int $fileUid): ‪File
238  {
239  return $this->get(ResourceFactory::class)->retrieveFileOrFolderObject($fileUid);
240  }
241 
242  protected function ‪getFileReference(int $fileUid): ‪FileReference
243  {
244  $fileCollector = GeneralUtility::makeInstance(FileCollector::class);
245  $fileCollector->addFileReferences([$fileUid]);
246  $fileReferences = $fileCollector->getFiles();
247 
248  return reset($fileReferences);
249  }
250 
251  protected function ‪getProcessedFile(int $fileUid): ‪ProcessedFile
252  {
253  return $this->get(ProcessedFileRepository::class)->findByUid($fileUid);
254  }
255 }
‪TYPO3\CMS\Core\Resource\ProcessedFileRepository
Definition: ProcessedFileRepository.php:39
‪TYPO3\CMS\Core\Core\Environment\getPublicPath
‪static getPublicPath()
Definition: Environment.php:187
‪TYPO3\CMS\Core\Resource\FileReference
Definition: FileReference.php:37
‪TYPO3\CMS\Frontend\Resource\FileCollector
Definition: FileCollector.php:47
‪TYPO3\CMS\Core\Resource\ResourceFactory
Definition: ResourceFactory.php:42
‪TYPO3\CMS\Core\Resource\File
Definition: File.php:26
‪TYPO3\CMS\Core\Resource\ProcessedFile
Definition: ProcessedFile.php:47
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:41
‪TYPO3\CMS\Fluid\Core\Rendering\RenderingContextFactory
Definition: RenderingContextFactory.php:51
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52