]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - tests/api/controllers/links/GetLinkIdTest.php
1ec56ef3cd97c870fc52b7bbb54875178c52a6ea
[github/shaarli/Shaarli.git] / tests / api / controllers / links / GetLinkIdTest.php
1 <?php
2
3 namespace Shaarli\Api\Controllers;
4
5 use malkusch\lock\mutex\NoMutex;
6 use Shaarli\Bookmark\Bookmark;
7 use Shaarli\Bookmark\BookmarkFileService;
8 use Shaarli\Config\ConfigManager;
9 use Shaarli\History;
10 use Slim\Container;
11 use Slim\Http\Environment;
12 use Slim\Http\Request;
13 use Slim\Http\Response;
14
15 /**
16 * Class GetLinkIdTest
17 *
18 * Test getLink by ID API service.
19 *
20 * @see http://shaarli.github.io/api-documentation/#links-link-get
21 *
22 * @package Shaarli\Api\Controllers
23 */
24 class GetLinkIdTest extends \Shaarli\TestCase
25 {
26 /**
27 * @var string datastore to test write operations
28 */
29 protected static $testDatastore = 'sandbox/datastore.php';
30
31 /**
32 * @var ConfigManager instance
33 */
34 protected $conf;
35
36 /**
37 * @var \ReferenceLinkDB instance.
38 */
39 protected $refDB = null;
40
41 /**
42 * @var Container instance.
43 */
44 protected $container;
45
46 /**
47 * @var Links controller instance.
48 */
49 protected $controller;
50
51 /**
52 * Number of JSON fields per link.
53 */
54 const NB_FIELDS_LINK = 9;
55
56 /**
57 * Before each test, instantiate a new Api with its config, plugins and bookmarks.
58 */
59 protected function setUp(): void
60 {
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');
67
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;
72
73 $this->controller = new Links($this->container);
74 }
75
76 /**
77 * After each test, remove the test datastore.
78 */
79 protected function tearDown(): void
80 {
81 @unlink(self::$testDatastore);
82 }
83
84 /**
85 * Test basic getLink service: return link ID=41.
86 */
87 public function testGetLinkId()
88 {
89 // Used by index_url().
90 $_SERVER['SERVER_NAME'] = 'domain.tld';
91 $_SERVER['SERVER_PORT'] = 80;
92 $_SERVER['SCRIPT_NAME'] = '/';
93
94 $id = 41;
95 $env = Environment::mock([
96 'REQUEST_METHOD' => 'GET',
97 ]);
98 $request = Request::createFromEnvironment($env);
99
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']);
105
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']);
110 $this->assertEquals(
111 'Stallman has a beard and is part of the Free Software Foundation (or not). Seriously, read this. #hashtag',
112 $data['description']
113 );
114 $this->assertEquals('sTuff', $data['tags'][0]);
115 $this->assertEquals(false, $data['private']);
116 $this->assertEquals(
117 \DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20150310_114651')->format(\DateTime::ATOM),
118 $data['created']
119 );
120 $this->assertEmpty($data['updated']);
121 }
122
123 /**
124 * Test basic getLink service: get non existent link => ApiLinkNotFoundException.
125 */
126 public function testGetLink404()
127 {
128 $this->expectException(\Shaarli\Api\Exceptions\ApiLinkNotFoundException::class);
129 $this->expectExceptionMessage('Link not found');
130
131 $env = Environment::mock([
132 'REQUEST_METHOD' => 'GET',
133 ]);
134 $request = Request::createFromEnvironment($env);
135
136 $this->controller->getLink($request, new Response(), ['id' => -1]);
137 }
138 }