‪TYPO3CMS  ‪main
UuidElementTest.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;
26 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
27 
28 final class ‪UuidElementTest extends UnitTestCase
29 {
30  protected bool ‪$resetSingletonInstances = true;
31 
32  protected function ‪setUp(): void
33  {
34  parent::setUp();
35  ‪$GLOBALS['LANG'] = $this->createMock(LanguageService::class);
36  }
37 
38  #[Test]
40  {
41  $data = [
42  'tableName' => 'aTable',
43  'fieldName' => 'identifier',
44  'parameterArray' => [
45  'itemFormElName' => 'identifier',
46  'itemFormElValue' => '',
47  'fieldConf' => [
48  'label' => 'foo',
49  'config' => [
50  'type' => 'uuid',
51  'required' => true,
52  ],
53  ],
54  ],
55  ];
56 
57  $this->expectException(\RuntimeException::class);
58  $this->expectExceptionCode(1678895476);
59 
60  $subject = new ‪UuidElement($this->createMock(IconFactory::class));
61  $subject->setData($data);
62  $subject->render();
63  }
64 
65  #[Test]
66  public function ‪renderThrowsExceptionOnInvalidUuid(): void
67  {
68  $data = [
69  'tableName' => 'aTable',
70  'fieldName' => 'identifier',
71  'parameterArray' => [
72  'itemFormElName' => 'identifier',
73  'itemFormElValue' => '_-invalid-_',
74  'fieldConf' => [
75  'label' => 'foo',
76  'config' => [
77  'type' => 'uuid',
78  'required' => true,
79  ],
80  ],
81  ],
82  ];
83 
84  $this->expectException(\RuntimeException::class);
85  $this->expectExceptionCode(1678895476);
86 
87  $subject = new ‪UuidElement($this->createMock(IconFactory::class));
88  $subject->setData($data);
89  $subject->render();
90  }
91 
92  #[Test]
94  {
95  $uuid = 'b3190536-1431-453e-afbb-25b8c5022513';
96  $data = [
97  'tableName' => 'aTable',
98  'fieldName' => 'identifier',
99  'parameterArray' => [
100  'itemFormElName' => 'identifier',
101  'itemFormElValue' => $uuid,
102  'fieldConf' => [
103  'label' => 'foo',
104  'config' => [
105  'type' => 'uuid',
106  ],
107  ],
108  ],
109  ];
110 
111  $nodeFactoryMock = $this->createMock(NodeFactory::class);
112  $fieldInformationMock = $this->createMock(FieldInformation::class);
113  $fieldInformationMock->method('render')->willReturn(['html' => '']);
114  $nodeFactoryMock->method('create')->with(self::anything())->willReturn($fieldInformationMock);
115 
116  $subject = new ‪UuidElement($this->createMock(IconFactory::class));
117  $subject->injectNodeFactory($nodeFactoryMock);
118  $subject->setData($data);
119  $subject->render();
120  $result = $subject->render();
121 
122  self::assertEquals('@typo3/backend/copy-to-clipboard.js', $result['javaScriptModules'][0]->getName());
123  self::assertMatchesRegularExpression('/<typo3-copy-to-clipboard.*text="' . $uuid . '"/s', $result['html']);
124  self::assertMatchesRegularExpression('/<input.*value="' . $uuid . '".*id="formengine-uuid-/s', $result['html']);
125  }
126 
127  #[Test]
129  {
130  $uuid = 'b3190536-1431-453e-afbb-25b8c5022513';
131  $data = [
132  'tableName' => 'aTable',
133  'fieldName' => 'identifier',
134  'parameterArray' => [
135  'itemFormElName' => 'identifier',
136  'itemFormElValue' => $uuid,
137  'fieldConf' => [
138  'label' => 'foo',
139  'config' => [
140  'type' => 'uuid',
141  'enableCopyToClipboard' => false,
142  ],
143  ],
144  ],
145  ];
146 
147  $nodeFactoryMock = $this->createMock(NodeFactory::class);
148  $fieldInformationMock = $this->createMock(FieldInformation::class);
149  $fieldInformationMock->method('render')->willReturn(['html' => '']);
150  $nodeFactoryMock->method('create')->with(self::anything())->willReturn($fieldInformationMock);
151 
152  $subject = new ‪UuidElement($this->createMock(IconFactory::class));
153  $subject->injectNodeFactory($nodeFactoryMock);
154  $subject->setData($data);
155  $subject->render();
156  $result = $subject->render();
157 
158  self::assertEmpty($result['javaScriptModules']);
159  self::assertDoesNotMatchRegularExpression('/<typo3-copy-to-clipboard.*text="' . $uuid . '"/s', $result['html']);
160  self::assertMatchesRegularExpression('/<input.*value="' . $uuid . '".*id="formengine-uuid-/s', $result['html']);
161  }
162 }
‪TYPO3\CMS\Backend\Form\NodeExpansion\FieldInformation
Definition: FieldInformation.php:33
‪TYPO3\CMS\Backend\Tests\Unit\Form\Element\UuidElementTest\$resetSingletonInstances
‪bool $resetSingletonInstances
Definition: UuidElementTest.php:30
‪TYPO3\CMS\Backend\Tests\Unit\Form\Element\UuidElementTest
Definition: UuidElementTest.php:29
‪TYPO3\CMS\Backend\Tests\Unit\Form\Element\UuidElementTest\renderReturnsInputElementWithUuidAndCopyToClipboardButton
‪renderReturnsInputElementWithUuidAndCopyToClipboardButton()
Definition: UuidElementTest.php:93
‪TYPO3\CMS\Backend\Tests\Unit\Form\Element\UuidElementTest\renderReturnsInputElementWithUuidAndWithoutCopyToClipboardButton
‪renderReturnsInputElementWithUuidAndWithoutCopyToClipboardButton()
Definition: UuidElementTest.php:128
‪TYPO3\CMS\Core\Imaging\IconFactory
Definition: IconFactory.php:34
‪TYPO3\CMS\Backend\Tests\Unit\Form\Element\UuidElementTest\setUp
‪setUp()
Definition: UuidElementTest.php:32
‪TYPO3\CMS\Backend\Form\Element\UuidElement
Definition: UuidElement.php:32
‪TYPO3\CMS\Backend\Tests\Unit\Form\Element
Definition: AbstractFormElementTest.php:18
‪TYPO3\CMS\Backend\Form\NodeFactory
Definition: NodeFactory.php:40
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Localization\LanguageService
Definition: LanguageService.php:46
‪TYPO3\CMS\Backend\Tests\Unit\Form\Element\UuidElementTest\renderThrowsExceptionOnEmptyElementValue
‪renderThrowsExceptionOnEmptyElementValue()
Definition: UuidElementTest.php:39
‪TYPO3\CMS\Backend\Tests\Unit\Form\Element\UuidElementTest\renderThrowsExceptionOnInvalidUuid
‪renderThrowsExceptionOnInvalidUuid()
Definition: UuidElementTest.php:66