‪TYPO3CMS  ‪main
LinkNodeTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
28 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
29 
30 final class ‪LinkNodeTest extends UnitTestCase
31 {
32  protected string ‪$testRoot;
33 
34  public function ‪setUp(): void
35  {
36  parent::setUp();
37  // do not use var path here, as link nodes get checked for public path as first part
38  $this->testRoot = ‪Environment::getPublicPath() . '/typo3temp/tests/';
39  $this->testFilesToDelete[] = ‪$this->testRoot;
40  ‪GeneralUtility::mkdir_deep($this->testRoot);
41  }
42 
47  {
48  $this->expectException(InvalidArgumentException::class);
49  $this->expectExceptionCode(1380485700);
50  $node = $this->getAccessibleMock(LinkNode::class, null, [], '', false);
51  $node->__construct([], null);
52  }
53 
58  {
59  $this->expectException(InvalidArgumentException::class);
60  $this->expectExceptionCode(1380546061);
61  $parent = $this->createMock(NodeInterface::class);
62  $node = $this->getAccessibleMock(LinkNode::class, null, [], '', false);
63  $structure = [
64  'name' => 'foo/bar',
65  ];
66  $node->__construct($structure, $parent);
67  }
68 
72  public function ‪constructorSetsParent(): void
73  {
74  $parent = $this->createMock(NodeInterface::class);
75  $node = $this->getAccessibleMock(LinkNode::class, null, [], '', false);
76  $structure = [
77  'name' => 'foo',
78  ];
79  $node->__construct($structure, $parent);
80  self::assertSame($parent, $node->_call('getParent'));
81  }
82 
86  public function ‪constructorSetsName(): void
87  {
88  $node = $this->getAccessibleMock(LinkNode::class, null, [], '', false);
89  $parent = $this->createMock(RootNodeInterface::class);
90  $name = ‪StringUtility::getUniqueId('test_');
91  $node->__construct(['name' => $name], $parent);
92  self::assertSame($name, $node->getName());
93  }
94 
98  public function ‪constructorSetsNameAndTarget(): void
99  {
100  $node = $this->getAccessibleMock(LinkNode::class, null, [], '', false);
101  $parent = $this->createMock(RootNodeInterface::class);
102  $name = ‪StringUtility::getUniqueId('test_');
103  $target = '../' . ‪StringUtility::getUniqueId('test_');
104  $node->__construct(['name' => $name, 'target' => $target], $parent);
105  self::assertSame($target, $node->_call('getTarget'));
106  }
107 
111  public function ‪getStatusReturnsArray(): void
112  {
113  $node = $this->getAccessibleMock(
114  LinkNode::class,
115  ['isWindowsOs', 'getAbsolutePath', 'exists'],
116  [],
117  '',
118  false
119  );
120  $path = $this->testRoot . ‪StringUtility::getUniqueId('dir_');
121  $node->method('getAbsolutePath')->willReturn($path);
122  self::assertIsArray($node->getStatus());
123  }
124 
129  {
130  $node = $this->getAccessibleMock(
131  LinkNode::class,
132  ['isWindowsOs', 'getAbsolutePath', 'exists'],
133  [],
134  '',
135  false
136  );
137  $path = $this->testRoot . ‪StringUtility::getUniqueId('dir_');
138  $node->method('getAbsolutePath')->willReturn($path);
139  $node->expects(self::once())->method('isWindowsOs')->willReturn(true);
140  $statusArray = $node->getStatus();
141  self::assertSame(ContextualFeedbackSeverity::INFO, $statusArray[0]->getSeverity());
142  }
143 
148  {
149  $node = $this->getAccessibleMock(
150  LinkNode::class,
151  ['isWindowsOs', 'getAbsolutePath', 'exists'],
152  [],
153  '',
154  false
155  );
156  $path = $this->testRoot . ‪StringUtility::getUniqueId('dir_');
157  $node->method('getAbsolutePath')->willReturn($path);
158  $node->method('isWindowsOs')->willReturn(false);
159  $node->expects(self::once())->method('exists')->willReturn(false);
160  $statusArray = $node->getStatus();
161  self::assertSame(ContextualFeedbackSeverity::ERROR, $statusArray[0]->getSeverity());
162  }
163 
168  {
169  $node = $this->getAccessibleMock(
170  LinkNode::class,
171  ['isWindowsOs', 'getAbsolutePath', 'exists', 'isLink', 'getRelativePathBelowSiteRoot'],
172  [],
173  '',
174  false
175  );
176  $node->method('getAbsolutePath')->willReturn('');
177  $node->method('exists')->willReturn(true);
178  $node->expects(self::once())->method('isLink')->willReturn(false);
179  $statusArray = $node->getStatus();
180  self::assertSame(ContextualFeedbackSeverity::WARNING, $statusArray[0]->getSeverity());
181  }
182 
187  {
188  $node = $this->getAccessibleMock(
189  LinkNode::class,
190  ['isWindowsOs', 'getAbsolutePath', 'exists', 'isLink', 'isTargetCorrect', 'getCurrentTarget', 'getRelativePathBelowSiteRoot'],
191  [],
192  '',
193  false
194  );
195  $node->method('getAbsolutePath')->willReturn('');
196  $node->method('getCurrentTarget')->willReturn('');
197  $node->method('exists')->willReturn(true);
198  $node->method('isLink')->willReturn(true);
199  $node->expects(self::once())->method('isLink')->willReturn(false);
200  $statusArray = $node->getStatus();
201  self::assertSame(ContextualFeedbackSeverity::ERROR, $statusArray[0]->getSeverity());
202  }
203 
208  {
209  $node = $this->getAccessibleMock(
210  LinkNode::class,
211  ['isWindowsOs', 'getAbsolutePath', 'exists', 'isLink', 'isTargetCorrect', 'getRelativePathBelowSiteRoot'],
212  [],
213  '',
214  false
215  );
216  $node->method('getAbsolutePath')->willReturn('');
217  $node->method('exists')->willReturn(true);
218  $node->expects(self::once())->method('isLink')->willReturn(true);
219  $node->expects(self::once())->method('isTargetCorrect')->willReturn(true);
220  $node->expects(self::once())->method('getRelativePathBelowSiteRoot')->willReturn('');
221  $statusArray = $node->getStatus();
222  self::assertSame(ContextualFeedbackSeverity::OK, $statusArray[0]->getSeverity());
223  }
224 
228  public function ‪fixReturnsEmptyArray(): void
229  {
230  $node = $this->getAccessibleMock(
231  LinkNode::class,
232  ['getRelativePathBelowSiteRoot'],
233  [],
234  '',
235  false
236  );
237  $statusArray = $node->fix();
238  self::assertEmpty($statusArray);
239  }
240 
245  {
246  $this->expectException(InvalidArgumentException::class);
247  $this->expectExceptionCode(1380556246);
248  $node = $this->getAccessibleMock(LinkNode::class, ['exists'], [], '', false);
249  $node->expects(self::once())->method('exists')->willReturn(false);
250  self::assertFalse($node->_call('isLink'));
251  }
252 
256  public function ‪isLinkReturnsTrueIfNameIsLink(): void
257  {
258  $node = $this->getAccessibleMock(LinkNode::class, ['exists', 'getAbsolutePath'], [], '', false);
259  $path = $this->testRoot . ‪StringUtility::getUniqueId('link_');
260  $target = $this->testRoot . ‪StringUtility::getUniqueId('linkTarget_');
261  touch($target);
262  symlink($target, $path);
263  $node->method('exists')->willReturn(true);
264  $node->method('getAbsolutePath')->willReturn($path);
265  self::assertTrue($node->_call('isLink'));
266  }
267 
271  public function ‪isFileReturnsFalseIfNameIsAFile(): void
272  {
273  $node = $this->getAccessibleMock(LinkNode::class, ['exists', 'getAbsolutePath'], [], '', false);
274  $path = $this->testRoot . ‪StringUtility::getUniqueId('file_');
275  touch($path);
276  $node->method('exists')->willReturn(true);
277  $node->method('getAbsolutePath')->willReturn($path);
278  self::assertFalse($node->_call('isLink'));
279  }
280 
285  {
286  $this->expectException(InvalidArgumentException::class);
287  $this->expectExceptionCode(1380556245);
288  $node = $this->getAccessibleMock(LinkNode::class, ['exists'], [], '', false);
289  $node->expects(self::once())->method('exists')->willReturn(false);
290  self::assertFalse($node->_call('isTargetCorrect'));
291  }
292 
297  {
298  $this->expectException(InvalidArgumentException::class);
299  $this->expectExceptionCode(1380556247);
300  $node = $this->getAccessibleMock(LinkNode::class, ['exists', 'isLink', 'getTarget'], [], '', false);
301  $node->method('exists')->willReturn(true);
302  $node->expects(self::once())->method('isLink')->willReturn(false);
303  self::assertTrue($node->_call('isTargetCorrect'));
304  }
305 
310  {
311  $node = $this->getAccessibleMock(LinkNode::class, ['exists', 'isLink', 'getTarget'], [], '', false);
312  $node->method('exists')->willReturn(true);
313  $node->method('isLink')->willReturn(true);
314  $node->expects(self::once())->method('getTarget')->willReturn('');
315  self::assertTrue($node->_call('isTargetCorrect'));
316  }
317 
322  {
323  $node = $this->getAccessibleMock(LinkNode::class, ['exists', 'isLink', 'getCurrentTarget', 'getTarget'], [], '', false);
324  $node->method('exists')->willReturn(true);
325  $node->method('isLink')->willReturn(true);
326  $node->expects(self::once())->method('getCurrentTarget')->willReturn('someLinkTarget/');
327  $node->expects(self::once())->method('getTarget')->willReturn('someLinkTarget');
328  self::assertTrue($node->_call('isTargetCorrect'));
329  }
330 
335  {
336  $path = $this->testRoot . ‪StringUtility::getUniqueId('link_');
337  $target = $this->testRoot . ‪StringUtility::getUniqueId('linkTarget_');
338  touch($target);
339  symlink($target, $path);
340  $node = $this->getAccessibleMock(
341  LinkNode::class,
342  ['exists', 'isLink', 'getTarget', 'getAbsolutePath'],
343  [],
344  '',
345  false
346  );
347  $node->method('exists')->willReturn(true);
348  $node->method('isLink')->willReturn(true);
349  $node->expects(self::once())->method('getTarget')->willReturn(str_replace('/', DIRECTORY_SEPARATOR, $target));
350  $node->expects(self::once())->method('getAbsolutePath')->willReturn($path);
351  self::assertTrue($node->_call('isTargetCorrect'));
352  }
353 
358  {
359  $path = $this->testRoot . ‪StringUtility::getUniqueId('link_');
360  $target = $this->testRoot . ‪StringUtility::getUniqueId('linkTarget_');
361  touch($target);
362  symlink($target, $path);
363  $node = $this->getAccessibleMock(
364  LinkNode::class,
365  ['exists', 'isLink', 'getTarget', 'getAbsolutePath'],
366  [],
367  '',
368  false
369  );
370  $node->method('exists')->willReturn(true);
371  $node->method('isLink')->willReturn(true);
372  $node->expects(self::once())->method('getTarget')->willReturn('foo');
373  $node->expects(self::once())->method('getAbsolutePath')->willReturn($path);
374  self::assertFalse($node->_call('isTargetCorrect'));
375  }
376 }
‪TYPO3\CMS\Core\Core\Environment\getPublicPath
‪static getPublicPath()
Definition: Environment.php:187
‪TYPO3\CMS\Install\FolderStructure\RootNodeInterface
Definition: RootNodeInterface.php:21
‪TYPO3\CMS\Core\Utility\GeneralUtility\mkdir_deep
‪static mkdir_deep(string $directory)
Definition: GeneralUtility.php:1650
‪TYPO3\CMS\Core\Type\ContextualFeedbackSeverity
‪ContextualFeedbackSeverity
Definition: ContextualFeedbackSeverity.php:25
‪TYPO3\CMS\Install\FolderStructure\Exception\InvalidArgumentException
Definition: InvalidArgumentException.php:23
‪TYPO3\CMS\Install\Tests\Unit\FolderStructure
Definition: AbstractFolderStructureTestCase.php:18
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:41
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\Utility\StringUtility
Definition: StringUtility.php:24
‪TYPO3\CMS\Install\FolderStructure\NodeInterface
Definition: NodeInterface.php:24
‪TYPO3\CMS\Core\Utility\StringUtility\getUniqueId
‪static getUniqueId(string $prefix='')
Definition: StringUtility.php:57