[BUGFIX] FollowUp to "Invalid localization overlay of cObject FILES"
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Resource / Service / FrontendContentAdapterServiceTest.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Unit\Resource\Service;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2013 Helmut Hummel <helmut.hummel@typo3.org>
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 3 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 *
19 * This script is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
26 /**
27 * Tests for the Frontend Content Adapter
28 */
29 class FrontendContentAdapterServiceTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
30 /**
31 /**
32 * @var array A backup of registered singleton instances
33 */
34 protected $singletonInstances = array();
35
36 /**
37 * @var \TYPO3\CMS\Frontend\Page\PageRepository|\PHPUnit_Framework_MockObject_MockObject
38 */
39 protected $pageRepositoryMock;
40
41 /**
42 * Saving the singletons
43 */
44 public function setUp() {
45 $this->pageRepositoryMock = $this->getMock('TYPO3\\CMS\\Frontend\\Page\\PageRepository');
46 $GLOBALS['TSFE'] = new \stdClass;
47 $GLOBALS['TSFE']->sys_page = $this->pageRepositoryMock;
48 }
49
50 /**
51 * @test
52 */
53 public function emptyRelationResetsLegacyFields() {
54 $this->pageRepositoryMock->expects($this->any())
55 ->method('getFileReferences')
56 ->will($this->returnValue(array()));
57 $dbRow = array(
58 'CType' => 'image',
59 'image' => '1'
60 );
61
62 \TYPO3\CMS\Core\Resource\Service\FrontendContentAdapterService::modifyDBRow($dbRow, 'tt_content');
63 $this->assertEmpty($dbRow['image']);
64 }
65
66 /**
67 * @test
68 */
69 public function imageFieldIsFilledWithPathOfImage() {
70 $fileReference = $this->getMock('TYPO3\\CMS\\Core\\Resource\\FileReference', array(), array(), '', FALSE);
71 $fileReference->expects($this->any())
72 ->method('getOriginalFile')
73 ->will($this->returnValue($this->getMock('TYPO3\\CMS\\Core\\Resource\\File', array(), array(), '', FALSE)));
74 $fileReference->expects($this->any())
75 ->method('getPublicUrl')
76 ->will($this->returnValue('path/to/file'));
77 $this->pageRepositoryMock->expects($this->any())
78 ->method('getFileReferences')
79 ->will($this->returnValue(array($fileReference)));
80 $dbRow = array(
81 'CType' => 'image',
82 'image' => '1'
83 );
84
85 \TYPO3\CMS\Core\Resource\Service\FrontendContentAdapterService::modifyDBRow($dbRow, 'tt_content');
86 $this->assertSame('../../path/to/file', $dbRow['image']);
87 }
88
89 public function conteRowsOfDifferentTypesDataProvider() {
90 $filePropertiesImage = array(
91 'title' => 'Image',
92 'description' => 'IMAGE DESCRIPTION',
93 );
94
95 $filePropertiesMedia = array(
96 'title' => 'Media',
97 'description' => 'MEDIA DESCRIPTION',
98 );
99
100 return array(
101 'Image Element' => array(
102 array(
103 'CType' => 'image',
104 'image' => '',
105 'media' => '',
106 ),
107 'IMAGE DESCRIPTION',
108 $filePropertiesImage
109 ),
110 'Textpic Element' => array(
111 array(
112 'CType' => 'textpic',
113 'image' => '',
114 'media' => '',
115 ),
116 'IMAGE DESCRIPTION',
117 $filePropertiesImage
118 ),
119 'Uploads Element' => array(
120 array(
121 'CType' => 'uploads',
122 'image' => '',
123 'media' => '',
124 ),
125 'MEDIA DESCRIPTION',
126 $filePropertiesMedia
127 ),
128 );
129 }
130
131 /**
132 * @test
133 * @dataProvider conteRowsOfDifferentTypesDataProvider
134 */
135 public function migrationOfLegacyFieldsIsOnlyDoneWhenRelationFieldIsVisibleInType($dbRow, $expectedCaption, $fileProperties) {
136 $fileReference = $this->getMock('TYPO3\\CMS\\Core\\Resource\\FileReference', array(), array(), '', FALSE);
137 $fileReference->expects($this->once())
138 ->method('getProperties')
139 ->will($this->returnValue($fileProperties));
140 $fileReference->expects($this->any())
141 ->method('getOriginalFile')
142 ->will($this->returnValue($this->getMock('TYPO3\\CMS\\Core\\Resource\\File', array(), array(), '', FALSE)));
143 $fileReference->expects($this->any())
144 ->method('getPublicUrl')
145 ->will($this->returnValue('path/to/file'));
146 $this->pageRepositoryMock->expects($this->any())
147 ->method('getFileReferences')
148 ->will($this->returnValue(array($fileReference)));
149
150 \TYPO3\CMS\Core\Resource\Service\FrontendContentAdapterService::modifyDBRow($dbRow, 'tt_content');
151 $this->assertSame($expectedCaption, $dbRow['imagecaption']);
152 }
153
154 /**
155 * @test
156 */
157 public function registerAdditionalTypeForMigrationAddsTypeToArray() {
158
159 $migrateFields = array(
160 'testtable' => array(
161 'testfield' => array(
162 'paths' => 'oldfield',
163 '__typeMatch' => array(
164 'typeField' => 'mytypefield',
165 'types' => array('mytype'),
166 )
167 )
168 ),
169 );
170
171 $expectedResult = array(
172 'testtable' => array(
173 'testfield' => array(
174 'paths' => 'oldfield',
175 '__typeMatch' => array(
176 'typeField' => 'mytypefield',
177 'types' => array('mytype', 'mytype2'),
178 )
179 )
180 ),
181 );
182
183 $frontendContentAdapterService = $this->getAccessibleFrontendContentAdapterServiceWithEmptyConfiguration();
184 $frontendContentAdapterService->_setStatic('migrateFields', $migrateFields);
185 $frontendContentAdapterService->registerAdditionalTypeForMigration('testtable', 'testfield', 'mytype2');
186 $this->assertEquals($expectedResult, $frontendContentAdapterService->_getStatic('migrateFields'));
187 }
188
189 /**
190 * @test
191 * @expectedException \RuntimeException
192 */
193 public function registerAdditionalTypeForMigrationThrowsExceptionIfNoConfigurationAvailable() {
194 $frontendContentAdapterService = $this->getAccessibleFrontendContentAdapterServiceWithEmptyConfiguration();
195 $frontendContentAdapterService->registerAdditionalTypeForMigration('testtable', 'testfield', 'mytype2');
196 }
197
198 /**
199 * @test
200 * @expectedException \RuntimeException
201 */
202 public function registerAdditionalTypeForMigrationThrowsExceptionIfNoTypeConfigurationAvailable() {
203
204 $migrateFields = array(
205 'testtable' => array(
206 'testfield' => array(
207 'paths' => 'oldfield',
208 )
209 ),
210 );
211
212 $frontendContentAdapterService = $this->getAccessibleFrontendContentAdapterServiceWithEmptyConfiguration();
213 $frontendContentAdapterService->_setStatic('migrateFields', $migrateFields);
214 $frontendContentAdapterService->registerAdditionalTypeForMigration('testtable', 'testfield', 'mytype2');
215 }
216
217 /**
218 * @test
219 * @dataProvider registerFieldForMigrationAddsCorrectConfigurationDataProvider
220 */
221 public function registerFieldForMigrationAddsCorrectConfiguration($expectedResult, $table, $field, $migrationFields, $oldFieldName, $typeField, $types) {
222 $frontendContentAdapterService = $this->getAccessibleFrontendContentAdapterServiceWithEmptyConfiguration();
223 $frontendContentAdapterService::registerFieldForMigration($table, $field, $migrationFields, $oldFieldName, $typeField, $types);
224 $newConfiguration = $frontendContentAdapterService->_getStatic('migrateFields');
225 $this->assertEquals($expectedResult, $newConfiguration);
226 }
227
228 /**
229 * Data provider for registerFieldForMigrationAddsCorrectConfiguration
230 *
231 * @return array
232 */
233 public function registerFieldForMigrationAddsCorrectConfigurationDataProvider() {
234 return array(
235 'table without type column' => array(
236 array(
237 'tablename' => array(
238 'newfield' => array(
239 'paths' => 'oldfield',
240 ),
241 ),
242 ),
243 'tablename',
244 'newfield',
245 'paths',
246 'oldfield',
247 NULL,
248 array()
249 ),
250 'table with type column' => array(
251 array(
252 'tablename' => array(
253 'newfield' => array(
254 'paths' => 'oldfield',
255 '__typeMatch' => array(
256 'typeField' => 'typecolumn',
257 'types' => array('firsttype'),
258 )
259 ),
260 ),
261 ),
262 'tablename',
263 'newfield',
264 'paths',
265 'oldfield',
266 'typecolumn',
267 array('firsttype'),
268 ),
269 );
270 }
271
272 /**
273 * @test
274 * @expectedException \InvalidArgumentException
275 */
276 public function registerFieldForMigrationThrowsExceptionForInvalidMigrationField() {
277 $frontendContentAdapterService = $this->getAccessibleFrontendContentAdapterServiceWithEmptyConfiguration();
278 $frontendContentAdapterService::registerFieldForMigration('table', 'field', 'invalidfield', 'oldfield');
279 }
280
281 /**
282 * Creates an accessible mock of the FrontendContentAdapterService class
283 * and sets the migrateFields property to an empty array.
284 *
285 * @return \TYPO3\CMS\Core\Resource\Service\FrontendContentAdapterService|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface
286 */
287 protected function getAccessibleFrontendContentAdapterServiceWithEmptyConfiguration() {
288 $frontendContentAdapterService = $this->getAccessibleMock('TYPO3\\CMS\\Core\\Resource\\Service\\FrontendContentAdapterService', array('dummy'));
289 $frontendContentAdapterService->_setStatic('migrateFields', array());
290 return $frontendContentAdapterService;
291 }
292 }