[TASK] FrontendContentAdapterService processes record repeatedly
[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 * @var string
42 */
43 protected $accessibleFixtureName;
44
45 /**
46 * Saving the singletons
47 */
48 public function setUp() {
49 $this->accessibleFixtureName = $this->buildAccessibleProxy('TYPO3\\CMS\\Core\\Resource\\Service\\FrontendContentAdapterService');
50 $this->singletonInstances = \TYPO3\CMS\Core\Utility\GeneralUtility::getSingletonInstances();
51 $this->fileRepositoryMock = $this->getMock('TYPO3\\CMS\\Core\\Resource\\FileRepository');
52 \TYPO3\CMS\Core\Utility\GeneralUtility::setSingletonInstance('TYPO3\\CMS\\Core\\Resource\\FileRepository', $this->fileRepositoryMock);
53 }
54
55 /**
56 * Restoring the singletons
57 */
58 public function tearDown() {
59 call_user_func_array($this->accessibleFixtureName . '::_setStatic', array('migrationCache', array()));
60 \TYPO3\CMS\Core\Utility\GeneralUtility::resetSingletonInstances($this->singletonInstances);
61 unset($this->fileRepositoryMock);
62 }
63
64 /**
65 * @test
66 */
67 public function customTableIsNotConsidered() {
68 $dbRow = array(
69 'uid' => uniqid(),
70 );
71 /* @see \TYPO3\CMS\Core\Resource\Service\FrontendContentAdapterService::modifyDBRow */
72 $result = call_user_func_array($this->accessibleFixtureName . '::modifyDBRow', array(&$dbRow, uniqid('tx_testtable')));
73 $this->assertNull($result);
74 }
75
76 /**
77 * @test
78 */
79 public function recordWithoutUidIsNotConsidered() {
80 $dbRow = array();
81 /* @see \TYPO3\CMS\Core\Resource\Service\FrontendContentAdapterService::modifyDBRow */
82 $result = call_user_func_array($this->accessibleFixtureName . '::modifyDBRow', array(&$dbRow, 'tt_content'));
83 $this->assertNull($result);
84 }
85
86 /**
87 * @test
88 */
89 public function emptyRelationResetsLegacyFields() {
90 $this->fileRepositoryMock->expects($this->any())
91 ->method('findByRelation')
92 ->will($this->returnValue(array()));
93 $dbRow = array(
94 'uid' => uniqid(),
95 'CType' => 'image',
96 'image' => '1'
97 );
98
99 /* @see \TYPO3\CMS\Core\Resource\Service\FrontendContentAdapterService::modifyDBRow */
100 call_user_func_array($this->accessibleFixtureName . '::modifyDBRow', array(&$dbRow, 'tt_content'));
101 $this->assertEmpty($dbRow['image']);
102 }
103
104 /**
105 * @test
106 */
107 public function processedRecordsAreCached() {
108 // Asserting that this is only called once,
109 // since second call shall be delivered from cache
110 $this->fileRepositoryMock->expects($this->once())
111 ->method('findByRelation')
112 ->will($this->returnValue(array()));
113
114 $testUid = uniqid();
115
116 $dbRow = array(
117 'uid' => $testUid,
118 'CType' => 'image',
119 'image' => '1',
120 );
121
122 /* @see \TYPO3\CMS\Core\Resource\Service\FrontendContentAdapterService::modifyDBRow */
123 call_user_func_array($this->accessibleFixtureName . '::modifyDBRow', array(&$dbRow, 'tt_content'));
124
125 $dbRow = array(
126 'uid' => $testUid,
127 'CType' => 'image',
128 'image' => '1',
129 );
130
131 /* @see \TYPO3\CMS\Core\Resource\Service\FrontendContentAdapterService::modifyDBRow */
132 call_user_func_array($this->accessibleFixtureName . '::modifyDBRow', array(&$dbRow, 'tt_content'));
133 }
134
135 /**
136 * @test
137 */
138 public function imageFieldIsFilledWithPathOfImage() {
139 $fileReference = $this->getMock('TYPO3\\CMS\\Core\\Resource\\FileReference', array(), array(), '', FALSE);
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->fileRepositoryMock->expects($this->any())
147 ->method('findByRelation')
148 ->will($this->returnValue(array($fileReference)));
149 $dbRow = array(
150 'uid' => uniqid(),
151 'CType' => 'image',
152 'image' => '1'
153 );
154
155 /* @see \TYPO3\CMS\Core\Resource\Service\FrontendContentAdapterService::modifyDBRow */
156 call_user_func_array($this->accessibleFixtureName . '::modifyDBRow', array(&$dbRow, 'tt_content'));
157 $this->assertSame('../../path/to/file', $dbRow['image']);
158 }
159
160 public function contentRowsOfDifferentTypesDataProvider() {
161 $filePropertiesImage = array(
162 'title' => 'Image',
163 'description' => 'IMAGE DESCRIPTION',
164 );
165
166 $filePropertiesMedia = array(
167 'title' => 'Media',
168 'description' => 'MEDIA DESCRIPTION',
169 );
170
171 return array(
172 'Image Element' => array(
173 array(
174 'uid' => uniqid(),
175 'CType' => 'image',
176 'image' => '',
177 'media' => '',
178 ),
179 'IMAGE DESCRIPTION',
180 $filePropertiesImage
181 ),
182 'Textpic Element' => array(
183 array(
184 'uid' => uniqid(),
185 'CType' => 'textpic',
186 'image' => '',
187 'media' => '',
188 ),
189 'IMAGE DESCRIPTION',
190 $filePropertiesImage
191 ),
192 'Uploads Element' => array(
193 array(
194 'uid' => uniqid(),
195 'CType' => 'uploads',
196 'image' => '',
197 'media' => '',
198 ),
199 'MEDIA DESCRIPTION',
200 $filePropertiesMedia
201 ),
202 );
203 }
204
205 /**
206 * @test
207 * @dataProvider contentRowsOfDifferentTypesDataProvider
208 */
209 public function migrationOfLegacyFieldsIsOnlyDoneWhenRelationFieldIsVisibleInType($dbRow, $expectedCaption, $fileProperties) {
210 $fileReference = $this->getMock('TYPO3\\CMS\\Core\\Resource\\FileReference', array(), array(), '', FALSE);
211 $fileReference->expects($this->once())
212 ->method('getProperties')
213 ->will($this->returnValue($fileProperties));
214 $fileReference->expects($this->any())
215 ->method('getOriginalFile')
216 ->will($this->returnValue($this->getMock('TYPO3\\CMS\\Core\\Resource\\File', array(), array(), '', FALSE)));
217 $fileReference->expects($this->any())
218 ->method('getPublicUrl')
219 ->will($this->returnValue('path/to/file'));
220 $this->fileRepositoryMock->expects($this->any())
221 ->method('findByRelation')
222 ->will($this->returnValue(array($fileReference)));
223
224 /* @see \TYPO3\CMS\Core\Resource\Service\FrontendContentAdapterService::modifyDBRow */
225 call_user_func_array($this->accessibleFixtureName . '::modifyDBRow', array(&$dbRow, 'tt_content'));
226 $this->assertSame($expectedCaption, $dbRow['imagecaption']);
227 }
228
229 }