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