3 namespace Shaarli\Api\Controllers
;
6 use Shaarli\Config\ConfigManager
;
8 use Slim\Http\Environment
;
10 use Slim\Http\Response
;
15 * Test POST Link REST API service.
17 * @package Shaarli\Api\Controllers
19 class PostLinkTest
extends \PHPUnit_Framework_TestCase
22 * @var string datastore to test write operations
24 protected static $testDatastore = 'sandbox/datastore.php';
27 * @var string datastore to test write operations
29 protected static $testHistory = 'sandbox/history.php';
32 * @var ConfigManager instance
37 * @var \ReferenceLinkDB instance.
39 protected $refDB = null;
42 * @var \History instance.
47 * @var Container instance.
52 * @var Links controller instance.
54 protected $controller;
57 * Number of JSON field per link.
59 const NB_FIELDS_LINK
= 9;
62 * Before every test, instantiate a new Api with its config, plugins and links.
64 public function setUp()
66 $this->conf
= new ConfigManager('tests/utils/config/configJson.json.php');
67 $this->refDB
= new \
ReferenceLinkDB();
68 $this->refDB
->write(self
::$testDatastore);
70 $refHistory = new \
ReferenceHistory();
71 $refHistory->write(self
::$testHistory);
72 $this->history
= new \
History(self
::$testHistory);
74 $this->container
= new Container();
75 $this->container
['conf'] = $this->conf
;
76 $this->container
['db'] = new \
LinkDB(self
::$testDatastore, true, false);
77 $this->container
['history'] = new \
History(self
::$testHistory);
79 $this->controller
= new Links($this->container
);
81 $mock = $this->getMock('\Slim\Router', ['relativePathFor']);
82 $mock->expects($this->any())
83 ->method('relativePathFor')
84 ->willReturn('api/v1/links/1');
86 // affect @property-read... seems to work
87 $this->controller
->getCi()->router
= $mock;
89 // Used by index_url().
90 $this->controller
->getCi()['environment'] = [
91 'SERVER_NAME' => 'domain.tld',
98 * After every test, remove the test datastore.
100 public function tearDown()
102 @unlink(self
::$testDatastore);
103 @unlink(self
::$testHistory);
107 * Test link creation without any field: creates a blank note.
109 public function testPostLinkMinimal()
111 $env = Environment
::mock([
112 'REQUEST_METHOD' => 'POST',
115 $request = Request
::createFromEnvironment($env);
117 $response = $this->controller
->postLink($request, new Response());
118 $this->assertEquals(201, $response->getStatusCode());
119 $this->assertEquals('api/v1/links/1', $response->getHeader('Location')[0]);
120 $data = json_decode((string) $response->getBody(), true);
121 $this->assertEquals(self
::NB_FIELDS_LINK
, count($data));
122 $this->assertEquals(43, $data['id']);
123 $this->assertRegExp('/[\w-_]{6}/', $data['shorturl']);
124 $this->assertEquals('http://domain.tld/?' . $data['shorturl'], $data['url']);
125 $this->assertEquals('?' . $data['shorturl'], $data['title']);
126 $this->assertEquals('', $data['description']);
127 $this->assertEquals([], $data['tags']);
128 $this->assertEquals(false, $data['private']);
129 $this->assertTrue(new \
DateTime('5 seconds ago') < \DateTime
::createFromFormat(\DateTime
::ATOM
, $data['created']));
130 $this->assertEquals('', $data['updated']);
132 $historyEntry = $this->history
->getHistory()[0];
133 $this->assertEquals(\History
::CREATED
, $historyEntry['event']);
135 (new \
DateTime())->add(\DateInterval
::createFromDateString('-5 seconds')) < $historyEntry['datetime']
137 $this->assertEquals(43, $historyEntry['id']);
141 * Test link creation with all available fields.
143 public function testPostLinkFull()
146 'url' => 'website.tld/test?foo=bar',
147 'title' => 'new entry',
148 'description' => 'shaare description',
149 'tags' => ['one', 'two'],
152 $env = Environment
::mock([
153 'REQUEST_METHOD' => 'POST',
154 'CONTENT_TYPE' => 'application/json'
157 $request = Request
::createFromEnvironment($env);
158 $request = $request->withParsedBody($link);
159 $response = $this->controller
->postLink($request, new Response());
161 $this->assertEquals(201, $response->getStatusCode());
162 $this->assertEquals('api/v1/links/1', $response->getHeader('Location')[0]);
163 $data = json_decode((string) $response->getBody(), true);
164 $this->assertEquals(self
::NB_FIELDS_LINK
, count($data));
165 $this->assertEquals(43, $data['id']);
166 $this->assertRegExp('/[\w-_]{6}/', $data['shorturl']);
167 $this->assertEquals('http://' . $link['url'], $data['url']);
168 $this->assertEquals($link['title'], $data['title']);
169 $this->assertEquals($link['description'], $data['description']);
170 $this->assertEquals($link['tags'], $data['tags']);
171 $this->assertEquals(true, $data['private']);
172 $this->assertTrue(new \
DateTime('2 seconds ago') < \DateTime
::createFromFormat(\DateTime
::ATOM
, $data['created']));
173 $this->assertEquals('', $data['updated']);
177 * Test link creation with an existing link (duplicate URL). Should return a 409 HTTP error and the existing link.
179 public function testPostLinkDuplicate()
182 'url' => 'mediagoblin.org/',
183 'title' => 'new entry',
184 'description' => 'shaare description',
185 'tags' => ['one', 'two'],
188 $env = Environment
::mock([
189 'REQUEST_METHOD' => 'POST',
190 'CONTENT_TYPE' => 'application/json'
193 $request = Request
::createFromEnvironment($env);
194 $request = $request->withParsedBody($link);
195 $response = $this->controller
->postLink($request, new Response());
197 $this->assertEquals(409, $response->getStatusCode());
198 $data = json_decode((string) $response->getBody(), true);
199 $this->assertEquals(self
::NB_FIELDS_LINK
, count($data));
200 $this->assertEquals(7, $data['id']);
201 $this->assertEquals('IuWvgA', $data['shorturl']);
202 $this->assertEquals('http://mediagoblin.org/', $data['url']);
203 $this->assertEquals('MediaGoblin', $data['title']);
204 $this->assertEquals('A free software media publishing platform #hashtagOther', $data['description']);
205 $this->assertEquals(['gnu', 'media', 'web', '.hidden', 'hashtag'], $data['tags']);
206 $this->assertEquals(false, $data['private']);
208 \DateTime
::createFromFormat(\LinkDB
::LINK_DATE_FORMAT
, '20130614_184135'),
209 \DateTime
::createFromFormat(\DateTime
::ATOM
, $data['created'])
212 \DateTime
::createFromFormat(\LinkDB
::LINK_DATE_FORMAT
, '20130615_184230'),
213 \DateTime
::createFromFormat(\DateTime
::ATOM
, $data['updated'])