]>
Commit | Line | Data |
---|---|---|
68016e37 A |
1 | <?php |
2 | ||
3 | namespace Shaarli\Api\Controllers; | |
4 | ||
fd1ddad9 | 5 | use malkusch\lock\mutex\NoMutex; |
e26e2060 A |
6 | use Shaarli\Bookmark\Bookmark; |
7 | use Shaarli\Bookmark\BookmarkFileService; | |
68016e37 | 8 | use Shaarli\Config\ConfigManager; |
dea72c71 | 9 | use Shaarli\History; |
a5a9cf23 | 10 | use Shaarli\TestCase; |
68016e37 A |
11 | use Slim\Container; |
12 | use Slim\Http\Environment; | |
13 | use Slim\Http\Request; | |
14 | use Slim\Http\Response; | |
5617dcf9 | 15 | use Slim\Router; |
68016e37 A |
16 | |
17 | /** | |
18 | * Class PostLinkTest | |
19 | * | |
20 | * Test POST Link REST API service. | |
21 | * | |
22 | * @package Shaarli\Api\Controllers | |
23 | */ | |
5617dcf9 | 24 | class PostLinkTest extends TestCase |
68016e37 A |
25 | { |
26 | /** | |
27 | * @var string datastore to test write operations | |
28 | */ | |
29 | protected static $testDatastore = 'sandbox/datastore.php'; | |
30 | ||
813849e5 A |
31 | /** |
32 | * @var string datastore to test write operations | |
33 | */ | |
34 | protected static $testHistory = 'sandbox/history.php'; | |
35 | ||
68016e37 A |
36 | /** |
37 | * @var ConfigManager instance | |
38 | */ | |
39 | protected $conf; | |
40 | ||
41 | /** | |
42 | * @var \ReferenceLinkDB instance. | |
43 | */ | |
44 | protected $refDB = null; | |
45 | ||
e26e2060 A |
46 | /** |
47 | * @var BookmarkFileService instance. | |
48 | */ | |
49 | protected $bookmarkService; | |
50 | ||
813849e5 | 51 | /** |
dea72c71 | 52 | * @var HistoryController instance. |
813849e5 A |
53 | */ |
54 | protected $history; | |
55 | ||
68016e37 A |
56 | /** |
57 | * @var Container instance. | |
58 | */ | |
59 | protected $container; | |
60 | ||
61 | /** | |
62 | * @var Links controller instance. | |
63 | */ | |
64 | protected $controller; | |
65 | ||
66 | /** | |
67 | * Number of JSON field per link. | |
68 | */ | |
69 | const NB_FIELDS_LINK = 9; | |
70 | ||
71 | /** | |
e26e2060 | 72 | * Before every test, instantiate a new Api with its config, plugins and bookmarks. |
68016e37 | 73 | */ |
8f60e120 | 74 | protected function setUp(): void |
68016e37 | 75 | { |
fd1ddad9 | 76 | $mutex = new NoMutex(); |
e26e2060 A |
77 | $this->conf = new ConfigManager('tests/utils/config/configJson'); |
78 | $this->conf->set('resource.datastore', self::$testDatastore); | |
68016e37 A |
79 | $this->refDB = new \ReferenceLinkDB(); |
80 | $this->refDB->write(self::$testDatastore); | |
813849e5 A |
81 | $refHistory = new \ReferenceHistory(); |
82 | $refHistory->write(self::$testHistory); | |
dea72c71 | 83 | $this->history = new History(self::$testHistory); |
fd1ddad9 | 84 | $this->bookmarkService = new BookmarkFileService($this->conf, $this->history, $mutex, true); |
813849e5 | 85 | |
68016e37 A |
86 | $this->container = new Container(); |
87 | $this->container['conf'] = $this->conf; | |
e26e2060 A |
88 | $this->container['db'] = $this->bookmarkService; |
89 | $this->container['history'] = $this->history; | |
68016e37 A |
90 | |
91 | $this->controller = new Links($this->container); | |
92 | ||
5617dcf9 | 93 | $mock = $this->createMock(Router::class); |
68016e37 | 94 | $mock->expects($this->any()) |
b37ca790 LC |
95 | ->method('pathFor') |
96 | ->willReturn('/api/v1/bookmarks/1'); | |
68016e37 A |
97 | |
98 | // affect @property-read... seems to work | |
99 | $this->controller->getCi()->router = $mock; | |
100 | ||
101 | // Used by index_url(). | |
102 | $this->controller->getCi()['environment'] = [ | |
103 | 'SERVER_NAME' => 'domain.tld', | |
104 | 'SERVER_PORT' => 80, | |
105 | 'SCRIPT_NAME' => '/', | |
106 | ]; | |
107 | } | |
108 | ||
109 | /** | |
110 | * After every test, remove the test datastore. | |
111 | */ | |
8f60e120 | 112 | protected function tearDown(): void |
68016e37 A |
113 | { |
114 | @unlink(self::$testDatastore); | |
813849e5 | 115 | @unlink(self::$testHistory); |
68016e37 A |
116 | } |
117 | ||
118 | /** | |
119 | * Test link creation without any field: creates a blank note. | |
120 | */ | |
121 | public function testPostLinkMinimal() | |
122 | { | |
123 | $env = Environment::mock([ | |
124 | 'REQUEST_METHOD' => 'POST', | |
125 | ]); | |
126 | ||
127 | $request = Request::createFromEnvironment($env); | |
128 | ||
129 | $response = $this->controller->postLink($request, new Response()); | |
130 | $this->assertEquals(201, $response->getStatusCode()); | |
b37ca790 | 131 | $this->assertEquals('/api/v1/bookmarks/1', $response->getHeader('Location')[0]); |
68016e37 A |
132 | $data = json_decode((string) $response->getBody(), true); |
133 | $this->assertEquals(self::NB_FIELDS_LINK, count($data)); | |
134 | $this->assertEquals(43, $data['id']); | |
a43e7842 | 135 | $this->assertRegExp('/[\w_-]{6}/', $data['shorturl']); |
301c7ab1 A |
136 | $this->assertEquals('http://domain.tld/shaare/' . $data['shorturl'], $data['url']); |
137 | $this->assertEquals('/shaare/' . $data['shorturl'], $data['title']); | |
68016e37 A |
138 | $this->assertEquals('', $data['description']); |
139 | $this->assertEquals([], $data['tags']); | |
e26e2060 | 140 | $this->assertEquals(true, $data['private']); |
9d9f6d75 V |
141 | $this->assertTrue( |
142 | new \DateTime('5 seconds ago') < \DateTime::createFromFormat(\DateTime::ATOM, $data['created']) | |
143 | ); | |
68016e37 | 144 | $this->assertEquals('', $data['updated']); |
813849e5 A |
145 | |
146 | $historyEntry = $this->history->getHistory()[0]; | |
dea72c71 | 147 | $this->assertEquals(History::CREATED, $historyEntry['event']); |
813849e5 A |
148 | $this->assertTrue( |
149 | (new \DateTime())->add(\DateInterval::createFromDateString('-5 seconds')) < $historyEntry['datetime'] | |
150 | ); | |
151 | $this->assertEquals(43, $historyEntry['id']); | |
68016e37 A |
152 | } |
153 | ||
154 | /** | |
155 | * Test link creation with all available fields. | |
156 | */ | |
157 | public function testPostLinkFull() | |
158 | { | |
159 | $link = [ | |
160 | 'url' => 'website.tld/test?foo=bar', | |
161 | 'title' => 'new entry', | |
162 | 'description' => 'shaare description', | |
163 | 'tags' => ['one', 'two'], | |
164 | 'private' => true, | |
b06fc28a A |
165 | 'created' => '2015-05-05T12:30:00+03:00', |
166 | 'updated' => '2016-06-05T14:32:10+03:00', | |
68016e37 A |
167 | ]; |
168 | $env = Environment::mock([ | |
169 | 'REQUEST_METHOD' => 'POST', | |
170 | 'CONTENT_TYPE' => 'application/json' | |
171 | ]); | |
172 | ||
173 | $request = Request::createFromEnvironment($env); | |
174 | $request = $request->withParsedBody($link); | |
175 | $response = $this->controller->postLink($request, new Response()); | |
176 | ||
177 | $this->assertEquals(201, $response->getStatusCode()); | |
b37ca790 | 178 | $this->assertEquals('/api/v1/bookmarks/1', $response->getHeader('Location')[0]); |
68016e37 A |
179 | $data = json_decode((string) $response->getBody(), true); |
180 | $this->assertEquals(self::NB_FIELDS_LINK, count($data)); | |
181 | $this->assertEquals(43, $data['id']); | |
a43e7842 | 182 | $this->assertRegExp('/[\w_-]{6}/', $data['shorturl']); |
68016e37 A |
183 | $this->assertEquals('http://' . $link['url'], $data['url']); |
184 | $this->assertEquals($link['title'], $data['title']); | |
185 | $this->assertEquals($link['description'], $data['description']); | |
186 | $this->assertEquals($link['tags'], $data['tags']); | |
187 | $this->assertEquals(true, $data['private']); | |
b06fc28a A |
188 | $this->assertSame($link['created'], $data['created']); |
189 | $this->assertSame($link['updated'], $data['updated']); | |
68016e37 A |
190 | } |
191 | ||
192 | /** | |
193 | * Test link creation with an existing link (duplicate URL). Should return a 409 HTTP error and the existing link. | |
194 | */ | |
195 | public function testPostLinkDuplicate() | |
196 | { | |
197 | $link = [ | |
198 | 'url' => 'mediagoblin.org/', | |
199 | 'title' => 'new entry', | |
200 | 'description' => 'shaare description', | |
201 | 'tags' => ['one', 'two'], | |
202 | 'private' => true, | |
203 | ]; | |
204 | $env = Environment::mock([ | |
205 | 'REQUEST_METHOD' => 'POST', | |
206 | 'CONTENT_TYPE' => 'application/json' | |
207 | ]); | |
208 | ||
209 | $request = Request::createFromEnvironment($env); | |
210 | $request = $request->withParsedBody($link); | |
211 | $response = $this->controller->postLink($request, new Response()); | |
212 | ||
213 | $this->assertEquals(409, $response->getStatusCode()); | |
214 | $data = json_decode((string) $response->getBody(), true); | |
215 | $this->assertEquals(self::NB_FIELDS_LINK, count($data)); | |
216 | $this->assertEquals(7, $data['id']); | |
217 | $this->assertEquals('IuWvgA', $data['shorturl']); | |
218 | $this->assertEquals('http://mediagoblin.org/', $data['url']); | |
219 | $this->assertEquals('MediaGoblin', $data['title']); | |
220 | $this->assertEquals('A free software media publishing platform #hashtagOther', $data['description']); | |
221 | $this->assertEquals(['gnu', 'media', 'web', '.hidden', 'hashtag'], $data['tags']); | |
222 | $this->assertEquals(false, $data['private']); | |
223 | $this->assertEquals( | |
e26e2060 | 224 | \DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20130614_184135'), |
68016e37 A |
225 | \DateTime::createFromFormat(\DateTime::ATOM, $data['created']) |
226 | ); | |
227 | $this->assertEquals( | |
e26e2060 | 228 | \DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20130615_184230'), |
68016e37 A |
229 | \DateTime::createFromFormat(\DateTime::ATOM, $data['updated']) |
230 | ); | |
231 | } | |
232 | } |