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