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