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