From a518928796f87ff61e7fcb34d69b374378fdadf6 Mon Sep 17 00:00:00 2001 From: cweiske Date: Sun, 28 Mar 2010 18:06:07 +0000 Subject: add test for api/export_csv.php git-svn-id: https://semanticscuttle.svn.sourceforge.net/svnroot/semanticscuttle/trunk@700 b3834d28-1941-0410-a4f8-b48e95affb8f --- tests/Api/ExportCsvTest.php | 104 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 tests/Api/ExportCsvTest.php (limited to 'tests/Api/ExportCsvTest.php') diff --git a/tests/Api/ExportCsvTest.php b/tests/Api/ExportCsvTest.php new file mode 100644 index 0000000..438df7f --- /dev/null +++ b/tests/Api/ExportCsvTest.php @@ -0,0 +1,104 @@ + + * @author Christian Weiske + * @author Eric Dane + * @license GPL http://www.gnu.org/licenses/gpl.html + * @link http://sourceforge.net/projects/semanticscuttle + */ + +require_once dirname(__FILE__) . '/../prepare.php'; +require_once 'HTTP/Request2.php'; + +if (!defined('PHPUnit_MAIN_METHOD')) { + define('PHPUnit_MAIN_METHOD', 'Api_ExportCsvTest::main'); +} + +/** + * Unit tests for the SemanticScuttle csv export API + * + * @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 Api_ExportCsvTest extends TestBaseApi +{ + protected $us; + protected $bs; + protected $urlPart = 'api/export_csv.php'; + + + + /** + * 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__) + ); + } + + + + /** + * Test if authentication is required when sending no auth data + */ + public function testAuthWithoutAuthData() + { + $req = $this->getRequest(null, false); + $res = $req->send(); + $this->assertEquals(401, $res->getStatus()); + } + + + + /** + * Test if authentication is required when sending wrong user data + + */ + public function testAuthWrongCredentials() + { + $req = $this->getRequest(null, false); + $req->setAuth('user', 'password', HTTP_Request2::AUTH_BASIC); + $res = $req->send(); + $this->assertEquals(401, $res->getStatus()); + } + + + + /** + * Test MIME content type and filename header fields + */ + public function testMimeTypeFilename() + { + $res = $this->getRequest()->send(); + + $this->assertEquals(200, $res->getStatus()); + //verify MIME content type + $this->assertEquals( + 'application/csv-tab-delimited-table; charset=utf-8', + $res->getHeader('content-type') + ); + //we need a file name + $this->assertNotNull($res->getHeader('content-disposition')); + } +} + +if (PHPUnit_MAIN_METHOD == 'Api_ExportCsvTest::main') { + Api_ExportCsvTest::main(); +} +?> \ No newline at end of file -- cgit v1.2.3-54-g00ecf 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/Api/ExportCsvTest.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-54-g00ecf From 74dc9ea6a4e553f38a97928617074fd76793c783 Mon Sep 17 00:00:00 2001 From: cweiske Date: Sun, 28 Mar 2010 18:08:38 +0000 Subject: make export_csv support filtering to multiple tags git-svn-id: https://semanticscuttle.svn.sourceforge.net/svnroot/semanticscuttle/trunk@702 b3834d28-1941-0410-a4f8-b48e95affb8f --- tests/Api/ExportCsvTest.php | 61 +++++++++++++++++++++++++++++++++++++++++++++ www/api/export_csv.php | 15 +++++++---- 2 files changed, 71 insertions(+), 5 deletions(-) (limited to 'tests/Api/ExportCsvTest.php') diff --git a/tests/Api/ExportCsvTest.php b/tests/Api/ExportCsvTest.php index ee7db4b..18008e1 100644 --- a/tests/Api/ExportCsvTest.php +++ b/tests/Api/ExportCsvTest.php @@ -162,6 +162,67 @@ class Api_ExportCsvTest extends TestBaseApi + /** + * Test CSV export with tag filter + */ + public function testTagFilter() + { + list($req, $uid) = $this->getAuthRequest('?tag=tag1'); + $this->addBookmark( + $uid, 'http://example.org/tag-1', 0, + array('unittest', 'tag1') + ); + $this->addBookmark( + $uid, 'http://example.org/tag-2', 0, + array('unittest', 'tag2') + ); + $this->addBookmark( + $uid, 'http://example.org/tag-3', 0, + array('unittest', 'tag1', 'tag2') + ); + + $body = $req->send()->getBody(); + $csv = $this->getCsvArray($body); + + $this->assertEquals(3, count($csv)); + $this->assertCsvHeader($csv); + + $this->assertEquals('http://example.org/tag-1', $csv[1][0]); + $this->assertEquals('http://example.org/tag-3', $csv[2][0]); + } + + + + /** + * Test CSV export with tag filter for multiple tags + */ + public function testTagFilterMultiple() + { + list($req, $uid) = $this->getAuthRequest('?tag=tag1+tag2'); + $this->addBookmark( + $uid, 'http://example.org/tag-1', 0, + array('unittest', 'tag1') + ); + $this->addBookmark( + $uid, 'http://example.org/tag-2', 0, + array('unittest', 'tag2') + ); + $this->addBookmark( + $uid, 'http://example.org/tag-3', 0, + array('unittest', 'tag1', 'tag2') + ); + + $body = $req->send()->getBody(); + $csv = $this->getCsvArray($body); + + $this->assertEquals(2, count($csv)); + $this->assertCsvHeader($csv); + + $this->assertEquals('http://example.org/tag-3', $csv[1][0]); + } + + + /** * Asserts that the CSV array contains the correct header * diff --git a/www/api/export_csv.php b/www/api/export_csv.php index b9cf497..43951ec 100644 --- a/www/api/export_csv.php +++ b/www/api/export_csv.php @@ -10,13 +10,18 @@ header("Content-disposition: filename=exportBookmarks.csv"); $bookmarkservice =SemanticScuttle_Service_Factory::get('Bookmark'); // Check to see if a tag was specified. -if (isset($_REQUEST['tag']) && (trim($_REQUEST['tag']) != '')) - $tag = trim($_REQUEST['tag']); -else - $tag = NULL; +if (isset($_REQUEST['tag']) && (trim($_REQUEST['tag']) != '')) { + //$_GET vars have + replaced to " " automatically + $tag = str_replace(' ', '+', trim($_REQUEST['tag'])); +} else { + $tag = null; +} // Get the posts relevant to the passed-in variables. -$bookmarks =& $bookmarkservice->getBookmarks(0, NULL, $userservice->getCurrentUserId(), $tag, NULL, getSortOrder()); +$bookmarks = $bookmarkservice->getBookmarks( + 0, null, $userservice->getCurrentUserId(), + $tag, null, getSortOrder() +); //columns titles echo 'url;title;tags;description'; -- cgit v1.2.3-54-g00ecf From ccbc63aec4869f64fb68706a6d687aa665a60c9c Mon Sep 17 00:00:00 2001 From: cweiske Date: Sun, 28 Mar 2010 18:09:59 +0000 Subject: test that public bookmarks of other people are not exported via csv git-svn-id: https://semanticscuttle.svn.sourceforge.net/svnroot/semanticscuttle/trunk@703 b3834d28-1941-0410-a4f8-b48e95affb8f --- tests/Api/ExportCsvTest.php | 5 +++++ www/api/export_csv.php | 15 ++++++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) (limited to 'tests/Api/ExportCsvTest.php') diff --git a/tests/Api/ExportCsvTest.php b/tests/Api/ExportCsvTest.php index 18008e1..2bff8a5 100644 --- a/tests/Api/ExportCsvTest.php +++ b/tests/Api/ExportCsvTest.php @@ -140,6 +140,11 @@ class Api_ExportCsvTest extends TestBaseApi $this->addBookmark( null, 'http://example.org/testBookmarks-private2', 2 ); + //public bookmark from other people that should not be + // exported, too + $this->addBookmark( + null, 'http://example.org/testBookmarks-other', 0 + ); $body = $req->send()->getBody(); $csv = $this->getCsvArray($body); diff --git a/www/api/export_csv.php b/www/api/export_csv.php index 43951ec..bb469b1 100644 --- a/www/api/export_csv.php +++ b/www/api/export_csv.php @@ -1,5 +1,18 @@ + * @author Christian Weiske + * @author Eric Dane + * @license GPL http://www.gnu.org/licenses/gpl.html + * @link http://sourceforge.net/projects/semanticscuttle + */ // Force HTTP authentication first! $httpContentType = 'application/csv-tab-delimited-table'; -- cgit v1.2.3-54-g00ecf