TYPO3 CMS  TYPO3_6-2
ConfigurationManagerTest.php
Go to the documentation of this file.
1 <?php
3 
23 
28  protected $testFilesToDelete = array();
29 
33  protected $fixture;
34 
35  public function setUp() {
37  array(
38  'getDefaultConfigurationFileLocation',
39  'getLocalConfigurationFileLocation',
40  )
41  );
42  }
43 
47  public function tearDown() {
48  foreach ($this->testFilesToDelete as $absoluteFileName) {
50  }
51  parent::tearDown();
52  }
53 
57  protected function createFixtureWithMockedMethods(array $methods) {
58  $this->fixture = $this->getMock(
59  'TYPO3\\CMS\\Core\\Configuration\\ConfigurationManager',
60  $methods
61  );
62  }
63 
69  $defaultConfigurationFile = PATH_site . 'typo3temp/' . $this->getUniqueId('defaultConfiguration');
70  file_put_contents(
71  $defaultConfigurationFile,
72  '<?php throw new \RuntimeException(\'foo\', 1310203814); ?>'
73  );
74  $this->testFilesToDelete[] = $defaultConfigurationFile;
75 
76  $this->fixture
77  ->expects($this->once())
78  ->method('getDefaultConfigurationFileLocation')
79  ->will($this->returnValue($defaultConfigurationFile));
80  $this->fixture->getDefaultConfiguration();
81  }
82 
88  $configurationFile = PATH_site . 'typo3temp/' . $this->getUniqueId('localConfiguration');
89  file_put_contents(
90  $configurationFile,
91  '<?php throw new \RuntimeException(\'foo\', 1310203815); ?>'
92  );
93  $this->testFilesToDelete[] = $configurationFile;
94 
95  $this->fixture
96  ->expects($this->once())
97  ->method('getLocalConfigurationFileLocation')
98  ->will($this->returnValue($configurationFile));
99  $this->fixture->getLocalConfiguration();
100  }
101 
106  $currentLocalConfiguration = array(
107  'notChanged' => 23,
108  'changed' => 'unChanged',
109  );
110  $overrideConfiguration = array(
111  'changed' => 'changed',
112  'new' => 'new'
113  );
114  $expectedConfiguration = array(
115  'notChanged' => 23,
116  'changed' => 'changed',
117  'new' => 'new',
118  );
119 
121  array(
122  'getLocalConfiguration',
123  'writeLocalConfiguration',
124  )
125  );
126  $this->fixture->expects($this->once())
127  ->method('getLocalConfiguration')
128  ->will($this->returnValue($currentLocalConfiguration));
129  $this->fixture->expects($this->once())
130  ->method('writeLocalConfiguration')
131  ->with($expectedConfiguration);
132 
133  $this->fixture->updateLocalConfiguration($overrideConfiguration);
134  }
135 
141  array(
142  'getDefaultConfiguration',
143  )
144  );
145  $this->fixture->expects($this->once())
146  ->method('getDefaultConfiguration')
147  ->will($this->returnValue(array(
148  'path' => 'value',
149  )
150  ));
151 
152  $this->assertSame('value', $this->fixture->getDefaultConfigurationValueByPath('path'));
153  }
154 
160  array(
161  'getLocalConfiguration',
162  )
163  );
164  $this->fixture->expects($this->once())
165  ->method('getLocalConfiguration')
166  ->will($this->returnValue(array(
167  'path' => 'value',
168  )
169  ));
170 
171  $this->assertSame('value', $this->fixture->getLocalConfigurationValueByPath('path'));
172  }
173 
179  array(
180  'getDefaultConfiguration',
181  'getLocalConfiguration',
182  )
183  );
184  $this->fixture->expects($this->once())
185  ->method('getDefaultConfiguration')
186  ->will($this->returnValue(array(
187  'path' => 'value',
188  )
189  ));
190  $this->fixture->expects($this->once())
191  ->method('getLocalConfiguration')
192  ->will($this->returnValue(array(
193  'path' => 'valueOverride',
194  )
195  ));
196 
197  $this->assertSame('valueOverride', $this->fixture->getConfigurationValueByPath('path'));
198  }
199 
204  $this->createFixtureWithMockedMethods(array(
205  'isValidLocalConfigurationPath',
206  ));
207  $this->fixture->expects($this->once())
208  ->method('isValidLocalConfigurationPath')
209  ->will($this->returnValue(FALSE));
210 
211  $this->assertFalse($this->fixture->setLocalConfigurationValueByPath('path', 'value'));
212  }
213 
218  $currentLocalConfiguration = array(
219  'notChanged' => 23,
220  'toUpdate' => 'notUpdated',
221  );
222  $expectedConfiguration = array(
223  'notChanged' => 23,
224  'toUpdate' => 'updated',
225  );
226 
228  array(
229  'isValidLocalConfigurationPath',
230  'getLocalConfiguration',
231  'writeLocalConfiguration',
232  )
233  );
234  $this->fixture->expects($this->once())
235  ->method('isValidLocalConfigurationPath')
236  ->will($this->returnValue(TRUE));
237  $this->fixture->expects($this->once())
238  ->method('getLocalConfiguration')
239  ->will($this->returnValue($currentLocalConfiguration));
240  $this->fixture->expects($this->once())
241  ->method('writeLocalConfiguration')
242  ->with($expectedConfiguration);
243 
244  $this->fixture->setLocalConfigurationValueByPath('toUpdate', 'updated');
245  }
246 
251  $currentLocalConfiguration = array(
252  'notChanged' => 23,
253  'toUpdate' => 'notUpdated',
254  );
255  $expectedConfiguration = array(
256  'notChanged' => 23,
257  'toUpdate' => 'updated',
258  'new' => 'new',
259  );
260 
262  array(
263  'isValidLocalConfigurationPath',
264  'getLocalConfiguration',
265  'writeLocalConfiguration',
266  )
267  );
268  $this->fixture->expects($this->any())
269  ->method('isValidLocalConfigurationPath')
270  ->will($this->returnValue(TRUE));
271  $this->fixture->expects($this->once())
272  ->method('getLocalConfiguration')
273  ->will($this->returnValue($currentLocalConfiguration));
274  $this->fixture->expects($this->once())
275  ->method('writeLocalConfiguration')
276  ->with($expectedConfiguration);
277 
278  $pairs = array(
279  'toUpdate' => 'updated',
280  'new' => 'new'
281  );
282  $this->fixture->setLocalConfigurationValuesByPathValuePairs($pairs);
283  }
284 
289  $currentLocalConfiguration = array(
290  'toRemove1' => 'foo',
291  'notChanged' => 23,
292  'toRemove2' => 'bar',
293  );
294  $expectedConfiguration = array(
295  'notChanged' => 23,
296  );
297 
299  array(
300  'getLocalConfiguration',
301  'writeLocalConfiguration',
302  )
303  );
304  $this->fixture->expects($this->once())
305  ->method('getLocalConfiguration')
306  ->will($this->returnValue($currentLocalConfiguration));
307  $this->fixture->expects($this->once())
308  ->method('writeLocalConfiguration')
309  ->with($expectedConfiguration);
310 
311  $removePaths = array(
312  'toRemove1',
313  'toRemove2',
314  );
315  $this->assertTrue($this->fixture->removeLocalConfigurationKeysByPath($removePaths));
316  }
317 
322  $currentLocalConfiguration = array(
323  'notChanged' => 23,
324  );
326  array(
327  'getLocalConfiguration',
328  'writeLocalConfiguration',
329  )
330  );
331  $this->fixture->expects($this->once())
332  ->method('getLocalConfiguration')
333  ->will($this->returnValue($currentLocalConfiguration));
334  $this->fixture->expects($this->never())
335  ->method('writeLocalConfiguration');
336 
337  $removeNothing = array();
338  $this->assertFalse($this->fixture->removeLocalConfigurationKeysByPath($removeNothing));
339  }
340 
345  $currentLocalConfiguration = array(
346  'notChanged' => 23,
347  );
349  array(
350  'getLocalConfiguration',
351  'writeLocalConfiguration',
352  )
353  );
354  $this->fixture->expects($this->once())
355  ->method('getLocalConfiguration')
356  ->will($this->returnValue($currentLocalConfiguration));
357  $this->fixture->expects($this->never())
358  ->method('writeLocalConfiguration');
359 
360  $removeNonExisting = array('notPresent');
361  $this->assertFalse($this->fixture->removeLocalConfigurationKeysByPath($removeNonExisting));
362  }
363 
367  public function canWriteConfigurationReturnsFalseIfDirectoryIsNotWritable() {
368  if (function_exists('posix_getegid') && posix_getegid() === 0) {
369  $this->markTestSkipped('Test skipped if run on linux as root');
370  } elseif (TYPO3_OS == 'WIN') {
371  $this->markTestSkipped('Not available on Windows');
372  }
374  $fixture = $this->getAccessibleMock('TYPO3\\CMS\\Core\\Configuration\\ConfigurationManager', array('dummy'));
375 
376  $directory = 'typo3temp/' . $this->getUniqueId('test_');
377  $absoluteDirectory = PATH_site . $directory;
378  mkdir($absoluteDirectory);
379  chmod($absoluteDirectory, 0544);
380  clearstatcache();
381 
382  $fixture->_set('pathTypo3Conf', $directory);
383 
384  $result = $fixture->canWriteConfiguration();
385 
386  chmod($absoluteDirectory, 0755);
387  rmdir($absoluteDirectory);
388 
389  $this->assertFalse($result);
390  }
391 
395  public function canWriteConfigurationReturnsFalseIfLocalConfigurationFileIsNotWritable() {
396  if (function_exists('posix_getegid') && posix_getegid() === 0) {
397  $this->markTestSkipped('Test skipped if run on linux as root');
398  } elseif (TYPO3_OS == 'WIN') {
399  $this->markTestSkipped('Not available on Windows');
400  }
402  $fixture = $this->getAccessibleMock('TYPO3\\CMS\\Core\\Configuration\\ConfigurationManager', array('dummy'));
403 
404  $file = 'typo3temp/' . $this->getUniqueId('test_');
405  $absoluteFile = PATH_site . $file;
406  touch($absoluteFile);
407  $this->testFilesToDelete[] = $absoluteFile;
408  chmod($absoluteFile, 0444);
409  clearstatcache();
410 
411  $fixture->_set('localConfigurationFile', $file);
412 
413  $result = $fixture->canWriteConfiguration();
414 
415  chmod($absoluteFile, 0644);
416 
417  $this->assertFalse($result);
418  }
419 
423  public function canWriteConfigurationReturnsTrueIfDirectoryAndFilesAreWritable() {
425  $fixture = $this->getAccessibleMock('TYPO3\\CMS\\Core\\Configuration\\ConfigurationManager', array('dummy'));
426 
427  $directory = 'typo3temp/' . $this->getUniqueId('test_');
428  $absoluteDirectory = PATH_site . $directory;
429  mkdir($absoluteDirectory);
430  $fixture->_set('pathTypo3Conf', $absoluteDirectory);
431 
432  $file1 = 'typo3temp/' . $this->getUniqueId('test_');
433  $absoluteFile1 = PATH_site . $file1;
434  touch($absoluteFile1);
435  $this->testFilesToDelete[] = $absoluteFile1;
436  $fixture->_set('localConfigurationFile', $absoluteFile1);
437 
438  $file2 = 'typo3temp/' . $this->getUniqueId('test_');
439  $absoluteFile2 = PATH_site . $file2;
440  touch($absoluteFile2);
441  $this->testFilesToDelete[] = $absoluteFile2;
442  $fixture->_set('localconfFile', $absoluteFile2);
443 
444  clearstatcache();
445 
446  $result = $fixture->canWriteConfiguration();
447 
448  $this->assertTrue($result);
449  }
450 
455  $configurationFile = PATH_site . 'typo3temp/' . $this->getUniqueId('localConfiguration');
456  if (!is_file($configurationFile)) {
457  if (!$fh = fopen($configurationFile, 'wb')) {
458  $this->markTestSkipped('Can not create file ' . $configurationFile . '. Please check your write permissions.');
459  }
460  fclose($fh);
461  }
462 
463  if (!@is_file($configurationFile)) {
464  throw new \RuntimeException('File ' . $configurationFile . ' could not be found. Please check your write permissions', 1346364362);
465  }
466  $this->testFilesToDelete[] = $configurationFile;
467 
468  $this->fixture
469  ->expects($this->any())
470  ->method('getLocalConfigurationFileLocation')
471  ->will($this->returnValue($configurationFile));
472 
473  $pairs = array(
474  'foo' => 42,
475  'bar' => 23
476  );
477  $expectedContent =
478  '<?php' . LF .
479  'return array(' . LF .
480  TAB . '\'bar\' => 23,' . LF .
481  TAB . '\'foo\' => 42,' . LF .
482  ');' . LF .
483  '?>';
484 
485  $this->fixture->writeLocalConfiguration($pairs);
486  $this->assertSame($expectedContent, file_get_contents($configurationFile));
487  }
488 
493  public function createLocalConfigurationFromFactoryConfigurationThrowsExceptionIfFileExists() {
495  $fixture = $this->getAccessibleMock('TYPO3\\CMS\\Core\\Configuration\\ConfigurationManager', array('dummy'));
496 
497  $file = 'typo3temp/' . $this->getUniqueId('test_');
498  $absoluteFile = PATH_site . $file;
499  touch($absoluteFile);
500  $this->testFilesToDelete[] = $absoluteFile;
501  $fixture->_set('localConfigurationFile', $file);
502 
503  $fixture->createLocalConfigurationFromFactoryConfiguration();
504  }
505 
509  public function createLocalConfigurationFromFactoryConfigurationWritesContentFromFactoryFile() {
511  $fixture = $this->getAccessibleMock('TYPO3\\CMS\\Core\\Configuration\\ConfigurationManager', array('writeLocalConfiguration'));
512  $fixture->_set('localConfigurationFile', 'typo3temp/' . $this->getUniqueId('dummy_'));
513 
514  $factoryConfigurationFile = 'typo3temp/' . $this->getUniqueId('test_') . '.php';
515  $factoryConfigurationAbsoluteFile = PATH_site . $factoryConfigurationFile;
516  $uniqueContentString = $this->getUniqueId('string_');
517  $validFactoryConfigurationFileContent =
518  '<?php' . LF .
519  'return array(' . LF .
520  $uniqueContentString . ' => foo,' . LF .
521  ');' . LF .
522  '?>';
523  file_put_contents(
524  $factoryConfigurationAbsoluteFile,
525  $validFactoryConfigurationFileContent
526  );
527  $this->testFilesToDelete[] = $factoryConfigurationAbsoluteFile;
528 
529  $fixture->_set('factoryConfigurationFile', $factoryConfigurationFile);
530 
531  $fixture
532  ->expects($this->once())
533  ->method('writeLocalConfiguration')
534  ->with($this->arrayHasKey($uniqueContentString));
535  $fixture->createLocalConfigurationFromFactoryConfiguration();
536  }
537 
541  public function createLocalConfigurationFromFactoryConfigurationMergesConfigurationWithAdditionalFactoryFile() {
543  $fixture = $this->getAccessibleMock('TYPO3\\CMS\\Core\\Configuration\\ConfigurationManager', array('writeLocalConfiguration'));
544  $fixture->_set('localConfigurationFile', 'typo3temp/' . $this->getUniqueId('dummy_'));
545 
546  $factoryConfigurationFile = 'typo3temp/' . $this->getUniqueId('test_') . '.php';
547  $factoryConfigurationAbsoluteFile = PATH_site . $factoryConfigurationFile;
548  $validFactoryConfigurationFileContent =
549  '<?php' . LF .
550  'return array();' . LF .
551  '?>';
552  file_put_contents(
553  $factoryConfigurationAbsoluteFile,
554  $validFactoryConfigurationFileContent
555  );
556  $this->testFilesToDelete[] = $factoryConfigurationAbsoluteFile;
557  $fixture->_set('factoryConfigurationFile', $factoryConfigurationFile);
558 
559  $additionalFactoryConfigurationFile = 'typo3temp/' . $this->getUniqueId('test_') . '.php';
560  $additionalFactoryConfigurationAbsoluteFile = PATH_site . $additionalFactoryConfigurationFile;
561  $uniqueContentString = $this->getUniqueId('string_');
562  $validAdditionalFactoryConfigurationFileContent =
563  '<?php' . LF .
564  'return array(' . LF .
565  $uniqueContentString . ' => foo,' . LF .
566  ');' . LF .
567  '?>';
568  file_put_contents(
569  $additionalFactoryConfigurationAbsoluteFile,
570  $validAdditionalFactoryConfigurationFileContent
571  );
572  $this->testFilesToDelete[] = $additionalFactoryConfigurationAbsoluteFile;
573  $fixture->_set('additionalFactoryConfigurationFile', $additionalFactoryConfigurationFile);
574 
575  $fixture
576  ->expects($this->once())
577  ->method('writeLocalConfiguration')
578  ->with($this->arrayHasKey($uniqueContentString));
579  $fixture->createLocalConfigurationFromFactoryConfiguration();
580  }
581 
585  public function isValidLocalConfigurationPathAcceptsWhitelistedPath() {
587  $fixture = $this->getAccessibleMock('TYPO3\\CMS\\Core\\Configuration\\ConfigurationManager', array('dummy'));
588  $fixture->_set('whiteListedLocalConfigurationPaths', array('foo/bar'));
589  $this->assertTrue($fixture->_call('isValidLocalConfigurationPath', 'foo/bar/baz'));
590  }
591 
595  public function isValidLocalConfigurationPathDeniesNotWhitelistedPath() {
597  $fixture = $this->getAccessibleMock('TYPO3\\CMS\\Core\\Configuration\\ConfigurationManager', array('dummy'));
598  $fixture->_set('whiteListedLocalConfigurationPaths', array('foo/bar'));
599  $this->assertFalse($fixture->_call('isValidLocalConfigurationPath', 'bar/baz'));
600  }
601 }
static unlink_tempfile($uploadedTempFileName)
if(preg_match($regexp, $sql)) $pairs
Remove pairs of single-quotes.
getAccessibleMock( $originalClassName, array $methods=array(), array $arguments=array(), $mockClassName='', $callOriginalConstructor=TRUE, $callOriginalClone=TRUE, $callAutoload=TRUE)
if($list_of_literals) if(!empty($literals)) if(!empty($literals)) $result
Analyse literals to prepend the N char to them if their contents aren&#39;t numeric.