‪TYPO3CMS  ‪main
ExportTest.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
18 use PHPUnit\Framework\Attributes\DataProvider;
19 use PHPUnit\Framework\Attributes\Test;
20 use PHPUnit\Framework\MockObject\MockObject;
25 use TYPO3\TestingFramework\Core\AccessibleObjectInterface;
26 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
27 
28 final class ‪ExportTest extends UnitTestCase
29 {
30  protected bool ‪$resetSingletonInstances = true;
31 
32  protected ‪Export&MockObject&AccessibleObjectInterface ‪$exportMock;
33 
34  protected function ‪setUp(): void
35  {
36  parent::setUp();
37  $this->exportMock = $this->getAccessibleMock(Export::class, null, [], '', false);
38  $this->exportMock->_set('compressionAvailable', true);
39  }
40 
41  #[DataProvider('setExportFileNameSanitizesFileNameProvider')]
42  #[Test]
43  public function ‪setExportFileNameSanitizesFileName(string $fileName, string $expected): void
44  {
45  $this->exportMock->setExportFileName($fileName);
46  $actual = $this->exportMock->getExportFileName();
47 
48  self::assertEquals($expected, $actual);
49  }
50 
51  public static function ‪setExportFileNameSanitizesFileNameProvider(): array
52  {
53  return [
54  [
55  'fileName' => 'my-export-file_20201012 äöüß!"§$%&/()²³¼½¬{[]};,:µ<>|.1',
56  'expected' => 'my-export-file_20201012.1',
57  ],
58  ];
59  }
60 
61  #[Test]
63  {
64  $this->exportMock->setPid(1);
65  $this->exportMock->setLevels(2);
66  $patternDateTime = '[0-9-_]{16}';
67  self::assertMatchesRegularExpression("/T3D_tree_PID1_L2_$patternDateTime.xml/", $this->exportMock->getOrGenerateExportFileNameWithFileExtension());
68  }
69 
70  #[Test]
72  {
73  $this->exportMock->setRecord(['page:1', 'tt_content:1']);
74  $patternDateTime = '[0-9-_]{16}';
75  self::assertMatchesRegularExpression("/T3D_recs_page_1-tt_conte_$patternDateTime.xml/", $this->exportMock->getOrGenerateExportFileNameWithFileExtension());
76  }
77 
78  #[Test]
80  {
81  $this->exportMock->setList(['sys_news:0', 'news:12']);
82  $patternDateTime = '[0-9-_]{16}';
83  self::assertMatchesRegularExpression("/T3D_list_sys_news_0-news_$patternDateTime.xml/", $this->exportMock->getOrGenerateExportFileNameWithFileExtension());
84  }
85 
87  {
88  return [
89  ['fileType' => ‪Export::FILETYPE_XML],
90  ['fileType' => ‪Export::FILETYPE_T3D],
91  ['fileType' => ‪Export::FILETYPE_T3DZ],
92  ];
93  }
94 
95  #[DataProvider('setExportFileTypeSucceedsWithSupportedFileTypeProvider')]
96  #[Test]
97  public function ‪setExportFileTypeSucceedsWithSupportedFileType(string $fileType): void
98  {
99  $this->exportMock->setExportFileType($fileType);
100  self::assertEquals($fileType, $this->exportMock->getExportFileType());
101  }
102 
103  #[Test]
105  {
106  $this->expectException(\Exception::class);
107  $this->exportMock->setExportFileType('json');
108  }
109 
111  {
112  return [
113  'Remove one typolink entry from relation' => [
114  'relations' => [
115  ['type' => 'db', 'itemArray' => [[
116  'id' => hexdec(substr(md5('fileRelation.png'), 0, 6)),
117  'table' => 'sys_file',
118  ]], 'softrefs' => ['keys' => ['typolink' => [
119  0 => ['subst' => ['type' => 'file', 'relFileName' => 'fileRelation.png']],
120  1 => ['subst' => ['type' => 'file', 'relFileName' => 'fileRelation2.png']],
121  ]]]],
122  ],
123  'expected' => [
124  ['type' => 'db', 'itemArray' => [[
125  'id' => hexdec(substr(md5('fileRelation.png'), 0, 6)),
126  'table' => 'sys_file',
127  ]], 'softrefs' => ['keys' => ['typolink' => [
128  1 => ['subst' => ['type' => 'file', 'relFileName' => 'fileRelation2.png']],
129  ]]]],
130  ],
131  ],
132  'Remove whole softrefs array from relation' => [
133  'relations' => [
134  ['type' => 'db', 'itemArray' => [[
135  'id' => hexdec(substr(md5('fileRelation2.png'), 0, 6)),
136  'table' => 'sys_file',
137  ]], 'softrefs' => ['keys' => ['typolink' => [
138  0 => ['subst' => ['type' => 'file', 'relFileName' => 'fileRelation2.png']],
139  ]]]],
140  ],
141  'expected' => [
142  ['type' => 'db', 'itemArray' => [[
143  'id' => hexdec(substr(md5('fileRelation2.png'), 0, 6)),
144  'table' => 'sys_file',
145  ]]],
146  ],
147  ],
148  ];
149  }
150 
151  #[DataProvider('removeRedundantSoftRefsInRelationsProcessesOriginalRelationsArrayDataProvider')]
152  #[Test]
153  public function ‪removeRedundantSoftRefsInRelationsProcessesOriginalRelationsArray(array $relations, array $expected): void
154  {
155  $resourceFactoryMock = $this->getAccessibleMock(
156  ResourceFactory::class,
157  ['retrieveFileOrFolderObject'],
158  [],
159  '',
160  false
161  );
162  $resourceFactoryMock->method('retrieveFileOrFolderObject')
163  ->willReturnCallback(function (string $input): ‪File {
164  $fakeFileUidDerivedFromFileName = hexdec(substr(md5($input), 0, 6));
165  $fileMock = $this->getAccessibleMock(
166  File::class,
167  null,
168  [],
169  '',
170  false
171  );
172  $fileMock->_set('properties', ['uid' => $fakeFileUidDerivedFromFileName]);
173  return $fileMock;
174  });
175  GeneralUtility::setSingletonInstance(ResourceFactory::class, $resourceFactoryMock);
176  self::assertEquals($expected, $this->exportMock->_call('removeRedundantSoftRefsInRelations', $relations));
177  }
178 
180  {
181  $oneDat = [
182  'files' => [
183  'e580c5887dcea669332e96e25900b20b' => [],
184  ],
185  'records' => [
186  'tt_content:8' => [
187  'data' => [],
188  'rels' => [
189  'pi_flexform' => [
190  'type' => 'flex',
191  'flexFormRels' => [
192  ],
193  ],
194  ],
195  ],
196  ],
197  ];
198 
199  $fullDat = [
200  'files' => [
201  'e580c5887dcea669332e96e25900b20b' => [],
202  ],
203  'records' => [
204  'tt_content:8' => [
205  'data' => [],
206  'rels' => [
207  'pi_flexform' => [
208  'type' => 'flex',
209  'flexFormRels' => [
210  'softrefs' => [
211  [
212  'keys' => [
213  [
214  [
215  'subst' => [
216  'type' => 'file',
217  'tokenID' => 'tokenID',
218  'relFileName' => 'relFileNameSoftrefs',
219  ],
220  ],
221  ],
222  ],
223  ],
224  ],
225  ],
226  'softrefs' => [
227  'keys' => [
228  [
229  [
230  'subst' => [
231  'type' => 'fileSoftrefs',
232  'tokenID' => 'tokenIDSoftrefs',
233  'relFileName' => 'relFileNameSoftrefsSoftrefs',
234  ],
235  ],
236  ],
237  ],
238  ],
239  ],
240  'tx_bootstrappackage_carousel_item' => [
241  'type' => 'db',
242  'itemArray' => [
243  0 => [
244  'id' => 2,
245  'table' => 'tx_bootstrappackage_carousel_item',
246  ],
247  ],
248  ],
249  ],
250  ],
251  ],
252  ];
253  $fullExpected = $fullDat;
254  $fullExpected['records']['tt_content:8']['rels']['pi_flexform']['flexFormRels']['softrefs'][0]['keys'][0][0]['file_ID'] = 'e580c5887dcea669332e96e25900b20b';
255 
256  return [
257  'Empty $this->dat' => ['dat' => [], 'expected' => []],
258  'Empty $this->dat[\'records\']' => ['dat' => ['records' => []], 'expected' => ['records' => []]],
259  'One record example' => ['dat' => $oneDat, 'expected' => $oneDat],
260  'Full example' => ['dat' => $fullDat, 'expected' => $fullExpected],
261  ];
262  }
263 
267  #[DataProvider('exportAddFilesFromRelationsSucceedsDataProvider')]
268  #[Test]
269  public function ‪exportAddFilesFromRelationsSucceeds(array $dat, array $expected): void
270  {
271  ‪$exportMock = $this->getAccessibleMock(
272  Export::class,
273  ['addError', 'exportAddFile', 'isSoftRefIncluded'],
274  [],
275  '',
276  false
277  );
278  ‪$exportMock->method('isSoftRefIncluded')->willReturn(true);
279 
280  ‪$exportMock->_set('dat', $dat);
281  ‪$exportMock->_call('exportAddFilesFromRelations');
282  self::assertEquals($expected, ‪$exportMock->_get('dat'));
283  }
284 }
‪TYPO3\CMS\Impexp\Tests\Unit
Definition: ExportTest.php:16
‪TYPO3\CMS\Impexp\Tests\Unit\ExportTest\setUp
‪setUp()
Definition: ExportTest.php:34
‪TYPO3\CMS\Impexp\Tests\Unit\ExportTest\exportAddFilesFromRelationsSucceeds
‪exportAddFilesFromRelationsSucceeds(array $dat, array $expected)
Definition: ExportTest.php:269
‪TYPO3\CMS\Impexp\Export\FILETYPE_XML
‪const FILETYPE_XML
Definition: Export.php:56
‪TYPO3\CMS\Impexp\Tests\Unit\ExportTest\exportAddFilesFromRelationsSucceedsDataProvider
‪static exportAddFilesFromRelationsSucceedsDataProvider()
Definition: ExportTest.php:179
‪TYPO3\CMS\Impexp\Tests\Unit\ExportTest\setExportFileTypeFailsWithUnsupportedFileType
‪setExportFileTypeFailsWithUnsupportedFileType()
Definition: ExportTest.php:104
‪TYPO3\CMS\Impexp\Tests\Unit\ExportTest\$exportMock
‪Export &MockObject &AccessibleObjectInterface $exportMock
Definition: ExportTest.php:32
‪TYPO3\CMS\Impexp\Tests\Unit\ExportTest\$resetSingletonInstances
‪bool $resetSingletonInstances
Definition: ExportTest.php:30
‪TYPO3\CMS\Impexp\Tests\Unit\ExportTest\setExportFileNameSanitizesFileName
‪setExportFileNameSanitizesFileName(string $fileName, string $expected)
Definition: ExportTest.php:43
‪TYPO3\CMS\Impexp\Tests\Unit\ExportTest\getOrGenerateExportFileNameWithFileExtensionConsidersLists
‪getOrGenerateExportFileNameWithFileExtensionConsidersLists()
Definition: ExportTest.php:79
‪TYPO3\CMS\Impexp\Export\FILETYPE_T3D
‪const FILETYPE_T3D
Definition: Export.php:57
‪TYPO3\CMS\Core\Resource\ResourceFactory
Definition: ResourceFactory.php:42
‪TYPO3\CMS\Core\Resource\File
Definition: File.php:26
‪TYPO3\CMS\Impexp\Tests\Unit\ExportTest\removeRedundantSoftRefsInRelationsProcessesOriginalRelationsArray
‪removeRedundantSoftRefsInRelationsProcessesOriginalRelationsArray(array $relations, array $expected)
Definition: ExportTest.php:153
‪TYPO3\CMS\Impexp\Tests\Unit\ExportTest\setExportFileTypeSucceedsWithSupportedFileType
‪setExportFileTypeSucceedsWithSupportedFileType(string $fileType)
Definition: ExportTest.php:97
‪TYPO3\CMS\Impexp\Tests\Unit\ExportTest\getOrGenerateExportFileNameWithFileExtensionConsidersPidAndLevels
‪getOrGenerateExportFileNameWithFileExtensionConsidersPidAndLevels()
Definition: ExportTest.php:62
‪TYPO3\CMS\Impexp\Tests\Unit\ExportTest
Definition: ExportTest.php:29
‪TYPO3\CMS\Impexp\Export\FILETYPE_T3DZ
‪const FILETYPE_T3DZ
Definition: Export.php:58
‪TYPO3\CMS\Impexp\Tests\Unit\ExportTest\getOrGenerateExportFileNameWithFileExtensionConsidersRecords
‪getOrGenerateExportFileNameWithFileExtensionConsidersRecords()
Definition: ExportTest.php:71
‪TYPO3\CMS\Impexp\Export
Definition: Export.php:52
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Impexp\Tests\Unit\ExportTest\setExportFileNameSanitizesFileNameProvider
‪static setExportFileNameSanitizesFileNameProvider()
Definition: ExportTest.php:51
‪TYPO3\CMS\Impexp\Tests\Unit\ExportTest\removeRedundantSoftRefsInRelationsProcessesOriginalRelationsArrayDataProvider
‪static removeRedundantSoftRefsInRelationsProcessesOriginalRelationsArrayDataProvider()
Definition: ExportTest.php:110
‪TYPO3\CMS\Impexp\Tests\Unit\ExportTest\setExportFileTypeSucceedsWithSupportedFileTypeProvider
‪static setExportFileTypeSucceedsWithSupportedFileTypeProvider()
Definition: ExportTest.php:86