‪TYPO3CMS  10.4
CssViewHelperTest.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 
24 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
25 
26 class ‪CssViewHelperTest extends FunctionalTestCase
27 {
31  protected ‪$initializeDatabase = false;
32 
36  public function ‪sourceDataProvider(): array
37  {
38  return [
39  'fileadmin reference' => ['fileadmin/StyleSheets/foo.css'],
40  'EXT: reference' => ['EXT:core/Resources/Public/StyleSheets/foo.css'],
41  'external reference' => ['https://typo3.com/foo.css'],
42  'external reference with 1 parameter' => ['https://typo3.com/foo.css?foo=bar'],
43  'external reference with 2 parameters' => ['https://typo3.com/foo.css?foo=bar&bar=baz'],
44  ];
45  }
46 
52  public function ‪sourceStringIsNotHtmlEncodedBeforePassedToAssetCollector(string $href): void
53  {
54  $assetCollector = new ‪AssetCollector();
55  $viewHelper = new ‪CssViewHelper();
56  $viewHelper->injectAssetCollector($assetCollector);
57  $viewHelper->setArguments([
58  'identifier' => 'test',
59  'href' => $href,
60  'priority' => false,
61  ]);
62  $viewHelper->initializeArgumentsAndRender();
63  $collectedJavaScripts = $assetCollector->getStyleSheets();
64  self::assertSame($collectedJavaScripts['test']['source'], $href);
65  self::assertSame($collectedJavaScripts['test']['attributes'], []);
66  }
67 
71  public function ‪booleanAttributesAreProperlyConverted(): void
72  {
73  $assetCollector = new ‪AssetCollector();
74  $viewHelper = new ‪CssViewHelper();
75  $viewHelper->injectAssetCollector($assetCollector);
76  $viewHelper->setArguments([
77  'identifier' => 'test',
78  'href' => 'my.css',
79  'disabled' => true,
80  'priority' => false,
81  ]);
82  $viewHelper->initializeArgumentsAndRender();
83  $collectedJavaScripts = $assetCollector->getStyleSheets();
84  self::assertSame($collectedJavaScripts['test']['source'], 'my.css');
85  self::assertSame($collectedJavaScripts['test']['attributes'], ['disabled' => 'disabled']);
86  }
87 
88  public static function ‪childNodeRenderingIsCorrectDataProvider(): array
89  {
90  return [
91  // Double quotes
92  'variable with double quotes is encoded' => [
93  '</style>/* " ', // variable value
94  'body { color: #{color}; }', // inner template source
95  'body { color: #&lt;/style&gt;/* &quot; ; }', // expectation
96  ],
97  'variable with double quotes is encoded in single quotes' => [
98  '</style>/* " ', // variable value
99  'body { color: \'#{color}\'; }', // inner template source
100  'body { color: \'#&lt;/style&gt;/* &quot; \'; }', // expectation
101  ],
102  'variable with double quotes is encoded in double quotes' => [
103  '</style>/* " ', // variable value
104  'body { color: "#{color}"; }', // inner template source
105  'body { color: "#&lt;/style&gt;/* &quot; "; }', // expectation
106  ],
107  // Single quotes
108  'variable with single quotes is encoded' => [
109  '</style>/* \' ', // variable value
110  'body { color: #{color}; }', // inner template source
111  'body { color: #&lt;/style&gt;/* &#039; ; }', // expectation
112  ],
113  'variable with single quotes is encoded in single quotes' => [
114  '</style>/* \' ', // variable value
115  'body { color: \'#{color}\'; }', // inner template source
116  'body { color: \'#&lt;/style&gt;/* &#039; \'; }', // expectation
117  ],
118  'variable with single quotes is encoded in double quotes' => [
119  '</style>/* \' ', // variable value
120  'body { color: "#{color}"; }', // inner template source
121  'body { color: "#&lt;/style&gt;/* &#039; "; }', // expectation
122  ],
123  // Raw instruction
124  'raw instruction is passed' => [
125  '</style>/* " ',
126  'body { color: #{color -> f:format.raw()}; }',
127  'body { color: #</style>/* " ; }',
128  ],
129  'raw instruction is passed in sigle quotes' => [
130  '</style>/* " ',
131  'body { color: \'#{color -> f:format.raw()}\'; }',
132  'body { color: \'#</style>/* " \'; }',
133  ],
134  'raw instruction is passed in double quotes' => [
135  '</style>/* " ',
136  'body { color: "#{color -> f:format.raw()}"; }',
137  'body { color: "#</style>/* " "; }',
138  ],
139  ];
140  }
141 
146  public function ‪childNodeRenderingIsCorrect(string $value, string $source, string $expectation): void
147  {
148  $assetCollector = new ‪AssetCollector();
149  GeneralUtility::setSingletonInstance(AssetCollector::class, $assetCollector);
150 
151  $view = new ‪StandaloneView();
152  $view->setTemplateSource(sprintf('<f:asset.css identifier="test">%s</f:asset.css>', $source));
153  $view->assign('color', $value);
154  $view->render();
155  GeneralUtility::removeSingletonInstance(AssetCollector::class, $assetCollector);
156 
157  self::assertSame($expectation, $assetCollector->getInlineStyleSheets()['test']['source']);
158  }
159 }
‪TYPO3\CMS\Core\Page\AssetCollector
Definition: AssetCollector.php:44
‪TYPO3\CMS\Fluid\Tests\Functional\ViewHelpers\Asset\CssViewHelperTest\booleanAttributesAreProperlyConverted
‪booleanAttributesAreProperlyConverted()
Definition: CssViewHelperTest.php:70
‪TYPO3\CMS\Fluid\Tests\Functional\ViewHelpers\Asset\CssViewHelperTest\childNodeRenderingIsCorrect
‪childNodeRenderingIsCorrect(string $value, string $source, string $expectation)
Definition: CssViewHelperTest.php:145
‪TYPO3\CMS\Fluid\Tests\Functional\ViewHelpers\Asset\CssViewHelperTest
Definition: CssViewHelperTest.php:27
‪TYPO3\CMS\Fluid\Tests\Functional\ViewHelpers\Asset
Definition: CssViewHelperTest.php:18
‪TYPO3\CMS\Fluid\Tests\Functional\ViewHelpers\Asset\CssViewHelperTest\sourceDataProvider
‪array sourceDataProvider()
Definition: CssViewHelperTest.php:35
‪TYPO3\CMS\Fluid\Tests\Functional\ViewHelpers\Asset\CssViewHelperTest\$initializeDatabase
‪bool $initializeDatabase
Definition: CssViewHelperTest.php:30
‪TYPO3\CMS\Fluid\ViewHelpers\Asset\CssViewHelper
Definition: CssViewHelper.php:38
‪TYPO3\CMS\Fluid\Tests\Functional\ViewHelpers\Asset\CssViewHelperTest\sourceStringIsNotHtmlEncodedBeforePassedToAssetCollector
‪sourceStringIsNotHtmlEncodedBeforePassedToAssetCollector(string $href)
Definition: CssViewHelperTest.php:51
‪TYPO3\CMS\Fluid\View\StandaloneView
Definition: StandaloneView.php:34
‪TYPO3\CMS\Fluid\Tests\Functional\ViewHelpers\Asset\CssViewHelperTest\childNodeRenderingIsCorrectDataProvider
‪static childNodeRenderingIsCorrectDataProvider()
Definition: CssViewHelperTest.php:87
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46