9f8e6dbb7e7b8944d7979d77bb4fec0f9d7f32c8
[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 unset($this->fileRepositoryMock);
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 }