LinkUtilsTest.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. require_once 'application/LinkUtils.php';
  3. /**
  4. * Class LinkUtilsTest.
  5. */
  6. class LinkUtilsTest extends PHPUnit_Framework_TestCase
  7. {
  8. /**
  9. * Test html_extract_title() when the title is found.
  10. */
  11. public function testHtmlExtractExistentTitle()
  12. {
  13. $title = 'Read me please.';
  14. $html = '<html><meta>stuff</meta><title>'. $title .'</title></html>';
  15. $this->assertEquals($title, html_extract_title($html));
  16. $html = '<html><title>'. $title .'</title>blabla<title>another</title></html>';
  17. $this->assertEquals($title, html_extract_title($html));
  18. }
  19. /**
  20. * Test html_extract_title() when the title is not found.
  21. */
  22. public function testHtmlExtractNonExistentTitle()
  23. {
  24. $html = '<html><meta>stuff</meta></html>';
  25. $this->assertFalse(html_extract_title($html));
  26. }
  27. /**
  28. * Test get_charset() with all priorities.
  29. */
  30. public function testGetCharset()
  31. {
  32. $headers = array('Content-Type' => 'text/html; charset=Headers');
  33. $html = '<html><meta>stuff</meta><meta charset="Html"/></html>';
  34. $default = 'default';
  35. $this->assertEquals('headers', get_charset($headers, $html, $default));
  36. $this->assertEquals('html', get_charset(array(), $html, $default));
  37. $this->assertEquals($default, get_charset(array(), '', $default));
  38. $this->assertEquals('utf-8', get_charset(array(), ''));
  39. }
  40. /**
  41. * Test headers_extract_charset() when the charset is found.
  42. */
  43. public function testHeadersExtractExistentCharset()
  44. {
  45. $charset = 'x-MacCroatian';
  46. $headers = array('Content-Type' => 'text/html; charset='. $charset);
  47. $this->assertEquals(strtolower($charset), headers_extract_charset($headers));
  48. }
  49. /**
  50. * Test headers_extract_charset() when the charset is not found.
  51. */
  52. public function testHeadersExtractNonExistentCharset()
  53. {
  54. $headers = array();
  55. $this->assertFalse(headers_extract_charset($headers));
  56. $headers = array('Content-Type' => 'text/html');
  57. $this->assertFalse(headers_extract_charset($headers));
  58. }
  59. /**
  60. * Test html_extract_charset() when the charset is found.
  61. */
  62. public function testHtmlExtractExistentCharset()
  63. {
  64. $charset = 'x-MacCroatian';
  65. $html = '<html><meta>stuff2</meta><meta charset="'. $charset .'"/></html>';
  66. $this->assertEquals(strtolower($charset), html_extract_charset($html));
  67. }
  68. /**
  69. * Test html_extract_charset() when the charset is not found.
  70. */
  71. public function testHtmlExtractNonExistentCharset()
  72. {
  73. $html = '<html><meta>stuff</meta></html>';
  74. $this->assertFalse(html_extract_charset($html));
  75. $html = '<html><meta>stuff</meta><meta charset=""/></html>';
  76. $this->assertFalse(html_extract_charset($html));
  77. }
  78. /**
  79. * Test count_private.
  80. */
  81. public function testCountPrivateLinks()
  82. {
  83. $refDB = new ReferenceLinkDB();
  84. $this->assertEquals($refDB->countPrivateLinks(), count_private($refDB->getLinks()));
  85. }
  86. /**
  87. * Test text2clickable without a redirector being set.
  88. */
  89. public function testText2clickableWithoutRedirector()
  90. {
  91. $text = 'stuff http://hello.there/is=someone#here otherstuff';
  92. $expectedText = 'stuff <a href="http://hello.there/is=someone#here">http://hello.there/is=someone#here</a> otherstuff';
  93. $processedText = text2clickable($text, '');
  94. $this->assertEquals($expectedText, $processedText);
  95. $text = 'stuff http://hello.there/is=someone#here(please) otherstuff';
  96. $expectedText = 'stuff <a href="http://hello.there/is=someone#here(please)">http://hello.there/is=someone#here(please)</a> otherstuff';
  97. $processedText = text2clickable($text, '');
  98. $this->assertEquals($expectedText, $processedText);
  99. $text = 'stuff http://hello.there/is=someone#here(please)&no otherstuff';
  100. $expectedText = 'stuff <a href="http://hello.there/is=someone#here(please)&no">http://hello.there/is=someone#here(please)&no</a> otherstuff';
  101. $processedText = text2clickable($text, '');
  102. $this->assertEquals($expectedText, $processedText);
  103. }
  104. /**
  105. * Test text2clickable a redirector set.
  106. */
  107. public function testText2clickableWithRedirector()
  108. {
  109. $text = 'stuff http://hello.there/is=someone#here otherstuff';
  110. $redirector = 'http://redirector.to';
  111. $expectedText = 'stuff <a href="'.
  112. $redirector .
  113. urlencode('http://hello.there/is=someone#here') .
  114. '">http://hello.there/is=someone#here</a> otherstuff';
  115. $processedText = text2clickable($text, $redirector);
  116. $this->assertEquals($expectedText, $processedText);
  117. }
  118. /**
  119. * Test testSpace2nbsp.
  120. */
  121. public function testSpace2nbsp()
  122. {
  123. $text = ' Are you thrilled by flags ?'. PHP_EOL .' Really?';
  124. $expectedText = '&nbsp; Are you &nbsp; thrilled &nbsp;by flags &nbsp; ?'. PHP_EOL .'&nbsp;Really?';
  125. $processedText = space2nbsp($text);
  126. $this->assertEquals($expectedText, $processedText);
  127. }
  128. /**
  129. * Test hashtags auto-link.
  130. */
  131. public function testHashtagAutolink()
  132. {
  133. $index = 'http://domain.tld/';
  134. $rawDescription = '#hashtag\n
  135. # nothashtag\n
  136. test#nothashtag #hashtag \#nothashtag\n
  137. test #hashtag #hashtag test #hashtag.test\n
  138. #hashtag #hashtag-nothashtag #hashtag_hashtag\n
  139. What is #ашок anyway?\n
  140. カタカナ #カタカナ」カタカナ\n';
  141. $autolinkedDescription = hashtag_autolink($rawDescription, $index);
  142. $this->assertContains($this->getHashtagLink('hashtag', $index), $autolinkedDescription);
  143. $this->assertNotContains(' #hashtag', $autolinkedDescription);
  144. $this->assertNotContains('>#nothashtag', $autolinkedDescription);
  145. $this->assertContains($this->getHashtagLink('ашок', $index), $autolinkedDescription);
  146. $this->assertContains($this->getHashtagLink('カタカナ', $index), $autolinkedDescription);
  147. $this->assertContains($this->getHashtagLink('hashtag_hashtag', $index), $autolinkedDescription);
  148. $this->assertNotContains($this->getHashtagLink('hashtag-nothashtag', $index), $autolinkedDescription);
  149. }
  150. /**
  151. * Test hashtags auto-link without index URL.
  152. */
  153. public function testHashtagAutolinkNoIndex()
  154. {
  155. $rawDescription = 'blabla #hashtag x#nothashtag';
  156. $autolinkedDescription = hashtag_autolink($rawDescription);
  157. $this->assertContains($this->getHashtagLink('hashtag'), $autolinkedDescription);
  158. $this->assertNotContains(' #hashtag', $autolinkedDescription);
  159. $this->assertNotContains('>#nothashtag', $autolinkedDescription);
  160. }
  161. /**
  162. * Util function to build an hashtag link.
  163. *
  164. * @param string $hashtag Hashtag name.
  165. * @param string $index Index URL.
  166. *
  167. * @return string HTML hashtag link.
  168. */
  169. private function getHashtagLink($hashtag, $index = '')
  170. {
  171. $hashtagLink = '<a href="'. $index .'?addtag=$1" title="Hashtag $1">#$1</a>';
  172. return str_replace('$1', $hashtag, $hashtagLink);
  173. }
  174. }