aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/api/controllers/links/PostLinkTest.php
diff options
context:
space:
mode:
authorArthurHoaro <arthur@hoa.ro>2019-07-27 12:34:30 +0200
committerArthurHoaro <arthur@hoa.ro>2019-07-27 12:34:30 +0200
commit38672ba0d1c722e5d6d33a58255ceb55e9410e46 (patch)
treedae4c7c47532380eac3ae641db99122fc77c93dc /tests/api/controllers/links/PostLinkTest.php
parent83faedadff76c5bdca036f39f13943f63b27e164 (diff)
parent1e77e0448bbd25675d8c0fe4a73206ad9048904b (diff)
downloadShaarli-38672ba0d1c722e5d6d33a58255ceb55e9410e46.tar.gz
Shaarli-38672ba0d1c722e5d6d33a58255ceb55e9410e46.tar.zst
Shaarli-38672ba0d1c722e5d6d33a58255ceb55e9410e46.zip
Merge tag 'v0.10.4' into stable
Release v0.10.4
Diffstat (limited to 'tests/api/controllers/links/PostLinkTest.php')
-rw-r--r--tests/api/controllers/links/PostLinkTest.php221
1 files changed, 221 insertions, 0 deletions
diff --git a/tests/api/controllers/links/PostLinkTest.php b/tests/api/controllers/links/PostLinkTest.php
new file mode 100644
index 00000000..5c2b5623
--- /dev/null
+++ b/tests/api/controllers/links/PostLinkTest.php
@@ -0,0 +1,221 @@
1<?php
2
3namespace Shaarli\Api\Controllers;
4
5use PHPUnit\Framework\TestCase;
6use Shaarli\Config\ConfigManager;
7use Slim\Container;
8use Slim\Http\Environment;
9use Slim\Http\Request;
10use Slim\Http\Response;
11use Slim\Router;
12
13/**
14 * Class PostLinkTest
15 *
16 * Test POST Link REST API service.
17 *
18 * @package Shaarli\Api\Controllers
19 */
20class PostLinkTest extends TestCase
21{
22 /**
23 * @var string datastore to test write operations
24 */
25 protected static $testDatastore = 'sandbox/datastore.php';
26
27 /**
28 * @var string datastore to test write operations
29 */
30 protected static $testHistory = 'sandbox/history.php';
31
32 /**
33 * @var ConfigManager instance
34 */
35 protected $conf;
36
37 /**
38 * @var \ReferenceLinkDB instance.
39 */
40 protected $refDB = null;
41
42 /**
43 * @var \History 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 links.
64 */
65 public function setUp()
66 {
67 $this->conf = new ConfigManager('tests/utils/config/configJson.json.php');
68 $this->refDB = new \ReferenceLinkDB();
69 $this->refDB->write(self::$testDatastore);
70
71 $refHistory = new \ReferenceHistory();
72 $refHistory->write(self::$testHistory);
73 $this->history = new \History(self::$testHistory);
74
75 $this->container = new Container();
76 $this->container['conf'] = $this->conf;
77 $this->container['db'] = new \LinkDB(self::$testDatastore, true, false);
78 $this->container['history'] = new \History(self::$testHistory);
79
80 $this->controller = new Links($this->container);
81
82 $mock = $this->createMock(Router::class);
83 $mock->expects($this->any())
84 ->method('relativePathFor')
85 ->willReturn('api/v1/links/1');
86
87 // affect @property-read... seems to work
88 $this->controller->getCi()->router = $mock;
89
90 // Used by index_url().
91 $this->controller->getCi()['environment'] = [
92 'SERVER_NAME' => 'domain.tld',
93 'SERVER_PORT' => 80,
94 'SCRIPT_NAME' => '/',
95 ];
96 }
97
98 /**
99 * After every test, remove the test datastore.
100 */
101 public function tearDown()
102 {
103 @unlink(self::$testDatastore);
104 @unlink(self::$testHistory);
105 }
106
107 /**
108 * Test link creation without any field: creates a blank note.
109 */
110 public function testPostLinkMinimal()
111 {
112 $env = Environment::mock([
113 'REQUEST_METHOD' => 'POST',
114 ]);
115
116 $request = Request::createFromEnvironment($env);
117
118 $response = $this->controller->postLink($request, new Response());
119 $this->assertEquals(201, $response->getStatusCode());
120 $this->assertEquals('api/v1/links/1', $response->getHeader('Location')[0]);
121 $data = json_decode((string) $response->getBody(), true);
122 $this->assertEquals(self::NB_FIELDS_LINK, count($data));
123 $this->assertEquals(43, $data['id']);
124 $this->assertRegExp('/[\w-_]{6}/', $data['shorturl']);
125 $this->assertEquals('http://domain.tld/?' . $data['shorturl'], $data['url']);
126 $this->assertEquals('?' . $data['shorturl'], $data['title']);
127 $this->assertEquals('', $data['description']);
128 $this->assertEquals([], $data['tags']);
129 $this->assertEquals(false, $data['private']);
130 $this->assertTrue(
131 new \DateTime('5 seconds ago') < \DateTime::createFromFormat(\DateTime::ATOM, $data['created'])
132 );
133 $this->assertEquals('', $data['updated']);
134
135 $historyEntry = $this->history->getHistory()[0];
136 $this->assertEquals(\History::CREATED, $historyEntry['event']);
137 $this->assertTrue(
138 (new \DateTime())->add(\DateInterval::createFromDateString('-5 seconds')) < $historyEntry['datetime']
139 );
140 $this->assertEquals(43, $historyEntry['id']);
141 }
142
143 /**
144 * Test link creation with all available fields.
145 */
146 public function testPostLinkFull()
147 {
148 $link = [
149 'url' => 'website.tld/test?foo=bar',
150 'title' => 'new entry',
151 'description' => 'shaare description',
152 'tags' => ['one', 'two'],
153 'private' => true,
154 ];
155 $env = Environment::mock([
156 'REQUEST_METHOD' => 'POST',
157 'CONTENT_TYPE' => 'application/json'
158 ]);
159
160 $request = Request::createFromEnvironment($env);
161 $request = $request->withParsedBody($link);
162 $response = $this->controller->postLink($request, new Response());
163
164 $this->assertEquals(201, $response->getStatusCode());
165 $this->assertEquals('api/v1/links/1', $response->getHeader('Location')[0]);
166 $data = json_decode((string) $response->getBody(), true);
167 $this->assertEquals(self::NB_FIELDS_LINK, count($data));
168 $this->assertEquals(43, $data['id']);
169 $this->assertRegExp('/[\w-_]{6}/', $data['shorturl']);
170 $this->assertEquals('http://' . $link['url'], $data['url']);
171 $this->assertEquals($link['title'], $data['title']);
172 $this->assertEquals($link['description'], $data['description']);
173 $this->assertEquals($link['tags'], $data['tags']);
174 $this->assertEquals(true, $data['private']);
175 $this->assertTrue(
176 new \DateTime('2 seconds ago') < \DateTime::createFromFormat(\DateTime::ATOM, $data['created'])
177 );
178 $this->assertEquals('', $data['updated']);
179 }
180
181 /**
182 * Test link creation with an existing link (duplicate URL). Should return a 409 HTTP error and the existing link.
183 */
184 public function testPostLinkDuplicate()
185 {
186 $link = [
187 'url' => 'mediagoblin.org/',
188 'title' => 'new entry',
189 'description' => 'shaare description',
190 'tags' => ['one', 'two'],
191 'private' => true,
192 ];
193 $env = Environment::mock([
194 'REQUEST_METHOD' => 'POST',
195 'CONTENT_TYPE' => 'application/json'
196 ]);
197
198 $request = Request::createFromEnvironment($env);
199 $request = $request->withParsedBody($link);
200 $response = $this->controller->postLink($request, new Response());
201
202 $this->assertEquals(409, $response->getStatusCode());
203 $data = json_decode((string) $response->getBody(), true);
204 $this->assertEquals(self::NB_FIELDS_LINK, count($data));
205 $this->assertEquals(7, $data['id']);
206 $this->assertEquals('IuWvgA', $data['shorturl']);
207 $this->assertEquals('http://mediagoblin.org/', $data['url']);
208 $this->assertEquals('MediaGoblin', $data['title']);
209 $this->assertEquals('A free software media publishing platform #hashtagOther', $data['description']);
210 $this->assertEquals(['gnu', 'media', 'web', '.hidden', 'hashtag'], $data['tags']);
211 $this->assertEquals(false, $data['private']);
212 $this->assertEquals(
213 \DateTime::createFromFormat(\LinkDB::LINK_DATE_FORMAT, '20130614_184135'),
214 \DateTime::createFromFormat(\DateTime::ATOM, $data['created'])
215 );
216 $this->assertEquals(
217 \DateTime::createFromFormat(\LinkDB::LINK_DATE_FORMAT, '20130615_184230'),
218 \DateTime::createFromFormat(\DateTime::ATOM, $data['updated'])
219 );
220 }
221}