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']);
130 $this->assertTrue(new \
DateTime('5 seconds ago') < \DateTime
::createFromFormat(\DateTime
::ATOM
, $data['created']));
131 $this->assertEquals('', $data['updated']);
133 $historyEntry = $this->history
->getHistory()[0];
134 $this->assertEquals(\History
::CREATED
, $historyEntry['event']);
136 (new \
DateTime())->add(\DateInterval
::createFromDateString('-5 seconds')) < $historyEntry['datetime']
138 $this->assertEquals(43, $historyEntry['id']);
142 * Test link creation with all available fields.
144 public function testPostLinkFull()
147 'url' => 'website.tld/test?foo=bar',
148 'title' => 'new entry',
149 'description' => 'shaare description',
150 'tags' => ['one', 'two'],
153 $env = Environment
::mock([
154 'REQUEST_METHOD' => 'POST',
155 'CONTENT_TYPE' => 'application/json'
158 $request = Request
::createFromEnvironment($env);
159 $request = $request->withParsedBody($link);
160 $response = $this->controller
->postLink($request, new Response());
162 $this->assertEquals(201, $response->getStatusCode());
163 $this->assertEquals('api/v1/links/1', $response->getHeader('Location')[0]);
164 $data = json_decode((string) $response->getBody(), true);
165 $this->assertEquals(self
::NB_FIELDS_LINK
, count($data));
166 $this->assertEquals(43, $data['id']);
167 $this->assertRegExp('/[\w-_]{6}/', $data['shorturl']);
168 $this->assertEquals('http://' . $link['url'], $data['url']);
169 $this->assertEquals($link['title'], $data['title']);
170 $this->assertEquals($link['description'], $data['description']);
171 $this->assertEquals($link['tags'], $data['tags']);
172 $this->assertEquals(true, $data['private']);
173 $this->assertTrue(new \
DateTime('2 seconds ago') < \DateTime
::createFromFormat(\DateTime
::ATOM
, $data['created']));
174 $this->assertEquals('', $data['updated']);
178 * Test link creation with an existing link (duplicate URL). Should return a 409 HTTP error and the existing link.
180 public function testPostLinkDuplicate()
183 'url' => 'mediagoblin.org/',
184 'title' => 'new entry',
185 'description' => 'shaare description',
186 'tags' => ['one', 'two'],
189 $env = Environment
::mock([
190 'REQUEST_METHOD' => 'POST',
191 'CONTENT_TYPE' => 'application/json'
194 $request = Request
::createFromEnvironment($env);
195 $request = $request->withParsedBody($link);
196 $response = $this->controller
->postLink($request, new Response());
198 $this->assertEquals(409, $response->getStatusCode());
199 $data = json_decode((string) $response->getBody(), true);
200 $this->assertEquals(self
::NB_FIELDS_LINK
, count($data));
201 $this->assertEquals(7, $data['id']);
202 $this->assertEquals('IuWvgA', $data['shorturl']);
203 $this->assertEquals('http://mediagoblin.org/', $data['url']);
204 $this->assertEquals('MediaGoblin', $data['title']);
205 $this->assertEquals('A free software media publishing platform #hashtagOther', $data['description']);
206 $this->assertEquals(['gnu', 'media', 'web', '.hidden', 'hashtag'], $data['tags']);
207 $this->assertEquals(false, $data['private']);
209 \DateTime
::createFromFormat(\LinkDB
::LINK_DATE_FORMAT
, '20130614_184135'),
210 \DateTime
::createFromFormat(\DateTime
::ATOM
, $data['created'])
213 \DateTime
::createFromFormat(\LinkDB
::LINK_DATE_FORMAT
, '20130615_184230'),
214 \DateTime
::createFromFormat(\DateTime
::ATOM
, $data['updated'])