From e5dab0a740e71384031caadecb9f9e283572e8f2 Mon Sep 17 00:00:00 2001 From: cweiske Date: Tue, 16 Feb 2010 21:54:31 +0000 Subject: allow adding of certain addresses git-svn-id: https://semanticscuttle.svn.sourceforge.net/svnroot/semanticscuttle/trunk@654 b3834d28-1941-0410-a4f8-b48e95affb8f --- tests/TestBase.php | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) (limited to 'tests/TestBase.php') diff --git a/tests/TestBase.php b/tests/TestBase.php index 05988a5..f23f678 100644 --- a/tests/TestBase.php +++ b/tests/TestBase.php @@ -29,11 +29,12 @@ class TestBase extends PHPUnit_Framework_TestCase /** * Create a new bookmark. * - * @param integer $user User ID the bookmark shall belong + * @param integer $user User ID the bookmark shall belong + * @param string $address Bookmark address to use * * @return integer ID of bookmark */ - protected function addBookmark($user = null) + protected function addBookmark($user = null, $address = null) { if ($user === null) { $user = $this->addUser(); @@ -41,8 +42,13 @@ class TestBase extends PHPUnit_Framework_TestCase $bs = SemanticScuttle_Service_Factory::get('Bookmark'); $rand = rand(); + + if ($address === null) { + $address = 'http://example.org/' . $rand; + } + $bid = $bs->addBookmark( - 'http://example.org/' . $rand, + $address, 'unittest bookmark #' . $rand, 'description', null, -- cgit v1.2.3 From 57d4892eb346bf8837416dd4ea3571d87bd0f486 Mon Sep 17 00:00:00 2001 From: cweiske Date: Tue, 16 Feb 2010 21:57:03 +0000 Subject: allow setting of status during addBookmark in tests git-svn-id: https://semanticscuttle.svn.sourceforge.net/svnroot/semanticscuttle/trunk@656 b3834d28-1941-0410-a4f8-b48e95affb8f --- tests/TestBase.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'tests/TestBase.php') diff --git a/tests/TestBase.php b/tests/TestBase.php index f23f678..277be02 100644 --- a/tests/TestBase.php +++ b/tests/TestBase.php @@ -31,11 +31,15 @@ class TestBase extends PHPUnit_Framework_TestCase * * @param integer $user User ID the bookmark shall belong * @param string $address Bookmark address to use + * @param integer $status Bookmark visibility * * @return integer ID of bookmark + * + * @see SemanticScuttle_Service_Bookmark::addBookmark() */ - protected function addBookmark($user = null, $address = null) - { + protected function addBookmark( + $user = null, $address = null, $status = 0 + ) { if ($user === null) { $user = $this->addUser(); } @@ -52,7 +56,7 @@ class TestBase extends PHPUnit_Framework_TestCase 'unittest bookmark #' . $rand, 'description', null, - 0, + $status, array('unittest'), null, null, false, false, $user -- cgit v1.2.3 From 97c3d1ed636609ef19dd3cc268d0f5923f5257db Mon Sep 17 00:00:00 2001 From: cweiske Date: Sat, 20 Feb 2010 11:08:52 +0000 Subject: add test for Bookmark2Tag::getTagsForBookmark() git-svn-id: https://semanticscuttle.svn.sourceforge.net/svnroot/semanticscuttle/trunk@663 b3834d28-1941-0410-a4f8-b48e95affb8f --- tests/Bookmark2TagTest.php | 128 +++++++++++++++++++++++++++++++++++++++++++++ tests/TestBase.php | 10 +++- 2 files changed, 136 insertions(+), 2 deletions(-) create mode 100644 tests/Bookmark2TagTest.php (limited to 'tests/TestBase.php') diff --git a/tests/Bookmark2TagTest.php b/tests/Bookmark2TagTest.php new file mode 100644 index 0000000..7d003c3 --- /dev/null +++ b/tests/Bookmark2TagTest.php @@ -0,0 +1,128 @@ + + * @author Christian Weiske + * @author Eric Dane + * @license GPL http://www.gnu.org/licenses/gpl.html + * @link http://sourceforge.net/projects/semanticscuttle + */ + +require_once 'prepare.php'; + +if (!defined('PHPUnit_MAIN_METHOD')) { + define('PHPUnit_MAIN_METHOD', 'Bookmark2TagTest::main'); +} + +/** + * Unit tests for the SemanticScuttle bookmark-tag combination service. + * + * @category Bookmarking + * @package SemanticScuttle + * @author Benjamin Huynh-Kim-Bang + * @author Christian Weiske + * @author Eric Dane + * @license GPL http://www.gnu.org/licenses/gpl.html + * @link http://sourceforge.net/projects/semanticscuttle + */ +class Bookmark2TagTest extends TestBase +{ + protected $us; + protected $bs; + protected $ts; + protected $tts; + + + + /** + * Used to run this test class standalone + * + * @return void + */ + public static function main() + { + require_once 'PHPUnit/TextUI/TestRunner.php'; + PHPUnit_TextUI_TestRunner::run( + new PHPUnit_Framework_TestSuite(__CLASS__) + ); + } + + + + protected function setUp() + { + $this->us = SemanticScuttle_Service_Factory::get('User'); + $this->bs = SemanticScuttle_Service_Factory::get('Bookmark'); + $this->bs->deleteAll(); + $this->b2ts= SemanticScuttle_Service_Factory::get('Bookmark2Tag'); + $this->b2ts->deleteAll(); + $this->tts = SemanticScuttle_Service_Factory::get('Tag2Tag'); + $this->tts->deleteAll(); + $this->tsts = SemanticScuttle_Service_Factory::get('TagStat'); + $this->tsts->deleteAll(); + $this->vs = SemanticScuttle_Service_Factory::get('Vote'); + $this->vs->deleteAll(); + } + + + + /** + * Test getTagsForBookmark() when the bookmark has no tags + * + * @return void + */ + public function testGetTagsForBookmarkNone() + { + $bid = $this->addBookmark(null, null, 0, array()); + $this->assertEquals( + array(), + $this->b2ts->getTagsForBookmark($bid) + ); + } + + + + /** + * Test getTagsForBookmark() when the bookmark has one tag + * + * @return void + */ + public function testGetTagsForBookmarkOne() + { + $bid = $this->addBookmark(null, null, 0, array()); + $this->b2ts->attachTags($bid, array('foo')); + $this->assertEquals( + array('foo'), + $this->b2ts->getTagsForBookmark($bid) + ); + } + + + + /** + * Test getTagsForBookmark() when the bookmark has three tags + * + * @return void + */ + public function testGetTagsForBookmarkThree() + { + $bid = $this->addBookmark(null, null, 0, array()); + $this->b2ts->attachTags($bid, array('foo', 'bar', 'fuu')); + + $tags = $this->b2ts->getTagsForBookmark($bid); + $this->assertType('array', $tags); + $this->assertContains('foo', $tags); + $this->assertContains('bar', $tags); + $this->assertContains('fuu', $tags); + } +} + +if (PHPUnit_MAIN_METHOD == 'Bookmark2TagTest::main') { + Bookmark2TagTest::main(); +} +?> \ No newline at end of file diff --git a/tests/TestBase.php b/tests/TestBase.php index 277be02..28bda27 100644 --- a/tests/TestBase.php +++ b/tests/TestBase.php @@ -32,17 +32,23 @@ class TestBase extends PHPUnit_Framework_TestCase * @param integer $user User ID the bookmark shall belong * @param string $address Bookmark address to use * @param integer $status Bookmark visibility + * @param array $tags Array of tags to attach. If "null" is given, + * it will automatically be "unittest" * * @return integer ID of bookmark * * @see SemanticScuttle_Service_Bookmark::addBookmark() */ protected function addBookmark( - $user = null, $address = null, $status = 0 + $user = null, $address = null, $status = 0, + $tags = null ) { if ($user === null) { $user = $this->addUser(); } + if ($tags === null) { + $tags = array('unittest'); + } $bs = SemanticScuttle_Service_Factory::get('Bookmark'); $rand = rand(); @@ -57,7 +63,7 @@ class TestBase extends PHPUnit_Framework_TestCase 'description', null, $status, - array('unittest'), + $tags, null, null, false, false, $user ); -- cgit v1.2.3 From ccabc1cdfa6605f4454653360ec00dc171368556 Mon Sep 17 00:00:00 2001 From: cweiske Date: Sun, 28 Mar 2010 18:04:53 +0000 Subject: support username and password change git-svn-id: https://semanticscuttle.svn.sourceforge.net/svnroot/semanticscuttle/trunk@699 b3834d28-1941-0410-a4f8-b48e95affb8f --- tests/TestBase.php | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) (limited to 'tests/TestBase.php') diff --git a/tests/TestBase.php b/tests/TestBase.php index 28bda27..aad772f 100644 --- a/tests/TestBase.php +++ b/tests/TestBase.php @@ -75,15 +75,26 @@ class TestBase extends PHPUnit_Framework_TestCase /** * Creates a new user in the database. * + * @param string $username Username + * @param string $password Password + * * @return integer ID of user */ - protected function addUser() + protected function addUser($username = null, $password = null) { $us = SemanticScuttle_Service_Factory::get('User'); $rand = rand(); + + if ($username === null) { + $username = 'unittestuser-' . $rand; + } + if ($password === null) { + $password = $rand; + } + $uid = $us->addUser( - 'unittestuser-' . $rand, - $rand, + $username, + $password, 'unittest-' . $rand . '@example.org' ); return $uid; -- cgit v1.2.3 From 90f7d528d3f34a57d517cca268766bdbae161585 Mon Sep 17 00:00:00 2001 From: cweiske Date: Sun, 28 Mar 2010 18:07:18 +0000 Subject: more tests for csv export api git-svn-id: https://semanticscuttle.svn.sourceforge.net/svnroot/semanticscuttle/trunk@701 b3834d28-1941-0410-a4f8-b48e95affb8f --- tests/Api/ExportCsvTest.php | 119 +++++++++++++++++++++++++++++++++++++++++++- tests/TestBase.php | 8 ++- tests/TestBaseApi.php | 44 ++++++++++++---- 3 files changed, 157 insertions(+), 14 deletions(-) (limited to 'tests/TestBase.php') diff --git a/tests/Api/ExportCsvTest.php b/tests/Api/ExportCsvTest.php index 438df7f..ee7db4b 100644 --- a/tests/Api/ExportCsvTest.php +++ b/tests/Api/ExportCsvTest.php @@ -85,7 +85,7 @@ class Api_ExportCsvTest extends TestBaseApi */ public function testMimeTypeFilename() { - $res = $this->getRequest()->send(); + $res = reset($this->getAuthRequest())->send(); $this->assertEquals(200, $res->getStatus()); //verify MIME content type @@ -96,6 +96,123 @@ class Api_ExportCsvTest extends TestBaseApi //we need a file name $this->assertNotNull($res->getHeader('content-disposition')); } + + + + /** + * Test CSV export without bookmarks + */ + public function testNoBookmarks() + { + list($req, $uid) = $this->getAuthRequest(); + $body = $req->send()->getBody(); + $csv = $this->getCsvArray($body); + + $this->assertEquals(1, count($csv)); + $this->assertCsvHeader($csv); + } + + + + /** + * Test CSV export with some bookmarks + */ + public function testBookmarks() + { + list($req, $uid) = $this->getAuthRequest(); + //public + $this->addBookmark( + $uid, 'http://example.org/testBookmarks', 0, + array('unittest', 'testBookmarks'), 'mytitle' + ); + //shared + $this->addBookmark( + $uid, 'http://example.org/testBookmarks-shared', 1, + array('unittest', 'testBookmarks'), 'mytitle-shared' + ); + //private + $this->addBookmark( + $uid, 'http://example.org/testBookmarks-private', 2, + array('unittest', 'testBookmarks'), 'mytitle-private' + ); + + //private other that should not in the export + $this->addBookmark( + null, 'http://example.org/testBookmarks-private2', 2 + ); + + $body = $req->send()->getBody(); + $csv = $this->getCsvArray($body); + + $this->assertEquals(4, count($csv)); + $this->assertCsvHeader($csv); + + $this->assertEquals('http://example.org/testBookmarks', $csv[1][0]); + $this->assertEquals('mytitle', $csv[1][1]); + $this->assertEquals('unittest,testbookmarks', $csv[1][2]); + + $this->assertEquals('http://example.org/testBookmarks-shared', $csv[2][0]); + $this->assertEquals('mytitle-shared', $csv[2][1]); + $this->assertEquals('unittest,testbookmarks', $csv[2][2]); + + $this->assertEquals('http://example.org/testBookmarks-private', $csv[3][0]); + $this->assertEquals('mytitle-private', $csv[3][1]); + $this->assertEquals('unittest,testbookmarks', $csv[3][2]); + } + + + + /** + * Asserts that the CSV array contains the correct header + * + * @param array $csv CSV array from getCsvArray() + * + * @return void + */ + protected function assertCsvHeader($csv) + { + $this->assertEquals( + array('url', 'title', 'tags', 'description'), + $csv[0] + ); + } + + + + /** + * Converts a string of CSV data to an array + * + * @param string $body String containing the full CSV file + * + * @return array Array of CSV data + */ + protected function getCsvArray($body) + { + $v53 = (version_compare(PHP_VERSION, '5.3.0') === 1); + + //dead simple implementation that does not work with + // advanced CSV files + $ar = array(); + foreach (explode("\n", $body) as $line) { + if ($v53) { + $ar[] = str_getcsv($line, ';'); + } else { + $arl = explode(';', $line); + foreach ($arl as &$str) { + if (substr($str, 0, 1) == '"' + && substr($str, -1) == '"' + ) { + $str = substr($str, 1, -1); + } + } + $ar[] = $arl; + } + } + if (count(end($ar)) == 1 && reset(end($ar)) == '') { + unset($ar[key($ar)]); + } + return $ar; + } } if (PHPUnit_MAIN_METHOD == 'Api_ExportCsvTest::main') { diff --git a/tests/TestBase.php b/tests/TestBase.php index aad772f..402330b 100644 --- a/tests/TestBase.php +++ b/tests/TestBase.php @@ -34,6 +34,7 @@ class TestBase extends PHPUnit_Framework_TestCase * @param integer $status Bookmark visibility * @param array $tags Array of tags to attach. If "null" is given, * it will automatically be "unittest" + * @param string $title Bookmark title * * @return integer ID of bookmark * @@ -41,7 +42,7 @@ class TestBase extends PHPUnit_Framework_TestCase */ protected function addBookmark( $user = null, $address = null, $status = 0, - $tags = null + $tags = null, $title = null ) { if ($user === null) { $user = $this->addUser(); @@ -56,10 +57,13 @@ class TestBase extends PHPUnit_Framework_TestCase if ($address === null) { $address = 'http://example.org/' . $rand; } + if ($title === null) { + $title = 'unittest bookmark #' . $rand; + } $bid = $bs->addBookmark( $address, - 'unittest bookmark #' . $rand, + $title, 'description', null, $status, diff --git a/tests/TestBaseApi.php b/tests/TestBaseApi.php index 03ca016..645ead9 100644 --- a/tests/TestBaseApi.php +++ b/tests/TestBaseApi.php @@ -42,8 +42,11 @@ class TestBaseApi extends TestBase $this->url = $GLOBALS['unittestUrl'] . $this->urlPart; $this->us = SemanticScuttle_Service_Factory::get('User'); + $this->us->deleteAll(); $this->bs = SemanticScuttle_Service_Factory::get('Bookmark'); $this->bs->deleteAll(); + $this->b2t = SemanticScuttle_Service_Factory::get('Bookmark2Tag'); + $this->b2t->deleteAll(); } @@ -51,28 +54,47 @@ class TestBaseApi extends TestBase /** * Gets a HTTP request object * - * @param string $urlSuffix Suffix for the URL - * @param boolean $auth If user authentication is needed + * @param string $urlSuffix Suffix for the URL * * @return HTTP_Request2 HTTP request object */ - protected function getRequest($urlSuffix = null, $auth = true) + protected function getRequest($urlSuffix = null) { $req = new HTTP_Request2( $this->url . $urlSuffix, HTTP_Request2::METHOD_GET ); - if ($auth) { - $this->addUser('testuser', 'testpassword'); - $req->setAuth( - 'testuser', 'testpassword', - HTTP_Request2::AUTH_BASIC - ); - } - return $req; } + + + /** + * Gets a HTTP request object + * + * @param string $urlSuffix Suffix for the URL + * @param mixed $auth If user authentication is needed (true/false) + * or array with username and password + * + * @return array(HTTP_Request2, integer) HTTP request object and user id + */ + protected function getAuthRequest($urlSuffix = null, $auth = true) + { + $req = $this->getRequest($urlSuffix); + if (is_array($auth)) { + list($username, $password) = $auth; + } else { + $username = 'testuser'; + $password = 'testpassword'; + } + $uid = $this->addUser($username, $password); + $req->setAuth( + $username, $password, + HTTP_Request2::AUTH_BASIC + ); + return array($req, $uid); + } + } ?> \ No newline at end of file -- cgit v1.2.3