3 namespace Shaarli\Api\Controllers
;
5 use malkusch\lock\mutex\NoMutex
;
6 use Shaarli\Bookmark\Bookmark
;
7 use Shaarli\Bookmark\BookmarkFileService
;
8 use Shaarli\Config\ConfigManager
;
11 use Slim\Http\Environment
;
12 use Slim\Http\Request
;
13 use Slim\Http\Response
;
18 * Test getLink by ID API service.
20 * @see http://shaarli.github.io/api-documentation/#links-link-get
22 * @package Shaarli\Api\Controllers
24 class GetLinkIdTest
extends \Shaarli\TestCase
27 * @var string datastore to test write operations
29 protected static $testDatastore = 'sandbox/datastore.php';
32 * @var ConfigManager instance
37 * @var \ReferenceLinkDB instance.
39 protected $refDB = null;
42 * @var Container instance.
47 * @var Links controller instance.
49 protected $controller;
52 * Number of JSON fields per link.
54 const NB_FIELDS_LINK
= 9;
57 * Before each test, instantiate a new Api with its config, plugins and bookmarks.
59 protected function setUp(): void
61 $mutex = new NoMutex();
62 $this->conf
= new ConfigManager('tests/utils/config/configJson');
63 $this->conf
->set('resource.datastore', self
::$testDatastore);
64 $this->refDB
= new \
ReferenceLinkDB();
65 $this->refDB
->write(self
::$testDatastore);
66 $history = new History('sandbox/history.php');
68 $this->container
= new Container();
69 $this->container
['conf'] = $this->conf
;
70 $this->container
['db'] = new BookmarkFileService($this->conf
, $history, $mutex, true);
71 $this->container
['history'] = null;
73 $this->controller
= new Links($this->container
);
77 * After each test, remove the test datastore.
79 protected function tearDown(): void
81 @unlink(self
::$testDatastore);
85 * Test basic getLink service: return link ID=41.
87 public function testGetLinkId()
89 // Used by index_url().
90 $_SERVER['SERVER_NAME'] = 'domain.tld';
91 $_SERVER['SERVER_PORT'] = 80;
92 $_SERVER['SCRIPT_NAME'] = '/';
95 $env = Environment
::mock([
96 'REQUEST_METHOD' => 'GET',
98 $request = Request
::createFromEnvironment($env);
100 $response = $this->controller
->getLink($request, new Response(), ['id' => $id]);
101 $this->assertEquals(200, $response->getStatusCode());
102 $data = json_decode((string) $response->getBody(), true);
103 $this->assertEquals(self
::NB_FIELDS_LINK
, count($data));
104 $this->assertEquals($id, $data['id']);
106 // Check link elements
107 $this->assertEquals('http://domain.tld/shaare/WDWyig', $data['url']);
108 $this->assertEquals('WDWyig', $data['shorturl']);
109 $this->assertEquals('Link title: @website', $data['title']);
111 'Stallman has a beard and is part of the Free Software Foundation (or not). Seriously, read this. #hashtag',
114 $this->assertEquals('sTuff', $data['tags'][0]);
115 $this->assertEquals(false, $data['private']);
117 \DateTime
::createFromFormat(Bookmark
::LINK_DATE_FORMAT
, '20150310_114651')->format(\DateTime
::ATOM
),
120 $this->assertEmpty($data['updated']);
124 * Test basic getLink service: get non existent link => ApiLinkNotFoundException.
126 public function testGetLink404()
128 $this->expectException(\Shaarli\Api\Exceptions\ApiLinkNotFoundException
::class);
129 $this->expectExceptionMessage('Link not found');
131 $env = Environment
::mock([
132 'REQUEST_METHOD' => 'GET',
134 $request = Request
::createFromEnvironment($env);
136 $this->controller
->getLink($request, new Response(), ['id' => -1]);