mirror of
https://github.com/friendica/friendica
synced 2024-12-23 08:40:16 +00:00
get_tags tests corrected. They test for the right things now.
This commit is contained in:
parent
bce3bfff38
commit
509532d168
2 changed files with 195 additions and 61 deletions
|
@ -425,7 +425,7 @@ function item_post(&$a) {
|
||||||
|
|
||||||
if(count($tags)) {
|
if(count($tags)) {
|
||||||
foreach($tags as $tag) {
|
foreach($tags as $tag) {
|
||||||
handle_tag($body, $inform, $str_tags, $profile_uid, $tag);
|
handle_tag($a, $body, $inform, $str_tags, $profile_uid, $tag);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -830,7 +830,7 @@ function item_content(&$a) {
|
||||||
* @param unknown_type $profile_uid
|
* @param unknown_type $profile_uid
|
||||||
* @param unknown_type $tag the tag to replace
|
* @param unknown_type $tag the tag to replace
|
||||||
*/
|
*/
|
||||||
function handle_body(&$body, &$inform, &$str_tags, $profile_uid, $tag) {
|
function handle_tag($a, &$body, &$inform, &$str_tags, $profile_uid, $tag) {
|
||||||
//is it a hash tag?
|
//is it a hash tag?
|
||||||
if(strpos($tag,'#') === 0) {
|
if(strpos($tag,'#') === 0) {
|
||||||
//if the tag is replaced...
|
//if the tag is replaced...
|
||||||
|
@ -851,7 +851,7 @@ function handle_body(&$body, &$inform, &$str_tags, $profile_uid, $tag) {
|
||||||
$str_tags .= ',';
|
$str_tags .= ',';
|
||||||
$str_tags .= $newtag;
|
$str_tags .= $newtag;
|
||||||
}
|
}
|
||||||
continue;
|
return;
|
||||||
}
|
}
|
||||||
//is it a person tag?
|
//is it a person tag?
|
||||||
if(strpos($tag,'@') === 0) {
|
if(strpos($tag,'@') === 0) {
|
||||||
|
@ -887,9 +887,10 @@ function handle_body(&$body, &$inform, &$str_tags, $profile_uid, $tag) {
|
||||||
//get the id
|
//get the id
|
||||||
$tagcid = intval(substr($newname,strrpos($newname,'+') + 1));
|
$tagcid = intval(substr($newname,strrpos($newname,'+') + 1));
|
||||||
//remove the next word from tag's name
|
//remove the next word from tag's name
|
||||||
if(strpos($name,' '))
|
if(strpos($name,' ')) {
|
||||||
$name = substr($name,0,strpos($name,' '));
|
$name = substr($name,0,strpos($name,' '));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
if($tagcid) { //if there was an id
|
if($tagcid) { //if there was an id
|
||||||
//select contact with that id from the logged in user's contact list
|
//select contact with that id from the logged in user's contact list
|
||||||
$r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
|
$r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
|
||||||
|
|
|
@ -1,14 +1,41 @@
|
||||||
<?php
|
<?php
|
||||||
/**
|
/**
|
||||||
|
* This file contains the tests for get_tags and the tag handling in item.php
|
||||||
|
*
|
||||||
* @package test.util
|
* @package test.util
|
||||||
*/
|
*/
|
||||||
|
|
||||||
require_once 'include/template_processor.php';
|
/**
|
||||||
|
* required, because it contains the get_tags() function
|
||||||
|
*/
|
||||||
require_once 'include/text.php';
|
require_once 'include/text.php';
|
||||||
|
/**
|
||||||
|
* required, because it contains the tag handling
|
||||||
|
*/
|
||||||
require_once 'mod/item.php';
|
require_once 'mod/item.php';
|
||||||
|
|
||||||
function q($sql) {
|
/**
|
||||||
|
* 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) {
|
||||||
$result=array(array('id'=>15,
|
$result=array(array('id'=>15,
|
||||||
'attag'=>'', 'network'=>'dfrn',
|
'attag'=>'', 'network'=>'dfrn',
|
||||||
'name'=>'Mike Lastname', 'alias'=>'Mike',
|
'name'=>'Mike Lastname', 'alias'=>'Mike',
|
||||||
|
@ -16,48 +43,59 @@ function q($sql) {
|
||||||
|
|
||||||
$args=func_get_args();
|
$args=func_get_args();
|
||||||
|
|
||||||
$str="";
|
|
||||||
foreach($args as $arg) {
|
|
||||||
$str.=", ".$arg;
|
|
||||||
}
|
|
||||||
|
|
||||||
//last parameter is always (in this test) uid, so, it should be 11
|
//last parameter is always (in this test) uid, so, it should be 11
|
||||||
if($args[count($args)-1]!=11) {
|
if($args[count($args)-1]!=11) {
|
||||||
throw new Exception("q from get_tags_test was used and uid was not 11. ");
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(2==count($args)) {
|
|
||||||
|
if(3==count($args)) {
|
||||||
//first call in handle_body, id only
|
//first call in handle_body, id only
|
||||||
if($result[0]['id']===$args[1]) {
|
if($result[0]['id']==$args[1]) {
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
throw new Exception($str);
|
|
||||||
//second call in handle_body, name
|
//second call in handle_body, name
|
||||||
if($result[0]['name']===$args[1]) {
|
if($result[0]['name']===$args[1]) {
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
throw new Exception($str);
|
|
||||||
//third call in handle_body, nick or attag
|
//third call in handle_body, nick or attag
|
||||||
if($result[0]['nick']===$args[2] || $result[0]['attag']===$args[1]) {
|
if($result[0]['nick']===$args[2] || $result[0]['attag']===$args[1]) {
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
// throw new Exception("Nothing fitted: ".$args[1].", ".$args[2]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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) {
|
function dbesc($str) {
|
||||||
return $str;
|
return $str;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* TestCase for tag handling.
|
||||||
|
*
|
||||||
|
* @author alexander
|
||||||
|
* @package test.util
|
||||||
|
*/
|
||||||
class GetTagsTest extends PHPUnit_Framework_TestCase {
|
class GetTagsTest extends PHPUnit_Framework_TestCase {
|
||||||
|
/** the mock to use as app */
|
||||||
|
private $a;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* initialize the test. That's a phpUnit function,
|
||||||
|
* don't change its name.
|
||||||
|
*/
|
||||||
public function setUp() {
|
public function setUp() {
|
||||||
set_include_path(
|
$this->a=new MockApp();
|
||||||
get_include_path() . PATH_SEPARATOR
|
|
||||||
. 'include' . PATH_SEPARATOR
|
|
||||||
. 'library' . PATH_SEPARATOR
|
|
||||||
. 'library/phpsec' . PATH_SEPARATOR
|
|
||||||
. '.' );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -70,12 +108,56 @@ class GetTagsTest extends PHPUnit_Framework_TestCase {
|
||||||
|
|
||||||
$inform='';
|
$inform='';
|
||||||
$str_tags='';
|
$str_tags='';
|
||||||
handle_body($text, $inform, $str_tags, 11, $tags[0]);
|
foreach($tags as $tag) {
|
||||||
|
handle_tag($this->a, $text, $inform, $str_tags, 11, $tag);
|
||||||
|
}
|
||||||
|
|
||||||
$this->assertEquals("@Mike", $tags[0]);
|
//correct tags found?
|
||||||
|
$this->assertEquals(1, count($tags));
|
||||||
|
$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);
|
$this->assertEquals("hi @[url=http://justatest.de]Mike Lastname[/url]", $text);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* test with one Person tag.
|
||||||
|
* There's a minor spelling mistake...
|
||||||
|
*/
|
||||||
|
public function testGetTagsShortPersonSpelling() {
|
||||||
|
$text="hi @Mike.because";
|
||||||
|
|
||||||
|
$tags=get_tags($text);
|
||||||
|
|
||||||
|
//correct tags found?
|
||||||
|
$this->assertEquals(1, count($tags));
|
||||||
|
$this->assertTrue(in_array("@Mike.because", $tags));
|
||||||
|
|
||||||
|
$inform='';
|
||||||
|
$str_tags='';
|
||||||
|
handle_tag($this->a, $text, $inform, $str_tags, 11, $tags[0]);
|
||||||
|
|
||||||
|
$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);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* test with two Person tags.
|
||||||
|
* There's a minor spelling mistake...
|
||||||
|
*/
|
||||||
|
public function testGetTagsPerson2Spelling() {
|
||||||
|
$text="hi @Mike@campino@friendica.eu";
|
||||||
|
|
||||||
|
$tags=get_tags($text);
|
||||||
|
|
||||||
|
$this->assertEquals(2, count($tags));
|
||||||
|
$this->assertTrue(in_array("@Mike", $tags));
|
||||||
|
$this->assertTrue(in_array("@campino@friendica.eu", $tags));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test with one hash tag.
|
* Test with one hash tag.
|
||||||
*/
|
*/
|
||||||
|
@ -84,7 +166,8 @@ class GetTagsTest extends PHPUnit_Framework_TestCase {
|
||||||
|
|
||||||
$tags=get_tags($text);
|
$tags=get_tags($text);
|
||||||
|
|
||||||
$this->assertEquals("#test_case", $tags[0]);
|
$this->assertEquals(1, count($tags));
|
||||||
|
$this->assertTrue(in_array("#test_case", $tags));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -95,13 +178,21 @@ class GetTagsTest extends PHPUnit_Framework_TestCase {
|
||||||
|
|
||||||
$tags=get_tags($text);
|
$tags=get_tags($text);
|
||||||
|
|
||||||
|
$this->assertEquals(3, count($tags));
|
||||||
|
$this->assertTrue(in_array("@Mike", $tags));
|
||||||
|
$this->assertTrue(in_array("@Mike This", $tags));
|
||||||
|
$this->assertTrue(in_array("#test_case", $tags));
|
||||||
|
|
||||||
$inform='';
|
$inform='';
|
||||||
$str_tags='';
|
$str_tags='';
|
||||||
handle_body($text, $inform, $str_tags, 11, $tags[0]);
|
foreach($tags as $tag) {
|
||||||
|
handle_tag($this->a, $text, $inform, $str_tags, 11, $tag);
|
||||||
|
}
|
||||||
|
|
||||||
|
$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);
|
||||||
|
|
||||||
$this->assertEquals("hi @[url=http://justatest.de]Mike[/url] This is a #test_case", $text);
|
|
||||||
$this->assertEquals("@Mike", $tags[0]);
|
|
||||||
$this->assertEquals("#test_case", $tags[1]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -112,8 +203,9 @@ class GetTagsTest extends PHPUnit_Framework_TestCase {
|
||||||
|
|
||||||
$tags=get_tags($text);
|
$tags=get_tags($text);
|
||||||
|
|
||||||
$this->assertEquals("@Mike", $tags[0]);
|
$this->assertEquals(2, count($tags));
|
||||||
$this->assertEquals("#test_case", $tags[1]);
|
$this->assertTrue(in_array("@Mike", $tags));
|
||||||
|
$this->assertTrue(in_array("#test_case", $tags));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -124,7 +216,42 @@ class GetTagsTest extends PHPUnit_Framework_TestCase {
|
||||||
|
|
||||||
$tags=get_tags($text);
|
$tags=get_tags($text);
|
||||||
|
|
||||||
$this->assertEquals("@Test", $tags[0]);
|
$this->assertEquals(2, count($tags));
|
||||||
|
$this->assertTrue(in_array("@Test I", $tags));
|
||||||
|
$this->assertTrue(in_array("@Test", $tags));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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='';
|
||||||
|
$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);
|
||||||
|
$this->assertEquals("@[url=http://justatest.de]Mike Lastname[/url]", $str_tags);
|
||||||
|
$this->assertEquals("cid:15", $inform);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -136,10 +263,12 @@ class GetTagsTest extends PHPUnit_Framework_TestCase {
|
||||||
|
|
||||||
$tags=get_tags($text);
|
$tags=get_tags($text);
|
||||||
|
|
||||||
$this->assertEquals("@Mike", $tags[0]);
|
$this->assertEquals(5, count($tags));
|
||||||
$this->assertEquals("#test_cases", $tags[1]);
|
$this->assertTrue(in_array("@Mike", $tags));
|
||||||
$this->assertEquals("@somebody@friendica.com", $tags[2]);
|
$this->assertTrue(in_array("#test_cases", $tags));
|
||||||
$this->assertEquals("#things", $tags[3]);
|
$this->assertTrue(in_array("@somebody@friendica.com", $tags));
|
||||||
|
$this->assertTrue(in_array("@somebody@friendica.com may", $tags));
|
||||||
|
$this->assertTrue(in_array("#things", $tags));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -156,19 +285,23 @@ class GetTagsTest extends PHPUnit_Framework_TestCase {
|
||||||
|
|
||||||
$tags=get_tags($text);
|
$tags=get_tags($text);
|
||||||
|
|
||||||
$this->assertEquals("@Mike", $tags[0]);
|
$this->assertTrue(in_array("@Mike", $tags));
|
||||||
$this->assertEquals("#test_cases", $tags[1]);
|
$this->assertTrue(in_array("#test_cases", $tags));
|
||||||
$this->assertEquals("@somebody@friendica.com", $tags[2]);
|
$this->assertTrue(in_array("@somebody@friendica.com", $tags));
|
||||||
$this->assertEquals("#things", $tags[3]);
|
$this->assertTrue(in_array("#things", $tags));
|
||||||
$this->assertEquals("#pitfalls", $tags[4]);
|
$this->assertTrue(in_array("#pitfalls", $tags));
|
||||||
$this->assertEquals("#tags", $tags[5]);
|
$this->assertTrue(in_array("#tags", $tags));
|
||||||
$this->assertEquals("@comment", $tags[6]);
|
$this->assertTrue(in_array("@comment", $tags));
|
||||||
$this->assertEquals("@fullstops", $tags[7]);
|
$this->assertTrue(in_array("@fullstops.because", $tags));
|
||||||
$this->assertEquals("#things", $tags[8]);
|
$this->assertTrue(in_array("#things", $tags));
|
||||||
$this->assertEquals("@Mike", $tags[9]);
|
$this->assertTrue(in_array("@Mike", $tags));
|
||||||
$this->assertEquals("@campino@friendica.eu", $tags[10]);
|
$this->assertTrue(in_array("#nice", $tags));
|
||||||
$this->assertEquals("#nice", $tags[11]);
|
$this->assertTrue(in_array("@first_last", $tags));
|
||||||
$this->assertEquals("@first_last", $tags[12]);
|
|
||||||
|
//right now, none of the is matched
|
||||||
|
$this->assertFalse(in_array("@Mike@campino@friendica.eu", $tags));
|
||||||
|
$this->assertTrue(in_array("@campino@friendica.eu", $tags));
|
||||||
|
$this->assertTrue(in_array("@campino@friendica.eu is", $tags));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in a new issue