4 namespace Shaarli\Api\Controllers
;
6 use malkusch\lock\mutex\NoMutex
;
7 use Shaarli\Bookmark\Bookmark
;
8 use Shaarli\Bookmark\BookmarkFileService
;
9 use Shaarli\Config\ConfigManager
;
11 use Shaarli\Plugin\PluginManager
;
13 use Slim\Http\Environment
;
14 use Slim\Http\Request
;
15 use Slim\Http\Response
;
17 class PutLinkTest
extends \Shaarli\TestCase
20 * @var string datastore to test write operations
22 protected static $testDatastore = 'sandbox/datastore.php';
25 * @var string datastore to test write operations
27 protected static $testHistory = 'sandbox/history.php';
30 * @var ConfigManager instance
35 * @var \ReferenceLinkDB instance.
37 protected $refDB = null;
40 * @var BookmarkFileService instance.
42 protected $bookmarkService;
45 * @var HistoryController instance.
50 * @var Container instance.
55 * @var Links controller instance.
57 protected $controller;
60 * Number of JSON field per link.
62 const NB_FIELDS_LINK
= 9;
65 * Before every test, instantiate a new Api with its config, plugins and bookmarks.
67 protected function setUp(): void
69 $mutex = new NoMutex();
70 $this->conf
= new ConfigManager('tests/utils/config/configJson');
71 $this->conf
->set('resource.datastore', self
::$testDatastore);
72 $this->refDB
= new \
ReferenceLinkDB();
73 $this->refDB
->write(self
::$testDatastore);
74 $refHistory = new \
ReferenceHistory();
75 $refHistory->write(self
::$testHistory);
76 $this->history
= new History(self
::$testHistory);
77 $pluginManager = new PluginManager($this->conf
);
78 $this->bookmarkService
= new BookmarkFileService(
85 $this->container
= new Container();
86 $this->container
['conf'] = $this->conf
;
87 $this->container
['db'] = $this->bookmarkService
;
88 $this->container
['history'] = $this->history
;
90 $this->controller
= new Links($this->container
);
92 // Used by index_url().
93 $this->controller
->getCi()['environment'] = [
94 'SERVER_NAME' => 'domain.tld',
101 * After every test, remove the test datastore.
103 protected function tearDown(): void
105 @unlink(self
::$testDatastore);
106 @unlink(self
::$testHistory);
110 * Test link update without value: reset the link to default values
112 public function testPutLinkMinimal()
114 $env = Environment
::mock([
115 'REQUEST_METHOD' => 'PUT',
118 $request = Request
::createFromEnvironment($env);
120 $response = $this->controller
->putLink($request, new Response(), ['id' => $id]);
121 $this->assertEquals(200, $response->getStatusCode());
122 $data = json_decode((string) $response->getBody(), true);
123 $this->assertEquals(self
::NB_FIELDS_LINK
, count($data));
124 $this->assertEquals($id, $data['id']);
125 $this->assertEquals('WDWyig', $data['shorturl']);
126 $this->assertEquals('http://domain.tld/shaare/WDWyig', $data['url']);
127 $this->assertEquals('/shaare/WDWyig', $data['title']);
128 $this->assertEquals('', $data['description']);
129 $this->assertEquals([], $data['tags']);
130 $this->assertEquals(true, $data['private']);
132 \DateTime
::createFromFormat('Ymd_His', '20150310_114651'),
133 \DateTime
::createFromFormat(\DateTime
::ATOM
, $data['created'])
136 new \
DateTime('5 seconds ago') < \DateTime
::createFromFormat(\DateTime
::ATOM
, $data['updated'])
139 $historyEntry = $this->history
->getHistory()[0];
140 $this->assertEquals(History
::UPDATED
, $historyEntry['event']);
142 (new \
DateTime())->add(\DateInterval
::createFromDateString('-5 seconds')) < $historyEntry['datetime']
144 $this->assertEquals($id, $historyEntry['id']);
148 * Test link update with new values
150 public function testPutLinkWithValues()
152 $env = Environment
::mock([
153 'REQUEST_METHOD' => 'PUT',
154 'CONTENT_TYPE' => 'application/json'
158 'url' => 'http://somewhere.else',
160 'description' => 'Percé jusques au fond du cœur [...]',
161 'tags' => ['corneille', 'rodrigue'],
164 $request = Request
::createFromEnvironment($env);
165 $request = $request->withParsedBody($update);
167 $response = $this->controller
->putLink($request, new Response(), ['id' => $id]);
168 $this->assertEquals(200, $response->getStatusCode());
169 $data = json_decode((string) $response->getBody(), true);
170 $this->assertEquals(self
::NB_FIELDS_LINK
, count($data));
171 $this->assertEquals($id, $data['id']);
172 $this->assertEquals('WDWyig', $data['shorturl']);
173 $this->assertEquals('http://somewhere.else', $data['url']);
174 $this->assertEquals('Le Cid', $data['title']);
175 $this->assertEquals('Percé jusques au fond du cœur [...]', $data['description']);
176 $this->assertEquals(['corneille', 'rodrigue'], $data['tags']);
177 $this->assertEquals(true, $data['private']);
179 \DateTime
::createFromFormat('Ymd_His', '20150310_114651'),
180 \DateTime
::createFromFormat(\DateTime
::ATOM
, $data['created'])
183 new \
DateTime('5 seconds ago') < \DateTime
::createFromFormat(\DateTime
::ATOM
, $data['updated'])
188 * Test link update with an existing URL: 409 Conflict with the existing link as body
190 public function testPutLinkDuplicate()
193 'url' => 'mediagoblin.org/',
194 'title' => 'new entry',
195 'description' => 'shaare description',
196 'tags' => ['one', 'two'],
199 $env = Environment
::mock([
200 'REQUEST_METHOD' => 'PUT',
201 'CONTENT_TYPE' => 'application/json'
204 $request = Request
::createFromEnvironment($env);
205 $request = $request->withParsedBody($link);
206 $response = $this->controller
->putLink($request, new Response(), ['id' => 41]);
208 $this->assertEquals(409, $response->getStatusCode());
209 $data = json_decode((string) $response->getBody(), true);
210 $this->assertEquals(self
::NB_FIELDS_LINK
, count($data));
211 $this->assertEquals(7, $data['id']);
212 $this->assertEquals('IuWvgA', $data['shorturl']);
213 $this->assertEquals('http://mediagoblin.org/', $data['url']);
214 $this->assertEquals('MediaGoblin', $data['title']);
215 $this->assertEquals('A free software media publishing platform #hashtagOther', $data['description']);
216 $this->assertEquals(['gnu', 'media', 'web', '.hidden', 'hashtag'], $data['tags']);
217 $this->assertEquals(false, $data['private']);
219 \DateTime
::createFromFormat(Bookmark
::LINK_DATE_FORMAT
, '20130614_184135'),
220 \DateTime
::createFromFormat(\DateTime
::ATOM
, $data['created'])
223 \DateTime
::createFromFormat(Bookmark
::LINK_DATE_FORMAT
, '20130615_184230'),
224 \DateTime
::createFromFormat(\DateTime
::ATOM
, $data['updated'])
229 * Test link update on non existent link => ApiLinkNotFoundException.
231 public function testGetLink404()
233 $this->expectException(\Shaarli\Api\Exceptions\ApiLinkNotFoundException
::class);
234 $this->expectExceptionMessage('Link not found');
236 $env = Environment
::mock([
237 'REQUEST_METHOD' => 'PUT',
239 $request = Request
::createFromEnvironment($env);
241 $this->controller
->putLink($request, new Response(), ['id' => -1]);
245 * Test link creation with a tag string provided
247 public function testPutLinkWithTagString(): void
253 $env = Environment
::mock([
254 'REQUEST_METHOD' => 'PUT',
255 'CONTENT_TYPE' => 'application/json'
258 $request = Request
::createFromEnvironment($env);
259 $request = $request->withParsedBody($link);
260 $response = $this->controller
->putLink($request, new Response(), ['id' => $id]);
262 $this->assertEquals(200, $response->getStatusCode());
263 $data = json_decode((string) $response->getBody(), true);
264 $this->assertEquals(self
::NB_FIELDS_LINK
, count($data));
265 $this->assertEquals(['one', 'two'], $data['tags']);
269 * Test link creation with a tag string provided
271 public function testPutLinkWithTagString2(): void
274 'tags' => ['one two'],
277 $env = Environment
::mock([
278 'REQUEST_METHOD' => 'PUT',
279 'CONTENT_TYPE' => 'application/json'
282 $request = Request
::createFromEnvironment($env);
283 $request = $request->withParsedBody($link);
284 $response = $this->controller
->putLink($request, new Response(), ['id' => $id]);
286 $this->assertEquals(200, $response->getStatusCode());
287 $data = json_decode((string) $response->getBody(), true);
288 $this->assertEquals(self
::NB_FIELDS_LINK
, count($data));
289 $this->assertEquals(['one', 'two'], $data['tags']);