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