TYPO3 CMS  TYPO3_7-6
AbstractNodeTest.php
Go to the documentation of this file.
1 <?php
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 
21 {
25  public function getNameReturnsSetName()
26  {
28  $node = $this->getAccessibleMock(\TYPO3\CMS\Install\FolderStructure\AbstractNode::class, ['dummy'], [], '', false);
29  $name = $this->getUniqueId('name_');
30  $node->_set('name', $name);
31  $this->assertSame($name, $node->getName());
32  }
33 
37  public function getTargetPermissionReturnsSetTargetPermission()
38  {
40  $node = $this->getAccessibleMock(\TYPO3\CMS\Install\FolderStructure\AbstractNode::class, ['dummy'], [], '', false);
41  $permission = '1234';
42  $node->_set('targetPermission', $permission);
43  $this->assertSame($permission, $node->_call('getTargetPermission'));
44  }
45 
49  public function getChildrenReturnsSetChildren()
50  {
52  $node = $this->getAccessibleMock(\TYPO3\CMS\Install\FolderStructure\AbstractNode::class, ['dummy'], [], '', false);
53  $children = ['1234'];
54  $node->_set('children', $children);
55  $this->assertSame($children, $node->_call('getChildren'));
56  }
57 
61  public function getParentReturnsSetParent()
62  {
64  $node = $this->getAccessibleMock(\TYPO3\CMS\Install\FolderStructure\AbstractNode::class, ['dummy'], [], '', false);
65  $parent = $this->getMock(\TYPO3\CMS\Install\FolderStructure\RootNodeInterface::class, [], [], '', false);
66  $node->_set('parent', $parent);
67  $this->assertSame($parent, $node->_call('getParent'));
68  }
69 
73  public function getAbsolutePathCallsParentForPathAndAppendsOwnName()
74  {
76  $node = $this->getAccessibleMock(\TYPO3\CMS\Install\FolderStructure\AbstractNode::class, ['dummy'], [], '', false);
77  $parent = $this->getMock(\TYPO3\CMS\Install\FolderStructure\RootNodeInterface::class, [], [], '', 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 
89  public function isWritableCallsParentIsWritable()
90  {
92  $node = $this->getAccessibleMock(\TYPO3\CMS\Install\FolderStructure\AbstractNode::class, ['dummy'], [], '', false);
93  $parentMock = $this->getMock(\TYPO3\CMS\Install\FolderStructure\NodeInterface::class, [], [], '', false);
94  $parentMock->expects($this->once())->method('isWritable');
95  $node->_set('parent', $parentMock);
96  $node->isWritable();
97  }
98 
102  public function isWritableReturnsWritableStatusOfParent()
103  {
105  $node = $this->getAccessibleMock(\TYPO3\CMS\Install\FolderStructure\AbstractNode::class, ['dummy'], [], '', false);
106  $parentMock = $this->getMock(\TYPO3\CMS\Install\FolderStructure\NodeInterface::class, [], [], '', false);
107  $parentMock->expects($this->once())->method('isWritable')->will($this->returnValue(true));
108  $node->_set('parent', $parentMock);
109  $this->assertTrue($node->isWritable());
110  }
111 
115  public function existsReturnsTrueIfNodeExists()
116  {
118  $node = $this->getAccessibleMock(\TYPO3\CMS\Install\FolderStructure\AbstractNode::class, ['getAbsolutePath'], [], '', 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 
128  public function existsReturnsTrueIfIsLinkAndTargetIsDead()
129  {
130  if (TYPO3_OS === 'WIN') {
131  $this->markTestSkipped('Test not available on Windows OS.');
132  }
134  $node = $this->getAccessibleMock(\TYPO3\CMS\Install\FolderStructure\AbstractNode::class, ['getAbsolutePath'], [], '', false);
135  $path = PATH_site . 'typo3temp/' . $this->getUniqueId('link_');
136  $target = PATH_site . 'typo3temp/' . $this->getUniqueId('notExists_');
137  symlink($target, $path);
138  $this->testFilesToDelete[] = $path;
139  $node->expects($this->any())->method('getAbsolutePath')->will($this->returnValue($path));
140  $this->assertTrue($node->_call('exists'));
141  }
142 
146  public function existsReturnsFalseIfNodeNotExists()
147  {
149  $node = $this->getAccessibleMock(\TYPO3\CMS\Install\FolderStructure\AbstractNode::class, ['getAbsolutePath'], [], '', false);
150  $path = $this->getVirtualTestFilePath('dir_');
151  $node->expects($this->any())->method('getAbsolutePath')->will($this->returnValue($path));
152  $this->assertFalse($node->_call('exists'));
153  }
154 
159  public function fixPermissionThrowsExceptionIfPermissionAreAlreadyCorrect()
160  {
162  $node = $this->getAccessibleMock(
163  \TYPO3\CMS\Install\FolderStructure\AbstractNode::class,
164  ['isPermissionCorrect', 'getAbsolutePath'],
165  [],
166  '',
167  false
168  );
169  $node->expects($this->any())->method('getAbsolutePath')->will($this->returnValue(''));
170  $node->expects($this->once())->method('isPermissionCorrect')->will($this->returnValue(true));
171  $node->_call('fixPermission');
172  }
173 
177  public function fixPermissionReturnsNoticeStatusIfPermissionCanNotBeChanged()
178  {
179  if (TYPO3_OS === 'WIN') {
180  $this->markTestSkipped('Test not available on Windows OS.');
181  }
182  if (function_exists('posix_getegid') && posix_getegid() === 0) {
183  $this->markTestSkipped('Test skipped if run on linux as root');
184  }
186  $node = $this->getAccessibleMock(
187  \TYPO3\CMS\Install\FolderStructure\AbstractNode::class,
188  ['isPermissionCorrect', 'getRelativePathBelowSiteRoot', 'getAbsolutePath'],
189  [],
190  '',
191  false
192  );
193  $node->expects($this->any())->method('getRelativePathBelowSiteRoot')->will($this->returnValue(''));
194  $node->expects($this->once())->method('isPermissionCorrect')->will($this->returnValue(false));
195  $path = $this->getVirtualTestDir('root_');
196  $subPath = $path . '/' . $this->getUniqueId('dir_');
197  mkdir($subPath);
198  chmod($path, 02000);
199  $node->expects($this->any())->method('getAbsolutePath')->will($this->returnValue($subPath));
200  $node->_set('targetPermission', '2770');
201  $this->assertInstanceOf(\TYPO3\CMS\Install\Status\NoticeStatus::class, $node->_call('fixPermission'));
202  chmod($path, 02770);
203  }
204 
208  public function fixPermissionReturnsNoticeStatusIfPermissionsCanNotBeChanged()
209  {
210  if (TYPO3_OS === 'WIN') {
211  $this->markTestSkipped('Test not available on Windows OS.');
212  }
213  if (function_exists('posix_getegid') && posix_getegid() === 0) {
214  $this->markTestSkipped('Test skipped if run on linux as root');
215  }
217  $node = $this->getAccessibleMock(
218  \TYPO3\CMS\Install\FolderStructure\AbstractNode::class,
219  ['isPermissionCorrect', 'getRelativePathBelowSiteRoot', 'getAbsolutePath'],
220  [],
221  '',
222  false
223  );
224  $node->expects($this->any())->method('getRelativePathBelowSiteRoot')->will($this->returnValue(''));
225  $node->expects($this->once())->method('isPermissionCorrect')->will($this->returnValue(false));
226  $path = $this->getVirtualTestDir('root_');
227  $subPath = $path . '/' . $this->getUniqueId('dir_');
228  mkdir($subPath);
229  chmod($path, 02000);
230  $node->expects($this->any())->method('getAbsolutePath')->will($this->returnValue($subPath));
231  $node->_set('targetPermission', '2770');
232  $this->assertInstanceOf(\TYPO3\CMS\Install\Status\NoticeStatus::class, $node->_call('fixPermission'));
233  chmod($path, 02770);
234  }
235 
239  public function fixPermissionReturnsOkStatusIfPermissionCanBeFixedAndSetsPermissionToCorrectValue()
240  {
241  if (TYPO3_OS === 'WIN') {
242  $this->markTestSkipped('Test not available on Windows OS.');
243  }
245  $node = $this->getAccessibleMock(
246  \TYPO3\CMS\Install\FolderStructure\AbstractNode::class,
247  ['isPermissionCorrect', 'getRelativePathBelowSiteRoot', 'getAbsolutePath'],
248  [],
249  '',
250  false
251  );
252  $node->expects($this->any())->method('getRelativePathBelowSiteRoot')->will($this->returnValue(''));
253  $node->expects($this->once())->method('isPermissionCorrect')->will($this->returnValue(false));
254  $path = $this->getVirtualTestDir('root_');
255  $subPath = $path . '/' . $this->getUniqueId('dir_');
256  mkdir($subPath);
257  chmod($path, 02770);
258  $node->_set('targetPermission', '2770');
259  $node->expects($this->any())->method('getAbsolutePath')->will($this->returnValue($subPath));
260  $this->assertInstanceOf(\TYPO3\CMS\Install\Status\OkStatus::class, $node->_call('fixPermission'));
261  $resultDirectoryPermissions = substr(decoct(fileperms($subPath)), 1);
262  $this->assertSame('2770', $resultDirectoryPermissions);
263  }
264 
268  public function isPermissionCorrectReturnsTrueOnWindowsOs()
269  {
271  $node = $this->getAccessibleMock(\TYPO3\CMS\Install\FolderStructure\AbstractNode::class, ['isWindowsOs'], [], '', false);
272  $node->expects($this->once())->method('isWindowsOs')->will($this->returnValue(true));
273  $this->assertTrue($node->_call('isPermissionCorrect'));
274  }
275 
279  public function isPermissionCorrectReturnsFalseIfTargetPermissionAndCurrentPermissionAreNotIdentical()
280  {
282  $node = $this->getAccessibleMock(\TYPO3\CMS\Install\FolderStructure\AbstractNode::class, ['isWindowsOs', 'getCurrentPermission'], [], '', 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 
292  public function getCurrentPermissionReturnsCurrentDirectoryPermission()
293  {
294  if (TYPO3_OS === 'WIN') {
295  $this->markTestSkipped('Test not available on Windows OS.');
296  }
298  $node = $this->getAccessibleMock(\TYPO3\CMS\Install\FolderStructure\AbstractNode::class, ['getAbsolutePath'], [], '', false);
299  $path = $this->getVirtualTestDir('dir_');
300  chmod($path, 02775);
301  clearstatcache();
302  $node->expects($this->any())->method('getAbsolutePath')->will($this->returnValue($path));
303  $this->assertSame('2775', $node->_call('getCurrentPermission'));
304  }
305 
309  public function getCurrentPermissionReturnsCurrentFilePermission()
310  {
311  if (TYPO3_OS === 'WIN') {
312  $this->markTestSkipped('Test not available on Windows OS.');
313  }
315  $node = $this->getAccessibleMock(\TYPO3\CMS\Install\FolderStructure\AbstractNode::class, ['getAbsolutePath'], [], '', false);
316  $file = $this->getVirtualTestFilePath('file_');
317  touch($file);
318  chmod($file, 0770);
319  clearstatcache();
320  $node->expects($this->any())->method('getAbsolutePath')->will($this->returnValue($file));
321  $this->assertSame('0770', $node->_call('getCurrentPermission'));
322  }
323 
328  public function getRelativePathBelowSiteRootThrowsExceptionIfGivenPathIsNotBelowPathSiteConstant()
329  {
331  $node = $this->getAccessibleMock(\TYPO3\CMS\Install\FolderStructure\AbstractNode::class, ['dummy'], [], '', false);
332  $node->_call('getRelativePathBelowSiteRoot', '/tmp');
333  }
334 
338  public function getRelativePathCallsGetAbsolutePathIfPathIsNull()
339  {
341  $node = $this->getAccessibleMock(
342  \TYPO3\CMS\Install\FolderStructure\AbstractNode::class,
343  ['getAbsolutePath'],
344  [],
345  '',
346  false
347  );
348  $node->expects($this->once())->method('getAbsolutePath')->will($this->returnValue(PATH_site));
349  $node->_call('getRelativePathBelowSiteRoot', null);
350  }
351 
355  public function getRelativePathBelowSiteRootReturnsSingleForwardSlashIfGivenPathEqualsPathSiteConstant()
356  {
358  $node = $this->getAccessibleMock(\TYPO3\CMS\Install\FolderStructure\AbstractNode::class, ['dummy'], [], '', false);
359  $result = $node->_call('getRelativePathBelowSiteRoot', PATH_site);
360  $this->assertSame('/', $result);
361  }
362 
366  public function getRelativePathBelowSiteRootReturnsSubPath()
367  {
369  $node = $this->getAccessibleMock(\TYPO3\CMS\Install\FolderStructure\AbstractNode::class, ['dummy'], [], '', false);
370  $result = $node->_call('getRelativePathBelowSiteRoot', PATH_site . 'foo/bar');
371  $this->assertSame('/foo/bar', $result);
372  }
373 }
getAccessibleMock( $originalClassName, $methods=[], array $arguments=[], $mockClassName='', $callOriginalConstructor=true, $callOriginalClone=true, $callAutoload=true)