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
;
12 use Slim\Http\Environment
;
13 use Slim\Http\Request
;
14 use Slim\Http\Response
;
20 * Test POST Link REST API service.
22 * @package Shaarli\Api\Controllers
24 class PostLinkTest
extends TestCase
27 * @var string datastore to test write operations
29 protected static $testDatastore = 'sandbox/datastore.php';
32 * @var string datastore to test write operations
34 protected static $testHistory = 'sandbox/history.php';
37 * @var ConfigManager instance
42 * @var \ReferenceLinkDB instance.
44 protected $refDB = null;
47 * @var BookmarkFileService instance.
49 protected $bookmarkService;
52 * @var HistoryController instance.
57 * @var Container instance.
62 * @var Links controller instance.
64 protected $controller;
67 * Number of JSON field per link.
69 const NB_FIELDS_LINK
= 9;
72 * Before every test, instantiate a new Api with its config, plugins and bookmarks.
74 protected function setUp(): void
76 $mutex = new NoMutex();
77 $this->conf
= new ConfigManager('tests/utils/config/configJson');
78 $this->conf
->set('resource.datastore', self
::$testDatastore);
79 $this->refDB
= new \
ReferenceLinkDB();
80 $this->refDB
->write(self
::$testDatastore);
81 $refHistory = new \
ReferenceHistory();
82 $refHistory->write(self
::$testHistory);
83 $this->history
= new History(self
::$testHistory);
84 $this->bookmarkService
= new BookmarkFileService($this->conf
, $this->history
, $mutex, true);
86 $this->container
= new Container();
87 $this->container
['conf'] = $this->conf
;
88 $this->container
['db'] = $this->bookmarkService
;
89 $this->container
['history'] = $this->history
;
91 $this->controller
= new Links($this->container
);
93 $mock = $this->createMock(Router
::class);
94 $mock->expects($this->any())
96 ->willReturn('/api/v1/bookmarks/1');
98 // affect @property-read... seems to work
99 $this->controller
->getCi()->router
= $mock;
101 // Used by index_url().
102 $this->controller
->getCi()['environment'] = [
103 'SERVER_NAME' => 'domain.tld',
105 'SCRIPT_NAME' => '/',
110 * After every test, remove the test datastore.
112 protected function tearDown(): void
114 @unlink(self
::$testDatastore);
115 @unlink(self
::$testHistory);
119 * Test link creation without any field: creates a blank note.
121 public function testPostLinkMinimal()
123 $env = Environment
::mock([
124 'REQUEST_METHOD' => 'POST',
127 $request = Request
::createFromEnvironment($env);
129 $response = $this->controller
->postLink($request, new Response());
130 $this->assertEquals(201, $response->getStatusCode());
131 $this->assertEquals('/api/v1/bookmarks/1', $response->getHeader('Location')[0]);
132 $data = json_decode((string) $response->getBody(), true);
133 $this->assertEquals(self
::NB_FIELDS_LINK
, count($data));
134 $this->assertEquals(43, $data['id']);
135 $this->assertRegExp('/[\w_-]{6}/', $data['shorturl']);
136 $this->assertEquals('http://domain.tld/shaare/' . $data['shorturl'], $data['url']);
137 $this->assertEquals('/shaare/' . $data['shorturl'], $data['title']);
138 $this->assertEquals('', $data['description']);
139 $this->assertEquals([], $data['tags']);
140 $this->assertEquals(true, $data['private']);
142 new \
DateTime('5 seconds ago') < \DateTime
::createFromFormat(\DateTime
::ATOM
, $data['created'])
144 $this->assertEquals('', $data['updated']);
146 $historyEntry = $this->history
->getHistory()[0];
147 $this->assertEquals(History
::CREATED
, $historyEntry['event']);
149 (new \
DateTime())->add(\DateInterval
::createFromDateString('-5 seconds')) < $historyEntry['datetime']
151 $this->assertEquals(43, $historyEntry['id']);
155 * Test link creation with all available fields.
157 public function testPostLinkFull()
160 'url' => 'website.tld/test?foo=bar',
161 'title' => 'new entry',
162 'description' => 'shaare description',
163 'tags' => ['one', 'two'],
165 'created' => '2015-05-05T12:30:00+03:00',
166 'updated' => '2016-06-05T14:32:10+03:00',
168 $env = Environment
::mock([
169 'REQUEST_METHOD' => 'POST',
170 'CONTENT_TYPE' => 'application/json'
173 $request = Request
::createFromEnvironment($env);
174 $request = $request->withParsedBody($link);
175 $response = $this->controller
->postLink($request, new Response());
177 $this->assertEquals(201, $response->getStatusCode());
178 $this->assertEquals('/api/v1/bookmarks/1', $response->getHeader('Location')[0]);
179 $data = json_decode((string) $response->getBody(), true);
180 $this->assertEquals(self
::NB_FIELDS_LINK
, count($data));
181 $this->assertEquals(43, $data['id']);
182 $this->assertRegExp('/[\w_-]{6}/', $data['shorturl']);
183 $this->assertEquals('http://' . $link['url'], $data['url']);
184 $this->assertEquals($link['title'], $data['title']);
185 $this->assertEquals($link['description'], $data['description']);
186 $this->assertEquals($link['tags'], $data['tags']);
187 $this->assertEquals(true, $data['private']);
188 $this->assertSame($link['created'], $data['created']);
189 $this->assertSame($link['updated'], $data['updated']);
193 * Test link creation with an existing link (duplicate URL). Should return a 409 HTTP error and the existing link.
195 public function testPostLinkDuplicate()
198 'url' => 'mediagoblin.org/',
199 'title' => 'new entry',
200 'description' => 'shaare description',
201 'tags' => ['one', 'two'],
204 $env = Environment
::mock([
205 'REQUEST_METHOD' => 'POST',
206 'CONTENT_TYPE' => 'application/json'
209 $request = Request
::createFromEnvironment($env);
210 $request = $request->withParsedBody($link);
211 $response = $this->controller
->postLink($request, new Response());
213 $this->assertEquals(409, $response->getStatusCode());
214 $data = json_decode((string) $response->getBody(), true);
215 $this->assertEquals(self
::NB_FIELDS_LINK
, count($data));
216 $this->assertEquals(7, $data['id']);
217 $this->assertEquals('IuWvgA', $data['shorturl']);
218 $this->assertEquals('http://mediagoblin.org/', $data['url']);
219 $this->assertEquals('MediaGoblin', $data['title']);
220 $this->assertEquals('A free software media publishing platform #hashtagOther', $data['description']);
221 $this->assertEquals(['gnu', 'media', 'web', '.hidden', 'hashtag'], $data['tags']);
222 $this->assertEquals(false, $data['private']);
224 \DateTime
::createFromFormat(Bookmark
::LINK_DATE_FORMAT
, '20130614_184135'),
225 \DateTime
::createFromFormat(\DateTime
::ATOM
, $data['created'])
228 \DateTime
::createFromFormat(Bookmark
::LINK_DATE_FORMAT
, '20130615_184230'),
229 \DateTime
::createFromFormat(\DateTime
::ATOM
, $data['updated'])