[TASK] Deprecate several stdWrap / cObj functions
[Packages/TYPO3.CMS.git] / typo3 / sysext / frontend / Tests / UnitDeprecated / ContentObject / ContentObjectRendererTest.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Frontend\Tests\UnitDeprecated\ContentObject;
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\Context\Context;
19 use TYPO3\CMS\Core\Core\Environment;
20 use TYPO3\CMS\Core\Log\Logger;
21 use TYPO3\CMS\Core\Package\PackageManager;
22 use TYPO3\CMS\Core\TypoScript\TemplateService;
23 use TYPO3\CMS\Core\Utility\GeneralUtility;
24 use TYPO3\CMS\Frontend\ContentObject\CaseContentObject;
25 use TYPO3\CMS\Frontend\ContentObject\ContentContentObject;
26 use TYPO3\CMS\Frontend\ContentObject\ContentObjectArrayContentObject;
27 use TYPO3\CMS\Frontend\ContentObject\ContentObjectArrayInternalContentObject;
28 use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
29 use TYPO3\CMS\Frontend\ContentObject\EditPanelContentObject;
30 use TYPO3\CMS\Frontend\ContentObject\FileContentObject;
31 use TYPO3\CMS\Frontend\ContentObject\FilesContentObject;
32 use TYPO3\CMS\Frontend\ContentObject\FluidTemplateContentObject;
33 use TYPO3\CMS\Frontend\ContentObject\HierarchicalMenuContentObject;
34 use TYPO3\CMS\Frontend\ContentObject\ImageContentObject;
35 use TYPO3\CMS\Frontend\ContentObject\ImageResourceContentObject;
36 use TYPO3\CMS\Frontend\ContentObject\LoadRegisterContentObject;
37 use TYPO3\CMS\Frontend\ContentObject\RecordsContentObject;
38 use TYPO3\CMS\Frontend\ContentObject\RestoreRegisterContentObject;
39 use TYPO3\CMS\Frontend\ContentObject\ScalableVectorGraphicsContentObject;
40 use TYPO3\CMS\Frontend\ContentObject\TemplateContentObject;
41 use TYPO3\CMS\Frontend\ContentObject\TextContentObject;
42 use TYPO3\CMS\Frontend\ContentObject\UserContentObject;
43 use TYPO3\CMS\Frontend\ContentObject\UserInternalContentObject;
44 use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
45 use TYPO3\CMS\Frontend\Page\PageRepository;
46 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
47
48 /**
49 * Test case
50 */
51 class ContentObjectRendererTest extends UnitTestCase
52 {
53 /**
54 * @var bool Reset singletons created by subject
55 */
56 protected $resetSingletonInstances = true;
57
58 /**
59 * @var \PHPUnit_Framework_MockObject_MockObject|ContentObjectRenderer
60 */
61 protected $subject;
62
63 /**
64 * @var \PHPUnit_Framework_MockObject_MockObject|TypoScriptFrontendController
65 */
66 protected $frontendControllerMock;
67
68 /**
69 * @var \PHPUnit_Framework_MockObject_MockObject|TemplateService
70 */
71 protected $templateServiceMock;
72
73 /**
74 * Default content object name -> class name map, shipped with TYPO3 CMS
75 *
76 * @var array
77 */
78 protected $contentObjectMap = [
79 'TEXT' => TextContentObject::class,
80 'CASE' => CaseContentObject::class,
81 'COBJ_ARRAY' => ContentObjectArrayContentObject::class,
82 'COA' => ContentObjectArrayContentObject::class,
83 'COA_INT' => ContentObjectArrayInternalContentObject::class,
84 'USER' => UserContentObject::class,
85 'USER_INT' => UserInternalContentObject::class,
86 'FILE' => FileContentObject::class,
87 'FILES' => FilesContentObject::class,
88 'IMAGE' => ImageContentObject::class,
89 'IMG_RESOURCE' => ImageResourceContentObject::class,
90 'CONTENT' => ContentContentObject::class,
91 'RECORDS' => RecordsContentObject::class,
92 'HMENU' => HierarchicalMenuContentObject::class,
93 'CASEFUNC' => CaseContentObject::class,
94 'LOAD_REGISTER' => LoadRegisterContentObject::class,
95 'RESTORE_REGISTER' => RestoreRegisterContentObject::class,
96 'TEMPLATE' => TemplateContentObject::class,
97 'FLUIDTEMPLATE' => FluidTemplateContentObject::class,
98 'SVG' => ScalableVectorGraphicsContentObject::class,
99 'EDITPANEL' => EditPanelContentObject::class
100 ];
101
102 /**
103 * Set up
104 */
105 protected function setUp(): void
106 {
107 $GLOBALS['SIM_ACCESS_TIME'] = 1534278180;
108 $packageManagerMock = $this->getMockBuilder(PackageManager::class)
109 ->disableOriginalConstructor()
110 ->getMock();
111 $this->templateServiceMock =
112 $this->getMockBuilder(TemplateService::class)
113 ->setConstructorArgs([null, $packageManagerMock])
114 ->setMethods(['linkData'])
115 ->getMock();
116 $pageRepositoryMock =
117 $this->getAccessibleMock(PageRepository::class, ['getRawRecord', 'getMountPointInfo']);
118 $this->frontendControllerMock =
119 $this->getAccessibleMock(
120 TypoScriptFrontendController::class,
121 ['sL'],
122 [],
123 '',
124 false
125 );
126 $this->frontendControllerMock->_set('context', GeneralUtility::makeInstance(Context::class));
127 $this->frontendControllerMock->tmpl = $this->templateServiceMock;
128 $this->frontendControllerMock->config = [];
129 $this->frontendControllerMock->page = [];
130 $this->frontendControllerMock->sys_page = $pageRepositoryMock;
131 $GLOBALS['TSFE'] = $this->frontendControllerMock;
132
133 $this->subject = $this->getAccessibleMock(
134 ContentObjectRenderer::class,
135 ['getResourceFactory', 'getEnvironmentVariable'],
136 [$this->frontendControllerMock]
137 );
138
139 $logger = $this->prophesize(Logger::class);
140 $this->subject->setLogger($logger->reveal());
141 $this->subject->setContentObjectClassMap($this->contentObjectMap);
142 $this->subject->start([], 'tt_content');
143 }
144
145 ////////////////////////////////////
146 // Test concerning link generation
147 ////////////////////////////////////
148
149 /**
150 * @test
151 */
152 public function filelinkCreatesCorrectUrlForFileWithUrlEncodedSpecialChars(): void
153 {
154 $fileNameAndPath = Environment::getPublicPath() . '/typo3temp/var/tests/phpunitJumpUrlTestFile with spaces & amps.txt';
155 file_put_contents($fileNameAndPath, 'Some test data');
156 $relativeFileNameAndPath = substr($fileNameAndPath, strlen(Environment::getPublicPath()) + 1);
157 $fileName = substr($fileNameAndPath, strlen(Environment::getPublicPath() . '/typo3temp/var/tests/'));
158
159 $expectedLink = str_replace('%2F', '/', rawurlencode($relativeFileNameAndPath));
160 $result = $this->subject->filelink($fileName, ['path' => 'typo3temp/var/tests/'], true);
161 $this->assertEquals('<a href="' . $expectedLink . '">' . $fileName . '</a>', $result);
162
163 GeneralUtility::unlink_tempfile($fileNameAndPath);
164 }
165
166 /**
167 * Check that stdWrap_addParams works properly.
168 *
169 * Show:
170 *
171 * - Delegates to method addParams.
172 * - Parameter 1 is $content.
173 * - Parameter 2 is $conf['addParams.'].
174 * - Returns the return value.
175 *
176 * @test
177 */
178 public function stdWrap_addParams(): void
179 {
180 $content = $this->getUniqueId('content');
181 $conf = [
182 'addParams' => $this->getUniqueId('not used'),
183 'addParams.' => [$this->getUniqueId('addParams.')],
184 ];
185 $return = $this->getUniqueId('return');
186 $subject = $this->getMockBuilder(ContentObjectRenderer::class)
187 ->setMethods(['addParams'])->getMock();
188 $subject
189 ->expects($this->once())
190 ->method('addParams')
191 ->with($content, $conf['addParams.'])
192 ->willReturn($return);
193 $this->assertSame(
194 $return,
195 $subject->stdWrap_addParams($content, $conf)
196 );
197 }
198
199 /**
200 * Check if stdWrap_filelink works properly.
201 *
202 * Show:
203 *
204 * - Delegates to method filelink.
205 * - Parameter 1 is $content.
206 * - Parameter 2 is $conf['filelink.'].
207 * - Returns the return value.
208 *
209 * @test
210 */
211 public function stdWrap_filelink(): void
212 {
213 $content = $this->getUniqueId('content');
214 $conf = [
215 'filelink' => $this->getUniqueId('not used'),
216 'filelink.' => [$this->getUniqueId('filelink.')],
217 ];
218 $subject = $this->getMockBuilder(ContentObjectRenderer::class)
219 ->setMethods(['filelink'])->getMock();
220 $subject->expects($this->once())->method('filelink')
221 ->with($content, $conf['filelink.'])->willReturn('return');
222 $this->assertSame(
223 'return',
224 $subject->stdWrap_filelink($content, $conf)
225 );
226 }
227
228 /**
229 * Check if stdWrap_filelist works properly.
230 *
231 * Show:
232 *
233 * - Delegates to method filelist.
234 * - Parameter is $conf['filelist'].
235 * - Returns the return value.
236 *
237 * @test
238 */
239 public function stdWrap_filelist(): void
240 {
241 $conf = [
242 'filelist' => $this->getUniqueId('filelist'),
243 'filelist.' => [$this->getUniqueId('not used')],
244 ];
245 $subject = $this->getMockBuilder(ContentObjectRenderer::class)
246 ->setMethods(['filelist'])->getMock();
247 $subject->expects($this->once())->method('filelist')
248 ->with($conf['filelist'])->willReturn('return');
249 $this->assertSame(
250 'return',
251 $subject->stdWrap_filelist('discard', $conf)
252 );
253 }
254 }