[BUGFIX] Fix inconsitency of file reference property getters
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Resource / FileReferenceTest.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Unit\Resource;
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 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 *
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
28 /**
29 * Testcase for the file class of the TYPO3 FAL
30 *
31 */
32 class FileReferenceTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
33
34 /**
35 * @var array A backup of registered singleton instances
36 */
37 protected $singletonInstances = array();
38
39 public function setUp() {
40 $this->singletonInstances = \TYPO3\CMS\Core\Utility\GeneralUtility::getSingletonInstances();
41 \TYPO3\CMS\Core\Utility\GeneralUtility::purgeInstances();
42 }
43
44 public function tearDown() {
45 \TYPO3\CMS\Core\Utility\GeneralUtility::resetSingletonInstances($this->singletonInstances);
46 }
47
48 /**
49 * @param array $fileReferenceProperties
50 * @param array $originalFileProperties
51 * @return \TYPO3\CMS\Core\Resource\FileReference|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface
52 */
53 protected function prepareFixture(array $fileReferenceProperties, array $originalFileProperties) {
54 $fixture = $this->getAccessibleMock('TYPO3\\CMS\\Core\\Resource\\FileReference', array('dummy'), array(), '', FALSE);
55 $originalFileMock = $this->getAccessibleMock('TYPO3\\CMS\\Core\\Resource\\File', array(), array(), '', FALSE);
56 $originalFileMock->expects($this->any())
57 ->method('getProperties')
58 ->will($this->returnValue($originalFileProperties)
59 );
60 $fixture->_set('originalFile', $originalFileMock);
61 $fixture->_set('propertiesOfFileReference', $fileReferenceProperties);
62
63 return $fixture;
64 }
65
66 /**
67 * @return array
68 */
69 public function propertiesDataProvider() {
70 return array(
71 'File properties correctly override file reference properties' => array(
72 array(
73 'title' => NULL,
74 'description' => 'fileReferenceDescription',
75 'alternative' => '',
76 ),
77 array(
78 'title' => 'fileTitle',
79 'description' => 'fileDescription',
80 'alternative' => 'fileAlternative',
81 'file_only_property' => 'fileOnlyPropertyValue',
82 ),
83 array(
84 'title' => 'fileTitle',
85 'description' => 'fileReferenceDescription',
86 'alternative' => '',
87 'file_only_property' => 'fileOnlyPropertyValue',
88 ),
89 )
90 );
91 }
92
93 /**
94 * @param array $fileReferenceProperties
95 * @param array $originalFileProperties
96 * @param array $expectedMergedProperties
97 * @test
98 * @dataProvider propertiesDataProvider
99 */
100 public function getPropertiesReturnsMergedPropertiesAndRespectsNullValues(array $fileReferenceProperties, array $originalFileProperties, array $expectedMergedProperties) {
101 $fixture = $this->prepareFixture($fileReferenceProperties, $originalFileProperties);
102 $actual = $fixture->getProperties();
103 $this->assertSame($expectedMergedProperties, $actual);
104 }
105
106 /**
107 * @param array $fileReferenceProperties
108 * @param array $originalFileProperties
109 * @param array $expectedMergedProperties
110 * @test
111 * @dataProvider propertiesDataProvider
112 */
113 public function hasPropertyReturnsTrueForAllMergedPropertyKeys($fileReferenceProperties, $originalFileProperties, $expectedMergedProperties) {
114 $fixture = $this->prepareFixture($fileReferenceProperties, $originalFileProperties);
115 foreach (array_keys($expectedMergedProperties) as $key) {
116 $this->assertTrue($fixture->hasProperty($key));
117 }
118 }
119
120 /**
121 * @param array $fileReferenceProperties
122 * @param array $originalFileProperties
123 * @param array $expectedMergedProperties
124 * @test
125 * @dataProvider propertiesDataProvider
126 */
127 public function getPropertyReturnsAllMergedPropertyKeys($fileReferenceProperties, $originalFileProperties, $expectedMergedProperties) {
128 $fixture = $this->prepareFixture($fileReferenceProperties, $originalFileProperties);
129 foreach ($expectedMergedProperties as $key => $expectedValue) {
130 $this->assertSame($expectedValue, $fixture->getProperty($key));
131 }
132 }
133
134 /**
135 * @param array $fileReferenceProperties
136 * @param array $originalFileProperties
137 * @param array $expectedMergedProperties
138 * @test
139 * @dataProvider propertiesDataProvider
140 * @expectedException \InvalidArgumentException
141 */
142 public function getPropertyThrowsExceptionForNotAvailableProperty($fileReferenceProperties, $originalFileProperties) {
143 $fixture = $this->prepareFixture($fileReferenceProperties, $originalFileProperties);
144 $fixture->getProperty(uniqid('nothingHere'));
145 }
146
147 /**
148 * @param array $fileReferenceProperties
149 * @param array $originalFileProperties
150 * @param array $expectedMergedProperties
151 * @test
152 * @dataProvider propertiesDataProvider
153 */
154 public function getPropertyDoesNotThrowExceptionForPropertyOnlyAvailableInOriginalFile($fileReferenceProperties, $originalFileProperties) {
155 $fixture = $this->prepareFixture($fileReferenceProperties, $originalFileProperties);
156 $this->assertSame($originalFileProperties['file_only_property'], $fixture->getProperty('file_only_property'));
157 }
158
159 /**
160 * @param array $fileReferenceProperties
161 * @param array $originalFileProperties
162 * @param array $expectedMergedProperties
163 * @test
164 * @dataProvider propertiesDataProvider
165 * @expectedException \InvalidArgumentException
166 */
167 public function getReferencePropertyThrowsExceptionForPropertyOnlyAvailableInOriginalFile($fileReferenceProperties, $originalFileProperties) {
168 $fixture = $this->prepareFixture($fileReferenceProperties, $originalFileProperties);
169 $fixture->getReferenceProperty('file_only_property');
170 }
171 }
172
173 ?>