PageRenderTime 53ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/t3lib/t3lib_divTest.php

https://github.com/foxsoft/typo3v4core
PHP | 1623 lines | 1168 code | 112 blank | 343 comment | 2 complexity | f98138e7ab2ceaaabe60adff051a09d6 MD5 | raw file
Possible License(s): Apache-2.0
  1. <?php
  2. /***************************************************************
  3. * Copyright notice
  4. *
  5. * (c) 2009-2010 Ingo Renner <ingo@typo3.org>
  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. * Testcase for class t3lib_div
  26. *
  27. * @author Ingo Renner <ingo@typo3.org>
  28. * @author Oliver Klee <typo3-coding@oliverklee.de>
  29. *
  30. * @package TYPO3
  31. * @subpackage t3lib
  32. */
  33. class t3lib_divTest extends tx_phpunit_testcase {
  34. /**
  35. * backup of the global variables _GET, _POST, _SERVER
  36. *
  37. * @var array
  38. */
  39. private $backupGlobalVariables;
  40. public function setUp() {
  41. $this->backupGlobalVariables = array(
  42. '_GET' => $_GET,
  43. '_POST' => $_POST,
  44. '_SERVER' => $_SERVER,
  45. 'TYPO3_CONF_VARS' => $GLOBALS['TYPO3_CONF_VARS'],
  46. );
  47. }
  48. public function tearDown() {
  49. foreach ($this->backupGlobalVariables as $key => $data) {
  50. $GLOBALS[$key] = $data;
  51. }
  52. }
  53. ///////////////////////////////
  54. // Tests concerning validIP
  55. ///////////////////////////////
  56. /**
  57. * Data provider for checkValidIpReturnsTrueForValidIp
  58. *
  59. * @return array Data sets
  60. */
  61. public static function validIpDataProvider() {
  62. return array(
  63. '0.0.0.0' => array('0.0.0.0'),
  64. 'private IPv4 class C' => array('192.168.0.1'),
  65. 'private IPv4 class A' => array('10.0.13.1'),
  66. 'private IPv6' => array('fe80::daa2:5eff:fe8b:7dfb'),
  67. );
  68. }
  69. /**
  70. * Checks if t3lib_div::validIP() returns true for valid IPs
  71. *
  72. * @test
  73. * @see t3lib_div::validIP()
  74. * @dataProvider validIpDataProvider
  75. */
  76. public function checkValidIpReturnsTrueForValidIp($ip) {
  77. $this->assertTrue(t3lib_div::validIP($ip));
  78. }
  79. /**
  80. * Data provider for checkValidIpReturnsFalseForInvalidIp
  81. *
  82. * @return array Data sets
  83. */
  84. public static function invalidIpDataProvider() {
  85. return array(
  86. 'null' => array(null),
  87. 'zero' => array(0),
  88. 'string' => array('test'),
  89. 'string empty' => array(''),
  90. 'string null' => array('null'),
  91. 'out of bounds IPv4' => array('300.300.300.300'),
  92. 'wrong dotted decimal notation with only two dots' => array('127.0.1'),
  93. );
  94. }
  95. /**
  96. * Checks if t3lib_div::validIP() returns false for invalid IPs
  97. *
  98. * @test
  99. * @see t3lib_div::validIP()
  100. * @dataProvider invalidIpDataProvider
  101. */
  102. public function checkValidIpReturnsFalseForInvalidIp($ip) {
  103. $this->assertFalse(t3lib_div::validIP($ip));
  104. }
  105. ///////////////////////////////
  106. // Tests concerning splitCalc
  107. ///////////////////////////////
  108. /**
  109. * @test
  110. */
  111. public function splitCalcForEmptyStringReturnsEmptyArray() {
  112. $this->assertEquals(
  113. array(),
  114. t3lib_div::splitCalc('', '+-*/')
  115. );
  116. }
  117. /**
  118. * @test
  119. */
  120. public function splitCalcForNumberWithoutOperatorReturnsArrayWithPlusAndNumber() {
  121. $this->assertEquals(
  122. array(array('+', 42)),
  123. t3lib_div::splitCalc('42', '+-*/')
  124. );
  125. }
  126. /**
  127. * @test
  128. */
  129. public function splitCalcForTwoNumbersWithAsterikReturnsFirstNumberWithPlusAndSecondNumberWithOperator() {
  130. $this->assertEquals(
  131. array(
  132. array('+', 42),
  133. array('*', 31),
  134. ),
  135. t3lib_div::splitCalc('42 * 31', '+-*/')
  136. );
  137. }
  138. //////////////////////////////////
  139. // Tests concerning calcPriority
  140. //////////////////////////////////
  141. /**
  142. * @see calcPriorityCalculatesBasicArithmeticOperation
  143. */
  144. public function calcPriorityTwoOperandsDataProvider() {
  145. return array(
  146. 'add' => array(9, '6 + 3'),
  147. 'substractWithPositiveResult' => array(3, '6 - 3'),
  148. 'substractWithNegativeResult' => array(-3, '3 - 6'),
  149. 'multiply' => array(6, '2 * 3'),
  150. 'divide' => array(2.5, '5 / 2'),
  151. 'modulus' => array(1, '5 % 2'),
  152. 'power' => array(8, '2 ^ 3'),
  153. );
  154. }
  155. /**
  156. * @test
  157. *
  158. * @dataProvider calcPriorityTwoOperandsDataProvider
  159. *
  160. * @param string $expected the expected value from calcPriority
  161. * @param string $arithmeticExpression the string to feed to calcPriority
  162. */
  163. public function calcPriorityCalculatesBasicArithmeticOperation($expected, $arithmeticExpression) {
  164. $this->assertEquals(
  165. $expected,
  166. t3lib_div::calcPriority($arithmeticExpression)
  167. );
  168. }
  169. /**
  170. * @test
  171. */
  172. public function calcPriorityCalculatesArithmeticOperationWithMultipleOperands() {
  173. $this->assertEquals(6.5, t3lib_div::calcPriority('5 + 3 / 2'));
  174. $this->assertEquals(14, t3lib_div::calcPriority('5 + 3 ^ 2'));
  175. $this->assertEquals(4, t3lib_div::calcPriority('5 % 2 + 3'));
  176. $this->assertEquals(3, t3lib_div::calcPriority('2 + 6 / 2 - 2'));
  177. }
  178. /**
  179. * @test
  180. */
  181. public function checkIntExplodeConvertsStringsToInteger() {
  182. $testString = '1,foo,2';
  183. $expectedArray = array(1, 0, 2);
  184. $actualArray = t3lib_div::intExplode(',', $testString);
  185. $this->assertEquals($expectedArray, $actualArray);
  186. }
  187. /**
  188. * @test
  189. */
  190. public function checkRevExplodeCorrectlyExplodesString() {
  191. $testString = 'my:words:here';
  192. $expectedArray = array('my:words', 'here');
  193. $actualArray = t3lib_div::revExplode(':', $testString, 2);
  194. $this->assertEquals($expectedArray, $actualArray);
  195. }
  196. /**
  197. * @test
  198. */
  199. public function checkTrimExplodeTrimsSpacesAtElementStartAndEnd() {
  200. $testString = ' a , b , c ,d ,, e,f,';
  201. $expectedArray = array('a', 'b', 'c', 'd', '', 'e', 'f', '');
  202. $actualArray = t3lib_div::trimExplode(',', $testString);
  203. $this->assertEquals($expectedArray, $actualArray);
  204. }
  205. /**
  206. * @test
  207. */
  208. public function checkTrimExplodeRemovesNewLines() {
  209. $testString = ' a , b , ' . LF . ' ,d ,, e,f,';
  210. $expectedArray = array('a', 'b', 'd', 'e', 'f');
  211. $actualArray = t3lib_div::trimExplode(',', $testString, true);
  212. $this->assertEquals($expectedArray, $actualArray);
  213. }
  214. /**
  215. * @test
  216. */
  217. public function checkTrimExplodeRemovesEmptyElements() {
  218. $testString = 'a , b , c , ,d ,, ,e,f,';
  219. $expectedArray = array('a', 'b', 'c', 'd', 'e', 'f');
  220. $actualArray = t3lib_div::trimExplode(',', $testString, true);
  221. $this->assertEquals($expectedArray, $actualArray);
  222. }
  223. /**
  224. * @test
  225. */
  226. public function checkTrimExplodeKeepsRemainingResultsWithEmptyItemsAfterReachingLimitWithPositiveParameter() {
  227. $testString = ' a , b , c , , d,, ,e ';
  228. $expectedArray = array('a', 'b', 'c,,d,,,e'); // limiting returns the rest of the string as the last element
  229. $actualArray = t3lib_div::trimExplode(',', $testString, false, 3);
  230. $this->assertEquals($expectedArray, $actualArray);
  231. }
  232. /**
  233. * @test
  234. */
  235. public function checkTrimExplodeKeepsRemainingResultsWithoutEmptyItemsAfterReachingLimitWithPositiveParameter() {
  236. $testString = ' a , b , c , , d,, ,e ';
  237. $expectedArray = array('a', 'b', 'c,d,e'); // limiting returns the rest of the string as the last element
  238. $actualArray = t3lib_div::trimExplode(',', $testString, true, 3);
  239. $this->assertEquals($expectedArray, $actualArray);
  240. }
  241. /**
  242. * @test
  243. */
  244. public function checkTrimExplodeKeepsRamainingResultsWithEmptyItemsAfterReachingLimitWithNegativeParameter() {
  245. $testString = ' a , b , c , d, ,e, f , , ';
  246. $expectedArray = array('a', 'b', 'c', 'd', '', 'e'); // limiting returns the rest of the string as the last element
  247. $actualArray = t3lib_div::trimExplode(',', $testString, false, -3);
  248. $this->assertEquals($expectedArray, $actualArray);
  249. }
  250. /**
  251. * @test
  252. */
  253. public function checkTrimExplodeKeepsRamainingResultsWithoutEmptyItemsAfterReachingLimitWithNegativeParameter() {
  254. $testString = ' a , b , c , d, ,e, f , , ';
  255. $expectedArray = array('a', 'b', 'c'); // limiting returns the rest of the string as the last element
  256. $actualArray = t3lib_div::trimExplode(',', $testString, true, -3);
  257. $this->assertEquals($expectedArray, $actualArray);
  258. }
  259. /**
  260. * @test
  261. */
  262. public function checkTrimExplodeReturnsExactResultsWithoutReachingLimitWithPositiveParameter() {
  263. $testString = ' a , b , , c , , , ';
  264. $expectedArray = array('a', 'b', 'c'); // limiting returns the rest of the string as the last element
  265. $actualArray = t3lib_div::trimExplode(',', $testString, true, 4);
  266. $this->assertEquals($expectedArray, $actualArray);
  267. }
  268. /**
  269. * @test
  270. */
  271. public function checkTrimExplodeKeepsZeroAsString() {
  272. $testString = 'a , b , c , ,d ,, ,e,f, 0 ,';
  273. $expectedArray = array('a', 'b', 'c', 'd', 'e', 'f', '0');
  274. $actualArray = t3lib_div::trimExplode(',', $testString, true);
  275. $this->assertEquals($expectedArray, $actualArray);
  276. }
  277. /**
  278. * @test
  279. */
  280. public function checkRemoveArrayEntryByValueRemovesEntriesFromOneDimensionalArray() {
  281. $inputArray = array(
  282. '0' => 'test1',
  283. '1' => 'test2',
  284. '2' => 'test3',
  285. '3' => 'test2',
  286. );
  287. $compareValue = 'test2';
  288. $expectedResult = array(
  289. '0' => 'test1',
  290. '2' => 'test3',
  291. );
  292. $actualResult = t3lib_div::removeArrayEntryByValue($inputArray, $compareValue);
  293. $this->assertEquals($expectedResult, $actualResult);
  294. }
  295. /**
  296. * @test
  297. */
  298. public function checkRemoveArrayEntryByValueRemovesEntriesFromMultiDimensionalArray() {
  299. $inputArray = array(
  300. '0' => 'foo',
  301. '1' => array(
  302. '10' => 'bar',
  303. ),
  304. '2' => 'bar',
  305. );
  306. $compareValue = 'bar';
  307. $expectedResult = array(
  308. '0' => 'foo',
  309. '1' => array(),
  310. );
  311. $actualResult = t3lib_div::removeArrayEntryByValue($inputArray, $compareValue);
  312. $this->assertEquals($expectedResult, $actualResult);
  313. }
  314. /**
  315. * @test
  316. */
  317. public function checkRemoveArrayEntryByValueRemovesEntryWithEmptyString() {
  318. $inputArray = array(
  319. '0' => 'foo',
  320. '1' => '',
  321. '2' => 'bar',
  322. );
  323. $compareValue = '';
  324. $expectedResult = array(
  325. '0' => 'foo',
  326. '2' => 'bar',
  327. );
  328. $actualResult = t3lib_div::removeArrayEntryByValue($inputArray, $compareValue);
  329. $this->assertEquals($expectedResult, $actualResult);
  330. }
  331. /**
  332. * Checks whether measurement strings like "100k" return the accordant
  333. * byte representation like 102400 in this case.
  334. *
  335. * @test
  336. */
  337. public function checkGetBytesFromSizeMeasurement() {
  338. $this->assertEquals(
  339. '102400',
  340. t3lib_div::getBytesFromSizeMeasurement('100k')
  341. );
  342. $this->assertEquals(
  343. '104857600',
  344. t3lib_div::getBytesFromSizeMeasurement('100m')
  345. );
  346. $this->assertEquals(
  347. '107374182400',
  348. t3lib_div::getBytesFromSizeMeasurement('100g')
  349. );
  350. }
  351. /**
  352. * @test
  353. */
  354. public function checkIndpEnvTypo3SitePathNotEmpty() {
  355. $actualEnv = t3lib_div::getIndpEnv('TYPO3_SITE_PATH');
  356. $this->assertTrue(strlen($actualEnv) >= 1);
  357. $this->assertEquals('/', $actualEnv{0});
  358. $this->assertEquals('/', $actualEnv{strlen($actualEnv) - 1});
  359. }
  360. /**
  361. * @test
  362. * @see t3lib_div::underscoredToUpperCamelCase
  363. */
  364. public function canConvertFromUnderscoredToUpperCamelCase() {
  365. $this->assertEquals('BlogExample', t3lib_div::underscoredToUpperCamelCase('blog_example'));
  366. $this->assertEquals('Blogexample', t3lib_div::underscoredToUpperCamelCase('blogexample'));
  367. }
  368. /**
  369. * @test
  370. * @see t3lib_div::underscoredToLowerCamelCase
  371. */
  372. public function canConvertFromUnderscoredToLowerCamelCase() {
  373. $this->assertEquals('minimalValue', t3lib_div::underscoredToLowerCamelCase('minimal_value'));
  374. $this->assertEquals('minimalvalue', t3lib_div::underscoredToLowerCamelCase('minimalvalue'));
  375. }
  376. /**
  377. * @test
  378. * @see t3lib_div::camelCaseToLowerCaseUnderscored
  379. */
  380. public function canConvertFromCamelCaseToLowerCaseUnderscored() {
  381. $this->assertEquals('blog_example', t3lib_div::camelCaseToLowerCaseUnderscored('BlogExample'));
  382. $this->assertEquals('blogexample', t3lib_div::camelCaseToLowerCaseUnderscored('Blogexample'));
  383. $this->assertEquals('blogexample', t3lib_div::camelCaseToLowerCaseUnderscored('blogexample'));
  384. $this->assertEquals('minimal_value', t3lib_div::camelCaseToLowerCaseUnderscored('minimalValue'));
  385. }
  386. /**
  387. * @test
  388. * @see t3lib_div::lcfirst
  389. */
  390. public function canConvertFirstCharacterToBeLowerCase() {
  391. $this->assertEquals('blogexample', t3lib_div::lcfirst('Blogexample'));
  392. $this->assertEquals('blogExample', t3lib_div::lcfirst('BlogExample'));
  393. $this->assertEquals('blogexample', t3lib_div::lcfirst('blogexample'));
  394. }
  395. /**
  396. * Tests whether whitespaces are encoded correctly in a quoted-printable mail header.
  397. * @test
  398. */
  399. public function areWhitespacesEncodedInQuotedPrintableMailHeader() {
  400. $this->assertEquals(
  401. '=?utf-8?Q?We_test_whether_the_copyright_character_=C2=A9_is_encoded_correctly?=',
  402. t3lib_div::encodeHeader(
  403. "We test whether the copyright character \xc2\xa9 is encoded correctly",
  404. 'quoted-printable',
  405. 'utf-8'
  406. )
  407. );
  408. }
  409. /**
  410. * Tests whether question marks are encoded correctly in a quoted-printable mail header.
  411. * @test
  412. */
  413. public function areQuestionMarksEncodedInQuotedPrintableMailHeader() {
  414. $this->assertEquals(
  415. '=?utf-8?Q?Is_the_copyright_character_=C2=A9_really_encoded_correctly=3F_Really=3F?=',
  416. t3lib_div::encodeHeader(
  417. "Is the copyright character \xc2\xa9 really encoded correctly? Really?",
  418. 'quoted-printable',
  419. 'utf-8'
  420. )
  421. );
  422. }
  423. /**
  424. * Data provider for valid URLs, like PHP's source code test cases
  425. */
  426. public function validUrlDataProvider() {
  427. return array(
  428. array('http://example.com/index.html'),
  429. array('http://www.example.com/index.php'),
  430. array('http://www.example/img/test.png'),
  431. array('http://www.example/img/dir/'),
  432. array('http://www.example/img/dir'),
  433. array('file:///tmp/test.c'),
  434. array('ftp://ftp.example.com/tmp/'),
  435. array('mailto:foo@bar.com'),
  436. array('news:news.php.net'),
  437. array('file://foo/bar'),
  438. array('http://qwe'),
  439. );
  440. }
  441. /**
  442. * Data provider for invalid URLs, like PHP's source code test cases
  443. */
  444. public function invalidUrlDataProvider() {
  445. return array(
  446. array('http//www.example/wrong/url/'),
  447. array('http:/www.example'),
  448. array('/tmp/test.c'),
  449. array('/'),
  450. array('http://'),
  451. array('http:/'),
  452. array('http:'),
  453. array('http'),
  454. array(''),
  455. array('-1'),
  456. array('array()'),
  457. array('qwe'),
  458. );
  459. }
  460. /**
  461. * @test
  462. * @dataProvider validUrlDataProvider
  463. * @see t3lib_div::isValidUrl()
  464. */
  465. public function checkisValidURL($url) {
  466. $this->assertTrue(t3lib_div::isValidUrl($url));
  467. }
  468. /**
  469. * @test
  470. * @dataProvider invalidUrlDataProvider
  471. * @see t3lib_div::isValidUrl()
  472. */
  473. public function checkisInValidURL($url) {
  474. $this->assertFalse(t3lib_div::isValidUrl($url));
  475. }
  476. /**
  477. * @test
  478. * @see t3lib_div::isValidUrl()
  479. */
  480. public function checkisValidURLSucceedsWithWebRessource() {
  481. $testUrl = 'http://www.example.org/';
  482. $this->assertTrue(t3lib_div::isValidUrl($testUrl));
  483. }
  484. /**
  485. * @test
  486. * @see t3lib_div::isValidUrl()
  487. */
  488. public function checkisValidURLSucceedsWithExtentedWebRessource() {
  489. $testUrl = 'https://user:pw@www.example.org:80/path?arg=value#fragment';
  490. $this->assertTrue(t3lib_div::isValidUrl($testUrl));
  491. }
  492. /**
  493. * @test
  494. * @see t3lib_div::isValidUrl()
  495. */
  496. public function checkisValidURLSucceedsWithTelnetRessource() {
  497. $testUrl = 'telnet://192.0.2.16:80/';
  498. $this->assertTrue(t3lib_div::isValidUrl($testUrl));
  499. }
  500. /**
  501. * @test
  502. */
  503. public function checkisValidURLSucceedsWithLdapRessource() {
  504. $testUrl = 'ldap://[2001:db8::7]/c=GB?objectClass?one';
  505. $this->assertTrue(t3lib_div::isValidUrl($testUrl));
  506. }
  507. /**
  508. * @test
  509. * @see t3lib_div::isValidUrl()
  510. */
  511. public function checkisValidURLSucceedsWithFileRessource() {
  512. $testUrl = 'file:///etc/passwd';
  513. $this->assertTrue(t3lib_div::isValidUrl($testUrl));
  514. }
  515. /**
  516. * @test
  517. * @see t3lib_div::isValidUrl()
  518. */
  519. public function checkisValidURLFailsWithHostnameOnly() {
  520. $testUrl = 'www.example.org/';
  521. $this->assertFalse(t3lib_div::isValidUrl($testUrl));
  522. }
  523. /**
  524. * @test
  525. * @see t3lib_div::isOnCurrentHost()
  526. */
  527. public function checkisOnCurrentHostFailsWithLocalhostIPOnly() {
  528. $testUrl = '127.0.0.1';
  529. $this->assertFalse(t3lib_div::isOnCurrentHost($testUrl));
  530. }
  531. /**
  532. * @test
  533. * @see t3lib_div::isOnCurrentHost()
  534. */
  535. public function checkisOnCurrentHostFailsWithPathsOnly() {
  536. $testUrl = './relpath/file.txt';
  537. $this->assertFalse(t3lib_div::isOnCurrentHost($testUrl));
  538. $testUrl = '/abspath/file.txt?arg=value';
  539. $this->assertFalse(t3lib_div::isOnCurrentHost($testUrl));
  540. }
  541. /**
  542. * @test
  543. * @see t3lib_div::isOnCurrentHost()
  544. */
  545. public function checkisOnCurrentHostFailsWithArbitraryString() {
  546. $testUrl = 'arbitrary string';
  547. $this->assertFalse(t3lib_div::isOnCurrentHost($testUrl));
  548. }
  549. /**
  550. * @test
  551. * @see t3lib_div::isOnCurrentHost()
  552. */
  553. public function checkisOnCurrentHostFailsWithEmptyUrl() {
  554. $testUrl = '';
  555. $this->assertFalse(t3lib_div::isOnCurrentHost($testUrl));
  556. }
  557. /**
  558. * @test
  559. * @see t3lib_div::isOnCurrentHost()
  560. */
  561. public function checkisOnCurrentHostFailsWithDifferentHost() {
  562. $testUrl = t3lib_div::getIndpEnv('TYPO3_REQUEST_HOST') . '.example.org';
  563. $this->assertFalse(t3lib_div::isOnCurrentHost($testUrl));
  564. }
  565. /**
  566. * @test
  567. * @see t3lib_div::isOnCurrentHost()
  568. */
  569. public function checkisOnCurrentHostSucceedsWithCurrentHost() {
  570. $testUrl = t3lib_div::getIndpEnv('TYPO3_REQUEST_URL');
  571. $this->assertTrue(t3lib_div::isOnCurrentHost($testUrl));
  572. }
  573. ////////////////////////////////////////
  574. // Tests concerning sanitizeLocalUrl
  575. ////////////////////////////////////////
  576. /**
  577. * Data provider for valid URLs.
  578. * @see sanitizeLocalUrlAcceptsValidUrls
  579. */
  580. public function validLocalUrlDataProvider() {
  581. $subDirectory = t3lib_div::getIndpEnv('TYPO3_SITE_PATH');
  582. $typo3SiteUrl = t3lib_div::getIndpEnv('TYPO3_SITE_URL');
  583. $typo3RequestHost = t3lib_div::getIndpEnv('TYPO3_REQUEST_HOST');
  584. return array(
  585. 'alt_intro.php' => array('alt_intro.php'),
  586. 'alt_intro.php?foo=1&bar=2' => array('alt_intro.php?foo=1&bar=2'),
  587. $subDirectory . 'typo3/alt_intro.php' => array($subDirectory . 'typo3/alt_intro.php'),
  588. $subDirectory . 'index.php' => array($subDirectory . 'index.php'),
  589. '../index.php' => array('../index.php'),
  590. '../typo3/alt_intro.php' => array('../typo3/alt_intro.php'),
  591. '../~userDirectory/index.php' => array('../~userDirectory/index.php'),
  592. '../typo3/mod.php?var1=test-case&var2=~user' => array('../typo3/mod.php?var1=test-case&var2=~user'),
  593. PATH_site . 'typo3/alt_intro.php' => array(PATH_site . 'typo3/alt_intro.php'),
  594. $typo3SiteUrl . 'typo3/alt_intro.php' => array($typo3SiteUrl . 'typo3/alt_intro.php'),
  595. $typo3RequestHost . $subDirectory . '/index.php' => array($typo3RequestHost . $subDirectory . '/index.php'),
  596. );
  597. }
  598. /**
  599. * Data provider for invalid URLs.
  600. * @see sanitizeLocalUrlDeniesInvalidUrls
  601. */
  602. public function invalidLocalUrlDataProvider() {
  603. return array(
  604. array(''),
  605. array('http://www.google.de/'),
  606. array('https://www.google.de/'),
  607. array('../typo3/whatever.php?argument=javascript:alert(0)'),
  608. );
  609. }
  610. /**
  611. * Tests whether valid local URLs are handled correctly.
  612. * @dataProvider validLocalUrlDataProvider
  613. * @test
  614. */
  615. public function sanitizeLocalUrlAcceptsPlainValidUrls($url) {
  616. $this->assertEquals($url, t3lib_div::sanitizeLocalUrl($url));
  617. }
  618. /**
  619. * Tests whether valid local URLs are handled correctly.
  620. * @dataProvider validLocalUrlDataProvider
  621. * @test
  622. */
  623. public function sanitizeLocalUrlAcceptsEncodedValidUrls($url) {
  624. $this->assertEquals(rawurlencode($url), t3lib_div::sanitizeLocalUrl(rawurlencode($url)));
  625. }
  626. /**
  627. * Tests whether valid local URLs are handled correctly.
  628. * @dataProvider invalidLocalUrlDataProvider
  629. * @test
  630. */
  631. public function sanitizeLocalUrlDeniesPlainInvalidUrls($url) {
  632. $this->assertEquals('', t3lib_div::sanitizeLocalUrl($url));
  633. }
  634. /**
  635. * Tests whether valid local URLs are handled correctly.
  636. * @dataProvider invalidLocalUrlDataProvider
  637. * @test
  638. */
  639. public function sanitizeLocalUrlDeniesEncodedInvalidUrls($url) {
  640. $this->assertEquals('', t3lib_div::sanitizeLocalUrl(rawurlencode($url)));
  641. }
  642. //////////////////////////////////////
  643. // Tests concerning arrayDiffAssocRecursive
  644. //////////////////////////////////////
  645. /**
  646. * Test if a one dimensional array is correctly diffed.
  647. *
  648. * @test
  649. * @see t3lib_div::arrayDiffAssocRecursive
  650. */
  651. public function doesArrayDiffAssocRecursiveCorrectlyHandleOneDimensionalArrays() {
  652. $array1 = array(
  653. 'key1' => 'value1',
  654. 'key2' => 'value2',
  655. 'key3' => 'value3',
  656. );
  657. $array2 = array(
  658. 'key1' => 'value1',
  659. 'key3' => 'value3',
  660. );
  661. $expectedResult = array(
  662. 'key2' => 'value2',
  663. );
  664. $actualResult = t3lib_div::arrayDiffAssocRecursive($array1, $array2);
  665. $this->assertEquals($expectedResult, $actualResult);
  666. }
  667. /**
  668. * Test if a three dimensional array is correctly diffed.
  669. *
  670. * @test
  671. * @see t3lib_div::arrayDiffAssocRecursive
  672. */
  673. public function doesArrayDiffAssocRecursiveCorrectlyHandleMultiDimensionalArrays() {
  674. $array1 = array(
  675. 'key1' => 'value1',
  676. 'key2' => array(
  677. 'key21' => 'value21',
  678. 'key22' => 'value22',
  679. 'key23' => array(
  680. 'key231' => 'value231',
  681. 'key232' => 'value232',
  682. ),
  683. ),
  684. );
  685. $array2 = array(
  686. 'key1' => 'value1',
  687. 'key2' => array(
  688. 'key21' => 'value21',
  689. 'key23' => array(
  690. 'key231' => 'value231',
  691. ),
  692. ),
  693. );
  694. $expectedResult = array(
  695. 'key2' => array(
  696. 'key22' => 'value22',
  697. 'key23' => array(
  698. 'key232' => 'value232',
  699. ),
  700. ),
  701. );
  702. $actualResult = t3lib_div::arrayDiffAssocRecursive($array1, $array2);
  703. $this->assertEquals($expectedResult, $actualResult);
  704. }
  705. /**
  706. * Test if arrays are correctly diffed if types are different.
  707. *
  708. * @test
  709. * @see t3lib_div::arrayDiffAssocRecursive
  710. */
  711. public function doesArrayDiffAssocRecursiveCorrectlyHandleMixedArrays() {
  712. $array1 = array(
  713. 'key1' => array(
  714. 'key11' => 'value11',
  715. 'key12' => 'value12',
  716. ),
  717. 'key2' => 'value2',
  718. 'key3' => 'value3',
  719. );
  720. $array2 = array(
  721. 'key1' => 'value1',
  722. 'key2' => array(
  723. 'key21' => 'value21',
  724. ),
  725. );
  726. $expectedResult = array(
  727. 'key3' => 'value3',
  728. );
  729. $actualResult = t3lib_div::arrayDiffAssocRecursive($array1, $array2);
  730. $this->assertEquals($expectedResult, $actualResult);
  731. }
  732. //////////////////////////////////////
  733. // Tests concerning removeDotsFromTS
  734. //////////////////////////////////////
  735. /**
  736. * Tests whether removeDotsFromTS() behaves correctly.
  737. * @test
  738. * @see t3lib_div::removeDotsFromTS()
  739. */
  740. public function doesRemoveDotsFromTypoScriptSucceed() {
  741. $typoScript = array(
  742. 'propertyA.' => array(
  743. 'keyA.' => array(
  744. 'valueA' => 1,
  745. ),
  746. 'keyB' => 2,
  747. ),
  748. 'propertyB' => 3,
  749. );
  750. $expectedResult = array(
  751. 'propertyA' => array(
  752. 'keyA' => array(
  753. 'valueA' => 1,
  754. ),
  755. 'keyB' => 2,
  756. ),
  757. 'propertyB' => 3,
  758. );
  759. $this->assertEquals($expectedResult, t3lib_div::removeDotsFromTS($typoScript));
  760. }
  761. /**
  762. * Tests whether removeDotsFromTS() behaves correctly.
  763. * @test
  764. * @see t3lib_div::removeDotsFromTS()
  765. */
  766. public function doesRemoveDotsFromTypoScriptCorrectlyOverrideWithArray() {
  767. $typoScript = array(
  768. 'propertyA.' => array(
  769. 'keyA' => 'getsOverridden',
  770. 'keyA.' => array(
  771. 'valueA' => 1,
  772. ),
  773. 'keyB' => 2,
  774. ),
  775. 'propertyB' => 3,
  776. );
  777. $expectedResult = array(
  778. 'propertyA' => array(
  779. 'keyA' => array(
  780. 'valueA' => 1,
  781. ),
  782. 'keyB' => 2,
  783. ),
  784. 'propertyB' => 3,
  785. );
  786. $this->assertEquals($expectedResult, t3lib_div::removeDotsFromTS($typoScript));
  787. }
  788. /**
  789. * Tests whether removeDotsFromTS() behaves correctly.
  790. * @test
  791. * @see t3lib_div::removeDotsFromTS()
  792. */
  793. public function doesRemoveDotsFromTypoScriptCorrectlyOverrideWithScalar() {
  794. $typoScript = array(
  795. 'propertyA.' => array(
  796. 'keyA.' => array(
  797. 'valueA' => 1,
  798. ),
  799. 'keyA' => 'willOverride',
  800. 'keyB' => 2,
  801. ),
  802. 'propertyB' => 3,
  803. );
  804. $expectedResult = array(
  805. 'propertyA' => array(
  806. 'keyA' => 'willOverride',
  807. 'keyB' => 2,
  808. ),
  809. 'propertyB' => 3,
  810. );
  811. $this->assertEquals($expectedResult, t3lib_div::removeDotsFromTS($typoScript));
  812. }
  813. /**
  814. * Tests whether getDirs() returns an array of diretories from a given path
  815. * @test
  816. * @see t3lib_div::getDirs($path)
  817. */
  818. public function checkGetDirsReturnsArrayOfDirectoriesFromGivenDirectory() {
  819. $path = PATH_t3lib;
  820. $directories = t3lib_div::get_dirs($path);
  821. $this->assertType('array', $directories);
  822. }
  823. /**
  824. * Tests whether getDirs() returns the string 'error' in case of problems reading from the given path
  825. * @test
  826. * @see t3lib_div::getDirs($path)
  827. */
  828. public function checkGetDirsReturnsStringErrorOnPathFailure() {
  829. $path = 'foo';
  830. $result = t3lib_div::get_dirs($path);
  831. $expectedResult = 'error';
  832. $this->assertEquals($expectedResult, $result);
  833. }
  834. //////////////////////////////////
  835. // Tests concerning hmac
  836. //////////////////////////////////
  837. /**
  838. * @test
  839. */
  840. public function hmacReturnsHashOfProperLength() {
  841. $hmac = t3lib_div::hmac('message');
  842. $this->assertTrue(!empty($hmac) && is_string($hmac));
  843. $this->assertTrue(strlen($hmac) == 40);
  844. }
  845. /**
  846. * @test
  847. */
  848. public function hmacReturnsEqualHashesForEqualInput() {
  849. $msg0 = 'message';
  850. $msg1 = 'message';
  851. $this->assertEquals(t3lib_div::hmac($msg0), t3lib_div::hmac($msg1));
  852. }
  853. /**
  854. * @test
  855. */
  856. public function hmacReturnsNotEqualHashesForNotEqualInput() {
  857. $msg0 = 'message0';
  858. $msg1 = 'message1';
  859. $this->assertNotEquals(t3lib_div::hmac($msg0), t3lib_div::hmac($msg1));
  860. }
  861. //////////////////////////////////
  862. // Tests concerning quoteJSvalue
  863. //////////////////////////////////
  864. /**
  865. * @test
  866. */
  867. public function quoteJSvalueHtmlspecialcharsDataByDefault() {
  868. $this->assertContains(
  869. '&gt;',
  870. t3lib_div::quoteJSvalue('>')
  871. );
  872. }
  873. /**
  874. * @test
  875. */
  876. public function quoteJSvaluetHtmlspecialcharsDataWithinCDataSetToFalse() {
  877. $this->assertContains(
  878. '&gt;',
  879. t3lib_div::quoteJSvalue('>', false)
  880. );
  881. }
  882. /**
  883. * @test
  884. */
  885. public function quoteJSvaluetNotHtmlspecialcharsDataWithinCDataSetToTrue() {
  886. $this->assertContains(
  887. '>',
  888. t3lib_div::quoteJSvalue('>', true)
  889. );
  890. }
  891. /**
  892. * @test
  893. */
  894. public function quoteJSvalueReturnsEmptyStringQuotedInSingleQuotes() {
  895. $this->assertEquals(
  896. "''",
  897. t3lib_div::quoteJSvalue("", true)
  898. );
  899. }
  900. /**
  901. * @test
  902. */
  903. public function quoteJSvalueNotModifiesStringWithoutSpecialCharacters() {
  904. $this->assertEquals(
  905. "'Hello world!'",
  906. t3lib_div::quoteJSvalue("Hello world!", true)
  907. );
  908. }
  909. /**
  910. * @test
  911. */
  912. public function quoteJSvalueEscapesSingleQuote() {
  913. $this->assertEquals(
  914. "'\\''",
  915. t3lib_div::quoteJSvalue("'", true)
  916. );
  917. }
  918. /**
  919. * @test
  920. */
  921. public function quoteJSvalueEscapesDoubleQuoteWithinCDataSetToTrue() {
  922. $this->assertEquals(
  923. "'\\\"'",
  924. t3lib_div::quoteJSvalue('"', true)
  925. );
  926. }
  927. /**
  928. * @test
  929. */
  930. public function quoteJSvalueEscapesAndHtmlspecialcharsDoubleQuoteWithinCDataSetToFalse() {
  931. $this->assertEquals(
  932. "'\\&quot;'",
  933. t3lib_div::quoteJSvalue('"', false)
  934. );
  935. }
  936. /**
  937. * @test
  938. */
  939. public function quoteJSvalueEscapesTab() {
  940. $this->assertEquals(
  941. "'" . '\t' . "'",
  942. t3lib_div::quoteJSvalue(TAB)
  943. );
  944. }
  945. /**
  946. * @test
  947. */
  948. public function quoteJSvalueEscapesLinefeed() {
  949. $this->assertEquals(
  950. "'" . '\n' . "'",
  951. t3lib_div::quoteJSvalue(LF)
  952. );
  953. }
  954. /**
  955. * @test
  956. */
  957. public function quoteJSvalueEscapesCarriageReturn() {
  958. $this->assertEquals(
  959. "'" . '\r' . "'",
  960. t3lib_div::quoteJSvalue(CR)
  961. );
  962. }
  963. /**
  964. * @test
  965. */
  966. public function quoteJSvalueEscapesBackslah() {
  967. $this->assertEquals(
  968. "'\\\\'",
  969. t3lib_div::quoteJSvalue('\\')
  970. );
  971. }
  972. /**
  973. * Tests the locallangXMLOverride feature of readLLfile()
  974. * @test
  975. */
  976. public function readLLfileLocallangXMLOverride() {
  977. $unique = uniqid('locallangXMLOverrideTest');
  978. $xml = '<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
  979. <T3locallang>
  980. <data type="array">
  981. <languageKey index="default" type="array">
  982. <label index="buttons.logout">EXIT</label>
  983. </languageKey>
  984. </data>
  985. </T3locallang>';
  986. $file = PATH_site . 'typo3temp/' . $unique . '.xml';
  987. t3lib_div::writeFileToTypo3tempDir($file, $xml);
  988. // get default value
  989. $defaultLL = t3lib_div::readLLfile('EXT:lang/locallang_core.xml', 'default');
  990. // set override file
  991. $GLOBALS['TYPO3_CONF_VARS']['SYS']['locallangXMLOverride']['EXT:lang/locallang_core.xml'][$unique] = $file;
  992. // get override value
  993. $overrideLL = t3lib_div::readLLfile('EXT:lang/locallang_core.xml', 'default');
  994. $this->assertNotEquals($overrideLL['default']['buttons.logout'], '');
  995. $this->assertNotEquals($defaultLL['default']['buttons.logout'], $overrideLL['default']['buttons.logout']);
  996. $this->assertEquals($overrideLL['default']['buttons.logout'], 'EXIT');
  997. unlink($file);
  998. }
  999. ///////////////////////////////
  1000. // Tests concerning _GETset()
  1001. ///////////////////////////////
  1002. /**
  1003. * @test
  1004. */
  1005. public function getSetCanSetWholeArray() {
  1006. $_GET = array();
  1007. $GLOBALS['HTTP_GET_VARS'] = array();
  1008. t3lib_div::_GETset(array('oneKey' => 'oneValue'));
  1009. $this->assertEquals(
  1010. array('oneKey' => 'oneValue'),
  1011. $_GET
  1012. );
  1013. $this->assertEquals(
  1014. array('oneKey' => 'oneValue'),
  1015. $GLOBALS['HTTP_GET_VARS']
  1016. );
  1017. }
  1018. /**
  1019. * @test
  1020. */
  1021. public function getSetForArrayDropsExistingValues() {
  1022. $_GET = array();
  1023. $GLOBALS['HTTP_GET_VARS'] = array();
  1024. t3lib_div::_GETset(array('foo' => 'bar'));
  1025. t3lib_div::_GETset(array('oneKey' => 'oneValue'));
  1026. $this->assertEquals(
  1027. array('oneKey' => 'oneValue'),
  1028. $_GET
  1029. );
  1030. $this->assertEquals(
  1031. array('oneKey' => 'oneValue'),
  1032. $GLOBALS['HTTP_GET_VARS']
  1033. );
  1034. }
  1035. /**
  1036. * @test
  1037. */
  1038. public function getSetCanAssignOneValueToOneKey() {
  1039. $_GET = array();
  1040. $GLOBALS['HTTP_GET_VARS'] = array();
  1041. t3lib_div::_GETset('oneValue', 'oneKey');
  1042. $this->assertEquals(
  1043. 'oneValue',
  1044. $_GET['oneKey']
  1045. );
  1046. $this->assertEquals(
  1047. 'oneValue',
  1048. $GLOBALS['HTTP_GET_VARS']['oneKey']
  1049. );
  1050. }
  1051. /**
  1052. * @test
  1053. */
  1054. public function getSetForOneValueNotDropsExistingValues() {
  1055. $_GET = array();
  1056. $GLOBALS['HTTP_GET_VARS'] = array();
  1057. t3lib_div::_GETset(array('foo' => 'bar'));
  1058. t3lib_div::_GETset('oneValue', 'oneKey');
  1059. $this->assertEquals(
  1060. array('foo' => 'bar', 'oneKey' => 'oneValue'),
  1061. $_GET
  1062. );
  1063. $this->assertEquals(
  1064. array('foo' => 'bar', 'oneKey' => 'oneValue'),
  1065. $GLOBALS['HTTP_GET_VARS']
  1066. );
  1067. }
  1068. /**
  1069. * @test
  1070. */
  1071. public function getSetCanAssignAnArrayToSpecificArrayElement() {
  1072. $_GET = array();
  1073. $GLOBALS['HTTP_GET_VARS'] = array();
  1074. t3lib_div::_GETset(array('childKey' => 'oneValue'), 'parentKey');
  1075. $this->assertEquals(
  1076. array('parentKey' => array('childKey' => 'oneValue')),
  1077. $_GET
  1078. );
  1079. $this->assertEquals(
  1080. array('parentKey' => array('childKey' => 'oneValue')),
  1081. $GLOBALS['HTTP_GET_VARS']
  1082. );
  1083. }
  1084. /**
  1085. * @test
  1086. */
  1087. public function getSetCanAssignAValueToSpecificArrayChildElement() {
  1088. $_GET = array();
  1089. $GLOBALS['HTTP_GET_VARS'] = array();
  1090. t3lib_div::_GETset('oneValue', 'parentKey|childKey');
  1091. $this->assertEquals(
  1092. array('parentKey' => array('childKey' => 'oneValue')),
  1093. $_GET
  1094. );
  1095. $this->assertEquals(
  1096. array('parentKey' => array('childKey' => 'oneValue')),
  1097. $GLOBALS['HTTP_GET_VARS']
  1098. );
  1099. }
  1100. /**
  1101. * @test
  1102. */
  1103. public function getSetCanAssignAnArrayToSpecificArrayChildElement() {
  1104. $_GET = array();
  1105. $GLOBALS['HTTP_GET_VARS'] = array();
  1106. t3lib_div::_GETset(
  1107. array('key1' => 'value1', 'key2' => 'value2'),
  1108. 'parentKey|childKey'
  1109. );
  1110. $this->assertEquals(
  1111. array(
  1112. 'parentKey' => array(
  1113. 'childKey' => array('key1' => 'value1', 'key2' => 'value2')
  1114. )
  1115. ),
  1116. $_GET
  1117. );
  1118. $this->assertEquals(
  1119. array(
  1120. 'parentKey' => array(
  1121. 'childKey' => array('key1' => 'value1', 'key2' => 'value2')
  1122. )
  1123. ),
  1124. $GLOBALS['HTTP_GET_VARS']
  1125. );
  1126. }
  1127. /**
  1128. * Checks if t3lib_div::fixPermissions() correctly sets permissions to single file
  1129. * This test assumes directory 'PATH_site'/typo3temp exists
  1130. * This test is not available on windows OS
  1131. *
  1132. * @test
  1133. * @see t3lib_div::fixPermissions()
  1134. */
  1135. public function checkFixPermissionsCorrectlySetsPermissionsToFile() {
  1136. if (TYPO3_OS == 'WIN') {
  1137. $this->markTestSkipped('fixPermissions() tests not available on Windows');
  1138. }
  1139. // Create and prepare test file
  1140. $filename = PATH_site . 'typo3temp/' . uniqid('test_');
  1141. t3lib_div::writeFileToTypo3tempDir($filename, '42');
  1142. chmod($filename, 0742);
  1143. // Set target permissions and run method
  1144. $GLOBALS['TYPO3_CONF_VARS']['BE']['fileCreateMask'] = '0660';
  1145. $GLOBALS['TYPO3_CONF_VARS']['BE']['createGroup'] = posix_getegid();
  1146. $fixPermissionsResult = t3lib_div::fixPermissions($filename);
  1147. // Get actual permissions and clean up
  1148. clearstatcache();
  1149. $resultFilePermissions = substr(decoct(fileperms($filename)), 2);
  1150. $resultFileGroup = filegroup($filename);
  1151. unlink($filename);
  1152. // Test if everything was ok
  1153. $this->assertTrue($fixPermissionsResult);
  1154. $this->assertEquals($resultFilePermissions, '0660');
  1155. $this->assertEquals($resultFileGroup, posix_getegid());
  1156. }
  1157. /**
  1158. * Checks if t3lib_div::fixPermissions() correctly sets permissions to hidden file
  1159. * This test assumes directory 'PATH_site'/typo3temp exists
  1160. * This test is not available on windows OS
  1161. *
  1162. * @test
  1163. * @see t3lib_div::fixPermissions()
  1164. */
  1165. public function checkFixPermissionsCorrectlySetsPermissionsToHiddenFile() {
  1166. if (TYPO3_OS == 'WIN') {
  1167. $this->markTestSkipped('fixPermissions() tests not available on Windows');
  1168. }
  1169. // Create and prepare test file
  1170. $filename = PATH_site . 'typo3temp/' . uniqid('.test_');
  1171. t3lib_div::writeFileToTypo3tempDir($filename, '42');
  1172. chmod($filename, 0742);
  1173. // Set target permissions and run method
  1174. $GLOBALS['TYPO3_CONF_VARS']['BE']['fileCreateMask'] = '0660';
  1175. $GLOBALS['TYPO3_CONF_VARS']['BE']['createGroup'] = posix_getegid();
  1176. $fixPermissionsResult = t3lib_div::fixPermissions($filename);
  1177. // Get actual permissions and clean up
  1178. clearstatcache();
  1179. $resultFilePermissions = substr(decoct(fileperms($filename)), 2);
  1180. $resultFileGroup = filegroup($filename);
  1181. unlink($filename);
  1182. // Test if everything was ok
  1183. $this->assertTrue($fixPermissionsResult);
  1184. $this->assertEquals($resultFilePermissions, '0660');
  1185. $this->assertEquals($resultFileGroup, posix_getegid());
  1186. }
  1187. /**
  1188. * Checks if t3lib_div::fixPermissions() correctly sets permissions to directory with trailing slash
  1189. * This test assumes directory 'PATH_site'/typo3temp exists
  1190. * This test is not available on windows OS
  1191. *
  1192. * @test
  1193. * @see t3lib_div::fixPermissions()
  1194. */
  1195. public function checkFixPermissionsCorrectlySetsPermissionsToDirectory() {
  1196. if (TYPO3_OS == 'WIN') {
  1197. $this->markTestSkipped('fixPermissions() tests not available on Windows');
  1198. }
  1199. // Create and prepare test directory
  1200. $directory = PATH_site . 'typo3temp/' . uniqid('test_');
  1201. t3lib_div::mkdir($directory);
  1202. chmod($directory, 1551);
  1203. // Set target permissions and run method
  1204. $GLOBALS['TYPO3_CONF_VARS']['BE']['folderCreateMask'] = '0770';
  1205. $GLOBALS['TYPO3_CONF_VARS']['BE']['createGroup'] = posix_getegid();
  1206. $fixPermissionsResult = t3lib_div::fixPermissions($directory . '/');
  1207. // Get actual permissions and clean up
  1208. clearstatcache();
  1209. $resultDirectoryPermissions = substr(decoct(fileperms($directory)), 1);
  1210. $resultDirectoryGroup = filegroup($directory);
  1211. t3lib_div::rmdir($directory);
  1212. // Test if everything was ok
  1213. $this->assertTrue($fixPermissionsResult);
  1214. $this->assertEquals($resultDirectoryPermissions, '0770');
  1215. $this->assertEquals($resultDirectoryGroup, posix_getegid());
  1216. }
  1217. /**
  1218. * Checks if t3lib_div::fixPermissions() correctly sets permissions to hidden directory
  1219. * This test assumes directory 'PATH_site'/typo3temp exists
  1220. * This test is not available on windows OS
  1221. *
  1222. * @test
  1223. * @see t3lib_div::fixPermissions()
  1224. */
  1225. public function checkFixPermissionsCorrectlySetsPermissionsToHiddenDirectory() {
  1226. if (TYPO3_OS == 'WIN') {
  1227. $this->markTestSkipped('fixPermissions() tests not available on Windows');
  1228. }
  1229. // Create and prepare test directory
  1230. $directory = PATH_site . 'typo3temp/' . uniqid('.test_');
  1231. t3lib_div::mkdir($directory);
  1232. chmod($directory, 1551);
  1233. // Set target permissions and run method
  1234. $GLOBALS['TYPO3_CONF_VARS']['BE']['folderCreateMask'] = '0770';
  1235. $GLOBALS['TYPO3_CONF_VARS']['BE']['createGroup'] = posix_getegid();
  1236. $fixPermissionsResult = t3lib_div::fixPermissions($directory);
  1237. // Get actual permissions and clean up
  1238. clearstatcache();
  1239. $resultDirectoryPermissions = substr(decoct(fileperms($directory)), 1);
  1240. $resultDirectoryGroup = filegroup($directory);
  1241. t3lib_div::rmdir($directory);
  1242. // Test if everything was ok
  1243. $this->assertTrue($fixPermissionsResult);
  1244. $this->assertEquals($resultDirectoryPermissions, '0770');
  1245. $this->assertEquals($resultDirectoryGroup, posix_getegid());
  1246. }
  1247. /**
  1248. * Checks if t3lib_div::fixPermissions() correctly sets permissions recursivly
  1249. * This test assumes directory 'PATH_site'/typo3temp exists
  1250. * This test is not available on windows OS
  1251. *
  1252. * @test
  1253. * @see t3lib_div::fixPermissions()
  1254. */
  1255. public function checkFixPermissionsCorrectlySetsPermissionsRecursive() {
  1256. if (TYPO3_OS == 'WIN') {
  1257. $this->markTestSkipped('fixPermissions() tests not available on Windows');
  1258. }
  1259. // Create and prepare test directory and file structure
  1260. $baseDirectory = PATH_site . 'typo3temp/' . uniqid('test_');
  1261. t3lib_div::mkdir($baseDirectory);
  1262. chmod($baseDirectory, 1751);
  1263. t3lib_div::writeFileToTypo3tempDir($baseDirectory . '/file', '42');
  1264. chmod($baseDirectory . '/file', 0742);
  1265. t3lib_div::mkdir($baseDirectory . '/foo');
  1266. chmod($baseDirectory . '/foo', 1751);
  1267. t3lib_div::writeFileToTypo3tempDir($baseDirectory . '/foo/file', '42');
  1268. chmod($baseDirectory . '/foo/file', 0742);
  1269. t3lib_div::mkdir($baseDirectory . '/.bar');
  1270. chmod($baseDirectory . '/.bar', 1751);
  1271. // Use this if writeFileToTypo3tempDir is fixed to create hidden files in subdirectories
  1272. // t3lib_div::writeFileToTypo3tempDir($baseDirectory . '/.bar/.file', '42');
  1273. // t3lib_div::writeFileToTypo3tempDir($baseDirectory . '/.bar/..file2', '42');
  1274. touch($baseDirectory . '/.bar/.file', '42');
  1275. chmod($baseDirectory . '/.bar/.file', 0742);
  1276. touch($baseDirectory . '/.bar/..file2', '42');
  1277. chmod($baseDirectory . '/.bar/..file2', 0742);
  1278. // Set target permissions and run method
  1279. $GLOBALS['TYPO3_CONF_VARS']['BE']['fileCreateMask'] = '0660';
  1280. $GLOBALS['TYPO3_CONF_VARS']['BE']['folderCreateMask'] = '0770';
  1281. $GLOBALS['TYPO3_CONF_VARS']['BE']['createGroup'] = posix_getegid();
  1282. $fixPermissionsResult = t3lib_div::fixPermissions($baseDirectory, TRUE);
  1283. // Get actual permissions
  1284. clearstatcache();
  1285. $resultBaseDirectoryPermissions = substr(decoct(fileperms($baseDirectory)), 1);
  1286. $resultBaseDirectoryGroup = filegroup($baseDirectory);
  1287. $resultBaseFilePermissions = substr(decoct(fileperms($baseDirectory . '/file')), 2);
  1288. $resultBaseFileGroup = filegroup($baseDirectory . '/file');
  1289. $resultFooDirectoryPermissions = substr(decoct(fileperms($baseDirectory . '/foo')), 1);
  1290. $resultFooDirectoryGroup = filegroup($baseDirectory . '/foo');
  1291. $resultFooFilePermissions = substr(decoct(fileperms($baseDirectory . '/foo/file')), 2);
  1292. $resultFooFileGroup = filegroup($baseDirectory . '/foo/file');
  1293. $resultBarDirectoryPermissions = substr(decoct(fileperms($baseDirectory . '/.bar')), 1);
  1294. $resultBarDirectoryGroup = filegroup($baseDirectory . '/.bar');
  1295. $resultBarFilePermissions = substr(decoct(fileperms($baseDirectory . '/.bar/.file')), 2);
  1296. $resultBarFileGroup = filegroup($baseDirectory . '/.bar/.file');
  1297. $resultBarFile2Permissions = substr(decoct(fileperms($baseDirectory . '/.bar/..file2')), 2);
  1298. $resultBarFile2Group = filegroup($baseDirectory . '/.bar/..file2');
  1299. // Clean up
  1300. unlink($baseDirectory . '/file');
  1301. unlink($baseDirectory . '/foo/file');
  1302. unlink($baseDirectory . '/.bar/.file');
  1303. unlink($baseDirectory . '/.bar/..file2');
  1304. t3lib_div::rmdir($baseDirectory . '/foo');
  1305. t3lib_div::rmdir($baseDirectory . '/.bar');
  1306. t3lib_div::rmdir($baseDirectory);
  1307. // Test if everything was ok
  1308. $this->assertTrue($fixPermissionsResult);
  1309. $this->assertEquals($resultBaseDirectoryPermissions, '0770');
  1310. $this->assertEquals($resultBaseDirectoryGroup, posix_getegid());
  1311. $this->assertEquals($resultBaseFilePermissions, '0660');
  1312. $this->assertEquals($resultBaseFileGroup, posix_getegid());
  1313. $this->assertEquals($resultFooDirectoryPermissions, '0770');
  1314. $this->assertEquals($resultFooDirectoryGroup, posix_getegid());
  1315. $this->assertEquals($resultFooFilePermissions, '0660');
  1316. $this->assertEquals($resultFooFileGroup, posix_getegid());
  1317. $this->assertEquals($resultBarDirectoryPermissions, '0770');
  1318. $this->assertEquals($resultBarDirectoryGroup, posix_getegid());
  1319. $this->assertEquals($resultBarFilePermissions, '0660');
  1320. $this->assertEquals($resultBarFileGroup, posix_getegid());
  1321. $this->assertEquals($resultBarFile2Permissions, '0660');
  1322. $this->assertEquals($resultBarFile2Group, posix_getegid());
  1323. }
  1324. /**
  1325. * Checks if t3lib_div::fixPermissions() does not fix permissions on not allowed path
  1326. * This test assumes directory 'PATH_site'/typo3temp exists
  1327. * This test is not available on windows OS
  1328. *
  1329. * @test
  1330. * @see t3lib_div::fixPermissions()
  1331. */
  1332. public function checkFixPermissionsDoesNotSetPermissionsToNotAllowedPath() {
  1333. if (TYPO3_OS == 'WIN') {
  1334. $this->markTestSkipped('fixPermissions() tests not available on Windows');
  1335. }
  1336. // Create and prepare test file
  1337. $filename = PATH_site . 'typo3temp/../typo3temp/' . uniqid('test_');
  1338. touch($filename);
  1339. chmod($filename, 0742);
  1340. // Set target permissions and run method
  1341. $GLOBALS['TYPO3_CONF_VARS']['BE']['fileCreateMask'] = '0660';
  1342. $fixPermissionsResult = t3lib_div::fixPermissions($filename);
  1343. // Get actual permissions and clean up
  1344. clearstatcache();
  1345. $resultFilePermissions = substr(decoct(fileperms($filename)), 2);
  1346. unlink($filename);
  1347. // Test if everything was ok
  1348. $this->assertFalse($fixPermissionsResult);
  1349. $this->assertEquals($resultFilePermissions, '0742');
  1350. }
  1351. /**
  1352. * Checks if t3lib_div::mkdir() correctly creates a directory
  1353. * This test assumes directory 'PATH_site'/typo3temp exists
  1354. *
  1355. * @test
  1356. * @see t3lib_div::mkdir()
  1357. */
  1358. public function checkMkdirCorrectlyCreatesDirectory() {
  1359. $directory = PATH_site . 'typo3temp/' . uniqid('test_');
  1360. $mkdirResult = t3lib_div::mkdir($directory);
  1361. $directoryCreated = is_dir($directory);
  1362. t3lib_div::rmdir($directory);
  1363. $this->assertTrue($mkdirResult);
  1364. $this->assertTrue($directoryCreated);
  1365. }
  1366. /**
  1367. * Checks if t3lib_div::mkdir() correctly creates a hidden directory
  1368. * This test assumes directory 'PATH_site'/typo3temp exists
  1369. *
  1370. * @test
  1371. * @see t3lib_div::mkdir()
  1372. */
  1373. public function checkMkdirCorrectlyCreatesHiddenDirectory() {
  1374. $directory = PATH_site . 'typo3temp/' . uniqid('.test_');
  1375. $mkdirResult = t3lib_div::mkdir($directory);
  1376. $directoryCreated = is_dir($directory);
  1377. t3lib_div::rmdir($directory);
  1378. $this->assertTrue($mkdirResult);
  1379. $this->assertTrue($directoryCreated);
  1380. }
  1381. /**
  1382. * Checks if t3lib_div::mkdir() correctly creates a directory with trailing slash
  1383. * This test assumes directory 'PATH_site'/typo3temp exists
  1384. *
  1385. * @test
  1386. * @see t3lib_div::mkdir()
  1387. */
  1388. public function checkMkdirCorrectlyCreatesDirectoryWithTrailingSlash() {
  1389. $directory = PATH_site . 'typo3temp/' . uniqid('test_');
  1390. $mkdirResult = t3lib_div::mkdir($directory);
  1391. $directoryCreated = is_dir($directory);
  1392. t3lib_div::rmdir($directory);
  1393. $this->assertTrue($mkdirResult);
  1394. $this->assertTrue($directoryCreated);
  1395. }
  1396. /**
  1397. * Checks if t3lib_div::split_fileref() return NO file extension if incomming $fileref is a folder
  1398. * This test avoid bug #0014845: Filelist module reports "type" of files also for directories
  1399. * This test assumes directory 'PATH_site'/typo3temp exists
  1400. *
  1401. * @test
  1402. * @see t3lib_div::split_fileref()
  1403. */
  1404. public function checkIfSplitFileRefReturnsFileTypeNotForFolders(){
  1405. $directoryName = uniqid('test_') . '.com';
  1406. $directoryPath = PATH_site . 'typo3temp/';
  1407. $directory = $directoryPath . $directoryName;
  1408. mkdir($directory, octdec($GLOBALS['TYPO3_CONF_VARS']['BE']['folderCreateMask']));
  1409. $fileInfo = t3lib_div::split_fileref($directory);
  1410. $directoryCreated = is_dir($directory);
  1411. $this->assertTrue($directoryCreated);
  1412. $this->assertType(PHPUnit_Framework_Constraint_IsType::TYPE_ARRAY, $fileInfo);
  1413. $this->assertEquals($directoryPath, $fileInfo['path']);
  1414. $this->assertEquals($directoryName, $fileInfo['file']);
  1415. $this->assertEquals($directoryName, $fileInfo['filebody']);
  1416. $this->assertEquals('', $fileInfo['fileext']);
  1417. $this->assertArrayNotHasKey('realFileext', $fileInfo);
  1418. rmdir($directory);
  1419. }
  1420. /**
  1421. * @test
  1422. * @see t3lib_div::split_fileref()
  1423. */
  1424. public function checkIfSplitFileRefReturnsFileTypeForFilesWithoutPathSite() {
  1425. $testFile = 'fileadmin/media/someFile.png';
  1426. $fileInfo = t3lib_div::split_fileref($testFile);
  1427. $this->assertType(PHPUnit_Framework_Constraint_IsType::TYPE_ARRAY, $fileInfo);
  1428. $this->assertEquals('fileadmin/media/', $fileInfo['path']);
  1429. $this->assertEquals('someFile.png', $fileInfo['file']);
  1430. $this->assertEquals('someFile', $fileInfo['filebody']);
  1431. $this->assertEquals('png', $fileInfo['fileext']);
  1432. }
  1433. }
  1434. ?>