GetLinksTest.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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->container['history'] = null;
  55. $this->controller = new Links($this->container);
  56. }
  57. /**
  58. * After every test, remove the test datastore.
  59. */
  60. public function tearDown()
  61. {
  62. @unlink(self::$testDatastore);
  63. }
  64. /**
  65. * Test basic getLinks service: returns all links.
  66. */
  67. public function testGetLinks()
  68. {
  69. // Used by index_url().
  70. $_SERVER['SERVER_NAME'] = 'domain.tld';
  71. $_SERVER['SERVER_PORT'] = 80;
  72. $_SERVER['SCRIPT_NAME'] = '/';
  73. $env = Environment::mock([
  74. 'REQUEST_METHOD' => 'GET',
  75. ]);
  76. $request = Request::createFromEnvironment($env);
  77. $response = $this->controller->getLinks($request, new Response());
  78. $this->assertEquals(200, $response->getStatusCode());
  79. $data = json_decode((string) $response->getBody(), true);
  80. $this->assertEquals($this->refDB->countLinks(), count($data));
  81. // Check order
  82. $order = [10, 11, 41, 8, 6, 7, 0, 1, 9, 4, 42];
  83. $cpt = 0;
  84. foreach ($data as $link) {
  85. $this->assertEquals(self::NB_FIELDS_LINK, count($link));
  86. $this->assertEquals($order[$cpt++], $link['id']);
  87. }
  88. // Check first element fields
  89. $first = $data[2];
  90. $this->assertEquals('http://domain.tld/?WDWyig', $first['url']);
  91. $this->assertEquals('WDWyig', $first['shorturl']);
  92. $this->assertEquals('Link title: @website', $first['title']);
  93. $this->assertEquals(
  94. 'Stallman has a beard and is part of the Free Software Foundation (or not). Seriously, read this. #hashtag',
  95. $first['description']
  96. );
  97. $this->assertEquals('sTuff', $first['tags'][0]);
  98. $this->assertEquals(false, $first['private']);
  99. $this->assertEquals(
  100. \DateTime::createFromFormat(\LinkDB::LINK_DATE_FORMAT, '20150310_114651')->format(\DateTime::ATOM),
  101. $first['created']
  102. );
  103. $this->assertEmpty($first['updated']);
  104. // Multi tags
  105. $link = $data[3];
  106. $this->assertEquals(7, count($link['tags']));
  107. // Update date
  108. $this->assertEquals(
  109. \DateTime::createFromFormat(\LinkDB::LINK_DATE_FORMAT, '20160803_093033')->format(\DateTime::ATOM),
  110. $link['updated']
  111. );
  112. }
  113. /**
  114. * Test getLinks service with offset and limit parameter:
  115. * limit=1 and offset=1 should return only the second link, ID=8 (ordered by creation date DESC).
  116. */
  117. public function testGetLinksOffsetLimit()
  118. {
  119. $env = Environment::mock([
  120. 'REQUEST_METHOD' => 'GET',
  121. 'QUERY_STRING' => 'offset=3&limit=1'
  122. ]);
  123. $request = Request::createFromEnvironment($env);
  124. $response = $this->controller->getLinks($request, new Response());
  125. $this->assertEquals(200, $response->getStatusCode());
  126. $data = json_decode((string) $response->getBody(), true);
  127. $this->assertEquals(1, count($data));
  128. $this->assertEquals(8, $data[0]['id']);
  129. $this->assertEquals(self::NB_FIELDS_LINK, count($data[0]));
  130. }
  131. /**
  132. * Test getLinks with limit=all (return all link).
  133. */
  134. public function testGetLinksLimitAll()
  135. {
  136. $env = Environment::mock([
  137. 'REQUEST_METHOD' => 'GET',
  138. 'QUERY_STRING' => 'limit=all'
  139. ]);
  140. $request = Request::createFromEnvironment($env);
  141. $response = $this->controller->getLinks($request, new Response());
  142. $this->assertEquals(200, $response->getStatusCode());
  143. $data = json_decode((string) $response->getBody(), true);
  144. $this->assertEquals($this->refDB->countLinks(), count($data));
  145. // Check order
  146. $order = [10, 11, 41, 8, 6, 7, 0, 1, 9, 4, 42];
  147. $cpt = 0;
  148. foreach ($data as $link) {
  149. $this->assertEquals(self::NB_FIELDS_LINK, count($link));
  150. $this->assertEquals($order[$cpt++], $link['id']);
  151. }
  152. }
  153. /**
  154. * Test getLinks service with offset and limit parameter:
  155. * limit=1 and offset=1 should return only the second link, ID=8 (ordered by creation date DESC).
  156. */
  157. public function testGetLinksOffsetTooHigh()
  158. {
  159. $env = Environment::mock([
  160. 'REQUEST_METHOD' => 'GET',
  161. 'QUERY_STRING' => 'offset=100'
  162. ]);
  163. $request = Request::createFromEnvironment($env);
  164. $response = $this->controller->getLinks($request, new Response());
  165. $this->assertEquals(200, $response->getStatusCode());
  166. $data = json_decode((string) $response->getBody(), true);
  167. $this->assertEmpty(count($data));
  168. }
  169. /**
  170. * Test getLinks with visibility parameter set to all
  171. */
  172. public function testGetLinksVisibilityAll()
  173. {
  174. $env = Environment::mock(
  175. [
  176. 'REQUEST_METHOD' => 'GET',
  177. 'QUERY_STRING' => 'visibility=all'
  178. ]
  179. );
  180. $request = Request::createFromEnvironment($env);
  181. $response = $this->controller->getLinks($request, new Response());
  182. $this->assertEquals(200, $response->getStatusCode());
  183. $data = json_decode((string)$response->getBody(), true);
  184. $this->assertEquals($this->refDB->countLinks(), count($data));
  185. $this->assertEquals(10, $data[0]['id']);
  186. $this->assertEquals(41, $data[2]['id']);
  187. $this->assertEquals(self::NB_FIELDS_LINK, count($data[0]));
  188. }
  189. /**
  190. * Test getLinks with visibility parameter set to private
  191. */
  192. public function testGetLinksVisibilityPrivate()
  193. {
  194. $env = Environment::mock([
  195. 'REQUEST_METHOD' => 'GET',
  196. 'QUERY_STRING' => 'visibility=private'
  197. ]);
  198. $request = Request::createFromEnvironment($env);
  199. $response = $this->controller->getLinks($request, new Response());
  200. $this->assertEquals(200, $response->getStatusCode());
  201. $data = json_decode((string) $response->getBody(), true);
  202. $this->assertEquals($this->refDB->countPrivateLinks(), count($data));
  203. $this->assertEquals(6, $data[0]['id']);
  204. $this->assertEquals(self::NB_FIELDS_LINK, count($data[0]));
  205. }
  206. /**
  207. * Test getLinks with visibility parameter set to public
  208. */
  209. public function testGetLinksVisibilityPublic()
  210. {
  211. $env = Environment::mock(
  212. [
  213. 'REQUEST_METHOD' => 'GET',
  214. 'QUERY_STRING' => 'visibility=public'
  215. ]
  216. );
  217. $request = Request::createFromEnvironment($env);
  218. $response = $this->controller->getLinks($request, new Response());
  219. $this->assertEquals(200, $response->getStatusCode());
  220. $data = json_decode((string)$response->getBody(), true);
  221. $this->assertEquals($this->refDB->countPublicLinks(), count($data));
  222. $this->assertEquals(10, $data[0]['id']);
  223. $this->assertEquals(41, $data[2]['id']);
  224. $this->assertEquals(self::NB_FIELDS_LINK, count($data[0]));
  225. }
  226. /**
  227. * Test getLinks service with offset and limit parameter:
  228. * limit=1 and offset=1 should return only the second link, ID=8 (ordered by creation date DESC).
  229. */
  230. public function testGetLinksSearchTerm()
  231. {
  232. // Only in description - 1 result
  233. $env = Environment::mock([
  234. 'REQUEST_METHOD' => 'GET',
  235. 'QUERY_STRING' => 'searchterm=Tropical'
  236. ]);
  237. $request = Request::createFromEnvironment($env);
  238. $response = $this->controller->getLinks($request, new Response());
  239. $this->assertEquals(200, $response->getStatusCode());
  240. $data = json_decode((string) $response->getBody(), true);
  241. $this->assertEquals(1, count($data));
  242. $this->assertEquals(1, $data[0]['id']);
  243. $this->assertEquals(self::NB_FIELDS_LINK, count($data[0]));
  244. // Only in tags - 1 result
  245. $env = Environment::mock([
  246. 'REQUEST_METHOD' => 'GET',
  247. 'QUERY_STRING' => 'searchterm=tag3'
  248. ]);
  249. $request = Request::createFromEnvironment($env);
  250. $response = $this->controller->getLinks($request, new Response());
  251. $this->assertEquals(200, $response->getStatusCode());
  252. $data = json_decode((string) $response->getBody(), true);
  253. $this->assertEquals(1, count($data));
  254. $this->assertEquals(0, $data[0]['id']);
  255. $this->assertEquals(self::NB_FIELDS_LINK, count($data[0]));
  256. // Multiple results (2)
  257. $env = Environment::mock([
  258. 'REQUEST_METHOD' => 'GET',
  259. 'QUERY_STRING' => 'searchterm=stallman'
  260. ]);
  261. $request = Request::createFromEnvironment($env);
  262. $response = $this->controller->getLinks($request, new Response());
  263. $this->assertEquals(200, $response->getStatusCode());
  264. $data = json_decode((string) $response->getBody(), true);
  265. $this->assertEquals(2, count($data));
  266. $this->assertEquals(41, $data[0]['id']);
  267. $this->assertEquals(self::NB_FIELDS_LINK, count($data[0]));
  268. $this->assertEquals(8, $data[1]['id']);
  269. $this->assertEquals(self::NB_FIELDS_LINK, count($data[1]));
  270. // Multiword - 2 results
  271. $env = Environment::mock([
  272. 'REQUEST_METHOD' => 'GET',
  273. 'QUERY_STRING' => 'searchterm=stallman+software'
  274. ]);
  275. $request = Request::createFromEnvironment($env);
  276. $response = $this->controller->getLinks($request, new Response());
  277. $this->assertEquals(200, $response->getStatusCode());
  278. $data = json_decode((string) $response->getBody(), true);
  279. $this->assertEquals(2, count($data));
  280. $this->assertEquals(41, $data[0]['id']);
  281. $this->assertEquals(self::NB_FIELDS_LINK, count($data[0]));
  282. $this->assertEquals(8, $data[1]['id']);
  283. $this->assertEquals(self::NB_FIELDS_LINK, count($data[1]));
  284. // URL encoding
  285. $env = Environment::mock([
  286. 'REQUEST_METHOD' => 'GET',
  287. 'QUERY_STRING' => 'searchterm='. urlencode('@web')
  288. ]);
  289. $request = Request::createFromEnvironment($env);
  290. $response = $this->controller->getLinks($request, new Response());
  291. $this->assertEquals(200, $response->getStatusCode());
  292. $data = json_decode((string) $response->getBody(), true);
  293. $this->assertEquals(2, count($data));
  294. $this->assertEquals(41, $data[0]['id']);
  295. $this->assertEquals(self::NB_FIELDS_LINK, count($data[0]));
  296. $this->assertEquals(8, $data[1]['id']);
  297. $this->assertEquals(self::NB_FIELDS_LINK, count($data[1]));
  298. }
  299. public function testGetLinksSearchTermNoResult()
  300. {
  301. $env = Environment::mock([
  302. 'REQUEST_METHOD' => 'GET',
  303. 'QUERY_STRING' => 'searchterm=nope'
  304. ]);
  305. $request = Request::createFromEnvironment($env);
  306. $response = $this->controller->getLinks($request, new Response());
  307. $this->assertEquals(200, $response->getStatusCode());
  308. $data = json_decode((string) $response->getBody(), true);
  309. $this->assertEquals(0, count($data));
  310. }
  311. public function testGetLinksSearchTags()
  312. {
  313. // Single tag
  314. $env = Environment::mock([
  315. 'REQUEST_METHOD' => 'GET',
  316. 'QUERY_STRING' => 'searchtags=dev',
  317. ]);
  318. $request = Request::createFromEnvironment($env);
  319. $response = $this->controller->getLinks($request, new Response());
  320. $this->assertEquals(200, $response->getStatusCode());
  321. $data = json_decode((string) $response->getBody(), true);
  322. $this->assertEquals(2, count($data));
  323. $this->assertEquals(0, $data[0]['id']);
  324. $this->assertEquals(self::NB_FIELDS_LINK, count($data[0]));
  325. $this->assertEquals(4, $data[1]['id']);
  326. $this->assertEquals(self::NB_FIELDS_LINK, count($data[1]));
  327. // Multitag + exclude
  328. $env = Environment::mock([
  329. 'REQUEST_METHOD' => 'GET',
  330. 'QUERY_STRING' => 'searchtags=stuff+-gnu',
  331. ]);
  332. $request = Request::createFromEnvironment($env);
  333. $response = $this->controller->getLinks($request, new Response());
  334. $this->assertEquals(200, $response->getStatusCode());
  335. $data = json_decode((string) $response->getBody(), true);
  336. $this->assertEquals(1, count($data));
  337. $this->assertEquals(41, $data[0]['id']);
  338. $this->assertEquals(self::NB_FIELDS_LINK, count($data[0]));
  339. // wildcard: placeholder at the start
  340. $env = Environment::mock([
  341. 'REQUEST_METHOD' => 'GET',
  342. 'QUERY_STRING' => 'searchtags=*Tuff',
  343. ]);
  344. $request = Request::createFromEnvironment($env);
  345. $response = $this->controller->getLinks($request, new Response());
  346. $this->assertEquals(200, $response->getStatusCode());
  347. $data = json_decode((string) $response->getBody(), true);
  348. $this->assertEquals(2, count($data));
  349. $this->assertEquals(41, $data[0]['id']);
  350. // wildcard: placeholder at the end
  351. $env = Environment::mock([
  352. 'REQUEST_METHOD' => 'GET',
  353. 'QUERY_STRING' => 'searchtags=c*',
  354. ]);
  355. $request = Request::createFromEnvironment($env);
  356. $response = $this->controller->getLinks($request, new Response());
  357. $this->assertEquals(200, $response->getStatusCode());
  358. $data = json_decode((string) $response->getBody(), true);
  359. $this->assertEquals(4, count($data));
  360. $this->assertEquals(6, $data[0]['id']);
  361. // wildcard: placeholder at the middle
  362. $env = Environment::mock([
  363. 'REQUEST_METHOD' => 'GET',
  364. 'QUERY_STRING' => 'searchtags=w*b',
  365. ]);
  366. $request = Request::createFromEnvironment($env);
  367. $response = $this->controller->getLinks($request, new Response());
  368. $this->assertEquals(200, $response->getStatusCode());
  369. $data = json_decode((string) $response->getBody(), true);
  370. $this->assertEquals(4, count($data));
  371. $this->assertEquals(6, $data[0]['id']);
  372. // wildcard: match all
  373. $env = Environment::mock([
  374. 'REQUEST_METHOD' => 'GET',
  375. 'QUERY_STRING' => 'searchtags=*',
  376. ]);
  377. $request = Request::createFromEnvironment($env);
  378. $response = $this->controller->getLinks($request, new Response());
  379. $this->assertEquals(200, $response->getStatusCode());
  380. $data = json_decode((string) $response->getBody(), true);
  381. $this->assertEquals(\ReferenceLinkDB::$NB_LINKS_TOTAL, count($data));
  382. $this->assertEquals(10, $data[0]['id']);
  383. $this->assertEquals(41, $data[2]['id']);
  384. // wildcard: optional ('*' does not need to expand)
  385. $env = Environment::mock([
  386. 'REQUEST_METHOD' => 'GET',
  387. 'QUERY_STRING' => 'searchtags=*stuff*',
  388. ]);
  389. $request = Request::createFromEnvironment($env);
  390. $response = $this->controller->getLinks($request, new Response());
  391. $this->assertEquals(200, $response->getStatusCode());
  392. $data = json_decode((string) $response->getBody(), true);
  393. $this->assertEquals(2, count($data));
  394. $this->assertEquals(41, $data[0]['id']);
  395. // wildcard: exclusions
  396. $env = Environment::mock([
  397. 'REQUEST_METHOD' => 'GET',
  398. 'QUERY_STRING' => 'searchtags=*a*+-*e*',
  399. ]);
  400. $request = Request::createFromEnvironment($env);
  401. $response = $this->controller->getLinks($request, new Response());
  402. $this->assertEquals(200, $response->getStatusCode());
  403. $data = json_decode((string) $response->getBody(), true);
  404. $this->assertEquals(1, count($data));
  405. $this->assertEquals(41, $data[0]['id']); // finds '#hashtag' in descr.
  406. // wildcard: exclude all
  407. $env = Environment::mock([
  408. 'REQUEST_METHOD' => 'GET',
  409. 'QUERY_STRING' => 'searchtags=-*',
  410. ]);
  411. $request = Request::createFromEnvironment($env);
  412. $response = $this->controller->getLinks($request, new Response());
  413. $this->assertEquals(200, $response->getStatusCode());
  414. $data = json_decode((string) $response->getBody(), true);
  415. $this->assertEquals(0, count($data));
  416. }
  417. /**
  418. * Test getLinks service with search tags+terms.
  419. */
  420. public function testGetLinksSearchTermsAndTags()
  421. {
  422. $env = Environment::mock([
  423. 'REQUEST_METHOD' => 'GET',
  424. 'QUERY_STRING' => 'searchterm=poke&searchtags=dev',
  425. ]);
  426. $request = Request::createFromEnvironment($env);
  427. $response = $this->controller->getLinks($request, new Response());
  428. $this->assertEquals(200, $response->getStatusCode());
  429. $data = json_decode((string) $response->getBody(), true);
  430. $this->assertEquals(1, count($data));
  431. $this->assertEquals(0, $data[0]['id']);
  432. $this->assertEquals(self::NB_FIELDS_LINK, count($data[0]));
  433. }
  434. }