summaryrefslogtreecommitdiff
path: root/tests/Api/ExportCsvTest.php
diff options
context:
space:
mode:
authorcweiske <cweiske@b3834d28-1941-0410-a4f8-b48e95affb8f>2010-03-28 18:07:18 +0000
committercweiske <cweiske@b3834d28-1941-0410-a4f8-b48e95affb8f>2010-03-28 18:07:18 +0000
commit90f7d528d3f34a57d517cca268766bdbae161585 (patch)
tree78a1534c6b9e3f9864a5a5730d6fd47615a6e22f /tests/Api/ExportCsvTest.php
parenta518928796f87ff61e7fcb34d69b374378fdadf6 (diff)
downloadsemanticscuttle-90f7d528d3f34a57d517cca268766bdbae161585.tar.gz
semanticscuttle-90f7d528d3f34a57d517cca268766bdbae161585.tar.bz2
more tests for csv export api
git-svn-id: https://semanticscuttle.svn.sourceforge.net/svnroot/semanticscuttle/trunk@701 b3834d28-1941-0410-a4f8-b48e95affb8f
Diffstat (limited to 'tests/Api/ExportCsvTest.php')
-rw-r--r--tests/Api/ExportCsvTest.php119
1 files changed, 118 insertions, 1 deletions
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') {