[TASK] Make sysext/core/Tests/Unit/Resource/ notice free
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Resource / ResourceCompressorIntegrationTest.php
1 <?php
2 declare(strict_types = 1);
3
4 namespace TYPO3\CMS\Core\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\Core\Environment;
20 use TYPO3\CMS\Core\Tests\Unit\Resource\ResourceCompressorTest\Fixtures\TestableResourceCompressor;
21 use TYPO3\CMS\Core\Utility\GeneralUtility;
22
23 /**
24 * Test case
25 */
26 class ResourceCompressorIntegrationTest extends BaseTestCase
27 {
28 /**
29 * @var TestableResourceCompressor
30 */
31 protected $resourceCompressor;
32
33 /**
34 * @var string
35 */
36 protected $fixtureDir;
37
38 /**
39 * @var string
40 */
41 protected $fixtureDirFromTest;
42
43 public function setUp(): void
44 {
45 $this->fixtureDir = 'sysext/core/Tests/Unit/Resource/ResourceCompressorTest/Fixtures/';
46 $this->fixtureDirFromTest = GeneralUtility::fixWindowsFilePath(__DIR__ . '/ResourceCompressorTest/Fixtures/');
47 }
48
49 /**
50 * @test
51 */
52 public function constructorCreatesTargetDirectory(): void
53 {
54 $this->resourceCompressor = new TestableResourceCompressor();
55 $dir = Environment::getPublicPath() . '/' . $this->resourceCompressor->getTargetDirectory();
56 self::assertFileExists($dir);
57 }
58
59 /**
60 * @test
61 */
62 public function constructorCreatesHtaccessFileIfSet(): void
63 {
64 $GLOBALS['TYPO3_CONF_VARS']['SYS']['generateApacheHtaccess'] = true;
65 $this->resourceCompressor = new TestableResourceCompressor();
66 $htaccessPath = Environment::getPublicPath() . '/' . $this->resourceCompressor->getTargetDirectory() . '.htaccess';
67 self::assertStringEqualsFile($htaccessPath, $this->resourceCompressor->getHtaccessTemplate());
68 }
69
70 /**
71 * @test
72 */
73 public function constructorDoesNotCreateHtaccessFileIfSetToFalse(): void
74 {
75 $GLOBALS['TYPO3_CONF_VARS']['SYS']['generateApacheHtaccess'] = false;
76 $this->resourceCompressor = new TestableResourceCompressor();
77 $htaccessPath = Environment::getPublicPath() . '/' . $this->resourceCompressor->getTargetDirectory() . '.htaccess';
78 self::assertFileNotExists($htaccessPath);
79 }
80
81 /**
82 * @test
83 */
84 public function concatenateCssFiles(): void
85 {
86 $files = [
87 'sampleFile1' => [
88 'excludeFromConcatenation' => false,
89 'file' => $this->fixtureDir . 'css_input_with_import.css',
90 'media' => 'screen',
91 'forceOnTop' => false,
92 ],
93 ];
94 $this->resourceCompressor = new TestableResourceCompressor();
95 $concatFiles = $this->resourceCompressor->concatenateCssFiles($files);
96 $mergedFile = array_pop($concatFiles);
97 $expected = file_get_contents(
98 $this->fixtureDirFromTest . 'expected' . DIRECTORY_SEPARATOR . 'merged-css_input_with_import.css'
99 );
100 self::assertStringEqualsFile(GeneralUtility::fixWindowsFilePath(Environment::getPublicPath() . '/' . $mergedFile['file']), $expected);
101 }
102
103 /**
104 * @test
105 */
106 public function concatenateCssFilesWorksWithFileFromNonRootPath(): void
107 {
108 $testFile = Environment::getPublicPath() . '/typo3temp/var/transient/css_input_with_import.css';
109 $this->testFilesToDelete[] = $testFile;
110 copy(Environment::getBackendPath() . '/' . $this->fixtureDir . 'css_input_with_import.css', $testFile);
111 $files = [
112 'sampleFile1' => [
113 'excludeFromConcatenation' => false,
114 'file' => 'typo3temp/var/transient/css_input_with_import.css',
115 'media' => 'screen',
116 'forceOnTop' => false,
117 ],
118 ];
119 $this->resourceCompressor = new TestableResourceCompressor();
120 $concatFiles = $this->resourceCompressor->concatenateCssFiles($files);
121 $mergedFile = array_pop($concatFiles);
122 $expected = file_get_contents(
123 $this->fixtureDirFromTest . 'expected' . DIRECTORY_SEPARATOR . 'merged-css_input_with_import_non_root.css'
124 );
125 self::assertStringEqualsFile(GeneralUtility::fixWindowsFilePath(Environment::getPublicPath() . '/' . $mergedFile['file']), $expected);
126 }
127
128 public function tearDown(): void
129 {
130 $this->testFilesToDelete[] = Environment::getPublicPath() . '/' . $this->resourceCompressor->getTargetDirectory();
131 parent::tearDown();
132 }
133 }