‪TYPO3CMS  ‪main
JsonElementTest.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\Test;
29 use TYPO3\CMS\Core\Package\PackageManager;
32 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
33 
34 final class ‪JsonElementTest extends UnitTestCase
35 {
36  protected bool ‪$resetSingletonInstances = true;
37 
38  protected function ‪setUp(): void
39  {
40  parent::setUp();
41  ‪$GLOBALS['BE_USER'] = new ‪BackendUserAuthentication();
42  }
43 
44  #[Test]
46  {
47  $data = [
48  'parameterArray' => [
49  'itemFormElName' => 'config',
50  'itemFormElValue' => ['foo' => 'bar'],
51  'fieldConf' => [
52  'label' => 'foo',
53  'config' => [
54  'type' => 'json',
55  'enableCodeEditor' => false,
56  'placeholder' => 'placeholder',
57  ],
58  ],
59  ],
60  ];
61 
62  $nodeFactoryMock = $this->createMock(NodeFactory::class);
63  $fieldInformationMock = $this->createMock(FieldInformation::class);
64  $fieldInformationMock->method('render')->willReturn(['html' => '']);
65  $nodeFactoryMock->method('create')->with(self::anything())->willReturn($fieldInformationMock);
66 
67  $subject = new ‪JsonElement();
68  $subject->injectNodeFactory($nodeFactoryMock);
69  $subject->setData($data);
70  $result = $subject->render();
71 
72  self::assertEquals('@typo3/backend/form-engine/element/json-element.js', $result['javaScriptModules'][0]->getName());
73  self::assertStringContainsString('<typo3-formengine-element-json', $result['html']);
74  self::assertStringContainsString('placeholder="placeholder"', $result['html']);
75  self::assertStringContainsString('&quot;foo&quot;: &quot;bar&quot;', $result['html']);
76  }
77 
78  #[Test]
79  public function ‪renderReturnsJsonInCodeEditor(): void
80  {
81  $data = [
82  'tableName' => 'aTable',
83  'fieldName' => 'aField',
84  'parameterArray' => [
85  'itemFormElName' => 'config',
86  'itemFormElValue' => ['foo' => 'bar'],
87  'fieldConf' => [
88  'label' => 'foo',
89  'config' => [
90  'type' => 'json',
91  'placeholder' => 'placeholder',
92  ],
93  ],
94  ],
95  ];
96 
97  GeneralUtility::setSingletonInstance(PackageManager::class, $this->createMock(PackageManager::class));
98 
99  $cacheManagerMock = $this->createMock(CacheManager::class);
100  $cacheMock = $this->createMock(FrontendInterface::class);
101  $cacheManagerMock->method('getCache')->with('assets')->willReturn($cacheMock);
102  $cacheMock->method('get')->withAnyParameters()->willReturn([]);
103  GeneralUtility::setSingletonInstance(CacheManager::class, $cacheManagerMock);
104 
105  $modeRegistryMock = $this->createMock(ModeRegistry::class);
106  $modeRegistryMock->method('getDefaultMode')->willReturn(new ‪Mode(‪JavaScriptModuleInstruction::create('foo')));
107  GeneralUtility::setSingletonInstance(ModeRegistry::class, $modeRegistryMock);
108 
109  $nodeFactoryMock = $this->createMock(NodeFactory::class);
110  $fieldInformationMock = $this->createMock(FieldInformation::class);
111  $fieldInformationMock->method('render')->willReturn(['html' => '']);
112  $nodeFactoryMock->method('create')->with(self::anything())->willReturn($fieldInformationMock);
113 
114  $subject = new ‪JsonElement();
115  $subject->injectNodeFactory($nodeFactoryMock);
116  $subject->setData($data);
117  $result = $subject->render();
118 
119  self::assertEquals('@typo3/backend/code-editor/element/code-mirror-element.js', $result['javaScriptModules'][0]->getName());
120  self::assertStringContainsString('<typo3-t3editor-codemirror', $result['html']);
121  self::assertStringContainsString('placeholder="placeholder"', $result['html']);
122  self::assertStringContainsString('&quot;foo&quot;: &quot;bar&quot;', $result['html']);
123  }
124 }
‪TYPO3\CMS\Backend\Form\NodeExpansion\FieldInformation
Definition: FieldInformation.php:33
‪TYPO3\CMS\Backend\Form\Element\JsonElement
Definition: JsonElement.php:34
‪TYPO3\CMS\Core\Page\JavaScriptModuleInstruction\create
‪static create(string $name, string $exportName=null)
Definition: JavaScriptModuleInstruction.php:47
‪TYPO3\CMS\Core\Page\JavaScriptModuleInstruction
Definition: JavaScriptModuleInstruction.php:23
‪TYPO3\CMS\Backend\Tests\Unit\Form\Element\JsonElementTest\setUp
‪setUp()
Definition: JsonElementTest.php:38
‪TYPO3\CMS\Core\Cache\CacheManager
Definition: CacheManager.php:36
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:61
‪TYPO3\CMS\Filelist\Type\Mode
‪Mode
Definition: Mode.php:24
‪TYPO3\CMS\Backend\Tests\Unit\Form\Element
Definition: AbstractFormElementTest.php:18
‪TYPO3\CMS\Backend\Tests\Unit\Form\Element\JsonElementTest
Definition: JsonElementTest.php:35
‪TYPO3\CMS\Backend\Form\NodeFactory
Definition: NodeFactory.php:40
‪TYPO3\CMS\Core\Cache\Frontend\FrontendInterface
Definition: FrontendInterface.php:22
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Backend\CodeEditor\Registry\ModeRegistry
Definition: ModeRegistry.php:29
‪TYPO3\CMS\Backend\Tests\Unit\Form\Element\JsonElementTest\$resetSingletonInstances
‪bool $resetSingletonInstances
Definition: JsonElementTest.php:36
‪TYPO3\CMS\Backend\Tests\Unit\Form\Element\JsonElementTest\renderReturnsJsonInStandardTextarea
‪renderReturnsJsonInStandardTextarea()
Definition: JsonElementTest.php:45
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Backend\Tests\Unit\Form\Element\JsonElementTest\renderReturnsJsonInCodeEditor
‪renderReturnsJsonInCodeEditor()
Definition: JsonElementTest.php:79