3 namespace Shaarli\Api\Controllers
;
5 use PHPUnit\Framework\TestCase
;
6 use Shaarli\Config\ConfigManager
;
9 use Slim\Http\Environment
;
10 use Slim\Http\Request
;
11 use Slim\Http\Response
;
17 * Test POST Link REST API service.
19 * @package Shaarli\Api\Controllers
21 class PostLinkTest
extends TestCase
24 * @var string datastore to test write operations
26 protected static $testDatastore = 'sandbox/datastore.php';
29 * @var string datastore to test write operations
31 protected static $testHistory = 'sandbox/history.php';
34 * @var ConfigManager instance
39 * @var \ReferenceLinkDB instance.
41 protected $refDB = null;
44 * @var HistoryController instance.
49 * @var Container instance.
54 * @var Links controller instance.
56 protected $controller;
59 * Number of JSON field per link.
61 const NB_FIELDS_LINK
= 9;
64 * Before every test, instantiate a new Api with its config, plugins and links.
66 public function setUp()
68 $this->conf
= new ConfigManager('tests/utils/config/configJson.json.php');
69 $this->refDB
= new \
ReferenceLinkDB();
70 $this->refDB
->write(self
::$testDatastore);
72 $refHistory = new \
ReferenceHistory();
73 $refHistory->write(self
::$testHistory);
74 $this->history
= new History(self
::$testHistory);
76 $this->container
= new Container();
77 $this->container
['conf'] = $this->conf
;
78 $this->container
['db'] = new \Shaarli\Bookmark\
LinkDB(self
::$testDatastore, true, false);
79 $this->container
['history'] = new History(self
::$testHistory);
81 $this->controller
= new Links($this->container
);
83 $mock = $this->createMock(Router
::class);
84 $mock->expects($this->any())
85 ->method('relativePathFor')
86 ->willReturn('api/v1/links/1');
88 // affect @property-read... seems to work
89 $this->controller
->getCi()->router
= $mock;
91 // Used by index_url().
92 $this->controller
->getCi()['environment'] = [
93 'SERVER_NAME' => 'domain.tld',
100 * After every test, remove the test datastore.
102 public function tearDown()
104 @unlink(self
::$testDatastore);
105 @unlink(self
::$testHistory);
109 * Test link creation without any field: creates a blank note.
111 public function testPostLinkMinimal()
113 $env = Environment
::mock([
114 'REQUEST_METHOD' => 'POST',
117 $request = Request
::createFromEnvironment($env);
119 $response = $this->controller
->postLink($request, new Response());
120 $this->assertEquals(201, $response->getStatusCode());
121 $this->assertEquals('api/v1/links/1', $response->getHeader('Location')[0]);
122 $data = json_decode((string) $response->getBody(), true);
123 $this->assertEquals(self
::NB_FIELDS_LINK
, count($data));
124 $this->assertEquals(43, $data['id']);
125 $this->assertRegExp('/[\w_-]{6}/', $data['shorturl']);
126 $this->assertEquals('http://domain.tld/?' . $data['shorturl'], $data['url']);
127 $this->assertEquals('?' . $data['shorturl'], $data['title']);
128 $this->assertEquals('', $data['description']);
129 $this->assertEquals([], $data['tags']);
130 $this->assertEquals(false, $data['private']);
132 new \
DateTime('5 seconds ago') < \DateTime
::createFromFormat(\DateTime
::ATOM
, $data['created'])
134 $this->assertEquals('', $data['updated']);
136 $historyEntry = $this->history
->getHistory()[0];
137 $this->assertEquals(History
::CREATED
, $historyEntry['event']);
139 (new \
DateTime())->add(\DateInterval
::createFromDateString('-5 seconds')) < $historyEntry['datetime']
141 $this->assertEquals(43, $historyEntry['id']);
145 * Test link creation with all available fields.
147 public function testPostLinkFull()
150 'url' => 'website.tld/test?foo=bar',
151 'title' => 'new entry',
152 'description' => 'shaare description',
153 'tags' => ['one', 'two'],
156 $env = Environment
::mock([
157 'REQUEST_METHOD' => 'POST',
158 'CONTENT_TYPE' => 'application/json'
161 $request = Request
::createFromEnvironment($env);
162 $request = $request->withParsedBody($link);
163 $response = $this->controller
->postLink($request, new Response());
165 $this->assertEquals(201, $response->getStatusCode());
166 $this->assertEquals('api/v1/links/1', $response->getHeader('Location')[0]);
167 $data = json_decode((string) $response->getBody(), true);
168 $this->assertEquals(self
::NB_FIELDS_LINK
, count($data));
169 $this->assertEquals(43, $data['id']);
170 $this->assertRegExp('/[\w_-]{6}/', $data['shorturl']);
171 $this->assertEquals('http://' . $link['url'], $data['url']);
172 $this->assertEquals($link['title'], $data['title']);
173 $this->assertEquals($link['description'], $data['description']);
174 $this->assertEquals($link['tags'], $data['tags']);
175 $this->assertEquals(true, $data['private']);
177 new \
DateTime('2 seconds ago') < \DateTime
::createFromFormat(\DateTime
::ATOM
, $data['created'])
179 $this->assertEquals('', $data['updated']);
183 * Test link creation with an existing link (duplicate URL). Should return a 409 HTTP error and the existing link.
185 public function testPostLinkDuplicate()
188 'url' => 'mediagoblin.org/',
189 'title' => 'new entry',
190 'description' => 'shaare description',
191 'tags' => ['one', 'two'],
194 $env = Environment
::mock([
195 'REQUEST_METHOD' => 'POST',
196 'CONTENT_TYPE' => 'application/json'
199 $request = Request
::createFromEnvironment($env);
200 $request = $request->withParsedBody($link);
201 $response = $this->controller
->postLink($request, new Response());
203 $this->assertEquals(409, $response->getStatusCode());
204 $data = json_decode((string) $response->getBody(), true);
205 $this->assertEquals(self
::NB_FIELDS_LINK
, count($data));
206 $this->assertEquals(7, $data['id']);
207 $this->assertEquals('IuWvgA', $data['shorturl']);
208 $this->assertEquals('http://mediagoblin.org/', $data['url']);
209 $this->assertEquals('MediaGoblin', $data['title']);
210 $this->assertEquals('A free software media publishing platform #hashtagOther', $data['description']);
211 $this->assertEquals(['gnu', 'media', 'web', '.hidden', 'hashtag'], $data['tags']);
212 $this->assertEquals(false, $data['private']);
214 \DateTime
::createFromFormat(\Shaarli\Bookmark\LinkDB
::LINK_DATE_FORMAT
, '20130614_184135'),
215 \DateTime
::createFromFormat(\DateTime
::ATOM
, $data['created'])
218 \DateTime
::createFromFormat(\Shaarli\Bookmark\LinkDB
::LINK_DATE_FORMAT
, '20130615_184230'),
219 \DateTime
::createFromFormat(\DateTime
::ATOM
, $data['updated'])