aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/api/ApiUtilsTest.php78
-rw-r--r--tests/api/controllers/PutLinkTest.php199
2 files changed, 277 insertions, 0 deletions
diff --git a/tests/api/ApiUtilsTest.php b/tests/api/ApiUtilsTest.php
index b4431d1b..62baf4c5 100644
--- a/tests/api/ApiUtilsTest.php
+++ b/tests/api/ApiUtilsTest.php
@@ -271,4 +271,82 @@ class ApiUtilsTest extends \PHPUnit_Framework_TestCase
271 271
272 $this->assertEquals($expected, ApiUtils::formatLink($link, $indexUrl)); 272 $this->assertEquals($expected, ApiUtils::formatLink($link, $indexUrl));
273 } 273 }
274
275 /**
276 * Test updateLink with valid data, and also unnecessary fields.
277 */
278 public function testUpdateLink()
279 {
280 $created = \DateTime::createFromFormat('Ymd_His', '20170107_160102');
281 $old = [
282 'id' => 12,
283 'url' => '?abc',
284 'shorturl' => 'abc',
285 'title' => 'Note',
286 'description' => '',
287 'tags' => '',
288 'private' => '',
289 'created' => $created,
290 ];
291
292 $new = [
293 'id' => 13,
294 'shorturl' => 'nope',
295 'url' => 'http://somewhere.else',
296 'title' => 'Le Cid',
297 'description' => 'Percé jusques au fond du cœur [...]',
298 'tags' => 'corneille rodrigue',
299 'private' => true,
300 'created' => 'creation',
301 'updated' => 'updation',
302 ];
303
304 $result = ApiUtils::updateLink($old, $new);
305 $this->assertEquals(12, $result['id']);
306 $this->assertEquals('http://somewhere.else', $result['url']);
307 $this->assertEquals('abc', $result['shorturl']);
308 $this->assertEquals('Le Cid', $result['title']);
309 $this->assertEquals('Percé jusques au fond du cœur [...]', $result['description']);
310 $this->assertEquals('corneille rodrigue', $result['tags']);
311 $this->assertEquals(true, $result['private']);
312 $this->assertEquals($created, $result['created']);
313 $this->assertTrue(new \DateTime('5 seconds ago') < $result['updated']);
314 }
315
316 /**
317 * Test updateLink with minimal data.
318 */
319 public function testUpdateLinkMinimal()
320 {
321 $created = \DateTime::createFromFormat('Ymd_His', '20170107_160102');
322 $old = [
323 'id' => 12,
324 'url' => '?abc',
325 'shorturl' => 'abc',
326 'title' => 'Note',
327 'description' => 'Interesting description!',
328 'tags' => 'doggo',
329 'private' => true,
330 'created' => $created,
331 ];
332
333 $new = [
334 'url' => '',
335 'title' => '',
336 'description' => '',
337 'tags' => '',
338 'private' => false,
339 ];
340
341 $result = ApiUtils::updateLink($old, $new);
342 $this->assertEquals(12, $result['id']);
343 $this->assertEquals('?abc', $result['url']);
344 $this->assertEquals('abc', $result['shorturl']);
345 $this->assertEquals('?abc', $result['title']);
346 $this->assertEquals('', $result['description']);
347 $this->assertEquals('', $result['tags']);
348 $this->assertEquals(false, $result['private']);
349 $this->assertEquals($created, $result['created']);
350 $this->assertTrue(new \DateTime('5 seconds ago') < $result['updated']);
351 }
274} 352}
diff --git a/tests/api/controllers/PutLinkTest.php b/tests/api/controllers/PutLinkTest.php
new file mode 100644
index 00000000..4096c1a7
--- /dev/null
+++ b/tests/api/controllers/PutLinkTest.php
@@ -0,0 +1,199 @@
1<?php
2
3
4namespace Shaarli\Api\Controllers;
5
6
7use Shaarli\Config\ConfigManager;
8use Slim\Container;
9use Slim\Http\Environment;
10use Slim\Http\Request;
11use Slim\Http\Response;
12
13class PutLinkTest extends \PHPUnit_Framework_TestCase
14{
15 /**
16 * @var string datastore to test write operations
17 */
18 protected static $testDatastore = 'sandbox/datastore.php';
19
20 /**
21 * @var ConfigManager instance
22 */
23 protected $conf;
24
25 /**
26 * @var \ReferenceLinkDB instance.
27 */
28 protected $refDB = null;
29
30 /**
31 * @var Container instance.
32 */
33 protected $container;
34
35 /**
36 * @var Links controller instance.
37 */
38 protected $controller;
39
40 /**
41 * Number of JSON field per link.
42 */
43 const NB_FIELDS_LINK = 9;
44
45 /**
46 * Before every test, instantiate a new Api with its config, plugins and links.
47 */
48 public function setUp()
49 {
50 $this->conf = new ConfigManager('tests/utils/config/configJson.json.php');
51 $this->refDB = new \ReferenceLinkDB();
52 $this->refDB->write(self::$testDatastore);
53
54 $this->container = new Container();
55 $this->container['conf'] = $this->conf;
56 $this->container['db'] = new \LinkDB(self::$testDatastore, true, false);
57
58 $this->controller = new Links($this->container);
59
60 // Used by index_url().
61 $this->controller->getCi()['environment'] = [
62 'SERVER_NAME' => 'domain.tld',
63 'SERVER_PORT' => 80,
64 'SCRIPT_NAME' => '/',
65 ];
66 }
67
68 /**
69 * After every test, remove the test datastore.
70 */
71 public function tearDown()
72 {
73 @unlink(self::$testDatastore);
74 }
75
76 /**
77 * Test link update without value: reset the link to default values
78 */
79 public function testPutLinkMinimal()
80 {
81 $env = Environment::mock([
82 'REQUEST_METHOD' => 'PUT',
83 ]);
84 $id = '41';
85 $request = Request::createFromEnvironment($env);
86
87 $response = $this->controller->putLink($request, new Response(), ['id' => $id]);
88 $this->assertEquals(200, $response->getStatusCode());
89 $data = json_decode((string) $response->getBody(), true);
90 $this->assertEquals(self::NB_FIELDS_LINK, count($data));
91 $this->assertEquals($id, $data['id']);
92 $this->assertEquals('WDWyig', $data['shorturl']);
93 $this->assertEquals('http://domain.tld/?WDWyig', $data['url']);
94 $this->assertEquals('?WDWyig', $data['title']);
95 $this->assertEquals('', $data['description']);
96 $this->assertEquals([], $data['tags']);
97 $this->assertEquals(false, $data['private']);
98 $this->assertEquals(
99 \DateTime::createFromFormat('Ymd_His', '20150310_114651'),
100 \DateTime::createFromFormat(\DateTime::ATOM, $data['created'])
101 );
102 $this->assertTrue(new \DateTime('5 seconds ago') < \DateTime::createFromFormat(\DateTime::ATOM, $data['updated']));
103 }
104
105 /**
106 * Test link update with new values
107 */
108 public function testPutLinkWithValues()
109 {
110 $env = Environment::mock([
111 'REQUEST_METHOD' => 'PUT',
112 'CONTENT_TYPE' => 'application/json'
113 ]);
114 $id = 41;
115 $update = [
116 'url' => 'http://somewhere.else',
117 'title' => 'Le Cid',
118 'description' => 'Percé jusques au fond du cœur [...]',
119 'tags' => ['corneille', 'rodrigue'],
120 'private' => true,
121 ];
122 $request = Request::createFromEnvironment($env);
123 $request = $request->withParsedBody($update);
124
125 $response = $this->controller->putLink($request, new Response(), ['id' => $id]);
126 $this->assertEquals(200, $response->getStatusCode());
127 $data = json_decode((string) $response->getBody(), true);
128 $this->assertEquals(self::NB_FIELDS_LINK, count($data));
129 $this->assertEquals($id, $data['id']);
130 $this->assertEquals('WDWyig', $data['shorturl']);
131 $this->assertEquals('http://somewhere.else', $data['url']);
132 $this->assertEquals('Le Cid', $data['title']);
133 $this->assertEquals('Percé jusques au fond du cœur [...]', $data['description']);
134 $this->assertEquals(['corneille', 'rodrigue'], $data['tags']);
135 $this->assertEquals(true, $data['private']);
136 $this->assertEquals(
137 \DateTime::createFromFormat('Ymd_His', '20150310_114651'),
138 \DateTime::createFromFormat(\DateTime::ATOM, $data['created'])
139 );
140 $this->assertTrue(new \DateTime('5 seconds ago') < \DateTime::createFromFormat(\DateTime::ATOM, $data['updated']));
141 }
142
143 /**
144 * Test link update with an existing URL: 409 Conflict with the existing link as body
145 */
146 public function testPutLinkDuplicate()
147 {
148 $link = [
149 'url' => 'mediagoblin.org/',
150 'title' => 'new entry',
151 'description' => 'shaare description',
152 'tags' => ['one', 'two'],
153 'private' => true,
154 ];
155 $env = Environment::mock([
156 'REQUEST_METHOD' => 'PUT',
157 'CONTENT_TYPE' => 'application/json'
158 ]);
159
160 $request = Request::createFromEnvironment($env);
161 $request = $request->withParsedBody($link);
162 $response = $this->controller->putLink($request, new Response(), ['id' => 41]);
163
164 $this->assertEquals(409, $response->getStatusCode());
165 $data = json_decode((string) $response->getBody(), true);
166 $this->assertEquals(self::NB_FIELDS_LINK, count($data));
167 $this->assertEquals(7, $data['id']);
168 $this->assertEquals('IuWvgA', $data['shorturl']);
169 $this->assertEquals('http://mediagoblin.org/', $data['url']);
170 $this->assertEquals('MediaGoblin', $data['title']);
171 $this->assertEquals('A free software media publishing platform #hashtagOther', $data['description']);
172 $this->assertEquals(['gnu', 'media', 'web', '.hidden', 'hashtag'], $data['tags']);
173 $this->assertEquals(false, $data['private']);
174 $this->assertEquals(
175 \DateTime::createFromFormat(\LinkDB::LINK_DATE_FORMAT, '20130614_184135'),
176 \DateTime::createFromFormat(\DateTime::ATOM, $data['created'])
177 );
178 $this->assertEquals(
179 \DateTime::createFromFormat(\LinkDB::LINK_DATE_FORMAT, '20130615_184230'),
180 \DateTime::createFromFormat(\DateTime::ATOM, $data['updated'])
181 );
182 }
183
184 /**
185 * Test link update on non existent link => ApiLinkNotFoundException.
186 *
187 * @expectedException Shaarli\Api\Exceptions\ApiLinkNotFoundException
188 * @expectedExceptionMessage Link not found
189 */
190 public function testGetLink404()
191 {
192 $env = Environment::mock([
193 'REQUEST_METHOD' => 'PUT',
194 ]);
195 $request = Request::createFromEnvironment($env);
196
197 $this->controller->putLink($request, new Response(), ['id' => -1]);
198 }
199}