3fdf1616e85e17fc58b88de5319294ae22a45963
[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 * Copyright notice
6 *
7 * (c) 2013 Christian Kuhn <lolli@schwarzbu.ch>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 *
19 * This script is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
26
27 /**
28 * Test case
29 */
30 class LinkNodeTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
31
32 /**
33 * @var array Directories or files in typo3temp/ created during tests to delete afterwards
34 */
35 protected $testNodesToDelete = array();
36
37 /**
38 * Tear down
39 */
40 public function tearDown() {
41 foreach ($this->testNodesToDelete as $node) {
42 if (\TYPO3\CMS\Core\Utility\GeneralUtility::isFirstPartOfStr($node, PATH_site . 'typo3temp/')) {
43 \TYPO3\CMS\Core\Utility\GeneralUtility::rmdir($node, TRUE);
44 }
45 }
46 parent::tearDown();
47 }
48
49 /**
50 * @test
51 * @expectedException \TYPO3\CMS\Install\FolderStructure\Exception\InvalidArgumentException
52 */
53 public function constructorThrowsExceptionIfParentIsNull() {
54 /** @var $node \TYPO3\CMS\Install\FolderStructure\LinkNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
55 $node = $this->getAccessibleMock('TYPO3\\CMS\\Install\\FolderStructure\\LinkNode', array('dummy'), array(), '', FALSE);
56 $node->__construct(array(), NULL);
57 }
58
59 /**
60 * @test
61 * @expectedException \TYPO3\CMS\Install\FolderStructure\Exception\InvalidArgumentException
62 */
63 public function constructorThrowsExceptionIfNameContainsForwardSlash() {
64 $parent = $this->getMock('TYPO3\CMS\Install\FolderStructure\NodeInterface', array(), array(), '', FALSE);
65 /** @var $node \TYPO3\CMS\Install\FolderStructure\LinkNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
66 $node = $this->getAccessibleMock('TYPO3\\CMS\\Install\\FolderStructure\\LinkNode', array('dummy'), array(), '', FALSE);
67 $structure = array(
68 'name' => 'foo/bar',
69 );
70 $node->__construct($structure, $parent);
71 }
72
73 /**
74 * @test
75 */
76 public function constructorSetsParent() {
77 $parent = $this->getMock('TYPO3\CMS\Install\FolderStructure\NodeInterface', array(), array(), '', FALSE);
78 /** @var $node \TYPO3\CMS\Install\FolderStructure\LinkNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
79 $node = $this->getAccessibleMock('TYPO3\\CMS\\Install\\FolderStructure\\LinkNode', array('dummy'), array(), '', FALSE);
80 $structure = array(
81 'name' => 'foo',
82 );
83 $node->__construct($structure, $parent);
84 $this->assertSame($parent, $node->_call('getParent'));
85 }
86
87 /**
88 * @test
89 */
90 public function constructorSetsName() {
91 /** @var $node \TYPO3\CMS\Install\FolderStructure\LinkNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
92 $node = $this->getAccessibleMock('TYPO3\\CMS\\Install\\FolderStructure\\LinkNode', array('dummy'), array(), '', FALSE);
93 $parent = $this->getMock('TYPO3\CMS\Install\FolderStructure\RootNodeInterface', array(), array(), '', FALSE);
94 $name = uniqid('test_');
95 $node->__construct(array('name' => $name), $parent);
96 $this->assertSame($name, $node->getName());
97 }
98
99 /**
100 * @test
101 */
102 public function constructorSetsTarget() {
103 /** @var $node \TYPO3\CMS\Install\FolderStructure\LinkNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
104 $node = $this->getAccessibleMock('TYPO3\\CMS\\Install\\FolderStructure\\LinkNode', array('dummy'), array(), '', FALSE);
105 $parent = $this->getMock('TYPO3\CMS\Install\FolderStructure\RootNodeInterface', array(), array(), '', FALSE);
106 $target = '../' . uniqid('test_');
107 $node->__construct(array('target' => $target), $parent);
108 $this->assertSame($target, $node->_call('getTarget'));
109 }
110
111 /**
112 * @test
113 */
114 public function getStatusReturnsArray() {
115 /** @var $node \TYPO3\CMS\Install\FolderStructure\LinkNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
116 $node = $this->getAccessibleMock(
117 'TYPO3\\CMS\\Install\\FolderStructure\\LinkNode',
118 array('isWindowsOs', 'getAbsolutePath', 'exists'),
119 array(),
120 '',
121 FALSE
122 );
123 $path = PATH_site . 'typo3temp/' . uniqid('dir_');
124 $node->expects($this->any())->method('getAbsolutePath')->will($this->returnValue($path));
125 $this->assertInternalType('array', $node->getStatus());
126 }
127
128 /**
129 * @test
130 */
131 public function getStatusReturnsArrayWithInformationStatusIfRunningOnWindows() {
132 /** @var $node \TYPO3\CMS\Install\FolderStructure\LinkNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
133 $node = $this->getAccessibleMock(
134 'TYPO3\\CMS\\Install\\FolderStructure\\LinkNode',
135 array('isWindowsOs', 'getAbsolutePath', 'exists'),
136 array(),
137 '',
138 FALSE
139 );
140 $path = PATH_site . 'typo3temp/' . uniqid('dir_');
141 $node->expects($this->any())->method('getAbsolutePath')->will($this->returnValue($path));
142 $node->expects($this->once())->method('isWindowsOs')->will($this->returnValue(TRUE));
143 $statusArray = $node->getStatus();
144 /** @var $status \TYPO3\CMS\Install\Status\StatusInterface */
145 $status = $statusArray[0];
146 $this->assertInstanceOf('TYPO3\\CMS\\Install\\Status\\InfoStatus', $status);
147 }
148
149 /**
150 * @test
151 */
152 public function getStatusReturnsArrayWithErrorStatusIfLinkNotExists() {
153 /** @var $node \TYPO3\CMS\Install\FolderStructure\LinkNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
154 $node = $this->getAccessibleMock(
155 'TYPO3\\CMS\\Install\\FolderStructure\\LinkNode',
156 array('isWindowsOs', 'getAbsolutePath', 'exists'),
157 array(),
158 '',
159 FALSE
160 );
161 $path = PATH_site . 'typo3temp/' . uniqid('dir_');
162 $node->expects($this->any())->method('getAbsolutePath')->will($this->returnValue($path));
163 $node->expects($this->any())->method('isWindowsOs')->will($this->returnValue(FALSE));
164 $node->expects($this->once())->method('exists')->will($this->returnValue(FALSE));
165 $statusArray = $node->getStatus();
166 /** @var $status \TYPO3\CMS\Install\Status\StatusInterface */
167 $status = $statusArray[0];
168 $this->assertInstanceOf('TYPO3\\CMS\\Install\\Status\\ErrorStatus', $status);
169 }
170
171 /**
172 * @test
173 */
174 public function getStatusReturnsArrayWithErrorStatusIfNodeIsNotALink() {
175 /** @var $node \TYPO3\CMS\Install\FolderStructure\LinkNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
176 $node = $this->getAccessibleMock(
177 'TYPO3\\CMS\\Install\\FolderStructure\\LinkNode',
178 array('isWindowsOs', 'getAbsolutePath', 'exists', 'isLink', 'getRelativePathBelowSiteRoot'),
179 array(),
180 '',
181 FALSE
182 );
183 $node->expects($this->any())->method('getAbsolutePath')->will($this->returnValue($path));
184 $node->expects($this->any())->method('exists')->will($this->returnValue(TRUE));
185 $node->expects($this->once())->method('isLink')->will($this->returnValue(FALSE));
186 $statusArray = $node->getStatus();
187 /** @var $status \TYPO3\CMS\Install\Status\StatusInterface */
188 $status = $statusArray[0];
189 $this->assertInstanceOf('\TYPO3\CMS\Install\Status\ErrorStatus', $status);
190 }
191
192 /**
193 * @test
194 */
195 public function getStatusReturnsErrorStatusIfLinkTargetIsNotCorrect() {
196 /** @var $node \TYPO3\CMS\Install\FolderStructure\LinkNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
197 $node = $this->getAccessibleMock(
198 'TYPO3\\CMS\\Install\\FolderStructure\\LinkNode',
199 array('isWindowsOs', 'getAbsolutePath', 'exists', 'isLink', 'isTargetCorrect', 'getCurrentTarget', 'getRelativePathBelowSiteRoot'),
200 array(),
201 '',
202 FALSE
203 );
204 $node->expects($this->any())->method('getAbsolutePath')->will($this->returnValue($path));
205 $node->expects($this->any())->method('getCurrentTarget')->will($this->returnValue(''));
206 $node->expects($this->any())->method('exists')->will($this->returnValue(TRUE));
207 $node->expects($this->any())->method('isLink')->will($this->returnValue(TRUE));
208 $node->expects($this->once())->method('isLink')->will($this->returnValue(FALSE));
209 $statusArray = $node->getStatus();
210 /** @var $status \TYPO3\CMS\Install\Status\StatusInterface */
211 $status = $statusArray[0];
212 $this->assertInstanceOf('\TYPO3\CMS\Install\Status\ErrorStatus', $status);
213 }
214
215 /**
216 * @test
217 */
218 public function getStatusReturnsOkStatusIfLinkExistsAndTargetIsCorrect() {
219 /** @var $node \TYPO3\CMS\Install\FolderStructure\LinkNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
220 $node = $this->getAccessibleMock(
221 'TYPO3\\CMS\\Install\\FolderStructure\\LinkNode',
222 array('isWindowsOs', 'getAbsolutePath', 'exists', 'isLink', 'isTargetCorrect', 'getRelativePathBelowSiteRoot'),
223 array(),
224 '',
225 FALSE
226 );
227 $node->expects($this->any())->method('getAbsolutePath')->will($this->returnValue($path));
228 $node->expects($this->any())->method('exists')->will($this->returnValue(TRUE));
229 $node->expects($this->once())->method('isLink')->will($this->returnValue(TRUE));
230 $node->expects($this->once())->method('isTargetCorrect')->will($this->returnValue(TRUE));
231 $statusArray = $node->getStatus();
232 /** @var $status \TYPO3\CMS\Install\Status\StatusInterface */
233 $status = $statusArray[0];
234 $this->assertInstanceOf('\TYPO3\CMS\Install\Status\OkStatus', $status);
235 }
236
237 /**
238 * @test
239 */
240 public function fixReturnsNoticeStatus() {
241 /** @var $node \TYPO3\CMS\Install\FolderStructure\LinkNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
242 $node = $this->getAccessibleMock(
243 'TYPO3\\CMS\\Install\\FolderStructure\\LinkNode',
244 array('getRelativePathBelowSiteRoot'),
245 array(),
246 '',
247 FALSE
248 );
249 $statusArray = $node->fix();
250 /** @var $status \TYPO3\CMS\Install\Status\StatusInterface */
251 $status = $statusArray[0];
252 $this->assertInstanceOf('\TYPO3\CMS\Install\Status\NoticeStatus', $status);
253 }
254
255 /**
256 * @test
257 * @expectedException \TYPO3\CMS\Install\FolderStructure\Exception\InvalidArgumentException
258 */
259 public function isLinkThrowsExceptionIfLinkNotExists() {
260 /** @var $node \TYPO3\CMS\Install\FolderStructure\LinkNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
261 $node = $this->getAccessibleMock('TYPO3\\CMS\\Install\\FolderStructure\\LinkNode', array('exists'), array(), '', FALSE);
262 $node->expects($this->once())->method('exists')->will($this->returnValue(FALSE));
263 $this->assertFalse($node->_call('isLink'));
264 }
265
266 /**
267 * @test
268 */
269 public function isLinkReturnsTrueIfNameIsLink() {
270 if (TYPO3_OS === 'WIN') {
271 $this->markTestSkipped('Test not available on Windows OS.');
272 }
273 /** @var $node \TYPO3\CMS\Install\FolderStructure\LinkNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
274 $node = $this->getAccessibleMock('TYPO3\\CMS\\Install\\FolderStructure\\LinkNode', array('exists', 'getAbsolutePath'), array(), '', FALSE);
275 $path = PATH_site . 'typo3temp/' . uniqid('link_');
276 $target = PATH_site . uniqid('linkTarget_');
277 symlink($target, $path);
278 $this->testNodesToDelete[] = $path;
279 $node->expects($this->any())->method('exists')->will($this->returnValue(TRUE));
280 $node->expects($this->any())->method('getAbsolutePath')->will($this->returnValue($path));
281 $this->assertTrue($node->_call('isLink'));
282 }
283
284 /**
285 * @test
286 */
287 public function isFileReturnsFalseIfNameIsAFile() {
288 if (TYPO3_OS === 'WIN') {
289 $this->markTestSkipped('Test not available on Windows OS.');
290 }
291 /** @var $node \TYPO3\CMS\Install\FolderStructure\LinkNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
292 $node = $this->getAccessibleMock('TYPO3\\CMS\\Install\\FolderStructure\\LinkNode', array('exists', 'getAbsolutePath'), array(), '', FALSE);
293 $path = PATH_site . 'typo3temp/' . uniqid('file_');
294 touch($path);
295 $this->testNodesToDelete[] = $path;
296 $node->expects($this->any())->method('exists')->will($this->returnValue(TRUE));
297 $node->expects($this->any())->method('getAbsolutePath')->will($this->returnValue($path));
298 $this->assertFalse($node->_call('isLink'));
299 }
300
301 /**
302 * @test
303 * @expectedException \TYPO3\CMS\Install\FolderStructure\Exception\InvalidArgumentException
304 */
305 public function isTargetCorrectThrowsExceptionIfLinkNotExists() {
306 /** @var $node \TYPO3\CMS\Install\FolderStructure\LinkNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
307 $node = $this->getAccessibleMock('TYPO3\\CMS\\Install\\FolderStructure\\LinkNode', array('exists'), array(), '', FALSE);
308 $node->expects($this->once())->method('exists')->will($this->returnValue(FALSE));
309 $this->assertFalse($node->_call('isTargetCorrect'));
310 }
311
312 /**
313 * @test
314 * @expectedException \TYPO3\CMS\Install\FolderStructure\Exception\InvalidArgumentException
315 */
316 public function isTargetCorrectThrowsExceptionIfNodeIsNotALink() {
317 /** @var $node \TYPO3\CMS\Install\FolderStructure\LinkNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
318 $node = $this->getAccessibleMock('TYPO3\\CMS\\Install\\FolderStructure\\LinkNode', array('exists', 'isLink', 'getTarget'), array(), '', FALSE);
319 $node->expects($this->any())->method('exists')->will($this->returnValue(TRUE));
320 $node->expects($this->once())->method('isLink')->will($this->returnValue(FALSE));
321 $this->assertTrue($node->_call('isTargetCorrect'));
322 }
323
324 /**
325 * @test
326 */
327 public function isTargetCorrectReturnsTrueIfNoExpectedLinkTargetIsSpecified() {
328 /** @var $node \TYPO3\CMS\Install\FolderStructure\LinkNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
329 $node = $this->getAccessibleMock('TYPO3\\CMS\\Install\\FolderStructure\\LinkNode', array('exists', 'isLink', 'getTarget'), array(), '', FALSE);
330 $node->expects($this->any())->method('exists')->will($this->returnValue(TRUE));
331 $node->expects($this->any())->method('isLink')->will($this->returnValue(TRUE));
332 $node->expects($this->once())->method('getTarget')->will($this->returnValue(''));
333 $this->assertTrue($node->_call('isTargetCorrect'));
334 }
335
336 /**
337 * @test
338 */
339 public function isTargetCorrectReturnsTrueIfActualTargetIsIdenticalToSpecifiedTarget() {
340 if (TYPO3_OS === 'WIN') {
341 $this->markTestSkipped('Test not available on Windows OS.');
342 }
343 $path = PATH_site . 'typo3temp/' . uniqid('link_');
344 $target = uniqid('linkTarget_');
345 symlink($target, $path);
346 $this->testNodesToDelete[] = $path;
347 /** @var $node \TYPO3\CMS\Install\FolderStructure\LinkNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
348 $node = $this->getAccessibleMock('TYPO3\\CMS\\Install\\FolderStructure\\LinkNode',
349 array('exists', 'isLink', 'getTarget', 'getAbsolutePath'),
350 array(),
351 '',
352 FALSE
353 );
354 $node->expects($this->any())->method('exists')->will($this->returnValue(TRUE));
355 $node->expects($this->any())->method('isLink')->will($this->returnValue(TRUE));
356 $node->expects($this->once())->method('getTarget')->will($this->returnValue($target));
357 $node->expects($this->once())->method('getAbsolutePath')->will($this->returnValue($path));
358 $this->assertTrue($node->_call('isTargetCorrect'));
359 }
360
361 /**
362 * @test
363 */
364 public function isTargetCorrectReturnsFalseIfActualTargetIsNotIdenticalToSpecifiedTarget() {
365 if (TYPO3_OS === 'WIN') {
366 $this->markTestSkipped('Test not available on Windows OS.');
367 }
368 $path = PATH_site . 'typo3temp/' . uniqid('link_');
369 $target = uniqid('linkTarget_');
370 symlink($target, $path);
371 $this->testNodesToDelete[] = $path;
372 /** @var $node \TYPO3\CMS\Install\FolderStructure\LinkNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
373 $node = $this->getAccessibleMock('TYPO3\\CMS\\Install\\FolderStructure\\LinkNode',
374 array('exists', 'isLink', 'getTarget', 'getAbsolutePath'),
375 array(),
376 '',
377 FALSE
378 );
379 $node->expects($this->any())->method('exists')->will($this->returnValue(TRUE));
380 $node->expects($this->any())->method('isLink')->will($this->returnValue(TRUE));
381 $node->expects($this->once())->method('getTarget')->will($this->returnValue('foo'));
382 $node->expects($this->once())->method('getAbsolutePath')->will($this->returnValue($path));
383 $this->assertFalse($node->_call('isTargetCorrect'));
384 }
385
386 }