2 namespace TYPO3\CMS\Install\Tests\Unit\FolderStructure
;
4 /***************************************************************
7 * (c) 2013 Christian Kuhn <lolli@schwarzbu.ch>
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.
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.
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.
27 * This copyright notice MUST APPEAR in all copies of the script!
28 ***************************************************************/
33 class AbstractNodeTest
extends \TYPO3\CMS\Core\Tests\UnitTestCase
{
36 * @var array Directories or files in typo3temp/ created during tests to delete afterwards
38 protected $testNodesToDelete = array();
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);
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());
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);
69 $node->_set('targetPermission', $permission);
70 $this->assertSame($permission, $node->getTargetPermission());
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->getChildren());
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->getParent());
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());
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);
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());
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'));
150 public function existsReturnsTrueIfIsLinkAndTargetIsDead() {
151 if (TYPO3_OS
=== 'WIN') {
152 $this->markTestSkipped('Test not available on Windows OS.');
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'));
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'));
177 * @expectedException \TYPO3\CMS\Install\FolderStructure\Exception
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'),
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');
196 public function fixPermissionReturnsErrorStatusIfPermissionCanNotBeChanged() {
197 if (TYPO3_OS
=== 'WIN') {
198 $this->markTestSkipped('Test not available on Windows OS.');
200 if (function_exists('posix_getegid') && posix_getegid() === 0) {
201 $this->markTestSkipped('Test skipped if run on linux as root');
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'),
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_');
215 $subPath = $path . '/' . uniqid('dir_');
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));
228 public function fixPermissionReturnsOkStatusIfPermissionCanBeFixedAndSetsPermissionToCorrectValue() {
229 if (TYPO3_OS
=== 'WIN') {
230 $this->markTestSkipped('Test not available on Windows OS.');
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'),
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_');
244 $subPath = $path . '/' . uniqid('dir_');
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);
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'));
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'));
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'));
292 public function getCurrentPermissionReturnsCurrentDirectoryPermission() {
293 if (TYPO3_OS
=== 'WIN') {
294 $this->markTestSkipped('Test not available on Windows OS.');
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));
303 $node->expects($this->any())->method('getAbsolutePath')->will($this->returnValue($path));
304 $this->assertSame('2775', $node->_call('getCurrentPermission'));
310 public function getCurrentPermissionReturnsCurrentFilePermission() {
311 if (TYPO3_OS
=== 'WIN') {
312 $this->markTestSkipped('Test not available on Windows OS.');
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_');
318 $this->testNodesToDelete
[] = $file;
319 chmod($file, octdec(770));
321 $node->expects($this->any())->method('getAbsolutePath')->will($this->returnValue($file));
322 $this->assertSame('0770', $node->_call('getCurrentPermission'));
327 * @expectedException \TYPO3\CMS\Install\FolderStructure\Exception\InvalidArgumentException
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');
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'),
347 $node->expects($this->once())->method('getAbsolutePath')->will($this->returnValue(PATH_site
));
348 $node->_call('getRelativePathBelowSiteRoot', NULL);
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);
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);