aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/api/controllers/links/PostLinkTest.php
diff options
context:
space:
mode:
authorArthurHoaro <arthur@hoa.ro>2018-07-10 18:06:26 +0200
committerGitHub <noreply@github.com>2018-07-10 18:06:26 +0200
commitc2c2338f9adecf971d01d7abcd09bab6275a9706 (patch)
tree9c8fe4d6c1ca1d5667b6bce786d872471322fac1 /tests/api/controllers/links/PostLinkTest.php
parentedb4a4d9c9fbc01d67ee8d095fe26648714e2285 (diff)
parent6410bf96707e381685b7dafbefcccd83ea762934 (diff)
downloadShaarli-c2c2338f9adecf971d01d7abcd09bab6275a9706.tar.gz
Shaarli-c2c2338f9adecf971d01d7abcd09bab6275a9706.tar.zst
Shaarli-c2c2338f9adecf971d01d7abcd09bab6275a9706.zip
Merge pull request #1141 from ArthurHoaro/api/tags
Implements Tags endpoints for Shaarli's REST API
Diffstat (limited to 'tests/api/controllers/links/PostLinkTest.php')
-rw-r--r--tests/api/controllers/links/PostLinkTest.php218
1 files changed, 218 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..100a9170
--- /dev/null
+++ b/tests/api/controllers/links/PostLinkTest.php
@@ -0,0 +1,218 @@
1<?php
2
3namespace Shaarli\Api\Controllers;
4
5
6use PHPUnit\Framework\TestCase;
7use Shaarli\Config\ConfigManager;
8use Slim\Container;
9use Slim\Http\Environment;
10use Slim\Http\Request;
11use Slim\Http\Response;
12use Slim\Router;
13
14/**
15 * Class PostLinkTest
16 *
17 * Test POST Link REST API service.
18 *
19 * @package Shaarli\Api\Controllers
20 */
21class PostLinkTest extends TestCase
22{
23 /**
24 * @var string datastore to test write operations
25 */
26 protected static $testDatastore = 'sandbox/datastore.php';
27
28 /**
29 * @var string datastore to test write operations
30 */
31 protected static $testHistory = 'sandbox/history.php';
32
33 /**
34 * @var ConfigManager instance
35 */
36 protected $conf;
37
38 /**
39 * @var \ReferenceLinkDB instance.
40 */
41 protected $refDB = null;
42
43 /**
44 * @var \History instance.
45 */
46 protected $history;
47
48 /**
49 * @var Container instance.
50 */
51 protected $container;
52
53 /**
54 * @var Links controller instance.
55 */
56 protected $controller;
57
58 /**
59 * Number of JSON field per link.
60 */
61 const NB_FIELDS_LINK = 9;
62
63 /**
64 * Before every test, instantiate a new Api with its config, plugins and links.
65 */
66 public function setUp()
67 {
68 $this->conf = new ConfigManager('tests/utils/config/configJson.json.php');
69 $this->refDB = new \ReferenceLinkDB();
70 $this->refDB->write(self::$testDatastore);
71
72 $refHistory = new \ReferenceHistory();
73 $refHistory->write(self::$testHistory);
74 $this->history = new \History(self::$testHistory);
75
76 $this->container = new Container();
77 $this->container['conf'] = $this->conf;
78 $this->container['db'] = new \LinkDB(self::$testDatastore, true, false);
79 $this->container['history'] = new \History(self::$testHistory);
80
81 $this->controller = new Links($this->container);
82
83 $mock = $this->createMock(Router::class);
84 $mock->expects($this->any())
85 ->method('relativePathFor')
86 ->willReturn('api/v1/links/1');
87
88 // affect @property-read... seems to work
89 $this->controller->getCi()->router = $mock;
90
91 // Used by index_url().
92 $this->controller->getCi()['environment'] = [
93 'SERVER_NAME' => 'domain.tld',
94 'SERVER_PORT' => 80,
95 'SCRIPT_NAME' => '/',
96 ];
97 }
98
99 /**
100 * After every test, remove the test datastore.
101 */
102 public function tearDown()
103 {
104 @unlink(self::$testDatastore);
105 @unlink(self::$testHistory);
106 }
107
108 /**
109 * Test link creation without any field: creates a blank note.
110 */
111 public function testPostLinkMinimal()
112 {
113 $env = Environment::mock([
114 'REQUEST_METHOD' => 'POST',
115 ]);
116
117 $request = Request::createFromEnvironment($env);
118
119 $response = $this->controller->postLink($request, new Response());
120 $this->assertEquals(201, $response->getStatusCode());
121 $this->assertEquals('api/v1/links/1', $response->getHeader('Location')[0]);
122 $data = json_decode((string) $response->getBody(), true);
123 $this->assertEquals(self::NB_FIELDS_LINK, count($data));
124 $this->assertEquals(43, $data['id']);
125 $this->assertRegExp('/[\w-_]{6}/', $data['shorturl']);
126 $this->assertEquals('http://domain.tld/?' . $data['shorturl'], $data['url']);
127 $this->assertEquals('?' . $data['shorturl'], $data['title']);
128 $this->assertEquals('', $data['description']);
129 $this->assertEquals([], $data['tags']);
130 $this->assertEquals(false, $data['private']);
131 $this->assertTrue(new \DateTime('5 seconds ago') < \DateTime::createFromFormat(\DateTime::ATOM, $data['created']));
132 $this->assertEquals('', $data['updated']);
133
134 $historyEntry = $this->history->getHistory()[0];
135 $this->assertEquals(\History::CREATED, $historyEntry['event']);
136 $this->assertTrue(
137 (new \DateTime())->add(\DateInterval::createFromDateString('-5 seconds')) < $historyEntry['datetime']
138 );
139 $this->assertEquals(43, $historyEntry['id']);
140 }
141
142 /**
143 * Test link creation with all available fields.
144 */
145 public function testPostLinkFull()
146 {
147 $link = [
148 'url' => 'website.tld/test?foo=bar',
149 'title' => 'new entry',
150 'description' => 'shaare description',
151 'tags' => ['one', 'two'],
152 'private' => true,
153 ];
154 $env = Environment::mock([
155 'REQUEST_METHOD' => 'POST',
156 'CONTENT_TYPE' => 'application/json'
157 ]);
158
159 $request = Request::createFromEnvironment($env);
160 $request = $request->withParsedBody($link);
161 $response = $this->controller->postLink($request, new Response());
162
163 $this->assertEquals(201, $response->getStatusCode());
164 $this->assertEquals('api/v1/links/1', $response->getHeader('Location')[0]);
165 $data = json_decode((string) $response->getBody(), true);
166 $this->assertEquals(self::NB_FIELDS_LINK, count($data));
167 $this->assertEquals(43, $data['id']);
168 $this->assertRegExp('/[\w-_]{6}/', $data['shorturl']);
169 $this->assertEquals('http://' . $link['url'], $data['url']);
170 $this->assertEquals($link['title'], $data['title']);
171 $this->assertEquals($link['description'], $data['description']);
172 $this->assertEquals($link['tags'], $data['tags']);
173 $this->assertEquals(true, $data['private']);
174 $this->assertTrue(new \DateTime('2 seconds ago') < \DateTime::createFromFormat(\DateTime::ATOM, $data['created']));
175 $this->assertEquals('', $data['updated']);
176 }
177
178 /**
179 * Test link creation with an existing link (duplicate URL). Should return a 409 HTTP error and the existing link.
180 */
181 public function testPostLinkDuplicate()
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' => 'POST',
192 'CONTENT_TYPE' => 'application/json'
193 ]);
194
195 $request = Request::createFromEnvironment($env);
196 $request = $request->withParsedBody($link);
197 $response = $this->controller->postLink($request, new Response());
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(\LinkDB::LINK_DATE_FORMAT, '20130614_184135'),
211 \DateTime::createFromFormat(\DateTime::ATOM, $data['created'])
212 );
213 $this->assertEquals(
214 \DateTime::createFromFormat(\LinkDB::LINK_DATE_FORMAT, '20130615_184230'),
215 \DateTime::createFromFormat(\DateTime::ATOM, $data['updated'])
216 );
217 }
218}