‪TYPO3CMS  ‪main
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 
20 use PHPUnit\Framework\Attributes\DataProvider;
21 use PHPUnit\Framework\Attributes\Test;
24 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
25 use TYPO3Fluid\Fluid\View\TemplateView;
26 
27 final class ‪CssViewHelperTest extends FunctionalTestCase
28 {
29  protected bool ‪$initializeDatabase = false;
30 
31  public static function ‪sourceDataProvider(): array
32  {
33  return [
34  'fileadmin reference' => ['fileadmin/StyleSheets/foo.css'],
35  'EXT: reference' => ['EXT:core/Resources/Public/StyleSheets/foo.css'],
36  'external reference' => ['https://typo3.com/foo.css'],
37  'external reference with 1 parameter' => ['https://typo3.com/foo.css?foo=bar'],
38  'external reference with 2 parameters' => ['https://typo3.com/foo.css?foo=bar&bar=baz'],
39  ];
40  }
41 
42  #[DataProvider('sourceDataProvider')]
43  #[Test]
45  {
46  $context = $this->get(RenderingContextFactory::class)->create();
47  $context->getTemplatePaths()->setTemplateSource('<f:asset.css identifier="test" href="' . $href . '" priority="0"/>');
48 
49  (new ‪TemplateView($context))->render();
50 
51  $collectedStyleSheets = $this->get(AssetCollector::class)->getStyleSheets();
52  self::assertSame($href, $collectedStyleSheets['test']['source']);
53  self::assertSame([], $collectedStyleSheets['test']['attributes']);
54  }
55 
56  #[Test]
58  {
59  $context = $this->get(RenderingContextFactory::class)->create();
60  $context->getTemplatePaths()->setTemplateSource('<f:asset.css identifier="test" href="my.css" disabled="1" priority="0"/>');
61 
62  (new ‪TemplateView($context))->render();
63 
64  $collectedStyleSheets = $this->get(AssetCollector::class)->getStyleSheets();
65  self::assertSame('my.css', $collectedStyleSheets['test']['source']);
66  self::assertSame(['disabled' => 'disabled'], $collectedStyleSheets['test']['attributes']);
67  }
68 
69  public static function ‪childNodeRenderingIsCorrectDataProvider(): array
70  {
71  return [
72  // Double quotes
73  'variable with double quotes is encoded' => [
74  '</style>/* " ', // variable value
75  'body { color: #{color}; }', // inner template source
76  'body { color: #&lt;/style&gt;/* &quot; ; }', // expectation
77  ],
78  'variable with double quotes is encoded in single quotes' => [
79  '</style>/* " ', // variable value
80  'body { color: \'#{color}\'; }', // inner template source
81  'body { color: \'#&lt;/style&gt;/* &quot; \'; }', // expectation
82  ],
83  'variable with double quotes is encoded in double quotes' => [
84  '</style>/* " ', // variable value
85  'body { color: "#{color}"; }', // inner template source
86  'body { color: "#&lt;/style&gt;/* &quot; "; }', // expectation
87  ],
88  // Single quotes
89  'variable with single quotes is encoded' => [
90  '</style>/* \' ', // variable value
91  'body { color: #{color}; }', // inner template source
92  'body { color: #&lt;/style&gt;/* &#039; ; }', // expectation
93  ],
94  'variable with single quotes is encoded in single quotes' => [
95  '</style>/* \' ', // variable value
96  'body { color: \'#{color}\'; }', // inner template source
97  'body { color: \'#&lt;/style&gt;/* &#039; \'; }', // expectation
98  ],
99  'variable with single quotes is encoded in double quotes' => [
100  '</style>/* \' ', // variable value
101  'body { color: "#{color}"; }', // inner template source
102  'body { color: "#&lt;/style&gt;/* &#039; "; }', // expectation
103  ],
104  // Raw instruction
105  'raw instruction is passed' => [
106  '</style>/* " ',
107  'body { color: #{color -> f:format.raw()}; }',
108  'body { color: #</style>/* " ; }',
109  ],
110  'raw instruction is passed in sigle quotes' => [
111  '</style>/* " ',
112  'body { color: \'#{color -> f:format.raw()}\'; }',
113  'body { color: \'#</style>/* " \'; }',
114  ],
115  'raw instruction is passed in double quotes' => [
116  '</style>/* " ',
117  'body { color: "#{color -> f:format.raw()}"; }',
118  'body { color: "#</style>/* " "; }',
119  ],
120  ];
121  }
122 
123  #[DataProvider('childNodeRenderingIsCorrectDataProvider')]
124  #[Test]
125  public function ‪childNodeRenderingIsCorrect(string $value, string $source, string $expectation): void
126  {
127  $context = $this->get(RenderingContextFactory::class)->create();
128  $context->getTemplatePaths()->setTemplateSource('<f:asset.css identifier="test">' . $source . '</f:asset.css>');
129  $context->getVariableProvider()->add('color', $value);
130 
131  (new ‪TemplateView($context))->render();
132 
133  $collectedInlineStyleSheets = $this->get(AssetCollector::class)->getInlineStyleSheets();
134  self::assertSame($expectation, $collectedInlineStyleSheets['test']['source']);
135  }
136 }
‪TYPO3\CMS\Core\Page\AssetCollector
Definition: AssetCollector.php:42
‪TYPO3\CMS\Fluid\Tests\Functional\ViewHelpers\Asset\CssViewHelperTest\booleanAttributesAreProperlyConverted
‪booleanAttributesAreProperlyConverted()
Definition: CssViewHelperTest.php:57
‪TYPO3\CMS\Fluid\Tests\Functional\ViewHelpers\Asset\CssViewHelperTest\childNodeRenderingIsCorrect
‪childNodeRenderingIsCorrect(string $value, string $source, string $expectation)
Definition: CssViewHelperTest.php:125
‪TYPO3\CMS\Fluid\Tests\Functional\ViewHelpers\Asset\CssViewHelperTest
Definition: CssViewHelperTest.php:28
‪TYPO3\CMS\Fluid\View\TemplateView
Definition: TemplateView.php:22
‪TYPO3\CMS\Fluid\Tests\Functional\ViewHelpers\Asset
Definition: CssViewHelperTest.php:18
‪TYPO3\CMS\Fluid\Tests\Functional\ViewHelpers\Asset\CssViewHelperTest\sourceDataProvider
‪static sourceDataProvider()
Definition: CssViewHelperTest.php:31
‪TYPO3\CMS\Fluid\Tests\Functional\ViewHelpers\Asset\CssViewHelperTest\$initializeDatabase
‪bool $initializeDatabase
Definition: CssViewHelperTest.php:29
‪TYPO3\CMS\Fluid\Tests\Functional\ViewHelpers\Asset\CssViewHelperTest\sourceStringIsNotHtmlEncodedBeforePassedToAssetCollector
‪sourceStringIsNotHtmlEncodedBeforePassedToAssetCollector(string $href)
Definition: CssViewHelperTest.php:44
‪TYPO3\CMS\Fluid\Tests\Functional\ViewHelpers\Asset\CssViewHelperTest\childNodeRenderingIsCorrectDataProvider
‪static childNodeRenderingIsCorrectDataProvider()
Definition: CssViewHelperTest.php:69
‪TYPO3\CMS\Fluid\Core\Rendering\RenderingContextFactory
Definition: RenderingContextFactory.php:51