[BUGFIX] Correct trim of trailing slash
[Packages/TYPO3.CMS.git] / typo3 / sysext / install / Tests / Unit / FolderStructure / LinkNodeTest.php
1 <?php
2 namespace TYPO3\CMS\Install\Tests\Unit\FolderStructure;
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 */
16
17 /**
18 * Test case
19 */
20 class LinkNodeTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
21
22 /**
23 * @test
24 * @expectedException \TYPO3\CMS\Install\FolderStructure\Exception\InvalidArgumentException
25 */
26 public function constructorThrowsExceptionIfParentIsNull() {
27 /** @var $node \TYPO3\CMS\Install\FolderStructure\LinkNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
28 $node = $this->getAccessibleMock(\TYPO3\CMS\Install\FolderStructure\LinkNode::class, array('dummy'), array(), '', FALSE);
29 $node->__construct(array(), NULL);
30 }
31
32 /**
33 * @test
34 * @expectedException \TYPO3\CMS\Install\FolderStructure\Exception\InvalidArgumentException
35 */
36 public function constructorThrowsExceptionIfNameContainsForwardSlash() {
37 $parent = $this->getMock(\TYPO3\CMS\Install\FolderStructure\NodeInterface::class, array(), array(), '', FALSE);
38 /** @var $node \TYPO3\CMS\Install\FolderStructure\LinkNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
39 $node = $this->getAccessibleMock(\TYPO3\CMS\Install\FolderStructure\LinkNode::class, array('dummy'), array(), '', FALSE);
40 $structure = array(
41 'name' => 'foo/bar',
42 );
43 $node->__construct($structure, $parent);
44 }
45
46 /**
47 * @test
48 */
49 public function constructorSetsParent() {
50 $parent = $this->getMock(\TYPO3\CMS\Install\FolderStructure\NodeInterface::class, array(), array(), '', FALSE);
51 /** @var $node \TYPO3\CMS\Install\FolderStructure\LinkNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
52 $node = $this->getAccessibleMock(\TYPO3\CMS\Install\FolderStructure\LinkNode::class, array('dummy'), array(), '', FALSE);
53 $structure = array(
54 'name' => 'foo',
55 );
56 $node->__construct($structure, $parent);
57 $this->assertSame($parent, $node->_call('getParent'));
58 }
59
60 /**
61 * @test
62 */
63 public function constructorSetsName() {
64 /** @var $node \TYPO3\CMS\Install\FolderStructure\LinkNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
65 $node = $this->getAccessibleMock(\TYPO3\CMS\Install\FolderStructure\LinkNode::class, array('dummy'), array(), '', FALSE);
66 $parent = $this->getMock(\TYPO3\CMS\Install\FolderStructure\RootNodeInterface::class, array(), array(), '', FALSE);
67 $name = $this->getUniqueId('test_');
68 $node->__construct(array('name' => $name), $parent);
69 $this->assertSame($name, $node->getName());
70 }
71
72 /**
73 * @test
74 */
75 public function constructorSetsTarget() {
76 /** @var $node \TYPO3\CMS\Install\FolderStructure\LinkNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
77 $node = $this->getAccessibleMock(\TYPO3\CMS\Install\FolderStructure\LinkNode::class, array('dummy'), array(), '', FALSE);
78 $parent = $this->getMock(\TYPO3\CMS\Install\FolderStructure\RootNodeInterface::class, array(), array(), '', FALSE);
79 $target = '../' . $this->getUniqueId('test_');
80 $node->__construct(array('target' => $target), $parent);
81 $this->assertSame($target, $node->_call('getTarget'));
82 }
83
84 /**
85 * @test
86 */
87 public function getStatusReturnsArray() {
88 /** @var $node \TYPO3\CMS\Install\FolderStructure\LinkNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
89 $node = $this->getAccessibleMock(
90 \TYPO3\CMS\Install\FolderStructure\LinkNode::class,
91 array('isWindowsOs', 'getAbsolutePath', 'exists'),
92 array(),
93 '',
94 FALSE
95 );
96 $path = PATH_site . 'typo3temp/' . $this->getUniqueId('dir_');
97 $node->expects($this->any())->method('getAbsolutePath')->will($this->returnValue($path));
98 $this->assertInternalType('array', $node->getStatus());
99 }
100
101 /**
102 * @test
103 */
104 public function getStatusReturnsArrayWithInformationStatusIfRunningOnWindows() {
105 /** @var $node \TYPO3\CMS\Install\FolderStructure\LinkNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
106 $node = $this->getAccessibleMock(
107 \TYPO3\CMS\Install\FolderStructure\LinkNode::class,
108 array('isWindowsOs', 'getAbsolutePath', 'exists'),
109 array(),
110 '',
111 FALSE
112 );
113 $path = PATH_site . 'typo3temp/' . $this->getUniqueId('dir_');
114 $node->expects($this->any())->method('getAbsolutePath')->will($this->returnValue($path));
115 $node->expects($this->once())->method('isWindowsOs')->will($this->returnValue(TRUE));
116 $statusArray = $node->getStatus();
117 /** @var $status \TYPO3\CMS\Install\Status\StatusInterface */
118 $status = $statusArray[0];
119 $this->assertInstanceOf(\TYPO3\CMS\Install\Status\InfoStatus::class, $status);
120 }
121
122 /**
123 * @test
124 */
125 public function getStatusReturnsArrayWithErrorStatusIfLinkNotExists() {
126 /** @var $node \TYPO3\CMS\Install\FolderStructure\LinkNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
127 $node = $this->getAccessibleMock(
128 \TYPO3\CMS\Install\FolderStructure\LinkNode::class,
129 array('isWindowsOs', 'getAbsolutePath', 'exists'),
130 array(),
131 '',
132 FALSE
133 );
134 $path = PATH_site . 'typo3temp/' . $this->getUniqueId('dir_');
135 $node->expects($this->any())->method('getAbsolutePath')->will($this->returnValue($path));
136 $node->expects($this->any())->method('isWindowsOs')->will($this->returnValue(FALSE));
137 $node->expects($this->once())->method('exists')->will($this->returnValue(FALSE));
138 $statusArray = $node->getStatus();
139 /** @var $status \TYPO3\CMS\Install\Status\StatusInterface */
140 $status = $statusArray[0];
141 $this->assertInstanceOf(\TYPO3\CMS\Install\Status\ErrorStatus::class, $status);
142 }
143
144 /**
145 * @test
146 */
147 public function getStatusReturnsArrayWithWarningStatusIfNodeIsNotALink() {
148 /** @var $node \TYPO3\CMS\Install\FolderStructure\LinkNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
149 $node = $this->getAccessibleMock(
150 \TYPO3\CMS\Install\FolderStructure\LinkNode::class,
151 array('isWindowsOs', 'getAbsolutePath', 'exists', 'isLink', 'getRelativePathBelowSiteRoot'),
152 array(),
153 '',
154 FALSE
155 );
156 $node->expects($this->any())->method('getAbsolutePath')->will($this->returnValue($path));
157 $node->expects($this->any())->method('exists')->will($this->returnValue(TRUE));
158 $node->expects($this->once())->method('isLink')->will($this->returnValue(FALSE));
159 $statusArray = $node->getStatus();
160 /** @var $status \TYPO3\CMS\Install\Status\StatusInterface */
161 $status = $statusArray[0];
162 $this->assertInstanceOf(\TYPO3\CMS\Install\Status\WarningStatus::class, $status);
163 }
164
165 /**
166 * @test
167 */
168 public function getStatusReturnsErrorStatusIfLinkTargetIsNotCorrect() {
169 /** @var $node \TYPO3\CMS\Install\FolderStructure\LinkNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
170 $node = $this->getAccessibleMock(
171 \TYPO3\CMS\Install\FolderStructure\LinkNode::class,
172 array('isWindowsOs', 'getAbsolutePath', 'exists', 'isLink', 'isTargetCorrect', 'getCurrentTarget', 'getRelativePathBelowSiteRoot'),
173 array(),
174 '',
175 FALSE
176 );
177 $node->expects($this->any())->method('getAbsolutePath')->will($this->returnValue($path));
178 $node->expects($this->any())->method('getCurrentTarget')->will($this->returnValue(''));
179 $node->expects($this->any())->method('exists')->will($this->returnValue(TRUE));
180 $node->expects($this->any())->method('isLink')->will($this->returnValue(TRUE));
181 $node->expects($this->once())->method('isLink')->will($this->returnValue(FALSE));
182 $statusArray = $node->getStatus();
183 /** @var $status \TYPO3\CMS\Install\Status\StatusInterface */
184 $status = $statusArray[0];
185 $this->assertInstanceOf(\TYPO3\CMS\Install\Status\ErrorStatus::class, $status);
186 }
187
188 /**
189 * @test
190 */
191 public function getStatusReturnsOkStatusIfLinkExistsAndTargetIsCorrect() {
192 /** @var $node \TYPO3\CMS\Install\FolderStructure\LinkNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
193 $node = $this->getAccessibleMock(
194 \TYPO3\CMS\Install\FolderStructure\LinkNode::class,
195 array('isWindowsOs', 'getAbsolutePath', 'exists', 'isLink', 'isTargetCorrect', 'getRelativePathBelowSiteRoot'),
196 array(),
197 '',
198 FALSE
199 );
200 $node->expects($this->any())->method('getAbsolutePath')->will($this->returnValue($path));
201 $node->expects($this->any())->method('exists')->will($this->returnValue(TRUE));
202 $node->expects($this->once())->method('isLink')->will($this->returnValue(TRUE));
203 $node->expects($this->once())->method('isTargetCorrect')->will($this->returnValue(TRUE));
204 $statusArray = $node->getStatus();
205 /** @var $status \TYPO3\CMS\Install\Status\StatusInterface */
206 $status = $statusArray[0];
207 $this->assertInstanceOf(\TYPO3\CMS\Install\Status\OkStatus::class, $status);
208 }
209
210 /**
211 * @test
212 */
213 public function fixReturnsEmptyArray() {
214 /** @var $node \TYPO3\CMS\Install\FolderStructure\LinkNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
215 $node = $this->getAccessibleMock(
216 \TYPO3\CMS\Install\FolderStructure\LinkNode::class,
217 array('getRelativePathBelowSiteRoot'),
218 array(),
219 '',
220 FALSE
221 );
222 $statusArray = $node->fix();
223 $this->assertEmpty($statusArray);
224 }
225
226 /**
227 * @test
228 * @expectedException \TYPO3\CMS\Install\FolderStructure\Exception\InvalidArgumentException
229 */
230 public function isLinkThrowsExceptionIfLinkNotExists() {
231 /** @var $node \TYPO3\CMS\Install\FolderStructure\LinkNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
232 $node = $this->getAccessibleMock(\TYPO3\CMS\Install\FolderStructure\LinkNode::class, array('exists'), array(), '', FALSE);
233 $node->expects($this->once())->method('exists')->will($this->returnValue(FALSE));
234 $this->assertFalse($node->_call('isLink'));
235 }
236
237 /**
238 * @test
239 */
240 public function isLinkReturnsTrueIfNameIsLink() {
241 if (TYPO3_OS === 'WIN') {
242 $this->markTestSkipped('Test not available on Windows OS.');
243 }
244 /** @var $node \TYPO3\CMS\Install\FolderStructure\LinkNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
245 $node = $this->getAccessibleMock(\TYPO3\CMS\Install\FolderStructure\LinkNode::class, array('exists', 'getAbsolutePath'), array(), '', FALSE);
246 $path = PATH_site . 'typo3temp/' . $this->getUniqueId('link_');
247 $target = PATH_site . $this->getUniqueId('linkTarget_');
248 symlink($target, $path);
249 $this->testFilesToDelete[] = $path;
250 $node->expects($this->any())->method('exists')->will($this->returnValue(TRUE));
251 $node->expects($this->any())->method('getAbsolutePath')->will($this->returnValue($path));
252 $this->assertTrue($node->_call('isLink'));
253 }
254
255 /**
256 * @test
257 */
258 public function isFileReturnsFalseIfNameIsAFile() {
259 if (TYPO3_OS === 'WIN') {
260 $this->markTestSkipped('Test not available on Windows OS.');
261 }
262 /** @var $node \TYPO3\CMS\Install\FolderStructure\LinkNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
263 $node = $this->getAccessibleMock(\TYPO3\CMS\Install\FolderStructure\LinkNode::class, array('exists', 'getAbsolutePath'), array(), '', FALSE);
264 $path = PATH_site . 'typo3temp/' . $this->getUniqueId('file_');
265 touch($path);
266 $this->testFilesToDelete[] = $path;
267 $node->expects($this->any())->method('exists')->will($this->returnValue(TRUE));
268 $node->expects($this->any())->method('getAbsolutePath')->will($this->returnValue($path));
269 $this->assertFalse($node->_call('isLink'));
270 }
271
272 /**
273 * @test
274 * @expectedException \TYPO3\CMS\Install\FolderStructure\Exception\InvalidArgumentException
275 */
276 public function isTargetCorrectThrowsExceptionIfLinkNotExists() {
277 /** @var $node \TYPO3\CMS\Install\FolderStructure\LinkNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
278 $node = $this->getAccessibleMock(\TYPO3\CMS\Install\FolderStructure\LinkNode::class, array('exists'), array(), '', FALSE);
279 $node->expects($this->once())->method('exists')->will($this->returnValue(FALSE));
280 $this->assertFalse($node->_call('isTargetCorrect'));
281 }
282
283 /**
284 * @test
285 * @expectedException \TYPO3\CMS\Install\FolderStructure\Exception\InvalidArgumentException
286 */
287 public function isTargetCorrectThrowsExceptionIfNodeIsNotALink() {
288 /** @var $node \TYPO3\CMS\Install\FolderStructure\LinkNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
289 $node = $this->getAccessibleMock(\TYPO3\CMS\Install\FolderStructure\LinkNode::class, array('exists', 'isLink', 'getTarget'), array(), '', FALSE);
290 $node->expects($this->any())->method('exists')->will($this->returnValue(TRUE));
291 $node->expects($this->once())->method('isLink')->will($this->returnValue(FALSE));
292 $this->assertTrue($node->_call('isTargetCorrect'));
293 }
294
295 /**
296 * @test
297 */
298 public function isTargetCorrectReturnsTrueIfNoExpectedLinkTargetIsSpecified() {
299 /** @var $node \TYPO3\CMS\Install\FolderStructure\LinkNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
300 $node = $this->getAccessibleMock(\TYPO3\CMS\Install\FolderStructure\LinkNode::class, array('exists', 'isLink', 'getTarget'), array(), '', FALSE);
301 $node->expects($this->any())->method('exists')->will($this->returnValue(TRUE));
302 $node->expects($this->any())->method('isLink')->will($this->returnValue(TRUE));
303 $node->expects($this->once())->method('getTarget')->will($this->returnValue(''));
304 $this->assertTrue($node->_call('isTargetCorrect'));
305 }
306
307 /**
308 * @test
309 */
310 public function isTargetCorrectAcceptsATargetWithATrailingSlash() {
311 /** @var $node \TYPO3\CMS\Install\FolderStructure\LinkNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
312 $node = $this->getAccessibleMock(\TYPO3\CMS\Install\FolderStructure\LinkNode::class, array('exists', 'isLink', 'getCurrentTarget', 'getTarget'), array(), '', FALSE);
313 $node->expects($this->any())->method('exists')->will($this->returnValue(TRUE));
314 $node->expects($this->any())->method('isLink')->will($this->returnValue(TRUE));
315 $node->expects($this->once())->method('getCurrentTarget')->will($this->returnValue('someLinkTarget/'));
316 $node->expects($this->once())->method('getTarget')->will($this->returnValue('someLinkTarget'));
317 $this->assertTrue($node->_call('isTargetCorrect'));
318 }
319
320 /**
321 * @test
322 */
323 public function isTargetCorrectReturnsTrueIfActualTargetIsIdenticalToSpecifiedTarget() {
324 if (TYPO3_OS === 'WIN') {
325 $this->markTestSkipped('Test not available on Windows OS.');
326 }
327 $path = PATH_site . 'typo3temp/' . $this->getUniqueId('link_');
328 $target = $this->getUniqueId('linkTarget_');
329 symlink($target, $path);
330 $this->testFilesToDelete[] = $path;
331 /** @var $node \TYPO3\CMS\Install\FolderStructure\LinkNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
332 $node = $this->getAccessibleMock(\TYPO3\CMS\Install\FolderStructure\LinkNode::class,
333 array('exists', 'isLink', 'getTarget', 'getAbsolutePath'),
334 array(),
335 '',
336 FALSE
337 );
338 $node->expects($this->any())->method('exists')->will($this->returnValue(TRUE));
339 $node->expects($this->any())->method('isLink')->will($this->returnValue(TRUE));
340 $node->expects($this->once())->method('getTarget')->will($this->returnValue($target));
341 $node->expects($this->once())->method('getAbsolutePath')->will($this->returnValue($path));
342 $this->assertTrue($node->_call('isTargetCorrect'));
343 }
344
345 /**
346 * @test
347 */
348 public function isTargetCorrectReturnsFalseIfActualTargetIsNotIdenticalToSpecifiedTarget() {
349 if (TYPO3_OS === 'WIN') {
350 $this->markTestSkipped('Test not available on Windows OS.');
351 }
352 $path = PATH_site . 'typo3temp/' . $this->getUniqueId('link_');
353 $target = $this->getUniqueId('linkTarget_');
354 symlink($target, $path);
355 $this->testFilesToDelete[] = $path;
356 /** @var $node \TYPO3\CMS\Install\FolderStructure\LinkNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
357 $node = $this->getAccessibleMock(\TYPO3\CMS\Install\FolderStructure\LinkNode::class,
358 array('exists', 'isLink', 'getTarget', 'getAbsolutePath'),
359 array(),
360 '',
361 FALSE
362 );
363 $node->expects($this->any())->method('exists')->will($this->returnValue(TRUE));
364 $node->expects($this->any())->method('isLink')->will($this->returnValue(TRUE));
365 $node->expects($this->once())->method('getTarget')->will($this->returnValue('foo'));
366 $node->expects($this->once())->method('getAbsolutePath')->will($this->returnValue($path));
367 $this->assertFalse($node->_call('isTargetCorrect'));
368 }
369
370 }