]> git.mxchange.org Git - friendica.git/blob - tests/get_tags_test.php
get tags test improved
[friendica.git] / tests / get_tags_test.php
1 <?php
2 /**
3  * @package test.util
4  */
5
6 require_once 'include/template_processor.php';\r
7 require_once 'include/text.php';
8 require_once 'mod/item.php';
9
10 function q($sql) {
11         
12         $result=array(array('id'=>15, 
13                         'attag'=>'', 'network'=>'dfrn', 
14                         'name'=>'Mike Lastname', 'alias'=>'Mike', 
15                         'nick'=>'Mike', 'url'=>"http://justatest.de")); 
16         
17         $args=func_get_args(); 
18
19         $str="";
20         foreach($args as $arg) {
21                 $str.=", ".$arg; 
22         }
23         
24         //last parameter is always (in this test) uid, so, it should be 11
25         if($args[count($args)-1]!=11) {
26                 throw new Exception("q from get_tags_test was used and uid was not 11. "); 
27         }
28         
29         if(2==count($args)) {
30                 //first call in handle_body, id only
31                 if($result[0]['id']===$args[1]) {
32                         return $result; 
33                 }
34         throw new Exception($str); 
35                 //second call in handle_body, name
36                 if($result[0]['name']===$args[1]) {\r
37                         return $result;\r
38                 }
39         }
40         throw new Exception($str);
41         //third call in handle_body, nick or attag
42         if($result[0]['nick']===$args[2] || $result[0]['attag']===$args[1]) {\r
43                 return $result;\r
44         }
45 //      throw new Exception("Nothing fitted: ".$args[1].", ".$args[2]); 
46 }
47
48 function dbesc($str) {
49         return $str; 
50 }
51
52 class GetTagsTest extends PHPUnit_Framework_TestCase {\r
53 \r
54         public function setUp() {\r
55                 set_include_path(\r
56                                 get_include_path() . PATH_SEPARATOR\r
57                                 . 'include' . PATH_SEPARATOR\r
58                                 . 'library' . PATH_SEPARATOR\r
59                                 . 'library/phpsec' . PATH_SEPARATOR\r
60                                 . '.' );\r
61         }
62
63         /**\r
64          * test with one Person tag\r
65          */\r
66         public function testGetTagsShortPerson() {\r
67                 $text="hi @Mike";\r
68 \r
69                 $tags=get_tags($text);\r
70
71                 $inform=''; 
72                 $str_tags='';
73                 handle_body($text, $inform, $str_tags, 11, $tags[0]);
74 \r
75                 $this->assertEquals("@Mike", $tags[0]);
76                 $this->assertEquals("hi @[url=http://justatest.de]Mike Lastname[/url]", $text);\r
77         }\r
78
79         /**
80          * Test with one hash tag.
81          */\r
82         public function testGetTagsShortTag() {\r
83                 $text="This is a #test_case";\r
84 \r
85                 $tags=get_tags($text);\r
86 \r
87                 $this->assertEquals("#test_case", $tags[0]);\r
88         }\r
89
90         /**
91          * test with a person and a hash tag
92          */\r
93         public function testGetTagsShortTagAndPerson() {\r
94                 $text="hi @Mike This is a #test_case";\r
95 \r
96                 $tags=get_tags($text);\r
97
98                 $inform='';\r
99                 $str_tags='';\r
100                 handle_body($text, $inform, $str_tags, 11, $tags[0]);
101                 
102                 $this->assertEquals("hi @[url=http://justatest.de]Mike[/url] This is a #test_case", $text); \r
103                 $this->assertEquals("@Mike", $tags[0]);\r
104                 $this->assertEquals("#test_case", $tags[1]);\r
105         }\r
106
107         /**
108          * test with a person, a hash tag and some special chars.
109          */\r
110         public function testGetTagsShortTagAndPersonSpecialChars() {\r
111                 $text="hi @Mike, This is a #test_case.";\r
112 \r
113                 $tags=get_tags($text);\r
114 \r
115                 $this->assertEquals("@Mike", $tags[0]);\r
116                 $this->assertEquals("#test_case", $tags[1]);\r
117         }\r
118
119         /**
120          * Test with a person tag and text behind it.
121          */\r
122         public function testGetTagsPersonOnly() {\r
123                 $text="@Test I saw the Theme Dev group was created.";\r
124 \r
125                 $tags=get_tags($text);\r
126 \r
127                 $this->assertEquals("@Test", $tags[0]);\r
128         }\r
129
130         /**
131          * test with two persons and one special tag.
132          */\r
133         public function testGetTags2Persons1TagSpecialChars() {\r
134                 $text="hi @Mike, I'm just writing #test_cases, so"\r
135                 ." so @somebody@friendica.com may change #things.";\r
136 \r
137                 $tags=get_tags($text);\r
138 \r
139                 $this->assertEquals("@Mike", $tags[0]);\r
140                 $this->assertEquals("#test_cases", $tags[1]);\r
141                 $this->assertEquals("@somebody@friendica.com", $tags[2]);\r
142                 $this->assertEquals("#things", $tags[3]);\r
143         }\r
144
145         /**
146          * test with a long text.
147          */\r
148         public function testGetTags() {\r
149                 $text="hi @Mike, I'm just writing #test_cases, "\r
150                 ." so @somebody@friendica.com may change #things. Of course I "\r
151                 ."look for a lot of #pitfalls, like #tags at the end of a sentence "\r
152                 ."@comment. I hope noone forgets about @fullstops.because that might"\r
153                 ." break #things. @Mike@campino@friendica.eu is also #nice, isn't it? "\r
154                 ."Now, add a @first_last tag. ";\r
155                 //TODO check whether this are all variants (no, auto-stuff is missing).\r
156 \r
157                 $tags=get_tags($text);\r
158 \r
159                 $this->assertEquals("@Mike", $tags[0]);\r
160                 $this->assertEquals("#test_cases", $tags[1]);\r
161                 $this->assertEquals("@somebody@friendica.com", $tags[2]);\r
162                 $this->assertEquals("#things", $tags[3]);\r
163                 $this->assertEquals("#pitfalls", $tags[4]);\r
164                 $this->assertEquals("#tags", $tags[5]);\r
165                 $this->assertEquals("@comment", $tags[6]);\r
166                 $this->assertEquals("@fullstops", $tags[7]);\r
167                 $this->assertEquals("#things", $tags[8]);\r
168                 $this->assertEquals("@Mike", $tags[9]);\r
169                 $this->assertEquals("@campino@friendica.eu", $tags[10]);\r
170                 $this->assertEquals("#nice", $tags[11]);\r
171                 $this->assertEquals("@first_last", $tags[12]);\r
172         }\r
173
174         /**
175          * test with an empty string
176          */\r
177         public function testGetTagsEmpty() {\r
178                 $tags=get_tags("");\r
179                 $this->assertEquals(0, count($tags));\r
180         }
181 }