[TASK] Add FileCollectionRegistry
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Resource / Collection / FileCollectionRegistryTest.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Unit\Resource\Collection;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2013 - Frans Saris <franssaris@gmail.com>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 * A copy is found in the text file GPL.txt and important notices to the license
19 * from the author is found in LICENSE.txt distributed with these scripts.
20 *
21 *
22 * This script is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * This copyright notice MUST APPEAR in all copies of the script!
28 ***************************************************************/
29
30 /**
31 * Test cases for FileCollectionRegistry
32 */
33 class FileCollectionRegistryTest extends \TYPO3\CMS\Core\Tests\Unit\Resource\BaseTestCase {
34
35 /**
36 * @var \TYPO3\CMS\Core\Resource\Collection\FileCollectionRegistry
37 */
38 protected $testSubject;
39
40 public function setUp() {
41 $this->initializeTestSubject();
42 }
43
44 protected function initializeTestSubject() {
45 $this->testSubject = new \TYPO3\CMS\Core\Resource\Collection\FileCollectionRegistry();
46 }
47
48 /**
49 * @test
50 */
51 public function registeredFileCollectionClassesCanBeRetrieved() {
52 $className = get_class($this->getMockForAbstractClass('TYPO3\\CMS\\Core\\Resource\\Collection\\AbstractFileCollection'));
53 $this->testSubject->registerFileCollectionClass($className, 'foobar');
54 $returnedClassName = $this->testSubject->getFileCollectionClass('foobar');
55 $this->assertEquals($className, $returnedClassName);
56 }
57
58 /**
59 * @test
60 * @expectedException InvalidArgumentException
61 * @expectedExceptionCode 1391295613
62 */
63 public function registerFileCollectionClassThrowsExceptionIfClassDoesNotExist() {
64 $this->testSubject->registerFileCollectionClass(uniqid(), uniqid());
65 }
66
67 /**
68 * @test
69 * @expectedException InvalidArgumentException
70 * @expectedExceptionCode 1391295611
71 */
72 public function registerFileCollectionClassThrowsExceptionIfTypeIsTooLong() {
73 $className = get_class($this->getMockForAbstractClass('TYPO3\\CMS\\Core\\Resource\\Collection\\AbstractFileCollection'));
74 $type = str_pad('', 40);
75 $this->testSubject->registerFileCollectionClass($className, $type);
76 }
77
78 /**
79 * @test
80 * @expectedException InvalidArgumentException
81 * @expectedExceptionCode 1391295643
82 */
83 public function registerFileCollectionClassThrowsExceptionIfTypeIsAlreadyRegistered() {
84 $className = get_class($this->getMockForAbstractClass('TYPO3\\CMS\\Core\\Resource\\Collection\\AbstractFileCollection'));
85 $className2 = get_class($this->getMockForAbstractClass('TYPO3\\CMS\\Core\\Resource\\Collection\\StaticFileCollection'));
86 $this->testSubject->registerFileCollectionClass($className, 'foobar');
87 $this->testSubject->registerFileCollectionClass($className2, 'foobar');
88 }
89
90 /**
91 * @test
92 */
93 public function registerFileCollectionClassOverridesExistingRegisteredFileCollectionClass() {
94 $className = get_class($this->getMockForAbstractClass('TYPO3\\CMS\\Core\\Resource\\Collection\\AbstractFileCollection'));
95 $className2 = get_class($this->getMockForAbstractClass('TYPO3\\CMS\\Core\\Resource\\Collection\\StaticFileCollection'));
96 $this->testSubject->registerFileCollectionClass($className, 'foobar');
97 $this->testSubject->registerFileCollectionClass($className2, 'foobar', TRUE);
98 }
99
100 /**
101 * @test
102 * @expectedException InvalidArgumentException
103 * @expectedExceptionCode 1391295644
104 */
105 public function getFileCollectionClassThrowsExceptionIfClassIsNotRegistered() {
106 $this->testSubject->getFileCollectionClass(uniqid());
107 }
108
109 /**
110 * @test
111 */
112 public function getFileCollectionClassAcceptsClassNameIfClassIsRegistered() {
113 $className = get_class($this->getMockForAbstractClass('TYPO3\\CMS\\Core\\Resource\\Collection\\AbstractFileCollection'));
114 $this->testSubject->registerFileCollectionClass($className, 'foobar');
115 $this->assertEquals($className, $this->testSubject->getFileCollectionClass('foobar'));
116 }
117
118 /**
119 * @test
120 */
121 public function fileCollectionRegistryIsInitializedWithPreconfiguredFileCollections() {
122 $className = get_class($this->getMockForAbstractClass('TYPO3\\CMS\\Core\\Resource\\Collection\\AbstractFileCollection'));
123 $type = uniqid();
124 $GLOBALS['TYPO3_CONF_VARS']['SYS']['fal']['registeredCollections'] = array(
125 $type => $className
126 );
127 $this->initializeTestSubject();
128 $this->assertEquals($className, $this->testSubject->getFileCollectionClass($type));
129 }
130
131 /**
132 * @test
133 */
134 public function fileCollectionExistsReturnsTrueForAllExistingFileCollections() {
135 $className = get_class($this->getMockForAbstractClass('TYPO3\\CMS\\Core\\Resource\\Collection\\AbstractFileCollection'));
136 $type = 'foo';
137 $GLOBALS['TYPO3_CONF_VARS']['SYS']['fal']['registeredCollections'] = array(
138 $type => $className
139 );
140 $this->initializeTestSubject();
141 $this->assertTrue($this->testSubject->fileCollectionTypeExists($type));
142 $this->assertFalse($this->testSubject->fileCollectionTypeExists('bar'));
143 }
144
145 /**
146 * @test
147 */
148 public function fileCollectionExistsReturnsFalseIfFileCollectionDoesNotExist() {
149 $GLOBALS['TYPO3_CONF_VARS']['SYS']['fal']['registeredFileCollections'] = array();
150 $this->initializeTestSubject();
151 $this->assertFalse($this->testSubject->fileCollectionTypeExists(uniqid()));
152 }
153
154 /**
155 * @test
156 */
157 public function addNewTypeToTCA() {
158
159 // Create a TCA fixture for sys_file_collection
160 $GLOBALS['TCA']['sys_file_collection'] = array(
161 'types' => array(
162 'typeB' => array('showitem' => 'fieldA, fieldB, fieldC;labelC;paletteC;specialC, fieldD'),
163 ),
164 'columns' => array(
165 'type' => array(
166 'config' => array(
167 'items' => array('Type A', 'typeA'),
168 'items' => array('Type B', 'typeB')
169 )
170 )
171 )
172 );
173
174 $type = 'my_type';
175 $label = 'The Label';
176
177 $this->testSubject->addTypeToTCA($type, $label, 'something');
178
179 // check type
180 $this->assertEquals('sys_language_uid;;;;1-1-1, l10n_parent, l10n_diffsource, title;;1, type, something', $GLOBALS['TCA']['sys_file_collection']['types']['my_type']['showitem']);
181
182 $indexOfNewType = count($GLOBALS['TCA']['sys_file_collection']['columns']['type']['config']['items']) - 1;
183
184 // check if columns.type.item exist
185 $this->assertEquals($type, $GLOBALS['TCA']['sys_file_collection']['columns']['type']['config']['items'][$indexOfNewType][1]);
186 $this->assertEquals($label, $GLOBALS['TCA']['sys_file_collection']['columns']['type']['config']['items'][$indexOfNewType][0]);
187 }
188 }