[FEATURE] ext:install Implement template files in FolderStructure
[Packages/TYPO3.CMS.git] / typo3 / sysext / install / Tests / Unit / FolderStructure / FileNodeTest.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 FileNodeTest 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\FileNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
54 $node = $this->getAccessibleMock('TYPO3\\CMS\\Install\\FolderStructure\\FileNode', 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\FileNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
65 $node = $this->getAccessibleMock('TYPO3\\CMS\\Install\\FolderStructure\\FileNode', 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\FileNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
78 $node = $this->getAccessibleMock('TYPO3\\CMS\\Install\\FolderStructure\\FileNode', array('dummy'), array(), '', FALSE);
79 $structure = array(
80 'name' => 'foo',
81 );
82 $node->__construct($structure, $parent);
83 $this->assertSame($parent, $node->getParent());
84 }
85
86 /**
87 * @test
88 */
89 public function constructorSetsTargetPermission() {
90 $parent = $this->getMock('TYPO3\CMS\Install\FolderStructure\NodeInterface', array(), array(), '', FALSE);
91 /** @var $node \TYPO3\CMS\Install\FolderStructure\FileNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
92 $node = $this->getAccessibleMock('TYPO3\\CMS\\Install\\FolderStructure\\FileNode', array('dummy'), array(), '', FALSE);
93 $targetPermission = '660';
94 $structure = array(
95 'name' => 'foo',
96 'targetPermission' => $targetPermission,
97 );
98 $node->__construct($structure, $parent);
99 $this->assertSame($targetPermission, $node->getTargetPermission());
100 }
101
102 /**
103 * @test
104 */
105 public function constructorSetsName() {
106 /** @var $node \TYPO3\CMS\Install\FolderStructure\FileNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
107 $node = $this->getAccessibleMock('TYPO3\\CMS\\Install\\FolderStructure\\FileNode', array('dummy'), array(), '', FALSE);
108 $parent = $this->getMock('TYPO3\CMS\Install\FolderStructure\RootNodeInterface', array(), array(), '', FALSE);
109 $name = uniqid('test_');
110 $node->__construct(array('name' => $name), $parent);
111 $this->assertSame($name, $node->getName());
112 }
113
114 /**
115 * @test
116 * @expectedException \TYPO3\CMS\Install\FolderStructure\Exception\InvalidArgumentException
117 */
118 public function constructorThrowsExceptionIfBothTargetContentAndTargetContentFileAreSet() {
119 /** @var $node \TYPO3\CMS\Install\FolderStructure\FileNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
120 $node = $this->getAccessibleMock('TYPO3\\CMS\\Install\\FolderStructure\\FileNode', array('dummy'), array(), '', FALSE);
121 $parent = $this->getMock('TYPO3\CMS\Install\FolderStructure\RootNodeInterface', array(), array(), '', FALSE);
122 $structure = array(
123 'name' => 'foo',
124 'targetContent' => 'foo',
125 'targetContentFile' => 'aPath',
126 );
127 $node->__construct($structure, $parent);
128 }
129
130 /**
131 * @test
132 */
133 public function constructorSetsTargetContent() {
134 /** @var $node \TYPO3\CMS\Install\FolderStructure\FileNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
135 $node = $this->getAccessibleMock('TYPO3\\CMS\\Install\\FolderStructure\\FileNode', array('dummy'), array(), '', FALSE);
136 $parent = $this->getMock('TYPO3\CMS\Install\FolderStructure\RootNodeInterface', array(), array(), '', FALSE);
137 $targetContent = uniqid('content_');
138 $structure = array(
139 'name' => 'foo',
140 'targetContent' => $targetContent,
141 );
142 $node->__construct($structure, $parent);
143 $this->assertSame($targetContent, $node->_get('targetContent'));
144 }
145
146 /**
147 * @test
148 */
149 public function constructorSetsTargetContentToContentOfTargetContentFile() {
150 /** @var $node \TYPO3\CMS\Install\FolderStructure\FileNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
151 $node = $this->getAccessibleMock('TYPO3\\CMS\\Install\\FolderStructure\\FileNode', array('dummy'), array(), '', FALSE);
152 $parent = $this->getMock('TYPO3\CMS\Install\FolderStructure\RootNodeInterface', array(), array(), '', FALSE);
153 $targetFile = PATH_site . 'typo3temp/' . uniqid('test_');
154 $targetContent = uniqid('content_');
155 file_put_contents($targetFile, $targetContent);
156 $this->testNodesToDelete[] = $targetFile;
157 $structure = array(
158 'name' => 'foo',
159 'targetContentFile' => $targetFile,
160 );
161 $node->__construct($structure, $parent);
162 $this->assertSame($targetContent, $node->_get('targetContent'));
163 }
164
165 /**
166 * @test
167 * @expectedException \TYPO3\CMS\Install\FolderStructure\Exception\InvalidArgumentException
168 */
169 public function constructorThrowsExceptionIfTargetContentFileDoesNotExist() {
170 /** @var $node \TYPO3\CMS\Install\FolderStructure\FileNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
171 $node = $this->getAccessibleMock('TYPO3\\CMS\\Install\\FolderStructure\\FileNode', array('dummy'), array(), '', FALSE);
172 $parent = $this->getMock('TYPO3\CMS\Install\FolderStructure\RootNodeInterface', array(), array(), '', FALSE);
173 $targetFile = PATH_site . 'typo3temp/' . uniqid('test_');
174 $structure = array(
175 'name' => 'foo',
176 'targetContentFile' => $targetFile,
177 );
178 $node->__construct($structure, $parent);
179 }
180
181 /**
182 * @test
183 */
184 public function targetContentIsNullIfNotGiven() {
185 /** @var $node \TYPO3\CMS\Install\FolderStructure\FileNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
186 $node = $this->getAccessibleMock('TYPO3\\CMS\\Install\\FolderStructure\\FileNode', array('dummy'), array(), '', FALSE);
187 $parent = $this->getMock('TYPO3\CMS\Install\FolderStructure\RootNodeInterface', array(), array(), '', FALSE);
188 $structure = array(
189 'name' => 'foo',
190 );
191 $node->__construct($structure, $parent);
192 $this->assertNull($node->_get('targetContent'));
193 }
194
195 /**
196 * @test
197 */
198 public function getStatusReturnsArray() {
199 /** @var $node \TYPO3\CMS\Install\FolderStructure\FileNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
200 $node = $this->getAccessibleMock(
201 'TYPO3\\CMS\\Install\\FolderStructure\\FileNode',
202 array('getAbsolutePath', 'exists', 'isFile', 'isWritable', 'isPermissionCorrect', 'isContentCorrect'),
203 array(),
204 '',
205 FALSE
206 );
207 $path = PATH_site . 'typo3temp/' . uniqid('dir_');
208 $node->expects($this->any())->method('getAbsolutePath')->will($this->returnValue($path));
209 $node->expects($this->any())->method('exists')->will($this->returnValue(TRUE));
210 $node->expects($this->any())->method('isFile')->will($this->returnValue(TRUE));
211 $node->expects($this->any())->method('isPermissionCorrect')->will($this->returnValue(TRUE));
212 $node->expects($this->any())->method('isWritable')->will($this->returnValue(TRUE));
213 $node->expects($this->any())->method('isContentCorrect')->will($this->returnValue(TRUE));
214 $this->assertInternalType('array', $node->getStatus());
215 }
216
217 /**
218 * @test
219 */
220 public function getStatusReturnsArrayWithWarningStatusIFileNotExists() {
221 /** @var $node \TYPO3\CMS\Install\FolderStructure\FileNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
222 $node = $this->getAccessibleMock(
223 'TYPO3\\CMS\\Install\\FolderStructure\\FileNode',
224 array('getAbsolutePath', 'exists', 'isFile', 'isWritable', 'isPermissionCorrect', 'isContentCorrect'),
225 array(),
226 '',
227 FALSE
228 );
229 $path = PATH_site . 'typo3temp/' . uniqid('dir_');
230 $node->expects($this->any())->method('getAbsolutePath')->will($this->returnValue($path));
231 $node->expects($this->any())->method('exists')->will($this->returnValue(FALSE));
232 $node->expects($this->any())->method('isFile')->will($this->returnValue(TRUE));
233 $node->expects($this->any())->method('isPermissionCorrect')->will($this->returnValue(TRUE));
234 $node->expects($this->any())->method('isWritable')->will($this->returnValue(TRUE));
235 $node->expects($this->any())->method('isContentCorrect')->will($this->returnValue(TRUE));
236 $statusArray = $node->getStatus();
237 /** @var $status \TYPO3\CMS\Install\Status\StatusInterface */
238 $status = $statusArray[0];
239 $this->assertInstanceOf('\TYPO3\CMS\Install\Status\WarningStatus', $status);
240 }
241
242 /**
243 * @test
244 */
245 public function getStatusReturnsArrayWithErrorStatusIfNodeIsNotAFile() {
246 /** @var $node \TYPO3\CMS\Install\FolderStructure\FileNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
247 $node = $this->getAccessibleMock(
248 'TYPO3\\CMS\\Install\\FolderStructure\\FileNode',
249 array('getAbsolutePath', 'exists', 'isFile', 'isWritable', 'isPermissionCorrect', 'isContentCorrect'),
250 array(),
251 '',
252 FALSE
253 );
254 $path = PATH_site . 'typo3temp/' . uniqid('dir_');
255 touch ($path);
256 $this->testNodesToDelete[] = $path;
257 $node->expects($this->any())->method('getAbsolutePath')->will($this->returnValue($path));
258 $node->expects($this->any())->method('exists')->will($this->returnValue(TRUE));
259 $node->expects($this->any())->method('isFile')->will($this->returnValue(FALSE));
260 $node->expects($this->any())->method('isPermissionCorrect')->will($this->returnValue(TRUE));
261 $node->expects($this->any())->method('isWritable')->will($this->returnValue(TRUE));
262 $node->expects($this->any())->method('isContentCorrect')->will($this->returnValue(TRUE));
263 $statusArray = $node->getStatus();
264 /** @var $status \TYPO3\CMS\Install\Status\StatusInterface */
265 $status = $statusArray[0];
266 $this->assertInstanceOf('\TYPO3\CMS\Install\Status\ErrorStatus', $status);
267 }
268
269 /**
270 * @test
271 */
272 public function getStatusReturnsArrayWithWarningStatusIfFileExistsButIsNotWritable() {
273 /** @var $node \TYPO3\CMS\Install\FolderStructure\FileNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
274 $node = $this->getAccessibleMock(
275 'TYPO3\\CMS\\Install\\FolderStructure\\FileNode',
276 array('getAbsolutePath', 'exists', 'isFile', 'isWritable', 'isPermissionCorrect', 'isContentCorrect'),
277 array(),
278 '',
279 FALSE
280 );
281 $path = PATH_site . 'typo3temp/' . uniqid('dir_');
282 touch ($path);
283 $this->testNodesToDelete[] = $path;
284 $node->expects($this->any())->method('getAbsolutePath')->will($this->returnValue($path));
285 $node->expects($this->any())->method('exists')->will($this->returnValue(TRUE));
286 $node->expects($this->any())->method('isFile')->will($this->returnValue(TRUE));
287 $node->expects($this->any())->method('isPermissionCorrect')->will($this->returnValue(TRUE));
288 $node->expects($this->any())->method('isWritable')->will($this->returnValue(FALSE));
289 $node->expects($this->any())->method('isContentCorrect')->will($this->returnValue(TRUE));
290 $statusArray = $node->getStatus();
291 /** @var $status \TYPO3\CMS\Install\Status\StatusInterface */
292 $status = $statusArray[0];
293 $this->assertInstanceOf('\TYPO3\CMS\Install\Status\WarningStatus', $status);
294 }
295
296 /**
297 * @test
298 */
299 public function getStatusReturnsArrayWithWarningStatusIfFileExistsButPermissionAreNotCorrect() {
300 /** @var $node \TYPO3\CMS\Install\FolderStructure\FileNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
301 $node = $this->getAccessibleMock(
302 'TYPO3\\CMS\\Install\\FolderStructure\\FileNode',
303 array('getAbsolutePath', 'exists', 'isFile', 'isWritable', 'isPermissionCorrect', 'isContentCorrect'),
304 array(),
305 '',
306 FALSE
307 );
308 $path = PATH_site . 'typo3temp/' . uniqid('dir_');
309 touch ($path);
310 $this->testNodesToDelete[] = $path;
311 $node->expects($this->any())->method('getAbsolutePath')->will($this->returnValue($path));
312 $node->expects($this->any())->method('exists')->will($this->returnValue(TRUE));
313 $node->expects($this->any())->method('isFile')->will($this->returnValue(TRUE));
314 $node->expects($this->any())->method('isPermissionCorrect')->will($this->returnValue(FALSE));
315 $node->expects($this->any())->method('isWritable')->will($this->returnValue(TRUE));
316 $node->expects($this->any())->method('isContentCorrect')->will($this->returnValue(TRUE));
317 $statusArray = $node->getStatus();
318 /** @var $status \TYPO3\CMS\Install\Status\StatusInterface */
319 $status = $statusArray[0];
320 $this->assertInstanceOf('\TYPO3\CMS\Install\Status\WarningStatus', $status);
321 }
322
323 /**
324 * @test
325 */
326 public function getStatusReturnsArrayWithErrorStatusIfFileExistsButContentIsNotCorrect() {
327 /** @var $node \TYPO3\CMS\Install\FolderStructure\FileNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
328 $node = $this->getAccessibleMock(
329 'TYPO3\\CMS\\Install\\FolderStructure\\FileNode',
330 array('getAbsolutePath', 'exists', 'isFile', 'isWritable', 'isPermissionCorrect', 'isContentCorrect'),
331 array(),
332 '',
333 FALSE
334 );
335 $path = PATH_site . 'typo3temp/' . uniqid('dir_');
336 touch ($path);
337 $this->testNodesToDelete[] = $path;
338 $node->expects($this->any())->method('getAbsolutePath')->will($this->returnValue($path));
339 $node->expects($this->any())->method('exists')->will($this->returnValue(TRUE));
340 $node->expects($this->any())->method('isFile')->will($this->returnValue(TRUE));
341 $node->expects($this->any())->method('isPermissionCorrect')->will($this->returnValue(TRUE));
342 $node->expects($this->any())->method('isWritable')->will($this->returnValue(TRUE));
343 $node->expects($this->any())->method('isContentCorrect')->will($this->returnValue(FALSE));
344 $statusArray = $node->getStatus();
345 /** @var $status \TYPO3\CMS\Install\Status\StatusInterface */
346 $status = $statusArray[0];
347 $this->assertInstanceOf('\TYPO3\CMS\Install\Status\ErrorStatus', $status);
348 }
349
350 /**
351 * @test
352 */
353 public function getStatusReturnsArrayWithOkStatusIfFileExistsAndPermissionAreCorrect() {
354 /** @var $node \TYPO3\CMS\Install\FolderStructure\FileNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
355 $node = $this->getAccessibleMock(
356 'TYPO3\\CMS\\Install\\FolderStructure\\FileNode',
357 array('getAbsolutePath', 'exists', 'isFile', 'isWritable', 'isPermissionCorrect', 'isContentCorrect'),
358 array(),
359 '',
360 FALSE
361 );
362 $path = PATH_site . 'typo3temp/' . uniqid('dir_');
363 touch ($path);
364 $this->testNodesToDelete[] = $path;
365 $node->expects($this->any())->method('getAbsolutePath')->will($this->returnValue($path));
366 $node->expects($this->any())->method('exists')->will($this->returnValue(TRUE));
367 $node->expects($this->any())->method('isFile')->will($this->returnValue(TRUE));
368 $node->expects($this->any())->method('isPermissionCorrect')->will($this->returnValue(TRUE));
369 $node->expects($this->any())->method('isWritable')->will($this->returnValue(TRUE));
370 $node->expects($this->any())->method('isContentCorrect')->will($this->returnValue(TRUE));
371 $statusArray = $node->getStatus();
372 /** @var $status \TYPO3\CMS\Install\Status\StatusInterface */
373 $status = $statusArray[0];
374 $this->assertInstanceOf('\TYPO3\CMS\Install\Status\OkStatus', $status);
375 }
376
377 /**
378 * @test
379 */
380 public function fixCallsFixSelfAndReturnsItsResult() {
381 /** @var $node \TYPO3\CMS\Install\FolderStructure\FileNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
382 $node = $this->getAccessibleMock(
383 'TYPO3\\CMS\\Install\\FolderStructure\\FileNode',
384 array('fixSelf'),
385 array(),
386 '',
387 FALSE
388 );
389 $uniqueReturn = array(uniqid('foo_'));
390 $node->expects($this->once())->method('fixSelf')->will($this->returnValue($uniqueReturn));
391 $this->assertSame($uniqueReturn, $node->fix());
392 }
393
394 /**
395 * @test
396 */
397 public function fixSelfCallsCreateFileIfFileDoesNotExistAndReturnsResult() {
398 /** @var $node \TYPO3\CMS\Install\FolderStructure\FileNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
399 $node = $this->getAccessibleMock(
400 'TYPO3\\CMS\\Install\\FolderStructure\\FileNode',
401 array('exists', 'createFile', 'setContent', 'getAbsolutePath', 'isFile', 'isPermissionCorrect'),
402 array(),
403 '',
404 FALSE
405 );
406 $node->expects($this->any())->method('exists')->will($this->returnValue(FALSE));
407 $node->expects($this->any())->method('isFile')->will($this->returnValue(TRUE));
408 $node->expects($this->any())->method('isPermissionCorrect')->will($this->returnValue(TRUE));
409 $uniqueReturn = uniqid();
410 $node->expects($this->once())->method('createFile')->will($this->returnValue($uniqueReturn));
411 $actualReturn = $node->_call('fixSelf');
412 $actualReturn = $actualReturn[0];
413 $this->assertSame($uniqueReturn, $actualReturn);
414 }
415
416 /**
417 * @test
418 */
419 public function fixSelfCallsSetsContentIfFileCreationWasSuccessfulAndTargetContentIsNotNullAndReturnsResult() {
420 /** @var $node \TYPO3\CMS\Install\FolderStructure\FileNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
421 $node = $this->getAccessibleMock(
422 'TYPO3\\CMS\\Install\\FolderStructure\\FileNode',
423 array('exists', 'createFile', 'setContent', 'getAbsolutePath', 'isFile', 'isPermissionCorrect'),
424 array(),
425 '',
426 FALSE
427 );
428 $node->expects($this->any())->method('exists')->will($this->returnValue(FALSE));
429 $node->expects($this->any())->method('isFile')->will($this->returnValue(TRUE));
430 $node->expects($this->any())->method('isPermissionCorrect')->will($this->returnValue(TRUE));
431 $uniqueReturn = uniqid();
432 $createFileStatus = $this->getMock('TYPO3\\CMS\\Install\\Status\\OkStatus', array(), array(), '', FALSE);
433 $node->expects($this->any())->method('createFile')->will($this->returnValue($createFileStatus));
434 $node->_set('targetContent', 'foo');
435 $node->expects($this->once())->method('setContent')->will($this->returnValue($uniqueReturn));
436 $actualReturn = $node->_call('fixSelf');
437 $actualReturn = $actualReturn[1];
438 $this->assertSame($uniqueReturn, $actualReturn);
439 }
440
441 /**
442 * @test
443 */
444 public function fixSelfDoesNotCallSetContentIfFileCreationFailed() {
445 /** @var $node \TYPO3\CMS\Install\FolderStructure\FileNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
446 $node = $this->getAccessibleMock(
447 'TYPO3\\CMS\\Install\\FolderStructure\\FileNode',
448 array('exists', 'createFile', 'setContent', 'getAbsolutePath', 'isFile', 'isPermissionCorrect'),
449 array(),
450 '',
451 FALSE
452 );
453 $node->expects($this->any())->method('exists')->will($this->returnValue(FALSE));
454 $node->expects($this->any())->method('isFile')->will($this->returnValue(TRUE));
455 $node->expects($this->any())->method('isPermissionCorrect')->will($this->returnValue(TRUE));
456 $createFileStatus = $this->getMock('TYPO3\\CMS\\Install\\Status\\ErrorStatus', array(), array(), '', FALSE);
457 $node->expects($this->any())->method('createFile')->will($this->returnValue($createFileStatus));
458 $node->_set('targetContent', 'foo');
459 $node->expects($this->never())->method('setContent');
460 $node->_call('fixSelf');
461 }
462
463 /**
464 * @test
465 */
466 public function fixSelfDoesNotCallSetContentIfFileTargetContentIsNull() {
467 /** @var $node \TYPO3\CMS\Install\FolderStructure\FileNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
468 $node = $this->getAccessibleMock(
469 'TYPO3\\CMS\\Install\\FolderStructure\\FileNode',
470 array('exists', 'createFile', 'setContent', 'getAbsolutePath', 'isFile', 'isPermissionCorrect'),
471 array(),
472 '',
473 FALSE
474 );
475 $node->expects($this->any())->method('exists')->will($this->returnValue(FALSE));
476 $node->expects($this->any())->method('isFile')->will($this->returnValue(TRUE));
477 $node->expects($this->any())->method('isPermissionCorrect')->will($this->returnValue(TRUE));
478 $createFileStatus = $this->getMock('TYPO3\\CMS\\Install\\Status\\OkStatus', array(), array(), '', FALSE);
479 $node->expects($this->any())->method('createFile')->will($this->returnValue($createFileStatus));
480 $node->_set('targetContent', NULL);
481 $node->expects($this->never())->method('setContent');
482 $node->_call('fixSelf');
483 }
484
485 /**
486 * @test
487 */
488 public function fixSelfReturnsErrorStatusIfNodeExistsButIsNotAFileAndReturnsResult() {
489 /** @var $node \TYPO3\CMS\Install\FolderStructure\FileNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
490 $node = $this->getAccessibleMock(
491 'TYPO3\\CMS\\Install\\FolderStructure\\FileNode',
492 array('exists', 'createFile', 'getAbsolutePath', 'isFile', 'isPermissionCorrect', 'fixPermission'),
493 array(),
494 '',
495 FALSE
496 );
497 $node->expects($this->any())->method('exists')->will($this->returnValue(TRUE));
498 $node->expects($this->any())->method('isFile')->will($this->returnValue(TRUE));
499 $node->expects($this->any())->method('isPermissionCorrect')->will($this->returnValue(FALSE));
500 $uniqueReturn = uniqid();
501 $node->expects($this->once())->method('fixPermission')->will($this->returnValue($uniqueReturn));
502 $this->assertSame(array($uniqueReturn), $node->_call('fixSelf'));
503 }
504
505 /**
506 * @test
507 */
508 public function fixSelfCallsFixPermissionIfFileExistsButPermissionAreWrong() {
509 /** @var $node \TYPO3\CMS\Install\FolderStructure\FileNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
510 $node = $this->getAccessibleMock(
511 'TYPO3\\CMS\\Install\\FolderStructure\\FileNode',
512 array('exists', 'createFile', 'getAbsolutePath', 'isFile', 'isPermissionCorrect', 'getRelativePathBelowSiteRoot'),
513 array(),
514 '',
515 FALSE
516 );
517 $node->expects($this->any())->method('exists')->will($this->returnValue(TRUE));
518 $node->expects($this->once())->method('isFile')->will($this->returnValue(FALSE));
519 $node->expects($this->any())->method('isPermissionCorrect')->will($this->returnValue(TRUE));
520 $resultArray = $node->_call('fixSelf');
521 $this->assertInstanceOf('TYPO3\\CMS\Install\\Status\\StatusInterface', $resultArray[0]);
522 }
523
524 /**
525 * @test
526 */
527 public function fixSelfReturnsArrayOfStatusMessages() {
528 $node = $this->getAccessibleMock(
529 'TYPO3\\CMS\\Install\\FolderStructure\\FileNode',
530 array('exists', 'isFile', 'isPermissionCorrect'),
531 array(),
532 '',
533 FALSE
534 );
535 $node->expects($this->any())->method('exists')->will($this->returnValue(TRUE));
536 $node->expects($this->any())->method('isFile')->will($this->returnValue(TRUE));
537 $node->expects($this->any())->method('isPermissionCorrect')->will($this->returnValue(TRUE));
538 $this->assertInternalType('array', $node->_call('fixSelf'));
539 }
540
541 /**
542 * @test
543 * @expectedException \TYPO3\CMS\Install\FolderStructure\Exception
544 */
545 public function createFileThrowsExceptionIfNodeExists() {
546 /** @var $node \TYPO3\CMS\Install\FolderStructure\FileNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
547 $node = $this->getAccessibleMock('TYPO3\\CMS\\Install\\FolderStructure\\FileNode', array('exists', 'getAbsolutePath'), array(), '', FALSE);
548 $node->expects($this->once())->method('getAbsolutePath')->will($this->returnValue(''));
549 $node->expects($this->once())->method('exists')->will($this->returnValue(TRUE));
550 $node->_call('createFile');
551 }
552
553 /**
554 * @test
555 */
556 public function createFileReturnsOkStatusIfFileWasCreated() {
557 /** @var $node \TYPO3\CMS\Install\FolderStructure\FileNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
558 $node = $this->getAccessibleMock('TYPO3\\CMS\\Install\\FolderStructure\\FileNode', array('exists', 'getAbsolutePath'), array(), '', FALSE);
559 $path = PATH_site . 'typo3temp/' . uniqid('file_');
560 $this->testNodesToDelete[] = $path;
561 $node->expects($this->once())->method('exists')->will($this->returnValue(FALSE));
562 $node->expects($this->any())->method('getAbsolutePath')->will($this->returnValue($path));
563 $this->assertInstanceOf('TYPO3\\CMS\Install\\Status\\StatusInterface', $node->_call('createFile'));
564 }
565
566 /**
567 * @test
568 */
569 public function createFileCreatesFile() {
570 /** @var $node \TYPO3\CMS\Install\FolderStructure\FileNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
571 $node = $this->getAccessibleMock('TYPO3\\CMS\\Install\\FolderStructure\\FileNode', array('exists', 'getAbsolutePath'), array(), '', FALSE);
572 $path = PATH_site . 'typo3temp/' . uniqid('file_');
573 $this->testNodesToDelete[] = $path;
574 $node->expects($this->once())->method('exists')->will($this->returnValue(FALSE));
575 $node->expects($this->any())->method('getAbsolutePath')->will($this->returnValue($path));
576 $node->_call('createFile');
577 $this->assertTrue(is_file($path));
578 }
579
580 /**
581 * @test
582 */
583 public function createFileReturnsErrorStatusIfFileWasNotCreated() {
584 if (TYPO3_OS === 'WIN') {
585 $this->markTestSkipped('Test not available on Windows OS.');
586 }
587 /** @var $node \TYPO3\CMS\Install\FolderStructure\FileNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
588 $node = $this->getAccessibleMock('TYPO3\\CMS\\Install\\FolderStructure\\FileNode', array('exists', 'getAbsolutePath'), array(), '', FALSE);
589 $path = PATH_site . 'typo3temp/' . uniqid('root_');
590 mkdir($path);
591 chmod($path, octdec(2550));
592 $subPath = $path . '/' . uniqid('file_');
593 $this->testNodesToDelete[] = $path;
594 $node->expects($this->once())->method('exists')->will($this->returnValue(FALSE));
595 $node->expects($this->any())->method('getAbsolutePath')->will($this->returnValue($subPath));
596 $this->assertInstanceOf('TYPO3\\CMS\Install\\Status\\StatusInterface', $node->_call('createFile'));
597 }
598
599 /**
600 * @test
601 * @expectedException \TYPO3\CMS\Install\FolderStructure\Exception
602 */
603 public function isContentCorrectThrowsExceptionIfTargetIsNotAFile() {
604 /** @var $node \TYPO3\CMS\Install\FolderStructure\FileNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
605 $node = $this->getAccessibleMock('TYPO3\\CMS\\Install\\FolderStructure\\FileNode', array('getAbsolutePath'), array(), '', FALSE);
606 $path = PATH_site . 'typo3temp/' . uniqid('dir_');
607 mkdir($path);
608 $this->testNodesToDelete[] = $path;
609 $node->expects($this->any())->method('getAbsolutePath')->will($this->returnValue($path));
610 $node->_call('isContentCorrect');
611 }
612
613 /**
614 * @test
615 */
616 public function isContentCorrectReturnsTrueIfTargetContentPropertyIsNull() {
617 /** @var $node \TYPO3\CMS\Install\FolderStructure\FileNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
618 $node = $this->getAccessibleMock('TYPO3\\CMS\\Install\\FolderStructure\\FileNode', array('getAbsolutePath'), array(), '', FALSE);
619 $path = PATH_site . 'typo3temp/' . uniqid('file_');
620 touch($path);
621 $this->testNodesToDelete[] = $path;
622 $node->expects($this->any())->method('getAbsolutePath')->will($this->returnValue($path));
623 $node->_set('targetContent', NULL);
624 $this->assertTrue($node->_call('isContentCorrect'));
625 }
626
627 /**
628 * @test
629 */
630 public function isContentCorrectReturnsTrueIfTargetContentEqualsCurrentContent() {
631 /** @var $node \TYPO3\CMS\Install\FolderStructure\FileNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
632 $node = $this->getAccessibleMock('TYPO3\\CMS\\Install\\FolderStructure\\FileNode', array('getAbsolutePath'), array(), '', FALSE);
633 $path = PATH_site . 'typo3temp/' . uniqid('file_');
634 $content = uniqid('content_');
635 file_put_contents($path, $content);
636 $this->testNodesToDelete[] = $path;
637 $node->expects($this->any())->method('getAbsolutePath')->will($this->returnValue($path));
638 $node->_set('targetContent', $content);
639 $this->assertTrue($node->_call('isContentCorrect'));
640 }
641
642 /**
643 * @test
644 */
645 public function isContentCorrectReturnsFalseIfTargetContentNotEqualsCurrentContent() {
646 /** @var $node \TYPO3\CMS\Install\FolderStructure\FileNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
647 $node = $this->getAccessibleMock('TYPO3\\CMS\\Install\\FolderStructure\\FileNode', array('getAbsolutePath'), array(), '', FALSE);
648 $path = PATH_site . 'typo3temp/' . uniqid('file_');
649 $content = uniqid('content1_');
650 $targetContent = uniqid('content2_');
651 file_put_contents($path, $content);
652 $this->testNodesToDelete[] = $path;
653 $node->expects($this->any())->method('getAbsolutePath')->will($this->returnValue($path));
654 $node->_set('targetContent', $targetContent);
655 $this->assertFalse($node->_call('isContentCorrect'));
656 }
657
658 /**
659 * @test
660 * @expectedException \TYPO3\CMS\Install\FolderStructure\Exception
661 */
662 public function setContentThrowsExceptionIfTargetIsNotAFile() {
663 /** @var $node \TYPO3\CMS\Install\FolderStructure\FileNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
664 $node = $this->getAccessibleMock('TYPO3\\CMS\\Install\\FolderStructure\\FileNode', array('getAbsolutePath'), array(), '', FALSE);
665 $path = PATH_site . 'typo3temp/' . uniqid('dir_');
666 mkdir($path);
667 $this->testNodesToDelete[] = $path;
668 $node->expects($this->any())->method('getAbsolutePath')->will($this->returnValue($path));
669 $node->_set('targetContent', 'foo');
670 $node->_call('setContent');
671 }
672
673 /**
674 * @test
675 * @expectedException \TYPO3\CMS\Install\FolderStructure\Exception
676 */
677 public function setContentThrowsExceptionIfTargetContentIsNull() {
678 /** @var $node \TYPO3\CMS\Install\FolderStructure\FileNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
679 $node = $this->getAccessibleMock('TYPO3\\CMS\\Install\\FolderStructure\\FileNode', array('getAbsolutePath'), array(), '', FALSE);
680 $path = PATH_site . 'typo3temp/' . uniqid('file_');
681 touch($path);
682 $this->testNodesToDelete[] = $path;
683 $node->expects($this->any())->method('getAbsolutePath')->will($this->returnValue($path));
684 $node->_set('targetContent', NULL);
685 $node->_call('setContent');
686 }
687
688 /**
689 * @test
690 */
691 public function setContentSetsContentToFile() {
692 /** @var $node \TYPO3\CMS\Install\FolderStructure\FileNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
693 $node = $this->getAccessibleMock('TYPO3\\CMS\\Install\\FolderStructure\\FileNode', array('getAbsolutePath', 'getRelativePathBelowSiteRoot'), array(), '', FALSE);
694 $path = PATH_site . 'typo3temp/' . uniqid('file_');
695 touch($path);
696 $this->testNodesToDelete[] = $path;
697 $node->expects($this->any())->method('getAbsolutePath')->will($this->returnValue($path));
698 $targetContent = uniqid('content_');
699 $node->_set('targetContent', $targetContent);
700 $node->_call('setContent');
701 $resultContent = file_get_contents($path);
702 $this->assertSame($targetContent, $resultContent);
703 }
704
705 /**
706 * @test
707 */
708 public function setContentReturnsOkStatusIfContentWasSuccessfullySet() {
709 /** @var $node \TYPO3\CMS\Install\FolderStructure\FileNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
710 $node = $this->getAccessibleMock('TYPO3\\CMS\\Install\\FolderStructure\\FileNode', array('getAbsolutePath', 'getRelativePathBelowSiteRoot'), array(), '', FALSE);
711 $path = PATH_site . 'typo3temp/' . uniqid('file_');
712 touch($path);
713 $this->testNodesToDelete[] = $path;
714 $node->expects($this->any())->method('getAbsolutePath')->will($this->returnValue($path));
715 $targetContent = uniqid('content_');
716 $node->_set('targetContent', $targetContent);
717 $this->assertInstanceOf('TYPO3\\CMS\\Install\\Status\\OkStatus', $node->_call('setContent'));
718 }
719
720 /**
721 * @test
722 */
723 public function setContentReturnsErrorStatusIfContentCanNotBeSetSet() {
724 if (TYPO3_OS === 'WIN') {
725 $this->markTestSkipped('Test not available on Windows OS.');
726 }
727 if (function_exists('posix_getegid') && posix_getegid() === 0) {
728 $this->markTestSkipped('Test skipped if run on linux as root');
729 }
730 /** @var $node \TYPO3\CMS\Install\FolderStructure\FileNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
731 $node = $this->getAccessibleMock('TYPO3\\CMS\\Install\\FolderStructure\\FileNode', array('getAbsolutePath', 'getRelativePathBelowSiteRoot'), array(), '', FALSE);
732 $dir = PATH_site . 'typo3temp/' . uniqid('dir_');
733 mkdir($dir);
734 $file = $dir . '/' . uniqid('file_');
735 touch($file);
736 chmod($file, octdec(0440));
737 $this->testNodesToDelete[] = $dir;
738 $node->expects($this->any())->method('getAbsolutePath')->will($this->returnValue($file));
739 $targetContent = uniqid('content_');
740 $node->_set('targetContent', $targetContent);
741 $this->assertInstanceOf('TYPO3\\CMS\\Install\\Status\\ErrorStatus', $node->_call('setContent'));
742 }
743
744 /**
745 * @test
746 */
747 public function isFileReturnsTrueIfNameIsFile() {
748 /** @var $node \TYPO3\CMS\Install\FolderStructure\FileNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
749 $node = $this->getAccessibleMock('TYPO3\\CMS\\Install\\FolderStructure\\FileNode', array('getAbsolutePath'), array(), '', FALSE);
750 $path = PATH_site . 'typo3temp/' . uniqid('file_');
751 touch($path);
752 $this->testNodesToDelete[] = $path;
753 $node->expects($this->any())->method('getAbsolutePath')->will($this->returnValue($path));
754 $this->assertTrue($node->_call('isFile'));
755 }
756
757 /**
758 * @test
759 */
760 public function isFileReturnsFalseIfNameIsALinkFile() {
761 if (TYPO3_OS === 'WIN') {
762 $this->markTestSkipped('Test not available on Windows OS.');
763 }
764 /** @var $node \TYPO3\CMS\Install\FolderStructure\FileNode|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
765 $node = $this->getAccessibleMock('TYPO3\\CMS\\Install\\FolderStructure\\FileNode', array('getAbsolutePath'), array(), '', FALSE);
766 $path = PATH_site . 'typo3temp/' . uniqid('root_');
767 \TYPO3\CMS\Core\Utility\GeneralUtility::mkdir_deep($path);
768 $this->testNodesToDelete[] = $path;
769 $link = uniqid('link_');
770 $file = uniqid('file_');
771 touch($path . '/' . $file);
772 symlink($path . '/' . $file, $path . '/' . $link);
773 $node->expects($this->any())->method('getAbsolutePath')->will($this->returnValue($path . '/' . $link));
774 $this->assertFalse($node->_call('isFile'));
775 }
776 }
777 ?>