[BUGFIX] Ensure pageUid of RedirectFinisher can be parsed correctly
[Packages/TYPO3.CMS.git] / typo3 / sysext / form / Tests / Unit / Domain / Finishers / RedirectFinisherTest.php
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
18 namespace TYPO3\CMS\Form\Tests\Unit\Domain\Finishers;
19
20 use Prophecy\Argument;
21 use TYPO3\CMS\Core\Http\Response;
22 use TYPO3\CMS\Core\Utility\GeneralUtility;
23 use TYPO3\CMS\Extbase\Mvc\Exception\StopActionException;
24 use TYPO3\CMS\Extbase\Mvc\Request;
25 use TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder;
26 use TYPO3\CMS\Extbase\Object\Exception;
27 use TYPO3\CMS\Extbase\Object\ObjectManager;
28 use TYPO3\CMS\Form\Domain\Finishers\FinisherContext;
29 use TYPO3\CMS\Form\Domain\Finishers\RedirectFinisher;
30 use TYPO3\CMS\Form\Domain\Runtime\FormRuntime;
31 use TYPO3\CMS\Form\Service\TranslationService;
32 use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
33 use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
34 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
35
36 /**
37 * Test case
38 */
39 class RedirectFinisherTest extends UnitTestCase
40 {
41 protected $resetSingletonInstances = true;
42
43 /**
44 * @test
45 * @dataProvider pageUidOptionForFinisherAcceptsVariousPageRepresentationsDataProvider
46 * @param string|int|null $pageUid
47 * @param int $expectedPage
48 * @throws Exception
49 */
50 public function pageUidOptionForFinisherAcceptsVariousPageRepresentations($pageUid, int $expectedPage): void
51 {
52 $uriPrefix = 'https://site.test/?id=';
53
54 $contentObjectRendererProphecy = $this->prophesize(ContentObjectRenderer::class);
55 $contentObjectRendererProphecy->typoLink_URL(Argument::type('array'))->will(function ($arguments) use ($uriPrefix) {
56 return $uriPrefix . $arguments[0]['parameter'];
57 });
58 $tsfeProphecy = $this->prophesize(TypoScriptFrontendController::class);
59 $tsfeProphecy->cObj = $contentObjectRendererProphecy->reveal();
60 $GLOBALS['TSFE'] = $tsfeProphecy->reveal();
61
62 $redirectFinisherMock = $this->getAccessibleMock(RedirectFinisher::class, null, [], '', false);
63 $redirectFinisherMock->_set('options', [
64 'pageUid' => $pageUid,
65 ]);
66
67 $formRuntimeProphecy = $this->prophesize(FormRuntime::class);
68 $formRuntimeProphecy->getRequest()->willReturn(new Request());
69 $formRuntimeProphecy->getResponse()->willReturn(new Response());
70
71 $finisherContextProphecy = $this->prophesize(FinisherContext::class);
72 $finisherContextProphecy->getFormRuntime()->willReturn($formRuntimeProphecy->reveal());
73 $finisherContextProphecy->cancel()->shouldBeCalledOnce();
74
75 $translationServiceProphecy = $this->prophesize(TranslationService::class);
76 $translationServiceProphecy->translateFinisherOption(Argument::cetera())->willReturnArgument(3);
77
78 $objectManagerProphecy = $this->prophesize(ObjectManager::class);
79 $objectManagerProphecy->get(UriBuilder::class)->willReturn(new UriBuilder());
80 $objectManagerProphecy->get(TranslationService::class)->willReturn($translationServiceProphecy->reveal());
81 GeneralUtility::setSingletonInstance(ObjectManager::class, $objectManagerProphecy->reveal());
82 $redirectFinisherMock->injectObjectManager($objectManagerProphecy->reveal());
83
84 try {
85 $redirectFinisherMock->execute($finisherContextProphecy->reveal());
86 self::fail('RedirectFinisher did not throw expected exception.');
87 } /** @noinspection PhpRedundantCatchClauseInspection */ catch (StopActionException $e) {
88 $response = $e->getResponse();
89 self::assertSame($uriPrefix . $expectedPage, $response->getHeader('Location')[0]);
90 }
91 }
92
93 public function pageUidOptionForFinisherAcceptsVariousPageRepresentationsDataProvider(): array
94 {
95 return [
96 'null' => [
97 null,
98 1,
99 ],
100 'no page' => [
101 '',
102 1,
103 ],
104 'page as integer' => [
105 3,
106 3,
107 ],
108 'page as string' => [
109 '3',
110 3,
111 ],
112 'page with table prefix' => [
113 'pages_3',
114 3,
115 ],
116 ];
117 }
118 }