]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - tests/api/controllers/links/PostLinkTest.php
Merge pull request #1616 from dimtion/fix-api-redirect
[github/shaarli/Shaarli.git] / tests / api / controllers / links / PostLinkTest.php
1 <?php
2
3 namespace Shaarli\Api\Controllers;
4
5 use malkusch\lock\mutex\NoMutex;
6 use Shaarli\Bookmark\Bookmark;
7 use Shaarli\Bookmark\BookmarkFileService;
8 use Shaarli\Config\ConfigManager;
9 use Shaarli\History;
10 use Shaarli\TestCase;
11 use Slim\Container;
12 use Slim\Http\Environment;
13 use Slim\Http\Request;
14 use Slim\Http\Response;
15 use Slim\Router;
16
17 /**
18 * Class PostLinkTest
19 *
20 * Test POST Link REST API service.
21 *
22 * @package Shaarli\Api\Controllers
23 */
24 class PostLinkTest extends TestCase
25 {
26 /**
27 * @var string datastore to test write operations
28 */
29 protected static $testDatastore = 'sandbox/datastore.php';
30
31 /**
32 * @var string datastore to test write operations
33 */
34 protected static $testHistory = 'sandbox/history.php';
35
36 /**
37 * @var ConfigManager instance
38 */
39 protected $conf;
40
41 /**
42 * @var \ReferenceLinkDB instance.
43 */
44 protected $refDB = null;
45
46 /**
47 * @var BookmarkFileService instance.
48 */
49 protected $bookmarkService;
50
51 /**
52 * @var HistoryController instance.
53 */
54 protected $history;
55
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 /**
72 * Before every test, instantiate a new Api with its config, plugins and bookmarks.
73 */
74 protected function setUp(): void
75 {
76 $mutex = new NoMutex();
77 $this->conf = new ConfigManager('tests/utils/config/configJson');
78 $this->conf->set('resource.datastore', self::$testDatastore);
79 $this->refDB = new \ReferenceLinkDB();
80 $this->refDB->write(self::$testDatastore);
81 $refHistory = new \ReferenceHistory();
82 $refHistory->write(self::$testHistory);
83 $this->history = new History(self::$testHistory);
84 $this->bookmarkService = new BookmarkFileService($this->conf, $this->history, $mutex, true);
85
86 $this->container = new Container();
87 $this->container['conf'] = $this->conf;
88 $this->container['db'] = $this->bookmarkService;
89 $this->container['history'] = $this->history;
90
91 $this->controller = new Links($this->container);
92
93 $mock = $this->createMock(Router::class);
94 $mock->expects($this->any())
95 ->method('pathFor')
96 ->willReturn('/api/v1/bookmarks/1');
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 */
112 protected function tearDown(): void
113 {
114 @unlink(self::$testDatastore);
115 @unlink(self::$testHistory);
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());
131 $this->assertEquals('/api/v1/bookmarks/1', $response->getHeader('Location')[0]);
132 $data = json_decode((string) $response->getBody(), true);
133 $this->assertEquals(self::NB_FIELDS_LINK, count($data));
134 $this->assertEquals(43, $data['id']);
135 $this->assertRegExp('/[\w_-]{6}/', $data['shorturl']);
136 $this->assertEquals('http://domain.tld/shaare/' . $data['shorturl'], $data['url']);
137 $this->assertEquals('/shaare/' . $data['shorturl'], $data['title']);
138 $this->assertEquals('', $data['description']);
139 $this->assertEquals([], $data['tags']);
140 $this->assertEquals(true, $data['private']);
141 $this->assertTrue(
142 new \DateTime('5 seconds ago') < \DateTime::createFromFormat(\DateTime::ATOM, $data['created'])
143 );
144 $this->assertEquals('', $data['updated']);
145
146 $historyEntry = $this->history->getHistory()[0];
147 $this->assertEquals(History::CREATED, $historyEntry['event']);
148 $this->assertTrue(
149 (new \DateTime())->add(\DateInterval::createFromDateString('-5 seconds')) < $historyEntry['datetime']
150 );
151 $this->assertEquals(43, $historyEntry['id']);
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,
165 'created' => '2015-05-05T12:30:00+03:00',
166 'updated' => '2016-06-05T14:32:10+03:00',
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());
178 $this->assertEquals('/api/v1/bookmarks/1', $response->getHeader('Location')[0]);
179 $data = json_decode((string) $response->getBody(), true);
180 $this->assertEquals(self::NB_FIELDS_LINK, count($data));
181 $this->assertEquals(43, $data['id']);
182 $this->assertRegExp('/[\w_-]{6}/', $data['shorturl']);
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']);
188 $this->assertSame($link['created'], $data['created']);
189 $this->assertSame($link['updated'], $data['updated']);
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(
224 \DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20130614_184135'),
225 \DateTime::createFromFormat(\DateTime::ATOM, $data['created'])
226 );
227 $this->assertEquals(
228 \DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20130615_184230'),
229 \DateTime::createFromFormat(\DateTime::ATOM, $data['updated'])
230 );
231 }
232 }