[BUGFIX] Use params set in db in recordLinkHandler
[Packages/TYPO3.CMS.git] / typo3 / sysext / frontend / Tests / TYPO3 / CMS / Frontend / Tests / Unit / Typolink / DatabaseRecordLinkBuilderTest.php
1 <?php
2 declare(strict_types = 1);
3
4 namespace TYPO3\CMS\Frontend\Tests\Unit\Typolink;
5
6 /*
7 * This file is part of the TYPO3 CMS project.
8 *
9 * It is free software; you can redistribute it and/or modify it under
10 * the terms of the GNU General Public License, either version 2
11 * of the License, or any later version.
12 *
13 * For the full copyright and license information, please read the
14 * LICENSE.txt file that was distributed with this source code.
15 *
16 * The TYPO3 project - inspiring people to share!
17 */
18
19 use Prophecy\Argument;
20 use TYPO3\CMS\Core\TypoScript\TemplateService;
21 use TYPO3\CMS\Core\Utility\GeneralUtility;
22 use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
23 use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
24 use TYPO3\CMS\Frontend\Page\PageRepository;
25 use TYPO3\CMS\Frontend\Typolink\DatabaseRecordLinkBuilder;
26 use TYPO3\CMS\Frontend\Typolink\UnableToLinkException;
27 use TYPO3\CMS\Recordlist\LinkHandler\RecordLinkHandler;
28 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
29
30 /**
31 * Tests for DatabaseRecordLinkBuilderTest
32 */
33 class DatabaseRecordLinkBuilderTest extends UnitTestCase
34 {
35
36 /**
37 * Dataprovider with different parameter configurations
38 *
39 * @return array
40 */
41 public function attributesSetInRecordLinkOverwriteConfiguredAttributesDataProvider(): array
42 {
43 return [
44 'attributes from db overwrite config' => [
45 '27 tsTarget tsClass tsTitle',
46 't3://record?identifier=tx_news&uid=1 dbTarget dbClass dbTitle',
47 '27 dbTarget dbClass dbTitle',
48 ],
49 'no attributes from db - config is taken' => [
50 '27 tsTarget tsClass tsTitle',
51 't3://record?identifier=tx_news&uid=1',
52 '27 tsTarget tsClass tsTitle',
53 ],
54 'mixed: target from db' => [
55 '27 tsTarget tsClass tsTitle',
56 't3://record?identifier=tx_news&uid=1 dbTarget',
57 '27 dbTarget tsClass tsTitle',
58 ],
59 'mixed: class from db' => [
60 '27 tsTarget tsClass tsTitle',
61 't3://record?identifier=tx_news&uid=1 - dbClass',
62 '27 tsTarget dbClass tsTitle',
63 ],
64 'mixed: title from db' => [
65 '27 tsTarget tsClass tsTitle',
66 't3://record?identifier=tx_news&uid=1 - - dbTitle',
67 '27 tsTarget tsClass dbTitle',
68 ],
69 'mixed: target and title from db' => [
70 '27 tsTarget tsClass tsTitle',
71 't3://record?identifier=tx_news&uid=1 dbTarget - dbTitle',
72 '27 dbTarget tsClass dbTitle',
73 ],
74 'mixed: target and title from db, no class set' => [
75 '27 tsTarget - tsTitle',
76 't3://record?identifier=tx_news&uid=1 dbTarget - dbTitle',
77 '27 dbTarget - dbTitle',
78 ],
79 'mixed: title from db, no config set' => [
80 '27',
81 't3://record?identifier=tx_news&uid=1 - - dbTitle',
82 '27 - - dbTitle',
83 ],
84 'no attributes configured' => [
85 '27',
86 't3://record?identifier=tx_news&uid=1',
87 '27',
88 ],
89
90 ];
91 }
92
93 /**
94 * @test
95 *
96 * Tests showing that values set in the link record directly will overwrite those configured
97 * in the default link handler configuration
98 *
99 * Note that the TypolinkCodecService is not mocked on purpose to get the full unit tested.
100 *
101 * @dataProvider attributesSetInRecordLinkOverwriteConfiguredAttributesDataProvider
102 * @param string $parameterFromTypoScript
103 * @param string $parameterFromDb
104 * @param string $expectedParameter
105 */
106 public function attributesSetInRecordLinkOverwriteConfiguredAttributes(string $parameterFromTypoScript, string $parameterFromDb, string $expectedParameter): void
107 {
108 $confFromDb = [
109 'parameter' => $parameterFromDb,
110 ];
111 $extractedLinkDetails = [
112 'identifier' => 'tx_news',
113 'uid' => '1',
114 'type' => 'record',
115 'typoLinkParameter' => 't3://record?identifier=tx_news&uid=1',
116 ];
117 $typoScriptConfig = [
118 'config.' => [
119 'recordLinks.' => [
120 'tx_news.' =>
121 [
122 'forceLink' => '0',
123 'typolink.' =>
124 [
125 'parameter' => $parameterFromTypoScript,
126 'additionalParams' => '&tx_news_pi1[news]={field:uid}',
127 'additionalParams.' =>
128 [
129 'insertData' => '1',
130 ],
131 ],
132 ],
133 ],
134 ],
135 ];
136 $pageTsConfig = [
137 'TCEMAIN.' =>
138 [
139 'linkHandler.' =>
140 [
141
142 'tx_news.' =>
143 [
144 'handler' => RecordLinkHandler::class,
145 'label' => 'News',
146 'configuration.' =>
147 [
148 'table' => 'tx_news_domain_model_news',
149 ],
150 'scanAfter' => 'page',
151 ],
152 ],
153 ],
154
155 ];
156 $target = '';
157 $linkText = 'Test Link';
158
159 $expectedConfiguration = [
160 'parameter' => $expectedParameter,
161 'additionalParams' => '&tx_news_pi1[news]={field:uid}',
162 'additionalParams.' => ['insertData' => '1'],
163 ];
164
165 // Arrange
166 $tsfe = $this->prophesize(TypoScriptFrontendController::class);
167 $templateService = $this->prophesize(TemplateService::class);
168 $pageRepository = $this->prophesize(PageRepository::class);
169 $cObj = $this->prophesize(ContentObjectRenderer::class);
170
171 $GLOBALS['TSFE'] = $tsfe->reveal();
172 $tsfe->tmpl = $templateService->reveal();
173 $tsfe->tmpl->setup = $typoScriptConfig;
174 $tsfe->sys_page = $pageRepository->reveal();
175 GeneralUtility::addInstance(ContentObjectRenderer::class, $cObj->reveal());
176
177 $pageRepository->checkRecord('tx_news_domain_model_news', 1)->willReturn(
178 [
179 'uid' => '1',
180 ]
181 );
182
183 $cObj->start(Argument::cetera())->shouldBeCalled();
184 $cObj->typoLink(Argument::cetera())->shouldBeCalled();
185
186 $tsfe->getPagesTSconfig()->willReturn($pageTsConfig);
187
188 // Act
189 $databaseRecordLinkBuilder = new DatabaseRecordLinkBuilder($cObj->reveal());
190 try {
191 $databaseRecordLinkBuilder->build($extractedLinkDetails, $linkText, $target, $confFromDb);
192 } catch (UnableToLinkException $exception) {
193 // Assert
194 $cObj->typoLink($linkText, $expectedConfiguration)->shouldHaveBeenCalled();
195 }
196 }
197 }