3 namespace Shaarli\Api\Controllers
;
7 use Slim\Http\Environment
;
9 use Slim\Http\Response
;
14 * Test Links REST API services.
15 * Note that api call results are tightly related to data contained in ReferenceLinkDB.
17 * @package Shaarli\Api\Controllers
19 class LinksTest
extends \PHPUnit_Framework_TestCase
22 * @var string datastore to test write operations
24 protected static $testDatastore = 'sandbox/datastore.php';
27 * @var \ConfigManager instance
32 * @var \ReferenceLinkDB instance.
34 protected $refDB = null;
37 * @var Container instance.
42 * @var Links controller instance.
44 protected $controller;
47 * Number of JSON field per link.
49 const NB_FIELDS_LINK
= 9;
52 * Before every test, instantiate a new Api with its config, plugins and links.
54 public function setUp()
56 $this->conf
= new \
ConfigManager('tests/utils/config/configJson.json.php');
57 $this->refDB
= new \
ReferenceLinkDB();
58 $this->refDB
->write(self
::$testDatastore);
60 $this->container
= new Container();
61 $this->container
['conf'] = $this->conf
;
62 $this->container
['db'] = new \
LinkDB(self
::$testDatastore, true, false);
64 $this->controller
= new Links($this->container
);
68 * After every test, remove the test datastore.
70 public function tearDown()
72 @unlink(self
::$testDatastore);
76 * Test basic getLinks service: returns all links.
78 public function testGetLinks()
80 // Used by index_url().
81 $_SERVER['SERVER_NAME'] = 'domain.tld';
82 $_SERVER['SERVER_PORT'] = 80;
83 $_SERVER['SCRIPT_NAME'] = '/';
85 $env = Environment
::mock([
86 'REQUEST_METHOD' => 'GET',
88 $request = Request
::createFromEnvironment($env);
90 $response = $this->controller
->getLinks($request, new Response());
91 $this->assertEquals(200, $response->getStatusCode());
92 $data = json_decode((string) $response->getBody(), true);
93 $this->assertEquals($this->refDB
->countLinks(), count($data));
96 $order = [41, 8, 6, 7, 0, 1, 4, 42];
98 foreach ($data as $link) {
99 $this->assertEquals(self
::NB_FIELDS_LINK
, count($link));
100 $this->assertEquals($order[$cpt++
], $link['id']);
103 // Check first element fields\
105 $this->assertEquals('http://domain.tld/?WDWyig', $first['url']);
106 $this->assertEquals('WDWyig', $first['shorturl']);
107 $this->assertEquals('Link title: @website', $first['title']);
109 'Stallman has a beard and is part of the Free Software Foundation (or not). Seriously, read this. #hashtag',
110 $first['description']
112 $this->assertEquals('sTuff', $first['tags'][0]);
113 $this->assertEquals(false, $first['private']);
115 \DateTime
::createFromFormat(\LinkDB
::LINK_DATE_FORMAT
, '20150310_114651')->format(\DateTime
::ATOM
),
118 $this->assertEmpty($first['updated']);
122 $this->assertEquals(7, count($link['tags']));
126 \DateTime
::createFromFormat(\LinkDB
::LINK_DATE_FORMAT
, '20160803_093033')->format(\DateTime
::ATOM
),
132 * Test getLinks service with offset and limit parameter:
133 * limit=1 and offset=1 should return only the second link, ID=8 (ordered by creation date DESC).
135 public function testGetLinksOffsetLimit()
137 $env = Environment
::mock([
138 'REQUEST_METHOD' => 'GET',
139 'QUERY_STRING' => 'offset=1&limit=1'
141 $request = Request
::createFromEnvironment($env);
142 $response = $this->controller
->getLinks($request, new Response());
143 $this->assertEquals(200, $response->getStatusCode());
144 $data = json_decode((string) $response->getBody(), true);
145 $this->assertEquals(1, count($data));
146 $this->assertEquals(8, $data[0]['id']);
147 $this->assertEquals(self
::NB_FIELDS_LINK
, count($data[0]));
151 * Test getLinks with limit=all (return all link).
153 public function testGetLinksLimitAll()
155 $env = Environment
::mock([
156 'REQUEST_METHOD' => 'GET',
157 'QUERY_STRING' => 'limit=all'
159 $request = Request
::createFromEnvironment($env);
160 $response = $this->controller
->getLinks($request, new Response());
161 $this->assertEquals(200, $response->getStatusCode());
162 $data = json_decode((string) $response->getBody(), true);
163 $this->assertEquals($this->refDB
->countLinks(), count($data));
165 $order = [41, 8, 6, 7, 0, 1, 4, 42];
167 foreach ($data as $link) {
168 $this->assertEquals(self
::NB_FIELDS_LINK
, count($link));
169 $this->assertEquals($order[$cpt++
], $link['id']);
174 * Test getLinks service with offset and limit parameter:
175 * limit=1 and offset=1 should return only the second link, ID=8 (ordered by creation date DESC).
177 public function testGetLinksOffsetTooHigh()
179 $env = Environment
::mock([
180 'REQUEST_METHOD' => 'GET',
181 'QUERY_STRING' => 'offset=100'
183 $request = Request
::createFromEnvironment($env);
184 $response = $this->controller
->getLinks($request, new Response());
185 $this->assertEquals(200, $response->getStatusCode());
186 $data = json_decode((string) $response->getBody(), true);
187 $this->assertEmpty(count($data));
191 * Test getLinks with private attribute to 1 or true.
193 public function testGetLinksPrivate()
195 $env = Environment
::mock([
196 'REQUEST_METHOD' => 'GET',
197 'QUERY_STRING' => 'private=true'
199 $request = Request
::createFromEnvironment($env);
200 $response = $this->controller
->getLinks($request, new Response());
201 $this->assertEquals(200, $response->getStatusCode());
202 $data = json_decode((string) $response->getBody(), true);
203 $this->assertEquals($this->refDB
->countPrivateLinks(), count($data));
204 $this->assertEquals(6, $data[0]['id']);
205 $this->assertEquals(self
::NB_FIELDS_LINK
, count($data[0]));
207 $env = Environment
::mock([
208 'REQUEST_METHOD' => 'GET',
209 'QUERY_STRING' => 'private=1'
211 $request = Request
::createFromEnvironment($env);
212 $response = $this->controller
->getLinks($request, new Response());
213 $this->assertEquals(200, $response->getStatusCode());
214 $data = json_decode((string) $response->getBody(), true);
215 $this->assertEquals($this->refDB
->countPrivateLinks(), count($data));
216 $this->assertEquals(6, $data[0]['id']);
217 $this->assertEquals(self
::NB_FIELDS_LINK
, count($data[0]));
221 * Test getLinks with private attribute to false or 0
223 public function testGetLinksNotPrivate()
225 $env = Environment
::mock(
227 'REQUEST_METHOD' => 'GET',
228 'QUERY_STRING' => 'private=0'
231 $request = Request
::createFromEnvironment($env);
232 $response = $this->controller
->getLinks($request, new Response());
233 $this->assertEquals(200, $response->getStatusCode());
234 $data = json_decode((string)$response->getBody(), true);
235 $this->assertEquals($this->refDB
->countLinks(), count($data));
236 $this->assertEquals(41, $data[0]['id']);
237 $this->assertEquals(self
::NB_FIELDS_LINK
, count($data[0]));
239 $env = Environment
::mock(
241 'REQUEST_METHOD' => 'GET',
242 'QUERY_STRING' => 'private=false'
245 $request = Request
::createFromEnvironment($env);
246 $response = $this->controller
->getLinks($request, new Response());
247 $this->assertEquals(200, $response->getStatusCode());
248 $data = json_decode((string)$response->getBody(), true);
249 $this->assertEquals($this->refDB
->countLinks(), count($data));
250 $this->assertEquals(41, $data[0]['id']);
251 $this->assertEquals(self
::NB_FIELDS_LINK
, count($data[0]));
255 * Test getLinks service with offset and limit parameter:
256 * limit=1 and offset=1 should return only the second link, ID=8 (ordered by creation date DESC).
258 public function testGetLinksSearchTerm()
260 // Only in description - 1 result
261 $env = Environment
::mock([
262 'REQUEST_METHOD' => 'GET',
263 'QUERY_STRING' => 'searchterm=Tropical'
265 $request = Request
::createFromEnvironment($env);
266 $response = $this->controller
->getLinks($request, new Response());
267 $this->assertEquals(200, $response->getStatusCode());
268 $data = json_decode((string) $response->getBody(), true);
269 $this->assertEquals(1, count($data));
270 $this->assertEquals(1, $data[0]['id']);
271 $this->assertEquals(self
::NB_FIELDS_LINK
, count($data[0]));
273 // Only in tags - 1 result
274 $env = Environment
::mock([
275 'REQUEST_METHOD' => 'GET',
276 'QUERY_STRING' => 'searchterm=tag3'
278 $request = Request
::createFromEnvironment($env);
279 $response = $this->controller
->getLinks($request, new Response());
280 $this->assertEquals(200, $response->getStatusCode());
281 $data = json_decode((string) $response->getBody(), true);
282 $this->assertEquals(1, count($data));
283 $this->assertEquals(0, $data[0]['id']);
284 $this->assertEquals(self
::NB_FIELDS_LINK
, count($data[0]));
286 // Multiple results (2)
287 $env = Environment
::mock([
288 'REQUEST_METHOD' => 'GET',
289 'QUERY_STRING' => 'searchterm=stallman'
291 $request = Request
::createFromEnvironment($env);
292 $response = $this->controller
->getLinks($request, new Response());
293 $this->assertEquals(200, $response->getStatusCode());
294 $data = json_decode((string) $response->getBody(), true);
295 $this->assertEquals(2, count($data));
296 $this->assertEquals(41, $data[0]['id']);
297 $this->assertEquals(self
::NB_FIELDS_LINK
, count($data[0]));
298 $this->assertEquals(8, $data[1]['id']);
299 $this->assertEquals(self
::NB_FIELDS_LINK
, count($data[1]));
301 // Multiword - 2 results
302 $env = Environment
::mock([
303 'REQUEST_METHOD' => 'GET',
304 'QUERY_STRING' => 'searchterm=stallman+software'
306 $request = Request
::createFromEnvironment($env);
307 $response = $this->controller
->getLinks($request, new Response());
308 $this->assertEquals(200, $response->getStatusCode());
309 $data = json_decode((string) $response->getBody(), true);
310 $this->assertEquals(2, count($data));
311 $this->assertEquals(41, $data[0]['id']);
312 $this->assertEquals(self
::NB_FIELDS_LINK
, count($data[0]));
313 $this->assertEquals(8, $data[1]['id']);
314 $this->assertEquals(self
::NB_FIELDS_LINK
, count($data[1]));
317 $env = Environment
::mock([
318 'REQUEST_METHOD' => 'GET',
319 'QUERY_STRING' => 'searchterm='. urlencode('@web')
321 $request = Request
::createFromEnvironment($env);
322 $response = $this->controller
->getLinks($request, new Response());
323 $this->assertEquals(200, $response->getStatusCode());
324 $data = json_decode((string) $response->getBody(), true);
325 $this->assertEquals(2, count($data));
326 $this->assertEquals(41, $data[0]['id']);
327 $this->assertEquals(self
::NB_FIELDS_LINK
, count($data[0]));
328 $this->assertEquals(8, $data[1]['id']);
329 $this->assertEquals(self
::NB_FIELDS_LINK
, count($data[1]));
332 public function testGetLinksSearchTermNoResult()
334 $env = Environment
::mock([
335 'REQUEST_METHOD' => 'GET',
336 'QUERY_STRING' => 'searchterm=nope'
338 $request = Request
::createFromEnvironment($env);
339 $response = $this->controller
->getLinks($request, new Response());
340 $this->assertEquals(200, $response->getStatusCode());
341 $data = json_decode((string) $response->getBody(), true);
342 $this->assertEquals(0, count($data));
345 public function testGetLinksSearchTags()
348 $env = Environment
::mock([
349 'REQUEST_METHOD' => 'GET',
350 'QUERY_STRING' => 'searchtags=dev',
352 $request = Request
::createFromEnvironment($env);
353 $response = $this->controller
->getLinks($request, new Response());
354 $this->assertEquals(200, $response->getStatusCode());
355 $data = json_decode((string) $response->getBody(), true);
356 $this->assertEquals(2, count($data));
357 $this->assertEquals(0, $data[0]['id']);
358 $this->assertEquals(self
::NB_FIELDS_LINK
, count($data[0]));
359 $this->assertEquals(4, $data[1]['id']);
360 $this->assertEquals(self
::NB_FIELDS_LINK
, count($data[1]));
362 // Multitag + exclude
363 $env = Environment
::mock([
364 'REQUEST_METHOD' => 'GET',
365 'QUERY_STRING' => 'searchtags=stuff+-gnu',
367 $request = Request
::createFromEnvironment($env);
368 $response = $this->controller
->getLinks($request, new Response());
369 $this->assertEquals(200, $response->getStatusCode());
370 $data = json_decode((string) $response->getBody(), true);
371 $this->assertEquals(1, count($data));
372 $this->assertEquals(41, $data[0]['id']);
373 $this->assertEquals(self
::NB_FIELDS_LINK
, count($data[0]));
377 * Test getLinks service with search tags+terms.
379 public function testGetLinksSearchTermsAndTags()
381 $env = Environment
::mock([
382 'REQUEST_METHOD' => 'GET',
383 'QUERY_STRING' => 'searchterm=poke&searchtags=dev',
385 $request = Request
::createFromEnvironment($env);
386 $response = $this->controller
->getLinks($request, new Response());
387 $this->assertEquals(200, $response->getStatusCode());
388 $data = json_decode((string) $response->getBody(), true);
389 $this->assertEquals(1, count($data));
390 $this->assertEquals(0, $data[0]['id']);
391 $this->assertEquals(self
::NB_FIELDS_LINK
, count($data[0]));