‪TYPO3CMS  ‪main
ScriptViewHelperTest.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;
25 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
26 
27 final class ‪ScriptViewHelperTest extends FunctionalTestCase
28 {
29  protected bool ‪$initializeDatabase = false;
30 
31  public static function ‪sourceDataProvider(): array
32  {
33  return [
34  'fileadmin reference' => ['fileadmin/JavaScript/foo.js'],
35  'EXT: reference' => ['EXT:core/Resources/Public/JavaScript/foo.js'],
36  'external reference' => ['https://typo3.com/foo.js'],
37  'external reference with 1 parameter' => ['https://typo3.com/foo.js?foo=bar'],
38  'external reference with 2 parameters' => ['https://typo3.com/foo.js?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.script identifier="test" src="' . $src . '" priority="0"/>');
48 
49  (new ‪TemplateView($context))->render();
50 
51  $collectedJavaScripts = $this->get(AssetCollector::class)->getJavaScripts();
52  self::assertSame($src, $collectedJavaScripts['test']['source']);
53  self::assertSame([], $collectedJavaScripts['test']['attributes']);
54  }
55 
56  #[Test]
58  {
59  $context = $this->get(RenderingContextFactory::class)->create();
60  $context->getTemplatePaths()->setTemplateSource('<f:asset.script identifier="test" src="my.js" async="1" defer="1" nomodule="1" priority="0"/>');
61 
62  (new ‪TemplateView($context))->render();
63 
64  $collectedJavaScripts = $this->get(AssetCollector::class)->getJavaScripts();
65  self::assertSame($collectedJavaScripts['test']['source'], 'my.js');
66  self::assertSame($collectedJavaScripts['test']['attributes'], ['async' => 'async', 'defer' => 'defer', 'nomodule' => 'nomodule']);
67  }
68 }
‪TYPO3\CMS\Core\Page\AssetCollector
Definition: AssetCollector.php:42
‪TYPO3\CMS\Fluid\Tests\Functional\ViewHelpers\Asset\ScriptViewHelperTest\sourceDataProvider
‪static sourceDataProvider()
Definition: ScriptViewHelperTest.php:31
‪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\ScriptViewHelperTest\$initializeDatabase
‪bool $initializeDatabase
Definition: ScriptViewHelperTest.php:29
‪TYPO3\CMS\Fluid\Tests\Functional\ViewHelpers\Asset\ScriptViewHelperTest\sourceStringIsNotHtmlEncodedBeforePassedToAssetCollector
‪sourceStringIsNotHtmlEncodedBeforePassedToAssetCollector(string $src)
Definition: ScriptViewHelperTest.php:44
‪TYPO3\CMS\Fluid\Tests\Functional\ViewHelpers\Asset\ScriptViewHelperTest\booleanAttributesAreProperlyConverted
‪booleanAttributesAreProperlyConverted()
Definition: ScriptViewHelperTest.php:57
‪TYPO3\CMS\Fluid\Core\Rendering\RenderingContextFactory
Definition: RenderingContextFactory.php:51
‪TYPO3\CMS\Fluid\Tests\Functional\ViewHelpers\Asset\ScriptViewHelperTest
Definition: ScriptViewHelperTest.php:28