3 namespace Shaarli\Api\Controllers
;
6 use PHPUnit\Framework\TestCase
;
7 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 \History 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 \
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']);
131 $this->assertTrue(new \
DateTime('5 seconds ago') < \DateTime
::createFromFormat(\DateTime
::ATOM
, $data['created']));
132 $this->assertEquals('', $data['updated']);
134 $historyEntry = $this->history
->getHistory()[0];
135 $this->assertEquals(\History
::CREATED
, $historyEntry['event']);
137 (new \
DateTime())->add(\DateInterval
::createFromDateString('-5 seconds')) < $historyEntry['datetime']
139 $this->assertEquals(43, $historyEntry['id']);
143 * Test link creation with all available fields.
145 public function testPostLinkFull()
148 'url' => 'website.tld/test?foo=bar',
149 'title' => 'new entry',
150 'description' => 'shaare description',
151 'tags' => ['one', 'two'],
154 $env = Environment
::mock([
155 'REQUEST_METHOD' => 'POST',
156 'CONTENT_TYPE' => 'application/json'
159 $request = Request
::createFromEnvironment($env);
160 $request = $request->withParsedBody($link);
161 $response = $this->controller
->postLink($request, new Response());
163 $this->assertEquals(201, $response->getStatusCode());
164 $this->assertEquals('api/v1/links/1', $response->getHeader('Location')[0]);
165 $data = json_decode((string) $response->getBody(), true);
166 $this->assertEquals(self
::NB_FIELDS_LINK
, count($data));
167 $this->assertEquals(43, $data['id']);
168 $this->assertRegExp('/[\w-_]{6}/', $data['shorturl']);
169 $this->assertEquals('http://' . $link['url'], $data['url']);
170 $this->assertEquals($link['title'], $data['title']);
171 $this->assertEquals($link['description'], $data['description']);
172 $this->assertEquals($link['tags'], $data['tags']);
173 $this->assertEquals(true, $data['private']);
174 $this->assertTrue(new \
DateTime('2 seconds ago') < \DateTime
::createFromFormat(\DateTime
::ATOM
, $data['created']));
175 $this->assertEquals('', $data['updated']);
179 * Test link creation with an existing link (duplicate URL). Should return a 409 HTTP error and the existing link.
181 public function testPostLinkDuplicate()
184 'url' => 'mediagoblin.org/',
185 'title' => 'new entry',
186 'description' => 'shaare description',
187 'tags' => ['one', 'two'],
190 $env = Environment
::mock([
191 'REQUEST_METHOD' => 'POST',
192 'CONTENT_TYPE' => 'application/json'
195 $request = Request
::createFromEnvironment($env);
196 $request = $request->withParsedBody($link);
197 $response = $this->controller
->postLink($request, new Response());
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(\LinkDB
::LINK_DATE_FORMAT
, '20130614_184135'),
211 \DateTime
::createFromFormat(\DateTime
::ATOM
, $data['created'])
214 \DateTime
::createFromFormat(\LinkDB
::LINK_DATE_FORMAT
, '20130615_184230'),
215 \DateTime
::createFromFormat(\DateTime
::ATOM
, $data['updated'])