aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/api/controllers
diff options
context:
space:
mode:
authorArthurHoaro <arthur@hoa.ro>2017-04-01 11:11:25 +0200
committerArthurHoaro <arthur@hoa.ro>2017-05-07 15:49:16 +0200
commitcf9181dddf8b6113b1b017e4bcb21fac0a0b1c83 (patch)
treed8426d3b7b61dbd5f9d849e1114c3ee64d102db3 /tests/api/controllers
parent4b385d6c344c4a0a0b424622833bc72974c21cb5 (diff)
downloadShaarli-cf9181dddf8b6113b1b017e4bcb21fac0a0b1c83.tar.gz
Shaarli-cf9181dddf8b6113b1b017e4bcb21fac0a0b1c83.tar.zst
Shaarli-cf9181dddf8b6113b1b017e4bcb21fac0a0b1c83.zip
REST API: implement PUT method
* Related to #609 * Documentation: http://shaarli.github.io/api-documentation/#links-link-put
Diffstat (limited to 'tests/api/controllers')
-rw-r--r--tests/api/controllers/PutLinkTest.php199
1 files changed, 199 insertions, 0 deletions
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}