4 namespace Shaarli\Api\Controllers
;
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
;
15 class PutLinkTest
extends \PHPUnit\Framework\TestCase
18 * @var string datastore to test write operations
20 protected static $testDatastore = 'sandbox/datastore.php';
23 * @var string datastore to test write operations
25 protected static $testHistory = 'sandbox/history.php';
28 * @var ConfigManager instance
33 * @var \ReferenceLinkDB instance.
35 protected $refDB = null;
38 * @var BookmarkFileService instance.
40 protected $bookmarkService;
43 * @var HistoryController instance.
48 * @var Container instance.
53 * @var Links controller instance.
55 protected $controller;
58 * Number of JSON field per link.
60 const NB_FIELDS_LINK
= 9;
63 * Before every test, instantiate a new Api with its config, plugins and bookmarks.
65 protected function setUp(): void
67 $this->conf
= new ConfigManager('tests/utils/config/configJson');
68 $this->conf
->set('resource.datastore', self
::$testDatastore);
69 $this->refDB
= new \
ReferenceLinkDB();
70 $this->refDB
->write(self
::$testDatastore);
71 $refHistory = new \
ReferenceHistory();
72 $refHistory->write(self
::$testHistory);
73 $this->history
= new History(self
::$testHistory);
74 $this->bookmarkService
= new BookmarkFileService($this->conf
, $this->history
, true);
76 $this->container
= new Container();
77 $this->container
['conf'] = $this->conf
;
78 $this->container
['db'] = $this->bookmarkService
;
79 $this->container
['history'] = $this->history
;
81 $this->controller
= new Links($this->container
);
83 // Used by index_url().
84 $this->controller
->getCi()['environment'] = [
85 'SERVER_NAME' => 'domain.tld',
92 * After every test, remove the test datastore.
94 protected function tearDown(): void
96 @unlink(self
::$testDatastore);
97 @unlink(self
::$testHistory);
101 * Test link update without value: reset the link to default values
103 public function testPutLinkMinimal()
105 $env = Environment
::mock([
106 'REQUEST_METHOD' => 'PUT',
109 $request = Request
::createFromEnvironment($env);
111 $response = $this->controller
->putLink($request, new Response(), ['id' => $id]);
112 $this->assertEquals(200, $response->getStatusCode());
113 $data = json_decode((string) $response->getBody(), true);
114 $this->assertEquals(self
::NB_FIELDS_LINK
, count($data));
115 $this->assertEquals($id, $data['id']);
116 $this->assertEquals('WDWyig', $data['shorturl']);
117 $this->assertEquals('http://domain.tld/shaare/WDWyig', $data['url']);
118 $this->assertEquals('/shaare/WDWyig', $data['title']);
119 $this->assertEquals('', $data['description']);
120 $this->assertEquals([], $data['tags']);
121 $this->assertEquals(true, $data['private']);
123 \DateTime
::createFromFormat('Ymd_His', '20150310_114651'),
124 \DateTime
::createFromFormat(\DateTime
::ATOM
, $data['created'])
127 new \
DateTime('5 seconds ago') < \DateTime
::createFromFormat(\DateTime
::ATOM
, $data['updated'])
130 $historyEntry = $this->history
->getHistory()[0];
131 $this->assertEquals(History
::UPDATED
, $historyEntry['event']);
133 (new \
DateTime())->add(\DateInterval
::createFromDateString('-5 seconds')) < $historyEntry['datetime']
135 $this->assertEquals($id, $historyEntry['id']);
139 * Test link update with new values
141 public function testPutLinkWithValues()
143 $env = Environment
::mock([
144 'REQUEST_METHOD' => 'PUT',
145 'CONTENT_TYPE' => 'application/json'
149 'url' => 'http://somewhere.else',
151 'description' => 'Percé jusques au fond du cœur [...]',
152 'tags' => ['corneille', 'rodrigue'],
155 $request = Request
::createFromEnvironment($env);
156 $request = $request->withParsedBody($update);
158 $response = $this->controller
->putLink($request, new Response(), ['id' => $id]);
159 $this->assertEquals(200, $response->getStatusCode());
160 $data = json_decode((string) $response->getBody(), true);
161 $this->assertEquals(self
::NB_FIELDS_LINK
, count($data));
162 $this->assertEquals($id, $data['id']);
163 $this->assertEquals('WDWyig', $data['shorturl']);
164 $this->assertEquals('http://somewhere.else', $data['url']);
165 $this->assertEquals('Le Cid', $data['title']);
166 $this->assertEquals('Percé jusques au fond du cœur [...]', $data['description']);
167 $this->assertEquals(['corneille', 'rodrigue'], $data['tags']);
168 $this->assertEquals(true, $data['private']);
170 \DateTime
::createFromFormat('Ymd_His', '20150310_114651'),
171 \DateTime
::createFromFormat(\DateTime
::ATOM
, $data['created'])
174 new \
DateTime('5 seconds ago') < \DateTime
::createFromFormat(\DateTime
::ATOM
, $data['updated'])
179 * Test link update with an existing URL: 409 Conflict with the existing link as body
181 public function testPutLinkDuplicate()
184 'url' => 'mediagoblin.org/',
185 'title' => 'new entry',
186 'description' => 'shaare description',
187 'tags' => ['one', 'two'],
190 $env = Environment
::mock([
191 'REQUEST_METHOD' => 'PUT',
192 'CONTENT_TYPE' => 'application/json'
195 $request = Request
::createFromEnvironment($env);
196 $request = $request->withParsedBody($link);
197 $response = $this->controller
->putLink($request, new Response(), ['id' => 41]);
199 $this->assertEquals(409, $response->getStatusCode());
200 $data = json_decode((string) $response->getBody(), true);
201 $this->assertEquals(self
::NB_FIELDS_LINK
, count($data));
202 $this->assertEquals(7, $data['id']);
203 $this->assertEquals('IuWvgA', $data['shorturl']);
204 $this->assertEquals('http://mediagoblin.org/', $data['url']);
205 $this->assertEquals('MediaGoblin', $data['title']);
206 $this->assertEquals('A free software media publishing platform #hashtagOther', $data['description']);
207 $this->assertEquals(['gnu', 'media', 'web', '.hidden', 'hashtag'], $data['tags']);
208 $this->assertEquals(false, $data['private']);
210 \DateTime
::createFromFormat(Bookmark
::LINK_DATE_FORMAT
, '20130614_184135'),
211 \DateTime
::createFromFormat(\DateTime
::ATOM
, $data['created'])
214 \DateTime
::createFromFormat(Bookmark
::LINK_DATE_FORMAT
, '20130615_184230'),
215 \DateTime
::createFromFormat(\DateTime
::ATOM
, $data['updated'])
220 * Test link update on non existent link => ApiLinkNotFoundException.
222 public function testGetLink404()
224 $this->expectException(\Shaarli\Api\Exceptions\ApiLinkNotFoundException
::class);
225 $this->expectExceptionMessage('Link not found');
227 $env = Environment
::mock([
228 'REQUEST_METHOD' => 'PUT',
230 $request = Request
::createFromEnvironment($env);
232 $this->controller
->putLink($request, new Response(), ['id' => -1]);