PageRenderTime 46ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/t3lib/t3lib_divTest.php

https://github.com/andreaswolf/typo3-tceforms
PHP | 2575 lines | 1885 code | 194 blank | 496 comment | 19 complexity | 96872f01e03bb6c00226ced3aa119182 MD5 | raw file
Possible License(s): Apache-2.0, BSD-2-Clause, LGPL-3.0

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /***************************************************************
  3. * Copyright notice
  4. *
  5. * (c) 2009-2011 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. * Enable backup of global and system variables
  36. *
  37. * @var boolean
  38. */
  39. protected $backupGlobals = TRUE;
  40. /**
  41. * Exclude TYPO3_DB from backup/ restore of $GLOBALS
  42. * because resource types cannot be handled during serializing
  43. *
  44. * @var array
  45. */
  46. protected $backupGlobalsBlacklist = array('TYPO3_DB');
  47. public function tearDown() {
  48. t3lib_div::purgeInstances();
  49. }
  50. ///////////////////////////////
  51. // Tests concerning gif_compress
  52. ///////////////////////////////
  53. /**
  54. * @test
  55. */
  56. public function gifCompressFixesPermissionOfConvertedFileIfUsingImagemagick() {
  57. if (TYPO3_OS == 'WIN') {
  58. $this->markTestSkipped('gifCompressFixesPermissionOfConvertedFileIfUsingImagemagick() test not available on Windows.');
  59. }
  60. if (!$GLOBALS['TYPO3_CONF_VARS']['GFX']['im'] || !$GLOBALS['TYPO3_CONF_VARS']['GFX']['im_path_lzw']) {
  61. $this->markTestSkipped('gifCompressFixesPermissionOfConvertedFileIfUsingImagemagick() test not available without imagemagick setup.');
  62. }
  63. $testFinder = t3lib_div::makeInstance('Tx_Phpunit_Service_TestFinder');
  64. $fixtureGifFile = $testFinder->getAbsoluteCoreTestsPath() . 't3lib/fixtures/clear.gif';
  65. $GLOBALS['TYPO3_CONF_VARS']['GFX']['gif_compress'] = TRUE;
  66. // Copy file to unique filename in typo3temp, set target permissions and run method
  67. $testFilename = PATH_site . 'typo3temp/' . uniqid('test_') . '.gif';
  68. @copy($fixtureGifFile, $testFilename);
  69. $GLOBALS['TYPO3_CONF_VARS']['BE']['fileCreateMask'] = '0777';
  70. t3lib_div::gif_compress($testFilename, 'IM');
  71. // Get actual permissions and clean up
  72. clearstatcache();
  73. $resultFilePermissions = substr(decoct(fileperms($testFilename)), 2);
  74. t3lib_div::unlink_tempfile($testFilename);
  75. $this->assertEquals($resultFilePermissions, '0777');
  76. }
  77. /**
  78. * @test
  79. */
  80. public function gifCompressFixesPermissionOfConvertedFileIfUsingGd() {
  81. if (TYPO3_OS == 'WIN') {
  82. $this->markTestSkipped('gifCompressFixesPermissionOfConvertedFileIfUsingImagemagick() test not available on Windows.');
  83. }
  84. $testFinder = t3lib_div::makeInstance('Tx_Phpunit_Service_TestFinder');
  85. $fixtureGifFile = $testFinder->getAbsoluteCoreTestsPath() . 't3lib/fixtures/clear.gif';
  86. $GLOBALS['TYPO3_CONF_VARS']['GFX']['gdlib'] = TRUE;
  87. $GLOBALS['TYPO3_CONF_VARS']['GFX']['gdlib_png'] = FALSE;
  88. // Copy file to unique filename in typo3temp, set target permissions and run method
  89. $testFilename = PATH_site . 'typo3temp/' . uniqid('test_') . '.gif';
  90. @copy($fixtureGifFile, $testFilename);
  91. $GLOBALS['TYPO3_CONF_VARS']['BE']['fileCreateMask'] = '0777';
  92. t3lib_div::gif_compress($testFilename, 'GD');
  93. // Get actual permissions and clean up
  94. clearstatcache();
  95. $resultFilePermissions = substr(decoct(fileperms($testFilename)), 2);
  96. t3lib_div::unlink_tempfile($testFilename);
  97. $this->assertEquals($resultFilePermissions, '0777');
  98. }
  99. ///////////////////////////////
  100. // Tests concerning png_to_gif_by_imagemagick
  101. ///////////////////////////////
  102. /**
  103. * @test
  104. */
  105. public function pngToGifByImagemagickFixesPermissionsOfConvertedFile() {
  106. if (TYPO3_OS == 'WIN') {
  107. $this->markTestSkipped('pngToGifByImagemagickFixesPermissionsOfConvertedFile() test not available on Windows.');
  108. }
  109. if (!$GLOBALS['TYPO3_CONF_VARS']['GFX']['im'] || !$GLOBALS['TYPO3_CONF_VARS']['GFX']['im_path_lzw']) {
  110. $this->markTestSkipped('pngToGifByImagemagickFixesPermissionsOfConvertedFile() test not available without imagemagick setup.');
  111. }
  112. $testFinder = t3lib_div::makeInstance('Tx_Phpunit_Service_TestFinder');
  113. $fixturePngFile = $testFinder->getAbsoluteCoreTestsPath() . 't3lib/fixtures/clear.png';
  114. $GLOBALS['TYPO3_CONF_VARS']['FE']['png_to_gif'] = TRUE;
  115. // Copy file to unique filename in typo3temp, set target permissions and run method
  116. $testFilename = PATH_site . 'typo3temp/' . uniqid('test_') . '.png';
  117. @copy($fixturePngFile, $testFilename);
  118. $GLOBALS['TYPO3_CONF_VARS']['BE']['fileCreateMask'] = '0777';
  119. $newGifFile = t3lib_div::png_to_gif_by_imagemagick($testFilename);
  120. // Get actual permissions and clean up
  121. clearstatcache();
  122. $resultFilePermissions = substr(decoct(fileperms($newGifFile)), 2);
  123. t3lib_div::unlink_tempfile($newGifFile);
  124. $this->assertEquals($resultFilePermissions, '0777');
  125. }
  126. ///////////////////////////////
  127. // Tests concerning read_png_gif
  128. ///////////////////////////////
  129. /**
  130. * @test
  131. */
  132. public function readPngGifFixesPermissionsOfConvertedFile() {
  133. if (TYPO3_OS == 'WIN') {
  134. $this->markTestSkipped('readPngGifFixesPermissionsOfConvertedFile() test not available on Windows.');
  135. }
  136. if (!$GLOBALS['TYPO3_CONF_VARS']['GFX']['im']) {
  137. $this->markTestSkipped('readPngGifFixesPermissionsOfConvertedFile() test not available without imagemagick setup.');
  138. }
  139. $testFinder = t3lib_div::makeInstance('Tx_Phpunit_Service_TestFinder');
  140. $testGifFile = $testFinder->getAbsoluteCoreTestsPath() . 't3lib/fixtures/clear.gif';
  141. // Set target permissions and run method
  142. $GLOBALS['TYPO3_CONF_VARS']['BE']['fileCreateMask'] = '0777';
  143. $newPngFile = t3lib_div::read_png_gif($testGifFile, TRUE);
  144. // Get actual permissions and clean up
  145. clearstatcache();
  146. $resultFilePermissions = substr(decoct(fileperms($newPngFile)), 2);
  147. t3lib_div::unlink_tempfile($newPngFile);
  148. $this->assertEquals($resultFilePermissions, '0777');
  149. }
  150. ///////////////////////////////
  151. // Tests concerning validIP
  152. ///////////////////////////////
  153. /**
  154. * Data provider for checkValidIpReturnsTrueForValidIp
  155. *
  156. * @return array Data sets
  157. */
  158. public static function validIpDataProvider() {
  159. return array(
  160. '0.0.0.0' => array('0.0.0.0'),
  161. 'private IPv4 class C' => array('192.168.0.1'),
  162. 'private IPv4 class A' => array('10.0.13.1'),
  163. 'private IPv6' => array('fe80::daa2:5eff:fe8b:7dfb'),
  164. );
  165. }
  166. /**
  167. * @test
  168. * @dataProvider validIpDataProvider
  169. */
  170. public function validIpReturnsTrueForValidIp($ip) {
  171. $this->assertTrue(t3lib_div::validIP($ip));
  172. }
  173. /**
  174. * Data provider for checkValidIpReturnsFalseForInvalidIp
  175. *
  176. * @return array Data sets
  177. */
  178. public static function invalidIpDataProvider() {
  179. return array(
  180. 'null' => array(null),
  181. 'zero' => array(0),
  182. 'string' => array('test'),
  183. 'string empty' => array(''),
  184. 'string null' => array('null'),
  185. 'out of bounds IPv4' => array('300.300.300.300'),
  186. 'dotted decimal notation with only two dots' => array('127.0.1'),
  187. );
  188. }
  189. /**
  190. * @test
  191. * @dataProvider invalidIpDataProvider
  192. */
  193. public function validIpReturnsFalseForInvalidIp($ip) {
  194. $this->assertFalse(t3lib_div::validIP($ip));
  195. }
  196. ///////////////////////////////
  197. // Tests concerning testInt
  198. ///////////////////////////////
  199. /**
  200. * Data provider for testIntReturnsTrue
  201. *
  202. * @return array Data sets
  203. */
  204. public function functionTestIntValidDataProvider() {
  205. return array(
  206. 'int' => array(32425),
  207. 'negative int' => array(-32425),
  208. 'largest int' => array(PHP_INT_MAX),
  209. 'int as string' => array('32425'),
  210. 'negative int as string' => array('-32425'),
  211. 'zero' => array(0),
  212. 'zero as string' => array('0'),
  213. );
  214. }
  215. /**
  216. * @test
  217. * @dataProvider functionTestIntValidDataProvider
  218. */
  219. public function testIntReturnsTrue($int) {
  220. $this->assertTrue(t3lib_div::testInt($int));
  221. }
  222. /**
  223. * Data provider for testIntReturnsFalse
  224. *
  225. * @return array Data sets
  226. */
  227. public function functionTestIntInvalidDataProvider() {
  228. return array(
  229. 'int as string with leading zero' => array('01234'),
  230. 'positive int as string with plus modifier' => array('+1234'),
  231. 'negative int as string with leading zero' => array('-01234'),
  232. 'largest int plus one' => array(PHP_INT_MAX + 1),
  233. 'string' => array('testInt'),
  234. 'empty string' => array(''),
  235. 'int in string' => array('5 times of testInt'),
  236. 'int as string with space after' => array('5 '),
  237. 'int as string with space before' => array(' 5'),
  238. 'int as string with many spaces before' => array(' 5'),
  239. 'float' => array(3.14159),
  240. 'float as string' => array('3.14159'),
  241. 'float as string only a dot' => array('10.'),
  242. 'float as string trailing zero would evaluate to int 10' => array('10.0'),
  243. 'float as string trailing zeros would evaluate to int 10' => array('10.00'),
  244. 'null' => array(NULL),
  245. 'empty array' => array(array()),
  246. 'int in array' => array(array(32425)),
  247. 'int as string in array' => array(array('32425')),
  248. );
  249. }
  250. /**
  251. * @test
  252. * @dataProvider functionTestIntInvalidDataProvider
  253. */
  254. public function testIntReturnsFalse($int) {
  255. $this->assertFalse(t3lib_div::testInt($int));
  256. }
  257. ///////////////////////////////
  258. // Tests concerning isFirstPartOfStr
  259. ///////////////////////////////
  260. /**
  261. * Data provider for isFirstPartOfStrReturnsTrueForMatchingFirstParts
  262. *
  263. * @return array
  264. */
  265. public function isFirstPartOfStrReturnsTrueForMatchingFirstPartDataProvider() {
  266. return array(
  267. 'match first part of string' => array('hello world', 'hello'),
  268. 'match whole string' => array('hello', 'hello'),
  269. 'integer is part of string with same number' => array('24', 24),
  270. 'string is part of integer with same number' => array(24, '24'),
  271. 'integer is part of string starting with same number' => array('24 beer please', 24),
  272. );
  273. }
  274. /**
  275. * @test
  276. * @dataProvider isFirstPartOfStrReturnsTrueForMatchingFirstPartDataProvider
  277. */
  278. public function isFirstPartOfStrReturnsTrueForMatchingFirstPart($string, $part) {
  279. $this->assertTrue(t3lib_div::isFirstPartOfStr($string, $part));
  280. }
  281. /**
  282. * Data provider for checkIsFirstPartOfStrReturnsFalseForNotMatchingFirstParts
  283. *
  284. * @return array
  285. */
  286. public function isFirstPartOfStrReturnsFalseForNotMatchingFirstPartDataProvider() {
  287. return array(
  288. 'no string match' => array('hello', 'bye'),
  289. 'no case sensitive string match' => array('hello world', 'Hello'),
  290. 'array is not part of string' => array('string', array()),
  291. 'string is not part of array' => array(array(), 'string'),
  292. 'null is not part of string' => array('string', NULL),
  293. 'string is not part of array' => array(NULL, 'string'),
  294. 'null is not part of array' => array(array(), NULL),
  295. 'array is not part of string' => array(NULL, array()),
  296. 'empty string is not part of empty string' => array('', ''),
  297. 'null is not part of empty string' => array('', NULL),
  298. 'false is not part of empty string' => array('', FALSE),
  299. 'empty string is not part of null' => array(NULL, ''),
  300. 'empty string is not part of false' => array(FALSE, ''),
  301. 'empty string is not part of zero integer' => array(0, ''),
  302. 'zero integer is not part of null' => array(NULL, 0),
  303. 'zero integer is not part of empty string' => array('', 0),
  304. );
  305. }
  306. /**
  307. * @test
  308. * @dataProvider isFirstPartOfStrReturnsFalseForNotMatchingFirstPartDataProvider
  309. */
  310. public function isFirstPartOfStrReturnsFalseForNotMatchingFirstPart($string, $part) {
  311. $this->assertFalse(t3lib_div::isFirstPartOfStr($string, $part));
  312. }
  313. ///////////////////////////////
  314. // Tests concerning splitCalc
  315. ///////////////////////////////
  316. /**
  317. * Data provider for splitCalc
  318. *
  319. * @return array expected values, arithmetic expression
  320. */
  321. public function splitCalcDataProvider() {
  322. return array(
  323. 'empty string returns empty array' => array(
  324. array(),
  325. '',
  326. ),
  327. 'number without operator returns array with plus and number' => array(
  328. array(array('+', 42)),
  329. '42',
  330. ),
  331. 'two numbers with asterisk return first number with plus and second number with asterisk' => array(
  332. array(array('+', 42), array('*', 31)),
  333. '42 * 31',
  334. ),
  335. );
  336. }
  337. /**
  338. * @test
  339. * @dataProvider splitCalcDataProvider
  340. */
  341. public function splitCalcCorrectlySplitsExpression($expected, $expression) {
  342. $this->assertEquals($expected, t3lib_div::splitCalc($expression, '+-*/'));
  343. }
  344. //////////////////////////////////
  345. // Tests concerning calcPriority
  346. //////////////////////////////////
  347. /**
  348. * Data provider for calcPriority
  349. *
  350. * @return array expected values, arithmetic expression
  351. */
  352. public function calcPriorityDataProvider() {
  353. return array(
  354. 'add' => array(9, '6 + 3'),
  355. 'substract with positive result' => array(3, '6 - 3'),
  356. 'substract with negative result' => array(-3, '3 - 6'),
  357. 'multiply' => array(6, '2 * 3'),
  358. 'divide' => array(2.5, '5 / 2'),
  359. 'modulus' => array(1, '5 % 2'),
  360. 'power' => array(8, '2 ^ 3'),
  361. 'three operands with non integer result' => array(6.5, '5 + 3 / 2'),
  362. 'three operands with power' => array(14, '5 + 3 ^ 2'),
  363. 'three operads with modulus' => array(4, '5 % 2 + 3'),
  364. 'four operands' => array(3, '2 + 6 / 2 - 2'),
  365. );
  366. }
  367. /**
  368. * @test
  369. * @dataProvider calcPriorityDataProvider
  370. */
  371. public function calcPriorityCorrectlyCalculatesExpression($expected, $expression) {
  372. $this->assertEquals($expected, t3lib_div::calcPriority($expression));
  373. }
  374. //////////////////////////////////
  375. // Tests concerning calcPriority
  376. //////////////////////////////////
  377. /**
  378. * Data provider for valid validEmail's
  379. *
  380. * @return array Valid email addresses
  381. */
  382. public function validEmailValidDataProvider() {
  383. return array(
  384. 'short mail address' => array('a@b.c'),
  385. 'simple mail address' => array('test@example.com'),
  386. 'uppercase characters' => array('QWERTYUIOPASDFGHJKLZXCVBNM@QWERTYUIOPASDFGHJKLZXCVBNM.NET'),
  387. // Fix / change if TYPO3 php requirement changed: Address ok with 5.2.6 and 5.3.2 but fails with 5.3.0 on windows
  388. // 'equal sign in local part' => array('test=mail@example.com'),
  389. 'dash in local part' => array('test-mail@example.com'),
  390. 'plus in local part' => array('test+mail@example.com'),
  391. // Fix / change if TYPO3 php requirement changed: Address ok with 5.2.6 and 5.3.2 but fails with 5.3.0 on windows
  392. // 'question mark in local part' => array('test?mail@example.com'),
  393. 'slash in local part' => array('foo/bar@example.com'),
  394. 'hash in local part' => array('foo#bar@example.com'),
  395. // Fix / change if TYPO3 php requirement changed: Address ok with 5.2.6 and 5.3.2 but fails with 5.3.0 on windows
  396. // 'dot in local part' => array('firstname.lastname@employee.2something.com'),
  397. // Fix / change if TYPO3 php requirement changed: Address ok with 5.2.6, but not ok with 5.3.2
  398. // 'dash as local part' => array('-@foo.com'),
  399. );
  400. }
  401. /**
  402. * @test
  403. * @dataProvider validEmailValidDataProvider
  404. */
  405. public function validEmailReturnsTrueForValidMailAddress($address) {
  406. $this->assertTrue(t3lib_div::validEmail($address));
  407. }
  408. /**
  409. * Data provider for invalid validEmail's
  410. *
  411. * @return array Invalid email addresses
  412. */
  413. public function validEmailInvalidDataProvider() {
  414. return array(
  415. '@ sign only' => array('@'),
  416. 'duplicate @' => array('test@@example.com'),
  417. 'duplicate @ combined with further special characters in local part' => array('test!.!@#$%^&*@example.com'),
  418. 'opening parenthesis in local part' => array('foo(bar@example.com'),
  419. 'closing parenthesis in local part' => array('foo)bar@example.com'),
  420. 'opening square bracket in local part' => array('foo[bar@example.com'),
  421. 'closing square bracket as local part' => array(']@example.com'),
  422. // Fix / change if TYPO3 php requirement changed: Address ok with 5.2.6, but not ok with 5.3.2
  423. // 'top level domain only' => array('test@com'),
  424. 'dash as second level domain' => array('foo@-.com'),
  425. 'domain part starting with dash' => array('foo@-foo.com'),
  426. 'domain part ending with dash' => array('foo@foo-.com'),
  427. 'number as top level domain' => array('foo@bar.123'),
  428. // Fix / change if TYPO3 php requirement changed: Address not ok with 5.2.6, but ok with 5.3.2 (?)
  429. // 'dash as top level domain' => array('foo@bar.-'),
  430. 'dot at beginning of domain part' => array('test@.com'),
  431. // Fix / change if TYPO3 php requirement changed: Address ok with 5.2.6, but not ok with 5.3.2
  432. // 'local part ends with dot' => array('e.x.a.m.p.l.e.@example.com'),
  433. 'trailing whitespace' => array('test@example.com '),
  434. 'trailing carriage return' => array('test@example.com' . CR),
  435. 'trailing linefeed' => array('test@example.com' . LF),
  436. 'trailing carriage return linefeed' => array('test@example.com' . CRLF),
  437. 'trailing tab' => array('test@example.com' . TAB),
  438. );
  439. }
  440. /**
  441. * @test
  442. * @dataProvider validEmailInvalidDataProvider
  443. */
  444. public function validEmailReturnsFalseForInvalidMailAddress($address) {
  445. $this->assertFalse(t3lib_div::validEmail($address));
  446. }
  447. //////////////////////////////////
  448. // Tests concerning intExplode
  449. //////////////////////////////////
  450. /**
  451. * @test
  452. */
  453. public function intExplodeConvertsStringsToInteger() {
  454. $testString = '1,foo,2';
  455. $expectedArray = array(1, 0, 2);
  456. $actualArray = t3lib_div::intExplode(',', $testString);
  457. $this->assertEquals($expectedArray, $actualArray);
  458. }
  459. //////////////////////////////////
  460. // Tests concerning revExplode
  461. //////////////////////////////////
  462. /**
  463. * @test
  464. */
  465. public function revExplodeExplodesString() {
  466. $testString = 'my:words:here';
  467. $expectedArray = array('my:words', 'here');
  468. $actualArray = t3lib_div::revExplode(':', $testString, 2);
  469. $this->assertEquals($expectedArray, $actualArray);
  470. }
  471. //////////////////////////////////
  472. // Tests concerning trimExplode
  473. //////////////////////////////////
  474. /**
  475. * @test
  476. */
  477. public function checkTrimExplodeTrimsSpacesAtElementStartAndEnd() {
  478. $testString = ' a , b , c ,d ,, e,f,';
  479. $expectedArray = array('a', 'b', 'c', 'd', '', 'e', 'f', '');
  480. $actualArray = t3lib_div::trimExplode(',', $testString);
  481. $this->assertEquals($expectedArray, $actualArray);
  482. }
  483. /**
  484. * @test
  485. */
  486. public function checkTrimExplodeRemovesNewLines() {
  487. $testString = ' a , b , ' . LF . ' ,d ,, e,f,';
  488. $expectedArray = array('a', 'b', 'd', 'e', 'f');
  489. $actualArray = t3lib_div::trimExplode(',', $testString, TRUE);
  490. $this->assertEquals($expectedArray, $actualArray);
  491. }
  492. /**
  493. * @test
  494. */
  495. public function checkTrimExplodeRemovesEmptyElements() {
  496. $testString = 'a , b , c , ,d ,, ,e,f,';
  497. $expectedArray = array('a', 'b', 'c', 'd', 'e', 'f');
  498. $actualArray = t3lib_div::trimExplode(',', $testString, TRUE);
  499. $this->assertEquals($expectedArray, $actualArray);
  500. }
  501. /**
  502. * @test
  503. */
  504. public function checkTrimExplodeKeepsRemainingResultsWithEmptyItemsAfterReachingLimitWithPositiveParameter() {
  505. $testString = ' a , b , c , , d,, ,e ';
  506. $expectedArray = array('a', 'b', 'c,,d,,,e');
  507. // Limiting returns the rest of the string as the last element
  508. $actualArray = t3lib_div::trimExplode(',', $testString, FALSE, 3);
  509. $this->assertEquals($expectedArray, $actualArray);
  510. }
  511. /**
  512. * @test
  513. */
  514. public function checkTrimExplodeKeepsRemainingResultsWithoutEmptyItemsAfterReachingLimitWithPositiveParameter() {
  515. $testString = ' a , b , c , , d,, ,e ';
  516. $expectedArray = array('a', 'b', 'c,d,e');
  517. // Limiting returns the rest of the string as the last element
  518. $actualArray = t3lib_div::trimExplode(',', $testString, TRUE, 3);
  519. $this->assertEquals($expectedArray, $actualArray);
  520. }
  521. /**
  522. * @test
  523. */
  524. public function checkTrimExplodeKeepsRamainingResultsWithEmptyItemsAfterReachingLimitWithNegativeParameter() {
  525. $testString = ' a , b , c , d, ,e, f , , ';
  526. $expectedArray = array('a', 'b', 'c', 'd', '', 'e');
  527. // limiting returns the rest of the string as the last element
  528. $actualArray = t3lib_div::trimExplode(',', $testString, FALSE, -3);
  529. $this->assertEquals($expectedArray, $actualArray);
  530. }
  531. /**
  532. * @test
  533. */
  534. public function checkTrimExplodeKeepsRamainingResultsWithoutEmptyItemsAfterReachingLimitWithNegativeParameter() {
  535. $testString = ' a , b , c , d, ,e, f , , ';
  536. $expectedArray = array('a', 'b', 'c');
  537. // Limiting returns the rest of the string as the last element
  538. $actualArray = t3lib_div::trimExplode(',', $testString, TRUE, -3);
  539. $this->assertEquals($expectedArray, $actualArray);
  540. }
  541. /**
  542. * @test
  543. */
  544. public function checkTrimExplodeReturnsExactResultsWithoutReachingLimitWithPositiveParameter() {
  545. $testString = ' a , b , , c , , , ';
  546. $expectedArray = array('a', 'b', 'c');
  547. // Limiting returns the rest of the string as the last element
  548. $actualArray = t3lib_div::trimExplode(',', $testString, TRUE, 4);
  549. $this->assertEquals($expectedArray, $actualArray);
  550. }
  551. /**
  552. * @test
  553. */
  554. public function checkTrimExplodeKeepsZeroAsString() {
  555. $testString = 'a , b , c , ,d ,, ,e,f, 0 ,';
  556. $expectedArray = array('a', 'b', 'c', 'd', 'e', 'f', '0');
  557. $actualArray = t3lib_div::trimExplode(',', $testString, TRUE);
  558. $this->assertEquals($expectedArray, $actualArray);
  559. }
  560. //////////////////////////////////
  561. // Tests concerning removeArrayEntryByValue
  562. //////////////////////////////////
  563. /**
  564. * @test
  565. */
  566. public function checkRemoveArrayEntryByValueRemovesEntriesFromOneDimensionalArray() {
  567. $inputArray = array(
  568. '0' => 'test1',
  569. '1' => 'test2',
  570. '2' => 'test3',
  571. '3' => 'test2',
  572. );
  573. $compareValue = 'test2';
  574. $expectedResult = array(
  575. '0' => 'test1',
  576. '2' => 'test3',
  577. );
  578. $actualResult = t3lib_div::removeArrayEntryByValue($inputArray, $compareValue);
  579. $this->assertEquals($expectedResult, $actualResult);
  580. }
  581. /**
  582. * @test
  583. */
  584. public function checkRemoveArrayEntryByValueRemovesEntriesFromMultiDimensionalArray() {
  585. $inputArray = array(
  586. '0' => 'foo',
  587. '1' => array(
  588. '10' => 'bar',
  589. ),
  590. '2' => 'bar',
  591. );
  592. $compareValue = 'bar';
  593. $expectedResult = array(
  594. '0' => 'foo',
  595. '1' => array(),
  596. );
  597. $actualResult = t3lib_div::removeArrayEntryByValue($inputArray, $compareValue);
  598. $this->assertEquals($expectedResult, $actualResult);
  599. }
  600. /**
  601. * @test
  602. */
  603. public function checkRemoveArrayEntryByValueRemovesEntryWithEmptyString() {
  604. $inputArray = array(
  605. '0' => 'foo',
  606. '1' => '',
  607. '2' => 'bar',
  608. );
  609. $compareValue = '';
  610. $expectedResult = array(
  611. '0' => 'foo',
  612. '2' => 'bar',
  613. );
  614. $actualResult = t3lib_div::removeArrayEntryByValue($inputArray, $compareValue);
  615. $this->assertEquals($expectedResult, $actualResult);
  616. }
  617. //////////////////////////////////
  618. // Tests concerning getBytesFromSizeMeasurement
  619. //////////////////////////////////
  620. /**
  621. * Data provider for getBytesFromSizeMeasurement
  622. *
  623. * @return array expected value, input string
  624. */
  625. public function getBytesFromSizeMeasurementDataProvider() {
  626. return array(
  627. '100 kilo Bytes' => array('102400', '100k'),
  628. '100 mega Bytes' => array('104857600', '100m'),
  629. '100 giga Bytes' => array('107374182400', '100g'),
  630. );
  631. }
  632. /**
  633. * @test
  634. * @dataProvider getBytesFromSizeMeasurementDataProvider
  635. */
  636. public function getBytesFromSizeMeasurementCalculatesCorrectByteValue($expected, $byteString) {
  637. $this->assertEquals($expected, t3lib_div::getBytesFromSizeMeasurement($byteString));
  638. }
  639. //////////////////////////////////
  640. // Tests concerning getIndpEnv
  641. //////////////////////////////////
  642. /**
  643. * @test
  644. */
  645. public function getIndpEnvTypo3SitePathReturnNonEmptyString() {
  646. $this->assertTrue(strlen(t3lib_div::getIndpEnv('TYPO3_SITE_PATH')) >= 1);
  647. }
  648. /**
  649. * @test
  650. */
  651. public function getIndpEnvTypo3SitePathReturnsStringStartingWithSlash() {
  652. $result = t3lib_div::getIndpEnv('TYPO3_SITE_PATH');
  653. $this->assertEquals('/', $result[0]);
  654. }
  655. /**
  656. * @test
  657. */
  658. public function getIndpEnvTypo3SitePathReturnsStringEndingWithSlash() {
  659. $result = t3lib_div::getIndpEnv('TYPO3_SITE_PATH');
  660. $this->assertEquals('/', $result[strlen($result) - 1]);
  661. }
  662. /**
  663. * @return array
  664. */
  665. public static function hostnameAndPortDataProvider() {
  666. return array(
  667. 'localhost ipv4 without port' => array('127.0.0.1', '127.0.0.1', ''),
  668. 'localhost ipv4 with port' => array('127.0.0.1:81', '127.0.0.1', '81'),
  669. 'localhost ipv6 without port' => array('[::1]', '[::1]', ''),
  670. 'localhost ipv6 with port' => array('[::1]:81', '[::1]', '81'),
  671. 'ipv6 without port' => array('[2001:DB8::1]', '[2001:DB8::1]', ''),
  672. 'ipv6 with port' => array('[2001:DB8::1]:81', '[2001:DB8::1]', '81'),
  673. 'hostname without port' => array('lolli.did.this', 'lolli.did.this', ''),
  674. 'hostname with port' => array('lolli.did.this:42', 'lolli.did.this', '42'),
  675. );
  676. }
  677. /**
  678. * @test
  679. * @dataProvider hostnameAndPortDataProvider
  680. */
  681. public function getIndpEnvTypo3HostOnlyParsesHostnamesAndIpAdresses($httpHost, $expectedIp) {
  682. $_SERVER['HTTP_HOST'] = $httpHost;
  683. $this->assertEquals($expectedIp, t3lib_div::getIndpEnv('TYPO3_HOST_ONLY'));
  684. }
  685. /**
  686. * @test
  687. * @dataProvider hostnameAndPortDataProvider
  688. */
  689. public function getIndpEnvTypo3PortParsesHostnamesAndIpAdresses($httpHost, $dummy, $expectedPort) {
  690. $_SERVER['HTTP_HOST'] = $httpHost;
  691. $this->assertEquals($expectedPort, t3lib_div::getIndpEnv('TYPO3_PORT'));
  692. }
  693. //////////////////////////////////
  694. // Tests concerning underscoredToUpperCamelCase
  695. //////////////////////////////////
  696. /**
  697. * Data provider for underscoredToUpperCamelCase
  698. *
  699. * @return array expected, input string
  700. */
  701. public function underscoredToUpperCamelCaseDataProvider() {
  702. return array(
  703. 'single word' => array('Blogexample', 'blogexample'),
  704. 'multiple words' => array('BlogExample', 'blog_example'),
  705. );
  706. }
  707. /**
  708. * @test
  709. * @dataProvider underscoredToUpperCamelCaseDataProvider
  710. */
  711. public function underscoredToUpperCamelCase($expected, $inputString) {
  712. $this->assertEquals($expected, t3lib_div::underscoredToUpperCamelCase($inputString));
  713. }
  714. //////////////////////////////////
  715. // Tests concerning underscoredToLowerCamelCase
  716. //////////////////////////////////
  717. /**
  718. * Data provider for underscoredToLowerCamelCase
  719. *
  720. * @return array expected, input string
  721. */
  722. public function underscoredToLowerCamelCaseDataProvider() {
  723. return array(
  724. 'single word' => array('minimalvalue', 'minimalvalue'),
  725. 'multiple words' => array('minimalValue', 'minimal_value'),
  726. );
  727. }
  728. /**
  729. * @test
  730. * @dataProvider underscoredToLowerCamelCaseDataProvider
  731. */
  732. public function underscoredToLowerCamelCase($expected, $inputString) {
  733. $this->assertEquals($expected, t3lib_div::underscoredToLowerCamelCase($inputString));
  734. }
  735. //////////////////////////////////
  736. // Tests concerning camelCaseToLowerCaseUnderscored
  737. //////////////////////////////////
  738. /**
  739. * Data provider for camelCaseToLowerCaseUnderscored
  740. *
  741. * @return array expected, input string
  742. */
  743. public function camelCaseToLowerCaseUnderscoredDataProvider() {
  744. return array(
  745. 'single word' => array('blogexample', 'blogexample'),
  746. 'single word starting upper case' => array('blogexample', 'Blogexample'),
  747. 'two words starting lower case' => array('minimal_value', 'minimalValue'),
  748. 'two words starting upper case' => array('blog_example', 'BlogExample'),
  749. );
  750. }
  751. /**
  752. * @test
  753. * @dataProvider camelCaseToLowerCaseUnderscoredDataProvider
  754. */
  755. public function camelCaseToLowerCaseUnderscored($expected, $inputString) {
  756. $this->assertEquals($expected, t3lib_div::camelCaseToLowerCaseUnderscored($inputString));
  757. }
  758. //////////////////////////////////
  759. // Tests concerning lcFirst
  760. //////////////////////////////////
  761. /**
  762. * Data provider for lcFirst
  763. *
  764. * @return array expected, input string
  765. */
  766. public function lcfirstDataProvider() {
  767. return array(
  768. 'single word' => array('blogexample', 'blogexample'),
  769. 'single Word starting upper case' => array('blogexample', 'Blogexample'),
  770. 'two words' => array('blogExample', 'BlogExample'),
  771. );
  772. }
  773. /**
  774. * @test
  775. * @dataProvider lcfirstDataProvider
  776. */
  777. public function lcFirst($expected, $inputString) {
  778. $this->assertEquals($expected, t3lib_div::lcfirst($inputString));
  779. }
  780. //////////////////////////////////
  781. // Tests concerning encodeHeader
  782. //////////////////////////////////
  783. /**
  784. * @test
  785. */
  786. public function encodeHeaderEncodesWhitespacesInQuotedPrintableMailHeader() {
  787. $this->assertEquals(
  788. '=?utf-8?Q?We_test_whether_the_copyright_character_=C2=A9_is_encoded_correctly?=',
  789. t3lib_div::encodeHeader(
  790. "We test whether the copyright character \xc2\xa9 is encoded correctly",
  791. 'quoted-printable',
  792. 'utf-8'
  793. )
  794. );
  795. }
  796. /**
  797. * @test
  798. */
  799. public function encodeHeaderEncodesQuestionmarksInQuotedPrintableMailHeader() {
  800. $this->assertEquals(
  801. '=?utf-8?Q?Is_the_copyright_character_=C2=A9_really_encoded_correctly=3F_Really=3F?=',
  802. t3lib_div::encodeHeader(
  803. "Is the copyright character \xc2\xa9 really encoded correctly? Really?",
  804. 'quoted-printable',
  805. 'utf-8'
  806. )
  807. );
  808. }
  809. //////////////////////////////////
  810. // Tests concerning isValidUrl
  811. //////////////////////////////////
  812. /**
  813. * Data provider for valid isValidUrl's
  814. *
  815. * @return array Valid ressource
  816. */
  817. public function validUrlValidRessourceDataProvider() {
  818. return array(
  819. 'http' => array('http://www.example.org/'),
  820. 'http without trailing slash' => array('http://qwe'),
  821. 'http directory with trailing slash' => array('http://www.example/img/dir/'),
  822. 'http directory without trailing slash' => array('http://www.example/img/dir'),
  823. 'http index.html' => array('http://example.com/index.html'),
  824. 'http index.php' => array('http://www.example.com/index.php'),
  825. 'http test.png' => array('http://www.example/img/test.png'),
  826. 'http username password querystring and ancher' => array('https://user:pw@www.example.org:80/path?arg=value#fragment'),
  827. 'file' => array('file:///tmp/test.c'),
  828. 'file directory' => array('file://foo/bar'),
  829. 'ftp directory' => array('ftp://ftp.example.com/tmp/'),
  830. 'mailto' => array('mailto:foo@bar.com'),
  831. 'news' => array('news:news.php.net'),
  832. 'telnet'=> array('telnet://192.0.2.16:80/'),
  833. 'ldap' => array('ldap://[2001:db8::7]/c=GB?objectClass?one'),
  834. );
  835. }
  836. /**
  837. * @test
  838. * @dataProvider validUrlValidRessourceDataProvider
  839. */
  840. public function validURLReturnsTrueForValidRessource($url) {
  841. $this->assertTrue(t3lib_div::isValidUrl($url));
  842. }
  843. /**
  844. * Data provider for invalid isValidUrl's
  845. *
  846. * @return array Invalid ressource
  847. */
  848. public function isValidUrlInvalidRessourceDataProvider() {
  849. return array(
  850. 'http missing colon' => array('http//www.example/wrong/url/'),
  851. 'http missing slash' => array('http:/www.example'),
  852. 'hostname only' => array('www.example.org/'),
  853. 'file missing protocol specification' => array('/tmp/test.c'),
  854. 'slash only' => array('/'),
  855. 'string http://' => array('http://'),
  856. 'string http:/' => array('http:/'),
  857. 'string http:' => array('http:'),
  858. 'string http' => array('http'),
  859. 'empty string' => array(''),
  860. 'string -1' => array('-1'),
  861. 'string array()' => array('array()'),
  862. 'random string' => array('qwe'),
  863. );
  864. }
  865. /**
  866. * @test
  867. * @dataProvider isValidUrlInvalidRessourceDataProvider
  868. */
  869. public function validURLReturnsFalseForInvalidRessoure($url) {
  870. $this->assertFalse(t3lib_div::isValidUrl($url));
  871. }
  872. //////////////////////////////////
  873. // Tests concerning isOnCurrentHost
  874. //////////////////////////////////
  875. /**
  876. * @test
  877. */
  878. public function isOnCurrentHostReturnsTrueWithCurrentHost() {
  879. $testUrl = t3lib_div::getIndpEnv('TYPO3_REQUEST_URL');
  880. $this->assertTrue(t3lib_div::isOnCurrentHost($testUrl));
  881. }
  882. /**
  883. * Data provider for invalid isOnCurrentHost's
  884. *
  885. * @return array Invalid Hosts
  886. */
  887. public function checkisOnCurrentHostInvalidHosts() {
  888. return array(
  889. 'empty string' => array(''),
  890. 'arbitrary string' => array('arbitrary string'),
  891. 'localhost IP' => array('127.0.0.1'),
  892. 'relative path' => array('./relpath/file.txt'),
  893. 'absolute path' => array('/abspath/file.txt?arg=value'),
  894. 'differnt host' => array(t3lib_div::getIndpEnv('TYPO3_REQUEST_HOST') . '.example.org'),
  895. );
  896. }
  897. ////////////////////////////////////////
  898. // Tests concerning sanitizeLocalUrl
  899. ////////////////////////////////////////
  900. /**
  901. * Data provider for valid sanitizeLocalUrl's
  902. *
  903. * @return array Valid url
  904. */
  905. public function sanitizeLocalUrlValidUrlDataProvider() {
  906. $subDirectory = t3lib_div::getIndpEnv('TYPO3_SITE_PATH');
  907. $typo3SiteUrl = t3lib_div::getIndpEnv('TYPO3_SITE_URL');
  908. $typo3RequestHost = t3lib_div::getIndpEnv('TYPO3_REQUEST_HOST');
  909. return array(
  910. 'alt_intro.php' => array('alt_intro.php'),
  911. 'alt_intro.php?foo=1&bar=2' => array('alt_intro.php?foo=1&bar=2'),
  912. $subDirectory . 'typo3/alt_intro.php' => array($subDirectory . 'typo3/alt_intro.php'),
  913. $subDirectory . 'index.php' => array($subDirectory . 'index.php'),
  914. '../index.php' => array('../index.php'),
  915. '../typo3/alt_intro.php' => array('../typo3/alt_intro.php'),
  916. '../~userDirectory/index.php' => array('../~userDirectory/index.php'),
  917. '../typo3/mod.php?var1=test-case&var2=~user' => array('../typo3/mod.php?var1=test-case&var2=~user'),
  918. PATH_site . 'typo3/alt_intro.php' => array(PATH_site . 'typo3/alt_intro.php'),
  919. $typo3SiteUrl . 'typo3/alt_intro.php' => array($typo3SiteUrl . 'typo3/alt_intro.php'),
  920. $typo3RequestHost . $subDirectory . '/index.php' => array($typo3RequestHost . $subDirectory . '/index.php'),
  921. );
  922. }
  923. /**
  924. * @test
  925. * @dataProvider sanitizeLocalUrlValidUrlDataProvider
  926. */
  927. public function sanitizeLocalUrlAcceptsNotEncodedValidUrls($url) {
  928. $this->assertEquals($url, t3lib_div::sanitizeLocalUrl($url));
  929. }
  930. /**
  931. * @test
  932. * @dataProvider sanitizeLocalUrlValidUrlDataProvider
  933. */
  934. public function sanitizeLocalUrlAcceptsEncodedValidUrls($url) {
  935. $this->assertEquals(rawurlencode($url), t3lib_div::sanitizeLocalUrl(rawurlencode($url)));
  936. }
  937. /**
  938. * Data provider for invalid sanitizeLocalUrl's
  939. *
  940. * @return array Valid url
  941. */
  942. public function sanitizeLocalUrlInvalidDataProvider() {
  943. return array(
  944. 'empty string' => array(''),
  945. 'http domain' => array('http://www.google.de/'),
  946. 'https domain' => array('https://www.google.de/'),
  947. 'relative path with XSS' => array('../typo3/whatever.php?argument=javascript:alert(0)'),
  948. );
  949. }
  950. /**
  951. * @test
  952. * @dataProvider sanitizeLocalUrlInvalidDataProvider
  953. */
  954. public function sanitizeLocalUrlDeniesPlainInvalidUrls($url) {
  955. $this->assertEquals('', t3lib_div::sanitizeLocalUrl($url));
  956. }
  957. /**
  958. * @test
  959. * @dataProvider sanitizeLocalUrlInvalidDataProvider
  960. */
  961. public function sanitizeLocalUrlDeniesEncodedInvalidUrls($url) {
  962. $this->assertEquals('', t3lib_div::sanitizeLocalUrl(rawurlencode($url)));
  963. }
  964. //////////////////////////////////////
  965. // Tests concerning arrayDiffAssocRecursive
  966. //////////////////////////////////////
  967. /**
  968. * @test
  969. */
  970. public function arrayDiffAssocRecursiveHandlesOneDimensionalArrays() {
  971. $array1 = array(
  972. 'key1' => 'value1',
  973. 'key2' => 'value2',
  974. 'key3' => 'value3',
  975. );
  976. $array2 = array(
  977. 'key1' => 'value1',
  978. 'key3' => 'value3',
  979. );
  980. $expectedResult = array(
  981. 'key2' => 'value2',
  982. );
  983. $actualResult = t3lib_div::arrayDiffAssocRecursive($array1, $array2);
  984. $this->assertEquals($expectedResult, $actualResult);
  985. }
  986. /**
  987. * @test
  988. */
  989. public function arrayDiffAssocRecursiveHandlesMultiDimensionalArrays() {
  990. $array1 = array(
  991. 'key1' => 'value1',
  992. 'key2' => array(
  993. 'key21' => 'value21',
  994. 'key22' => 'value22',
  995. 'key23' => array(
  996. 'key231' => 'value231',
  997. 'key232' => 'value232',
  998. ),
  999. ),
  1000. );
  1001. $array2 = array(
  1002. 'key1' => 'value1',
  1003. 'key2' => array(
  1004. 'key21' => 'value21',
  1005. 'key23' => array(
  1006. 'key231' => 'value231',
  1007. ),
  1008. ),
  1009. );
  1010. $expectedResult = array(
  1011. 'key2' => array(
  1012. 'key22' => 'value22',
  1013. 'key23' => array(
  1014. 'key232' => 'value232',
  1015. ),
  1016. ),
  1017. );
  1018. $actualResult = t3lib_div::arrayDiffAssocRecursive($array1, $array2);
  1019. $this->assertEquals($expectedResult, $actualResult);
  1020. }
  1021. /**
  1022. * @test
  1023. */
  1024. public function arrayDiffAssocRecursiveHandlesMixedArrays() {
  1025. $array1 = array(
  1026. 'key1' => array(
  1027. 'key11' => 'value11',
  1028. 'key12' => 'value12',
  1029. ),
  1030. 'key2' => 'value2',
  1031. 'key3' => 'value3',
  1032. );
  1033. $array2 = array(
  1034. 'key1' => 'value1',
  1035. 'key2' => array(
  1036. 'key21' => 'value21',
  1037. ),
  1038. );
  1039. $expectedResult = array(
  1040. 'key3' => 'value3',
  1041. );
  1042. $actualResult = t3lib_div::arrayDiffAssocRecursive($array1, $array2);
  1043. $this->assertEquals($expectedResult, $actualResult);
  1044. }
  1045. //////////////////////////////////////
  1046. // Tests concerning removeDotsFromTS
  1047. //////////////////////////////////////
  1048. /**
  1049. * @test
  1050. */
  1051. public function removeDotsFromTypoScriptSucceedsWithDottedArray() {
  1052. $typoScript = array(
  1053. 'propertyA.' => array(
  1054. 'keyA.' => array(
  1055. 'valueA' => 1,
  1056. ),
  1057. 'keyB' => 2,
  1058. ),
  1059. 'propertyB' => 3,
  1060. );
  1061. $expectedResult = array(
  1062. 'propertyA' => array(
  1063. 'keyA' => array(
  1064. 'valueA' => 1,
  1065. ),
  1066. 'keyB' => 2,
  1067. ),
  1068. 'propertyB' => 3,
  1069. );
  1070. $this->assertEquals($expectedResult, t3lib_div::removeDotsFromTS($typoScript));
  1071. }
  1072. /**
  1073. * @test
  1074. */
  1075. public function removeDotsFromTypoScriptOverridesSubArray() {
  1076. $typoScript = array(
  1077. 'propertyA.' => array(
  1078. 'keyA' => 'getsOverridden',
  1079. 'keyA.' => array(
  1080. 'valueA' => 1,
  1081. ),
  1082. 'keyB' => 2,
  1083. ),
  1084. 'propertyB' => 3,
  1085. );
  1086. $expectedResult = array(
  1087. 'propertyA' => array(
  1088. 'keyA' => array(
  1089. 'valueA' => 1,
  1090. ),
  1091. 'keyB' => 2,
  1092. ),
  1093. 'propertyB' => 3,
  1094. );
  1095. $this->assertEquals($expectedResult, t3lib_div::removeDotsFromTS($typoScript));
  1096. }
  1097. /**
  1098. * @test
  1099. */
  1100. public function removeDotsFromTypoScriptOverridesWithScalar() {
  1101. $typoScript = array(
  1102. 'propertyA.' => array(
  1103. 'keyA.' => array(
  1104. 'valueA' => 1,
  1105. ),
  1106. 'keyA' => 'willOverride',
  1107. 'keyB' => 2,
  1108. ),
  1109. 'propertyB' => 3,
  1110. );
  1111. $expectedResult = array(
  1112. 'propertyA' => array(
  1113. 'keyA' => 'willOverride',
  1114. 'keyB' => 2,
  1115. ),
  1116. 'propertyB' => 3,
  1117. );
  1118. $this->assertEquals($expectedResult, t3lib_div::removeDotsFromTS($typoScript));
  1119. }
  1120. //////////////////////////////////////
  1121. // Tests concerning get_dirs
  1122. //////////////////////////////////////
  1123. /**
  1124. * @test
  1125. */
  1126. public function getDirsReturnsArrayOfDirectoriesFromGivenDirectory() {
  1127. $path = PATH_t3lib;
  1128. $directories = t3lib_div::get_dirs($path);
  1129. $this->assertType('array', $directories);
  1130. }
  1131. /**
  1132. * @test
  1133. */
  1134. public function getDirsReturnsStringErrorOnPathFailure() {
  1135. $path = 'foo';
  1136. $result = t3lib_div::get_dirs($path);
  1137. $expectedResult = 'error';
  1138. $this->assertEquals($expectedResult, $result);
  1139. }
  1140. //////////////////////////////////
  1141. // Tests concerning hmac
  1142. //////////////////////////////////
  1143. /**
  1144. * @test
  1145. */
  1146. public function hmacReturnsHashOfProperLength() {
  1147. $hmac = t3lib_div::hmac('message');
  1148. $this->assertTrue(!empty($hmac) && is_string($hmac));
  1149. $this->assertTrue(strlen($hmac) == 40);
  1150. }
  1151. /**
  1152. * @test
  1153. */
  1154. public function hmacReturnsEqualHashesForEqualInput() {
  1155. $msg0 = 'message';
  1156. $msg1 = 'message';
  1157. $this->assertEquals(t3lib_div::hmac($msg0), t3lib_div::hmac($msg1));
  1158. }
  1159. /**
  1160. * @test
  1161. */
  1162. public function hmacReturnsNoEqualHashesForNonEqualInput() {
  1163. $msg0 = 'message0';
  1164. $msg1 = 'message1';
  1165. $this->assertNotEquals(t3lib_div::hmac($msg0), t3lib_div::hmac($msg1));
  1166. }
  1167. //////////////////////////////////
  1168. // Tests concerning quoteJSvalue
  1169. //////////////////////////////////
  1170. /**
  1171. * @test
  1172. */
  1173. public function quoteJSvalueHtmlspecialcharsDataByDefault() {
  1174. $this->assertContains(
  1175. '&gt;',
  1176. t3lib_div::quoteJSvalue('>')
  1177. );
  1178. }
  1179. /**
  1180. * @test
  1181. */
  1182. public function quoteJSvaluetHtmlspecialcharsDataWithinCDataSetToFalse() {
  1183. $this->assertContains(
  1184. '&gt;',
  1185. t3lib_div::quoteJSvalue('>', false)
  1186. );
  1187. }
  1188. /**
  1189. * @test
  1190. */
  1191. public function quoteJSvaluetNotHtmlspecialcharsDataWithinCDataSetToTrue() {
  1192. $this->assertContains(
  1193. '>',
  1194. t3lib_div::quoteJSvalue('>', true)
  1195. );
  1196. }
  1197. /**
  1198. * @test
  1199. */
  1200. public function quoteJSvalueReturnsEmptyStringQuotedInSingleQuotes() {
  1201. $this->assertEquals(
  1202. "''",
  1203. t3lib_div::quoteJSvalue("", true)
  1204. );
  1205. }
  1206. /**
  1207. * @test
  1208. */
  1209. public function quoteJSvalueNotModifiesStringWithoutSpecialCharacters() {
  1210. $this->assertEquals(
  1211. "'Hello world!'",
  1212. t3lib_div::quoteJSvalue("Hello world!", true)
  1213. );
  1214. }
  1215. /**
  1216. * @test
  1217. */
  1218. public function quoteJSvalueEscapesSingleQuote() {
  1219. $this->assertEquals(
  1220. "'\\''",
  1221. t3lib_div::quoteJSvalue("'", true)
  1222. );
  1223. }
  1224. /**
  1225. * @test
  1226. */
  1227. public function quoteJSvalueEscapesDoubleQuoteWithinCDataSetToTrue() {
  1228. $this->assertEquals(
  1229. "'\\\"'",
  1230. t3lib_div::quoteJSvalue('"', true)
  1231. );
  1232. }
  1233. /**
  1234. * @test
  1235. */
  1236. public function quoteJSvalueEscapesAndHtmlspecialcharsDoubleQuoteWithinCDataSetToFalse() {
  1237. $this->assertEquals(
  1238. "'\\&quot;'",
  1239. t3lib_div::quoteJSvalue('"', false)
  1240. );
  1241. }
  1242. /**
  1243. * @test
  1244. */
  1245. public function quoteJSvalueEscapesTab() {
  1246. $this->assertEquals(
  1247. "'" . '\t' . "'",
  1248. t3lib_div::quoteJSvalue(TAB)
  1249. );
  1250. }
  1251. /**
  1252. * @test
  1253. */
  1254. public function quoteJSvalueEscapesLinefeed() {
  1255. $this->assertEquals(
  1256. "'" . '\n' . "'",
  1257. t3lib_div::quoteJSvalue(LF)
  1258. );
  1259. }
  1260. /**
  1261. * @test
  1262. */
  1263. public function quoteJSvalueEscapesCarriageReturn() {
  1264. $this->assertEquals(
  1265. "'" . '\r' . "'",
  1266. t3lib_div::quoteJSvalue(CR)
  1267. );
  1268. }
  1269. /**
  1270. * @test
  1271. */
  1272. public function quoteJSvalueEscapesBackslah() {
  1273. $this->assertEquals(
  1274. "'\\\\'",
  1275. t3lib_div::quoteJSvalue('\\')
  1276. );
  1277. }
  1278. //////////////////////////////////
  1279. // Tests concerning readLLfile
  1280. //////////////////////////////////
  1281. /**
  1282. * @test
  1283. */
  1284. public function readLLfileHandlesLocallangXMLOverride() {
  1285. $unique = uniqid('locallangXMLOverrideTest');
  1286. $xml = '<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
  1287. <T3locallang>
  1288. <data type="array">
  1289. <languageKey index="default" type="array">
  1290. <label index="buttons.logout">EXIT</label>
  1291. </languageKey>
  1292. </data>
  1293. </T3locallang>';
  1294. $file = PATH_site . 'typo3temp/' . $unique . '.xml';
  1295. t3lib_div::writeFileToTypo3tempDir($file, $xml);
  1296. // Get default value
  1297. $defaultLL = t3lib_div::readLLfile('EXT:lang/locallang_core.xml', 'default');
  1298. // Set override file
  1299. $GLOBALS['TYPO3_CONF_VARS']['SYS']['locallangXMLOverride']['EXT:lang/locallang_core.xml'][$unique] = $file;
  1300. // Get override value
  1301. $overrideLL = t3lib_div::readLLfile('EXT:lang/locallang_core.xml', 'default');
  1302. // Clean up again
  1303. unlink($file);
  1304. $this->assertNotEquals($overrideLL['default']['buttons.logout'], '');
  1305. $this->assertNotEquals($defaultLL['default']['buttons.logout'], $overrideLL['default']['buttons.logout']);
  1306. $this->assertEquals($overrideLL['default']['buttons.logout'], 'EXIT');
  1307. }
  1308. ///////////////////////////////
  1309. // Tests concerning _GETset()
  1310. ///////////////////////////////
  1311. /**
  1312. * @test
  1313. */
  1314. public function getSetWritesArrayToGetSystemVariable() {
  1315. $_GET = array();
  1316. $GLOBALS['HTTP_GET_VARS'] = array();
  1317. $getParameters = array('foo' => 'bar');
  1318. t3lib_div::_GETset($getParameters);
  1319. $this->assertSame($getParameters, $_GET);
  1320. }
  1321. /**
  1322. * @test
  1323. */
  1324. public function getSetWritesArrayToGlobalsHttpGetVars() {
  1325. $_GET = array();
  1326. $GLOBALS['HTTP_GET_VARS'] = array();
  1327. $getParameters = array('foo' => 'bar');
  1328. t3lib_div::_GETset($getParameters);
  1329. $this->assertSame($getParameters, $GLOBALS['HTTP_GET_VARS']);
  1330. }
  1331. /**
  1332. * @test
  1333. */
  1334. public function getSetForArrayDropsExistingValues() {
  1335. $_GET = array();
  1336. $GLOBALS['HTTP_GET_VARS'] = array();
  1337. t3lib_div::_GETset(array('foo' => 'bar'));
  1338. t3lib_div::_GETset(array('oneKey' => 'oneValue'));
  1339. $this->assertEquals(
  1340. array('oneKey' => 'oneValue'),
  1341. $GLOBALS['HTTP_GET_VARS']
  1342. );
  1343. }
  1344. /**
  1345. * @test
  1346. */
  1347. public function getSetAssignsOneValueToOneKey() {
  1348. $_GET = array();
  1349. $GLOBALS['HTTP_GET_VARS'] = array();
  1350. t3lib_div::_GETset('oneValue', 'oneKey');
  1351. $this->assertEquals(
  1352. 'oneValue',
  1353. $GLOBALS['HTTP_GET_VARS']['oneKey']
  1354. );
  1355. }
  1356. /**
  1357. * @test
  1358. */
  1359. public function getSetForOneValueDoesNotDropUnrelatedValues() {
  1360. $_GET = array();
  1361. $GLOBALS['HTTP_GET_VARS'] = array();
  1362. t3lib_div::_GETset(array('foo' => 'bar'));
  1363. t3lib_div::_GETset('oneValue', 'oneKey');
  1364. $this->assertEquals(
  1365. array('foo' => 'bar', 'oneKey' => 'oneValue'),
  1366. $GLOBALS['HTTP_GET_VARS']
  1367. );
  1368. }
  1369. /**
  1370. * @test
  1371. */
  1372. public function getSetCanAssignsAnArrayToASpecificArrayElement() {
  1373. $_GET = array();
  1374. $GLOBALS['HTTP_GET_VARS'] = array();
  1375. t3lib_div::_GETset(array('childKey' => 'oneValue'), 'parentKey');
  1376. $this->assertEquals(
  1377. array('parentKey' => array('childKey' => 'oneValue')),
  1378. $GLOBALS['HTTP_GET_VARS']
  1379. );
  1380. }
  1381. /**
  1382. * @test
  1383. */
  1384. public function getSetCanAssignAStringValueToASpecificArrayChildElement() {
  1385. $_GET = array();
  1386. $GLOBALS['HTTP_GET_VARS'] = array();
  1387. t3lib_div::_GETset('oneValue', 'parentKey|childKey');
  1388. $this->assertEquals(
  1389. array('parentKey' => array('childKey' => 'oneValue')),
  1390. $GLOBALS['HTTP_GET_VARS']
  1391. );
  1392. }
  1393. /**
  1394. * @test
  1395. */
  1396. public function getSetCanAssignAnArrayToASpecificArrayChildElement() {
  1397. $_GET = array();
  1398. $GLOBALS['HTTP_GET_VARS'] = array();
  1399. t3lib_div::_GETset(
  1400. array('key1' => 'value1', 'key2' => 'value2'),
  1401. 'parentKey|childKey'
  1402. );
  1403. $this->assertEquals(
  1404. array(
  1405. 'parentKey' => array(
  1406. 'childKey' => array('key1' => 'value1', 'key2' => 'value2')
  1407. )
  1408. ),
  1409. $GLOBALS['HTTP_GET_VARS']
  1410. );
  1411. }
  1412. ///////////////////////////////
  1413. // Tests concerning fixPermissions
  1414. ///////////////////////////////
  1415. /**
  1416. * @test
  1417. */
  1418. public function fixPermissionsCorrectlySetsGroup() {
  1419. if (TYPO3_OS == 'WIN') {
  1420. $this->markTestSkipped('fixPermissionsCorrectlySetsGroupOwnerOfFile() tests not available on Windows');
  1421. }
  1422. if (!function_exists('posix_getegid')) {
  1423. $this->markTestSkipped('Function posix_getegid() not available, fixPermissionsCorrectlySetsGroupOwnerOfFile() tests skipped');
  1424. }
  1425. // Create and prepare test file
  1426. $filename = PATH_site . 'typo3temp/' . uniqid('test_');
  1427. t3lib_div::writeFileToTypo3tempDir($filename, '42');
  1428. // Set target group and run method
  1429. $GLOBALS['TYPO3_CONF_VARS']['BE']['createGroup'] = posix_getegid();
  1430. $fixPermissionsResult = t3lib_div::fixPermissions($filename);
  1431. clearstatcache();
  1432. $resultFileGroup = filegroup($filename);
  1433. unlink($filename);
  1434. $this->assertEquals($resultFileGroup, posix_getegid());
  1435. }
  1436. /**
  1437. * @test
  1438. */
  1439. public function fixPermissionsCorrectlySetsPermissionsToFile() {
  1440. if (TYPO3_OS == 'WIN') {
  1441. $this->markTestSkipped('fixPermissions() tests not available on Windows');
  1442. }
  1443. // Create and prepare test file
  1444. $filename = PATH_site . 'typo3temp/' . uniqid('test_');
  1445. t3lib_div::writeFileToTypo3tempDir($filename, '42');
  1446. chmod($filename, 0742);
  1447. // Set target permissions and run method
  1448. $GLOBALS['TYPO3_CONF_VARS']['BE']['fileCreateMask'] = '0660';
  1449. $fixPermissionsResult = t3lib_div::fixPermissions($filename);
  1450. // Get actual permissions and clean up
  1451. clearstatcache();
  1452. $resultFilePermissions = substr(decoct(fileperms($filename)), 2);
  1453. unlink($filename);
  1454. // Test if everything was ok
  1455. $this->assertTrue($fixPermissionsResult);
  1456. $this->assertEquals($resultFilePermissions, '0660');
  1457. }
  1458. /**
  1459. * @test
  1460. */
  1461. public function fixPermissionsCorrectlySetsPermissionsToHiddenFile() {
  1462. if (TYPO3_OS == 'WIN') {
  1463. $this->markTestSkipped('fixPermissions() tests not available on Windows');
  1464. }
  1465. // Create and prepare test file
  1466. $filename = PATH_site . 'typo3temp/' . uniqid('.test_');
  1467. t3lib_div::writeFileToTypo3tempDir($filename, '42');
  1468. chmod($filename, 0742);
  1469. // Set target permissions and run method
  1470. $GLOBALS['TYPO3_CONF_VARS']['BE']['fileCreateMask'] = '0660';
  1471. $fixPermissionsResult = t3lib_div::fixPermissions($filename);
  1472. // Get actual permissions and clean up
  1473. clearstatcache();
  1474. $resultFilePermissions = substr(decoct(fileperms($filename)), 2);
  1475. unlink($filename);
  1476. // Test if everything was ok
  1477. $this->assertTrue($fixPermissionsResult);
  1478. $this->assertEquals($resultFilePermissions, '0660');
  1479. }
  1480. /**
  1481. * @test
  1482. */
  1483. public function fixPermissionsCorrectlySetsPermissionsToDirectory() {
  1484. if (TYPO3_OS == 'WIN') {
  1485. $this->markTestSkipped('fixPermissions() tests not available on Windows');
  1486. }
  1487. // Create and prepare test directory
  1488. $directory = PATH_site . 'typo3temp/' . uniqid('test_');
  1489. t3lib_div::mkdir($directory);
  1490. chmod($directory, 1551);
  1491. // Set target permissions and run method
  1492. $GLOBALS['TYPO3_CONF_VARS']['BE']['folderCreateMask'] = '0770';
  1493. $fixPermissionsResult = t3lib_div::fixPermissions($directory . '/');
  1494. // Get actual permissions and clean up
  1495. clearstatcache();
  1496. $resultDirectoryPermissions = substr(decoct(fileperms($directory)), 1);
  1497. t3lib_div::rmdir($directory);
  1498. // Test if everything was ok
  1499. $this->assertTrue($fixPermissionsResult);
  1500. $this->assertEquals($resultDirectoryPermissions, '0770');
  1501. }
  1502. /**
  1503. * @test
  1504. */
  1505. public function fixPermissionsCorrectlySetsPermissionsToHiddenDirectory() {
  1506. if (TYPO3_OS == 'WIN') {
  1507. $this->markTestSkipped('fixPermissions() tests not available on Windows');
  1508. }
  1509. // Create and prepare test directory
  1510. $directory = PATH_site . 'typo3temp/' . uniqid('.test_');
  1511. t3lib_div::mkdir($directory);
  1512. chmod($directory, 1551);
  1513. // Set target permissions and run method
  1514. $GLOBALS['TYPO3_CONF_VARS']['BE']['folderCreateMask'] = '0770';
  1515. $fixPermissionsResult = t3lib_div::fixPermissions($directory);
  1516. // Get actual permissions and clean up
  1517. clearstatcache();
  1518. $resultDirectoryPermissions = substr(decoct(fileperms($directory)), 1);
  1519. t3lib_div::rmdir($directory);
  1520. // Test if everything was o…

Large files files are truncated, but you can click here to view the full file