GetLinksTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. <?php
  2. namespace Shaarli\Api\Controllers;
  3. use Shaarli\Config\ConfigManager;
  4. use Slim\Container;
  5. use Slim\Http\Environment;
  6. use Slim\Http\Request;
  7. use Slim\Http\Response;
  8. /**
  9. * Class GetLinksTest
  10. *
  11. * Test get Link list REST API service.
  12. *
  13. * @see http://shaarli.github.io/api-documentation/#links-links-collection-get
  14. *
  15. * @package Shaarli\Api\Controllers
  16. */
  17. class GetLinksTest extends \PHPUnit_Framework_TestCase
  18. {
  19. /**
  20. * @var string datastore to test write operations
  21. */
  22. protected static $testDatastore = 'sandbox/datastore.php';
  23. /**
  24. * @var ConfigManager instance
  25. */
  26. protected $conf;
  27. /**
  28. * @var \ReferenceLinkDB instance.
  29. */
  30. protected $refDB = null;
  31. /**
  32. * @var Container instance.
  33. */
  34. protected $container;
  35. /**
  36. * @var Links controller instance.
  37. */
  38. protected $controller;
  39. /**
  40. * Number of JSON field per link.
  41. */
  42. const NB_FIELDS_LINK = 9;
  43. /**
  44. * Before every test, instantiate a new Api with its config, plugins and links.
  45. */
  46. public function setUp()
  47. {
  48. $this->conf = new ConfigManager('tests/utils/config/configJson');
  49. $this->refDB = new \ReferenceLinkDB();
  50. $this->refDB->write(self::$testDatastore);
  51. $this->container = new Container();
  52. $this->container['conf'] = $this->conf;
  53. $this->container['db'] = new \LinkDB(self::$testDatastore, true, false);
  54. $this->controller = new Links($this->container);
  55. }
  56. /**
  57. * After every test, remove the test datastore.
  58. */
  59. public function tearDown()
  60. {
  61. @unlink(self::$testDatastore);
  62. }
  63. /**
  64. * Test basic getLinks service: returns all links.
  65. */
  66. public function testGetLinks()
  67. {
  68. // Used by index_url().
  69. $_SERVER['SERVER_NAME'] = 'domain.tld';
  70. $_SERVER['SERVER_PORT'] = 80;
  71. $_SERVER['SCRIPT_NAME'] = '/';
  72. $env = Environment::mock([
  73. 'REQUEST_METHOD' => 'GET',
  74. ]);
  75. $request = Request::createFromEnvironment($env);
  76. $response = $this->controller->getLinks($request, new Response());
  77. $this->assertEquals(200, $response->getStatusCode());
  78. $data = json_decode((string) $response->getBody(), true);
  79. $this->assertEquals($this->refDB->countLinks(), count($data));
  80. // Check order
  81. $order = [41, 8, 6, 7, 0, 1, 4, 42];
  82. $cpt = 0;
  83. foreach ($data as $link) {
  84. $this->assertEquals(self::NB_FIELDS_LINK, count($link));
  85. $this->assertEquals($order[$cpt++], $link['id']);
  86. }
  87. // Check first element fields
  88. $first = $data[0];
  89. $this->assertEquals('http://domain.tld/?WDWyig', $first['url']);
  90. $this->assertEquals('WDWyig', $first['shorturl']);
  91. $this->assertEquals('Link title: @website', $first['title']);
  92. $this->assertEquals(
  93. 'Stallman has a beard and is part of the Free Software Foundation (or not). Seriously, read this. #hashtag',
  94. $first['description']
  95. );
  96. $this->assertEquals('sTuff', $first['tags'][0]);
  97. $this->assertEquals(false, $first['private']);
  98. $this->assertEquals(
  99. \DateTime::createFromFormat(\LinkDB::LINK_DATE_FORMAT, '20150310_114651')->format(\DateTime::ATOM),
  100. $first['created']
  101. );
  102. $this->assertEmpty($first['updated']);
  103. // Multi tags
  104. $link = $data[1];
  105. $this->assertEquals(7, count($link['tags']));
  106. // Update date
  107. $this->assertEquals(
  108. \DateTime::createFromFormat(\LinkDB::LINK_DATE_FORMAT, '20160803_093033')->format(\DateTime::ATOM),
  109. $link['updated']
  110. );
  111. }
  112. /**
  113. * Test getLinks service with offset and limit parameter:
  114. * limit=1 and offset=1 should return only the second link, ID=8 (ordered by creation date DESC).
  115. */
  116. public function testGetLinksOffsetLimit()
  117. {
  118. $env = Environment::mock([
  119. 'REQUEST_METHOD' => 'GET',
  120. 'QUERY_STRING' => 'offset=1&limit=1'
  121. ]);
  122. $request = Request::createFromEnvironment($env);
  123. $response = $this->controller->getLinks($request, new Response());
  124. $this->assertEquals(200, $response->getStatusCode());
  125. $data = json_decode((string) $response->getBody(), true);
  126. $this->assertEquals(1, count($data));
  127. $this->assertEquals(8, $data[0]['id']);
  128. $this->assertEquals(self::NB_FIELDS_LINK, count($data[0]));
  129. }
  130. /**
  131. * Test getLinks with limit=all (return all link).
  132. */
  133. public function testGetLinksLimitAll()
  134. {
  135. $env = Environment::mock([
  136. 'REQUEST_METHOD' => 'GET',
  137. 'QUERY_STRING' => 'limit=all'
  138. ]);
  139. $request = Request::createFromEnvironment($env);
  140. $response = $this->controller->getLinks($request, new Response());
  141. $this->assertEquals(200, $response->getStatusCode());
  142. $data = json_decode((string) $response->getBody(), true);
  143. $this->assertEquals($this->refDB->countLinks(), count($data));
  144. // Check order
  145. $order = [41, 8, 6, 7, 0, 1, 4, 42];
  146. $cpt = 0;
  147. foreach ($data as $link) {
  148. $this->assertEquals(self::NB_FIELDS_LINK, count($link));
  149. $this->assertEquals($order[$cpt++], $link['id']);
  150. }
  151. }
  152. /**
  153. * Test getLinks service with offset and limit parameter:
  154. * limit=1 and offset=1 should return only the second link, ID=8 (ordered by creation date DESC).
  155. */
  156. public function testGetLinksOffsetTooHigh()
  157. {
  158. $env = Environment::mock([
  159. 'REQUEST_METHOD' => 'GET',
  160. 'QUERY_STRING' => 'offset=100'
  161. ]);
  162. $request = Request::createFromEnvironment($env);
  163. $response = $this->controller->getLinks($request, new Response());
  164. $this->assertEquals(200, $response->getStatusCode());
  165. $data = json_decode((string) $response->getBody(), true);
  166. $this->assertEmpty(count($data));
  167. }
  168. /**
  169. * Test getLinks with visibility parameter set to all
  170. */
  171. public function testGetLinksVisibilityAll()
  172. {
  173. $env = Environment::mock(
  174. [
  175. 'REQUEST_METHOD' => 'GET',
  176. 'QUERY_STRING' => 'visibility=all'
  177. ]
  178. );
  179. $request = Request::createFromEnvironment($env);
  180. $response = $this->controller->getLinks($request, new Response());
  181. $this->assertEquals(200, $response->getStatusCode());
  182. $data = json_decode((string)$response->getBody(), true);
  183. $this->assertEquals($this->refDB->countLinks(), count($data));
  184. $this->assertEquals(41, $data[0]['id']);
  185. $this->assertEquals(self::NB_FIELDS_LINK, count($data[0]));
  186. }
  187. /**
  188. * Test getLinks with visibility parameter set to private
  189. */
  190. public function testGetLinksVisibilityPrivate()
  191. {
  192. $env = Environment::mock([
  193. 'REQUEST_METHOD' => 'GET',
  194. 'QUERY_STRING' => 'visibility=private'
  195. ]);
  196. $request = Request::createFromEnvironment($env);
  197. $response = $this->controller->getLinks($request, new Response());
  198. $this->assertEquals(200, $response->getStatusCode());
  199. $data = json_decode((string) $response->getBody(), true);
  200. $this->assertEquals($this->refDB->countPrivateLinks(), count($data));
  201. $this->assertEquals(6, $data[0]['id']);
  202. $this->assertEquals(self::NB_FIELDS_LINK, count($data[0]));
  203. }
  204. /**
  205. * Test getLinks with visibility parameter set to public
  206. */
  207. public function testGetLinksVisibilityPublic()
  208. {
  209. $env = Environment::mock(
  210. [
  211. 'REQUEST_METHOD' => 'GET',
  212. 'QUERY_STRING' => 'visibility=public'
  213. ]
  214. );
  215. $request = Request::createFromEnvironment($env);
  216. $response = $this->controller->getLinks($request, new Response());
  217. $this->assertEquals(200, $response->getStatusCode());
  218. $data = json_decode((string)$response->getBody(), true);
  219. $this->assertEquals($this->refDB->countPublicLinks(), count($data));
  220. $this->assertEquals(41, $data[0]['id']);
  221. $this->assertEquals(self::NB_FIELDS_LINK, count($data[0]));
  222. }
  223. /**
  224. * Test getLinks service with offset and limit parameter:
  225. * limit=1 and offset=1 should return only the second link, ID=8 (ordered by creation date DESC).
  226. */
  227. public function testGetLinksSearchTerm()
  228. {
  229. // Only in description - 1 result
  230. $env = Environment::mock([
  231. 'REQUEST_METHOD' => 'GET',
  232. 'QUERY_STRING' => 'searchterm=Tropical'
  233. ]);
  234. $request = Request::createFromEnvironment($env);
  235. $response = $this->controller->getLinks($request, new Response());
  236. $this->assertEquals(200, $response->getStatusCode());
  237. $data = json_decode((string) $response->getBody(), true);
  238. $this->assertEquals(1, count($data));
  239. $this->assertEquals(1, $data[0]['id']);
  240. $this->assertEquals(self::NB_FIELDS_LINK, count($data[0]));
  241. // Only in tags - 1 result
  242. $env = Environment::mock([
  243. 'REQUEST_METHOD' => 'GET',
  244. 'QUERY_STRING' => 'searchterm=tag3'
  245. ]);
  246. $request = Request::createFromEnvironment($env);
  247. $response = $this->controller->getLinks($request, new Response());
  248. $this->assertEquals(200, $response->getStatusCode());
  249. $data = json_decode((string) $response->getBody(), true);
  250. $this->assertEquals(1, count($data));
  251. $this->assertEquals(0, $data[0]['id']);
  252. $this->assertEquals(self::NB_FIELDS_LINK, count($data[0]));
  253. // Multiple results (2)
  254. $env = Environment::mock([
  255. 'REQUEST_METHOD' => 'GET',
  256. 'QUERY_STRING' => 'searchterm=stallman'
  257. ]);
  258. $request = Request::createFromEnvironment($env);
  259. $response = $this->controller->getLinks($request, new Response());
  260. $this->assertEquals(200, $response->getStatusCode());
  261. $data = json_decode((string) $response->getBody(), true);
  262. $this->assertEquals(2, count($data));
  263. $this->assertEquals(41, $data[0]['id']);
  264. $this->assertEquals(self::NB_FIELDS_LINK, count($data[0]));
  265. $this->assertEquals(8, $data[1]['id']);
  266. $this->assertEquals(self::NB_FIELDS_LINK, count($data[1]));
  267. // Multiword - 2 results
  268. $env = Environment::mock([
  269. 'REQUEST_METHOD' => 'GET',
  270. 'QUERY_STRING' => 'searchterm=stallman+software'
  271. ]);
  272. $request = Request::createFromEnvironment($env);
  273. $response = $this->controller->getLinks($request, new Response());
  274. $this->assertEquals(200, $response->getStatusCode());
  275. $data = json_decode((string) $response->getBody(), true);
  276. $this->assertEquals(2, count($data));
  277. $this->assertEquals(41, $data[0]['id']);
  278. $this->assertEquals(self::NB_FIELDS_LINK, count($data[0]));
  279. $this->assertEquals(8, $data[1]['id']);
  280. $this->assertEquals(self::NB_FIELDS_LINK, count($data[1]));
  281. // URL encoding
  282. $env = Environment::mock([
  283. 'REQUEST_METHOD' => 'GET',
  284. 'QUERY_STRING' => 'searchterm='. urlencode('@web')
  285. ]);
  286. $request = Request::createFromEnvironment($env);
  287. $response = $this->controller->getLinks($request, new Response());
  288. $this->assertEquals(200, $response->getStatusCode());
  289. $data = json_decode((string) $response->getBody(), true);
  290. $this->assertEquals(2, count($data));
  291. $this->assertEquals(41, $data[0]['id']);
  292. $this->assertEquals(self::NB_FIELDS_LINK, count($data[0]));
  293. $this->assertEquals(8, $data[1]['id']);
  294. $this->assertEquals(self::NB_FIELDS_LINK, count($data[1]));
  295. }
  296. public function testGetLinksSearchTermNoResult()
  297. {
  298. $env = Environment::mock([
  299. 'REQUEST_METHOD' => 'GET',
  300. 'QUERY_STRING' => 'searchterm=nope'
  301. ]);
  302. $request = Request::createFromEnvironment($env);
  303. $response = $this->controller->getLinks($request, new Response());
  304. $this->assertEquals(200, $response->getStatusCode());
  305. $data = json_decode((string) $response->getBody(), true);
  306. $this->assertEquals(0, count($data));
  307. }
  308. public function testGetLinksSearchTags()
  309. {
  310. // Single tag
  311. $env = Environment::mock([
  312. 'REQUEST_METHOD' => 'GET',
  313. 'QUERY_STRING' => 'searchtags=dev',
  314. ]);
  315. $request = Request::createFromEnvironment($env);
  316. $response = $this->controller->getLinks($request, new Response());
  317. $this->assertEquals(200, $response->getStatusCode());
  318. $data = json_decode((string) $response->getBody(), true);
  319. $this->assertEquals(2, count($data));
  320. $this->assertEquals(0, $data[0]['id']);
  321. $this->assertEquals(self::NB_FIELDS_LINK, count($data[0]));
  322. $this->assertEquals(4, $data[1]['id']);
  323. $this->assertEquals(self::NB_FIELDS_LINK, count($data[1]));
  324. // Multitag + exclude
  325. $env = Environment::mock([
  326. 'REQUEST_METHOD' => 'GET',
  327. 'QUERY_STRING' => 'searchtags=stuff+-gnu',
  328. ]);
  329. $request = Request::createFromEnvironment($env);
  330. $response = $this->controller->getLinks($request, new Response());
  331. $this->assertEquals(200, $response->getStatusCode());
  332. $data = json_decode((string) $response->getBody(), true);
  333. $this->assertEquals(1, count($data));
  334. $this->assertEquals(41, $data[0]['id']);
  335. $this->assertEquals(self::NB_FIELDS_LINK, count($data[0]));
  336. }
  337. /**
  338. * Test getLinks service with search tags+terms.
  339. */
  340. public function testGetLinksSearchTermsAndTags()
  341. {
  342. $env = Environment::mock([
  343. 'REQUEST_METHOD' => 'GET',
  344. 'QUERY_STRING' => 'searchterm=poke&searchtags=dev',
  345. ]);
  346. $request = Request::createFromEnvironment($env);
  347. $response = $this->controller->getLinks($request, new Response());
  348. $this->assertEquals(200, $response->getStatusCode());
  349. $data = json_decode((string) $response->getBody(), true);
  350. $this->assertEquals(1, count($data));
  351. $this->assertEquals(0, $data[0]['id']);
  352. $this->assertEquals(self::NB_FIELDS_LINK, count($data[0]));
  353. }
  354. }