UtilsTest.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. /**
  3. * Utilities' tests
  4. */
  5. require_once 'application/Utils.php';
  6. require_once 'tests/utils/ReferenceSessionIdHashes.php';
  7. // Initialize reference data before PHPUnit starts a session
  8. ReferenceSessionIdHashes::genAllHashes();
  9. /**
  10. * Unitary tests for Shaarli utilities
  11. */
  12. class UtilsTest extends PHPUnit_Framework_TestCase
  13. {
  14. // Session ID hashes
  15. protected static $sidHashes = null;
  16. /**
  17. * Assign reference data
  18. */
  19. public static function setUpBeforeClass()
  20. {
  21. self::$sidHashes = ReferenceSessionIdHashes::getHashes();
  22. }
  23. /**
  24. * Represent a link by its hash
  25. */
  26. public function testSmallHash()
  27. {
  28. $this->assertEquals('CyAAJw', smallHash('http://test.io'));
  29. $this->assertEquals(6, strlen(smallHash('https://github.com')));
  30. }
  31. /**
  32. * Look for a substring at the beginning of a string
  33. */
  34. public function testStartsWithCaseInsensitive()
  35. {
  36. $this->assertTrue(startsWith('Lorem ipsum', 'lorem', false));
  37. $this->assertTrue(startsWith('Lorem ipsum', 'LoReM i', false));
  38. }
  39. /**
  40. * Look for a substring at the beginning of a string (case-sensitive)
  41. */
  42. public function testStartsWithCaseSensitive()
  43. {
  44. $this->assertTrue(startsWith('Lorem ipsum', 'Lorem', true));
  45. $this->assertFalse(startsWith('Lorem ipsum', 'lorem', true));
  46. $this->assertFalse(startsWith('Lorem ipsum', 'LoReM i', true));
  47. }
  48. /**
  49. * Look for a substring at the beginning of a string (Unicode)
  50. */
  51. public function testStartsWithSpecialChars()
  52. {
  53. $this->assertTrue(startsWith('å!ùµ', 'å!', false));
  54. $this->assertTrue(startsWith('µ$åù', 'µ$', true));
  55. }
  56. /**
  57. * Look for a substring at the end of a string
  58. */
  59. public function testEndsWithCaseInsensitive()
  60. {
  61. $this->assertTrue(endsWith('Lorem ipsum', 'ipsum', false));
  62. $this->assertTrue(endsWith('Lorem ipsum', 'm IpsUM', false));
  63. }
  64. /**
  65. * Look for a substring at the end of a string (case-sensitive)
  66. */
  67. public function testEndsWithCaseSensitive()
  68. {
  69. $this->assertTrue(endsWith('lorem Ipsum', 'Ipsum', true));
  70. $this->assertFalse(endsWith('lorem Ipsum', 'ipsum', true));
  71. $this->assertFalse(endsWith('lorem Ipsum', 'M IPsuM', true));
  72. }
  73. /**
  74. * Look for a substring at the end of a string (Unicode)
  75. */
  76. public function testEndsWithSpecialChars()
  77. {
  78. $this->assertTrue(endsWith('å!ùµ', 'ùµ', false));
  79. $this->assertTrue(endsWith('µ$åù', 'åù', true));
  80. }
  81. /**
  82. * Check valid date strings, according to a DateTime format
  83. */
  84. public function testCheckValidDateFormat()
  85. {
  86. $this->assertTrue(checkDateFormat('Ymd', '20150627'));
  87. $this->assertTrue(checkDateFormat('Y-m-d', '2015-06-27'));
  88. }
  89. /**
  90. * Check erroneous date strings, according to a DateTime format
  91. */
  92. public function testCheckInvalidDateFormat()
  93. {
  94. $this->assertFalse(checkDateFormat('Ymd', '2015'));
  95. $this->assertFalse(checkDateFormat('Y-m-d', '2015-06'));
  96. $this->assertFalse(checkDateFormat('Ymd', 'DeLorean'));
  97. }
  98. /**
  99. * Test generate location with valid data.
  100. */
  101. public function testGenerateLocation() {
  102. $ref = 'http://localhost/?test';
  103. $this->assertEquals($ref, generateLocation($ref, 'localhost'));
  104. $ref = 'http://localhost:8080/?test';
  105. $this->assertEquals($ref, generateLocation($ref, 'localhost:8080'));
  106. $ref = '?localreferer#hash';
  107. $this->assertEquals($ref, generateLocation($ref, 'localhost:8080'));
  108. }
  109. /**
  110. * Test generate location - anti loop.
  111. */
  112. public function testGenerateLocationLoop() {
  113. $ref = 'http://localhost/?test';
  114. $this->assertEquals('?', generateLocation($ref, 'localhost', array('test')));
  115. }
  116. /**
  117. * Test generate location - from other domain.
  118. */
  119. public function testGenerateLocationOut() {
  120. $ref = 'http://somewebsite.com/?test';
  121. $this->assertEquals('?', generateLocation($ref, 'localhost'));
  122. }
  123. /**
  124. * Test is_session_id_valid with a valid ID - TEST ALL THE HASHES!
  125. *
  126. * This tests extensively covers all hash algorithms / bit representations
  127. */
  128. public function testIsAnyHashSessionIdValid()
  129. {
  130. foreach (self::$sidHashes as $algo => $bpcs) {
  131. foreach ($bpcs as $bpc => $hash) {
  132. $this->assertTrue(is_session_id_valid($hash));
  133. }
  134. }
  135. }
  136. /**
  137. * Test is_session_id_valid with a valid ID - SHA-1 hashes
  138. */
  139. public function testIsSha1SessionIdValid()
  140. {
  141. $this->assertTrue(is_session_id_valid(sha1('shaarli')));
  142. }
  143. /**
  144. * Test is_session_id_valid with a valid ID - SHA-256 hashes
  145. */
  146. public function testIsSha256SessionIdValid()
  147. {
  148. $this->assertTrue(is_session_id_valid(hash('sha256', 'shaarli')));
  149. }
  150. /**
  151. * Test is_session_id_valid with a valid ID - SHA-512 hashes
  152. */
  153. public function testIsSha512SessionIdValid()
  154. {
  155. $this->assertTrue(is_session_id_valid(hash('sha512', 'shaarli')));
  156. }
  157. /**
  158. * Test is_session_id_valid with invalid IDs.
  159. */
  160. public function testIsSessionIdInvalid()
  161. {
  162. $this->assertFalse(is_session_id_valid(''));
  163. $this->assertFalse(is_session_id_valid(array()));
  164. $this->assertFalse(
  165. is_session_id_valid('c0ZqcWF3VFE2NmJBdm1HMVQ0ZHJ3UmZPbTFsNGhkNHI=')
  166. );
  167. }
  168. /**
  169. * Test text2clickable without a redirector being set.
  170. */
  171. public function testText2clickableWithoutRedirector()
  172. {
  173. $text = 'stuff http://hello.there/is=someone#here otherstuff';
  174. $expectedText = 'stuff <a href="http://hello.there/is=someone#here">http://hello.there/is=someone#here</a> otherstuff';
  175. $processedText = text2clickable($text, '');
  176. $this->assertEquals($expectedText, $processedText);
  177. }
  178. /**
  179. * Test text2clickable a redirector set.
  180. */
  181. public function testText2clickableWithRedirector()
  182. {
  183. $text = 'stuff http://hello.there/is=someone#here otherstuff';
  184. $redirector = 'http://redirector.to';
  185. $expectedText = 'stuff <a href="'.
  186. $redirector .
  187. urlencode('http://hello.there/is=someone#here') .
  188. '">http://hello.there/is=someone#here</a> otherstuff';
  189. $processedText = text2clickable($text, $redirector);
  190. $this->assertEquals($expectedText, $processedText);
  191. }
  192. /**
  193. * Test testSpace2nbsp.
  194. */
  195. public function testSpace2nbsp()
  196. {
  197. $text = ' Are you thrilled by flags ?'. PHP_EOL .' Really?';
  198. $expectedText = '&nbsp; Are you &nbsp; thrilled &nbsp;by flags &nbsp; ?'. PHP_EOL .'&nbsp;Really?';
  199. $processedText = space2nbsp($text);
  200. $this->assertEquals($expectedText, $processedText);
  201. }
  202. }