GetLinksTest.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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 = [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[0];
  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[1];
  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=1&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 = [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(41, $data[0]['id']);
  186. $this->assertEquals(self::NB_FIELDS_LINK, count($data[0]));
  187. }
  188. /**
  189. * Test getLinks with visibility parameter set to private
  190. */
  191. public function testGetLinksVisibilityPrivate()
  192. {
  193. $env = Environment::mock([
  194. 'REQUEST_METHOD' => 'GET',
  195. 'QUERY_STRING' => 'visibility=private'
  196. ]);
  197. $request = Request::createFromEnvironment($env);
  198. $response = $this->controller->getLinks($request, new Response());
  199. $this->assertEquals(200, $response->getStatusCode());
  200. $data = json_decode((string) $response->getBody(), true);
  201. $this->assertEquals($this->refDB->countPrivateLinks(), count($data));
  202. $this->assertEquals(6, $data[0]['id']);
  203. $this->assertEquals(self::NB_FIELDS_LINK, count($data[0]));
  204. }
  205. /**
  206. * Test getLinks with visibility parameter set to public
  207. */
  208. public function testGetLinksVisibilityPublic()
  209. {
  210. $env = Environment::mock(
  211. [
  212. 'REQUEST_METHOD' => 'GET',
  213. 'QUERY_STRING' => 'visibility=public'
  214. ]
  215. );
  216. $request = Request::createFromEnvironment($env);
  217. $response = $this->controller->getLinks($request, new Response());
  218. $this->assertEquals(200, $response->getStatusCode());
  219. $data = json_decode((string)$response->getBody(), true);
  220. $this->assertEquals($this->refDB->countPublicLinks(), count($data));
  221. $this->assertEquals(41, $data[0]['id']);
  222. $this->assertEquals(self::NB_FIELDS_LINK, count($data[0]));
  223. }
  224. /**
  225. * Test getLinks service with offset and limit parameter:
  226. * limit=1 and offset=1 should return only the second link, ID=8 (ordered by creation date DESC).
  227. */
  228. public function testGetLinksSearchTerm()
  229. {
  230. // Only in description - 1 result
  231. $env = Environment::mock([
  232. 'REQUEST_METHOD' => 'GET',
  233. 'QUERY_STRING' => 'searchterm=Tropical'
  234. ]);
  235. $request = Request::createFromEnvironment($env);
  236. $response = $this->controller->getLinks($request, new Response());
  237. $this->assertEquals(200, $response->getStatusCode());
  238. $data = json_decode((string) $response->getBody(), true);
  239. $this->assertEquals(1, count($data));
  240. $this->assertEquals(1, $data[0]['id']);
  241. $this->assertEquals(self::NB_FIELDS_LINK, count($data[0]));
  242. // Only in tags - 1 result
  243. $env = Environment::mock([
  244. 'REQUEST_METHOD' => 'GET',
  245. 'QUERY_STRING' => 'searchterm=tag3'
  246. ]);
  247. $request = Request::createFromEnvironment($env);
  248. $response = $this->controller->getLinks($request, new Response());
  249. $this->assertEquals(200, $response->getStatusCode());
  250. $data = json_decode((string) $response->getBody(), true);
  251. $this->assertEquals(1, count($data));
  252. $this->assertEquals(0, $data[0]['id']);
  253. $this->assertEquals(self::NB_FIELDS_LINK, count($data[0]));
  254. // Multiple results (2)
  255. $env = Environment::mock([
  256. 'REQUEST_METHOD' => 'GET',
  257. 'QUERY_STRING' => 'searchterm=stallman'
  258. ]);
  259. $request = Request::createFromEnvironment($env);
  260. $response = $this->controller->getLinks($request, new Response());
  261. $this->assertEquals(200, $response->getStatusCode());
  262. $data = json_decode((string) $response->getBody(), true);
  263. $this->assertEquals(2, count($data));
  264. $this->assertEquals(41, $data[0]['id']);
  265. $this->assertEquals(self::NB_FIELDS_LINK, count($data[0]));
  266. $this->assertEquals(8, $data[1]['id']);
  267. $this->assertEquals(self::NB_FIELDS_LINK, count($data[1]));
  268. // Multiword - 2 results
  269. $env = Environment::mock([
  270. 'REQUEST_METHOD' => 'GET',
  271. 'QUERY_STRING' => 'searchterm=stallman+software'
  272. ]);
  273. $request = Request::createFromEnvironment($env);
  274. $response = $this->controller->getLinks($request, new Response());
  275. $this->assertEquals(200, $response->getStatusCode());
  276. $data = json_decode((string) $response->getBody(), true);
  277. $this->assertEquals(2, count($data));
  278. $this->assertEquals(41, $data[0]['id']);
  279. $this->assertEquals(self::NB_FIELDS_LINK, count($data[0]));
  280. $this->assertEquals(8, $data[1]['id']);
  281. $this->assertEquals(self::NB_FIELDS_LINK, count($data[1]));
  282. // URL encoding
  283. $env = Environment::mock([
  284. 'REQUEST_METHOD' => 'GET',
  285. 'QUERY_STRING' => 'searchterm='. urlencode('@web')
  286. ]);
  287. $request = Request::createFromEnvironment($env);
  288. $response = $this->controller->getLinks($request, new Response());
  289. $this->assertEquals(200, $response->getStatusCode());
  290. $data = json_decode((string) $response->getBody(), true);
  291. $this->assertEquals(2, count($data));
  292. $this->assertEquals(41, $data[0]['id']);
  293. $this->assertEquals(self::NB_FIELDS_LINK, count($data[0]));
  294. $this->assertEquals(8, $data[1]['id']);
  295. $this->assertEquals(self::NB_FIELDS_LINK, count($data[1]));
  296. }
  297. public function testGetLinksSearchTermNoResult()
  298. {
  299. $env = Environment::mock([
  300. 'REQUEST_METHOD' => 'GET',
  301. 'QUERY_STRING' => 'searchterm=nope'
  302. ]);
  303. $request = Request::createFromEnvironment($env);
  304. $response = $this->controller->getLinks($request, new Response());
  305. $this->assertEquals(200, $response->getStatusCode());
  306. $data = json_decode((string) $response->getBody(), true);
  307. $this->assertEquals(0, count($data));
  308. }
  309. public function testGetLinksSearchTags()
  310. {
  311. // Single tag
  312. $env = Environment::mock([
  313. 'REQUEST_METHOD' => 'GET',
  314. 'QUERY_STRING' => 'searchtags=dev',
  315. ]);
  316. $request = Request::createFromEnvironment($env);
  317. $response = $this->controller->getLinks($request, new Response());
  318. $this->assertEquals(200, $response->getStatusCode());
  319. $data = json_decode((string) $response->getBody(), true);
  320. $this->assertEquals(2, count($data));
  321. $this->assertEquals(0, $data[0]['id']);
  322. $this->assertEquals(self::NB_FIELDS_LINK, count($data[0]));
  323. $this->assertEquals(4, $data[1]['id']);
  324. $this->assertEquals(self::NB_FIELDS_LINK, count($data[1]));
  325. // Multitag + exclude
  326. $env = Environment::mock([
  327. 'REQUEST_METHOD' => 'GET',
  328. 'QUERY_STRING' => 'searchtags=stuff+-gnu',
  329. ]);
  330. $request = Request::createFromEnvironment($env);
  331. $response = $this->controller->getLinks($request, new Response());
  332. $this->assertEquals(200, $response->getStatusCode());
  333. $data = json_decode((string) $response->getBody(), true);
  334. $this->assertEquals(1, count($data));
  335. $this->assertEquals(41, $data[0]['id']);
  336. $this->assertEquals(self::NB_FIELDS_LINK, count($data[0]));
  337. // wildcard: placeholder at the start
  338. $env = Environment::mock([
  339. 'REQUEST_METHOD' => 'GET',
  340. 'QUERY_STRING' => 'searchtags=*Tuff',
  341. ]);
  342. $request = Request::createFromEnvironment($env);
  343. $response = $this->controller->getLinks($request, new Response());
  344. $this->assertEquals(200, $response->getStatusCode());
  345. $data = json_decode((string) $response->getBody(), true);
  346. $this->assertEquals(2, count($data));
  347. $this->assertEquals(41, $data[0]['id']);
  348. // wildcard: placeholder at the end
  349. $env = Environment::mock([
  350. 'REQUEST_METHOD' => 'GET',
  351. 'QUERY_STRING' => 'searchtags=c*',
  352. ]);
  353. $request = Request::createFromEnvironment($env);
  354. $response = $this->controller->getLinks($request, new Response());
  355. $this->assertEquals(200, $response->getStatusCode());
  356. $data = json_decode((string) $response->getBody(), true);
  357. $this->assertEquals(4, count($data));
  358. $this->assertEquals(6, $data[0]['id']);
  359. // wildcard: placeholder at the middle
  360. $env = Environment::mock([
  361. 'REQUEST_METHOD' => 'GET',
  362. 'QUERY_STRING' => 'searchtags=w*b',
  363. ]);
  364. $request = Request::createFromEnvironment($env);
  365. $response = $this->controller->getLinks($request, new Response());
  366. $this->assertEquals(200, $response->getStatusCode());
  367. $data = json_decode((string) $response->getBody(), true);
  368. $this->assertEquals(4, count($data));
  369. $this->assertEquals(6, $data[0]['id']);
  370. // wildcard: match all
  371. $env = Environment::mock([
  372. 'REQUEST_METHOD' => 'GET',
  373. 'QUERY_STRING' => 'searchtags=*',
  374. ]);
  375. $request = Request::createFromEnvironment($env);
  376. $response = $this->controller->getLinks($request, new Response());
  377. $this->assertEquals(200, $response->getStatusCode());
  378. $data = json_decode((string) $response->getBody(), true);
  379. $this->assertEquals(9, count($data));
  380. $this->assertEquals(41, $data[0]['id']);
  381. // wildcard: optional ('*' does not need to expand)
  382. $env = Environment::mock([
  383. 'REQUEST_METHOD' => 'GET',
  384. 'QUERY_STRING' => 'searchtags=*stuff*',
  385. ]);
  386. $request = Request::createFromEnvironment($env);
  387. $response = $this->controller->getLinks($request, new Response());
  388. $this->assertEquals(200, $response->getStatusCode());
  389. $data = json_decode((string) $response->getBody(), true);
  390. $this->assertEquals(2, count($data));
  391. $this->assertEquals(41, $data[0]['id']);
  392. // wildcard: exclusions
  393. $env = Environment::mock([
  394. 'REQUEST_METHOD' => 'GET',
  395. 'QUERY_STRING' => 'searchtags=*a*+-*e*',
  396. ]);
  397. $request = Request::createFromEnvironment($env);
  398. $response = $this->controller->getLinks($request, new Response());
  399. $this->assertEquals(200, $response->getStatusCode());
  400. $data = json_decode((string) $response->getBody(), true);
  401. $this->assertEquals(1, count($data));
  402. $this->assertEquals(41, $data[0]['id']); // finds '#hashtag' in descr.
  403. // wildcard: exclude all
  404. $env = Environment::mock([
  405. 'REQUEST_METHOD' => 'GET',
  406. 'QUERY_STRING' => 'searchtags=-*',
  407. ]);
  408. $request = Request::createFromEnvironment($env);
  409. $response = $this->controller->getLinks($request, new Response());
  410. $this->assertEquals(200, $response->getStatusCode());
  411. $data = json_decode((string) $response->getBody(), true);
  412. $this->assertEquals(0, count($data));
  413. }
  414. /**
  415. * Test getLinks service with search tags+terms.
  416. */
  417. public function testGetLinksSearchTermsAndTags()
  418. {
  419. $env = Environment::mock([
  420. 'REQUEST_METHOD' => 'GET',
  421. 'QUERY_STRING' => 'searchterm=poke&searchtags=dev',
  422. ]);
  423. $request = Request::createFromEnvironment($env);
  424. $response = $this->controller->getLinks($request, new Response());
  425. $this->assertEquals(200, $response->getStatusCode());
  426. $data = json_decode((string) $response->getBody(), true);
  427. $this->assertEquals(1, count($data));
  428. $this->assertEquals(0, $data[0]['id']);
  429. $this->assertEquals(self::NB_FIELDS_LINK, count($data[0]));
  430. }
  431. }