LinkUtilsTest.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. }
  96. /**
  97. * Test text2clickable a redirector set.
  98. */
  99. public function testText2clickableWithRedirector()
  100. {
  101. $text = 'stuff http://hello.there/is=someone#here otherstuff';
  102. $redirector = 'http://redirector.to';
  103. $expectedText = 'stuff <a href="'.
  104. $redirector .
  105. urlencode('http://hello.there/is=someone#here') .
  106. '">http://hello.there/is=someone#here</a> otherstuff';
  107. $processedText = text2clickable($text, $redirector);
  108. $this->assertEquals($expectedText, $processedText);
  109. }
  110. /**
  111. * Test testSpace2nbsp.
  112. */
  113. public function testSpace2nbsp()
  114. {
  115. $text = ' Are you thrilled by flags ?'. PHP_EOL .' Really?';
  116. $expectedText = '&nbsp; Are you &nbsp; thrilled &nbsp;by flags &nbsp; ?'. PHP_EOL .'&nbsp;Really?';
  117. $processedText = space2nbsp($text);
  118. $this->assertEquals($expectedText, $processedText);
  119. }
  120. /**
  121. * Test hashtags auto-link.
  122. */
  123. public function testHashtagAutolink()
  124. {
  125. $index = 'http://domain.tld/';
  126. $rawDescription = '#hashtag\n
  127. # nothashtag\n
  128. test#nothashtag #hashtag \#nothashtag\n
  129. test #hashtag #hashtag test #hashtag.test\n
  130. #hashtag #hashtag-nothashtag #hashtag_hashtag\n
  131. What is #ашок anyway?\n
  132. カタカナ #カタカナ」カタカナ\n';
  133. $autolinkedDescription = hashtag_autolink($rawDescription, $index);
  134. $this->assertContains($this->getHashtagLink('hashtag', $index), $autolinkedDescription);
  135. $this->assertNotContains(' #hashtag', $autolinkedDescription);
  136. $this->assertNotContains('>#nothashtag', $autolinkedDescription);
  137. $this->assertContains($this->getHashtagLink('ашок', $index), $autolinkedDescription);
  138. $this->assertContains($this->getHashtagLink('カタカナ', $index), $autolinkedDescription);
  139. $this->assertContains($this->getHashtagLink('hashtag_hashtag', $index), $autolinkedDescription);
  140. $this->assertNotContains($this->getHashtagLink('hashtag-nothashtag', $index), $autolinkedDescription);
  141. }
  142. /**
  143. * Test hashtags auto-link without index URL.
  144. */
  145. public function testHashtagAutolinkNoIndex()
  146. {
  147. $rawDescription = 'blabla #hashtag x#nothashtag';
  148. $autolinkedDescription = hashtag_autolink($rawDescription);
  149. $this->assertContains($this->getHashtagLink('hashtag'), $autolinkedDescription);
  150. $this->assertNotContains(' #hashtag', $autolinkedDescription);
  151. $this->assertNotContains('>#nothashtag', $autolinkedDescription);
  152. }
  153. /**
  154. * Util function to build an hashtag link.
  155. *
  156. * @param string $hashtag Hashtag name.
  157. * @param string $index Index URL.
  158. *
  159. * @return string HTML hashtag link.
  160. */
  161. private function getHashtagLink($hashtag, $index = '')
  162. {
  163. $hashtagLink = '<a href="'. $index .'?addtag=$1" title="Hashtag $1">#$1</a>';
  164. return str_replace('$1', $hashtag, $hashtagLink);
  165. }
  166. }