4 namespace Shaarli\Api\Controllers
;
7 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 \History 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 \
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'])
118 $this->assertTrue(new \
DateTime('5 seconds ago') < \DateTime
::createFromFormat(\DateTime
::ATOM
, $data['updated']));
120 $historyEntry = $this->history
->getHistory()[0];
121 $this->assertEquals(\History
::UPDATED
, $historyEntry['event']);
123 (new \
DateTime())->add(\DateInterval
::createFromDateString('-5 seconds')) < $historyEntry['datetime']
125 $this->assertEquals($id, $historyEntry['id']);
129 * Test link update with new values
131 public function testPutLinkWithValues()
133 $env = Environment
::mock([
134 'REQUEST_METHOD' => 'PUT',
135 'CONTENT_TYPE' => 'application/json'
139 'url' => 'http://somewhere.else',
141 'description' => 'Percé jusques au fond du cœur [...]',
142 'tags' => ['corneille', 'rodrigue'],
145 $request = Request
::createFromEnvironment($env);
146 $request = $request->withParsedBody($update);
148 $response = $this->controller
->putLink($request, new Response(), ['id' => $id]);
149 $this->assertEquals(200, $response->getStatusCode());
150 $data = json_decode((string) $response->getBody(), true);
151 $this->assertEquals(self
::NB_FIELDS_LINK
, count($data));
152 $this->assertEquals($id, $data['id']);
153 $this->assertEquals('WDWyig', $data['shorturl']);
154 $this->assertEquals('http://somewhere.else', $data['url']);
155 $this->assertEquals('Le Cid', $data['title']);
156 $this->assertEquals('Percé jusques au fond du cœur [...]', $data['description']);
157 $this->assertEquals(['corneille', 'rodrigue'], $data['tags']);
158 $this->assertEquals(true, $data['private']);
160 \DateTime
::createFromFormat('Ymd_His', '20150310_114651'),
161 \DateTime
::createFromFormat(\DateTime
::ATOM
, $data['created'])
163 $this->assertTrue(new \
DateTime('5 seconds ago') < \DateTime
::createFromFormat(\DateTime
::ATOM
, $data['updated']));
167 * Test link update with an existing URL: 409 Conflict with the existing link as body
169 public function testPutLinkDuplicate()
172 'url' => 'mediagoblin.org/',
173 'title' => 'new entry',
174 'description' => 'shaare description',
175 'tags' => ['one', 'two'],
178 $env = Environment
::mock([
179 'REQUEST_METHOD' => 'PUT',
180 'CONTENT_TYPE' => 'application/json'
183 $request = Request
::createFromEnvironment($env);
184 $request = $request->withParsedBody($link);
185 $response = $this->controller
->putLink($request, new Response(), ['id' => 41]);
187 $this->assertEquals(409, $response->getStatusCode());
188 $data = json_decode((string) $response->getBody(), true);
189 $this->assertEquals(self
::NB_FIELDS_LINK
, count($data));
190 $this->assertEquals(7, $data['id']);
191 $this->assertEquals('IuWvgA', $data['shorturl']);
192 $this->assertEquals('http://mediagoblin.org/', $data['url']);
193 $this->assertEquals('MediaGoblin', $data['title']);
194 $this->assertEquals('A free software media publishing platform #hashtagOther', $data['description']);
195 $this->assertEquals(['gnu', 'media', 'web', '.hidden', 'hashtag'], $data['tags']);
196 $this->assertEquals(false, $data['private']);
198 \DateTime
::createFromFormat(\LinkDB
::LINK_DATE_FORMAT
, '20130614_184135'),
199 \DateTime
::createFromFormat(\DateTime
::ATOM
, $data['created'])
202 \DateTime
::createFromFormat(\LinkDB
::LINK_DATE_FORMAT
, '20130615_184230'),
203 \DateTime
::createFromFormat(\DateTime
::ATOM
, $data['updated'])
208 * Test link update on non existent link => ApiLinkNotFoundException.
210 * @expectedException Shaarli\Api\Exceptions\ApiLinkNotFoundException
211 * @expectedExceptionMessage Link not found
213 public function testGetLink404()
215 $env = Environment
::mock([
216 'REQUEST_METHOD' => 'PUT',
218 $request = Request
::createFromEnvironment($env);
220 $this->controller
->putLink($request, new Response(), ['id' => -1]);