]> git.mxchange.org Git - friendica.git/commitdiff
splitted test cases.
authorAlexander Kampmann <programmer@nurfuerspam.de>
Fri, 9 Mar 2012 11:57:11 +0000 (12:57 +0100)
committerAlexander Kampmann <programmer@nurfuerspam.de>
Fri, 9 Mar 2012 11:57:11 +0000 (12:57 +0100)
mod/item.php
tests/get_tags_test.php [new file with mode: 0644]
tests/xss_filter_test.php

index 81d7c753b42484e1fe787b3829803a9159d2449d..a9edf0da66f7461466861e46bf30c8f324c96757 100755 (executable)
@@ -425,110 +425,7 @@ function item_post(&$a) {
 
        if(count($tags)) {
                foreach($tags as $tag) {
-                       
-                       if(isset($profile))
-                               unset($profile);
-                       if(strpos($tag,'#') === 0) {
-                               if(strpos($tag,'[url='))
-                                       continue;
-                               $basetag = str_replace('_',' ',substr($tag,1));
-                               $body = str_replace($tag,'#[url=' . $a->get_baseurl() . '/search?search=' . rawurlencode($basetag) . ']' . $basetag . '[/url]',$body);
-
-                               $newtag = '#[url=' . $a->get_baseurl() . '/search?search=' . rawurlencode($basetag) . ']' . $basetag . '[/url]';
-                               if(! stristr($str_tags,$newtag)) {
-                                       if(strlen($str_tags))
-                                               $str_tags .= ',';
-                                       $str_tags .= $newtag;
-                               } 
-                               continue;
-                       }
-                       if(strpos($tag,'@') === 0) {
-                               if(strpos($tag,'[url='))
-                                       continue;
-                               $stat = false;
-                               $name = substr($tag,1);
-                               if((strpos($name,'@')) || (strpos($name,'http://'))) {
-                                       $newname = $name;
-                                       $links = @lrdd($name);
-                                       if(count($links)) {
-                                               foreach($links as $link) {
-                                                       if($link['@attributes']['rel'] === 'http://webfinger.net/rel/profile-page')
-                                       $profile = $link['@attributes']['href'];
-                                                       if($link['@attributes']['rel'] === 'salmon') {
-                                                               if(strlen($inform))
-                                                                       $inform .= ',';
-                                       $inform .= 'url:' . str_replace(',','%2c',$link['@attributes']['href']);
-                                                       }
-                                               }
-                                       }
-                               }
-                               else {
-                                       $newname = $name;
-                                       $alias = '';
-                                       $tagcid = 0;
-                                       if(strrpos($newname,'+')) {
-                                               $tagcid = intval(substr($newname,strrpos($newname,'+') + 1));
-                                               if(strpos($name,' '))
-                                                       $name = substr($name,0,strpos($name,' '));
-                                       }       
-                                       if($tagcid) {
-                                               $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
-                                                       intval($tagcid),
-                                                       intval($profile_uid)
-                                               );
-                                       }
-                                       elseif(strstr($name,'_') || strstr($name,' ')) {
-                                               $newname = str_replace('_',' ',$name);
-                                               $r = q("SELECT * FROM `contact` WHERE `name` = '%s' AND `uid` = %d LIMIT 1",
-                                                       dbesc($newname),
-                                                       intval($profile_uid)
-                                               );
-                                       }
-                                       else {
-                                               $r = q("SELECT * FROM `contact` WHERE `attag` = '%s' OR `nick` = '%s' AND `uid` = %d ORDER BY `attag` DESC LIMIT 1",
-                                                       dbesc($name),
-                                                       dbesc($name),
-                                                       intval($profile_uid)
-                                               );
-                                       }
-                                       if(count($r)) {
-                                               $profile = $r[0]['url'];
-                                               if($r[0]['network'] === 'stat') {
-                                                       $newname = $r[0]['nick'];
-                                                       $stat = true;
-                                                       if($r[0]['alias'])
-                                                               $alias = $r[0]['alias'];
-                                               }
-                                               else
-                                                       $newname = $r[0]['name'];
-                                               if(strlen($inform))
-                                                       $inform .= ',';
-                                               $inform .= 'cid:' . $r[0]['id'];
-                                       }
-                               }
-                               if($profile) {
-                                       $body = str_replace('@' . $name, '@' . '[url=' . $profile . ']' . $newname      . '[/url]', $body);
-                                       $profile = str_replace(',','%2c',$profile);
-                                       $newtag = '@[url=' . $profile . ']' . $newname  . '[/url]';
-                                       if(! stristr($str_tags,$newtag)) {
-                                               if(strlen($str_tags))
-                                                       $str_tags .= ',';
-                                               $str_tags .= $newtag;
-                                       }
-
-                                       // Status.Net seems to require the numeric ID URL in a mention if the person isn't 
-                                       // subscribed to you. But the nickname URL is OK if they are. Grrr. We'll tag both. 
-
-                                       if(strlen($alias)) {
-                                               $newtag = '@[url=' . $alias . ']' . $newname    . '[/url]';
-                                               if(! stristr($str_tags,$newtag)) {
-                                                       if(strlen($str_tags))
-                                                               $str_tags .= ',';
-                                                       $str_tags .= $newtag;
-                                               }
-                                       }
-                               }
-                       }
+                       handle_tag($body, $inform, $str_tags, $profile_uid, $tag); 
                }
        }
 
@@ -922,3 +819,110 @@ function item_content(&$a) {
                drop_item($a->argv[2]);
        }
 }
+
+function handle_body(&$body, &$inform, &$str_tags, $profile_uid, $tag) {
+       $profile=null; 
+       if(isset($profile))\r
+               unset($profile);\r
+       if(strpos($tag,'#') === 0) {\r
+               if(strpos($tag,'[url='))\r
+                       continue;\r
+               $basetag = str_replace('_',' ',substr($tag,1));\r
+               $body = str_replace($tag,'#[url=' . $a->get_baseurl() . '/search?search=' . rawurlencode($basetag) . ']' . $basetag . '[/url]',$body);\r
+       \r
+               $newtag = '#[url=' . $a->get_baseurl() . '/search?search=' . rawurlencode($basetag) . ']' . $basetag . '[/url]';\r
+               if(! stristr($str_tags,$newtag)) {\r
+                       if(strlen($str_tags))\r
+                               $str_tags .= ',';\r
+                       $str_tags .= $newtag;\r
+               }\r
+               continue;\r
+       }\r
+       if(strpos($tag,'@') === 0) {\r
+               if(strpos($tag,'[url='))\r
+                       continue;\r
+               $stat = false;\r
+               $name = substr($tag,1);\r
+               if((strpos($name,'@')) || (strpos($name,'http://'))) {\r
+                       $newname = $name;\r
+                       $links = @lrdd($name);\r
+                       if(count($links)) {\r
+                               foreach($links as $link) {\r
+                                       if($link['@attributes']['rel'] === 'http://webfinger.net/rel/profile-page')\r
+                                               $profile = $link['@attributes']['href'];\r
+                                       if($link['@attributes']['rel'] === 'salmon') {\r
+                                               if(strlen($inform))\r
+                                                       $inform .= ',';\r
+                                               $inform .= 'url:' . str_replace(',','%2c',$link['@attributes']['href']);\r
+                                       }\r
+                               }\r
+                       }\r
+               }\r
+               else {\r
+                       $newname = $name;\r
+                       $alias = '';\r
+                       $tagcid = 0;\r
+                       if(strrpos($newname,'+')) {\r
+                               $tagcid = intval(substr($newname,strrpos($newname,'+') + 1));\r
+                               if(strpos($name,' '))\r
+                                       $name = substr($name,0,strpos($name,' '));\r
+                       }\r
+                       if($tagcid) {\r
+                               $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",\r
+                                               intval($tagcid),\r
+                                               intval($profile_uid)\r
+                               );\r
+                       }\r
+                       elseif(strstr($name,'_') || strstr($name,' ')) {\r
+                               $newname = str_replace('_',' ',$name);\r
+                               $r = q("SELECT * FROM `contact` WHERE `name` = '%s' AND `uid` = %d LIMIT 1",\r
+                                               dbesc($newname),\r
+                                               intval($profile_uid)\r
+                               );\r
+                       }\r
+                       else {\r
+                               $r = q("SELECT * FROM `contact` WHERE `attag` = '%s' OR `nick` = '%s' AND `uid` = %d ORDER BY `attag` DESC LIMIT 1",\r
+                                               dbesc($name),\r
+                                               dbesc($name),\r
+                                               intval($profile_uid)\r
+                               );\r
+                       }\r
+                       if(count($r)) {\r
+                               $profile = $r[0]['url'];\r
+                               if($r[0]['network'] === 'stat') {\r
+                                       $newname = $r[0]['nick'];\r
+                                       $stat = true;\r
+                                       if($r[0]['alias'])\r
+                                               $alias = $r[0]['alias'];\r
+                               }\r
+                               else\r
+                                       $newname = $r[0]['name'];\r
+                               if(strlen($inform))\r
+                                       $inform .= ',';\r
+                               $inform .= 'cid:' . $r[0]['id'];\r
+                       }\r
+               }\r
+               if($profile) {\r
+                       $body = str_replace('@' . $name, '@' . '[url=' . $profile . ']' . $newname      . '[/url]', $body);\r
+                       $profile = str_replace(',','%2c',$profile);\r
+                       $newtag = '@[url=' . $profile . ']' . $newname  . '[/url]';\r
+                       if(! stristr($str_tags,$newtag)) {\r
+                               if(strlen($str_tags))\r
+                                       $str_tags .= ',';\r
+                               $str_tags .= $newtag;\r
+                       }\r
+       \r
+                       // Status.Net seems to require the numeric ID URL in a mention if the person isn't\r
+                       // subscribed to you. But the nickname URL is OK if they are. Grrr. We'll tag both.\r
+       \r
+                       if(strlen($alias)) {\r
+                               $newtag = '@[url=' . $alias . ']' . $newname    . '[/url]';\r
+                               if(! stristr($str_tags,$newtag)) {\r
+                                       if(strlen($str_tags))\r
+                                               $str_tags .= ',';\r
+                                       $str_tags .= $newtag;\r
+                               }\r
+                       }\r
+               }\r
+       }
+}
diff --git a/tests/get_tags_test.php b/tests/get_tags_test.php
new file mode 100644 (file)
index 0000000..a458f0f
--- /dev/null
@@ -0,0 +1,147 @@
+<?php
+/**
+ * @package test.util
+ */
+
+require_once 'include/template_processor.php';\r
+require_once 'include/text.php';
+require_once 'mod/item.php';
+
+function q($sql) {
+       return array(array('id'=>15, 'network'=>'stat', 'alias'=>'Mike', 'nick'=>'Mike', 'url'=>"http://justatest.de"));
+
+}
+function dbesc($str) {
+       echo $str; 
+}
+
+class GetTagsTest extends PHPUnit_Framework_TestCase {\r
+\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
+       }
+
+       /**\r
+        * test with one Person tag\r
+        */\r
+       public function testGetTagsShortPerson() {\r
+               $text="hi @Mike";\r
+\r
+               $tags=get_tags($text);\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
+       }\r
+
+       /**
+        * Test with one hash tag.
+        */\r
+       public function testGetTagsShortTag() {\r
+               $text="This is a #test_case";\r
+\r
+               $tags=get_tags($text);\r
+\r
+               $this->assertEquals("#test_case", $tags[0]);\r
+       }\r
+
+       /**
+        * test with a person and a hash tag
+        */\r
+       public function testGetTagsShortTagAndPerson() {\r
+               $text="hi @Mike This is a #test_case";\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
+       }\r
+
+       /**
+        * test with a person, a hash tag and some special chars.
+        */\r
+       public function testGetTagsShortTagAndPersonSpecialChars() {\r
+               $text="hi @Mike, This is a #test_case.";\r
+\r
+               $tags=get_tags($text);\r
+\r
+               $this->assertEquals("@Mike", $tags[0]);\r
+               $this->assertEquals("#test_case", $tags[1]);\r
+       }\r
+
+       /**
+        * Test with a person tag and text behind it.
+        */\r
+       public function testGetTagsPersonOnly() {\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
+       }\r
+
+       /**
+        * test with two persons and one special tag.
+        */\r
+       public function testGetTags2Persons1TagSpecialChars() {\r
+               $text="hi @Mike, I'm just writing #test_cases, so"\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
+       }\r
+
+       /**
+        * test with a long text.
+        */\r
+       public function testGetTags() {\r
+               $text="hi @Mike, I'm just writing #test_cases, "\r
+               ." so @somebody@friendica.com may change #things. Of course I "\r
+               ."look for a lot of #pitfalls, like #tags at the end of a sentence "\r
+               ."@comment. I hope noone forgets about @fullstops.because that might"\r
+               ." break #things. @Mike@campino@friendica.eu is also #nice, isn't it? "\r
+               ."Now, add a @first_last tag. ";\r
+               //TODO check whether this are all variants (no, auto-stuff is missing).\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
+       }\r
+
+       /**
+        * test with an empty string
+        */\r
+       public function testGetTagsEmpty() {\r
+               $tags=get_tags("");\r
+               $this->assertEquals(0, count($tags));\r
+       }
+}
\ No newline at end of file
index 35d3b1be9e16d89458ea11ed4a5c79004b05d3db..fe944c2a748779011ed8e1d5ed69b1e8e641a2f6 100644 (file)
@@ -1,6 +1,5 @@
 <?php
 /**
- * Tests, without pHPUnit by now
  * @package test.util
  */
 
@@ -187,93 +186,6 @@ class AntiXSSTest extends PHPUnit_Framework_TestCase {
                $this->assertFalse(attribute_contains($testAttr, "class2"));
        }
 
-       /**
-        * test get_tags
-        */
-       public function testGetTagsShortPerson() {
-               $text="hi @Mike"; 
-               
-               $tags=get_tags($text);
-               
-               $this->assertEquals("@Mike", $tags[0]);
-       }
-       
-       public function testGetTagsShortTag() {\r
-               $text="This is a #test_case";\r
-       \r
-               $tags=get_tags($text);\r
-       \r
-               $this->assertEquals("#test_case", $tags[0]);\r
-       }
-       
-       public function testGetTagsShortTagAndPerson() {\r
-               $text="hi @Mike This is a #test_case";\r
-       \r
-               $tags=get_tags($text);\r
-       
-               $this->assertEquals("@Mike", $tags[0]);\r
-               $this->assertEquals("#test_case", $tags[1]);\r
-       }
-       
-       public function testGetTagsShortTagAndPersonSpecialChars() {\r
-               $text="hi @Mike, This is a #test_case.";\r
-       \r
-               $tags=get_tags($text);\r
-       \r
-               $this->assertEquals("@Mike", $tags[0]);\r
-               $this->assertEquals("#test_case", $tags[1]);\r
-       }
-       
-       public function testGetTagsPersonOnly() {
-               $text="@Test I saw the Theme Dev group was created."; 
-               
-               $tags=get_tags($text);\r
-               \r
-               $this->assertEquals("@Test", $tags[0]);
-       }
-       
-       public function testGetTags2Persons1TagSpecialChars() {\r
-               $text="hi @Mike, I'm just writing #test_cases, so"
-               ." 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]);
-               $this->assertEquals("@somebody@friendica.com", $tags[2]);
-               $this->assertEquals("#things", $tags[3]);\r
-       }
-       
-       public function testGetTags() {
-               $text="hi @Mike, I'm just writing #test_cases, "
-               ." so @somebody@friendica.com may change #things. Of course I "
-               ."look for a lot of #pitfalls, like #tags at the end of a sentence "
-               ."@comment. I hope noone forgets about @fullstops.because that might"
-               ." break #things. @Mike@campino@friendica.eu is also #nice, isn't it? "
-               ."Now, add a @first_last tag. ";
-               //check whether this are all variants (no, auto-stuff is missing).
-
-               $tags=get_tags($text);
-
-               $this->assertEquals("@Mike", $tags[0]);
-               $this->assertEquals("#test_cases", $tags[1]);
-               $this->assertEquals("@somebody@friendica.com", $tags[2]);
-               $this->assertEquals("#things", $tags[3]);
-               $this->assertEquals("#pitfalls", $tags[4]);
-               $this->assertEquals("#tags", $tags[5]);
-               $this->assertEquals("@comment", $tags[6]);
-               $this->assertEquals("@fullstops", $tags[7]);
-               $this->assertEquals("#things", $tags[8]);
-               $this->assertEquals("@Mike", $tags[9]);
-               $this->assertEquals("@campino@friendica.eu", $tags[10]);
-               $this->assertEquals("#nice", $tags[11]);
-               $this->assertEquals("@first_last", $tags[12]);
-       }
-
-       public function testGetTagsEmpty() {
-               $tags=get_tags("");
-               $this->assertEquals(0, count($tags));
-       }
        //function qp, quick and dirty??
        //get_mentions
        //get_contact_block, bis Zeile 538