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) {
52 if (3==count($args)) {
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]) {
62 //third call in handle_body, nick or attag
63 if ($result[0]['nick']===$args[2] || $result[0]['attag']===$args[1]) {
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() {
98 $this->a=new MockApp();
102 * test with one Person tag
104 public function testGetTagsShortPerson() {
107 $tags=get_tags($text);
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));
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);
126 * test with one Person tag.
127 * There's a minor spelling mistake...
129 public function testGetTagsShortPersonSpelling() {
130 $text="hi @Mike.because";
132 $tags=get_tags($text);
134 //correct tags found?
135 $this->assertEquals(1, count($tags));
136 $this->assertTrue(in_array("@Mike.because", $tags));
140 handle_tag($this->a, $text, $inform, $str_tags, 11, $tags[0]);
142 // (mike) - This is a tricky case.
143 // we support mentions as in @mike@example.com - which contains a period.
144 // This shouldn't match anything unless you have a contact named "Mike.because".
145 // We may need another test for "@Mike. because" - which should return the contact
146 // as we ignore trailing periods in tags.
148 // $this->assertEquals("cid:15", $inform);
149 // $this->assertEquals("@[url=http://justatest.de]Mike Lastname[/url]", $str_tags);
150 // $this->assertEquals("hi @[url=http://justatest.de]Mike Lastname[/url].because", $text);
152 $this->assertEquals("", $inform);
153 $this->assertEquals("", $str_tags);
158 * test with two Person tags.
159 * There's a minor spelling mistake...
162 public function testGetTagsPerson2Spelling() {
163 $text="hi @Mike@campino@friendica.eu";
165 $tags=get_tags($text);
167 // This construct is not supported. Results are indeterminate
168 // $this->assertEquals(2, count($tags));
169 // $this->assertTrue(in_array("@Mike", $tags));
170 // $this->assertTrue(in_array("@campino@friendica.eu", $tags));
174 * Test with one hash tag.
176 public function testGetTagsShortTag() {
177 $text="This is a #test_case";
179 $tags=get_tags($text);
181 $this->assertEquals(1, count($tags));
182 $this->assertTrue(in_array("#test_case", $tags));
186 * test with a person and a hash tag
188 public function testGetTagsShortTagAndPerson() {
189 $text="hi @Mike This is a #test_case";
191 $tags=get_tags($text);
193 $this->assertEquals(3, count($tags));
194 $this->assertTrue(in_array("@Mike", $tags));
195 $this->assertTrue(in_array("@Mike This", $tags));
196 $this->assertTrue(in_array("#test_case", $tags));
200 foreach ($tags as $tag) {
201 handle_tag($this->a, $text, $inform, $str_tags, 11, $tag);
204 $this->assertEquals("cid:15", $inform);
205 $this->assertEquals("@[url=http://justatest.de]Mike Lastname[/url],#[url=baseurl/search?tag=test%20case]test case[/url]", $str_tags);
206 $this->assertEquals("hi @[url=http://justatest.de]Mike Lastname[/url] This is a #[url=baseurl/search?tag=test%20case]test case[/url]", $text);
211 * test with a person, a hash tag and some special chars.
213 public function testGetTagsShortTagAndPersonSpecialChars() {
214 $text="hi @Mike, This is a #test_case.";
216 $tags=get_tags($text);
218 $this->assertEquals(2, count($tags));
219 $this->assertTrue(in_array("@Mike", $tags));
220 $this->assertTrue(in_array("#test_case", $tags));
224 * Test with a person tag and text behind it.
226 public function testGetTagsPersonOnly() {
227 $text="@Test I saw the Theme Dev group was created.";
229 $tags=get_tags($text);
231 $this->assertEquals(2, count($tags));
232 $this->assertTrue(in_array("@Test I", $tags));
233 $this->assertTrue(in_array("@Test", $tags));
237 * this test demonstrates strange behaviour by intval.
238 * It makes the next test fail.
240 public function testIntval() {
241 $this->assertEquals(15, intval("15 it"));
245 * test a tag with an id in it
247 public function testIdTag() {
248 $text="Test with @mike+15 id tag";
250 $tags=get_tags($text);
252 $this->assertEquals(2, count($tags));
253 $this->assertTrue(in_array("@mike+15", $tags));
255 //happens right now, but it shouldn't be necessary
256 $this->assertTrue(in_array("@mike+15 id", $tags));
260 foreach ($tags as $tag) {
261 handle_tag($this->a, $text, $inform, $str_tags, 11, $tag);
264 $this->assertEquals("Test with @[url=http://justatest.de]Mike Lastname[/url] id tag", $text);
265 $this->assertEquals("@[url=http://justatest.de]Mike Lastname[/url]", $str_tags);
266 // this test may produce two cid:15 entries - which is OK because duplicates are pruned before delivery
267 $this->assertContains("cid:15",$inform);
271 * test with two persons and one special tag.
273 public function testGetTags2Persons1TagSpecialChars() {
274 $text="hi @Mike, I'm just writing #test_cases, so"
275 ." so @somebody@friendica.com may change #things.";
277 $tags=get_tags($text);
279 $this->assertEquals(5, count($tags));
280 $this->assertTrue(in_array("@Mike", $tags));
281 $this->assertTrue(in_array("#test_cases", $tags));
282 $this->assertTrue(in_array("@somebody@friendica.com", $tags));
283 $this->assertTrue(in_array("@somebody@friendica.com may", $tags));
284 $this->assertTrue(in_array("#things", $tags));
288 * test with a long text.
290 public function testGetTags() {
291 $text="hi @Mike, I'm just writing #test_cases, "
292 ." so @somebody@friendica.com may change #things. Of course I "
293 ."look for a lot of #pitfalls, like #tags at the end of a sentence "
294 ."@comment. I hope noone forgets about @fullstops.because that might"
295 ." break #things. @Mike@campino@friendica.eu is also #nice, isn't it? "
296 ."Now, add a @first_last tag. ";
298 $tags=get_tags($text);
300 $this->assertTrue(in_array("@Mike", $tags));
301 $this->assertTrue(in_array("#test_cases", $tags));
302 $this->assertTrue(in_array("@somebody@friendica.com", $tags));
303 $this->assertTrue(in_array("#things", $tags));
304 $this->assertTrue(in_array("#pitfalls", $tags));
305 $this->assertTrue(in_array("#tags", $tags));
306 $this->assertTrue(in_array("@comment", $tags));
307 $this->assertTrue(in_array("@fullstops.because", $tags));
308 $this->assertTrue(in_array("#things", $tags));
309 $this->assertTrue(in_array("@Mike", $tags));
310 $this->assertTrue(in_array("#nice", $tags));
311 $this->assertTrue(in_array("@first_last", $tags));
313 //right now, none of the is matched (unsupported)
314 // $this->assertFalse(in_array("@Mike@campino@friendica.eu", $tags));
315 // $this->assertTrue(in_array("@campino@friendica.eu", $tags));
316 // $this->assertTrue(in_array("@campino@friendica.eu is", $tags));
320 * test with an empty string
322 public function testGetTagsEmpty() {
324 $this->assertEquals(0, count($tags));