4 namespace Shaarli\Api\Controllers
;
6 use Shaarli\Config\ConfigManager
;
9 use Slim\Http\Environment
;
10 use Slim\Http\Request
;
11 use Slim\Http\Response
;
13 class PutLinkTest
extends \PHPUnit\Framework\TestCase
16 * @var string datastore to test write operations
18 protected static $testDatastore = 'sandbox/datastore.php';
21 * @var string datastore to test write operations
23 protected static $testHistory = 'sandbox/history.php';
26 * @var ConfigManager instance
31 * @var \ReferenceLinkDB instance.
33 protected $refDB = null;
36 * @var HistoryController instance.
41 * @var Container instance.
46 * @var Links controller instance.
48 protected $controller;
51 * Number of JSON field per link.
53 const NB_FIELDS_LINK
= 9;
56 * Before every test, instantiate a new Api with its config, plugins and links.
58 public function setUp()
60 $this->conf
= new ConfigManager('tests/utils/config/configJson.json.php');
61 $this->refDB
= new \
ReferenceLinkDB();
62 $this->refDB
->write(self
::$testDatastore);
64 $refHistory = new \
ReferenceHistory();
65 $refHistory->write(self
::$testHistory);
66 $this->history
= new History(self
::$testHistory);
68 $this->container
= new Container();
69 $this->container
['conf'] = $this->conf
;
70 $this->container
['db'] = new \Shaarli\Bookmark\
LinkDB(self
::$testDatastore, true, false);
71 $this->container
['history'] = new History(self
::$testHistory);
73 $this->controller
= new Links($this->container
);
75 // Used by index_url().
76 $this->controller
->getCi()['environment'] = [
77 'SERVER_NAME' => 'domain.tld',
84 * After every test, remove the test datastore.
86 public function tearDown()
88 @unlink(self
::$testDatastore);
89 @unlink(self
::$testHistory);
93 * Test link update without value: reset the link to default values
95 public function testPutLinkMinimal()
97 $env = Environment
::mock([
98 'REQUEST_METHOD' => 'PUT',
101 $request = Request
::createFromEnvironment($env);
103 $response = $this->controller
->putLink($request, new Response(), ['id' => $id]);
104 $this->assertEquals(200, $response->getStatusCode());
105 $data = json_decode((string) $response->getBody(), true);
106 $this->assertEquals(self
::NB_FIELDS_LINK
, count($data));
107 $this->assertEquals($id, $data['id']);
108 $this->assertEquals('WDWyig', $data['shorturl']);
109 $this->assertEquals('http://domain.tld/?WDWyig', $data['url']);
110 $this->assertEquals('?WDWyig', $data['title']);
111 $this->assertEquals('', $data['description']);
112 $this->assertEquals([], $data['tags']);
113 $this->assertEquals(false, $data['private']);
115 \DateTime
::createFromFormat('Ymd_His', '20150310_114651'),
116 \DateTime
::createFromFormat(\DateTime
::ATOM
, $data['created'])
119 new \
DateTime('5 seconds ago') < \DateTime
::createFromFormat(\DateTime
::ATOM
, $data['updated'])
122 $historyEntry = $this->history
->getHistory()[0];
123 $this->assertEquals(History
::UPDATED
, $historyEntry['event']);
125 (new \
DateTime())->add(\DateInterval
::createFromDateString('-5 seconds')) < $historyEntry['datetime']
127 $this->assertEquals($id, $historyEntry['id']);
131 * Test link update with new values
133 public function testPutLinkWithValues()
135 $env = Environment
::mock([
136 'REQUEST_METHOD' => 'PUT',
137 'CONTENT_TYPE' => 'application/json'
141 'url' => 'http://somewhere.else',
143 'description' => 'Percé jusques au fond du cœur [...]',
144 'tags' => ['corneille', 'rodrigue'],
147 $request = Request
::createFromEnvironment($env);
148 $request = $request->withParsedBody($update);
150 $response = $this->controller
->putLink($request, new Response(), ['id' => $id]);
151 $this->assertEquals(200, $response->getStatusCode());
152 $data = json_decode((string) $response->getBody(), true);
153 $this->assertEquals(self
::NB_FIELDS_LINK
, count($data));
154 $this->assertEquals($id, $data['id']);
155 $this->assertEquals('WDWyig', $data['shorturl']);
156 $this->assertEquals('http://somewhere.else', $data['url']);
157 $this->assertEquals('Le Cid', $data['title']);
158 $this->assertEquals('Percé jusques au fond du cœur [...]', $data['description']);
159 $this->assertEquals(['corneille', 'rodrigue'], $data['tags']);
160 $this->assertEquals(true, $data['private']);
162 \DateTime
::createFromFormat('Ymd_His', '20150310_114651'),
163 \DateTime
::createFromFormat(\DateTime
::ATOM
, $data['created'])
166 new \
DateTime('5 seconds ago') < \DateTime
::createFromFormat(\DateTime
::ATOM
, $data['updated'])
171 * Test link update with an existing URL: 409 Conflict with the existing link as body
173 public function testPutLinkDuplicate()
176 'url' => 'mediagoblin.org/',
177 'title' => 'new entry',
178 'description' => 'shaare description',
179 'tags' => ['one', 'two'],
182 $env = Environment
::mock([
183 'REQUEST_METHOD' => 'PUT',
184 'CONTENT_TYPE' => 'application/json'
187 $request = Request
::createFromEnvironment($env);
188 $request = $request->withParsedBody($link);
189 $response = $this->controller
->putLink($request, new Response(), ['id' => 41]);
191 $this->assertEquals(409, $response->getStatusCode());
192 $data = json_decode((string) $response->getBody(), true);
193 $this->assertEquals(self
::NB_FIELDS_LINK
, count($data));
194 $this->assertEquals(7, $data['id']);
195 $this->assertEquals('IuWvgA', $data['shorturl']);
196 $this->assertEquals('http://mediagoblin.org/', $data['url']);
197 $this->assertEquals('MediaGoblin', $data['title']);
198 $this->assertEquals('A free software media publishing platform #hashtagOther', $data['description']);
199 $this->assertEquals(['gnu', 'media', 'web', '.hidden', 'hashtag'], $data['tags']);
200 $this->assertEquals(false, $data['private']);
202 \DateTime
::createFromFormat(\Shaarli\Bookmark\LinkDB
::LINK_DATE_FORMAT
, '20130614_184135'),
203 \DateTime
::createFromFormat(\DateTime
::ATOM
, $data['created'])
206 \DateTime
::createFromFormat(\Shaarli\Bookmark\LinkDB
::LINK_DATE_FORMAT
, '20130615_184230'),
207 \DateTime
::createFromFormat(\DateTime
::ATOM
, $data['updated'])
212 * Test link update on non existent link => ApiLinkNotFoundException.
214 * @expectedException Shaarli\Api\Exceptions\ApiLinkNotFoundException
215 * @expectedExceptionMessage Link not found
217 public function testGetLink404()
219 $env = Environment
::mock([
220 'REQUEST_METHOD' => 'PUT',
222 $request = Request
::createFromEnvironment($env);
224 $this->controller
->putLink($request, new Response(), ['id' => -1]);