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
;
10 use Shaarli\Plugin\PluginManager
;
12 use Slim\Http\Environment
;
13 use Slim\Http\Request
;
14 use Slim\Http\Response
;
19 * Test getLink by ID API service.
21 * @see http://shaarli.github.io/api-documentation/#links-link-get
23 * @package Shaarli\Api\Controllers
25 class GetLinkIdTest
extends \Shaarli\TestCase
28 * @var string datastore to test write operations
30 protected static $testDatastore = 'sandbox/datastore.php';
33 * @var ConfigManager instance
38 * @var \ReferenceLinkDB instance.
40 protected $refDB = null;
43 * @var Container instance.
48 * @var Links controller instance.
50 protected $controller;
53 * Number of JSON fields per link.
55 const NB_FIELDS_LINK
= 9;
58 * Before each test, instantiate a new Api with its config, plugins and bookmarks.
60 protected function setUp(): void
62 $mutex = new NoMutex();
63 $this->conf
= new ConfigManager('tests/utils/config/configJson');
64 $this->conf
->set('resource.datastore', self
::$testDatastore);
65 $this->refDB
= new \
ReferenceLinkDB();
66 $this->refDB
->write(self
::$testDatastore);
67 $history = new History('sandbox/history.php');
69 $this->container
= new Container();
70 $this->container
['conf'] = $this->conf
;
71 $pluginManager = new PluginManager($this->conf
);
72 $this->container
['db'] = new BookmarkFileService(
79 $this->container
['history'] = null;
81 $this->controller
= new Links($this->container
);
85 * After each test, remove the test datastore.
87 protected function tearDown(): void
89 @unlink(self
::$testDatastore);
93 * Test basic getLink service: return link ID=41.
95 public function testGetLinkId()
97 // Used by index_url().
98 $_SERVER['SERVER_NAME'] = 'domain.tld';
99 $_SERVER['SERVER_PORT'] = 80;
100 $_SERVER['SCRIPT_NAME'] = '/';
103 $env = Environment
::mock([
104 'REQUEST_METHOD' => 'GET',
106 $request = Request
::createFromEnvironment($env);
108 $response = $this->controller
->getLink($request, new Response(), ['id' => $id]);
109 $this->assertEquals(200, $response->getStatusCode());
110 $data = json_decode((string) $response->getBody(), true);
111 $this->assertEquals(self
::NB_FIELDS_LINK
, count($data));
112 $this->assertEquals($id, $data['id']);
114 // Check link elements
115 $this->assertEquals('http://domain.tld/shaare/WDWyig', $data['url']);
116 $this->assertEquals('WDWyig', $data['shorturl']);
117 $this->assertEquals('Link title: @website', $data['title']);
119 'Stallman has a beard and is part of the Free Software Foundation (or not). Seriously, read this. #hashtag',
122 $this->assertEquals('sTuff', $data['tags'][0]);
123 $this->assertEquals(false, $data['private']);
125 \DateTime
::createFromFormat(Bookmark
::LINK_DATE_FORMAT
, '20150310_114651')->format(\DateTime
::ATOM
),
128 $this->assertEmpty($data['updated']);
132 * Test basic getLink service: get non existent link => ApiLinkNotFoundException.
134 public function testGetLink404()
136 $this->expectException(\Shaarli\Api\Exceptions\ApiLinkNotFoundException
::class);
137 $this->expectExceptionMessage('Link not found');
139 $env = Environment
::mock([
140 'REQUEST_METHOD' => 'GET',
142 $request = Request
::createFromEnvironment($env);
144 $this->controller
->getLink($request, new Response(), ['id' => -1]);