‪TYPO3CMS  ‪main
FileCollectionRegistryTest.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\Unit\UnitTestCase;
26 
27 final class ‪FileCollectionRegistryTest extends UnitTestCase
28 {
29  #[Test]
31  {
32  $className = TestingFileCollection::class;
33  $subject = new ‪FileCollectionRegistry();
34  $subject->registerFileCollectionClass($className, 'foobar');
35  $returnedClassName = $subject->getFileCollectionClass('foobar');
36  self::assertEquals($className, $returnedClassName);
37  }
38 
39  #[Test]
41  {
42  $this->expectException(\InvalidArgumentException::class);
43  $this->expectExceptionCode(1391295613);
44  $subject = new ‪FileCollectionRegistry();
45  $subject->registerFileCollectionClass(‪StringUtility::getUniqueId('class_'), substr(‪StringUtility::getUniqueId('type_'), 0, 30));
46  }
47 
48  #[Test]
50  {
51  $this->expectException(\InvalidArgumentException::class);
52  $this->expectExceptionCode(1391295611);
53  $subject = new ‪FileCollectionRegistry();
54  $className = TestingFileCollection::class;
55  $type = str_pad('', 40);
56  $subject->registerFileCollectionClass($className, $type);
57  }
58 
59  #[Test]
61  {
62  $this->expectException(\InvalidArgumentException::class);
63  $this->expectExceptionCode(1391295643);
64  $subject = new ‪FileCollectionRegistry();
65  $className = TestingFileCollection::class;
66  $className2 = OtherTestingFileCollection::class;
67  $subject->registerFileCollectionClass($className, 'foobar');
68  $subject->registerFileCollectionClass($className2, 'foobar');
69  }
70 
71  #[Test]
73  {
74  $className = TestingFileCollection::class;
75  $className2 = OtherTestingFileCollection::class;
76  $subject = new ‪FileCollectionRegistry();
77  $subject->registerFileCollectionClass($className, 'foobar');
78  $subject->registerFileCollectionClass($className2, 'foobar', true);
79  }
80 
81  #[Test]
83  {
84  $this->expectException(\InvalidArgumentException::class);
85  $this->expectExceptionCode(1391295644);
86  $subject = new ‪FileCollectionRegistry();
87  $subject->getFileCollectionClass(‪StringUtility::getUniqueId());
88  }
89 
90  #[Test]
92  {
93  $className = TestingFileCollection::class;
94  $subject = new ‪FileCollectionRegistry();
95  $subject->registerFileCollectionClass($className, 'foobar');
96  self::assertEquals($className, $subject->getFileCollectionClass('foobar'));
97  }
98 
99  #[Test]
101  {
102  $className = TestingFileCollection::class;
103  $type = substr(‪StringUtility::getUniqueId('type_'), 0, 30);
104  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['fal']['registeredCollections'] = [
105  $type => $className,
106  ];
107  $subject = new ‪FileCollectionRegistry();
108  self::assertEquals($className, $subject->getFileCollectionClass($type));
109  }
110 
111  #[Test]
113  {
114  $className = TestingFileCollection::class;
115  $type = 'foo';
116  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['fal']['registeredCollections'] = [
117  $type => $className,
118  ];
119  $subject = new ‪FileCollectionRegistry();
120  self::assertTrue($subject->fileCollectionTypeExists($type));
121  self::assertFalse($subject->fileCollectionTypeExists('bar'));
122  }
123 
124  #[Test]
126  {
127  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['fal']['registeredFileCollections'] = [];
128  $subject = new ‪FileCollectionRegistry();
129  self::assertFalse($subject->fileCollectionTypeExists(‪StringUtility::getUniqueId('name_')));
130  }
131 
132  #[Test]
133  public function ‪addNewTypeToTCA(): void
134  {
135  // Create a TCA fixture for sys_file_collection
136  ‪$GLOBALS['TCA']['sys_file_collection'] = [
137  'types' => [
138  'typeB' => ['showitem' => 'fieldA, fieldB, fieldC;labelC, --palette--;;paletteC, fieldD'],
139  ],
140  'columns' => [
141  'type' => [
142  'config' => [
143  'items' => [
144  ['label' => 'Type B', 'value' => 'typeB'],
145  ],
146  ],
147  ],
148  ],
149  ];
150 
151  $type = 'my_type';
152  $label = 'The Label';
153 
154  $subject = new ‪FileCollectionRegistry();
155  $subject->addTypeToTCA($type, $label, 'something');
156 
157  // Add another item, so that phpstan doesn't complain about non-existing array keys.
158  ‪$GLOBALS['TCA']['sys_file_collection']['columns']['type']['config']['items'][] = [
159  ['label' => 'Type C', 'value' => 'typeC'],
160  ];
161 
162  // check type
163  self::assertEquals('sys_language_uid, l10n_parent, l10n_diffsource, title, --palette--;;1, type, something', ‪$GLOBALS['TCA']['sys_file_collection']['types']['my_type']['showitem']);
164 
165  // check if columns.type.item exist
166  self::assertEquals($type, ‪$GLOBALS['TCA']['sys_file_collection']['columns']['type']['config']['items'][1]['value']);
167  self::assertEquals($label, ‪$GLOBALS['TCA']['sys_file_collection']['columns']['type']['config']['items'][1]['label']);
168  }
169 }
‪TYPO3\CMS\Core\Tests\Unit\Resource\Collection\FileCollectionRegistryTest\registerFileCollectionClassThrowsExceptionIfTypeIsTooLong
‪registerFileCollectionClassThrowsExceptionIfTypeIsTooLong()
Definition: FileCollectionRegistryTest.php:49
‪TYPO3\CMS\Core\Tests\Unit\Resource\Collection\FileCollectionRegistryTest\addNewTypeToTCA
‪addNewTypeToTCA()
Definition: FileCollectionRegistryTest.php:133
‪TYPO3\CMS\Core\Tests\Unit\Resource\Collection\FileCollectionRegistryTest\fileCollectionExistsReturnsTrueForAllExistingFileCollections
‪fileCollectionExistsReturnsTrueForAllExistingFileCollections()
Definition: FileCollectionRegistryTest.php:112
‪TYPO3\CMS\Core\Tests\Unit\Resource\Collection\FileCollectionRegistryTest\fileCollectionExistsReturnsFalseIfFileCollectionDoesNotExist
‪fileCollectionExistsReturnsFalseIfFileCollectionDoesNotExist()
Definition: FileCollectionRegistryTest.php:125
‪TYPO3\CMS\Core\Tests\Unit\Resource\Collection\FileCollectionRegistryTest\getFileCollectionClassAcceptsClassNameIfClassIsRegistered
‪getFileCollectionClassAcceptsClassNameIfClassIsRegistered()
Definition: FileCollectionRegistryTest.php:91
‪TYPO3\CMS\Core\Tests\Unit\Resource\Collection\FileCollectionRegistryTest\registerFileCollectionClassOverridesExistingRegisteredFileCollectionClass
‪registerFileCollectionClassOverridesExistingRegisteredFileCollectionClass()
Definition: FileCollectionRegistryTest.php:72
‪TYPO3\CMS\Core\Tests\Unit\Resource\Collection\FileCollectionRegistryTest\registerFileCollectionClassThrowsExceptionIfClassDoesNotExist
‪registerFileCollectionClassThrowsExceptionIfClassDoesNotExist()
Definition: FileCollectionRegistryTest.php:40
‪TYPO3\CMS\Core\Tests\Unit\Resource\Collection\FileCollectionRegistryTest
Definition: FileCollectionRegistryTest.php:28
‪TYPO3\CMS\Core\Tests\Unit\Resource\Collection\FileCollectionRegistryTest\getFileCollectionClassThrowsExceptionIfClassIsNotRegistered
‪getFileCollectionClassThrowsExceptionIfClassIsNotRegistered()
Definition: FileCollectionRegistryTest.php:82
‪TYPO3\CMS\Core\Tests\Unit\Resource\Collection\FileCollectionRegistryTest\registerFileCollectionClassThrowsExceptionIfTypeIsAlreadyRegistered
‪registerFileCollectionClassThrowsExceptionIfTypeIsAlreadyRegistered()
Definition: FileCollectionRegistryTest.php:60
‪TYPO3\CMS\Core\Resource\Collection\FileCollectionRegistry
Definition: FileCollectionRegistry.php:25
‪TYPO3\CMS\Core\Tests\Unit\Resource\Collection\FileCollectionRegistryTest\fileCollectionRegistryIsInitializedWithPreconfiguredFileCollections
‪fileCollectionRegistryIsInitializedWithPreconfiguredFileCollections()
Definition: FileCollectionRegistryTest.php:100
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Tests\Unit\Resource\Collection\FileCollectionRegistryTest\registeredFileCollectionClassesCanBeRetrieved
‪registeredFileCollectionClassesCanBeRetrieved()
Definition: FileCollectionRegistryTest.php:30
‪TYPO3\CMS\Core\Tests\Unit\Resource\Collection\Fixtures\TestingFileCollection
Definition: TestingFileCollection.php:26
‪TYPO3\CMS\Core\Utility\StringUtility
Definition: StringUtility.php:24
‪TYPO3\CMS\Core\Tests\Unit\Resource\Collection\Fixtures\OtherTestingFileCollection
Definition: OtherTestingFileCollection.php:28
‪TYPO3\CMS\Core\Utility\StringUtility\getUniqueId
‪static getUniqueId(string $prefix='')
Definition: StringUtility.php:57
‪TYPO3\CMS\Core\Tests\Unit\Resource\Collection
Definition: FileCollectionRegistryTest.php:18