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