[!!!][TASK] Rewrite install tool
[Packages/TYPO3.CMS.git] / typo3 / sysext / install / Tests / Unit / FolderStructure / RootNodeTest.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 RootNodeTest 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\RootNodeException
51 */
52 public function constructorThrowsExceptionIfParentIsNotNull() {
53 /** @var $node \TYPO3\CMS\Install\FolderStructure\RootNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
54 $node = $this->getAccessibleMock('TYPO3\\CMS\\Install\\FolderStructure\\RootNode', array('isWindowsOs'), array(), '', FALSE);
55 $falseParent = $this->getMock(
56 'TYPO3\CMS\Install\FolderStructure\RootNodeInterface',
57 array(),
58 array(),
59 '',
60 FALSE
61 );
62 $node->__construct(array(), $falseParent);
63 }
64
65 /**
66 * @test
67 * @expectedException \TYPO3\CMS\Install\FolderStructure\Exception\InvalidArgumentException
68 */
69 public function constructorThrowsExceptionIfAbsolutePathIsNotSet() {
70 /** @var $node \TYPO3\CMS\Install\FolderStructure\RootNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
71 $node = $this->getAccessibleMock('TYPO3\\CMS\\Install\\FolderStructure\\RootNode', array('isWindowsOs'), array(), '', FALSE);
72 $structure = array(
73 'type' => 'root',
74 );
75 $node->__construct($structure, NULL);
76 }
77
78 /**
79 * @test
80 * @expectedException \TYPO3\CMS\Install\FolderStructure\Exception\InvalidArgumentException
81 */
82 public function constructorThrowsExceptionIfAbsolutePathIsNotAbsoluteOnWindows() {
83 /** @var $node \TYPO3\CMS\Install\FolderStructure\RootNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
84 $node = $this->getAccessibleMock('TYPO3\\CMS\\Install\\FolderStructure\\RootNode', array('isWindowsOs'), array(), '', FALSE);
85 $node
86 ->expects($this->any())
87 ->method('isWindowsOs')
88 ->will($this->returnValue(TRUE));
89 $structure = array(
90 'name' => '/bar'
91 );
92 $node->__construct($structure, NULL);
93 }
94
95 /**
96 * @test
97 * @expectedException \TYPO3\CMS\Install\FolderStructure\Exception\InvalidArgumentException
98 */
99 public function constructorThrowsExceptionIfAbsolutePathIsNotAbsoluteOnUnix() {
100 /** @var $node \TYPO3\CMS\Install\FolderStructure\RootNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
101 $node = $this->getAccessibleMock('TYPO3\\CMS\\Install\\FolderStructure\\RootNode', array('isWindowsOs'), array(), '', FALSE);
102 $node
103 ->expects($this->any())
104 ->method('isWindowsOs')
105 ->will($this->returnValue(FALSE));
106 $structure = array(
107 'name' => 'C:/bar'
108 );
109 $node->__construct($structure, NULL);
110 }
111
112 /**
113 * @test
114 */
115 public function constructorSetsParentToNull() {
116 /** @var $node \TYPO3\CMS\Install\FolderStructure\RootNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
117 $node = $this->getAccessibleMock('TYPO3\\CMS\\Install\\FolderStructure\\RootNode', array('isWindowsOs'), array(), '', FALSE);
118 $node
119 ->expects($this->any())
120 ->method('isWindowsOs')
121 ->will($this->returnValue(FALSE));
122 $structure = array(
123 'name' => '/bar'
124 );
125 $node->__construct($structure, NULL);
126 $this->assertNull($node->getParent());
127 }
128
129 /**
130 * @test
131 */
132 public function getChildrenReturnsChildCreatedByConstructor() {
133 /** @var $node \TYPO3\CMS\Install\FolderStructure\RootNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
134 $node = $this->getAccessibleMock('TYPO3\\CMS\\Install\\FolderStructure\\RootNode', array('isWindowsOs'), array(), '', FALSE);
135 $node
136 ->expects($this->any())
137 ->method('isWindowsOs')
138 ->will($this->returnValue(FALSE));
139 $childName = uniqid('test_');
140 $structure = array(
141 'name' => '/foo',
142 'children' => array(
143 array(
144 'type' => 'TYPO3\\CMS\\install\\FolderStructure\\DirectoryNode',
145 'name' => $childName,
146 ),
147 ),
148 );
149 $node->__construct($structure, NULL);
150 $children = $node->getChildren();
151 /** @var $child \TYPO3\CMS\install\FolderStructure\NodeInterface */
152 $child = $children[0];
153 $this->assertInstanceOf('TYPO3\\CMS\\install\\FolderStructure\\DirectoryNode', $child);
154 $this->assertSame($childName, $child->getName());
155 }
156
157 /**
158 * @test
159 */
160 public function constructorSetsTargetPermission() {
161 /** @var $node \TYPO3\CMS\Install\FolderStructure\RootNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
162 $node = $this->getAccessibleMock('TYPO3\\CMS\\Install\\FolderStructure\\RootNode', array('isWindowsOs'), array(), '', FALSE);
163 $node
164 ->expects($this->any())
165 ->method('isWindowsOs')
166 ->will($this->returnValue(FALSE));
167 $targetPermission = '2550';
168 $structure = array(
169 'name' => '/foo',
170 'targetPermission' => $targetPermission,
171 );
172 $node->__construct($structure, NULL);
173 $this->assertSame($targetPermission, $node->getTargetPermission());
174 }
175
176 /**
177 * @test
178 */
179 public function constructorSetsName() {
180 /** @var $node \TYPO3\CMS\Install\FolderStructure\RootNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
181 $node = $this->getAccessibleMock('TYPO3\\CMS\\Install\\FolderStructure\\RootNode', array('isWindowsOs'), array(), '', FALSE);
182 $node
183 ->expects($this->any())
184 ->method('isWindowsOs')
185 ->will($this->returnValue(FALSE));
186 $name = '/' . uniqid('test_');
187 $node->__construct(array('name' => $name), NULL);
188 $this->assertSame($name, $node->getName());
189 }
190
191 /**
192 * @test
193 */
194 public function getStatusReturnsArrayWithOkStatusAndCallsOwnStatusMethods() {
195 /** @var $node \TYPO3\CMS\Install\FolderStructure\DirectoryNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
196 $node = $this->getAccessibleMock(
197 'TYPO3\\CMS\\Install\\FolderStructure\\RootNode',
198 array('getAbsolutePath', 'exists', 'isDirectory', 'isWritable', 'isPermissionCorrect'),
199 array(),
200 '',
201 FALSE
202 );
203 $path = PATH_site . 'typo3temp/' . uniqid('dir_');
204 touch ($path);
205 $this->testNodesToDelete[] = $path;
206 $node->expects($this->any())->method('getAbsolutePath')->will($this->returnValue($path));
207 $node->expects($this->once())->method('exists')->will($this->returnValue(TRUE));
208 $node->expects($this->once())->method('isDirectory')->will($this->returnValue(TRUE));
209 $node->expects($this->once())->method('isPermissionCorrect')->will($this->returnValue(TRUE));
210 $node->expects($this->once())->method('isWritable')->will($this->returnValue(TRUE));
211 $statusArray = $node->getStatus();
212 /** @var $status \TYPO3\CMS\Install\Status\StatusInterface */
213 $status = $statusArray[0];
214 $this->assertInstanceOf('\TYPO3\CMS\Install\Status\OkStatus', $status);
215 }
216
217 /**
218 * @test
219 */
220 public function getStatusCallsGetChildrenStatusForStatus() {
221 /** @var $node \TYPO3\CMS\Install\FolderStructure\DirectoryNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
222 $node = $this->getAccessibleMock(
223 'TYPO3\\CMS\\Install\\FolderStructure\\RootNode',
224 array('getAbsolutePath', 'exists', 'isDirectory', 'isWritable', 'isPermissionCorrect', 'getChildrenStatus'),
225 array(),
226 '',
227 FALSE
228 );
229 $path = PATH_site . 'typo3temp/' . uniqid('dir_');
230 touch ($path);
231 $this->testNodesToDelete[] = $path;
232 $node->expects($this->any())->method('getAbsolutePath')->will($this->returnValue($path));
233 $node->expects($this->any())->method('exists')->will($this->returnValue(TRUE));
234 $node->expects($this->any())->method('isDirectory')->will($this->returnValue(TRUE));
235 $node->expects($this->any())->method('isPermissionCorrect')->will($this->returnValue(TRUE));
236 $node->expects($this->any())->method('isWritable')->will($this->returnValue(TRUE));
237 $childStatusMock = $this->getMock('TYPO3\\CMS\\Install\\Status\\ErrorStatus', array(), array(), '', FALSE);
238 $node->expects($this->once())->method('getChildrenStatus')->will($this->returnValue(array($childStatusMock)));
239 $statusArray = $node->getStatus();
240 /** @var $status \TYPO3\CMS\Install\Status\StatusInterface */
241 $statusSelf = $statusArray[0];
242 $statusOfChild = $statusArray[1];
243 $this->assertInstanceOf('\TYPO3\CMS\Install\Status\OkStatus', $statusSelf);
244 $this->assertInstanceOf('\TYPO3\CMS\Install\Status\ErrorStatus', $statusOfChild);
245 }
246
247 /**
248 * @test
249 */
250 public function getAbsolutePathReturnsGivenName() {
251 /** @var $node \TYPO3\CMS\Install\FolderStructure\RootNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
252 $node = $this->getAccessibleMock('TYPO3\\CMS\\Install\\FolderStructure\\RootNode', array('isWindowsOs'), array(), '', FALSE);
253 $node
254 ->expects($this->any())
255 ->method('isWindowsOs')
256 ->will($this->returnValue(FALSE));
257 $path = '/foo/bar';
258 $structure = array(
259 'name' => $path,
260 );
261 $node->__construct($structure, NULL);
262 $this->assertSame($path, $node->getAbsolutePath());
263 }
264 }