3 namespace Shaarli\Api\Controllers
;
5 use PHPUnit\Framework\TestCase
;
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
;
19 * Test POST Link REST API service.
21 * @package Shaarli\Api\Controllers
23 class PostLinkTest
extends TestCase
26 * @var string datastore to test write operations
28 protected static $testDatastore = 'sandbox/datastore.php';
31 * @var string datastore to test write operations
33 protected static $testHistory = 'sandbox/history.php';
36 * @var ConfigManager instance
41 * @var \ReferenceLinkDB instance.
43 protected $refDB = null;
46 * @var BookmarkFileService instance.
48 protected $bookmarkService;
51 * @var HistoryController instance.
56 * @var Container instance.
61 * @var Links controller instance.
63 protected $controller;
66 * Number of JSON field per link.
68 const NB_FIELDS_LINK
= 9;
71 * Before every test, instantiate a new Api with its config, plugins and bookmarks.
73 public function setUp()
75 $this->conf
= new ConfigManager('tests/utils/config/configJson');
76 $this->conf
->set('resource.datastore', self
::$testDatastore);
77 $this->refDB
= new \
ReferenceLinkDB();
78 $this->refDB
->write(self
::$testDatastore);
79 $refHistory = new \
ReferenceHistory();
80 $refHistory->write(self
::$testHistory);
81 $this->history
= new History(self
::$testHistory);
82 $this->bookmarkService
= new BookmarkFileService($this->conf
, $this->history
, true);
84 $this->container
= new Container();
85 $this->container
['conf'] = $this->conf
;
86 $this->container
['db'] = $this->bookmarkService
;
87 $this->container
['history'] = $this->history
;
89 $this->controller
= new Links($this->container
);
91 $mock = $this->createMock(Router
::class);
92 $mock->expects($this->any())
94 ->willReturn('/api/v1/bookmarks/1');
96 // affect @property-read... seems to work
97 $this->controller
->getCi()->router
= $mock;
99 // Used by index_url().
100 $this->controller
->getCi()['environment'] = [
101 'SERVER_NAME' => 'domain.tld',
103 'SCRIPT_NAME' => '/',
108 * After every test, remove the test datastore.
110 public function tearDown()
112 @unlink(self
::$testDatastore);
113 @unlink(self
::$testHistory);
117 * Test link creation without any field: creates a blank note.
119 public function testPostLinkMinimal()
121 $env = Environment
::mock([
122 'REQUEST_METHOD' => 'POST',
125 $request = Request
::createFromEnvironment($env);
127 $response = $this->controller
->postLink($request, new Response());
128 $this->assertEquals(201, $response->getStatusCode());
129 $this->assertEquals('/api/v1/bookmarks/1', $response->getHeader('Location')[0]);
130 $data = json_decode((string) $response->getBody(), true);
131 $this->assertEquals(self
::NB_FIELDS_LINK
, count($data));
132 $this->assertEquals(43, $data['id']);
133 $this->assertRegExp('/[\w_-]{6}/', $data['shorturl']);
134 $this->assertEquals('http://domain.tld/?' . $data['shorturl'], $data['url']);
135 $this->assertEquals('?' . $data['shorturl'], $data['title']);
136 $this->assertEquals('', $data['description']);
137 $this->assertEquals([], $data['tags']);
138 $this->assertEquals(true, $data['private']);
140 new \
DateTime('5 seconds ago') < \DateTime
::createFromFormat(\DateTime
::ATOM
, $data['created'])
142 $this->assertEquals('', $data['updated']);
144 $historyEntry = $this->history
->getHistory()[0];
145 $this->assertEquals(History
::CREATED
, $historyEntry['event']);
147 (new \
DateTime())->add(\DateInterval
::createFromDateString('-5 seconds')) < $historyEntry['datetime']
149 $this->assertEquals(43, $historyEntry['id']);
153 * Test link creation with all available fields.
155 public function testPostLinkFull()
158 'url' => 'website.tld/test?foo=bar',
159 'title' => 'new entry',
160 'description' => 'shaare description',
161 'tags' => ['one', 'two'],
164 $env = Environment
::mock([
165 'REQUEST_METHOD' => 'POST',
166 'CONTENT_TYPE' => 'application/json'
169 $request = Request
::createFromEnvironment($env);
170 $request = $request->withParsedBody($link);
171 $response = $this->controller
->postLink($request, new Response());
173 $this->assertEquals(201, $response->getStatusCode());
174 $this->assertEquals('/api/v1/bookmarks/1', $response->getHeader('Location')[0]);
175 $data = json_decode((string) $response->getBody(), true);
176 $this->assertEquals(self
::NB_FIELDS_LINK
, count($data));
177 $this->assertEquals(43, $data['id']);
178 $this->assertRegExp('/[\w_-]{6}/', $data['shorturl']);
179 $this->assertEquals('http://' . $link['url'], $data['url']);
180 $this->assertEquals($link['title'], $data['title']);
181 $this->assertEquals($link['description'], $data['description']);
182 $this->assertEquals($link['tags'], $data['tags']);
183 $this->assertEquals(true, $data['private']);
185 new \
DateTime('2 seconds ago') < \DateTime
::createFromFormat(\DateTime
::ATOM
, $data['created'])
187 $this->assertEquals('', $data['updated']);
191 * Test link creation with an existing link (duplicate URL). Should return a 409 HTTP error and the existing link.
193 public function testPostLinkDuplicate()
196 'url' => 'mediagoblin.org/',
197 'title' => 'new entry',
198 'description' => 'shaare description',
199 'tags' => ['one', 'two'],
202 $env = Environment
::mock([
203 'REQUEST_METHOD' => 'POST',
204 'CONTENT_TYPE' => 'application/json'
207 $request = Request
::createFromEnvironment($env);
208 $request = $request->withParsedBody($link);
209 $response = $this->controller
->postLink($request, new Response());
211 $this->assertEquals(409, $response->getStatusCode());
212 $data = json_decode((string) $response->getBody(), true);
213 $this->assertEquals(self
::NB_FIELDS_LINK
, count($data));
214 $this->assertEquals(7, $data['id']);
215 $this->assertEquals('IuWvgA', $data['shorturl']);
216 $this->assertEquals('http://mediagoblin.org/', $data['url']);
217 $this->assertEquals('MediaGoblin', $data['title']);
218 $this->assertEquals('A free software media publishing platform #hashtagOther', $data['description']);
219 $this->assertEquals(['gnu', 'media', 'web', '.hidden', 'hashtag'], $data['tags']);
220 $this->assertEquals(false, $data['private']);
222 \DateTime
::createFromFormat(Bookmark
::LINK_DATE_FORMAT
, '20130614_184135'),
223 \DateTime
::createFromFormat(\DateTime
::ATOM
, $data['created'])
226 \DateTime
::createFromFormat(Bookmark
::LINK_DATE_FORMAT
, '20130615_184230'),
227 \DateTime
::createFromFormat(\DateTime
::ATOM
, $data['updated'])