‪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 
20 use PHPUnit\Framework\Attributes\Test;
29 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
30 
31 final class ‪LinkNodeTest extends UnitTestCase
32 {
33  protected string ‪$testRoot;
34 
35  public function ‪setUp(): void
36  {
37  parent::setUp();
38  // do not use var path here, as link nodes get checked for public path as first part
39  $this->testRoot = ‪Environment::getPublicPath() . '/typo3temp/tests/';
40  $this->testFilesToDelete[] = ‪$this->testRoot;
41  ‪GeneralUtility::mkdir_deep($this->testRoot);
42  }
43 
44  #[Test]
46  {
47  $this->expectException(InvalidArgumentException::class);
48  $this->expectExceptionCode(1380485700);
49  $node = $this->getAccessibleMock(LinkNode::class, null, [], '', false);
50  $node->__construct([], null);
51  }
52 
53  #[Test]
55  {
56  $this->expectException(InvalidArgumentException::class);
57  $this->expectExceptionCode(1380546061);
58  $parent = $this->createMock(NodeInterface::class);
59  $node = $this->getAccessibleMock(LinkNode::class, null, [], '', false);
60  $structure = [
61  'name' => 'foo/bar',
62  ];
63  $node->__construct($structure, $parent);
64  }
65 
66  #[Test]
67  public function ‪constructorSetsParent(): void
68  {
69  $parent = $this->createMock(NodeInterface::class);
70  $node = $this->getAccessibleMock(LinkNode::class, null, [], '', false);
71  $structure = [
72  'name' => 'foo',
73  ];
74  $node->__construct($structure, $parent);
75  self::assertSame($parent, $node->_call('getParent'));
76  }
77 
78  #[Test]
79  public function ‪constructorSetsName(): void
80  {
81  $node = $this->getAccessibleMock(LinkNode::class, null, [], '', false);
82  $parent = $this->createMock(RootNodeInterface::class);
83  $name = ‪StringUtility::getUniqueId('test_');
84  $node->__construct(['name' => $name], $parent);
85  self::assertSame($name, $node->getName());
86  }
87 
88  #[Test]
89  public function ‪constructorSetsNameAndTarget(): void
90  {
91  $node = $this->getAccessibleMock(LinkNode::class, null, [], '', false);
92  $parent = $this->createMock(RootNodeInterface::class);
93  $name = ‪StringUtility::getUniqueId('test_');
94  $target = '../' . ‪StringUtility::getUniqueId('test_');
95  $node->__construct(['name' => $name, 'target' => $target], $parent);
96  self::assertSame($target, $node->_call('getTarget'));
97  }
98 
99  #[Test]
100  public function ‪getStatusReturnsArray(): void
101  {
102  $node = $this->getAccessibleMock(
103  LinkNode::class,
104  ['isWindowsOs', 'getAbsolutePath', 'exists'],
105  [],
106  '',
107  false
108  );
109  $path = $this->testRoot . ‪StringUtility::getUniqueId('dir_');
110  $node->method('getAbsolutePath')->willReturn($path);
111  self::assertIsArray($node->getStatus());
112  }
113 
114  #[Test]
116  {
117  $node = $this->getAccessibleMock(
118  LinkNode::class,
119  ['isWindowsOs', 'getAbsolutePath', 'exists'],
120  [],
121  '',
122  false
123  );
124  $path = $this->testRoot . ‪StringUtility::getUniqueId('dir_');
125  $node->method('getAbsolutePath')->willReturn($path);
126  $node->expects(self::once())->method('isWindowsOs')->willReturn(true);
127  $statusArray = $node->getStatus();
128  self::assertSame(ContextualFeedbackSeverity::INFO, $statusArray[0]->getSeverity());
129  }
130 
131  #[Test]
133  {
134  $node = $this->getAccessibleMock(
135  LinkNode::class,
136  ['isWindowsOs', 'getAbsolutePath', 'exists'],
137  [],
138  '',
139  false
140  );
141  $path = $this->testRoot . ‪StringUtility::getUniqueId('dir_');
142  $node->method('getAbsolutePath')->willReturn($path);
143  $node->method('isWindowsOs')->willReturn(false);
144  $node->expects(self::once())->method('exists')->willReturn(false);
145  $statusArray = $node->getStatus();
146  self::assertSame(ContextualFeedbackSeverity::ERROR, $statusArray[0]->getSeverity());
147  }
148 
149  #[Test]
151  {
152  $node = $this->getAccessibleMock(
153  LinkNode::class,
154  ['isWindowsOs', 'getAbsolutePath', 'exists', 'isLink', 'getRelativePathBelowSiteRoot'],
155  [],
156  '',
157  false
158  );
159  $node->method('getAbsolutePath')->willReturn('');
160  $node->method('exists')->willReturn(true);
161  $node->expects(self::once())->method('isLink')->willReturn(false);
162  $statusArray = $node->getStatus();
163  self::assertSame(ContextualFeedbackSeverity::WARNING, $statusArray[0]->getSeverity());
164  }
165 
166  #[Test]
168  {
169  $node = $this->getAccessibleMock(
170  LinkNode::class,
171  ['isWindowsOs', 'getAbsolutePath', 'exists', 'isLink', 'isTargetCorrect', 'getCurrentTarget', 'getRelativePathBelowSiteRoot'],
172  [],
173  '',
174  false
175  );
176  $node->method('getAbsolutePath')->willReturn('');
177  $node->method('getCurrentTarget')->willReturn('');
178  $node->method('exists')->willReturn(true);
179  $node->method('isLink')->willReturn(true);
180  $node->expects(self::once())->method('isLink')->willReturn(false);
181  $statusArray = $node->getStatus();
182  self::assertSame(ContextualFeedbackSeverity::ERROR, $statusArray[0]->getSeverity());
183  }
184 
185  #[Test]
187  {
188  $node = $this->getAccessibleMock(
189  LinkNode::class,
190  ['isWindowsOs', 'getAbsolutePath', 'exists', 'isLink', 'isTargetCorrect', 'getRelativePathBelowSiteRoot'],
191  [],
192  '',
193  false
194  );
195  $node->method('getAbsolutePath')->willReturn('');
196  $node->method('exists')->willReturn(true);
197  $node->expects(self::once())->method('isLink')->willReturn(true);
198  $node->expects(self::once())->method('isTargetCorrect')->willReturn(true);
199  $node->expects(self::once())->method('getRelativePathBelowSiteRoot')->willReturn('');
200  $statusArray = $node->getStatus();
201  self::assertSame(ContextualFeedbackSeverity::OK, $statusArray[0]->getSeverity());
202  }
203 
204  #[Test]
205  public function ‪fixReturnsEmptyArray(): void
206  {
207  $node = $this->getAccessibleMock(
208  LinkNode::class,
209  ['getRelativePathBelowSiteRoot'],
210  [],
211  '',
212  false
213  );
214  $statusArray = $node->fix();
215  self::assertEmpty($statusArray);
216  }
217 
218  #[Test]
220  {
221  $this->expectException(InvalidArgumentException::class);
222  $this->expectExceptionCode(1380556246);
223  $node = $this->getAccessibleMock(LinkNode::class, ['exists'], [], '', false);
224  $node->expects(self::once())->method('exists')->willReturn(false);
225  self::assertFalse($node->_call('isLink'));
226  }
227 
228  #[Test]
229  public function ‪isLinkReturnsTrueIfNameIsLink(): void
230  {
231  $node = $this->getAccessibleMock(LinkNode::class, ['exists', 'getAbsolutePath'], [], '', false);
232  $path = $this->testRoot . ‪StringUtility::getUniqueId('link_');
233  $target = $this->testRoot . ‪StringUtility::getUniqueId('linkTarget_');
234  touch($target);
235  symlink($target, $path);
236  $node->method('exists')->willReturn(true);
237  $node->method('getAbsolutePath')->willReturn($path);
238  self::assertTrue($node->_call('isLink'));
239  }
240 
241  #[Test]
242  public function ‪isFileReturnsFalseIfNameIsAFile(): void
243  {
244  $node = $this->getAccessibleMock(LinkNode::class, ['exists', 'getAbsolutePath'], [], '', false);
245  $path = $this->testRoot . ‪StringUtility::getUniqueId('file_');
246  touch($path);
247  $node->method('exists')->willReturn(true);
248  $node->method('getAbsolutePath')->willReturn($path);
249  self::assertFalse($node->_call('isLink'));
250  }
251 
252  #[Test]
254  {
255  $this->expectException(InvalidArgumentException::class);
256  $this->expectExceptionCode(1380556245);
257  $node = $this->getAccessibleMock(LinkNode::class, ['exists'], [], '', false);
258  $node->expects(self::once())->method('exists')->willReturn(false);
259  self::assertFalse($node->_call('isTargetCorrect'));
260  }
261 
262  #[Test]
264  {
265  $this->expectException(InvalidArgumentException::class);
266  $this->expectExceptionCode(1380556247);
267  $node = $this->getAccessibleMock(LinkNode::class, ['exists', 'isLink', 'getTarget'], [], '', false);
268  $node->method('exists')->willReturn(true);
269  $node->expects(self::once())->method('isLink')->willReturn(false);
270  self::assertTrue($node->_call('isTargetCorrect'));
271  }
272 
273  #[Test]
275  {
276  $node = $this->getAccessibleMock(LinkNode::class, ['exists', 'isLink', 'getTarget'], [], '', false);
277  $node->method('exists')->willReturn(true);
278  $node->method('isLink')->willReturn(true);
279  $node->expects(self::once())->method('getTarget')->willReturn('');
280  self::assertTrue($node->_call('isTargetCorrect'));
281  }
282 
283  #[Test]
285  {
286  $node = $this->getAccessibleMock(LinkNode::class, ['exists', 'isLink', 'getCurrentTarget', 'getTarget'], [], '', false);
287  $node->method('exists')->willReturn(true);
288  $node->method('isLink')->willReturn(true);
289  $node->expects(self::once())->method('getCurrentTarget')->willReturn('someLinkTarget/');
290  $node->expects(self::once())->method('getTarget')->willReturn('someLinkTarget');
291  self::assertTrue($node->_call('isTargetCorrect'));
292  }
293 
294  #[Test]
296  {
297  $path = $this->testRoot . ‪StringUtility::getUniqueId('link_');
298  $target = $this->testRoot . ‪StringUtility::getUniqueId('linkTarget_');
299  touch($target);
300  symlink($target, $path);
301  $node = $this->getAccessibleMock(
302  LinkNode::class,
303  ['exists', 'isLink', 'getTarget', 'getAbsolutePath'],
304  [],
305  '',
306  false
307  );
308  $node->method('exists')->willReturn(true);
309  $node->method('isLink')->willReturn(true);
310  $node->expects(self::once())->method('getTarget')->willReturn(str_replace('/', DIRECTORY_SEPARATOR, $target));
311  $node->expects(self::once())->method('getAbsolutePath')->willReturn($path);
312  self::assertTrue($node->_call('isTargetCorrect'));
313  }
314 
315  #[Test]
317  {
318  $path = $this->testRoot . ‪StringUtility::getUniqueId('link_');
319  $target = $this->testRoot . ‪StringUtility::getUniqueId('linkTarget_');
320  touch($target);
321  symlink($target, $path);
322  $node = $this->getAccessibleMock(
323  LinkNode::class,
324  ['exists', 'isLink', 'getTarget', 'getAbsolutePath'],
325  [],
326  '',
327  false
328  );
329  $node->method('exists')->willReturn(true);
330  $node->method('isLink')->willReturn(true);
331  $node->expects(self::once())->method('getTarget')->willReturn('foo');
332  $node->expects(self::once())->method('getAbsolutePath')->willReturn($path);
333  self::assertFalse($node->_call('isTargetCorrect'));
334  }
335 }
‪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:1654
‪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