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