]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - tests/api/controllers/links/PutLinkTest.php
namespacing: \Shaarli\Bookmark\LinkDB
[github/shaarli/Shaarli.git] / tests / api / controllers / links / PutLinkTest.php
CommitLineData
cf9181dd
A
1<?php
2
3
4namespace Shaarli\Api\Controllers;
5
cf9181dd
A
6use Shaarli\Config\ConfigManager;
7use Slim\Container;
8use Slim\Http\Environment;
9use Slim\Http\Request;
10use Slim\Http\Response;
11
12class PutLinkTest extends \PHPUnit_Framework_TestCase
13{
14 /**
15 * @var string datastore to test write operations
16 */
17 protected static $testDatastore = 'sandbox/datastore.php';
18
813849e5
A
19 /**
20 * @var string datastore to test write operations
21 */
22 protected static $testHistory = 'sandbox/history.php';
23
cf9181dd
A
24 /**
25 * @var ConfigManager instance
26 */
27 protected $conf;
28
29 /**
30 * @var \ReferenceLinkDB instance.
31 */
32 protected $refDB = null;
33
813849e5 34 /**
bdc5152d 35 * @var \Shaarli\History instance.
813849e5
A
36 */
37 protected $history;
38
cf9181dd
A
39 /**
40 * @var Container instance.
41 */
42 protected $container;
43
44 /**
45 * @var Links controller instance.
46 */
47 protected $controller;
48
49 /**
50 * Number of JSON field per link.
51 */
52 const NB_FIELDS_LINK = 9;
53
54 /**
55 * Before every test, instantiate a new Api with its config, plugins and links.
56 */
57 public function setUp()
58 {
59 $this->conf = new ConfigManager('tests/utils/config/configJson.json.php');
60 $this->refDB = new \ReferenceLinkDB();
61 $this->refDB->write(self::$testDatastore);
62
813849e5
A
63 $refHistory = new \ReferenceHistory();
64 $refHistory->write(self::$testHistory);
bdc5152d 65 $this->history = new \Shaarli\History(self::$testHistory);
813849e5 66
cf9181dd
A
67 $this->container = new Container();
68 $this->container['conf'] = $this->conf;
f24896b2 69 $this->container['db'] = new \Shaarli\Bookmark\LinkDB(self::$testDatastore, true, false);
bdc5152d 70 $this->container['history'] = new \Shaarli\History(self::$testHistory);
cf9181dd
A
71
72 $this->controller = new Links($this->container);
73
74 // Used by index_url().
75 $this->controller->getCi()['environment'] = [
76 'SERVER_NAME' => 'domain.tld',
77 'SERVER_PORT' => 80,
78 'SCRIPT_NAME' => '/',
79 ];
80 }
81
82 /**
83 * After every test, remove the test datastore.
84 */
85 public function tearDown()
86 {
87 @unlink(self::$testDatastore);
813849e5 88 @unlink(self::$testHistory);
cf9181dd
A
89 }
90
91 /**
92 * Test link update without value: reset the link to default values
93 */
94 public function testPutLinkMinimal()
95 {
96 $env = Environment::mock([
97 'REQUEST_METHOD' => 'PUT',
98 ]);
99 $id = '41';
100 $request = Request::createFromEnvironment($env);
101
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']);
113 $this->assertEquals(
114 \DateTime::createFromFormat('Ymd_His', '20150310_114651'),
115 \DateTime::createFromFormat(\DateTime::ATOM, $data['created'])
116 );
9d9f6d75
V
117 $this->assertTrue(
118 new \DateTime('5 seconds ago') < \DateTime::createFromFormat(\DateTime::ATOM, $data['updated'])
119 );
813849e5
A
120
121 $historyEntry = $this->history->getHistory()[0];
bdc5152d 122 $this->assertEquals(\Shaarli\History::UPDATED, $historyEntry['event']);
813849e5
A
123 $this->assertTrue(
124 (new \DateTime())->add(\DateInterval::createFromDateString('-5 seconds')) < $historyEntry['datetime']
125 );
126 $this->assertEquals($id, $historyEntry['id']);
cf9181dd
A
127 }
128
129 /**
130 * Test link update with new values
131 */
132 public function testPutLinkWithValues()
133 {
134 $env = Environment::mock([
135 'REQUEST_METHOD' => 'PUT',
136 'CONTENT_TYPE' => 'application/json'
137 ]);
138 $id = 41;
139 $update = [
140 'url' => 'http://somewhere.else',
141 'title' => 'Le Cid',
142 'description' => 'Percé jusques au fond du cœur [...]',
143 'tags' => ['corneille', 'rodrigue'],
144 'private' => true,
145 ];
146 $request = Request::createFromEnvironment($env);
147 $request = $request->withParsedBody($update);
148
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']);
160 $this->assertEquals(
161 \DateTime::createFromFormat('Ymd_His', '20150310_114651'),
162 \DateTime::createFromFormat(\DateTime::ATOM, $data['created'])
163 );
9d9f6d75
V
164 $this->assertTrue(
165 new \DateTime('5 seconds ago') < \DateTime::createFromFormat(\DateTime::ATOM, $data['updated'])
166 );
cf9181dd
A
167 }
168
169 /**
170 * Test link update with an existing URL: 409 Conflict with the existing link as body
171 */
172 public function testPutLinkDuplicate()
173 {
174 $link = [
175 'url' => 'mediagoblin.org/',
176 'title' => 'new entry',
177 'description' => 'shaare description',
178 'tags' => ['one', 'two'],
179 'private' => true,
180 ];
181 $env = Environment::mock([
182 'REQUEST_METHOD' => 'PUT',
183 'CONTENT_TYPE' => 'application/json'
184 ]);
185
186 $request = Request::createFromEnvironment($env);
187 $request = $request->withParsedBody($link);
188 $response = $this->controller->putLink($request, new Response(), ['id' => 41]);
189
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']);
200 $this->assertEquals(
f24896b2 201 \DateTime::createFromFormat(\Shaarli\Bookmark\LinkDB::LINK_DATE_FORMAT, '20130614_184135'),
cf9181dd
A
202 \DateTime::createFromFormat(\DateTime::ATOM, $data['created'])
203 );
204 $this->assertEquals(
f24896b2 205 \DateTime::createFromFormat(\Shaarli\Bookmark\LinkDB::LINK_DATE_FORMAT, '20130615_184230'),
cf9181dd
A
206 \DateTime::createFromFormat(\DateTime::ATOM, $data['updated'])
207 );
208 }
209
210 /**
211 * Test link update on non existent link => ApiLinkNotFoundException.
212 *
213 * @expectedException Shaarli\Api\Exceptions\ApiLinkNotFoundException
214 * @expectedExceptionMessage Link not found
215 */
216 public function testGetLink404()
217 {
218 $env = Environment::mock([
219 'REQUEST_METHOD' => 'PUT',
220 ]);
221 $request = Request::createFromEnvironment($env);
222
223 $this->controller->putLink($request, new Response(), ['id' => -1]);
224 }
225}