[TASK] Fix CGL violations against ClassDocComment
[Packages/TYPO3.CMS.git] / tests / Unit / t3lib / class.t3lib_lockTest.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2010-2011 Christian Kuhn <lolli@schwarzbu.ch>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 *
17 * This script is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
24
25 /**
26 * Testcase for t3lib_lock
27 *
28 * @author Christian Kuhn <lolli@schwarzbu.ch>
29 *
30 * @package TYPO3
31 * @subpackage t3lib
32 */
33 class t3lib_lockTest extends tx_phpunit_testcase {
34
35 /**
36 * Enable backup of global and system variables
37 *
38 * @var boolean
39 */
40 protected $backupGlobals = TRUE;
41
42 /**
43 * Exclude TYPO3_DB from backup/ restore of $GLOBALS
44 * because resource types cannot be handled during serializing
45 *
46 * @var array
47 */
48 protected $backupGlobalsBlacklist = array('TYPO3_DB');
49
50 ///////////////////////////////
51 // tests concerning __construct
52 ///////////////////////////////
53
54 /**
55 * @test
56 */
57 public function constructorUsesDefaultLockingMethodSimple() {
58 $instance = new t3lib_lock('999999999');
59 $this->assertSame('simple', $instance->getMethod());
60 }
61
62 /**
63 * @test
64 */
65 public function constructorSetsMethodToGivenParameter() {
66 $instance = new t3lib_lock('999999999', 'flock');
67 $this->assertSame('flock', $instance->getMethod());
68 }
69
70 /**
71 * @test
72 */
73 public function constructorDoesNotThrowExceptionIfUsingDisableMethod() {
74 $instance = new t3lib_lock('999999999', 'disable');
75 }
76
77 /**
78 * @test
79 * @expectedException InvalidArgumentException
80 */
81 public function constructorThrowsExceptionForNotExistingLockingMethod() {
82 $instance = new t3lib_lock('999999999', 'foo');
83 }
84
85 /**
86 * @test
87 */
88 public function constructorUsesDefaultValueForLoops() {
89 $instance = new t3lib_lock('999999999');
90 $instance->setEnableLogging(FALSE);
91 $t3libLockReflection = new ReflectionClass('t3lib_lock');
92 $t3libLockReflectionResourceProperty = $t3libLockReflection->getProperty('loops');
93 $t3libLockReflectionResourceProperty->setAccessible(TRUE);
94 $this->assertSame(150, $t3libLockReflectionResourceProperty->getValue($instance));
95 }
96
97 /**
98 * @test
99 */
100 public function constructorSetsLoopsToGivenNumberOfLoops() {
101 $instance = new t3lib_lock('999999999', 'simple', 10);
102 $instance->setEnableLogging(FALSE);
103 $t3libLockReflection = new ReflectionClass('t3lib_lock');
104 $t3libLockReflectionResourceProperty = $t3libLockReflection->getProperty('loops');
105 $t3libLockReflectionResourceProperty->setAccessible(TRUE);
106 $this->assertSame(10, $t3libLockReflectionResourceProperty->getValue($instance));
107 }
108
109 /**
110 * @test
111 */
112 public function constructorUsesDefaultValueForSteps() {
113 $instance = new t3lib_lock('999999999');
114 $instance->setEnableLogging(FALSE);
115 $t3libLockReflection = new ReflectionClass('t3lib_lock');
116 $t3libLockReflectionResourceProperty = $t3libLockReflection->getProperty('step');
117 $t3libLockReflectionResourceProperty->setAccessible(TRUE);
118 $this->assertSame(200, $t3libLockReflectionResourceProperty->getValue($instance));
119 }
120
121 /**
122 * @test
123 */
124 public function constructorSetsStepToGivenNumberOfStep() {
125 $instance = new t3lib_lock('999999999', 'simple', 0, 10);
126 $instance->setEnableLogging(FALSE);
127 $t3libLockReflection = new ReflectionClass('t3lib_lock');
128 $t3libLockReflectionResourceProperty = $t3libLockReflection->getProperty('step');
129 $t3libLockReflectionResourceProperty->setAccessible(TRUE);
130 $this->assertSame(10, $t3libLockReflectionResourceProperty->getValue($instance));
131 }
132
133 /**
134 * @test
135 */
136 public function constructorCreatesLockDirectoryIfNotExisting() {
137 t3lib_div::rmdir(PATH_site . 'typo3temp/locks/', TRUE);
138 $instance = new t3lib_lock('999999999', 'simple');
139 $this->assertTrue(is_dir(PATH_site . 'typo3temp/locks/'));
140 }
141
142 /**
143 * @test
144 */
145 public function constructorSetsIdToMd5OfStringIfUsingSimleLocking() {
146 $instance = new t3lib_lock('999999999', 'simple');
147 $this->assertSame(md5('999999999'), $instance->getId());
148 }
149
150 /**
151 * @test
152 */
153 public function constructorSetsResourceToPathWithIdIfUsingSimpleLocking() {
154 $instance = new t3lib_lock('999999999', 'simple');
155 $this->assertSame(PATH_site . 'typo3temp/locks/' . md5('999999999'), $instance->getResource());
156 }
157
158 /**
159 * @test
160 */
161 public function constructorSetsIdToAbsCrc32OfIdStringIfUsingSemaphoreLocking() {
162 if (!function_exists('sem_get')) {
163 $this->markTestSkipped('The system does not support semaphore base locking.');
164 }
165 $instance = new t3lib_lock('999999999', 'semaphore');
166 $this->assertSame(abs(crc32('999999999')), $instance->getId());
167 }
168
169 /**
170 * @test
171 */
172 public function constructorSetsResourceToSemaphoreResourceIfUsingSemaphoreLocking() {
173 if (!function_exists('sem_get')) {
174 $this->markTestSkipped('The system does not support semaphore base locking.');
175 }
176 $instance = new t3lib_lock('999999999', 'semaphore');
177 $this->assertTrue(is_resource($instance->getResource()));
178 }
179
180 ///////////////////////////////
181 // tests concerning acquire
182 ///////////////////////////////
183
184 /**
185 * @test
186 */
187 public function acquireFixesPermissionsOnLockFileIfUsingSimpleLogging() {
188 if (TYPO3_OS == 'WIN') {
189 $this->markTestSkipped('acquireFixesPermissionsOnLockFileIfUsingSimpleLogging() test not available on Windows.');
190 }
191
192 // Use a very high id to be unique
193 $instance = new t3lib_lock(999999999, 'simple');
194 $pathOfLockFile = $instance->getResource();
195 $GLOBALS['TYPO3_CONF_VARS']['BE']['fileCreateMask'] = '0777';
196
197 // Acquire lock, get actual file permissions and clean up
198 $instance->acquire();
199 clearstatcache();
200 $resultFilePermissions = substr(decoct(fileperms($pathOfLockFile)), 2);
201 $instance->__destruct();
202
203 $this->assertEquals($resultFilePermissions, '0777');
204 }
205
206
207 ///////////////////////////////
208 // tests concerning release
209 ///////////////////////////////
210
211 /**
212 * Dataprovider for releaseRemovesLockfileInTypo3TempLocks
213 */
214 public function fileBasedLockMethods() {
215 return array(
216 'simple' => array('simple'),
217 'flock' => array('flock'),
218 );
219 }
220
221 /**
222 * @test
223 * @dataProvider fileBasedLockMethods
224 */
225 public function releaseRemovesLockfileInTypo3TempLocks($lockMethod) {
226 // Use a very high id to be unique
227 $instance = new t3lib_lock(999999999, 'simple');
228 // Disable logging
229 $instance->setEnableLogging(FALSE);
230 // File pointer to current lock file
231 $lockFile = $instance->getResource();
232 $instance->acquire();
233
234 $instance->release();
235
236 $this->assertFalse(is_file($lockFile));
237 }
238
239 /**
240 * Dataprovider for releaseDoesNotRemoveFilesNotWithinTypo3TempLocksDirectory
241 */
242 public function invalidFileReferences() {
243 return array(
244 'simple not within PATH_site' => array('simple', '/tmp/TYPO3-Lock-Test'),
245 'flock not withing PATH_site' => array('flock', '/tmp/TYPO3-Lock-Test'),
246 'simple directory traversal' => array('simple', PATH_site . 'typo3temp/../typo3temp/locks/foo'),
247 'flock directory traversal' => array('flock', PATH_site . 'typo3temp/../typo3temp/locks/foo'),
248 'simple directory traversal 2' => array('simple', PATH_site . 'typo3temp/locks/../locks/foo'),
249 'flock directory traversal 2' => array('flock', PATH_site . 'typo3temp/locks/../locks/foo'),
250 'simple within uploads' => array('simple', PATH_site . 'uploads/TYPO3-Lock-Test'),
251 'flock within uploads' => array('flock', PATH_site . 'uploads/TYPO3-Lock-Test'),
252 );
253 }
254
255 /**
256 * @test
257 * @dataProvider invalidFileReferences
258 */
259 public function releaseDoesNotRemoveFilesNotWithinTypo3TempLocksDirectory($lockMethod, $file) {
260 if (TYPO3_OS === 'WIN') {
261 $this->markTestSkipped('releaseDoesNotRemoveFilesNotWithinTypo3TempLocksDirectory() test not available on Windows.');
262 }
263 // Reflection needs php 5.3.2 or above
264 if (version_compare(phpversion(), '5.3.2', '<')) {
265 $this->markTestSkipped('releaseDoesNotRemoveFilesNotWithinTypo3TempLocksDirectory() test not available with php version smaller than 5.3.2');
266 }
267
268 // Create test file
269 touch($file);
270 if (!is_file($file)) {
271 $this->markTestSkipped('releaseDoesNotRemoveFilesNotWithinTypo3TempLocksDirectory() skipped: Test file could not be created');
272 }
273
274 // Create t3lib_lock instance, set lockfile to invalid path
275 $instance = new t3lib_lock(999999999, $lockMethod);
276 $instance->setEnableLogging(FALSE);
277 $t3libLockReflection = new ReflectionClass('t3lib_lock');
278 $t3libLockReflectionResourceProperty = $t3libLockReflection->getProperty('resource');
279 $t3libLockReflectionResourceProperty->setAccessible(TRUE);
280 $t3libLockReflectionResourceProperty->setValue($instance, $file);
281 $t3libLockReflectionAcquiredProperty = $t3libLockReflection->getProperty('isAcquired');
282 $t3libLockReflectionAcquiredProperty->setAccessible(TRUE);
283 $t3libLockReflectionAcquiredProperty->setValue($instance, TRUE);
284
285 // Call release method
286 $instance->release();
287
288 // Check if file is still there and clean up
289 $fileExists = is_file($file);
290 if (is_file($file)) {
291 unlink($file);
292 }
293
294 $this->assertTrue($fileExists);
295 }
296 }
297 ?>