‪TYPO3CMS  ‪main
NewRecordViewHelperTest.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\Test;
25 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
26 use TYPO3Fluid\Fluid\View\TemplateView;
27 
28 final class ‪NewRecordViewHelperTest extends FunctionalTestCase
29 {
30  protected bool ‪$initializeDatabase = false;
31 
33 
34  public function ‪setUp(): void
35  {
36  parent::setUp();
37  $this->request = (new ‪ServerRequest('https://www.example.com/'))
38  ->withAttribute('applicationType', ‪SystemEnvironmentBuilder::REQUESTTYPE_BE)
39  ->withAttribute('normalizedParams', new ‪NormalizedParams([], [], '', ''));
40  }
41 
42  #[Test]
44  {
45  $context = $this->get(RenderingContextFactory::class)->create();
46  $context->setRequest($this->request);
47  $context->getViewHelperResolver()->addNamespace('be', 'TYPO3\\CMS\\Backend\\ViewHelpers');
48  $context->getTemplatePaths()->setTemplateSource('<be:link.newRecord table="a_table" pid="17">new record at a_table on page 17</be:link.newRecord>');
49  $result = urldecode((new TemplateView($context))->render());
50 
51  self::assertStringContainsString('/typo3/record/edit', $result);
52  self::assertStringContainsString('edit[a_table][17]=new', $result);
53  }
54 
55  #[Test]
56  public function ‪renderReturnsValidLinkForRoot(): void
57  {
58  $context = $this->get(RenderingContextFactory::class)->create();
59  $context->setRequest($this->request);
60  $context->getViewHelperResolver()->addNamespace('be', 'TYPO3\\CMS\\Backend\\ViewHelpers');
61  $context->getTemplatePaths()->setTemplateSource('<be:link.newRecord table="a_table">new record at a_table on root</be:link.newRecord>');
62  $result = urldecode((new TemplateView($context))->render());
63 
64  self::assertStringContainsString('/typo3/record/edit', $result);
65  self::assertStringContainsString('edit[a_table][0]=new', $result);
66  }
67 
68  #[Test]
70  {
71  $context = $this->get(RenderingContextFactory::class)->create();
72  $context->setRequest($this->request);
73  $context->getViewHelperResolver()->addNamespace('be', 'TYPO3\\CMS\\Backend\\ViewHelpers');
74  $context->getTemplatePaths()->setTemplateSource("{be:link.newRecord(table: 'b_table', pid:17)}");
75  $result = urldecode((new TemplateView($context))->render());
76 
77  self::assertStringContainsString('/typo3/record/edit', $result);
78  self::assertStringContainsString('edit[b_table][17]=new', $result);
79  }
80 
81  #[Test]
83  {
84  $context = $this->get(RenderingContextFactory::class)->create();
85  $context->getViewHelperResolver()->addNamespace('be', 'TYPO3\\CMS\\Backend\\ViewHelpers');
86  $context->getTemplatePaths()->setTemplateSource('<be:link.newRecord table="c_table" returnUrl="foo/bar" pid="17">new record at c_table</be:link.newRecord>');
87  $result = urldecode((new TemplateView($context))->render());
88 
89  self::assertStringContainsString('/typo3/record/edit', $result);
90  self::assertStringContainsString('edit[c_table][17]=new', $result);
91  self::assertStringContainsString('returnUrl=foo/bar', $result);
92  }
93 
94  #[Test]
95  public function ‪renderReturnsValidLinkWithPosition(): void
96  {
97  $context = $this->get(RenderingContextFactory::class)->create();
98  $context->setRequest($this->request);
99  $context->getViewHelperResolver()->addNamespace('be', 'TYPO3\\CMS\\Backend\\ViewHelpers');
100  $context->getTemplatePaths()->setTemplateSource('<be:link.newRecord uid="-11" table="c_table">new record at c_table after record with uid 11</be:link.newRecord>');
101  $result = urldecode((new TemplateView($context))->render());
102 
103  self::assertStringContainsString('/typo3/record/edit', $result);
104  self::assertStringContainsString('edit[c_table][-11]=new', $result);
105  }
106 
107  #[Test]
109  {
110  $context = $this->get(RenderingContextFactory::class)->create();
111  $context->setRequest($this->request);
112  $context->getViewHelperResolver()->addNamespace('be', 'TYPO3\\CMS\\Backend\\ViewHelpers');
113  $context->getTemplatePaths()->setTemplateSource('<be:link.newRecord table="c_table" defaultValues="{c_table: {c_field: \'c_value\'}}" pid="17">new record at c_table</be:link.newRecord>');
114  $result = urldecode((new TemplateView($context))->render());
115 
116  self::assertStringContainsString('/typo3/record/edit', $result);
117  self::assertStringContainsString('edit[c_table][17]=new', $result);
118  self::assertStringContainsString('defVals[c_table][c_field]=c_value', $result);
119  }
120 
121  #[Test]
123  {
124  $context = $this->get(RenderingContextFactory::class)->create();
125  $context->setRequest($this->request);
126  $context->getViewHelperResolver()->addNamespace('be', 'TYPO3\\CMS\\Backend\\ViewHelpers');
127  $context->getTemplatePaths()->setTemplateSource('<be:link.newRecord table="c_table" defaultValues="{c_table: {c_field: \'c_value\', c_field2: \'c_value2\'}}" pid="17">new record at c_table</be:link.newRecord>');
128  $result = urldecode((new TemplateView($context))->render());
129 
130  self::assertStringContainsString('/typo3/record/edit', $result);
131  self::assertStringContainsString('edit[c_table][17]=new', $result);
132  self::assertStringContainsString('defVals[c_table][c_field]=c_value&amp;defVals[c_table][c_field2]=c_value2', $result);
133  }
134 
135  #[Test]
137  {
138  $this->expectException(\InvalidArgumentException::class);
139  $this->expectExceptionCode(1526134901);
140 
141  $context = $this->get(RenderingContextFactory::class)->create();
142  $context->setRequest($this->request);
143  $context->getViewHelperResolver()->addNamespace('be', 'TYPO3\\CMS\\Backend\\ViewHelpers');
144  $context->getTemplatePaths()->setTemplateSource('<be:link.newRecord uid="42" table="c_table">if uid given, it must be negative</be:link.newRecord>');
145  (new TemplateView($context))->render();
146  }
147 
148  #[Test]
150  {
151  $this->expectException(\InvalidArgumentException::class);
152  $this->expectExceptionCode(1526129969);
153 
154  $context = $this->get(RenderingContextFactory::class)->create();
155  $context->setRequest($this->request);
156  $context->getViewHelperResolver()->addNamespace('be', 'TYPO3\\CMS\\Backend\\ViewHelpers');
157  $context->getTemplatePaths()->setTemplateSource('<be:link.newRecord uid="-42" pid="18" table="c_table">can\'t handle uid and pid together</be:link.newRecord>');
158  (new TemplateView($context))->render();
159  }
160 }
‪TYPO3\CMS\Core\Core\SystemEnvironmentBuilder
Definition: SystemEnvironmentBuilder.php:41
‪TYPO3\CMS\Core\Core\SystemEnvironmentBuilder\REQUESTTYPE_BE
‪const REQUESTTYPE_BE
Definition: SystemEnvironmentBuilder.php:45
‪TYPO3\CMS\Core\Http\ServerRequest
Definition: ServerRequest.php:39
‪TYPO3\CMS\Fluid\Core\Rendering\RenderingContextFactory
Definition: RenderingContextFactory.php:51
‪TYPO3\CMS\Core\Http\NormalizedParams
Definition: NormalizedParams.php:38