]>
Commit | Line | Data |
---|---|---|
1 | <?php | |
2 | ||
3 | ||
4 | namespace Shaarli\Api\Controllers; | |
5 | ||
6 | use Shaarli\Bookmark\Bookmark; | |
7 | use Shaarli\Bookmark\BookmarkFileService; | |
8 | use Shaarli\Config\ConfigManager; | |
9 | use Shaarli\History; | |
10 | use Slim\Container; | |
11 | use Slim\Http\Environment; | |
12 | use Slim\Http\Request; | |
13 | use Slim\Http\Response; | |
14 | ||
15 | class PutLinkTest extends \PHPUnit\Framework\TestCase | |
16 | { | |
17 | /** | |
18 | * @var string datastore to test write operations | |
19 | */ | |
20 | protected static $testDatastore = 'sandbox/datastore.php'; | |
21 | ||
22 | /** | |
23 | * @var string datastore to test write operations | |
24 | */ | |
25 | protected static $testHistory = 'sandbox/history.php'; | |
26 | ||
27 | /** | |
28 | * @var ConfigManager instance | |
29 | */ | |
30 | protected $conf; | |
31 | ||
32 | /** | |
33 | * @var \ReferenceLinkDB instance. | |
34 | */ | |
35 | protected $refDB = null; | |
36 | ||
37 | /** | |
38 | * @var BookmarkFileService instance. | |
39 | */ | |
40 | protected $bookmarkService; | |
41 | ||
42 | /** | |
43 | * @var HistoryController instance. | |
44 | */ | |
45 | protected $history; | |
46 | ||
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 | /** | |
63 | * Before every test, instantiate a new Api with its config, plugins and bookmarks. | |
64 | */ | |
65 | protected function setUp(): void | |
66 | { | |
67 | $this->conf = new ConfigManager('tests/utils/config/configJson'); | |
68 | $this->conf->set('resource.datastore', self::$testDatastore); | |
69 | $this->refDB = new \ReferenceLinkDB(); | |
70 | $this->refDB->write(self::$testDatastore); | |
71 | $refHistory = new \ReferenceHistory(); | |
72 | $refHistory->write(self::$testHistory); | |
73 | $this->history = new History(self::$testHistory); | |
74 | $this->bookmarkService = new BookmarkFileService($this->conf, $this->history, true); | |
75 | ||
76 | $this->container = new Container(); | |
77 | $this->container['conf'] = $this->conf; | |
78 | $this->container['db'] = $this->bookmarkService; | |
79 | $this->container['history'] = $this->history; | |
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 | */ | |
94 | protected function tearDown(): void | |
95 | { | |
96 | @unlink(self::$testDatastore); | |
97 | @unlink(self::$testHistory); | |
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']); | |
117 | $this->assertEquals('http://domain.tld/shaare/WDWyig', $data['url']); | |
118 | $this->assertEquals('/shaare/WDWyig', $data['title']); | |
119 | $this->assertEquals('', $data['description']); | |
120 | $this->assertEquals([], $data['tags']); | |
121 | $this->assertEquals(true, $data['private']); | |
122 | $this->assertEquals( | |
123 | \DateTime::createFromFormat('Ymd_His', '20150310_114651'), | |
124 | \DateTime::createFromFormat(\DateTime::ATOM, $data['created']) | |
125 | ); | |
126 | $this->assertTrue( | |
127 | new \DateTime('5 seconds ago') < \DateTime::createFromFormat(\DateTime::ATOM, $data['updated']) | |
128 | ); | |
129 | ||
130 | $historyEntry = $this->history->getHistory()[0]; | |
131 | $this->assertEquals(History::UPDATED, $historyEntry['event']); | |
132 | $this->assertTrue( | |
133 | (new \DateTime())->add(\DateInterval::createFromDateString('-5 seconds')) < $historyEntry['datetime'] | |
134 | ); | |
135 | $this->assertEquals($id, $historyEntry['id']); | |
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 | ); | |
173 | $this->assertTrue( | |
174 | new \DateTime('5 seconds ago') < \DateTime::createFromFormat(\DateTime::ATOM, $data['updated']) | |
175 | ); | |
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( | |
210 | \DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20130614_184135'), | |
211 | \DateTime::createFromFormat(\DateTime::ATOM, $data['created']) | |
212 | ); | |
213 | $this->assertEquals( | |
214 | \DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20130615_184230'), | |
215 | \DateTime::createFromFormat(\DateTime::ATOM, $data['updated']) | |
216 | ); | |
217 | } | |
218 | ||
219 | /** | |
220 | * Test link update on non existent link => ApiLinkNotFoundException. | |
221 | */ | |
222 | public function testGetLink404() | |
223 | { | |
224 | $this->expectException(\Shaarli\Api\Exceptions\ApiLinkNotFoundException::class); | |
225 | $this->expectExceptionMessage('Link not found'); | |
226 | ||
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 | } |