[BUGFIX] Multiple fixes for Locking API and TSFE locking
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Locking / SimpleLockStrategyTest.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Unit\Locking;
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 use TYPO3\CMS\Core\Locking\SimpleLockStrategy;
18 use TYPO3\CMS\Core\Tests\UnitTestCase;
19 use TYPO3\CMS\Core\Utility\GeneralUtility;
20
21 /**
22 * Testcase for \TYPO3\CMS\Core\Locking\SimpleLockStrategy
23 *
24 * @author Markus Klein <klein.t3@reelworx.at>
25 */
26 class SimpleLockStrategyTest extends UnitTestCase {
27
28 /**
29 * @test
30 */
31 public function constructorCreatesLockDirectoryIfNotExisting() {
32 GeneralUtility::rmdir(PATH_site . SimpleLockStrategy::FILE_LOCK_FOLDER, TRUE);
33 new SimpleLockStrategy('999999999');
34 $this->assertTrue(is_dir(PATH_site . SimpleLockStrategy::FILE_LOCK_FOLDER));
35 }
36
37 /**
38 * @test
39 */
40 public function constructorSetsResourceToPathWithIdIfUsingSimpleLocking() {
41 $lock = $this->getAccessibleMock(SimpleLockStrategy::class, ['dummy'], ['999999999']);
42 $this->assertSame(PATH_site . SimpleLockStrategy::FILE_LOCK_FOLDER . 'simple_' . md5('999999999'), $lock->_get('filePath'));
43 }
44
45 /**
46 * @test
47 */
48 public function acquireFixesPermissionsOnLockFile() {
49 if (TYPO3_OS === 'WIN') {
50 $this->markTestSkipped('Test not available on Windows.');
51 }
52 // Use a very high id to be unique
53 /** @var SimpleLockStrategy|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface $lock */
54 $lock = $this->getAccessibleMock(SimpleLockStrategy::class, ['dummy'], ['999999999']);
55
56 $pathOfLockFile = $lock->_get('filePath');
57
58 $GLOBALS['TYPO3_CONF_VARS']['BE']['fileCreateMask'] = '0777';
59
60 // Acquire lock, get actual file permissions and clean up
61 $lock->acquire();
62 clearstatcache();
63 $resultFilePermissions = substr(decoct(fileperms($pathOfLockFile)), 2);
64 $lock->release();
65 $this->assertEquals($resultFilePermissions, '0777');
66 }
67
68 /**
69 * @test
70 */
71 public function releaseRemovesLockfileInTypo3TempLocks() {
72 /** @var SimpleLockStrategy|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface $lock */
73 $lock = $this->getAccessibleMock(SimpleLockStrategy::class, ['dummy'], ['999999999']);
74
75 $pathOfLockFile = $lock->_get('filePath');
76
77 $lock->acquire();
78 $lock->release();
79
80 $this->assertFalse(is_file($pathOfLockFile));
81 }
82
83 /**
84 * Dataprovider for releaseDoesNotRemoveFilesNotWithinTypo3TempLocksDirectory
85 */
86 public function invalidFileReferences() {
87 return array(
88 'not withing PATH_site' => array('/tmp/TYPO3-Lock-Test'),
89 'directory traversal' => array(PATH_site . 'typo3temp/../typo3temp/locks/foo'),
90 'directory traversal 2' => array(PATH_site . 'typo3temp/locks/../locks/foo'),
91 'within uploads' => array(PATH_site . 'uploads/TYPO3-Lock-Test')
92 );
93 }
94
95 /**
96 * @test
97 * @dataProvider invalidFileReferences
98 * @param string $file
99 * @throws \PHPUnit_Framework_SkippedTestError
100 */
101 public function releaseDoesNotRemoveFilesNotWithinTypo3TempLocksDirectory($file) {
102 if (TYPO3_OS === 'WIN') {
103 $this->markTestSkipped('releaseDoesNotRemoveFilesNotWithinTypo3TempLocksDirectory() test not available on Windows.');
104 }
105 // Create test file
106 touch($file);
107 if (!is_file($file)) {
108 $this->markTestIncomplete('releaseDoesNotRemoveFilesNotWithinTypo3TempLocksDirectory() skipped: Test file could not be created');
109 }
110 // Create instance, set lock file to invalid path
111 /** @var SimpleLockStrategy|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface $lock */
112 $lock = $this->getAccessibleMock(SimpleLockStrategy::class, ['dummy'], ['999999999']);
113 $lock->_set('filePath', $file);
114 $lock->_set('isAcquired', TRUE);
115
116 // Call release method
117 $lock->release();
118 // Check if file is still there and clean up
119 $fileExists = is_file($file);
120 if (is_file($file)) {
121 unlink($file);
122 }
123 $this->assertTrue($fileExists);
124 }
125
126 }