‪TYPO3CMS  11.5
DatabaseRecordLinkBuilderTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
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 
19 
20 use Prophecy\Argument;
21 use Prophecy\PhpUnit\ProphecyTrait;
22 use Psr\Http\Message\ServerRequestInterface;
27 use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
32 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
33 
37 class ‪DatabaseRecordLinkBuilderTest extends UnitTestCase
38 {
39  use ProphecyTrait;
40 
47  {
48  return [
49  'attributes from db overwrite config' => [
50  '27 tsTarget tsClass tsTitle',
51  't3://record?identifier=tx_news&uid=1 dbTarget dbClass dbTitle',
52  '27 dbTarget dbClass dbTitle',
53  ],
54  'no attributes from db - config is taken' => [
55  '27 tsTarget tsClass tsTitle',
56  't3://record?identifier=tx_news&uid=1',
57  '27 tsTarget tsClass tsTitle',
58  ],
59  'mixed: target from db' => [
60  '27 tsTarget tsClass tsTitle',
61  't3://record?identifier=tx_news&uid=1 dbTarget',
62  '27 dbTarget tsClass tsTitle',
63  ],
64  'mixed: class from db' => [
65  '27 tsTarget tsClass tsTitle',
66  't3://record?identifier=tx_news&uid=1 - dbClass',
67  '27 tsTarget dbClass tsTitle',
68  ],
69  'mixed: title from db' => [
70  '27 tsTarget tsClass tsTitle',
71  't3://record?identifier=tx_news&uid=1 - - dbTitle',
72  '27 tsTarget tsClass dbTitle',
73  ],
74  'mixed: target and title from db' => [
75  '27 tsTarget tsClass tsTitle',
76  't3://record?identifier=tx_news&uid=1 dbTarget - dbTitle',
77  '27 dbTarget tsClass dbTitle',
78  ],
79  'mixed: target and title from db, no class set' => [
80  '27 tsTarget - tsTitle',
81  't3://record?identifier=tx_news&uid=1 dbTarget - dbTitle',
82  '27 dbTarget - dbTitle',
83  ],
84  'mixed: title from db, no config set' => [
85  '27',
86  't3://record?identifier=tx_news&uid=1 - - dbTitle',
87  '27 - - dbTitle',
88  ],
89  'no attributes configured' => [
90  '27',
91  't3://record?identifier=tx_news&uid=1',
92  '27',
93  ],
94 
95  ];
96  }
97 
111  public function ‪attributesSetInRecordLinkOverwriteConfiguredAttributes(string $parameterFromTypoScript, string $parameterFromDb, string $expectedParameter): void
112  {
113  $confFromDb = [
114  'parameter' => $parameterFromDb,
115  ];
116  $extractedLinkDetails = [
117  'identifier' => 'tx_news',
118  'uid' => '1',
119  'type' => 'record',
120  'typoLinkParameter' => 't3://record?identifier=tx_news&uid=1',
121  ];
122  $typoScriptConfig = [
123  'config.' => [
124  'recordLinks.' => [
125  'tx_news.' =>
126  [
127  'forceLink' => '0',
128  'typolink.' =>
129  [
130  'parameter' => $parameterFromTypoScript,
131  'additionalParams' => '&tx_news_pi1[news]={field:uid}',
132  'additionalParams.' =>
133  [
134  'insertData' => '1',
135  ],
136  ],
137  ],
138  ],
139  ],
140  ];
141  $pageTsConfig = [
142  'TCEMAIN.' =>
143  [
144  'linkHandler.' =>
145  [
146 
147  'tx_news.' =>
148  [
149  'handler' => RecordLinkHandler::class,
150  'label' => 'News',
151  'configuration.' =>
152  [
153  'table' => 'tx_news_domain_model_news',
154  ],
155  'scanAfter' => 'page',
156  ],
157  ],
158  ],
159 
160  ];
161  $target = '';
162  $linkText = 'Test Link';
163 
164  $expectedConfiguration = [
165  'parameter' => $expectedParameter,
166  'additionalParams' => '&tx_news_pi1[news]={field:uid}',
167  'additionalParams.' => ['insertData' => '1'],
168  ];
169 
170  // Arrange
171  $frontendControllerProphecy = $this->prophesize(TypoScriptFrontendController::class);
172  $context = new ‪Context();
173  $templateService = $this->prophesize(TemplateService::class);
174  $pageRepository = $this->prophesize(PageRepository::class);
175  $cObj = $this->prophesize(ContentObjectRenderer::class);
176  $cObj->getRequest()->willReturn($this->prophesize(ServerRequestInterface::class)->reveal());
177 
178  $frontendControllerProphecy->getContext()->willReturn($context);
179  $frontendController = $frontendControllerProphecy->reveal();
180  $frontendController->tmpl = $templateService->reveal();
181  $frontendController->tmpl->setup = $typoScriptConfig;
182  $frontendController->sys_page = $pageRepository->reveal();
183  GeneralUtility::addInstance(ContentObjectRenderer::class, $cObj->reveal());
184 
185  $pageRepository->checkRecord('tx_news_domain_model_news', 1)->willReturn(
186  [
187  'uid' => '1',
188  ]
189  );
190 
191  $cObj->start(Argument::cetera())->shouldBeCalled();
192  $cObj->typoLink(Argument::cetera())->shouldBeCalled();
193 
194  $frontendControllerProphecy->getPagesTSconfig()->willReturn($pageTsConfig);
195 
196  // Act
197  $databaseRecordLinkBuilder = new ‪DatabaseRecordLinkBuilder($cObj->reveal(), $frontendController);
198  try {
199  $databaseRecordLinkBuilder->build($extractedLinkDetails, $linkText, $target, $confFromDb);
200  } catch (‪UnableToLinkException $exception) {
201  // Assert
202  $cObj->typoLink($linkText, $expectedConfiguration)->shouldHaveBeenCalled();
203  }
204  }
205 }
‪TYPO3\CMS\Core\Context\Context
Definition: Context.php:53
‪TYPO3\CMS\Core\TypoScript\TemplateService
Definition: TemplateService.php:46
‪TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController
Definition: TypoScriptFrontendController.php:104
‪TYPO3\CMS\Core\Domain\Repository\PageRepository
Definition: PageRepository.php:53
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50