‪TYPO3CMS  ‪main
PaletteAndSingleContainerTest.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;
26 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
27 
28 final class ‪PaletteAndSingleContainerTest extends UnitTestCase
29 {
30  #[Test]
32  {
33  $nodeFactoryMock = $this->createMock(NodeFactory::class);
34  $singleFieldContainerMock = $this->createMock(SingleFieldContainer::class);
35  $singleFieldContainerReturn = [
36  'additionalHiddenFields' => [],
37  'additionalInlineLanguageLabelFiles' => [],
38  'stylesheetFiles' => [],
39  'javaScriptModules' => [],
40  'inlineData' => [],
41  'html' => 'aFieldRenderedHtml',
42  ];
43  $singleFieldContainerMock->expects(self::atLeastOnce())->method('render')->withAnyParameters()->willReturn($singleFieldContainerReturn);
44 
45  $labelReference = 'LLL:EXT:Resources/Private/Language/locallang.xlf:aLabel';
46  $input = [
47  'tableName' => 'aTable',
48  'recordTypeValue' => 'aType',
49  'processedTca' => [
50  'columns' => [
51  'aField' => [],
52  ],
53  'palettes' => [
54  'aPalette' => [
55  'showitem' => 'aField',
56  ],
57  ],
58  ],
59  'fieldsArray' => [
60  '--palette--;' . $labelReference . ';aPalette',
61  ],
62  ];
63 
64  $languageService = $this->createMock(LanguageService::class);
65  ‪$GLOBALS['LANG'] = $languageService;
66  $backendUserAuthentication = $this->createMock(BackendUserAuthentication::class);
67  $backendUserAuthentication->method('shallDisplayDebugInformation')->willReturn(true);
68  ‪$GLOBALS['BE_USER'] = $backendUserAuthentication;
69 
70  // Expect translation call to the label reference and empty description
71  $series = [
72  $labelReference,
73  '',
74  ];
75  $languageService->method('sL')->willReturnCallback(function (string $input) use (&$series): string {
76  self::assertSame(array_shift($series), $input);
77  return $input;
78  });
79 
80  $expectedChildDataArray = $input;
81  $expectedChildDataArray['renderType'] = 'singleFieldContainer';
82  $expectedChildDataArray['fieldName'] = 'aField';
83 
84  $nodeFactoryMock->method('create')->with($expectedChildDataArray)->willReturn($singleFieldContainerMock);
85 
86  $subject = new ‪PaletteAndSingleContainer();
87  $subject->injectNodeFactory($nodeFactoryMock);
88  $subject->setData($input);
89  $containerResult = $subject->render();
90 
91  // Expect label is in answer HTML
92  self::assertStringContainsString($labelReference, $containerResult['html']);
93  }
94 
95  #[Test]
97  {
98  $nodeFactoryMock = $this->createMock(NodeFactory::class);
99  $singleFieldContainerMock = $this->createMock(SingleFieldContainer::class);
100  $singleFieldContainerReturn = [
101  'additionalHiddenFields' => [],
102  'additionalInlineLanguageLabelFiles' => [],
103  'stylesheetFiles' => [],
104  'javaScriptModules' => [],
105  'inlineData' => [],
106  'html' => 'aFieldRenderedHtml',
107  ];
108  $singleFieldContainerMock->expects(self::atLeastOnce())->method('render')->withAnyParameters()->willReturn($singleFieldContainerReturn);
109 
110  $labelReference = 'LLL:EXT:Resources/Private/Language/locallang.xlf:aLabel';
111  $descriptionReference = 'LLL:EXT:Resources/Private/Language/locallang.xlf:aDescription';
112  $input = [
113  'tableName' => 'aTable',
114  'recordTypeValue' => 'aType',
115  'processedTca' => [
116  'columns' => [
117  'aField' => [],
118  ],
119  'palettes' => [
120  'aPalette' => [
121  'label' => $labelReference,
122  'description' => $descriptionReference,
123  'showitem' => 'aField',
124  ],
125  ],
126  ],
127  'fieldsArray' => [
128  '--palette--;;aPalette',
129  ],
130  ];
131 
132  $languageService = $this->createMock(LanguageService::class);
133  ‪$GLOBALS['LANG'] = $languageService;
134  $backendUserAuthentication = $this->createMock(BackendUserAuthentication::class);
135  $backendUserAuthentication->method('shallDisplayDebugInformation')->willReturn(true);
136  ‪$GLOBALS['BE_USER'] = $backendUserAuthentication;
137 
138  // Expect translation call to the label and description references
139  $series = [
140  $labelReference,
141  $descriptionReference,
142  ];
143  $languageService->method('sL')->willReturnCallback(function (string $input) use (&$series): string {
144  self::assertSame(array_shift($series), $input);
145  return $input;
146  });
147 
148  $expectedChildDataArray = $input;
149  $expectedChildDataArray['renderType'] = 'singleFieldContainer';
150  $expectedChildDataArray['fieldName'] = 'aField';
151 
152  $nodeFactoryMock->method('create')->with($expectedChildDataArray)->willReturn($singleFieldContainerMock);
153 
154  $subject = new ‪PaletteAndSingleContainer();
155  $subject->injectNodeFactory($nodeFactoryMock);
156  $subject->setData($input);
157  $containerResult = $subject->render();
158 
159  // Expect label and description are in answer HTML
160  self::assertStringContainsString($labelReference, $containerResult['html']);
161  self::assertStringContainsString($descriptionReference, $containerResult['html']);
162  }
163 
164  #[Test]
166  {
167  $nodeFactoryMock = $this->createMock(NodeFactory::class);
168  $singleFieldContainerMock = $this->createMock(SingleFieldContainer::class);
169  $singleFieldContainerReturn = [
170  'additionalHiddenFields' => [],
171  'additionalInlineLanguageLabelFiles' => [],
172  'stylesheetFiles' => [],
173  'javaScriptModules' => [],
174  'inlineData' => [],
175  'html' => 'aFieldRenderedHtml',
176  ];
177  $singleFieldContainerMock->expects(self::atLeastOnce())->method('render')->withAnyParameters()->willReturn($singleFieldContainerReturn);
178 
179  $labelReferenceFieldArray = 'LLL:EXT:Resources/Private/Language/locallang.xlf:aLabel';
180  $labelReferencePaletteArray = 'LLL:EXT:Resources/Private/Language/locallang.xlf:aLabelPalette';
181  $descriptionReferencePaletteArray = 'LLL:EXT:Resources/Private/Language/locallang.xlf:aDescriptionPalette';
182  $input = [
183  'tableName' => 'aTable',
184  'recordTypeValue' => 'aType',
185  'processedTca' => [
186  'columns' => [
187  'aField' => [],
188  ],
189  'palettes' => [
190  'aPalette' => [
191  'label' => $labelReferencePaletteArray,
192  'description' => $descriptionReferencePaletteArray,
193  'showitem' => 'aField',
194  ],
195  ],
196  ],
197  'fieldsArray' => [
198  '--palette--;' . $labelReferenceFieldArray . ';aPalette',
199  ],
200  ];
201 
202  $languageService = $this->createMock(LanguageService::class);
203  ‪$GLOBALS['LANG'] = $languageService;
204  $backendUserAuthentication = $this->createMock(BackendUserAuthentication::class);
205  $backendUserAuthentication->method('shallDisplayDebugInformation')->willReturn(true);
206  ‪$GLOBALS['BE_USER'] = $backendUserAuthentication;
207 
208  // Expect translation call to the label and description references
209  $series = [
210  $labelReferenceFieldArray,
211  $descriptionReferencePaletteArray,
212  ];
213  $languageService->method('sL')->willReturnCallback(function (string $input) use (&$series): string {
214  self::assertSame(array_shift($series), $input);
215  return $input;
216  });
217 
218  $expectedChildDataArray = $input;
219  $expectedChildDataArray['renderType'] = 'singleFieldContainer';
220  $expectedChildDataArray['fieldName'] = 'aField';
221 
222  $nodeFactoryMock->method('create')->with($expectedChildDataArray)->willReturn($singleFieldContainerMock);
223 
224  $subject = new ‪PaletteAndSingleContainer();
225  $subject->injectNodeFactory($nodeFactoryMock);
226  $subject->setData($input);
227  $containerResult = $subject->render();
228 
229  // Expect label and description are in answer HTML
230  self::assertStringContainsString($labelReferenceFieldArray, $containerResult['html']);
231  self::assertStringContainsString($descriptionReferencePaletteArray, $containerResult['html']);
232  }
233 }
‪TYPO3\CMS\Backend\Tests\Unit\Form\Container\PaletteAndSingleContainerTest\renderUsesPaletteLabelFromFieldArray
‪renderUsesPaletteLabelFromFieldArray()
Definition: PaletteAndSingleContainerTest.php:31
‪TYPO3\CMS\Backend\Tests\Unit\Form\Container
Definition: ListOfFieldsContainerTest.php:18
‪TYPO3\CMS\Backend\Tests\Unit\Form\Container\PaletteAndSingleContainerTest
Definition: PaletteAndSingleContainerTest.php:29
‪TYPO3\CMS\Backend\Tests\Unit\Form\Container\PaletteAndSingleContainerTest\renderUsesPaletteValuesFromPaletteArray
‪renderUsesPaletteValuesFromPaletteArray()
Definition: PaletteAndSingleContainerTest.php:96
‪TYPO3\CMS\Backend\Form\Container\SingleFieldContainer
Definition: SingleFieldContainer.php:35
‪TYPO3\CMS\Backend\Tests\Unit\Form\Container\PaletteAndSingleContainerTest\renderPrefersFieldArrayPaletteValuesOverPaletteValues
‪renderPrefersFieldArrayPaletteValuesOverPaletteValues()
Definition: PaletteAndSingleContainerTest.php:165
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:62
‪TYPO3\CMS\Backend\Form\NodeFactory
Definition: NodeFactory.php:40
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Localization\LanguageService
Definition: LanguageService.php:46
‪TYPO3\CMS\Backend\Form\Container\PaletteAndSingleContainer
Definition: PaletteAndSingleContainer.php:33