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