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