[TASK] ext:install Protect some methods in FolderStructure
[Packages/TYPO3.CMS.git] / typo3 / sysext / install / Tests / Unit / FolderStructure / AbstractNodeTest.php
1 <?php
2 namespace TYPO3\CMS\Install\Tests\Unit\FolderStructure;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2013 Christian Kuhn <lolli@schwarzbu.ch>
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 * A copy is found in the textfile GPL.txt and important notices to the license
19 * from the author is found in LICENSE.txt distributed with these scripts.
20 *
21 *
22 * This script is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * This copyright notice MUST APPEAR in all copies of the script!
28 ***************************************************************/
29
30 /**
31 * Test case
32 */
33 class AbstractNodeTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
34
35 /**
36 * @var array Directories or files in typo3temp/ created during tests to delete afterwards
37 */
38 protected $testNodesToDelete = array();
39
40 /**
41 * Tear down
42 */
43 public function tearDown() {
44 foreach($this->testNodesToDelete as $node) {
45 if (\TYPO3\CMS\Core\Utility\GeneralUtility::isFirstPartOfStr($node, PATH_site . 'typo3temp/')) {
46 \TYPO3\CMS\Core\Utility\GeneralUtility::rmdir($node, TRUE);
47 }
48 }
49 }
50
51 /**
52 * @test
53 */
54 public function getNameReturnsSetName() {
55 /** @var $node \TYPO3\CMS\Install\FolderStructure\AbstractNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
56 $node = $this->getAccessibleMock('TYPO3\\CMS\\Install\\FolderStructure\\AbstractNode', array('dummy'), array(), '', FALSE);
57 $name = uniqid('name_');
58 $node->_set('name', $name);
59 $this->assertSame($name, $node->getName());
60 }
61
62 /**
63 * @test
64 */
65 public function getTargetPermissionReturnsSetTargetPermission() {
66 /** @var $node \TYPO3\CMS\Install\FolderStructure\AbstractNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
67 $node = $this->getAccessibleMock('TYPO3\\CMS\\Install\\FolderStructure\\AbstractNode', array('dummy'), array(), '', FALSE);
68 $permission = '1234';
69 $node->_set('targetPermission', $permission);
70 $this->assertSame($permission, $node->_call('getTargetPermission'));
71 }
72
73 /**
74 * @test
75 */
76 public function getChildrenReturnsSetChildren() {
77 /** @var $node \TYPO3\CMS\Install\FolderStructure\AbstractNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
78 $node = $this->getAccessibleMock('TYPO3\\CMS\\Install\\FolderStructure\\AbstractNode', array('dummy'), array(), '', FALSE);
79 $children = array('1234');
80 $node->_set('children', $children);
81 $this->assertSame($children, $node->_call('getChildren'));
82 }
83
84 /**
85 * @test
86 */
87 public function getParentReturnsSetParent() {
88 /** @var $node \TYPO3\CMS\Install\FolderStructure\AbstractNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
89 $node = $this->getAccessibleMock('TYPO3\\CMS\\Install\\FolderStructure\\AbstractNode', array('dummy'), array(), '', FALSE);
90 $parent = $this->getMock('TYPO3\CMS\Install\FolderStructure\RootNodeInterface', array(), array(), '', FALSE);
91 $node->_set('parent', $parent);
92 $this->assertSame($parent, $node->_call('getParent'));
93 }
94
95 /**
96 * @test
97 */
98 public function getAbsolutePathCallsParentForPathAndAppendsOwnName() {
99 /** @var $node \TYPO3\CMS\Install\FolderStructure\AbstractNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
100 $node = $this->getAccessibleMock('TYPO3\\CMS\\Install\\FolderStructure\\AbstractNode', array('dummy'), array(), '', FALSE);
101 $parent = $this->getMock('TYPO3\CMS\Install\FolderStructure\RootNodeInterface', array(), array(), '', FALSE);
102 $parentPath = '/foo/bar';
103 $parent->expects($this->once())->method('getAbsolutePath')->will($this->returnValue($parentPath));
104 $name = uniqid('test_');
105 $node->_set('parent', $parent);
106 $node->_set('name', $name);
107 $this->assertSame($parentPath . '/' . $name, $node->getAbsolutePath());
108 }
109
110 /**
111 * @test
112 */
113 public function isWritableCallsParentIsWritable() {
114 /** @var $node \TYPO3\CMS\Install\FolderStructure\AbstractNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
115 $node = $this->getAccessibleMock('TYPO3\\CMS\\Install\\FolderStructure\\AbstractNode', array('dummy'), array(), '', FALSE);
116 $parentMock = $this->getMock('TYPO3\\CMS\\Install\\FolderStructure\\NodeInterface', array(), array(), '', FALSE);
117 $parentMock->expects($this->once())->method('isWritable');
118 $node->_set('parent', $parentMock);
119 $node->isWritable();
120 }
121
122 /**
123 * @test
124 */
125 public function isWritableReturnsWritableStatusOfParent() {
126 /** @var $node \TYPO3\CMS\Install\FolderStructure\AbstractNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
127 $node = $this->getAccessibleMock('TYPO3\\CMS\\Install\\FolderStructure\\AbstractNode', array('dummy'), array(), '', FALSE);
128 $parentMock = $this->getMock('TYPO3\\CMS\\Install\\FolderStructure\\NodeInterface', array(), array(), '', FALSE);
129 $parentMock->expects($this->once())->method('isWritable')->will($this->returnValue(TRUE));
130 $node->_set('parent', $parentMock);
131 $this->assertTrue($node->isWritable());
132 }
133
134 /**
135 * @test
136 */
137 public function existsReturnsTrueIfNodeExists() {
138 /** @var $node \TYPO3\CMS\Install\FolderStructure\AbstractNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
139 $node = $this->getAccessibleMock('TYPO3\\CMS\\Install\\FolderStructure\\AbstractNode', array('getAbsolutePath'), array(), '', FALSE);
140 $path = PATH_site . 'typo3temp/' . uniqid('dir_');
141 \TYPO3\CMS\Core\Utility\GeneralUtility::mkdir_deep($path);
142 $this->testNodesToDelete[] = $path;
143 $node->expects($this->any())->method('getAbsolutePath')->will($this->returnValue($path));
144 $this->assertTrue($node->_call('exists'));
145 }
146
147 /**
148 * @test
149 */
150 public function existsReturnsTrueIfIsLinkAndTargetIsDead() {
151 if (TYPO3_OS === 'WIN') {
152 $this->markTestSkipped('Test not available on Windows OS.');
153 }
154 /** @var $node \TYPO3\CMS\Install\FolderStructure\AbstractNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
155 $node = $this->getAccessibleMock('TYPO3\\CMS\\Install\\FolderStructure\\AbstractNode', array('getAbsolutePath'), array(), '', FALSE);
156 $path = PATH_site . 'typo3temp/' . uniqid('link_');
157 $target = PATH_site . 'typo3temp/' . uniqid('notExists_');
158 symlink($target, $path);
159 $this->testNodesToDelete[] = $path;
160 $node->expects($this->any())->method('getAbsolutePath')->will($this->returnValue($path));
161 $this->assertTrue($node->_call('exists'));
162 }
163
164 /**
165 * @test
166 */
167 public function existsReturnsFalseIfNodeNotExists() {
168 /** @var $node \TYPO3\CMS\Install\FolderStructure\AbstractNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
169 $node = $this->getAccessibleMock('TYPO3\\CMS\\Install\\FolderStructure\\AbstractNode', array('getAbsolutePath'), array(), '', FALSE);
170 $path = PATH_site . 'typo3temp/' . uniqid('dir_');
171 $node->expects($this->any())->method('getAbsolutePath')->will($this->returnValue($path));
172 $this->assertFalse($node->_call('exists'));
173 }
174
175 /**
176 * @test
177 * @expectedException \TYPO3\CMS\Install\FolderStructure\Exception
178 */
179 public function fixPermissionThrowsExceptionIfPermissionAreAlreadyCorrect() {
180 /** @var $node \TYPO3\CMS\Install\FolderStructure\AbstractNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
181 $node = $this->getAccessibleMock(
182 'TYPO3\\CMS\\Install\\FolderStructure\\AbstractNode',
183 array('isPermissionCorrect', 'getAbsolutePath'),
184 array(),
185 '',
186 FALSE
187 );
188 $node->expects($this->any())->method('getAbsolutePath')->will($this->returnValue(''));
189 $node->expects($this->once())->method('isPermissionCorrect')->will($this->returnValue(TRUE));
190 $node->_call('fixPermission');
191 }
192
193 /**
194 * @test
195 */
196 public function fixPermissionReturnsErrorStatusIfPermissionCanNotBeChanged() {
197 if (TYPO3_OS === 'WIN') {
198 $this->markTestSkipped('Test not available on Windows OS.');
199 }
200 if (function_exists('posix_getegid') && posix_getegid() === 0) {
201 $this->markTestSkipped('Test skipped if run on linux as root');
202 }
203 /** @var $node \TYPO3\CMS\Install\FolderStructure\AbstractNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
204 $node = $this->getAccessibleMock(
205 'TYPO3\\CMS\\Install\\FolderStructure\\AbstractNode',
206 array('isPermissionCorrect', 'getRelativePathBelowSiteRoot', 'getAbsolutePath'),
207 array(),
208 '',
209 FALSE
210 );
211 $node->expects($this->any())->method('getRelativePathBelowSiteRoot')->will($this->returnValue(''));
212 $node->expects($this->once())->method('isPermissionCorrect')->will($this->returnValue(FALSE));
213 $path = PATH_site . 'typo3temp/' . uniqid('root_');
214 mkdir($path);
215 $subPath = $path . '/' . uniqid('dir_');
216 mkdir($subPath);
217 chmod($path, octdec(2000));
218 $this->testNodesToDelete[] = $path;
219 $node->expects($this->any())->method('getAbsolutePath')->will($this->returnValue($subPath));
220 $node->_set('targetPermission', '2770');
221 $this->assertInstanceOf('TYPO3\\CMS\\Install\\Status\\ErrorStatus', $node->_call('fixPermission'));
222 chmod($path, octdec(2770));
223 }
224
225 /**
226 * @test
227 */
228 public function fixPermissionReturnsOkStatusIfPermissionCanBeFixedAndSetsPermissionToCorrectValue() {
229 if (TYPO3_OS === 'WIN') {
230 $this->markTestSkipped('Test not available on Windows OS.');
231 }
232 /** @var $node \TYPO3\CMS\Install\FolderStructure\AbstractNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
233 $node = $this->getAccessibleMock(
234 'TYPO3\\CMS\\Install\\FolderStructure\\AbstractNode',
235 array('isPermissionCorrect', 'getRelativePathBelowSiteRoot', 'getAbsolutePath'),
236 array(),
237 '',
238 FALSE
239 );
240 $node->expects($this->any())->method('getRelativePathBelowSiteRoot')->will($this->returnValue(''));
241 $node->expects($this->once())->method('isPermissionCorrect')->will($this->returnValue(FALSE));
242 $path = PATH_site . 'typo3temp/' . uniqid('root_');
243 mkdir($path);
244 $subPath = $path . '/' . uniqid('dir_');
245 mkdir($subPath);
246 chmod($path, octdec(2770));
247 $this->testNodesToDelete[] = $path;
248 $node->_set('targetPermission', '2775');
249 $node->expects($this->any())->method('getAbsolutePath')->will($this->returnValue($subPath));
250 $this->assertInstanceOf('TYPO3\\CMS\\Install\\Status\\OkStatus', $node->_call('fixPermission'));
251 $resultDirectoryPermissions = substr(decoct(fileperms($subPath)), 1);
252 $this->assertSame('2775', $resultDirectoryPermissions);
253 }
254
255 /**
256 * @test
257 */
258 public function isPermissionCorrectReturnsTrueOnWindowsOs() {
259 /** @var $node \TYPO3\CMS\Install\FolderStructure\AbstractNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
260 $node = $this->getAccessibleMock('TYPO3\\CMS\\Install\\FolderStructure\\AbstractNode', array('isWindowsOs'), array(), '', FALSE);
261 $node->expects($this->once())->method('isWindowsOs')->will($this->returnValue(TRUE));
262 $this->assertTrue($node->_call('isPermissionCorrect'));
263 }
264
265 /**
266 * @test
267 */
268 public function isPermissionCorrectReturnsTrueIfTargetPermissionAndCurrentPermissionAreIdentical() {
269 /** @var $node \TYPO3\CMS\Install\FolderStructure\AbstractNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
270 $node = $this->getAccessibleMock('TYPO3\\CMS\\Install\\FolderStructure\\AbstractNode', array('isWindowsOs', 'getCurrentPermission'), array(), '', FALSE);
271 $node->expects($this->any())->method('isWindowsOs')->will($this->returnValue(FALSE));
272 $node->expects($this->any())->method('getCurrentPermission')->will($this->returnValue('foo'));
273 $node->_set('targetPermission', 'foo');
274 $this->assertTrue($node->_call('isPermissionCorrect'));
275 }
276
277 /**
278 * @test
279 */
280 public function isPermissionCorrectReturnsFalseIfTargetPermissionAndCurrentPermissionAreNotIdentical() {
281 /** @var $node \TYPO3\CMS\Install\FolderStructure\AbstractNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
282 $node = $this->getAccessibleMock('TYPO3\\CMS\\Install\\FolderStructure\\AbstractNode', array('isWindowsOs', 'getCurrentPermission'), array(), '', FALSE);
283 $node->expects($this->any())->method('isWindowsOs')->will($this->returnValue(FALSE));
284 $node->expects($this->any())->method('getCurrentPermission')->will($this->returnValue('foo'));
285 $node->_set('targetPermission', 'bar');
286 $this->assertFalse($node->_call('isPermissionCorrect'));
287 }
288
289 /**
290 * @test
291 */
292 public function getCurrentPermissionReturnsCurrentDirectoryPermission() {
293 if (TYPO3_OS === 'WIN') {
294 $this->markTestSkipped('Test not available on Windows OS.');
295 }
296 /** @var $node \TYPO3\CMS\Install\FolderStructure\AbstractNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
297 $node = $this->getAccessibleMock('TYPO3\\CMS\\Install\\FolderStructure\\AbstractNode', array('getAbsolutePath'), array(), '', FALSE);
298 $path = PATH_site . 'typo3temp/' . uniqid('dir_');
299 \TYPO3\CMS\Core\Utility\GeneralUtility::mkdir_deep($path);
300 $this->testNodesToDelete[] = $path;
301 chmod($path, octdec(2775));
302 clearstatcache();
303 $node->expects($this->any())->method('getAbsolutePath')->will($this->returnValue($path));
304 $this->assertSame('2775', $node->_call('getCurrentPermission'));
305 }
306
307 /**
308 * @test
309 */
310 public function getCurrentPermissionReturnsCurrentFilePermission() {
311 if (TYPO3_OS === 'WIN') {
312 $this->markTestSkipped('Test not available on Windows OS.');
313 }
314 /** @var $node \TYPO3\CMS\Install\FolderStructure\AbstractNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
315 $node = $this->getAccessibleMock('TYPO3\\CMS\\Install\\FolderStructure\\AbstractNode', array('getAbsolutePath'), array(), '', FALSE);
316 $file = PATH_site . 'typo3temp/' . uniqid('file_');
317 touch($file);
318 $this->testNodesToDelete[] = $file;
319 chmod($file, octdec(770));
320 clearstatcache();
321 $node->expects($this->any())->method('getAbsolutePath')->will($this->returnValue($file));
322 $this->assertSame('0770', $node->_call('getCurrentPermission'));
323 }
324
325 /**
326 * @test
327 * @expectedException \TYPO3\CMS\Install\FolderStructure\Exception\InvalidArgumentException
328 */
329 public function getRelativePathBelowSiteRootThrowsExceptionIfGivenPathIsNotBelowPathSiteConstant() {
330 /** @var $node \TYPO3\CMS\Install\FolderStructure\AbstractNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
331 $node = $this->getAccessibleMock('TYPO3\\CMS\\Install\\FolderStructure\\AbstractNode', array('dummy'), array(), '', FALSE);
332 $node->_call('getRelativePathBelowSiteRoot', '/tmp');
333 }
334
335 /**
336 * @test
337 */
338 public function getRelativePathCallsGetAbsolutePathIfPathIsNull() {
339 /** @var $node \TYPO3\CMS\Install\FolderStructure\AbstractNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
340 $node = $this->getAccessibleMock(
341 'TYPO3\\CMS\\Install\\FolderStructure\\AbstractNode',
342 array('getAbsolutePath'),
343 array(),
344 '',
345 FALSE
346 );
347 $node->expects($this->once())->method('getAbsolutePath')->will($this->returnValue(PATH_site));
348 $node->_call('getRelativePathBelowSiteRoot', NULL);
349 }
350
351 /**
352 * @test
353 */
354 public function getRelativePathBelowSiteRootReturnsSingleForwardSlashIfGivenPathEqualsPathSiteConstant() {
355 /** @var $node \TYPO3\CMS\Install\FolderStructure\AbstractNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
356 $node = $this->getAccessibleMock('TYPO3\\CMS\\Install\\FolderStructure\\AbstractNode', array('dummy'), array(), '', FALSE);
357 $result = $node->_call('getRelativePathBelowSiteRoot', PATH_site);
358 $this->assertSame('/', $result);
359 }
360
361 /**
362 * @test
363 */
364 public function getRelativePathBelowSiteRootReturnsSubPath() {
365 /** @var $node \TYPO3\CMS\Install\FolderStructure\AbstractNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
366 $node = $this->getAccessibleMock('TYPO3\\CMS\\Install\\FolderStructure\\AbstractNode', array('dummy'), array(), '', FALSE);
367 $result = $node->_call('getRelativePathBelowSiteRoot', PATH_site . 'foo/bar');
368 $this->assertSame('/foo/bar', $result);
369 }
370 }
371 ?>