‪TYPO3CMS  10.4
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 
27 use TYPO3\TestingFramework\Core\AccessibleObjectInterface;
28 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
29 
33 class ‪LinkNodeTest extends UnitTestCase
34 {
39  {
40  $this->expectException(InvalidArgumentException::class);
41  $this->expectExceptionCode(1380485700);
43  $node = $this->getAccessibleMock(LinkNode::class, ['dummy'], [], '', false);
44  $node->__construct([], null);
45  }
46 
51  {
52  $this->expectException(InvalidArgumentException::class);
53  $this->expectExceptionCode(1380546061);
54  $parent = $this->createMock(NodeInterface::class);
56  $node = $this->getAccessibleMock(LinkNode::class, ['dummy'], [], '', false);
57  $structure = [
58  'name' => 'foo/bar',
59  ];
60  $node->__construct($structure, $parent);
61  }
62 
66  public function ‪constructorSetsParent()
67  {
68  $parent = $this->createMock(NodeInterface::class);
70  $node = $this->getAccessibleMock(LinkNode::class, ['dummy'], [], '', false);
71  $structure = [
72  'name' => 'foo',
73  ];
74  $node->__construct($structure, $parent);
75  self::assertSame($parent, $node->_call('getParent'));
76  }
77 
81  public function ‪constructorSetsName()
82  {
84  $node = $this->getAccessibleMock(LinkNode::class, ['dummy'], [], '', false);
85  $parent = $this->createMock(RootNodeInterface::class);
86  $name = ‪StringUtility::getUniqueId('test_');
87  $node->__construct(['name' => $name], $parent);
88  self::assertSame($name, $node->getName());
89  }
90 
95  {
97  $node = $this->getAccessibleMock(LinkNode::class, ['dummy'], [], '', false);
98  $parent = $this->createMock(RootNodeInterface::class);
99  $name = ‪StringUtility::getUniqueId('test_');
100  $target = '../' . ‪StringUtility::getUniqueId('test_');
101  $node->__construct(['name' => $name, 'target' => $target], $parent);
102  self::assertSame($target, $node->_call('getTarget'));
103  }
104 
108  public function ‪getStatusReturnsArray()
109  {
111  $node = $this->getAccessibleMock(
112  LinkNode::class,
113  ['isWindowsOs', 'getAbsolutePath', 'exists'],
114  [],
115  '',
116  false
117  );
118  // do not use var path here, as link nodes get checked for public path as first part
119  $path = ‪Environment::getPublicPath() . '/typo3temp/tests/' . ‪StringUtility::getUniqueId('dir_');
120  $node->expects(self::any())->method('getAbsolutePath')->willReturn($path);
121  self::assertIsArray($node->getStatus());
122  }
123 
128  {
130  $node = $this->getAccessibleMock(
131  LinkNode::class,
132  ['isWindowsOs', 'getAbsolutePath', 'exists'],
133  [],
134  '',
135  false
136  );
137  // do not use var path here, as link nodes get checked for public path as first part
138  $path = ‪Environment::getPublicPath() . '/tests/' . ‪StringUtility::getUniqueId('dir_');
139  $node->expects(self::any())->method('getAbsolutePath')->willReturn($path);
140  $node->expects(self::once())->method('isWindowsOs')->willReturn(true);
141  $statusArray = $node->getStatus();
142  self::assertSame(‪FlashMessage::INFO, $statusArray[0]->getSeverity());
143  }
144 
149  {
151  $node = $this->getAccessibleMock(
152  LinkNode::class,
153  ['isWindowsOs', 'getAbsolutePath', 'exists'],
154  [],
155  '',
156  false
157  );
158  // do not use var path here, as link nodes get checked for public path as first part
159  $path = ‪Environment::getPublicPath() . '/tests/' . ‪StringUtility::getUniqueId('dir_');
160  $node->expects(self::any())->method('getAbsolutePath')->willReturn($path);
161  $node->expects(self::any())->method('isWindowsOs')->willReturn(false);
162  $node->expects(self::once())->method('exists')->willReturn(false);
163  $statusArray = $node->getStatus();
164  self::assertSame(‪FlashMessage::ERROR, $statusArray[0]->getSeverity());
165  }
166 
171  {
173  $node = $this->getAccessibleMock(
174  LinkNode::class,
175  ['isWindowsOs', 'getAbsolutePath', 'exists', 'isLink', 'getRelativePathBelowSiteRoot'],
176  [],
177  '',
178  false
179  );
180  $node->expects(self::any())->method('getAbsolutePath')->willReturn('');
181  $node->expects(self::any())->method('exists')->willReturn(true);
182  $node->expects(self::once())->method('isLink')->willReturn(false);
183  $statusArray = $node->getStatus();
184  self::assertSame(‪FlashMessage::WARNING, $statusArray[0]->getSeverity());
185  }
186 
191  {
193  $node = $this->getAccessibleMock(
194  LinkNode::class,
195  ['isWindowsOs', 'getAbsolutePath', 'exists', 'isLink', 'isTargetCorrect', 'getCurrentTarget', 'getRelativePathBelowSiteRoot'],
196  [],
197  '',
198  false
199  );
200  $node->expects(self::any())->method('getAbsolutePath')->willReturn('');
201  $node->expects(self::any())->method('getCurrentTarget')->willReturn('');
202  $node->expects(self::any())->method('exists')->willReturn(true);
203  $node->expects(self::any())->method('isLink')->willReturn(true);
204  $node->expects(self::once())->method('isLink')->willReturn(false);
205  $statusArray = $node->getStatus();
206  self::assertSame(‪FlashMessage::ERROR, $statusArray[0]->getSeverity());
207  }
208 
213  {
215  $node = $this->getAccessibleMock(
216  LinkNode::class,
217  ['isWindowsOs', 'getAbsolutePath', 'exists', 'isLink', 'isTargetCorrect', 'getRelativePathBelowSiteRoot'],
218  [],
219  '',
220  false
221  );
222  $node->expects(self::any())->method('getAbsolutePath')->willReturn('');
223  $node->expects(self::any())->method('exists')->willReturn(true);
224  $node->expects(self::once())->method('isLink')->willReturn(true);
225  $node->expects(self::once())->method('isTargetCorrect')->willReturn(true);
226  $node->expects(self::once())->method('getRelativePathBelowSiteRoot')->willReturn('');
227  $statusArray = $node->getStatus();
228  self::assertSame(‪FlashMessage::OK, $statusArray[0]->getSeverity());
229  }
230 
234  public function ‪fixReturnsEmptyArray()
235  {
237  $node = $this->getAccessibleMock(
238  LinkNode::class,
239  ['getRelativePathBelowSiteRoot'],
240  [],
241  '',
242  false
243  );
244  $statusArray = $node->fix();
245  self::assertEmpty($statusArray);
246  }
247 
252  {
253  $this->expectException(InvalidArgumentException::class);
254  $this->expectExceptionCode(1380556246);
256  $node = $this->getAccessibleMock(LinkNode::class, ['exists'], [], '', false);
257  $node->expects(self::once())->method('exists')->willReturn(false);
258  self::assertFalse($node->_call('isLink'));
259  }
260 
265  {
267  $node = $this->getAccessibleMock(LinkNode::class, ['exists', 'getAbsolutePath'], [], '', false);
268  $path = ‪Environment::getVarPath() . '/tests/' . ‪StringUtility::getUniqueId('link_');
269  $target = ‪Environment::getVarPath() . '/tests/' . ‪StringUtility::getUniqueId('linkTarget_');
270  touch($target);
271  symlink($target, $path);
272  $this->testFilesToDelete[] = $path;
273  $this->testFilesToDelete[] = $target;
274  $node->expects(self::any())->method('exists')->willReturn(true);
275  $node->expects(self::any())->method('getAbsolutePath')->willReturn($path);
276  self::assertTrue($node->_call('isLink'));
277  }
278 
283  {
285  $node = $this->getAccessibleMock(LinkNode::class, ['exists', 'getAbsolutePath'], [], '', false);
286  $path = ‪Environment::getVarPath() . '/tests/' . ‪StringUtility::getUniqueId('file_');
287  touch($path);
288  $this->testFilesToDelete[] = $path;
289  $node->expects(self::any())->method('exists')->willReturn(true);
290  $node->expects(self::any())->method('getAbsolutePath')->willReturn($path);
291  self::assertFalse($node->_call('isLink'));
292  }
293 
298  {
299  $this->expectException(InvalidArgumentException::class);
300  $this->expectExceptionCode(1380556245);
302  $node = $this->getAccessibleMock(LinkNode::class, ['exists'], [], '', false);
303  $node->expects(self::once())->method('exists')->willReturn(false);
304  self::assertFalse($node->_call('isTargetCorrect'));
305  }
306 
311  {
312  $this->expectException(InvalidArgumentException::class);
313  $this->expectExceptionCode(1380556247);
315  $node = $this->getAccessibleMock(LinkNode::class, ['exists', 'isLink', 'getTarget'], [], '', false);
316  $node->expects(self::any())->method('exists')->willReturn(true);
317  $node->expects(self::once())->method('isLink')->willReturn(false);
318  self::assertTrue($node->_call('isTargetCorrect'));
319  }
320 
325  {
327  $node = $this->getAccessibleMock(LinkNode::class, ['exists', 'isLink', 'getTarget'], [], '', false);
328  $node->expects(self::any())->method('exists')->willReturn(true);
329  $node->expects(self::any())->method('isLink')->willReturn(true);
330  $node->expects(self::once())->method('getTarget')->willReturn('');
331  self::assertTrue($node->_call('isTargetCorrect'));
332  }
333 
338  {
340  $node = $this->getAccessibleMock(LinkNode::class, ['exists', 'isLink', 'getCurrentTarget', 'getTarget'], [], '', false);
341  $node->expects(self::any())->method('exists')->willReturn(true);
342  $node->expects(self::any())->method('isLink')->willReturn(true);
343  $node->expects(self::once())->method('getCurrentTarget')->willReturn('someLinkTarget/');
344  $node->expects(self::once())->method('getTarget')->willReturn('someLinkTarget');
345  self::assertTrue($node->_call('isTargetCorrect'));
346  }
347 
353  {
354  $path = ‪Environment::getVarPath() . '/tests/' . ‪StringUtility::getUniqueId('link_');
355  $target = ‪Environment::getVarPath() . '/tests/' . ‪StringUtility::getUniqueId('linkTarget_');
356  touch($target);
357  symlink($target, $path);
358  $this->testFilesToDelete[] = $path;
359  $this->testFilesToDelete[] = $target;
361  $node = $this->getAccessibleMock(
362  LinkNode::class,
363  ['exists', 'isLink', 'getTarget', 'getAbsolutePath'],
364  [],
365  '',
366  false
367  );
368  $node->expects(self::any())->method('exists')->willReturn(true);
369  $node->expects(self::any())->method('isLink')->willReturn(true);
370  $node->expects(self::once())->method('getTarget')->willReturn(str_replace('/', DIRECTORY_SEPARATOR, $target));
371  $node->expects(self::once())->method('getAbsolutePath')->willReturn($path);
372  self::assertTrue($node->_call('isTargetCorrect'));
373  }
374 
380  {
381  $path = ‪Environment::getVarPath() . '/tests/' . ‪StringUtility::getUniqueId('link_');
382  $target = ‪Environment::getVarPath() . '/tests/' . ‪StringUtility::getUniqueId('linkTarget_');
383  touch($target);
384  symlink($target, $path);
385  $this->testFilesToDelete[] = $path;
386  $this->testFilesToDelete[] = $target;
388  $node = $this->getAccessibleMock(
389  LinkNode::class,
390  ['exists', 'isLink', 'getTarget', 'getAbsolutePath'],
391  [],
392  '',
393  false
394  );
395  $node->expects(self::any())->method('exists')->willReturn(true);
396  $node->expects(self::any())->method('isLink')->willReturn(true);
397  $node->expects(self::once())->method('getTarget')->willReturn('foo');
398  $node->expects(self::once())->method('getAbsolutePath')->willReturn($path);
399  self::assertFalse($node->_call('isTargetCorrect'));
400  }
401 }
‪TYPO3\CMS\Core\Core\Environment\getPublicPath
‪static string getPublicPath()
Definition: Environment.php:180
‪TYPO3\CMS\Install\FolderStructure\RootNodeInterface
Definition: RootNodeInterface.php:22
‪TYPO3\CMS\Install\FolderStructure\Exception\InvalidArgumentException
Definition: InvalidArgumentException.php:24
‪TYPO3\CMS\Core\Messaging\AbstractMessage\WARNING
‪const WARNING
Definition: AbstractMessage.php:30
‪TYPO3\CMS\Install\Tests\Unit\FolderStructure
Definition: AbstractNodeTest.php:16
‪TYPO3\CMS\Core\Messaging\AbstractMessage\OK
‪const OK
Definition: AbstractMessage.php:29
‪TYPO3\CMS\Core\Messaging\AbstractMessage\INFO
‪const INFO
Definition: AbstractMessage.php:28
‪TYPO3\CMS\Core\Utility\StringUtility\getUniqueId
‪static string getUniqueId($prefix='')
Definition: StringUtility.php:92
‪TYPO3\CMS\Core\Messaging\FlashMessage
Definition: FlashMessage.php:24
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:40
‪TYPO3\CMS\Core\Utility\StringUtility
Definition: StringUtility.php:22
‪TYPO3\CMS\Install\FolderStructure\NodeInterface
Definition: NodeInterface.php:24
‪TYPO3\CMS\Core\Messaging\AbstractMessage\ERROR
‪const ERROR
Definition: AbstractMessage.php:31
‪TYPO3\CMS\Core\Core\Environment\getVarPath
‪static string getVarPath()
Definition: Environment.php:192