[TASK] Decouple TemplateService->getFileName
[Packages/TYPO3.CMS.git] / typo3 / sysext / frontend / Tests / Unit / Resource / FilePathSanitizerTest.php
1 <?php
2 declare(strict_types = 1);
3
4 namespace TYPO3\CMS\Frontend\Tests\Unit\Resource;
5
6 /*
7 * This file is part of the TYPO3 CMS project.
8 *
9 * It is free software; you can redistribute it and/or modify it under
10 * the terms of the GNU General Public License, either version 2
11 * of the License, or any later version.
12 *
13 * For the full copyright and license information, please read the
14 * LICENSE.txt file that was distributed with this source code.
15 *
16 * The TYPO3 project - inspiring people to share!
17 */
18
19 use TYPO3\CMS\Core\Resource\Exception\FileDoesNotExistException;
20 use TYPO3\CMS\Core\Resource\Exception\InvalidFileNameException;
21 use TYPO3\CMS\Frontend\Resource\FilePathSanitizer;
22 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
23
24 /**
25 * Testcase for \TYPO3\CMS\Frontend\Resource\FilePathSanitizer
26 */
27 class FilePathSanitizerTest extends UnitTestCase
28 {
29 /**
30 * @test
31 */
32 public function sanitizeReturnsUrlCorrectly(): void
33 {
34 $subject = new FilePathSanitizer();
35 $this->assertSame('http://example.com', $subject->sanitize('http://example.com'));
36 $this->assertSame('https://example.com', $subject->sanitize('https://example.com'));
37 }
38
39 /**
40 * @test
41 */
42 public function sanitizeReturnsFileCorrectly(): void
43 {
44 $subject = new FilePathSanitizer();
45 $this->assertSame('typo3/index.php', $subject->sanitize('typo3/index.php'));
46 }
47
48 /**
49 * @test
50 */
51 public function sanitizeFailsIfDirectoryGiven(): void
52 {
53 $this->expectException(FileDoesNotExistException::class);
54 $subject = new FilePathSanitizer();
55 $subject->sanitize(__DIR__);
56 }
57
58 /**
59 * @test
60 */
61 public function sanitizeThrowsExceptionWithInvalidFileName(): void
62 {
63 $this->expectException(InvalidFileNameException::class);
64 $this->assertNull((new FilePathSanitizer())->sanitize(' '));
65 $this->assertNull((new FilePathSanitizer())->sanitize('something/../else'));
66 }
67 }