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 \Shaarli\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 \Shaarli\
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 \Shaarli\
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'])
118 new \
DateTime('5 seconds ago') < \DateTime
::createFromFormat(\DateTime
::ATOM
, $data['updated'])
121 $historyEntry = $this->history
->getHistory()[0];
122 $this->assertEquals(\Shaarli\History
::UPDATED
, $historyEntry['event']);
124 (new \
DateTime())->add(\DateInterval
::createFromDateString('-5 seconds')) < $historyEntry['datetime']
126 $this->assertEquals($id, $historyEntry['id']);
130 * Test link update with new values
132 public function testPutLinkWithValues()
134 $env = Environment
::mock([
135 'REQUEST_METHOD' => 'PUT',
136 'CONTENT_TYPE' => 'application/json'
140 'url' => 'http://somewhere.else',
142 'description' => 'Percé jusques au fond du cœur [...]',
143 'tags' => ['corneille', 'rodrigue'],
146 $request = Request
::createFromEnvironment($env);
147 $request = $request->withParsedBody($update);
149 $response = $this->controller
->putLink($request, new Response(), ['id' => $id]);
150 $this->assertEquals(200, $response->getStatusCode());
151 $data = json_decode((string) $response->getBody(), true);
152 $this->assertEquals(self
::NB_FIELDS_LINK
, count($data));
153 $this->assertEquals($id, $data['id']);
154 $this->assertEquals('WDWyig', $data['shorturl']);
155 $this->assertEquals('http://somewhere.else', $data['url']);
156 $this->assertEquals('Le Cid', $data['title']);
157 $this->assertEquals('Percé jusques au fond du cœur [...]', $data['description']);
158 $this->assertEquals(['corneille', 'rodrigue'], $data['tags']);
159 $this->assertEquals(true, $data['private']);
161 \DateTime
::createFromFormat('Ymd_His', '20150310_114651'),
162 \DateTime
::createFromFormat(\DateTime
::ATOM
, $data['created'])
165 new \
DateTime('5 seconds ago') < \DateTime
::createFromFormat(\DateTime
::ATOM
, $data['updated'])
170 * Test link update with an existing URL: 409 Conflict with the existing link as body
172 public function testPutLinkDuplicate()
175 'url' => 'mediagoblin.org/',
176 'title' => 'new entry',
177 'description' => 'shaare description',
178 'tags' => ['one', 'two'],
181 $env = Environment
::mock([
182 'REQUEST_METHOD' => 'PUT',
183 'CONTENT_TYPE' => 'application/json'
186 $request = Request
::createFromEnvironment($env);
187 $request = $request->withParsedBody($link);
188 $response = $this->controller
->putLink($request, new Response(), ['id' => 41]);
190 $this->assertEquals(409, $response->getStatusCode());
191 $data = json_decode((string) $response->getBody(), true);
192 $this->assertEquals(self
::NB_FIELDS_LINK
, count($data));
193 $this->assertEquals(7, $data['id']);
194 $this->assertEquals('IuWvgA', $data['shorturl']);
195 $this->assertEquals('http://mediagoblin.org/', $data['url']);
196 $this->assertEquals('MediaGoblin', $data['title']);
197 $this->assertEquals('A free software media publishing platform #hashtagOther', $data['description']);
198 $this->assertEquals(['gnu', 'media', 'web', '.hidden', 'hashtag'], $data['tags']);
199 $this->assertEquals(false, $data['private']);
201 \DateTime
::createFromFormat(\LinkDB
::LINK_DATE_FORMAT
, '20130614_184135'),
202 \DateTime
::createFromFormat(\DateTime
::ATOM
, $data['created'])
205 \DateTime
::createFromFormat(\LinkDB
::LINK_DATE_FORMAT
, '20130615_184230'),
206 \DateTime
::createFromFormat(\DateTime
::ATOM
, $data['updated'])
211 * Test link update on non existent link => ApiLinkNotFoundException.
213 * @expectedException Shaarli\Api\Exceptions\ApiLinkNotFoundException
214 * @expectedExceptionMessage Link not found
216 public function testGetLink404()
218 $env = Environment
::mock([
219 'REQUEST_METHOD' => 'PUT',
221 $request = Request
::createFromEnvironment($env);
223 $this->controller
->putLink($request, new Response(), ['id' => -1]);