]>
Commit | Line | Data |
---|---|---|
cf9181dd A |
1 | <?php |
2 | ||
3 | ||
4 | namespace Shaarli\Api\Controllers; | |
5 | ||
e26e2060 A |
6 | use Shaarli\Bookmark\Bookmark; |
7 | use Shaarli\Bookmark\BookmarkFileService; | |
cf9181dd | 8 | use Shaarli\Config\ConfigManager; |
dea72c71 | 9 | use Shaarli\History; |
cf9181dd A |
10 | use Slim\Container; |
11 | use Slim\Http\Environment; | |
12 | use Slim\Http\Request; | |
13 | use Slim\Http\Response; | |
14 | ||
dea72c71 | 15 | class PutLinkTest extends \PHPUnit\Framework\TestCase |
cf9181dd A |
16 | { |
17 | /** | |
18 | * @var string datastore to test write operations | |
19 | */ | |
20 | protected static $testDatastore = 'sandbox/datastore.php'; | |
21 | ||
813849e5 A |
22 | /** |
23 | * @var string datastore to test write operations | |
24 | */ | |
25 | protected static $testHistory = 'sandbox/history.php'; | |
26 | ||
cf9181dd A |
27 | /** |
28 | * @var ConfigManager instance | |
29 | */ | |
30 | protected $conf; | |
31 | ||
32 | /** | |
33 | * @var \ReferenceLinkDB instance. | |
34 | */ | |
35 | protected $refDB = null; | |
36 | ||
e26e2060 A |
37 | /** |
38 | * @var BookmarkFileService instance. | |
39 | */ | |
40 | protected $bookmarkService; | |
41 | ||
813849e5 | 42 | /** |
dea72c71 | 43 | * @var HistoryController instance. |
813849e5 A |
44 | */ |
45 | protected $history; | |
46 | ||
cf9181dd A |
47 | /** |
48 | * @var Container instance. | |
49 | */ | |
50 | protected $container; | |
51 | ||
52 | /** | |
53 | * @var Links controller instance. | |
54 | */ | |
55 | protected $controller; | |
56 | ||
57 | /** | |
58 | * Number of JSON field per link. | |
59 | */ | |
60 | const NB_FIELDS_LINK = 9; | |
61 | ||
62 | /** | |
e26e2060 | 63 | * Before every test, instantiate a new Api with its config, plugins and bookmarks. |
cf9181dd | 64 | */ |
8f60e120 | 65 | protected function setUp(): void |
cf9181dd | 66 | { |
e26e2060 A |
67 | $this->conf = new ConfigManager('tests/utils/config/configJson'); |
68 | $this->conf->set('resource.datastore', self::$testDatastore); | |
cf9181dd A |
69 | $this->refDB = new \ReferenceLinkDB(); |
70 | $this->refDB->write(self::$testDatastore); | |
813849e5 A |
71 | $refHistory = new \ReferenceHistory(); |
72 | $refHistory->write(self::$testHistory); | |
dea72c71 | 73 | $this->history = new History(self::$testHistory); |
e26e2060 | 74 | $this->bookmarkService = new BookmarkFileService($this->conf, $this->history, true); |
813849e5 | 75 | |
cf9181dd A |
76 | $this->container = new Container(); |
77 | $this->container['conf'] = $this->conf; | |
e26e2060 A |
78 | $this->container['db'] = $this->bookmarkService; |
79 | $this->container['history'] = $this->history; | |
cf9181dd A |
80 | |
81 | $this->controller = new Links($this->container); | |
82 | ||
83 | // Used by index_url(). | |
84 | $this->controller->getCi()['environment'] = [ | |
85 | 'SERVER_NAME' => 'domain.tld', | |
86 | 'SERVER_PORT' => 80, | |
87 | 'SCRIPT_NAME' => '/', | |
88 | ]; | |
89 | } | |
90 | ||
91 | /** | |
92 | * After every test, remove the test datastore. | |
93 | */ | |
8f60e120 | 94 | protected function tearDown(): void |
cf9181dd A |
95 | { |
96 | @unlink(self::$testDatastore); | |
813849e5 | 97 | @unlink(self::$testHistory); |
cf9181dd A |
98 | } |
99 | ||
100 | /** | |
101 | * Test link update without value: reset the link to default values | |
102 | */ | |
103 | public function testPutLinkMinimal() | |
104 | { | |
105 | $env = Environment::mock([ | |
106 | 'REQUEST_METHOD' => 'PUT', | |
107 | ]); | |
108 | $id = '41'; | |
109 | $request = Request::createFromEnvironment($env); | |
110 | ||
111 | $response = $this->controller->putLink($request, new Response(), ['id' => $id]); | |
112 | $this->assertEquals(200, $response->getStatusCode()); | |
113 | $data = json_decode((string) $response->getBody(), true); | |
114 | $this->assertEquals(self::NB_FIELDS_LINK, count($data)); | |
115 | $this->assertEquals($id, $data['id']); | |
116 | $this->assertEquals('WDWyig', $data['shorturl']); | |
301c7ab1 A |
117 | $this->assertEquals('http://domain.tld/shaare/WDWyig', $data['url']); |
118 | $this->assertEquals('/shaare/WDWyig', $data['title']); | |
cf9181dd A |
119 | $this->assertEquals('', $data['description']); |
120 | $this->assertEquals([], $data['tags']); | |
e26e2060 | 121 | $this->assertEquals(true, $data['private']); |
cf9181dd A |
122 | $this->assertEquals( |
123 | \DateTime::createFromFormat('Ymd_His', '20150310_114651'), | |
124 | \DateTime::createFromFormat(\DateTime::ATOM, $data['created']) | |
125 | ); | |
9d9f6d75 V |
126 | $this->assertTrue( |
127 | new \DateTime('5 seconds ago') < \DateTime::createFromFormat(\DateTime::ATOM, $data['updated']) | |
128 | ); | |
813849e5 A |
129 | |
130 | $historyEntry = $this->history->getHistory()[0]; | |
dea72c71 | 131 | $this->assertEquals(History::UPDATED, $historyEntry['event']); |
813849e5 A |
132 | $this->assertTrue( |
133 | (new \DateTime())->add(\DateInterval::createFromDateString('-5 seconds')) < $historyEntry['datetime'] | |
134 | ); | |
135 | $this->assertEquals($id, $historyEntry['id']); | |
cf9181dd A |
136 | } |
137 | ||
138 | /** | |
139 | * Test link update with new values | |
140 | */ | |
141 | public function testPutLinkWithValues() | |
142 | { | |
143 | $env = Environment::mock([ | |
144 | 'REQUEST_METHOD' => 'PUT', | |
145 | 'CONTENT_TYPE' => 'application/json' | |
146 | ]); | |
147 | $id = 41; | |
148 | $update = [ | |
149 | 'url' => 'http://somewhere.else', | |
150 | 'title' => 'Le Cid', | |
151 | 'description' => 'Percé jusques au fond du cœur [...]', | |
152 | 'tags' => ['corneille', 'rodrigue'], | |
153 | 'private' => true, | |
154 | ]; | |
155 | $request = Request::createFromEnvironment($env); | |
156 | $request = $request->withParsedBody($update); | |
157 | ||
158 | $response = $this->controller->putLink($request, new Response(), ['id' => $id]); | |
159 | $this->assertEquals(200, $response->getStatusCode()); | |
160 | $data = json_decode((string) $response->getBody(), true); | |
161 | $this->assertEquals(self::NB_FIELDS_LINK, count($data)); | |
162 | $this->assertEquals($id, $data['id']); | |
163 | $this->assertEquals('WDWyig', $data['shorturl']); | |
164 | $this->assertEquals('http://somewhere.else', $data['url']); | |
165 | $this->assertEquals('Le Cid', $data['title']); | |
166 | $this->assertEquals('Percé jusques au fond du cœur [...]', $data['description']); | |
167 | $this->assertEquals(['corneille', 'rodrigue'], $data['tags']); | |
168 | $this->assertEquals(true, $data['private']); | |
169 | $this->assertEquals( | |
170 | \DateTime::createFromFormat('Ymd_His', '20150310_114651'), | |
171 | \DateTime::createFromFormat(\DateTime::ATOM, $data['created']) | |
172 | ); | |
9d9f6d75 V |
173 | $this->assertTrue( |
174 | new \DateTime('5 seconds ago') < \DateTime::createFromFormat(\DateTime::ATOM, $data['updated']) | |
175 | ); | |
cf9181dd A |
176 | } |
177 | ||
178 | /** | |
179 | * Test link update with an existing URL: 409 Conflict with the existing link as body | |
180 | */ | |
181 | public function testPutLinkDuplicate() | |
182 | { | |
183 | $link = [ | |
184 | 'url' => 'mediagoblin.org/', | |
185 | 'title' => 'new entry', | |
186 | 'description' => 'shaare description', | |
187 | 'tags' => ['one', 'two'], | |
188 | 'private' => true, | |
189 | ]; | |
190 | $env = Environment::mock([ | |
191 | 'REQUEST_METHOD' => 'PUT', | |
192 | 'CONTENT_TYPE' => 'application/json' | |
193 | ]); | |
194 | ||
195 | $request = Request::createFromEnvironment($env); | |
196 | $request = $request->withParsedBody($link); | |
197 | $response = $this->controller->putLink($request, new Response(), ['id' => 41]); | |
198 | ||
199 | $this->assertEquals(409, $response->getStatusCode()); | |
200 | $data = json_decode((string) $response->getBody(), true); | |
201 | $this->assertEquals(self::NB_FIELDS_LINK, count($data)); | |
202 | $this->assertEquals(7, $data['id']); | |
203 | $this->assertEquals('IuWvgA', $data['shorturl']); | |
204 | $this->assertEquals('http://mediagoblin.org/', $data['url']); | |
205 | $this->assertEquals('MediaGoblin', $data['title']); | |
206 | $this->assertEquals('A free software media publishing platform #hashtagOther', $data['description']); | |
207 | $this->assertEquals(['gnu', 'media', 'web', '.hidden', 'hashtag'], $data['tags']); | |
208 | $this->assertEquals(false, $data['private']); | |
209 | $this->assertEquals( | |
e26e2060 | 210 | \DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20130614_184135'), |
cf9181dd A |
211 | \DateTime::createFromFormat(\DateTime::ATOM, $data['created']) |
212 | ); | |
213 | $this->assertEquals( | |
e26e2060 | 214 | \DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20130615_184230'), |
cf9181dd A |
215 | \DateTime::createFromFormat(\DateTime::ATOM, $data['updated']) |
216 | ); | |
217 | } | |
218 | ||
219 | /** | |
220 | * Test link update on non existent link => ApiLinkNotFoundException. | |
cf9181dd A |
221 | */ |
222 | public function testGetLink404() | |
223 | { | |
b1baca99 A |
224 | $this->expectException(\Shaarli\Api\Exceptions\ApiLinkNotFoundException::class); |
225 | $this->expectExceptionMessage('Link not found'); | |
226 | ||
cf9181dd A |
227 | $env = Environment::mock([ |
228 | 'REQUEST_METHOD' => 'PUT', | |
229 | ]); | |
230 | $request = Request::createFromEnvironment($env); | |
231 | ||
232 | $this->controller->putLink($request, new Response(), ['id' => -1]); | |
233 | } | |
234 | } |