TYPO3 CMS  TYPO3_8-7
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  */
18 
23 {
27  public function getNameReturnsSetName()
28  {
30  $node = $this->getAccessibleMock(\TYPO3\CMS\Install\FolderStructure\AbstractNode::class, ['dummy'], [], '', false);
31  $name = $this->getUniqueId('name_');
32  $node->_set('name', $name);
33  $this->assertSame($name, $node->getName());
34  }
35 
39  public function getTargetPermissionReturnsSetTargetPermission()
40  {
42  $node = $this->getAccessibleMock(\TYPO3\CMS\Install\FolderStructure\AbstractNode::class, ['dummy'], [], '', false);
43  $permission = '1234';
44  $node->_set('targetPermission', $permission);
45  $this->assertSame($permission, $node->_call('getTargetPermission'));
46  }
47 
51  public function getChildrenReturnsSetChildren()
52  {
54  $node = $this->getAccessibleMock(\TYPO3\CMS\Install\FolderStructure\AbstractNode::class, ['dummy'], [], '', false);
55  $children = ['1234'];
56  $node->_set('children', $children);
57  $this->assertSame($children, $node->_call('getChildren'));
58  }
59 
63  public function getParentReturnsSetParent()
64  {
66  $node = $this->getAccessibleMock(\TYPO3\CMS\Install\FolderStructure\AbstractNode::class, ['dummy'], [], '', false);
67  $parent = $this->createMock(\TYPO3\CMS\Install\FolderStructure\RootNodeInterface::class);
68  $node->_set('parent', $parent);
69  $this->assertSame($parent, $node->_call('getParent'));
70  }
71 
75  public function getAbsolutePathCallsParentForPathAndAppendsOwnName()
76  {
78  $node = $this->getAccessibleMock(\TYPO3\CMS\Install\FolderStructure\AbstractNode::class, ['dummy'], [], '', false);
79  $parent = $this->createMock(\TYPO3\CMS\Install\FolderStructure\RootNodeInterface::class);
80  $parentPath = '/foo/bar';
81  $parent->expects($this->once())->method('getAbsolutePath')->will($this->returnValue($parentPath));
82  $name = $this->getUniqueId('test_');
83  $node->_set('parent', $parent);
84  $node->_set('name', $name);
85  $this->assertSame($parentPath . '/' . $name, $node->getAbsolutePath());
86  }
87 
91  public function isWritableCallsParentIsWritable()
92  {
94  $node = $this->getAccessibleMock(\TYPO3\CMS\Install\FolderStructure\AbstractNode::class, ['dummy'], [], '', false);
95  $parentMock = $this->createMock(\TYPO3\CMS\Install\FolderStructure\NodeInterface::class);
96  $parentMock->expects($this->once())->method('isWritable');
97  $node->_set('parent', $parentMock);
98  $node->isWritable();
99  }
100 
104  public function isWritableReturnsWritableStatusOfParent()
105  {
107  $node = $this->getAccessibleMock(\TYPO3\CMS\Install\FolderStructure\AbstractNode::class, ['dummy'], [], '', false);
108  $parentMock = $this->createMock(\TYPO3\CMS\Install\FolderStructure\NodeInterface::class);
109  $parentMock->expects($this->once())->method('isWritable')->will($this->returnValue(true));
110  $node->_set('parent', $parentMock);
111  $this->assertTrue($node->isWritable());
112  }
113 
117  public function existsReturnsTrueIfNodeExists()
118  {
120  $node = $this->getAccessibleMock(\TYPO3\CMS\Install\FolderStructure\AbstractNode::class, ['getAbsolutePath'], [], '', false);
121  $path = $this->getVirtualTestDir('dir_');
122  $node->expects($this->any())->method('getAbsolutePath')->will($this->returnValue($path));
123  $this->assertTrue($node->_call('exists'));
124  }
125 
130  public function existsReturnsTrueIfIsLinkAndTargetIsDead()
131  {
133  $node = $this->getAccessibleMock(\TYPO3\CMS\Install\FolderStructure\AbstractNode::class, ['getAbsolutePath'], [], '', false);
134  $path = PATH_site . 'typo3temp/var/tests/' . $this->getUniqueId('link_');
135  $target = PATH_site . 'typo3temp/var/tests/' . $this->getUniqueId('notExists_');
136  touch($target);
137  symlink($target, $path);
138  unlink($target);
139  $this->testFilesToDelete[] = $path;
140  $node->expects($this->any())->method('getAbsolutePath')->will($this->returnValue($path));
141  $this->assertTrue($node->_call('exists'));
142  }
143 
147  public function existsReturnsFalseIfNodeNotExists()
148  {
150  $node = $this->getAccessibleMock(\TYPO3\CMS\Install\FolderStructure\AbstractNode::class, ['getAbsolutePath'], [], '', false);
151  $path = $this->getVirtualTestFilePath('dir_');
152  $node->expects($this->any())->method('getAbsolutePath')->will($this->returnValue($path));
153  $this->assertFalse($node->_call('exists'));
154  }
155 
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  $this->expectException(Exception::class);
170  $this->expectExceptionCode(1366744035);
171  $node->expects($this->any())->method('getAbsolutePath')->will($this->returnValue(''));
172  $node->expects($this->once())->method('isPermissionCorrect')->will($this->returnValue(true));
173  $node->_call('fixPermission');
174  }
175 
179  public function fixPermissionReturnsNoticeStatusIfPermissionCanNotBeChanged()
180  {
181  if (function_exists('posix_getegid') && posix_getegid() === 0) {
182  $this->markTestSkipped('Test skipped if run on linux as root');
183  }
185  $node = $this->getAccessibleMock(
186  \TYPO3\CMS\Install\FolderStructure\AbstractNode::class,
187  ['isPermissionCorrect', 'getRelativePathBelowSiteRoot', 'getAbsolutePath'],
188  [],
189  '',
190  false
191  );
192  $node->expects($this->any())->method('getRelativePathBelowSiteRoot')->will($this->returnValue(''));
193  $node->expects($this->once())->method('isPermissionCorrect')->will($this->returnValue(false));
194  $path = $this->getVirtualTestDir('root_');
195  $subPath = $path . '/' . $this->getUniqueId('dir_');
196  mkdir($subPath);
197  chmod($path, 02000);
198  $node->expects($this->any())->method('getAbsolutePath')->will($this->returnValue($subPath));
199  $node->_set('targetPermission', '2770');
200  $this->assertInstanceOf(\TYPO3\CMS\Install\Status\NoticeStatus::class, $node->_call('fixPermission'));
201  chmod($path, 02770);
202  }
203 
207  public function fixPermissionReturnsNoticeStatusIfPermissionsCanNotBeChanged()
208  {
209  if (function_exists('posix_getegid') && posix_getegid() === 0) {
210  $this->markTestSkipped('Test skipped if run on linux as root');
211  }
213  $node = $this->getAccessibleMock(
214  \TYPO3\CMS\Install\FolderStructure\AbstractNode::class,
215  ['isPermissionCorrect', 'getRelativePathBelowSiteRoot', 'getAbsolutePath'],
216  [],
217  '',
218  false
219  );
220  $node->expects($this->any())->method('getRelativePathBelowSiteRoot')->will($this->returnValue(''));
221  $node->expects($this->once())->method('isPermissionCorrect')->will($this->returnValue(false));
222  $path = $this->getVirtualTestDir('root_');
223  $subPath = $path . '/' . $this->getUniqueId('dir_');
224  mkdir($subPath);
225  chmod($path, 02000);
226  $node->expects($this->any())->method('getAbsolutePath')->will($this->returnValue($subPath));
227  $node->_set('targetPermission', '2770');
228  $this->assertInstanceOf(\TYPO3\CMS\Install\Status\NoticeStatus::class, $node->_call('fixPermission'));
229  chmod($path, 02770);
230  }
231 
235  public function fixPermissionReturnsOkStatusIfPermissionCanBeFixedAndSetsPermissionToCorrectValue()
236  {
238  $node = $this->getAccessibleMock(
239  \TYPO3\CMS\Install\FolderStructure\AbstractNode::class,
240  ['isPermissionCorrect', 'getRelativePathBelowSiteRoot', 'getAbsolutePath'],
241  [],
242  '',
243  false
244  );
245  $node->expects($this->any())->method('getRelativePathBelowSiteRoot')->will($this->returnValue(''));
246  $node->expects($this->once())->method('isPermissionCorrect')->will($this->returnValue(false));
247  $path = $this->getVirtualTestDir('root_');
248  $subPath = $path . '/' . $this->getUniqueId('dir_');
249  mkdir($subPath);
250  chmod($path, 02770);
251  $node->_set('targetPermission', '2770');
252  $node->expects($this->any())->method('getAbsolutePath')->will($this->returnValue($subPath));
253  $this->assertInstanceOf(\TYPO3\CMS\Install\Status\OkStatus::class, $node->_call('fixPermission'));
254  $resultDirectoryPermissions = substr(decoct(fileperms($subPath)), 1);
255  $this->assertSame('2770', $resultDirectoryPermissions);
256  }
257 
261  public function isPermissionCorrectReturnsTrueOnWindowsOs()
262  {
264  $node = $this->getAccessibleMock(\TYPO3\CMS\Install\FolderStructure\AbstractNode::class, ['isWindowsOs'], [], '', false);
265  $node->expects($this->once())->method('isWindowsOs')->will($this->returnValue(true));
266  $this->assertTrue($node->_call('isPermissionCorrect'));
267  }
268 
272  public function isPermissionCorrectReturnsFalseIfTargetPermissionAndCurrentPermissionAreNotIdentical()
273  {
275  $node = $this->getAccessibleMock(\TYPO3\CMS\Install\FolderStructure\AbstractNode::class, ['isWindowsOs', 'getCurrentPermission'], [], '', false);
276  $node->expects($this->any())->method('isWindowsOs')->will($this->returnValue(false));
277  $node->expects($this->any())->method('getCurrentPermission')->will($this->returnValue('foo'));
278  $node->_set('targetPermission', 'bar');
279  $this->assertFalse($node->_call('isPermissionCorrect'));
280  }
281 
285  public function getCurrentPermissionReturnsCurrentDirectoryPermission()
286  {
288  $node = $this->getAccessibleMock(\TYPO3\CMS\Install\FolderStructure\AbstractNode::class, ['getAbsolutePath'], [], '', false);
289  $path = $this->getVirtualTestDir('dir_');
290  chmod($path, 02775);
291  clearstatcache();
292  $node->expects($this->any())->method('getAbsolutePath')->will($this->returnValue($path));
293  $this->assertSame('2775', $node->_call('getCurrentPermission'));
294  }
295 
299  public function getCurrentPermissionReturnsCurrentFilePermission()
300  {
302  $node = $this->getAccessibleMock(\TYPO3\CMS\Install\FolderStructure\AbstractNode::class, ['getAbsolutePath'], [], '', false);
303  $file = $this->getVirtualTestFilePath('file_');
304  touch($file);
305  chmod($file, 0770);
306  clearstatcache();
307  $node->expects($this->any())->method('getAbsolutePath')->will($this->returnValue($file));
308  $this->assertSame('0770', $node->_call('getCurrentPermission'));
309  }
310 
314  public function getRelativePathBelowSiteRootThrowsExceptionIfGivenPathIsNotBelowPathSiteConstant()
315  {
316  $this->expectException(InvalidArgumentException::class);
317  $this->expectExceptionCode(1366398198);
319  $node = $this->getAccessibleMock(\TYPO3\CMS\Install\FolderStructure\AbstractNode::class, ['dummy'], [], '', false);
320  $node->_call('getRelativePathBelowSiteRoot', '/tmp');
321  }
322 
326  public function getRelativePathCallsGetAbsolutePathIfPathIsNull()
327  {
329  $node = $this->getAccessibleMock(
330  \TYPO3\CMS\Install\FolderStructure\AbstractNode::class,
331  ['getAbsolutePath'],
332  [],
333  '',
334  false
335  );
336  $node->expects($this->once())->method('getAbsolutePath')->will($this->returnValue(PATH_site));
337  $node->_call('getRelativePathBelowSiteRoot', null);
338  }
339 
343  public function getRelativePathBelowSiteRootReturnsSingleForwardSlashIfGivenPathEqualsPathSiteConstant()
344  {
346  $node = $this->getAccessibleMock(\TYPO3\CMS\Install\FolderStructure\AbstractNode::class, ['dummy'], [], '', false);
347  $result = $node->_call('getRelativePathBelowSiteRoot', PATH_site);
348  $this->assertSame('/', $result);
349  }
350 
354  public function getRelativePathBelowSiteRootReturnsSubPath()
355  {
357  $node = $this->getAccessibleMock(\TYPO3\CMS\Install\FolderStructure\AbstractNode::class, ['dummy'], [], '', false);
358  $result = $node->_call('getRelativePathBelowSiteRoot', PATH_site . 'foo/bar');
359  $this->assertSame('/foo/bar', $result);
360  }
361 }