4 namespace Shaarli\Api\Controllers
;
6 use Shaarli\Config\ConfigManager
;
8 use Slim\Http\Environment
;
10 use Slim\Http\Response
;
12 class PutLinkTest
extends \PHPUnit_Framework_TestCase
15 * @var string datastore to test write operations
17 protected static $testDatastore = 'sandbox/datastore.php';
20 * @var string datastore to test write operations
22 protected static $testHistory = 'sandbox/history.php';
25 * @var ConfigManager instance
30 * @var \ReferenceLinkDB instance.
32 protected $refDB = null;
35 * @var \History instance.
40 * @var Container instance.
45 * @var Links controller instance.
47 protected $controller;
50 * Number of JSON field per link.
52 const NB_FIELDS_LINK
= 9;
55 * Before every test, instantiate a new Api with its config, plugins and links.
57 public function setUp()
59 $this->conf
= new ConfigManager('tests/utils/config/configJson.json.php');
60 $this->refDB
= new \
ReferenceLinkDB();
61 $this->refDB
->write(self
::$testDatastore);
63 $refHistory = new \
ReferenceHistory();
64 $refHistory->write(self
::$testHistory);
65 $this->history
= new \
History(self
::$testHistory);
67 $this->container
= new Container();
68 $this->container
['conf'] = $this->conf
;
69 $this->container
['db'] = new \
LinkDB(self
::$testDatastore, true, false);
70 $this->container
['history'] = new \
History(self
::$testHistory);
72 $this->controller
= new Links($this->container
);
74 // Used by index_url().
75 $this->controller
->getCi()['environment'] = [
76 'SERVER_NAME' => 'domain.tld',
83 * After every test, remove the test datastore.
85 public function tearDown()
87 @unlink(self
::$testDatastore);
88 @unlink(self
::$testHistory);
92 * Test link update without value: reset the link to default values
94 public function testPutLinkMinimal()
96 $env = Environment
::mock([
97 'REQUEST_METHOD' => 'PUT',
100 $request = Request
::createFromEnvironment($env);
102 $response = $this->controller
->putLink($request, new Response(), ['id' => $id]);
103 $this->assertEquals(200, $response->getStatusCode());
104 $data = json_decode((string) $response->getBody(), true);
105 $this->assertEquals(self
::NB_FIELDS_LINK
, count($data));
106 $this->assertEquals($id, $data['id']);
107 $this->assertEquals('WDWyig', $data['shorturl']);
108 $this->assertEquals('http://domain.tld/?WDWyig', $data['url']);
109 $this->assertEquals('?WDWyig', $data['title']);
110 $this->assertEquals('', $data['description']);
111 $this->assertEquals([], $data['tags']);
112 $this->assertEquals(false, $data['private']);
114 \DateTime
::createFromFormat('Ymd_His', '20150310_114651'),
115 \DateTime
::createFromFormat(\DateTime
::ATOM
, $data['created'])
117 $this->assertTrue(new \
DateTime('5 seconds ago') < \DateTime
::createFromFormat(\DateTime
::ATOM
, $data['updated']));
119 $historyEntry = $this->history
->getHistory()[0];
120 $this->assertEquals(\History
::UPDATED
, $historyEntry['event']);
122 (new \
DateTime())->add(\DateInterval
::createFromDateString('-5 seconds')) < $historyEntry['datetime']
124 $this->assertEquals($id, $historyEntry['id']);
128 * Test link update with new values
130 public function testPutLinkWithValues()
132 $env = Environment
::mock([
133 'REQUEST_METHOD' => 'PUT',
134 'CONTENT_TYPE' => 'application/json'
138 'url' => 'http://somewhere.else',
140 'description' => 'Percé jusques au fond du cœur [...]',
141 'tags' => ['corneille', 'rodrigue'],
144 $request = Request
::createFromEnvironment($env);
145 $request = $request->withParsedBody($update);
147 $response = $this->controller
->putLink($request, new Response(), ['id' => $id]);
148 $this->assertEquals(200, $response->getStatusCode());
149 $data = json_decode((string) $response->getBody(), true);
150 $this->assertEquals(self
::NB_FIELDS_LINK
, count($data));
151 $this->assertEquals($id, $data['id']);
152 $this->assertEquals('WDWyig', $data['shorturl']);
153 $this->assertEquals('http://somewhere.else', $data['url']);
154 $this->assertEquals('Le Cid', $data['title']);
155 $this->assertEquals('Percé jusques au fond du cœur [...]', $data['description']);
156 $this->assertEquals(['corneille', 'rodrigue'], $data['tags']);
157 $this->assertEquals(true, $data['private']);
159 \DateTime
::createFromFormat('Ymd_His', '20150310_114651'),
160 \DateTime
::createFromFormat(\DateTime
::ATOM
, $data['created'])
162 $this->assertTrue(new \
DateTime('5 seconds ago') < \DateTime
::createFromFormat(\DateTime
::ATOM
, $data['updated']));
166 * Test link update with an existing URL: 409 Conflict with the existing link as body
168 public function testPutLinkDuplicate()
171 'url' => 'mediagoblin.org/',
172 'title' => 'new entry',
173 'description' => 'shaare description',
174 'tags' => ['one', 'two'],
177 $env = Environment
::mock([
178 'REQUEST_METHOD' => 'PUT',
179 'CONTENT_TYPE' => 'application/json'
182 $request = Request
::createFromEnvironment($env);
183 $request = $request->withParsedBody($link);
184 $response = $this->controller
->putLink($request, new Response(), ['id' => 41]);
186 $this->assertEquals(409, $response->getStatusCode());
187 $data = json_decode((string) $response->getBody(), true);
188 $this->assertEquals(self
::NB_FIELDS_LINK
, count($data));
189 $this->assertEquals(7, $data['id']);
190 $this->assertEquals('IuWvgA', $data['shorturl']);
191 $this->assertEquals('http://mediagoblin.org/', $data['url']);
192 $this->assertEquals('MediaGoblin', $data['title']);
193 $this->assertEquals('A free software media publishing platform #hashtagOther', $data['description']);
194 $this->assertEquals(['gnu', 'media', 'web', '.hidden', 'hashtag'], $data['tags']);
195 $this->assertEquals(false, $data['private']);
197 \DateTime
::createFromFormat(\LinkDB
::LINK_DATE_FORMAT
, '20130614_184135'),
198 \DateTime
::createFromFormat(\DateTime
::ATOM
, $data['created'])
201 \DateTime
::createFromFormat(\LinkDB
::LINK_DATE_FORMAT
, '20130615_184230'),
202 \DateTime
::createFromFormat(\DateTime
::ATOM
, $data['updated'])
207 * Test link update on non existent link => ApiLinkNotFoundException.
209 * @expectedException Shaarli\Api\Exceptions\ApiLinkNotFoundException
210 * @expectedExceptionMessage Link not found
212 public function testGetLink404()
214 $env = Environment
::mock([
215 'REQUEST_METHOD' => 'PUT',
217 $request = Request
::createFromEnvironment($env);
219 $this->controller
->putLink($request, new Response(), ['id' => -1]);