‪TYPO3CMS  ‪main
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 PHPUnit\Framework\Attributes\DataProvider;
21 use PHPUnit\Framework\Attributes\Test;
27 use TYPO3\CMS\Core\LinkHandling\TypoLinkCodecService;
35 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
36 
37 final class ‪DatabaseRecordLinkBuilderTest extends UnitTestCase
38 {
39  protected bool ‪$resetSingletonInstances = true;
40 
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 
96  #[DataProvider('attributesSetInRecordLinkOverwriteConfiguredAttributesDataProvider')]
97  #[Test]
98  public function ‪attributesSetInRecordLinkOverwriteConfiguredAttributes(string $parameterFromTypoScript, string $parameterFromDb, string $expectedParameter): void
99  {
100  $confFromDb = [
101  'parameter' => $parameterFromDb,
102  ];
103  $extractedLinkDetails = [
104  'identifier' => 'tx_news',
105  'uid' => '1',
106  'type' => 'record',
107  'typoLinkParameter' => 't3://record?identifier=tx_news&uid=1',
108  ];
109  $typoScriptConfig = [
110  'config.' => [
111  'recordLinks.' => [
112  'tx_news.' =>
113  [
114  'forceLink' => '0',
115  'typolink.' =>
116  [
117  'parameter' => $parameterFromTypoScript,
118  'additionalParams' => '&tx_news_pi1[news]={field:uid}',
119  'additionalParams.' =>
120  [
121  'insertData' => '1',
122  ],
123  ],
124  ],
125  ],
126  ],
127  ];
128  $pageTsConfig = [
129  'TCEMAIN.' =>
130  [
131  'linkHandler.' =>
132  [
133  'tx_news.' =>
134  [
135  'handler' => RecordLinkHandler::class,
136  'label' => 'News',
137  'configuration.' =>
138  [
139  'table' => 'tx_news_domain_model_news',
140  ],
141  'scanAfter' => 'page',
142  ],
143  ],
144  ],
145 
146  ];
147  $target = '';
148  $linkText = 'Test Link';
149 
150  $expectedConfiguration = [
151  'parameter' => $expectedParameter,
152  'additionalParams' => '&tx_news_pi1[news]={field:uid}',
153  'additionalParams.' => ['insertData' => '1'],
154  ];
155 
156  // Arrange
157  $frontendControllerMock = $this->createMock(TypoScriptFrontendController::class);
158  $pageRepositoryMock = $this->createMock(PageRepository::class);
159  $contentObjectRendererMock = $this->createMock(ContentObjectRenderer::class);
160  $frontendTypoScript = new ‪FrontendTypoScript(new ‪RootNode(), [], [], []);
161  $frontendTypoScript->setSetupArray($typoScriptConfig);
162  $request = (new ‪ServerRequest())->withAttribute('frontend.typoscript', $frontendTypoScript);
163  $contentObjectRendererMock->method('getRequest')->willReturn($request);
164  GeneralUtility::setSingletonInstance(Context::class, new ‪Context());
165  GeneralUtility::addInstance(PageRepository::class, $pageRepositoryMock);
166  GeneralUtility::addInstance(ContentObjectRenderer::class, $contentObjectRendererMock);
167  GeneralUtility::addInstance(TypoLinkCodecService::class, new TypoLinkCodecService(new ‪NoopEventDispatcher()));
168 
169  $pageRepositoryMock
170  ->method('checkRecord')
171  ->with('tx_news_domain_model_news', 1)
172  ->willReturn(
173  [
174  'uid' => '1',
175  ]
176  );
177 
178  $contentObjectRendererMock->expects(self::once())->method('start');
179  $contentObjectRendererMock->expects(self::once())->method('createLink');
180 
181  // Act
182  $databaseRecordLinkBuilder = $this->getAccessibleMock(DatabaseRecordLinkBuilder::class, ['getPageTsConfig'], [$contentObjectRendererMock, $frontendControllerMock]);
183  $databaseRecordLinkBuilder->method('getPageTsConfig')->willReturn($pageTsConfig);
184  try {
185  $databaseRecordLinkBuilder->build($extractedLinkDetails, $linkText, $target, $confFromDb);
186  } catch (‪UnableToLinkException) {
187  // Assert
188  $contentObjectRendererMock->expects(self::once())->method('typoLink')->with($linkText, $expectedConfiguration);
189  }
190  }
191 }
‪TYPO3\CMS\Core\Context\Context
Definition: Context.php:54
‪TYPO3\CMS\Core\Http\ServerRequest
Definition: ServerRequest.php:39
‪TYPO3\CMS\Core\EventDispatcher\NoopEventDispatcher
Definition: NoopEventDispatcher.php:29
‪TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController
Definition: TypoScriptFrontendController.php:58
‪TYPO3\CMS\Core\TypoScript\AST\Node\RootNode
Definition: RootNode.php:26
‪TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer
Definition: ContentObjectRenderer.php:102
‪TYPO3\CMS\Core\Domain\Repository\PageRepository
Definition: PageRepository.php:69
‪TYPO3\CMS\Core\TypoScript\FrontendTypoScript
Definition: FrontendTypoScript.php:30
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52