regex.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. <?php
  2. /**
  3. * Regular expression tests for the wire
  4. */
  5. class TheWireRegexTest extends ElggCoreUnitTest {
  6. /**
  7. * Called before each test object.
  8. */
  9. public function __construct() {
  10. $this->ia = elgg_set_ignore_access(TRUE);
  11. parent::__construct();
  12. // all __construct() code should come after here
  13. }
  14. /**
  15. * Called before each test method.
  16. */
  17. public function setUp() {
  18. }
  19. /**
  20. * Called after each test method.
  21. */
  22. public function tearDown() {
  23. // do not allow SimpleTest to interpret Elgg notices as exceptions
  24. $this->swallowErrors();
  25. }
  26. /**
  27. * Called after each test object.
  28. */
  29. public function __destruct() {
  30. elgg_set_ignore_access($this->ia);
  31. // all __destruct() code should go above here
  32. parent::__destruct();
  33. }
  34. /**
  35. * Get the link for a user's wire page
  36. *
  37. * @param string $username Username
  38. * @return string
  39. */
  40. protected function getUserWireLink($username) {
  41. $url = "thewire/owner/$username";
  42. $url = elgg_normalize_url($url);
  43. return "<a href=\"$url\">@$username</a>";
  44. }
  45. /**
  46. * Get the link for a hashtag page
  47. *
  48. * @param string $tag Tag string
  49. * @return string
  50. */
  51. protected function getHashtagLink($tag) {
  52. $url = "thewire/tag/$tag";
  53. $url = elgg_normalize_url($url);
  54. return "<a href=\"$url\">#$tag</a>";
  55. }
  56. /**
  57. * Get a link for an email address mailto
  58. *
  59. * @param string $address Email address
  60. * @return string
  61. */
  62. protected function getEmailLink($address) {
  63. return "<a href=\"mailto:$address\">$address</a>";
  64. }
  65. /**
  66. * Get the html for a link
  67. *
  68. * @param string $address URL
  69. * @return string
  70. */
  71. protected function getLink($address) {
  72. return parse_urls($address);
  73. }
  74. /**
  75. * Usernames: @user
  76. */
  77. public function testReplaceUsernames() {
  78. // beginning of text
  79. $text = "@user test";
  80. $expected = $this->getUserWireLink('user') . " test";
  81. $result = thewire_filter($text);
  82. $this->assertEqual($result, $expected);
  83. // after space
  84. $text = "test @user test";
  85. $expected = "test " . $this->getUserWireLink('user') . " test";
  86. $result = thewire_filter($text);
  87. $this->assertEqual($result, $expected);
  88. // followed by comma
  89. $text = "test @user, test";
  90. $expected = "test " . $this->getUserWireLink('user') . ", test";
  91. $result = thewire_filter($text);
  92. $this->assertEqual($result, $expected);
  93. // preceded by comma
  94. $text = "test ,@user test";
  95. $expected = "test ," . $this->getUserWireLink('user') . " test";
  96. $result = thewire_filter($text);
  97. $this->assertEqual($result, $expected);
  98. // include digit
  99. $text = "@3user test";
  100. $expected = $this->getUserWireLink('3user') . " test";
  101. $result = thewire_filter($text);
  102. $this->assertEqual($result, $expected);
  103. // include underscore
  104. $text = "@user_name test";
  105. $expected = $this->getUserWireLink('user_name') . " test";
  106. $result = thewire_filter($text);
  107. $this->assertEqual($result, $expected);
  108. // parentheses
  109. $text = "test (@user) test";
  110. $expected = "test (" . $this->getUserWireLink('user') . ") test";
  111. $result = thewire_filter($text);
  112. $this->assertEqual($result, $expected);
  113. // utf8 characters
  114. $text = "@tyúkanyó";
  115. $expected = $this->getUserWireLink('tyúkanyó');
  116. $result = thewire_filter($text);
  117. $this->assertEqual($result, $expected);
  118. }
  119. /**
  120. * Hashtags: #tag
  121. */
  122. public function testReplaceHashtags() {
  123. // tag at beginning
  124. $text = "#tag test";
  125. $expected = $this->getHashtagLink('tag') . " test";
  126. $result = thewire_filter($text);
  127. $this->assertEqual($result, $expected);
  128. // tag not at beginning
  129. $text = "test #tag test";
  130. $expected = "test " . $this->getHashtagLink('tag') . " test";
  131. $result = thewire_filter($text);
  132. $this->assertEqual($result, $expected);
  133. // followed by comma
  134. $text = "test #tag, test";
  135. $expected = "test " . $this->getHashtagLink('tag') . ", test";
  136. $result = thewire_filter($text);
  137. $this->assertEqual($result, $expected);
  138. // preceded by comma
  139. $text = "test,#tag test";
  140. $expected = "test," . $this->getHashtagLink('tag') . " test";
  141. $result = thewire_filter($text);
  142. $this->assertEqual($result, $expected);
  143. // followed by period
  144. $text = "test #tag. test";
  145. $expected = "test " . $this->getHashtagLink('tag') . ". test";
  146. $result = thewire_filter($text);
  147. $this->assertEqual($result, $expected);
  148. // parentheses
  149. $text = "test (#tag) test";
  150. $expected = "test (" . $this->getHashtagLink('tag') . ") test";
  151. $result = thewire_filter($text);
  152. $this->assertEqual($result, $expected);
  153. // include number
  154. $text = "test #tag2000 test";
  155. $expected = "test " . $this->getHashtagLink('tag2000') . " test";
  156. $result = thewire_filter($text);
  157. $this->assertEqual($result, $expected);
  158. // cannot be just a number
  159. $text = "test #1 test";
  160. $expected = $text;
  161. $result = thewire_filter($text);
  162. $this->assertEqual($result, $expected);
  163. }
  164. /**
  165. * Email: johndoe@gmail.com
  166. */
  167. public function testReplaceEmailAddress() {
  168. // email at beginning of text
  169. $text = "test@test.com test";
  170. $expected = $this->getEmailLink('test@test.com') . " test";
  171. $result = thewire_filter($text);
  172. $this->assertEqual($result, $expected);
  173. // after space
  174. $text = "test test@test.com test";
  175. $expected = "test " . $this->getEmailLink('test@test.com') . " test";
  176. $result = thewire_filter($text);
  177. $this->assertEqual($result, $expected);
  178. // followed by comma
  179. $text = "test test@test.com, test";
  180. $expected = "test " . $this->getEmailLink('test@test.com') . ", test";
  181. $result = thewire_filter($text);
  182. $this->assertEqual($result, $expected);
  183. // preceded by comma
  184. $text = "test,test@test.com test";
  185. $expected = "test," . $this->getEmailLink('test@test.com') . " test";
  186. $result = thewire_filter($text);
  187. $this->assertEqual($result, $expected);
  188. // followed by period
  189. $text = "test test@test.com. test";
  190. $expected = "test " . $this->getEmailLink('test@test.com') . ". test";
  191. $result = thewire_filter($text);
  192. $this->assertEqual($result, $expected);
  193. // parentheses
  194. $text = "test (test@test.com) test";
  195. $expected = "test (" . $this->getEmailLink('test@test.com') . ") test";
  196. $result = thewire_filter($text);
  197. $this->assertEqual($result, $expected);
  198. // includes digits
  199. $text = "user1@domain1.com";
  200. $expected = $this->getEmailLink('user1@domain1.com');
  201. $result = thewire_filter($text);
  202. $this->assertEqual($result, $expected);
  203. // includes underscore
  204. $text = "user_name@domain.com";
  205. $expected = $this->getEmailLink('user_name@domain.com');
  206. $result = thewire_filter($text);
  207. $this->assertEqual($result, $expected);
  208. // includes period
  209. $text = "user.name@domain.com";
  210. $expected = $this->getEmailLink('user.name@domain.com');
  211. $result = thewire_filter($text);
  212. $this->assertEqual($result, $expected);
  213. // includes subdomains
  214. $text = "user.name@domain.com.uk";
  215. $expected = $this->getEmailLink('user.name@domain.com.uk');
  216. $result = thewire_filter($text);
  217. $this->assertEqual($result, $expected);
  218. }
  219. /**
  220. * Links: http://www.example.org/
  221. */
  222. public function testReplaceLinks() {
  223. // beginning of text
  224. $text = "http://www.test.org";
  225. $expected = $this->getLink('http://www.test.org');
  226. $result = thewire_filter($text);
  227. $this->assertEqual($result, $expected);
  228. // not at beginning of text
  229. $text = "test http://www.test.org";
  230. $expected = 'test ' . $this->getLink('http://www.test.org');
  231. $result = thewire_filter($text);
  232. $this->assertEqual($result, $expected);
  233. // followed by comma
  234. $text = "test http://www.test.org, test";
  235. $expected = 'test ' . $this->getLink('http://www.test.org') . ', test';
  236. $result = thewire_filter($text);
  237. $this->assertEqual($result, $expected);
  238. // preceeded by comma
  239. $text = "test,http://www.test.org test";
  240. $expected = 'test,' . $this->getLink('http://www.test.org') . ' test';
  241. $result = thewire_filter($text);
  242. $this->assertEqual($result, $expected);
  243. // followed by period
  244. $text = "test http://www.test.org. test";
  245. $expected = 'test ' . $this->getLink('http://www.test.org') . '. test';
  246. $result = thewire_filter($text);
  247. $this->assertEqual($result, $expected);
  248. // surrounded by parentheses
  249. $text = "test (http://www.test.org) test";
  250. $expected = 'test (' . $this->getLink('http://www.test.org') . ') test';
  251. $result = thewire_filter($text);
  252. $this->assertEqual($result, $expected);
  253. // no http://
  254. $text = "test www.test.org test";
  255. $expected = 'test ' . $this->getLink('www.test.org') . ' test';
  256. $result = thewire_filter($text);
  257. $this->assertEqual($result, $expected);
  258. }
  259. }