3 namespace Shaarli\Api\Controllers
;
5 use Shaarli\Bookmark\Bookmark
;
6 use Shaarli\Bookmark\BookmarkFileService
;
7 use Shaarli\Config\ConfigManager
;
10 use Slim\Http\Environment
;
11 use Slim\Http\Request
;
12 use Slim\Http\Response
;
17 * Test getLink by ID API service.
19 * @see http://shaarli.github.io/api-documentation/#links-link-get
21 * @package Shaarli\Api\Controllers
23 class GetLinkIdTest
extends \Shaarli\TestCase
26 * @var string datastore to test write operations
28 protected static $testDatastore = 'sandbox/datastore.php';
31 * @var ConfigManager instance
36 * @var \ReferenceLinkDB instance.
38 protected $refDB = null;
41 * @var Container instance.
46 * @var Links controller instance.
48 protected $controller;
51 * Number of JSON fields per link.
53 const NB_FIELDS_LINK
= 9;
56 * Before each test, instantiate a new Api with its config, plugins and bookmarks.
58 protected function setUp(): void
60 $this->conf
= new ConfigManager('tests/utils/config/configJson');
61 $this->conf
->set('resource.datastore', self
::$testDatastore);
62 $this->refDB
= new \
ReferenceLinkDB();
63 $this->refDB
->write(self
::$testDatastore);
64 $history = new History('sandbox/history.php');
66 $this->container
= new Container();
67 $this->container
['conf'] = $this->conf
;
68 $this->container
['db'] = new BookmarkFileService($this->conf
, $history, true);
69 $this->container
['history'] = null;
71 $this->controller
= new Links($this->container
);
75 * After each test, remove the test datastore.
77 protected function tearDown(): void
79 @unlink(self
::$testDatastore);
83 * Test basic getLink service: return link ID=41.
85 public function testGetLinkId()
87 // Used by index_url().
88 $_SERVER['SERVER_NAME'] = 'domain.tld';
89 $_SERVER['SERVER_PORT'] = 80;
90 $_SERVER['SCRIPT_NAME'] = '/';
93 $env = Environment
::mock([
94 'REQUEST_METHOD' => 'GET',
96 $request = Request
::createFromEnvironment($env);
98 $response = $this->controller
->getLink($request, new Response(), ['id' => $id]);
99 $this->assertEquals(200, $response->getStatusCode());
100 $data = json_decode((string) $response->getBody(), true);
101 $this->assertEquals(self
::NB_FIELDS_LINK
, count($data));
102 $this->assertEquals($id, $data['id']);
104 // Check link elements
105 $this->assertEquals('http://domain.tld/shaare/WDWyig', $data['url']);
106 $this->assertEquals('WDWyig', $data['shorturl']);
107 $this->assertEquals('Link title: @website', $data['title']);
109 'Stallman has a beard and is part of the Free Software Foundation (or not). Seriously, read this. #hashtag',
112 $this->assertEquals('sTuff', $data['tags'][0]);
113 $this->assertEquals(false, $data['private']);
115 \DateTime
::createFromFormat(Bookmark
::LINK_DATE_FORMAT
, '20150310_114651')->format(\DateTime
::ATOM
),
118 $this->assertEmpty($data['updated']);
122 * Test basic getLink service: get non existent link => ApiLinkNotFoundException.
124 public function testGetLink404()
126 $this->expectException(\Shaarli\Api\Exceptions\ApiLinkNotFoundException
::class);
127 $this->expectExceptionMessage('Link not found');
129 $env = Environment
::mock([
130 'REQUEST_METHOD' => 'GET',
132 $request = Request
::createFromEnvironment($env);
134 $this->controller
->getLink($request, new Response(), ['id' => -1]);