3 * This file contains the tests for get_tags and the tag handling in item.php
9 * required, because it contains the get_tags() function
11 require_once 'include/text.php';
13 * required, because it contains the tag handling
15 require_once 'mod/item.php';
18 * A class which can be used as replacement for an app if
19 * only get_baseurl is used.
21 * @author Alexander Kampmann
25 function get_baseurl() {
31 * the test should not rely on a database,
32 * so this is a replacement for the database access method q.
34 * It simulates the user with uid 11 has one contact, named Mike Lastname.
39 $result=array(array('id'=>15,
40 'attag'=>'', 'network'=>'dfrn',
41 'name'=>'Mike Lastname', 'alias'=>'Mike',
42 'nick'=>'Mike', 'url'=>"http://justatest.de"));
44 $args=func_get_args();
46 //last parameter is always (in this test) uid, so, it should be 11
47 if($args[count($args)-1]!=11) {
53 //first call in handle_body, id only
54 if($result[0]['id']==$args[1]) {
57 //second call in handle_body, name
58 if($result[0]['name']===$args[1]) {
\r
62 //third call in handle_body, nick or attag
63 if($result[0]['nick']===$args[2] || $result[0]['attag']===$args[1]) {
\r
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.
74 * DON'T USE HAT FUNCTION OUTSIDE A TEST!
79 function dbesc($str) {
84 * TestCase for tag handling.
89 class GetTagsTest extends PHPUnit_Framework_TestCase {
90 /** the mock to use as app */
94 * initialize the test. That's a phpUnit function,
95 * don't change its name.
97 public function setUp() {
\r
98 $this->a=new MockApp();
102 * test with one Person tag
\r
104 public function testGetTagsShortPerson() {
\r
107 $tags=get_tags($text);
\r
111 foreach($tags as $tag) {
112 handle_tag($this->a, $text, $inform, $str_tags, 11, $tag);
115 //correct tags found?
116 $this->assertEquals(1, count($tags));
\r
117 $this->assertTrue(in_array("@Mike", $tags));
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
126 * test with one Person tag.
127 * There's a minor spelling mistake...
\r
129 public function testGetTagsShortPersonSpelling() {
\r
130 $text="hi @Mike.because";
\r
132 $tags=get_tags($text);
\r
134 //correct tags found?
\r
135 $this->assertEquals(1, count($tags));
\r
136 $this->assertTrue(in_array("@Mike.because", $tags));
140 handle_tag($this->a, $text, $inform, $str_tags, 11, $tags[0]);
\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
148 * test with two Person tags.
149 * There's a minor spelling mistake...
\r
152 public function testGetTagsPerson2Spelling() {
\r
153 $text="hi @Mike@campino@friendica.eu";
\r
155 $tags=get_tags($text);
\r
157 // This construct is not supported. Results are indeterminate
158 // $this->assertEquals(2, count($tags));
\r
159 // $this->assertTrue(in_array("@Mike", $tags));
160 // $this->assertTrue(in_array("@campino@friendica.eu", $tags));
\r
164 * Test with one hash tag.
166 public function testGetTagsShortTag() {
\r
167 $text="This is a #test_case";
\r
169 $tags=get_tags($text);
\r
171 $this->assertEquals(1, count($tags));
\r
172 $this->assertTrue(in_array("#test_case", $tags));
\r
176 * test with a person and a hash tag
178 public function testGetTagsShortTagAndPerson() {
\r
179 $text="hi @Mike This is a #test_case";
\r
181 $tags=get_tags($text);
\r
183 $this->assertEquals(3, count($tags));
184 $this->assertTrue(in_array("@Mike", $tags));
185 $this->assertTrue(in_array("@Mike This", $tags));
\r
186 $this->assertTrue(in_array("#test_case", $tags));
\r
190 foreach($tags as $tag) {
191 handle_tag($this->a, $text, $inform, $str_tags, 11, $tag);
194 $this->assertEquals("cid:15", $inform);
195 $this->assertEquals("@[url=http://justatest.de]Mike Lastname[/url],#[url=baseurl/search?search=test%20case]test case[/url]", $str_tags);
196 $this->assertEquals("hi @[url=http://justatest.de]Mike Lastname[/url] This is a #[url=baseurl/search?search=test%20case]test case[/url]", $text);
201 * test with a person, a hash tag and some special chars.
203 public function testGetTagsShortTagAndPersonSpecialChars() {
\r
204 $text="hi @Mike, This is a #test_case.";
\r
206 $tags=get_tags($text);
\r
208 $this->assertEquals(2, count($tags));
209 $this->assertTrue(in_array("@Mike", $tags));
210 $this->assertTrue(in_array("#test_case", $tags));
\r
214 * Test with a person tag and text behind it.
216 public function testGetTagsPersonOnly() {
\r
217 $text="@Test I saw the Theme Dev group was created.";
\r
219 $tags=get_tags($text);
\r
221 $this->assertEquals(2, count($tags));
\r
222 $this->assertTrue(in_array("@Test I", $tags));
223 $this->assertTrue(in_array("@Test", $tags));
\r
227 * this test demonstrates strange behaviour by intval.
228 * It makes the next test fail.
230 public function testIntval() {
231 $this->assertEquals(15, intval("15 it"));
235 * test a tag with an id in it
237 public function testIdTag() {
238 $text="Test with @mike+15 id tag";
240 $tags=get_tags($text);
242 $this->assertEquals(2, count($tags));
243 $this->assertTrue(in_array("@mike+15", $tags));
245 //happens right now, but it shouldn't be necessary
246 $this->assertTrue(in_array("@mike+15 id", $tags));
250 foreach($tags as $tag) {
251 handle_tag($this->a, $text, $inform, $str_tags, 11, $tag);
254 $this->assertEquals("Test with @[url=http://justatest.de]Mike Lastname[/url] id tag", $text);
\r
255 $this->assertEquals("@[url=http://justatest.de]Mike Lastname[/url]", $str_tags);
\r
256 $this->assertEquals("cid:15", $inform);
260 * test with two persons and one special tag.
262 public function testGetTags2Persons1TagSpecialChars() {
\r
263 $text="hi @Mike, I'm just writing #test_cases, so"
\r
264 ." so @somebody@friendica.com may change #things.";
\r
266 $tags=get_tags($text);
\r
268 $this->assertEquals(5, count($tags));
\r
269 $this->assertTrue(in_array("@Mike", $tags));
\r
270 $this->assertTrue(in_array("#test_cases", $tags));
271 $this->assertTrue(in_array("@somebody@friendica.com", $tags));
\r
272 $this->assertTrue(in_array("@somebody@friendica.com may", $tags));
\r
273 $this->assertTrue(in_array("#things", $tags));
\r
277 * test with a long text.
279 public function testGetTags() {
\r
280 $text="hi @Mike, I'm just writing #test_cases, "
\r
281 ." so @somebody@friendica.com may change #things. Of course I "
\r
282 ."look for a lot of #pitfalls, like #tags at the end of a sentence "
\r
283 ."@comment. I hope noone forgets about @fullstops.because that might"
\r
284 ." break #things. @Mike@campino@friendica.eu is also #nice, isn't it? "
\r
285 ."Now, add a @first_last tag. ";
\r
287 $tags=get_tags($text);
\r
289 $this->assertTrue(in_array("@Mike", $tags));
\r
290 $this->assertTrue(in_array("#test_cases", $tags));
\r
291 $this->assertTrue(in_array("@somebody@friendica.com", $tags));
\r
292 $this->assertTrue(in_array("#things", $tags));
\r
293 $this->assertTrue(in_array("#pitfalls", $tags));
\r
294 $this->assertTrue(in_array("#tags", $tags));
\r
295 $this->assertTrue(in_array("@comment", $tags));
\r
296 $this->assertTrue(in_array("@fullstops.because", $tags));
\r
297 $this->assertTrue(in_array("#things", $tags));
\r
298 $this->assertTrue(in_array("@Mike", $tags));
\r
299 $this->assertTrue(in_array("#nice", $tags));
\r
300 $this->assertTrue(in_array("@first_last", $tags));
302 //right now, none of the is matched (unsupported)
303 // $this->assertFalse(in_array("@Mike@campino@friendica.eu", $tags));
\r
304 // $this->assertTrue(in_array("@campino@friendica.eu", $tags));
305 // $this->assertTrue(in_array("@campino@friendica.eu is", $tags));
\r
309 * test with an empty string
311 public function testGetTagsEmpty() {
\r
312 $tags=get_tags("");
\r
313 $this->assertEquals(0, count($tags));
\r