/tests/Api/ExportCsvTest.php

https://github.com/omigeot-ccpo/SemanticScuttle-SSO · PHP · 262 lines · 149 code · 43 blank · 70 comment · 10 complexity · b6d930e5b051ec61ecdd3c516d4d4464 MD5 · raw file

  1. <?php
  2. /**
  3. * SemanticScuttle - your social bookmark manager.
  4. *
  5. * PHP version 5.
  6. *
  7. * @category Bookmarking
  8. * @package SemanticScuttle
  9. * @author Benjamin Huynh-Kim-Bang <mensonge@users.sourceforge.net>
  10. * @author Christian Weiske <cweiske@cweiske.de>
  11. * @author Eric Dane <ericdane@users.sourceforge.net>
  12. * @license GPL http://www.gnu.org/licenses/gpl.html
  13. * @link http://sourceforge.net/projects/semanticscuttle
  14. */
  15. require_once 'HTTP/Request2.php';
  16. /**
  17. * Unit tests for the SemanticScuttle csv export API
  18. *
  19. * @category Bookmarking
  20. * @package SemanticScuttle
  21. * @author Benjamin Huynh-Kim-Bang <mensonge@users.sourceforge.net>
  22. * @author Christian Weiske <cweiske@cweiske.de>
  23. * @author Eric Dane <ericdane@users.sourceforge.net>
  24. * @license GPL http://www.gnu.org/licenses/gpl.html
  25. * @link http://sourceforge.net/projects/semanticscuttle
  26. */
  27. class Api_ExportCsvTest extends TestBaseApi
  28. {
  29. protected $us;
  30. protected $bs;
  31. protected $urlPart = 'api/export_csv.php';
  32. /**
  33. * Test if authentication is required when sending no auth data
  34. */
  35. public function testAuthWithoutAuthData()
  36. {
  37. $req = $this->getRequest(null, false);
  38. $res = $req->send();
  39. $this->assertEquals(401, $res->getStatus());
  40. }
  41. /**
  42. * Test if authentication is required when sending wrong user data
  43. */
  44. public function testAuthWrongCredentials()
  45. {
  46. $req = $this->getRequest(null, false);
  47. $req->setAuth('user', 'password', HTTP_Request2::AUTH_BASIC);
  48. $res = $req->send();
  49. $this->assertEquals(401, $res->getStatus());
  50. }
  51. /**
  52. * Test MIME content type and filename header fields
  53. */
  54. public function testMimeTypeFilename()
  55. {
  56. $res = reset($this->getAuthRequest())->send();
  57. $this->assertEquals(200, $res->getStatus());
  58. //verify MIME content type
  59. $this->assertEquals(
  60. 'application/csv-tab-delimited-table; charset=utf-8',
  61. $res->getHeader('content-type')
  62. );
  63. //we need a file name
  64. $this->assertNotNull($res->getHeader('content-disposition'));
  65. }
  66. /**
  67. * Test CSV export without bookmarks
  68. */
  69. public function testNoBookmarks()
  70. {
  71. list($req, $uid) = $this->getAuthRequest();
  72. $body = $req->send()->getBody();
  73. $csv = $this->getCsvArray($body);
  74. $this->assertEquals(1, count($csv));
  75. $this->assertCsvHeader($csv);
  76. }
  77. /**
  78. * Test CSV export with some bookmarks
  79. */
  80. public function testBookmarks()
  81. {
  82. list($req, $uid) = $this->getAuthRequest();
  83. //public
  84. $this->addBookmark(
  85. $uid, 'http://example.org/testBookmarks', 0,
  86. array('unittest', 'testBookmarks'), 'mytitle'
  87. );
  88. //shared
  89. $this->addBookmark(
  90. $uid, 'http://example.org/testBookmarks-shared', 1,
  91. array('unittest', 'testBookmarks'), 'mytitle-shared'
  92. );
  93. //private
  94. $this->addBookmark(
  95. $uid, 'http://example.org/testBookmarks-private', 2,
  96. array('unittest', 'testBookmarks'), 'mytitle-private'
  97. );
  98. //private other that should not in the export
  99. $this->addBookmark(
  100. null, 'http://example.org/testBookmarks-private2', 2
  101. );
  102. //public bookmark from other people that should not be
  103. // exported, too
  104. $this->addBookmark(
  105. null, 'http://example.org/testBookmarks-other', 0
  106. );
  107. $body = $req->send()->getBody();
  108. $csv = $this->getCsvArray($body);
  109. $this->assertEquals(4, count($csv));
  110. $this->assertCsvHeader($csv);
  111. $this->assertEquals('http://example.org/testBookmarks', $csv[1][0]);
  112. $this->assertEquals('mytitle', $csv[1][1]);
  113. $this->assertEquals('unittest,testbookmarks', $csv[1][2]);
  114. $this->assertEquals('http://example.org/testBookmarks-shared', $csv[2][0]);
  115. $this->assertEquals('mytitle-shared', $csv[2][1]);
  116. $this->assertEquals('unittest,testbookmarks', $csv[2][2]);
  117. $this->assertEquals('http://example.org/testBookmarks-private', $csv[3][0]);
  118. $this->assertEquals('mytitle-private', $csv[3][1]);
  119. $this->assertEquals('unittest,testbookmarks', $csv[3][2]);
  120. }
  121. /**
  122. * Test CSV export with tag filter
  123. */
  124. public function testTagFilter()
  125. {
  126. list($req, $uid) = $this->getAuthRequest('?tag=tag1');
  127. $this->addBookmark(
  128. $uid, 'http://example.org/tag-1', 0,
  129. array('unittest', 'tag1')
  130. );
  131. $this->addBookmark(
  132. $uid, 'http://example.org/tag-2', 0,
  133. array('unittest', 'tag2')
  134. );
  135. $this->addBookmark(
  136. $uid, 'http://example.org/tag-3', 0,
  137. array('unittest', 'tag1', 'tag2')
  138. );
  139. $body = $req->send()->getBody();
  140. $csv = $this->getCsvArray($body);
  141. $this->assertEquals(3, count($csv));
  142. $this->assertCsvHeader($csv);
  143. $this->assertEquals('http://example.org/tag-1', $csv[1][0]);
  144. $this->assertEquals('http://example.org/tag-3', $csv[2][0]);
  145. }
  146. /**
  147. * Test CSV export with tag filter for multiple tags
  148. */
  149. public function testTagFilterMultiple()
  150. {
  151. list($req, $uid) = $this->getAuthRequest('?tag=tag1+tag2');
  152. $this->addBookmark(
  153. $uid, 'http://example.org/tag-1', 0,
  154. array('unittest', 'tag1')
  155. );
  156. $this->addBookmark(
  157. $uid, 'http://example.org/tag-2', 0,
  158. array('unittest', 'tag2')
  159. );
  160. $this->addBookmark(
  161. $uid, 'http://example.org/tag-3', 0,
  162. array('unittest', 'tag1', 'tag2')
  163. );
  164. $body = $req->send()->getBody();
  165. $csv = $this->getCsvArray($body);
  166. $this->assertEquals(2, count($csv));
  167. $this->assertCsvHeader($csv);
  168. $this->assertEquals('http://example.org/tag-3', $csv[1][0]);
  169. }
  170. /**
  171. * Asserts that the CSV array contains the correct header
  172. *
  173. * @param array $csv CSV array from getCsvArray()
  174. *
  175. * @return void
  176. */
  177. protected function assertCsvHeader($csv)
  178. {
  179. $this->assertEquals(
  180. array('url', 'title', 'tags', 'description'),
  181. $csv[0]
  182. );
  183. }
  184. /**
  185. * Converts a string of CSV data to an array
  186. *
  187. * @param string $body String containing the full CSV file
  188. *
  189. * @return array Array of CSV data
  190. */
  191. protected function getCsvArray($body)
  192. {
  193. $v53 = (version_compare(PHP_VERSION, '5.3.0') === 1);
  194. //dead simple implementation that does not work with
  195. // advanced CSV files
  196. $ar = array();
  197. foreach (explode("\n", $body) as $line) {
  198. if ($v53) {
  199. $ar[] = str_getcsv($line, ';');
  200. } else {
  201. $arl = explode(';', $line);
  202. foreach ($arl as &$str) {
  203. if (substr($str, 0, 1) == '"'
  204. && substr($str, -1) == '"'
  205. ) {
  206. $str = substr($str, 1, -1);
  207. }
  208. }
  209. $ar[] = $arl;
  210. }
  211. }
  212. if (count(end($ar)) == 1 && reset(end($ar)) == '') {
  213. unset($ar[key($ar)]);
  214. }
  215. return $ar;
  216. }
  217. }
  218. ?>