3 namespace Shaarli\Api\Controllers
;
5 use PHPUnit\Framework\TestCase
;
6 use Shaarli\Config\ConfigManager
;
8 use Slim\Http\Environment
;
10 use Slim\Http\Response
;
16 * Test POST Link REST API service.
18 * @package Shaarli\Api\Controllers
20 class PostLinkTest
extends TestCase
23 * @var string datastore to test write operations
25 protected static $testDatastore = 'sandbox/datastore.php';
28 * @var string datastore to test write operations
30 protected static $testHistory = 'sandbox/history.php';
33 * @var ConfigManager instance
38 * @var \ReferenceLinkDB instance.
40 protected $refDB = null;
43 * @var \History 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 links.
65 public function setUp()
67 $this->conf
= new ConfigManager('tests/utils/config/configJson.json.php');
68 $this->refDB
= new \
ReferenceLinkDB();
69 $this->refDB
->write(self
::$testDatastore);
71 $refHistory = new \
ReferenceHistory();
72 $refHistory->write(self
::$testHistory);
73 $this->history
= new \
History(self
::$testHistory);
75 $this->container
= new Container();
76 $this->container
['conf'] = $this->conf
;
77 $this->container
['db'] = new \
LinkDB(self
::$testDatastore, true, false);
78 $this->container
['history'] = new \
History(self
::$testHistory);
80 $this->controller
= new Links($this->container
);
82 $mock = $this->createMock(Router
::class);
83 $mock->expects($this->any())
84 ->method('relativePathFor')
85 ->willReturn('api/v1/links/1');
87 // affect @property-read... seems to work
88 $this->controller
->getCi()->router
= $mock;
90 // Used by index_url().
91 $this->controller
->getCi()['environment'] = [
92 'SERVER_NAME' => 'domain.tld',
99 * After every test, remove the test datastore.
101 public function tearDown()
103 @unlink(self
::$testDatastore);
104 @unlink(self
::$testHistory);
108 * Test link creation without any field: creates a blank note.
110 public function testPostLinkMinimal()
112 $env = Environment
::mock([
113 'REQUEST_METHOD' => 'POST',
116 $request = Request
::createFromEnvironment($env);
118 $response = $this->controller
->postLink($request, new Response());
119 $this->assertEquals(201, $response->getStatusCode());
120 $this->assertEquals('api/v1/links/1', $response->getHeader('Location')[0]);
121 $data = json_decode((string) $response->getBody(), true);
122 $this->assertEquals(self
::NB_FIELDS_LINK
, count($data));
123 $this->assertEquals(43, $data['id']);
124 $this->assertRegExp('/[\w-_]{6}/', $data['shorturl']);
125 $this->assertEquals('http://domain.tld/?' . $data['shorturl'], $data['url']);
126 $this->assertEquals('?' . $data['shorturl'], $data['title']);
127 $this->assertEquals('', $data['description']);
128 $this->assertEquals([], $data['tags']);
129 $this->assertEquals(false, $data['private']);
131 new \
DateTime('5 seconds ago') < \DateTime
::createFromFormat(\DateTime
::ATOM
, $data['created'])
133 $this->assertEquals('', $data['updated']);
135 $historyEntry = $this->history
->getHistory()[0];
136 $this->assertEquals(\History
::CREATED
, $historyEntry['event']);
138 (new \
DateTime())->add(\DateInterval
::createFromDateString('-5 seconds')) < $historyEntry['datetime']
140 $this->assertEquals(43, $historyEntry['id']);
144 * Test link creation with all available fields.
146 public function testPostLinkFull()
149 'url' => 'website.tld/test?foo=bar',
150 'title' => 'new entry',
151 'description' => 'shaare description',
152 'tags' => ['one', 'two'],
155 $env = Environment
::mock([
156 'REQUEST_METHOD' => 'POST',
157 'CONTENT_TYPE' => 'application/json'
160 $request = Request
::createFromEnvironment($env);
161 $request = $request->withParsedBody($link);
162 $response = $this->controller
->postLink($request, new Response());
164 $this->assertEquals(201, $response->getStatusCode());
165 $this->assertEquals('api/v1/links/1', $response->getHeader('Location')[0]);
166 $data = json_decode((string) $response->getBody(), true);
167 $this->assertEquals(self
::NB_FIELDS_LINK
, count($data));
168 $this->assertEquals(43, $data['id']);
169 $this->assertRegExp('/[\w-_]{6}/', $data['shorturl']);
170 $this->assertEquals('http://' . $link['url'], $data['url']);
171 $this->assertEquals($link['title'], $data['title']);
172 $this->assertEquals($link['description'], $data['description']);
173 $this->assertEquals($link['tags'], $data['tags']);
174 $this->assertEquals(true, $data['private']);
176 new \
DateTime('2 seconds ago') < \DateTime
::createFromFormat(\DateTime
::ATOM
, $data['created'])
178 $this->assertEquals('', $data['updated']);
182 * Test link creation with an existing link (duplicate URL). Should return a 409 HTTP error and the existing link.
184 public function testPostLinkDuplicate()
187 'url' => 'mediagoblin.org/',
188 'title' => 'new entry',
189 'description' => 'shaare description',
190 'tags' => ['one', 'two'],
193 $env = Environment
::mock([
194 'REQUEST_METHOD' => 'POST',
195 'CONTENT_TYPE' => 'application/json'
198 $request = Request
::createFromEnvironment($env);
199 $request = $request->withParsedBody($link);
200 $response = $this->controller
->postLink($request, new Response());
202 $this->assertEquals(409, $response->getStatusCode());
203 $data = json_decode((string) $response->getBody(), true);
204 $this->assertEquals(self
::NB_FIELDS_LINK
, count($data));
205 $this->assertEquals(7, $data['id']);
206 $this->assertEquals('IuWvgA', $data['shorturl']);
207 $this->assertEquals('http://mediagoblin.org/', $data['url']);
208 $this->assertEquals('MediaGoblin', $data['title']);
209 $this->assertEquals('A free software media publishing platform #hashtagOther', $data['description']);
210 $this->assertEquals(['gnu', 'media', 'web', '.hidden', 'hashtag'], $data['tags']);
211 $this->assertEquals(false, $data['private']);
213 \DateTime
::createFromFormat(\LinkDB
::LINK_DATE_FORMAT
, '20130614_184135'),
214 \DateTime
::createFromFormat(\DateTime
::ATOM
, $data['created'])
217 \DateTime
::createFromFormat(\LinkDB
::LINK_DATE_FORMAT
, '20130615_184230'),
218 \DateTime
::createFromFormat(\DateTime
::ATOM
, $data['updated'])