<?php
/**
+ * This file contains the tests for get_tags and the tag handling in item.php
+ *
* @package test.util
*/
-require_once 'include/template_processor.php';\r
+/**
+ * required, because it contains the get_tags() function
+ */
require_once 'include/text.php';
+/**
+ * required, because it contains the tag handling
+ */
require_once 'mod/item.php';
+/**
+ * A class which can be used as replacement for an app if
+ * only get_baseurl is used.
+ *
+ * @author Alexander Kampmann
+ * @package test.util
+ */
+class MockApp {
+ function get_baseurl() {
+ return "baseurl";
+ }
+};
+
+/**
+ * the test should not rely on a database,
+ * so this is a replacement for the database access method q.
+ *
+ * It simulates the user with uid 11 has one contact, named Mike Lastname.
+ *
+ * @param string $sql
+ */
function q($sql) {
- return array(array('id'=>15, 'network'=>'stat', 'alias'=>'Mike', 'nick'=>'Mike', 'url'=>"http://justatest.de"));
+ $result=array(array('id'=>15,
+ 'attag'=>'', 'network'=>'dfrn',
+ 'name'=>'Mike Lastname', 'alias'=>'Mike',
+ 'nick'=>'Mike', 'url'=>"http://justatest.de"));
+
+ $args=func_get_args();
+ //last parameter is always (in this test) uid, so, it should be 11
+ if($args[count($args)-1]!=11) {
+ return;
+ }
+
+
+ if(3==count($args)) {
+ //first call in handle_body, id only
+ if($result[0]['id']==$args[1]) {
+ return $result;
+ }
+ //second call in handle_body, name
+ if($result[0]['name']===$args[1]) {\r
+ return $result;\r
+ }
+ }
+ //third call in handle_body, nick or attag
+ if($result[0]['nick']===$args[2] || $result[0]['attag']===$args[1]) {\r
+ return $result;\r
+ }
}
+
+/**
+ * replacement for dbesc.
+ * I don't want to test dbesc here, so
+ * I just return the input. It won't be a problem, because
+ * the test does not use a real database.
+ *
+ * DON'T USE HAT FUNCTION OUTSIDE A TEST!
+ *
+ * @param string $str
+ * @return input
+ */
function dbesc($str) {
- echo $str;
+ return $str;
}
-class GetTagsTest extends PHPUnit_Framework_TestCase {\r
-\r
+/**
+ * TestCase for tag handling.
+ *
+ * @author alexander
+ * @package test.util
+ */
+class GetTagsTest extends PHPUnit_Framework_TestCase {
+ /** the mock to use as app */
+ private $a; \r
+
+ /**
+ * initialize the test. That's a phpUnit function,
+ * don't change its name.
+ */\r
public function setUp() {\r
- set_include_path(\r
- get_include_path() . PATH_SEPARATOR\r
- . 'include' . PATH_SEPARATOR\r
- . 'library' . PATH_SEPARATOR\r
- . 'library/phpsec' . PATH_SEPARATOR\r
- . '.' );\r
+ $this->a=new MockApp();
}
/**\r
$inform='';
$str_tags='';
- handle_body($text, $inform, $str_tags, 11, $tags[0]);
-\r
- $this->assertEquals("@Mike", $tags[0]);
- $this->assertEquals($text, "hi @[url=http://justatest.de]Mike[/url]");\r
+ foreach($tags as $tag) {
+ handle_tag($this->a, $text, $inform, $str_tags, 11, $tag);
+ }
+
+ //correct tags found?
+ $this->assertEquals(1, count($tags)); \r
+ $this->assertTrue(in_array("@Mike", $tags));
+
+ //correct output from handle_tag?
+ $this->assertEquals("cid:15", $inform);
+ $this->assertEquals("@[url=http://justatest.de]Mike Lastname[/url]", $str_tags);
+ $this->assertEquals("hi @[url=http://justatest.de]Mike Lastname[/url]", $text);\r
+ }
+
+ /**\r
+ * test with one Person tag.
+ * There's a minor spelling mistake...\r
+ */\r
+ public function testGetTagsShortPersonSpelling() {\r
+ $text="hi @Mike.because";\r
+ \r
+ $tags=get_tags($text);\r
+
+ //correct tags found?\r
+ $this->assertEquals(1, count($tags));\r
+ $this->assertTrue(in_array("@Mike.because", $tags));
+ \r
+ $inform='';\r
+ $str_tags='';\r
+ handle_tag($this->a, $text, $inform, $str_tags, 11, $tags[0]);\r
+ \r
+ $this->assertEquals("cid:15", $inform);
+ $this->assertEquals("@[url=http://justatest.de]Mike Lastname[/url]", $str_tags);
+ $this->assertEquals("hi @[url=http://justatest.de]Mike Lastname[/url].because", $text);\r
+ }
+
+ /**\r
+ * test with two Person tags.
+ * There's a minor spelling mistake...\r
+ */\r
+ public function testGetTagsPerson2Spelling() {\r
+ $text="hi @Mike@campino@friendica.eu";\r
+ \r
+ $tags=get_tags($text);\r
+
+ $this->assertEquals(2, count($tags)); \r
+ $this->assertTrue(in_array("@Mike", $tags));
+ $this->assertTrue(in_array("@campino@friendica.eu", $tags));\r
}\r
/**
$text="This is a #test_case";\r
\r
$tags=get_tags($text);\r
-\r
- $this->assertEquals("#test_case", $tags[0]);\r
+
+ $this->assertEquals(1, count($tags));\r
+ $this->assertTrue(in_array("#test_case", $tags));\r
}\r
/**
\r
$tags=get_tags($text);\r
- $inform='';\r
- $str_tags='';\r
- handle_body($text, $inform, $str_tags, 11, $tags[0]);
-
- $this->assertEquals("hi @[url=http://justatest.de]Mike[/url] This is a #test_case", $text); \r
- $this->assertEquals("@Mike", $tags[0]);\r
- $this->assertEquals("#test_case", $tags[1]);\r
+ $this->assertEquals(3, count($tags));
+ $this->assertTrue(in_array("@Mike", $tags));
+ $this->assertTrue(in_array("@Mike This", $tags));\r
+ $this->assertTrue(in_array("#test_case", $tags));\r
+
+ $inform='';
+ $str_tags='';
+ foreach($tags as $tag) {
+ handle_tag($this->a, $text, $inform, $str_tags, 11, $tag);
+ }
+ \r
+ $this->assertEquals("cid:15", $inform);
+ $this->assertEquals("@[url=http://justatest.de]Mike Lastname[/url],#[url=baseurl/search?search=test%20case]test case[/url]", $str_tags);
+ $this->assertEquals("hi @[url=http://justatest.de]Mike Lastname[/url] This is a #[url=baseurl/search?search=test%20case]test case[/url]", $text);
+ \r
}\r
/**
\r
$tags=get_tags($text);\r
\r
- $this->assertEquals("@Mike", $tags[0]);\r
- $this->assertEquals("#test_case", $tags[1]);\r
+ $this->assertEquals(2, count($tags));
+ $this->assertTrue(in_array("@Mike", $tags));
+ $this->assertTrue(in_array("#test_case", $tags));\r
}\r
/**
$text="@Test I saw the Theme Dev group was created.";\r
\r
$tags=get_tags($text);\r
-\r
- $this->assertEquals("@Test", $tags[0]);\r
+
+ $this->assertEquals(2, count($tags));\r
+ $this->assertTrue(in_array("@Test I", $tags));
+ $this->assertTrue(in_array("@Test", $tags));\r
}\r
+ /**
+ * this test demonstrates strange behaviour by intval.
+ * It makes the next test fail.
+ */
+ public function testIntval() {
+ $this->assertEquals(15, intval("15 it"));
+ }
+
+ /**
+ * test a tag with an id in it
+ */
+ public function testIdTag() {
+ $text="Test with @mike+15 id tag";
+
+ $tags=get_tags($text);
+
+ $this->assertEquals(2, count($tags));
+ $this->assertTrue(in_array("@mike+15", $tags));
+
+ //happens right now, but it shouldn't be necessary
+ $this->assertTrue(in_array("@mike+15 id", $tags));
+
+ $inform='';\r
+ $str_tags='';
+ foreach($tags as $tag) {
+ handle_tag($this->a, $text, $inform, $str_tags, 11, $tag);
+ }
+
+ $this->assertEquals("Test with @[url=http://justatest.de]Mike Lastname[/url] id tag", $text);\r
+ $this->assertEquals("@[url=http://justatest.de]Mike Lastname[/url]", $str_tags);\r
+ $this->assertEquals("cid:15", $inform);
+ }
+
/**
* test with two persons and one special tag.
*/\r
." so @somebody@friendica.com may change #things.";\r
\r
$tags=get_tags($text);\r
-\r
- $this->assertEquals("@Mike", $tags[0]);\r
- $this->assertEquals("#test_cases", $tags[1]);\r
- $this->assertEquals("@somebody@friendica.com", $tags[2]);\r
- $this->assertEquals("#things", $tags[3]);\r
+
+ $this->assertEquals(5, count($tags));\r
+ $this->assertTrue(in_array("@Mike", $tags));\r
+ $this->assertTrue(in_array("#test_cases", $tags));
+ $this->assertTrue(in_array("@somebody@friendica.com", $tags));\r
+ $this->assertTrue(in_array("@somebody@friendica.com may", $tags));\r
+ $this->assertTrue(in_array("#things", $tags));\r
}\r
/**
\r
$tags=get_tags($text);\r
\r
- $this->assertEquals("@Mike", $tags[0]);\r
- $this->assertEquals("#test_cases", $tags[1]);\r
- $this->assertEquals("@somebody@friendica.com", $tags[2]);\r
- $this->assertEquals("#things", $tags[3]);\r
- $this->assertEquals("#pitfalls", $tags[4]);\r
- $this->assertEquals("#tags", $tags[5]);\r
- $this->assertEquals("@comment", $tags[6]);\r
- $this->assertEquals("@fullstops", $tags[7]);\r
- $this->assertEquals("#things", $tags[8]);\r
- $this->assertEquals("@Mike", $tags[9]);\r
- $this->assertEquals("@campino@friendica.eu", $tags[10]);\r
- $this->assertEquals("#nice", $tags[11]);\r
- $this->assertEquals("@first_last", $tags[12]);\r
+ $this->assertTrue(in_array("@Mike", $tags));\r
+ $this->assertTrue(in_array("#test_cases", $tags));\r
+ $this->assertTrue(in_array("@somebody@friendica.com", $tags));\r
+ $this->assertTrue(in_array("#things", $tags));\r
+ $this->assertTrue(in_array("#pitfalls", $tags));\r
+ $this->assertTrue(in_array("#tags", $tags));\r
+ $this->assertTrue(in_array("@comment", $tags));\r
+ $this->assertTrue(in_array("@fullstops.because", $tags));\r
+ $this->assertTrue(in_array("#things", $tags));\r
+ $this->assertTrue(in_array("@Mike", $tags));\r
+ $this->assertTrue(in_array("#nice", $tags));\r
+ $this->assertTrue(in_array("@first_last", $tags));
+
+ //right now, none of the is matched
+ $this->assertFalse(in_array("@Mike@campino@friendica.eu", $tags));\r
+ $this->assertTrue(in_array("@campino@friendica.eu", $tags));
+ $this->assertTrue(in_array("@campino@friendica.eu is", $tags));\r
}\r
/**