5aa31eaed9aabbdba95a21389732aa84699a02ab
[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 * @var \PHPUnit_Framework_MockObject_MockObject
32 */
33 protected $fileRepositoryMock;
34
35 /**
36 * @var array A backup of registered singleton instances
37 */
38 protected $singletonInstances = array();
39
40 /**
41 * Saving the singletons
42 */
43 public function setUp() {
44 $this->singletonInstances = \TYPO3\CMS\Core\Utility\GeneralUtility::getSingletonInstances();
45 $this->fileRepositoryMock = $this->getMock('TYPO3\\CMS\\Core\\Resource\\FileRepository');
46 \TYPO3\CMS\Core\Utility\GeneralUtility::setSingletonInstance('TYPO3\\CMS\\Core\\Resource\\FileRepository', $this->fileRepositoryMock);
47 }
48
49 /**
50 * Restoring the singletons
51 */
52 public function tearDown() {
53 \TYPO3\CMS\Core\Utility\GeneralUtility::resetSingletonInstances($this->singletonInstances);
54 parent::tearDown();
55 }
56
57 /**
58 * @test
59 */
60 public function emptyRelationResetsLegacyFields() {
61 $this->fileRepositoryMock->expects($this->any())
62 ->method('findByRelation')
63 ->will($this->returnValue(array()));
64 $dbRow = array(
65 'CType' => 'image',
66 'image' => '1'
67 );
68
69 \TYPO3\CMS\Core\Resource\Service\FrontendContentAdapterService::modifyDBRow($dbRow, 'tt_content');
70 $this->assertEmpty($dbRow['image']);
71 }
72
73 /**
74 * @test
75 */
76 public function imageFieldIsFilledWithPathOfImage() {
77 $fileReference = $this->getMock('TYPO3\\CMS\\Core\\Resource\\FileReference', array(), array(), '', FALSE);
78 $fileReference->expects($this->any())
79 ->method('getOriginalFile')
80 ->will($this->returnValue($this->getMock('TYPO3\\CMS\\Core\\Resource\\File', array(), array(), '', FALSE)));
81 $fileReference->expects($this->any())
82 ->method('getPublicUrl')
83 ->will($this->returnValue('path/to/file'));
84 $this->fileRepositoryMock->expects($this->any())
85 ->method('findByRelation')
86 ->will($this->returnValue(array($fileReference)));
87 $dbRow = array(
88 'CType' => 'image',
89 'image' => '1'
90 );
91
92 \TYPO3\CMS\Core\Resource\Service\FrontendContentAdapterService::modifyDBRow($dbRow, 'tt_content');
93 $this->assertSame('../../path/to/file', $dbRow['image']);
94 }
95
96 public function conteRowsOfDifferentTypesDataProvider() {
97 $filePropertiesImage = array(
98 'title' => 'Image',
99 'description' => 'IMAGE DESCRIPTION',
100 );
101
102 $filePropertiesMedia = array(
103 'title' => 'Media',
104 'description' => 'MEDIA DESCRIPTION',
105 );
106
107 return array(
108 'Image Element' => array(
109 array(
110 'CType' => 'image',
111 'image' => '',
112 'media' => '',
113 ),
114 'IMAGE DESCRIPTION',
115 $filePropertiesImage
116 ),
117 'Textpic Element' => array(
118 array(
119 'CType' => 'textpic',
120 'image' => '',
121 'media' => '',
122 ),
123 'IMAGE DESCRIPTION',
124 $filePropertiesImage
125 ),
126 'Uploads Element' => array(
127 array(
128 'CType' => 'uploads',
129 'image' => '',
130 'media' => '',
131 ),
132 'MEDIA DESCRIPTION',
133 $filePropertiesMedia
134 ),
135 );
136 }
137
138 /**
139 * @test
140 * @dataProvider conteRowsOfDifferentTypesDataProvider
141 */
142 public function migrationOfLegacyFieldsIsOnlyDoneWhenRelationFieldIsVisibleInType($dbRow, $expectedCaption, $fileProperties) {
143 $fileReference = $this->getMock('TYPO3\\CMS\\Core\\Resource\\FileReference', array(), array(), '', FALSE);
144 $fileReference->expects($this->once())
145 ->method('getProperties')
146 ->will($this->returnValue($fileProperties));
147 $fileReference->expects($this->any())
148 ->method('getOriginalFile')
149 ->will($this->returnValue($this->getMock('TYPO3\\CMS\\Core\\Resource\\File', array(), array(), '', FALSE)));
150 $fileReference->expects($this->any())
151 ->method('getPublicUrl')
152 ->will($this->returnValue('path/to/file'));
153 $this->fileRepositoryMock->expects($this->any())
154 ->method('findByRelation')
155 ->will($this->returnValue(array($fileReference)));
156
157 \TYPO3\CMS\Core\Resource\Service\FrontendContentAdapterService::modifyDBRow($dbRow, 'tt_content');
158 $this->assertSame($expectedCaption, $dbRow['imagecaption']);
159 }
160
161 /**
162 * @test
163 */
164 public function registerAdditionalTypeForMigrationAddsTypeToArray() {
165
166 $migrateFields = array(
167 'testtable' => array(
168 'testfield' => array(
169 'paths' => 'oldfield',
170 '__typeMatch' => array(
171 'typeField' => 'mytypefield',
172 'types' => array('mytype'),
173 )
174 )
175 ),
176 );
177
178 $expectedResult = array(
179 'testtable' => array(
180 'testfield' => array(
181 'paths' => 'oldfield',
182 '__typeMatch' => array(
183 'typeField' => 'mytypefield',
184 'types' => array('mytype', 'mytype2'),
185 )
186 )
187 ),
188 );
189
190 $frontendContentAdapterService = $this->getAccessibleFrontendContentAdapterServiceWithEmptyConfiguration();
191 $frontendContentAdapterService->_setStatic('migrateFields', $migrateFields);
192 $frontendContentAdapterService->registerAdditionalTypeForMigration('testtable', 'testfield', 'mytype2');
193 $this->assertEquals($expectedResult, $frontendContentAdapterService->_getStatic('migrateFields'));
194 }
195
196 /**
197 * @test
198 * @expectedException \RuntimeException
199 */
200 public function registerAdditionalTypeForMigrationThrowsExceptionIfNoConfigurationAvailable() {
201 $frontendContentAdapterService = $this->getAccessibleFrontendContentAdapterServiceWithEmptyConfiguration();
202 $frontendContentAdapterService->registerAdditionalTypeForMigration('testtable', 'testfield', 'mytype2');
203 }
204
205 /**
206 * @test
207 * @expectedException \RuntimeException
208 */
209 public function registerAdditionalTypeForMigrationThrowsExceptionIfNoTypeConfigurationAvailable() {
210
211 $migrateFields = array(
212 'testtable' => array(
213 'testfield' => array(
214 'paths' => 'oldfield',
215 )
216 ),
217 );
218
219 $frontendContentAdapterService = $this->getAccessibleFrontendContentAdapterServiceWithEmptyConfiguration();
220 $frontendContentAdapterService->_setStatic('migrateFields', $migrateFields);
221 $frontendContentAdapterService->registerAdditionalTypeForMigration('testtable', 'testfield', 'mytype2');
222 }
223
224 /**
225 * @test
226 * @dataProvider registerFieldForMigrationAddsCorrectConfigurationDataProvider
227 */
228 public function registerFieldForMigrationAddsCorrectConfiguration($expectedResult, $table, $field, $migrationFields, $oldFieldName, $typeField, $types) {
229 $frontendContentAdapterService = $this->getAccessibleFrontendContentAdapterServiceWithEmptyConfiguration();
230 $frontendContentAdapterService::registerFieldForMigration($table, $field, $migrationFields, $oldFieldName, $typeField, $types);
231 $newConfiguration = $frontendContentAdapterService->_getStatic('migrateFields');
232 $this->assertEquals($expectedResult, $newConfiguration);
233 }
234
235 /**
236 * Data provider for registerFieldForMigrationAddsCorrectConfiguration
237 *
238 * @return array
239 */
240 public function registerFieldForMigrationAddsCorrectConfigurationDataProvider() {
241 return array(
242 'table without type column' => array(
243 array(
244 'tablename' => array(
245 'newfield' => array(
246 'paths' => 'oldfield',
247 ),
248 ),
249 ),
250 'tablename',
251 'newfield',
252 'paths',
253 'oldfield',
254 NULL,
255 array()
256 ),
257 'table with type column' => array(
258 array(
259 'tablename' => array(
260 'newfield' => array(
261 'paths' => 'oldfield',
262 '__typeMatch' => array(
263 'typeField' => 'typecolumn',
264 'types' => array('firsttype'),
265 )
266 ),
267 ),
268 ),
269 'tablename',
270 'newfield',
271 'paths',
272 'oldfield',
273 'typecolumn',
274 array('firsttype'),
275 ),
276 );
277 }
278
279 /**
280 * @test
281 * @expectedException \InvalidArgumentException
282 */
283 public function registerFieldForMigrationThrowsExceptionForInvalidMigrationField() {
284 $frontendContentAdapterService = $this->getAccessibleFrontendContentAdapterServiceWithEmptyConfiguration();
285 $frontendContentAdapterService::registerFieldForMigration('table', 'field', 'invalidfield', 'oldfield');
286 }
287
288 /**
289 * Creates an accessible mock of the FrontendContentAdapterService class
290 * and sets the migrateFields property to an empty array.
291 *
292 * @return \TYPO3\CMS\Core\Resource\Service\FrontendContentAdapterService|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface
293 */
294 protected function getAccessibleFrontendContentAdapterServiceWithEmptyConfiguration() {
295 $frontendContentAdapterService = $this->getAccessibleMock('TYPO3\\CMS\\Core\\Resource\\Service\\FrontendContentAdapterService', array('dummy'));
296 $frontendContentAdapterService->_setStatic('migrateFields', array());
297 return $frontendContentAdapterService;
298 }
299 }