‪TYPO3CMS  11.5
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\Unit\UnitTestCase;
28 
32 class ‪LinkNodeTest extends UnitTestCase
33 {
38  {
39  $this->expectException(InvalidArgumentException::class);
40  $this->expectExceptionCode(1380485700);
41  $node = $this->getAccessibleMock(LinkNode::class, ['dummy'], [], '', false);
42  $node->__construct([], null);
43  }
44 
49  {
50  $this->expectException(InvalidArgumentException::class);
51  $this->expectExceptionCode(1380546061);
52  $parent = $this->createMock(NodeInterface::class);
53  $node = $this->getAccessibleMock(LinkNode::class, ['dummy'], [], '', false);
54  $structure = [
55  'name' => 'foo/bar',
56  ];
57  $node->__construct($structure, $parent);
58  }
59 
63  public function ‪constructorSetsParent(): void
64  {
65  $parent = $this->createMock(NodeInterface::class);
66  $node = $this->getAccessibleMock(LinkNode::class, ['dummy'], [], '', false);
67  $structure = [
68  'name' => 'foo',
69  ];
70  $node->__construct($structure, $parent);
71  self::assertSame($parent, $node->_call('getParent'));
72  }
73 
77  public function ‪constructorSetsName(): void
78  {
79  $node = $this->getAccessibleMock(LinkNode::class, ['dummy'], [], '', false);
80  $parent = $this->createMock(RootNodeInterface::class);
81  $name = ‪StringUtility::getUniqueId('test_');
82  $node->__construct(['name' => $name], $parent);
83  self::assertSame($name, $node->getName());
84  }
85 
89  public function ‪constructorSetsNameAndTarget(): void
90  {
91  $node = $this->getAccessibleMock(LinkNode::class, ['dummy'], [], '', 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 
102  public function ‪getStatusReturnsArray(): void
103  {
104  $node = $this->getAccessibleMock(
105  LinkNode::class,
106  ['isWindowsOs', 'getAbsolutePath', 'exists'],
107  [],
108  '',
109  false
110  );
111  // do not use var path here, as link nodes get checked for public path as first part
112  $path = ‪Environment::getPublicPath() . '/typo3temp/tests/' . ‪StringUtility::getUniqueId('dir_');
113  $node->method('getAbsolutePath')->willReturn($path);
114  self::assertIsArray($node->getStatus());
115  }
116 
121  {
122  $node = $this->getAccessibleMock(
123  LinkNode::class,
124  ['isWindowsOs', 'getAbsolutePath', 'exists'],
125  [],
126  '',
127  false
128  );
129  // do not use var path here, as link nodes get checked for public path as first part
130  $path = ‪Environment::getPublicPath() . '/tests/' . ‪StringUtility::getUniqueId('dir_');
131  $node->method('getAbsolutePath')->willReturn($path);
132  $node->expects(self::once())->method('isWindowsOs')->willReturn(true);
133  $statusArray = $node->getStatus();
134  self::assertSame(‪FlashMessage::INFO, $statusArray[0]->getSeverity());
135  }
136 
141  {
142  $node = $this->getAccessibleMock(
143  LinkNode::class,
144  ['isWindowsOs', 'getAbsolutePath', 'exists'],
145  [],
146  '',
147  false
148  );
149  // do not use var path here, as link nodes get checked for public path as first part
150  $path = ‪Environment::getPublicPath() . '/tests/' . ‪StringUtility::getUniqueId('dir_');
151  $node->method('getAbsolutePath')->willReturn($path);
152  $node->method('isWindowsOs')->willReturn(false);
153  $node->expects(self::once())->method('exists')->willReturn(false);
154  $statusArray = $node->getStatus();
155  self::assertSame(‪FlashMessage::ERROR, $statusArray[0]->getSeverity());
156  }
157 
162  {
163  $node = $this->getAccessibleMock(
164  LinkNode::class,
165  ['isWindowsOs', 'getAbsolutePath', 'exists', 'isLink', 'getRelativePathBelowSiteRoot'],
166  [],
167  '',
168  false
169  );
170  $node->method('getAbsolutePath')->willReturn('');
171  $node->method('exists')->willReturn(true);
172  $node->expects(self::once())->method('isLink')->willReturn(false);
173  $statusArray = $node->getStatus();
174  self::assertSame(‪FlashMessage::WARNING, $statusArray[0]->getSeverity());
175  }
176 
181  {
182  $node = $this->getAccessibleMock(
183  LinkNode::class,
184  ['isWindowsOs', 'getAbsolutePath', 'exists', 'isLink', 'isTargetCorrect', 'getCurrentTarget', 'getRelativePathBelowSiteRoot'],
185  [],
186  '',
187  false
188  );
189  $node->method('getAbsolutePath')->willReturn('');
190  $node->method('getCurrentTarget')->willReturn('');
191  $node->method('exists')->willReturn(true);
192  $node->method('isLink')->willReturn(true);
193  $node->expects(self::once())->method('isLink')->willReturn(false);
194  $statusArray = $node->getStatus();
195  self::assertSame(‪FlashMessage::ERROR, $statusArray[0]->getSeverity());
196  }
197 
202  {
203  $node = $this->getAccessibleMock(
204  LinkNode::class,
205  ['isWindowsOs', 'getAbsolutePath', 'exists', 'isLink', 'isTargetCorrect', 'getRelativePathBelowSiteRoot'],
206  [],
207  '',
208  false
209  );
210  $node->method('getAbsolutePath')->willReturn('');
211  $node->method('exists')->willReturn(true);
212  $node->expects(self::once())->method('isLink')->willReturn(true);
213  $node->expects(self::once())->method('isTargetCorrect')->willReturn(true);
214  $node->expects(self::once())->method('getRelativePathBelowSiteRoot')->willReturn('');
215  $statusArray = $node->getStatus();
216  self::assertSame(‪FlashMessage::OK, $statusArray[0]->getSeverity());
217  }
218 
222  public function ‪fixReturnsEmptyArray(): void
223  {
224  $node = $this->getAccessibleMock(
225  LinkNode::class,
226  ['getRelativePathBelowSiteRoot'],
227  [],
228  '',
229  false
230  );
231  $statusArray = $node->fix();
232  self::assertEmpty($statusArray);
233  }
234 
239  {
240  $this->expectException(InvalidArgumentException::class);
241  $this->expectExceptionCode(1380556246);
242  $node = $this->getAccessibleMock(LinkNode::class, ['exists'], [], '', false);
243  $node->expects(self::once())->method('exists')->willReturn(false);
244  self::assertFalse($node->_call('isLink'));
245  }
246 
250  public function ‪isLinkReturnsTrueIfNameIsLink(): void
251  {
252  $node = $this->getAccessibleMock(LinkNode::class, ['exists', 'getAbsolutePath'], [], '', false);
253  $path = ‪Environment::getVarPath() . '/tests/' . ‪StringUtility::getUniqueId('link_');
254  $target = ‪Environment::getVarPath() . '/tests/' . ‪StringUtility::getUniqueId('linkTarget_');
255  touch($target);
256  symlink($target, $path);
257  $this->testFilesToDelete[] = $path;
258  $this->testFilesToDelete[] = $target;
259  $node->method('exists')->willReturn(true);
260  $node->method('getAbsolutePath')->willReturn($path);
261  self::assertTrue($node->_call('isLink'));
262  }
263 
267  public function ‪isFileReturnsFalseIfNameIsAFile(): void
268  {
269  $node = $this->getAccessibleMock(LinkNode::class, ['exists', 'getAbsolutePath'], [], '', false);
270  $path = ‪Environment::getVarPath() . '/tests/' . ‪StringUtility::getUniqueId('file_');
271  touch($path);
272  $this->testFilesToDelete[] = $path;
273  $node->method('exists')->willReturn(true);
274  $node->method('getAbsolutePath')->willReturn($path);
275  self::assertFalse($node->_call('isLink'));
276  }
277 
282  {
283  $this->expectException(InvalidArgumentException::class);
284  $this->expectExceptionCode(1380556245);
285  $node = $this->getAccessibleMock(LinkNode::class, ['exists'], [], '', false);
286  $node->expects(self::once())->method('exists')->willReturn(false);
287  self::assertFalse($node->_call('isTargetCorrect'));
288  }
289 
294  {
295  $this->expectException(InvalidArgumentException::class);
296  $this->expectExceptionCode(1380556247);
297  $node = $this->getAccessibleMock(LinkNode::class, ['exists', 'isLink', 'getTarget'], [], '', false);
298  $node->method('exists')->willReturn(true);
299  $node->expects(self::once())->method('isLink')->willReturn(false);
300  self::assertTrue($node->_call('isTargetCorrect'));
301  }
302 
307  {
308  $node = $this->getAccessibleMock(LinkNode::class, ['exists', 'isLink', 'getTarget'], [], '', false);
309  $node->method('exists')->willReturn(true);
310  $node->method('isLink')->willReturn(true);
311  $node->expects(self::once())->method('getTarget')->willReturn('');
312  self::assertTrue($node->_call('isTargetCorrect'));
313  }
314 
319  {
320  $node = $this->getAccessibleMock(LinkNode::class, ['exists', 'isLink', 'getCurrentTarget', 'getTarget'], [], '', false);
321  $node->method('exists')->willReturn(true);
322  $node->method('isLink')->willReturn(true);
323  $node->expects(self::once())->method('getCurrentTarget')->willReturn('someLinkTarget/');
324  $node->expects(self::once())->method('getTarget')->willReturn('someLinkTarget');
325  self::assertTrue($node->_call('isTargetCorrect'));
326  }
327 
333  {
334  $path = ‪Environment::getVarPath() . '/tests/' . ‪StringUtility::getUniqueId('link_');
335  $target = ‪Environment::getVarPath() . '/tests/' . ‪StringUtility::getUniqueId('linkTarget_');
336  touch($target);
337  symlink($target, $path);
338  $this->testFilesToDelete[] = $path;
339  $this->testFilesToDelete[] = $target;
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 
359  {
360  $path = ‪Environment::getVarPath() . '/tests/' . ‪StringUtility::getUniqueId('link_');
361  $target = ‪Environment::getVarPath() . '/tests/' . ‪StringUtility::getUniqueId('linkTarget_');
362  touch($target);
363  symlink($target, $path);
364  $this->testFilesToDelete[] = $path;
365  $this->testFilesToDelete[] = $target;
366  $node = $this->getAccessibleMock(
367  LinkNode::class,
368  ['exists', 'isLink', 'getTarget', 'getAbsolutePath'],
369  [],
370  '',
371  false
372  );
373  $node->method('exists')->willReturn(true);
374  $node->method('isLink')->willReturn(true);
375  $node->expects(self::once())->method('getTarget')->willReturn('foo');
376  $node->expects(self::once())->method('getAbsolutePath')->willReturn($path);
377  self::assertFalse($node->_call('isTargetCorrect'));
378  }
379 }
‪TYPO3\CMS\Core\Core\Environment\getPublicPath
‪static string getPublicPath()
Definition: Environment.php:206
‪TYPO3\CMS\Install\FolderStructure\RootNodeInterface
Definition: RootNodeInterface.php:21
‪TYPO3\CMS\Install\FolderStructure\Exception\InvalidArgumentException
Definition: InvalidArgumentException.php:23
‪TYPO3\CMS\Core\Messaging\AbstractMessage\WARNING
‪const WARNING
Definition: AbstractMessage.php:30
‪TYPO3\CMS\Install\Tests\Unit\FolderStructure
Definition: AbstractNodeTest.php:18
‪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:128
‪TYPO3\CMS\Core\Messaging\FlashMessage
Definition: FlashMessage.php:26
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:43
‪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:218