/mod/thewire/tests/regex.php

https://github.com/wangaiying/elgg4ysu · PHP · 297 lines · 168 code · 43 blank · 86 comment · 0 complexity · 6fd4d9cfce5fff3c0ac6932eb04d615d MD5 · raw file

  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. }
  114. /**
  115. * Hashtags: #tag
  116. */
  117. public function testReplaceHashtags() {
  118. // tag at beginning
  119. $text = "#tag test";
  120. $expected = $this->getHashtagLink('tag') . " test";
  121. $result = thewire_filter($text);
  122. $this->assertEqual($result, $expected);
  123. // tag not at beginning
  124. $text = "test #tag test";
  125. $expected = "test " . $this->getHashtagLink('tag') . " test";
  126. $result = thewire_filter($text);
  127. $this->assertEqual($result, $expected);
  128. // followed by comma
  129. $text = "test #tag, test";
  130. $expected = "test " . $this->getHashtagLink('tag') . ", test";
  131. $result = thewire_filter($text);
  132. $this->assertEqual($result, $expected);
  133. // preceded 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. // followed by period
  139. $text = "test #tag. test";
  140. $expected = "test " . $this->getHashtagLink('tag') . ". test";
  141. $result = thewire_filter($text);
  142. $this->assertEqual($result, $expected);
  143. // parentheses
  144. $text = "test (#tag) test";
  145. $expected = "test (" . $this->getHashtagLink('tag') . ") test";
  146. $result = thewire_filter($text);
  147. $this->assertEqual($result, $expected);
  148. // include number
  149. $text = "test #tag2000 test";
  150. $expected = "test " . $this->getHashtagLink('tag2000') . " test";
  151. $result = thewire_filter($text);
  152. $this->assertEqual($result, $expected);
  153. // cannot be just a number
  154. $text = "test #1 test";
  155. $expected = $text;
  156. $result = thewire_filter($text);
  157. $this->assertEqual($result, $expected);
  158. }
  159. /**
  160. * Email: johndoe@gmail.com
  161. */
  162. public function testReplaceEmailAddress() {
  163. // email at beginning of text
  164. $text = "test@test.com test";
  165. $expected = $this->getEmailLink('test@test.com') . " test";
  166. $result = thewire_filter($text);
  167. $this->assertEqual($result, $expected);
  168. // after space
  169. $text = "test test@test.com test";
  170. $expected = "test " . $this->getEmailLink('test@test.com') . " test";
  171. $result = thewire_filter($text);
  172. $this->assertEqual($result, $expected);
  173. // followed by comma
  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. // preceded 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. // followed by period
  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. // parentheses
  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. // includes digits
  194. $text = "user1@domain1.com";
  195. $expected = $this->getEmailLink('user1@domain1.com');
  196. $result = thewire_filter($text);
  197. $this->assertEqual($result, $expected);
  198. // includes underscore
  199. $text = "user_name@domain.com";
  200. $expected = $this->getEmailLink('user_name@domain.com');
  201. $result = thewire_filter($text);
  202. $this->assertEqual($result, $expected);
  203. // includes period
  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 subdomains
  209. $text = "user.name@domain.com.uk";
  210. $expected = $this->getEmailLink('user.name@domain.com.uk');
  211. $result = thewire_filter($text);
  212. $this->assertEqual($result, $expected);
  213. }
  214. /**
  215. * Links: http://www.example.org/
  216. */
  217. public function testReplaceLinks() {
  218. // beginning of text
  219. $text = "http://www.test.org";
  220. $expected = $this->getLink('http://www.test.org');
  221. $result = thewire_filter($text);
  222. $this->assertEqual($result, $expected);
  223. // not at beginning of text
  224. $text = "test http://www.test.org";
  225. $expected = 'test ' . $this->getLink('http://www.test.org');
  226. $result = thewire_filter($text);
  227. $this->assertEqual($result, $expected);
  228. // followed by comma
  229. $text = "test http://www.test.org, test";
  230. $expected = 'test ' . $this->getLink('http://www.test.org') . ', test';
  231. $result = thewire_filter($text);
  232. $this->assertEqual($result, $expected);
  233. // preceeded 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. // followed by period
  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. // surrounded by parentheses
  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. // no http://
  249. $text = "test www.test.org test";
  250. $expected = 'test ' . $this->getLink('www.test.org') . ' test';
  251. $result = thewire_filter($text);
  252. $this->assertEqual($result, $expected);
  253. }
  254. }