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