Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

325 lines
8.8 KiB

10 years ago
10 years ago
10 years ago
  1. <?php
  2. /**
  3. * This file contains the tests for get_tags and the tag handling in item.php
  4. *
  5. * @package test.util
  6. */
  7. /**
  8. * required, because it contains the get_tags() function
  9. */
  10. require_once 'include/text.php';
  11. /**
  12. * required, because it contains the tag handling
  13. */
  14. require_once 'mod/item.php';
  15. /**
  16. * A class which can be used as replacement for an app if
  17. * only get_baseurl is used.
  18. *
  19. * @author Alexander Kampmann
  20. * @package test.util
  21. */
  22. class MockApp {
  23. function get_baseurl() {
  24. return "baseurl";
  25. }
  26. };
  27. /**
  28. * the test should not rely on a database,
  29. * so this is a replacement for the database access method q.
  30. *
  31. * It simulates the user with uid 11 has one contact, named Mike Lastname.
  32. *
  33. * @param string $sql
  34. */
  35. function q($sql) {
  36. $result=array(array('id'=>15,
  37. 'attag'=>'', 'network'=>'dfrn',
  38. 'name'=>'Mike Lastname', 'alias'=>'Mike',
  39. 'nick'=>'Mike', 'url'=>"http://justatest.de"));
  40. $args=func_get_args();
  41. //last parameter is always (in this test) uid, so, it should be 11
  42. if($args[count($args)-1]!=11) {
  43. return;
  44. }
  45. if(3==count($args)) {
  46. //first call in handle_body, id only
  47. if($result[0]['id']==$args[1]) {
  48. return $result;
  49. }
  50. //second call in handle_body, name
  51. if($result[0]['name']===$args[1]) {
  52. return $result;
  53. }
  54. }
  55. //third call in handle_body, nick or attag
  56. if($result[0]['nick']===$args[2] || $result[0]['attag']===$args[1]) {
  57. return $result;
  58. }
  59. }
  60. /**
  61. * replacement for dbesc.
  62. * I don't want to test dbesc here, so
  63. * I just return the input. It won't be a problem, because
  64. * the test does not use a real database.
  65. *
  66. * DON'T USE HAT FUNCTION OUTSIDE A TEST!
  67. *
  68. * @param string $str
  69. * @return input
  70. */
  71. function dbesc($str) {
  72. return $str;
  73. }
  74. /**
  75. * TestCase for tag handling.
  76. *
  77. * @author alexander
  78. * @package test.util
  79. */
  80. class GetTagsTest extends PHPUnit_Framework_TestCase {
  81. /** the mock to use as app */
  82. private $a;
  83. /**
  84. * initialize the test. That's a phpUnit function,
  85. * don't change its name.
  86. */
  87. public function setUp() {
  88. $this->a=new MockApp();
  89. }
  90. /**
  91. * test with one Person tag
  92. */
  93. public function testGetTagsShortPerson() {
  94. $text="hi @Mike";
  95. $tags=get_tags($text);
  96. $inform='';
  97. $str_tags='';
  98. foreach($tags as $tag) {
  99. handle_tag($this->a, $text, $inform, $str_tags, 11, $tag);
  100. }
  101. //correct tags found?
  102. $this->assertEquals(1, count($tags));
  103. $this->assertTrue(in_array("@Mike", $tags));
  104. //correct output from handle_tag?
  105. $this->assertEquals("cid:15", $inform);
  106. $this->assertEquals("@[url=http://justatest.de]Mike Lastname[/url]", $str_tags);
  107. $this->assertEquals("hi @[url=http://justatest.de]Mike Lastname[/url]", $text);
  108. }
  109. /**
  110. * test with one Person tag.
  111. * There's a minor spelling mistake...
  112. */
  113. public function testGetTagsShortPersonSpelling() {
  114. $text="hi @Mike.because";
  115. $tags=get_tags($text);
  116. //correct tags found?
  117. $this->assertEquals(1, count($tags));
  118. $this->assertTrue(in_array("@Mike.because", $tags));
  119. $inform='';
  120. $str_tags='';
  121. handle_tag($this->a, $text, $inform, $str_tags, 11, $tags[0]);
  122. // (mike) - This is a tricky case.
  123. // we support mentions as in @mike@example.com - which contains a period.
  124. // This shouldn't match anything unless you have a contact named "Mike.because".
  125. // We may need another test for "@Mike. because" - which should return the contact
  126. // as we ignore trailing periods in tags.
  127. // $this->assertEquals("cid:15", $inform);
  128. // $this->assertEquals("@[url=http://justatest.de]Mike Lastname[/url]", $str_tags);
  129. // $this->assertEquals("hi @[url=http://justatest.de]Mike Lastname[/url].because", $text);
  130. $this->assertEquals("", $inform);
  131. $this->assertEquals("", $str_tags);
  132. }
  133. /**
  134. * test with two Person tags.
  135. * There's a minor spelling mistake...
  136. */
  137. public function testGetTagsPerson2Spelling() {
  138. $text="hi @Mike@campino@friendica.eu";
  139. $tags=get_tags($text);
  140. // This construct is not supported. Results are indeterminate
  141. // $this->assertEquals(2, count($tags));
  142. // $this->assertTrue(in_array("@Mike", $tags));
  143. // $this->assertTrue(in_array("@campino@friendica.eu", $tags));
  144. }
  145. /**
  146. * Test with one hash tag.
  147. */
  148. public function testGetTagsShortTag() {
  149. $text="This is a #test_case";
  150. $tags=get_tags($text);
  151. $this->assertEquals(1, count($tags));
  152. $this->assertTrue(in_array("#test_case", $tags));
  153. }
  154. /**
  155. * test with a person and a hash tag
  156. */
  157. public function testGetTagsShortTagAndPerson() {
  158. $text="hi @Mike This is a #test_case";
  159. $tags=get_tags($text);
  160. $this->assertEquals(3, count($tags));
  161. $this->assertTrue(in_array("@Mike", $tags));
  162. $this->assertTrue(in_array("@Mike This", $tags));
  163. $this->assertTrue(in_array("#test_case", $tags));
  164. $inform='';
  165. $str_tags='';
  166. foreach($tags as $tag) {
  167. handle_tag($this->a, $text, $inform, $str_tags, 11, $tag);
  168. }
  169. $this->assertEquals("cid:15", $inform);
  170. $this->assertEquals("@[url=http://justatest.de]Mike Lastname[/url],#[url=baseurl/search?tag=test%20case]test case[/url]", $str_tags);
  171. $this->assertEquals("hi @[url=http://justatest.de]Mike Lastname[/url] This is a #[url=baseurl/search?tag=test%20case]test case[/url]", $text);
  172. }
  173. /**
  174. * test with a person, a hash tag and some special chars.
  175. */
  176. public function testGetTagsShortTagAndPersonSpecialChars() {
  177. $text="hi @Mike, This is a #test_case.";
  178. $tags=get_tags($text);
  179. $this->assertEquals(2, count($tags));
  180. $this->assertTrue(in_array("@Mike", $tags));
  181. $this->assertTrue(in_array("#test_case", $tags));
  182. }
  183. /**
  184. * Test with a person tag and text behind it.
  185. */
  186. public function testGetTagsPersonOnly() {
  187. $text="@Test I saw the Theme Dev group was created.";
  188. $tags=get_tags($text);
  189. $this->assertEquals(2, count($tags));
  190. $this->assertTrue(in_array("@Test I", $tags));
  191. $this->assertTrue(in_array("@Test", $tags));
  192. }
  193. /**
  194. * this test demonstrates strange behaviour by intval.
  195. * It makes the next test fail.
  196. */
  197. public function testIntval() {
  198. $this->assertEquals(15, intval("15 it"));
  199. }
  200. /**
  201. * test a tag with an id in it
  202. */
  203. public function testIdTag() {
  204. $text="Test with @mike+15 id tag";
  205. $tags=get_tags($text);
  206. $this->assertEquals(2, count($tags));
  207. $this->assertTrue(in_array("@mike+15", $tags));
  208. //happens right now, but it shouldn't be necessary
  209. $this->assertTrue(in_array("@mike+15 id", $tags));
  210. $inform='';
  211. $str_tags='';
  212. foreach($tags as $tag) {
  213. handle_tag($this->a, $text, $inform, $str_tags, 11, $tag);
  214. }
  215. $this->assertEquals("Test with @[url=http://justatest.de]Mike Lastname[/url] id tag", $text);
  216. $this->assertEquals("@[url=http://justatest.de]Mike Lastname[/url]", $str_tags);
  217. // this test may produce two cid:15 entries - which is OK because duplicates are pruned before delivery
  218. $this->assertContains("cid:15",$inform);
  219. }
  220. /**
  221. * test with two persons and one special tag.
  222. */
  223. public function testGetTags2Persons1TagSpecialChars() {
  224. $text="hi @Mike, I'm just writing #test_cases, so"
  225. ." so @somebody@friendica.com may change #things.";
  226. $tags=get_tags($text);
  227. $this->assertEquals(5, count($tags));
  228. $this->assertTrue(in_array("@Mike", $tags));
  229. $this->assertTrue(in_array("#test_cases", $tags));
  230. $this->assertTrue(in_array("@somebody@friendica.com", $tags));
  231. $this->assertTrue(in_array("@somebody@friendica.com may", $tags));
  232. $this->assertTrue(in_array("#things", $tags));
  233. }
  234. /**
  235. * test with a long text.
  236. */
  237. public function testGetTags() {
  238. $text="hi @Mike, I'm just writing #test_cases, "
  239. ." so @somebody@friendica.com may change #things. Of course I "
  240. ."look for a lot of #pitfalls, like #tags at the end of a sentence "
  241. ."@comment. I hope noone forgets about @fullstops.because that might"
  242. ." break #things. @Mike@campino@friendica.eu is also #nice, isn't it? "
  243. ."Now, add a @first_last tag. ";
  244. $tags=get_tags($text);
  245. $this->assertTrue(in_array("@Mike", $tags));
  246. $this->assertTrue(in_array("#test_cases", $tags));
  247. $this->assertTrue(in_array("@somebody@friendica.com", $tags));
  248. $this->assertTrue(in_array("#things", $tags));
  249. $this->assertTrue(in_array("#pitfalls", $tags));
  250. $this->assertTrue(in_array("#tags", $tags));
  251. $this->assertTrue(in_array("@comment", $tags));
  252. $this->assertTrue(in_array("@fullstops.because", $tags));
  253. $this->assertTrue(in_array("#things", $tags));
  254. $this->assertTrue(in_array("@Mike", $tags));
  255. $this->assertTrue(in_array("#nice", $tags));
  256. $this->assertTrue(in_array("@first_last", $tags));
  257. //right now, none of the is matched (unsupported)
  258. // $this->assertFalse(in_array("@Mike@campino@friendica.eu", $tags));
  259. // $this->assertTrue(in_array("@campino@friendica.eu", $tags));
  260. // $this->assertTrue(in_array("@campino@friendica.eu is", $tags));
  261. }
  262. /**
  263. * test with an empty string
  264. */
  265. public function testGetTagsEmpty() {
  266. $tags=get_tags("");
  267. $this->assertEquals(0, count($tags));
  268. }
  269. }