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