‪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 
30 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
31 use TYPO3Fluid\Fluid\Core\ViewHelper\Exception;
32 use TYPO3Fluid\Fluid\View\TemplateView;
33 
34 final class ‪FileViewHelperTest extends FunctionalTestCase
35 {
36  private const ‪TEMPLATE_PATH = 'EXT:fluid/Tests/Functional/Fixtures/ViewHelpers/Link/FileViewHelper/Template.html';
37 
38  protected array ‪$additionalFoldersToCreate = [
39  '/fileadmin/user_upload',
40  '/fileadmin/_processed_',
41  ];
42 
44  'typo3/sysext/fluid/Tests/Functional/Fixtures/ViewHelpers/Link/FileViewHelper/Folders/fileadmin/user_upload/typo3_image2.jpg' => 'fileadmin/user_upload/typo3_image2.jpg',
45  'typo3/sysext/fluid/Tests/Functional/Fixtures/ViewHelpers/Link/FileViewHelper/Folders/fileadmin/_processed_/csm_typo3_image2_5c2670fd59.jpg' => 'fileadmin/_processed_/csm_typo3_image2_5c2670fd59.jpg',
46  ];
47 
48  protected function ‪setUp(): void
49  {
50  parent::setUp();
51  $this->importCSVDataSet(__DIR__ . '/../../Fixtures/ViewHelpers/Link/FileViewHelper/DatabaseImport.csv');
52  $this->importCSVDataSet(__DIR__ . '/../../Fixtures/be_users.csv');
53  $this->setUpBackendUser(1);
54  }
55 
56  protected function ‪tearDown(): void
57  {
58  $filesToDelete = [
59  ‪Environment::getPublicPath() . '/fileadmin/user_upload/typo3_image2.jpg',
60  ‪Environment::getPublicPath() . '/fileadmin/_processed_/csm_typo3_image2_5c2670fd59.jpg',
61  ];
62 
63  foreach ($filesToDelete as $file) {
64  if (file_exists($file)) {
65  unlink($file);
66  }
67  }
68 
69  parent::tearDown();
70  }
71 
75  public function ‪throwsExceptionOnMissingFile(): void
76  {
77  $this->expectException(Exception::class);
78  $this->expectExceptionCode(1621511632);
79 
80  $context = $this->get(RenderingContextFactory::class)->create();
81  $context->getTemplatePaths()->setTemplatePathAndFilename(self::TEMPLATE_PATH);
82  (new TemplateView($context))->render();
83  }
84 
88  public function ‪renderTagsForPublicFileTest(): void
89  {
90  $context = $this->get(RenderingContextFactory::class)->create();
91  $context->getTemplatePaths()->setTemplatePathAndFilename(self::TEMPLATE_PATH);
92  $view = new TemplateView($context);
93  $view->assign('file', $this->‪getFile(1));
94 
95  self::assertEquals(
96  [
97  '<a title="uid-1" href="fileadmin/user_upload/typo3_image2.jpg">typo3_image2.jpg</a>',
98  '<a title="uid-1" target="_blank" href="fileadmin/user_upload/typo3_image2.jpg">Link file</a>',
99  '<a title="uid-1" href="fileadmin/user_upload/typo3_image2.jpg">Link file - alt-name.jpg</a>',
100  '<a title="uid-1" download="" href="fileadmin/user_upload/typo3_image2.jpg">Download file</a>',
101  '<a title="uid-1" download="" href="fileadmin/user_upload/typo3_image2.jpg">Download file - alt name not valid</a>',
102  '<a title="uid-1" download="" href="fileadmin/user_upload/typo3_image2.jpg">Download file - alt name wrong extension</a>',
103  '<a title="uid-1" download="alt-name.jpg" href="fileadmin/user_upload/typo3_image2.jpg">Download file - alt-name.jpg</a>',
104  '<a title="uid-1" download="alt-name.jpg" href="fileadmin/user_upload/typo3_image2.jpg">Download file - extension is appended</a>',
105  ],
106  array_values(array_filter(explode(LF, $view->render())))
107  );
108  }
109 
113  public function ‪renderTagsForNonPublicFileTest(): void
114  {
115  // Set storage to non-public
116  $connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable('sys_file_storage');
117  $connection->update('sys_file_storage', ['is_public' => 0], ['uid' => 1]);
118 
119  $context = $this->get(RenderingContextFactory::class)->create();
120  $context->getTemplatePaths()->setTemplatePathAndFilename(self::TEMPLATE_PATH);
121  $view = new TemplateView($context);
122  $view->assign('file', $this->‪getFile(1));
123 
124  $expected = [
125  'index.php?eID=dumpFile&amp;t=f&amp;f=1&amp;token=',
126  'index.php?eID=dumpFile&amp;t=f&amp;f=1&amp;token=',
127  'index.php?eID=dumpFile&amp;t=f&amp;f=1&amp;fn=alt-name.jpg&amp;token=',
128  'index.php?eID=dumpFile&amp;t=f&amp;f=1&amp;dl=1&amp;token=',
129  'index.php?eID=dumpFile&amp;t=f&amp;f=1&amp;dl=1&amp;token=',
130  'index.php?eID=dumpFile&amp;t=f&amp;f=1&amp;dl=1&amp;token=',
131  'index.php?eID=dumpFile&amp;t=f&amp;f=1&amp;dl=1&amp;fn=alt-name.jpg&amp;token=',
132  'index.php?eID=dumpFile&amp;t=f&amp;f=1&amp;dl=1&amp;fn=alt-name.jpg&amp;token=',
133  ];
134 
135  foreach (array_values(array_filter(explode(LF, $view->render()))) as $key => $tag) {
136  self::assertStringContainsString($expected[$key], $tag);
137  }
138  }
139 
144  {
145  $context = $this->get(RenderingContextFactory::class)->create();
146  $context->getTemplatePaths()->setTemplatePathAndFilename(self::TEMPLATE_PATH);
147  $view = new TemplateView($context);
148  $view->assign('file', $this->‪getFileReference(2));
149 
150  self::assertEquals(
151  [
152  '<a title="uid-2" href="fileadmin/user_upload/typo3_image2.jpg">typo3_image2.jpg</a>',
153  '<a title="uid-2" target="_blank" href="fileadmin/user_upload/typo3_image2.jpg">Link file</a>',
154  '<a title="uid-2" href="fileadmin/user_upload/typo3_image2.jpg">Link file - alt-name.jpg</a>',
155  '<a title="uid-2" download="" href="fileadmin/user_upload/typo3_image2.jpg">Download file</a>',
156  '<a title="uid-2" download="" href="fileadmin/user_upload/typo3_image2.jpg">Download file - alt name not valid</a>',
157  '<a title="uid-2" download="" href="fileadmin/user_upload/typo3_image2.jpg">Download file - alt name wrong extension</a>',
158  '<a title="uid-2" download="alt-name.jpg" href="fileadmin/user_upload/typo3_image2.jpg">Download file - alt-name.jpg</a>',
159  '<a title="uid-2" download="alt-name.jpg" href="fileadmin/user_upload/typo3_image2.jpg">Download file - extension is appended</a>',
160  ],
161  array_values(array_filter(explode(LF, $view->render())))
162  );
163  }
164 
169  {
170  // Set storage to non-public
171  $connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable('sys_file_storage');
172  $connection->update('sys_file_storage', ['is_public' => 0], ['uid' => 1]);
173 
174  $context = $this->get(RenderingContextFactory::class)->create();
175  $context->getTemplatePaths()->setTemplatePathAndFilename(self::TEMPLATE_PATH);
176  $view = new TemplateView($context);
177  $view->assign('file', $this->‪getFileReference(2));
178 
179  $expected = [
180  'index.php?eID=dumpFile&amp;t=r&amp;r=2&amp;token=',
181  'index.php?eID=dumpFile&amp;t=r&amp;r=2&amp;token=',
182  'index.php?eID=dumpFile&amp;t=r&amp;r=2&amp;fn=alt-name.jpg&amp;token=',
183  'index.php?eID=dumpFile&amp;t=r&amp;r=2&amp;dl=1&amp;token=',
184  'index.php?eID=dumpFile&amp;t=r&amp;r=2&amp;dl=1&amp;token=',
185  'index.php?eID=dumpFile&amp;t=r&amp;r=2&amp;dl=1&amp;token=',
186  'index.php?eID=dumpFile&amp;t=r&amp;r=2&amp;dl=1&amp;fn=alt-name.jpg&amp;token=',
187  'index.php?eID=dumpFile&amp;t=r&amp;r=2&amp;dl=1&amp;fn=alt-name.jpg&amp;token=',
188  ];
189 
190  foreach (array_values(array_filter(explode(LF, $view->render()))) as $key => $tag) {
191  self::assertStringContainsString($expected[$key], $tag);
192  }
193  }
194 
199  {
200  $context = $this->get(RenderingContextFactory::class)->create();
201  $context->getTemplatePaths()->setTemplatePathAndFilename(self::TEMPLATE_PATH);
202  $view = new TemplateView($context);
203  $view->assign('file', $this->‪getProcessedFile(3));
204 
205  self::assertEquals(
206  [
207  '<a title="uid-3" href="fileadmin/_processed_/csm_typo3_image2_5c2670fd59.jpg">csm_typo3_image2_5c2670fd59.jpg</a>',
208  '<a title="uid-3" target="_blank" href="fileadmin/_processed_/csm_typo3_image2_5c2670fd59.jpg">Link file</a>',
209  '<a title="uid-3" href="fileadmin/_processed_/csm_typo3_image2_5c2670fd59.jpg">Link file - alt-name.jpg</a>',
210  '<a title="uid-3" download="" href="fileadmin/_processed_/csm_typo3_image2_5c2670fd59.jpg">Download file</a>',
211  '<a title="uid-3" download="" href="fileadmin/_processed_/csm_typo3_image2_5c2670fd59.jpg">Download file - alt name not valid</a>',
212  '<a title="uid-3" download="" href="fileadmin/_processed_/csm_typo3_image2_5c2670fd59.jpg">Download file - alt name wrong extension</a>',
213  '<a title="uid-3" download="alt-name.jpg" href="fileadmin/_processed_/csm_typo3_image2_5c2670fd59.jpg">Download file - alt-name.jpg</a>',
214  '<a title="uid-3" download="alt-name.jpg" href="fileadmin/_processed_/csm_typo3_image2_5c2670fd59.jpg">Download file - extension is appended</a>',
215  ],
216  array_values(array_filter(explode(LF, $view->render())))
217  );
218  }
219 
224  {
225  // Set storage to non-public
226  $connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable('sys_file_storage');
227  $connection->update('sys_file_storage', ['is_public' => 0], ['uid' => 1]);
228 
229  $context = $this->get(RenderingContextFactory::class)->create();
230  $context->getTemplatePaths()->setTemplatePathAndFilename(self::TEMPLATE_PATH);
231  $view = new TemplateView($context);
232  $view->assign('file', $this->‪getProcessedFile(3));
233 
234  $expected = [
235  'index.php?eID=dumpFile&amp;t=p&amp;p=3&amp;token=',
236  'index.php?eID=dumpFile&amp;t=p&amp;p=3&amp;token=',
237  'index.php?eID=dumpFile&amp;t=p&amp;p=3&amp;fn=alt-name.jpg&amp;token=',
238  'index.php?eID=dumpFile&amp;t=p&amp;p=3&amp;dl=1&amp;token=',
239  'index.php?eID=dumpFile&amp;t=p&amp;p=3&amp;dl=1&amp;token=',
240  'index.php?eID=dumpFile&amp;t=p&amp;p=3&amp;dl=1&amp;token=',
241  'index.php?eID=dumpFile&amp;t=p&amp;p=3&amp;dl=1&amp;fn=alt-name.jpg&amp;token=',
242  'index.php?eID=dumpFile&amp;t=p&amp;p=3&amp;dl=1&amp;fn=alt-name.jpg&amp;token=',
243  ];
244 
245  foreach (array_values(array_filter(explode(LF, $view->render()))) as $key => $tag) {
246  self::assertStringContainsString($expected[$key], $tag);
247  }
248  }
249 
250  protected function ‪getFile(int $fileUid): ‪File
251  {
252  return $this->get(ResourceFactory::class)->retrieveFileOrFolderObject($fileUid);
253  }
254 
255  protected function ‪getFileReference(int $fileUid): ‪FileReference
256  {
257  $fileCollector = GeneralUtility::makeInstance(FileCollector::class);
258  $fileCollector->addFileReferences([$fileUid]);
259  $fileReferences = $fileCollector->getFiles();
260 
261  return reset($fileReferences);
262  }
263 
264  protected function ‪getProcessedFile(int $fileUid): ‪ProcessedFile
265  {
266  return $this->get(ProcessedFileRepository::class)->findByUid($fileUid);
267  }
268 }
‪TYPO3\CMS\Core\Resource\ProcessedFileRepository
Definition: ProcessedFileRepository.php:37
‪TYPO3\CMS\Core\Core\Environment\getPublicPath
‪static getPublicPath()
Definition: Environment.php:187
‪TYPO3\CMS\Core\Resource\FileReference
Definition: FileReference.php:35
‪TYPO3\CMS\Frontend\Resource\FileCollector
Definition: FileCollector.php:47
‪TYPO3\CMS\Core\Resource\ResourceFactory
Definition: ResourceFactory.php:41
‪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:51
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:51