[CLEANUP] Move test file deletion into UnitTestCase class
[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 * @test
24 */
25 public function getNameReturnsSetName() {
26 /** @var $node \TYPO3\CMS\Install\FolderStructure\AbstractNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
27 $node = $this->getAccessibleMock('TYPO3\\CMS\\Install\\FolderStructure\\AbstractNode', array('dummy'), array(), '', FALSE);
28 $name = uniqid('name_');
29 $node->_set('name', $name);
30 $this->assertSame($name, $node->getName());
31 }
32
33 /**
34 * @test
35 */
36 public function getTargetPermissionReturnsSetTargetPermission() {
37 /** @var $node \TYPO3\CMS\Install\FolderStructure\AbstractNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
38 $node = $this->getAccessibleMock('TYPO3\\CMS\\Install\\FolderStructure\\AbstractNode', array('dummy'), array(), '', FALSE);
39 $permission = '1234';
40 $node->_set('targetPermission', $permission);
41 $this->assertSame($permission, $node->_call('getTargetPermission'));
42 }
43
44 /**
45 * @test
46 */
47 public function getChildrenReturnsSetChildren() {
48 /** @var $node \TYPO3\CMS\Install\FolderStructure\AbstractNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
49 $node = $this->getAccessibleMock('TYPO3\\CMS\\Install\\FolderStructure\\AbstractNode', array('dummy'), array(), '', FALSE);
50 $children = array('1234');
51 $node->_set('children', $children);
52 $this->assertSame($children, $node->_call('getChildren'));
53 }
54
55 /**
56 * @test
57 */
58 public function getParentReturnsSetParent() {
59 /** @var $node \TYPO3\CMS\Install\FolderStructure\AbstractNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
60 $node = $this->getAccessibleMock('TYPO3\\CMS\\Install\\FolderStructure\\AbstractNode', array('dummy'), array(), '', FALSE);
61 $parent = $this->getMock('TYPO3\CMS\Install\FolderStructure\RootNodeInterface', array(), array(), '', FALSE);
62 $node->_set('parent', $parent);
63 $this->assertSame($parent, $node->_call('getParent'));
64 }
65
66 /**
67 * @test
68 */
69 public function getAbsolutePathCallsParentForPathAndAppendsOwnName() {
70 /** @var $node \TYPO3\CMS\Install\FolderStructure\AbstractNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
71 $node = $this->getAccessibleMock('TYPO3\\CMS\\Install\\FolderStructure\\AbstractNode', array('dummy'), array(), '', FALSE);
72 $parent = $this->getMock('TYPO3\CMS\Install\FolderStructure\RootNodeInterface', array(), array(), '', FALSE);
73 $parentPath = '/foo/bar';
74 $parent->expects($this->once())->method('getAbsolutePath')->will($this->returnValue($parentPath));
75 $name = uniqid('test_');
76 $node->_set('parent', $parent);
77 $node->_set('name', $name);
78 $this->assertSame($parentPath . '/' . $name, $node->getAbsolutePath());
79 }
80
81 /**
82 * @test
83 */
84 public function isWritableCallsParentIsWritable() {
85 /** @var $node \TYPO3\CMS\Install\FolderStructure\AbstractNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
86 $node = $this->getAccessibleMock('TYPO3\\CMS\\Install\\FolderStructure\\AbstractNode', array('dummy'), array(), '', FALSE);
87 $parentMock = $this->getMock('TYPO3\\CMS\\Install\\FolderStructure\\NodeInterface', array(), array(), '', FALSE);
88 $parentMock->expects($this->once())->method('isWritable');
89 $node->_set('parent', $parentMock);
90 $node->isWritable();
91 }
92
93 /**
94 * @test
95 */
96 public function isWritableReturnsWritableStatusOfParent() {
97 /** @var $node \TYPO3\CMS\Install\FolderStructure\AbstractNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
98 $node = $this->getAccessibleMock('TYPO3\\CMS\\Install\\FolderStructure\\AbstractNode', array('dummy'), array(), '', FALSE);
99 $parentMock = $this->getMock('TYPO3\\CMS\\Install\\FolderStructure\\NodeInterface', array(), array(), '', FALSE);
100 $parentMock->expects($this->once())->method('isWritable')->will($this->returnValue(TRUE));
101 $node->_set('parent', $parentMock);
102 $this->assertTrue($node->isWritable());
103 }
104
105 /**
106 * @test
107 */
108 public function existsReturnsTrueIfNodeExists() {
109 /** @var $node \TYPO3\CMS\Install\FolderStructure\AbstractNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
110 $node = $this->getAccessibleMock('TYPO3\\CMS\\Install\\FolderStructure\\AbstractNode', array('getAbsolutePath'), array(), '', FALSE);
111 $path = PATH_site . 'typo3temp/' . uniqid('dir_');
112 \TYPO3\CMS\Core\Utility\GeneralUtility::mkdir_deep($path);
113 $this->testFilesToDelete[] = $path;
114 $node->expects($this->any())->method('getAbsolutePath')->will($this->returnValue($path));
115 $this->assertTrue($node->_call('exists'));
116 }
117
118 /**
119 * @test
120 */
121 public function existsReturnsTrueIfIsLinkAndTargetIsDead() {
122 if (TYPO3_OS === 'WIN') {
123 $this->markTestSkipped('Test not available on Windows OS.');
124 }
125 /** @var $node \TYPO3\CMS\Install\FolderStructure\AbstractNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
126 $node = $this->getAccessibleMock('TYPO3\\CMS\\Install\\FolderStructure\\AbstractNode', array('getAbsolutePath'), array(), '', FALSE);
127 $path = PATH_site . 'typo3temp/' . uniqid('link_');
128 $target = PATH_site . 'typo3temp/' . uniqid('notExists_');
129 symlink($target, $path);
130 $this->testFilesToDelete[] = $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 existsReturnsFalseIfNodeNotExists() {
139 /** @var $node \TYPO3\CMS\Install\FolderStructure\AbstractNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
140 $node = $this->getAccessibleMock('TYPO3\\CMS\\Install\\FolderStructure\\AbstractNode', array('getAbsolutePath'), array(), '', FALSE);
141 $path = PATH_site . 'typo3temp/' . uniqid('dir_');
142 $node->expects($this->any())->method('getAbsolutePath')->will($this->returnValue($path));
143 $this->assertFalse($node->_call('exists'));
144 }
145
146 /**
147 * @test
148 * @expectedException \TYPO3\CMS\Install\FolderStructure\Exception
149 */
150 public function fixPermissionThrowsExceptionIfPermissionAreAlreadyCorrect() {
151 /** @var $node \TYPO3\CMS\Install\FolderStructure\AbstractNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
152 $node = $this->getAccessibleMock(
153 'TYPO3\\CMS\\Install\\FolderStructure\\AbstractNode',
154 array('isPermissionCorrect', 'getAbsolutePath'),
155 array(),
156 '',
157 FALSE
158 );
159 $node->expects($this->any())->method('getAbsolutePath')->will($this->returnValue(''));
160 $node->expects($this->once())->method('isPermissionCorrect')->will($this->returnValue(TRUE));
161 $node->_call('fixPermission');
162 }
163
164 /**
165 * @test
166 */
167 public function fixPermissionReturnsNoticeStatusIfPermissionCanNotBeChanged() {
168 if (TYPO3_OS === 'WIN') {
169 $this->markTestSkipped('Test not available on Windows OS.');
170 }
171 if (function_exists('posix_getegid') && posix_getegid() === 0) {
172 $this->markTestSkipped('Test skipped if run on linux as root');
173 }
174 /** @var $node \TYPO3\CMS\Install\FolderStructure\AbstractNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
175 $node = $this->getAccessibleMock(
176 'TYPO3\\CMS\\Install\\FolderStructure\\AbstractNode',
177 array('isPermissionCorrect', 'getRelativePathBelowSiteRoot', 'getAbsolutePath'),
178 array(),
179 '',
180 FALSE
181 );
182 $node->expects($this->any())->method('getRelativePathBelowSiteRoot')->will($this->returnValue(''));
183 $node->expects($this->once())->method('isPermissionCorrect')->will($this->returnValue(FALSE));
184 $path = PATH_site . 'typo3temp/' . uniqid('root_');
185 mkdir($path);
186 $subPath = $path . '/' . uniqid('dir_');
187 mkdir($subPath);
188 chmod($path, 02000);
189 $this->testFilesToDelete[] = $path;
190 $node->expects($this->any())->method('getAbsolutePath')->will($this->returnValue($subPath));
191 $node->_set('targetPermission', '2770');
192 $this->assertInstanceOf('TYPO3\\CMS\\Install\\Status\\NoticeStatus', $node->_call('fixPermission'));
193 chmod($path, 02770);
194 }
195
196 /**
197 * @test
198 */
199 public function fixPermissionReturnsNoticeStatusIfPermissionsCanNotBeChanged() {
200 if (TYPO3_OS === 'WIN') {
201 $this->markTestSkipped('Test not available on Windows OS.');
202 }
203 if (function_exists('posix_getegid') && posix_getegid() === 0) {
204 $this->markTestSkipped('Test skipped if run on linux as root');
205 }
206 /** @var $node \TYPO3\CMS\Install\FolderStructure\AbstractNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
207 $node = $this->getAccessibleMock(
208 'TYPO3\\CMS\\Install\\FolderStructure\\AbstractNode',
209 array('isPermissionCorrect', 'getRelativePathBelowSiteRoot', 'getAbsolutePath'),
210 array(),
211 '',
212 FALSE
213 );
214 $node->expects($this->any())->method('getRelativePathBelowSiteRoot')->will($this->returnValue(''));
215 $node->expects($this->once())->method('isPermissionCorrect')->will($this->returnValue(FALSE));
216 $path = PATH_site . 'typo3temp/' . uniqid('root_');
217 mkdir($path);
218 $subPath = $path . '/' . uniqid('dir_');
219 mkdir($subPath);
220 chmod($path, 02000);
221 $this->testFilesToDelete[] = $path;
222 $node->expects($this->any())->method('getAbsolutePath')->will($this->returnValue($subPath));
223 $node->_set('targetPermission', '2770');
224 $this->assertInstanceOf('TYPO3\\CMS\\Install\\Status\\NoticeStatus', $node->_call('fixPermission'));
225 chmod($path, 02770);
226 }
227
228 /**
229 * @test
230 */
231 public function fixPermissionReturnsOkStatusIfPermissionCanBeFixedAndSetsPermissionToCorrectValue() {
232 if (TYPO3_OS === 'WIN') {
233 $this->markTestSkipped('Test not available on Windows OS.');
234 }
235 /** @var $node \TYPO3\CMS\Install\FolderStructure\AbstractNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
236 $node = $this->getAccessibleMock(
237 'TYPO3\\CMS\\Install\\FolderStructure\\AbstractNode',
238 array('isPermissionCorrect', 'getRelativePathBelowSiteRoot', 'getAbsolutePath'),
239 array(),
240 '',
241 FALSE
242 );
243 $node->expects($this->any())->method('getRelativePathBelowSiteRoot')->will($this->returnValue(''));
244 $node->expects($this->once())->method('isPermissionCorrect')->will($this->returnValue(FALSE));
245 $path = PATH_site . 'typo3temp/' . uniqid('root_');
246 mkdir($path);
247 $subPath = $path . '/' . uniqid('dir_');
248 mkdir($subPath);
249 chmod($path, 02770);
250 $this->testFilesToDelete[] = $path;
251 $node->_set('targetPermission', '2770');
252 $node->expects($this->any())->method('getAbsolutePath')->will($this->returnValue($subPath));
253 $this->assertInstanceOf('TYPO3\\CMS\\Install\\Status\\OkStatus', $node->_call('fixPermission'));
254 $resultDirectoryPermissions = substr(decoct(fileperms($subPath)), 1);
255 $this->assertSame('2770', $resultDirectoryPermissions);
256 }
257
258 /**
259 * @test
260 */
261 public function isPermissionCorrectReturnsTrueOnWindowsOs() {
262 /** @var $node \TYPO3\CMS\Install\FolderStructure\AbstractNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
263 $node = $this->getAccessibleMock('TYPO3\\CMS\\Install\\FolderStructure\\AbstractNode', array('isWindowsOs'), array(), '', FALSE);
264 $node->expects($this->once())->method('isWindowsOs')->will($this->returnValue(TRUE));
265 $this->assertTrue($node->_call('isPermissionCorrect'));
266 }
267
268 /**
269 * @test
270 */
271 public function isPermissionCorrectReturnsFalseIfTargetPermissionAndCurrentPermissionAreNotIdentical() {
272 /** @var $node \TYPO3\CMS\Install\FolderStructure\AbstractNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
273 $node = $this->getAccessibleMock('TYPO3\\CMS\\Install\\FolderStructure\\AbstractNode', array('isWindowsOs', 'getCurrentPermission'), array(), '', FALSE);
274 $node->expects($this->any())->method('isWindowsOs')->will($this->returnValue(FALSE));
275 $node->expects($this->any())->method('getCurrentPermission')->will($this->returnValue('foo'));
276 $node->_set('targetPermission', 'bar');
277 $this->assertFalse($node->_call('isPermissionCorrect'));
278 }
279
280 /**
281 * @test
282 */
283 public function getCurrentPermissionReturnsCurrentDirectoryPermission() {
284 if (TYPO3_OS === 'WIN') {
285 $this->markTestSkipped('Test not available on Windows OS.');
286 }
287 /** @var $node \TYPO3\CMS\Install\FolderStructure\AbstractNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
288 $node = $this->getAccessibleMock('TYPO3\\CMS\\Install\\FolderStructure\\AbstractNode', array('getAbsolutePath'), array(), '', FALSE);
289 $path = PATH_site . 'typo3temp/' . uniqid('dir_');
290 \TYPO3\CMS\Core\Utility\GeneralUtility::mkdir_deep($path);
291 $this->testFilesToDelete[] = $path;
292 chmod($path, 02775);
293 clearstatcache();
294 $node->expects($this->any())->method('getAbsolutePath')->will($this->returnValue($path));
295 $this->assertSame('2775', $node->_call('getCurrentPermission'));
296 }
297
298 /**
299 * @test
300 */
301 public function getCurrentPermissionReturnsCurrentFilePermission() {
302 if (TYPO3_OS === 'WIN') {
303 $this->markTestSkipped('Test not available on Windows OS.');
304 }
305 /** @var $node \TYPO3\CMS\Install\FolderStructure\AbstractNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
306 $node = $this->getAccessibleMock('TYPO3\\CMS\\Install\\FolderStructure\\AbstractNode', array('getAbsolutePath'), array(), '', FALSE);
307 $file = PATH_site . 'typo3temp/' . uniqid('file_');
308 touch($file);
309 $this->testFilesToDelete[] = $file;
310 chmod($file, 0770);
311 clearstatcache();
312 $node->expects($this->any())->method('getAbsolutePath')->will($this->returnValue($file));
313 $this->assertSame('0770', $node->_call('getCurrentPermission'));
314 }
315
316 /**
317 * @test
318 * @expectedException \TYPO3\CMS\Install\FolderStructure\Exception\InvalidArgumentException
319 */
320 public function getRelativePathBelowSiteRootThrowsExceptionIfGivenPathIsNotBelowPathSiteConstant() {
321 /** @var $node \TYPO3\CMS\Install\FolderStructure\AbstractNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
322 $node = $this->getAccessibleMock('TYPO3\\CMS\\Install\\FolderStructure\\AbstractNode', array('dummy'), array(), '', FALSE);
323 $node->_call('getRelativePathBelowSiteRoot', '/tmp');
324 }
325
326 /**
327 * @test
328 */
329 public function getRelativePathCallsGetAbsolutePathIfPathIsNull() {
330 /** @var $node \TYPO3\CMS\Install\FolderStructure\AbstractNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
331 $node = $this->getAccessibleMock(
332 'TYPO3\\CMS\\Install\\FolderStructure\\AbstractNode',
333 array('getAbsolutePath'),
334 array(),
335 '',
336 FALSE
337 );
338 $node->expects($this->once())->method('getAbsolutePath')->will($this->returnValue(PATH_site));
339 $node->_call('getRelativePathBelowSiteRoot', NULL);
340 }
341
342 /**
343 * @test
344 */
345 public function getRelativePathBelowSiteRootReturnsSingleForwardSlashIfGivenPathEqualsPathSiteConstant() {
346 /** @var $node \TYPO3\CMS\Install\FolderStructure\AbstractNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
347 $node = $this->getAccessibleMock('TYPO3\\CMS\\Install\\FolderStructure\\AbstractNode', array('dummy'), array(), '', FALSE);
348 $result = $node->_call('getRelativePathBelowSiteRoot', PATH_site);
349 $this->assertSame('/', $result);
350 }
351
352 /**
353 * @test
354 */
355 public function getRelativePathBelowSiteRootReturnsSubPath() {
356 /** @var $node \TYPO3\CMS\Install\FolderStructure\AbstractNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
357 $node = $this->getAccessibleMock('TYPO3\\CMS\\Install\\FolderStructure\\AbstractNode', array('dummy'), array(), '', FALSE);
358 $result = $node->_call('getRelativePathBelowSiteRoot', PATH_site . 'foo/bar');
359 $this->assertSame('/foo/bar', $result);
360 }
361
362 }