[BUGFIX] Initialize language key in AbstractPlugin
[Packages/TYPO3.CMS.git] / typo3 / sysext / frontend / Tests / Unit / Plugin / AbstractPluginTest.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Frontend\Tests\Unit\Plugin;
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 use TYPO3\CMS\Core\Utility\GeneralUtility;
18 use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
19 use TYPO3\CMS\Frontend\ContentObject\TextContentObject;
20 use TYPO3\CMS\Frontend\Plugin\AbstractPlugin;
21 use TYPO3\CMS\Frontend\Tests\Unit\Fixtures\ResultBrowserPluginHook;
22 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
23
24 /**
25 * Test case
26 */
27 class AbstractPluginTest extends UnitTestCase
28 {
29 /**
30 * @var AbstractPlugin
31 */
32 protected $abstractPlugin;
33
34 /**
35 * @var array
36 */
37 protected $defaultPiVars;
38
39 /**
40 * @var bool
41 */
42 protected $resetSingletonInstances = true;
43
44 /**
45 * Sets up this testcase
46 */
47 protected function setUp()
48 {
49 parent::setUp();
50
51 // Allow objects until 100 levels deep when executing the stdWrap
52 $GLOBALS['TSFE'] = new \stdClass();
53 $GLOBALS['TSFE']->cObjectDepthCounter = 100;
54
55 $this->abstractPlugin = new AbstractPlugin();
56 $contentObjectRenderer = new ContentObjectRenderer();
57 $contentObjectRenderer->setContentObjectClassMap([
58 'TEXT' => TextContentObject::class,
59 ]);
60 $this->abstractPlugin->cObj = $contentObjectRenderer;
61 $this->defaultPiVars = $this->abstractPlugin->piVars;
62 }
63
64 /**
65 * Data provider for piSetPiVarDefaultsStdWrap
66 *
67 * @return array input-array with configuration and stdWrap, expected output-array in piVars
68 */
69 public function piSetPiVarDefaultsStdWrapProvider()
70 {
71 return [
72 'stdWrap on conf, non-recursive, stdWrap 1 level deep' => [
73 [
74 'abc' => 'DEF',
75 'abc.' => [
76 'stdWrap.' => [
77 'wrap' => 'test | test'
78 ],
79 ],
80 ],
81 [
82 'abc' => 'testDEFtest',
83 'pointer' => '',
84 'mode' => '',
85 'sword' => '',
86 'sort' => '',
87 ],
88 ],
89 'stdWrap on conf, non-recursive, stdWrap 2 levels deep' => [
90 [
91 'xyz.' => [
92 'stdWrap.' => [
93 'cObject' => 'TEXT',
94 'cObject.' => [
95 'data' => 'date:U',
96 'strftime' => '%Y',
97 ],
98 ],
99 ],
100 ],
101 [
102 'xyz' => date('Y'),
103 'pointer' => '',
104 'mode' => '',
105 'sword' => '',
106 'sort' => '',
107 ],
108 ],
109 'stdWrap on conf, recursive' => [
110 [
111 'abc.' => [
112 'def' => 'DEF',
113 'def.' => [
114 'ghi' => '123',
115 'stdWrap.' => [
116 'wrap' => 'test | test'
117 ],
118 ],
119 ],
120 ],
121 [
122 'abc.' => [
123 'def' => 'testDEFtest',
124 'def.' => [
125 'ghi' => '123',
126 ],
127 ],
128 'pointer' => '',
129 'mode' => '',
130 'sword' => '',
131 'sort' => '',
132 ],
133 ],
134 ];
135 }
136
137 /**
138 * @test
139 * @dataProvider piSetPiVarDefaultsStdWrapProvider
140 */
141 public function piSetPiVarDefaultsStdWrap($input, $expected)
142 {
143 $this->resetSingletonInstances = true;
144 $this->abstractPlugin->piVars = $this->defaultPiVars;
145
146 $this->abstractPlugin->conf['_DEFAULT_PI_VARS.'] = $input;
147 $this->abstractPlugin->pi_setPiVarDefaults();
148 $this->assertEquals($expected, $this->abstractPlugin->piVars);
149 }
150
151 /**
152 * Data provider for multiple registered result browser implementations
153 *
154 * @return array
155 */
156 public function registeredResultBrowserProvider()
157 {
158 return [
159 'Result browser returning false' => [
160 'className' => $this->getUniqueId('tx_coretest'),
161 'returnValue' => false,
162 'expected' => ''
163 ],
164 'Result browser returning null' => [
165 'className' => $this->getUniqueId('tx_coretest'),
166 'returnValue' => null,
167 'expected' => ''
168 ],
169 'Result browser returning whitespace string' => [
170 'className' => $this->getUniqueId('tx_coretest'),
171 'returnValue' => ' ',
172 'expected' => ''
173 ],
174 'Result browser returning HTML' => [
175 'className' => $this->getUniqueId('tx_coretest'),
176 'returnValue' => '<div><a href="index.php?id=1&pointer=1">1</a><a href="index.php?id=1&pointer=2">2</a><a href="index.php?id=1&pointer=3">3</a><a href="index.php?id=1&pointer=4">4</a></div>',
177 'expected' => '<div><a href="index.php?id=1&pointer=1">1</a><a href="index.php?id=1&pointer=2">2</a><a href="index.php?id=1&pointer=3">3</a><a href="index.php?id=1&pointer=4">4</a></div>'
178 ],
179 'Result browser returning a truthy integer as string' => [
180 'className' => $this->getUniqueId('tx_coretest'),
181 'returnValue' => '1',
182 'expected' => '1'
183 ],
184 'Result browser returning a falsy integer' => [
185 'className' => $this->getUniqueId('tx_coretest'),
186 'returnValue' => 0,
187 'expected' => ''
188 ],
189 'Result browser returning a truthy integer' => [
190 'className' => $this->getUniqueId('tx_coretest'),
191 'returnValue' => 1,
192 'expected' => ''
193 ],
194 'Result browser returning a positive integer' => [
195 'className' => $this->getUniqueId('tx_coretest'),
196 'returnValue' => 42,
197 'expected' => ''
198 ]
199 ];
200 }
201
202 /**
203 * @test
204 * @dataProvider registeredResultBrowserProvider
205 *
206 * @param string $className
207 * @param mixed $returnValue
208 * @param string $expected
209 */
210 public function registeredResultBrowsersAreUsed($className, $returnValue, $expected)
211 {
212 $resultBrowserHook = $this->getMockBuilder(ResultBrowserPluginHook::class)
213 ->setMockClassName($className)
214 ->setMethods(['pi_list_browseresults'])
215 ->disableOriginalConstructor()
216 ->getMock();
217
218 // Register hook mock object
219 GeneralUtility::addInstance($className, $resultBrowserHook);
220 $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS'][AbstractPlugin::class]['pi_list_browseresults'] = [$className];
221
222 $resultBrowserHook->expects($this->atLeastOnce())
223 ->method('pi_list_browseresults')
224 ->with(1, '', [], 'pointer', true, false, $this->abstractPlugin)
225 ->will($this->returnValue($returnValue));
226
227 $actualReturnValue = $this->abstractPlugin->pi_list_browseresults();
228
229 $this->assertSame($expected, $actualReturnValue);
230
231 unset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS'][AbstractPlugin::class]['pi_list_browseresults']);
232 }
233 }