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