diff options
author | ArthurHoaro <arthur@hoa.ro> | 2018-05-19 15:04:04 +0200 |
---|---|---|
committer | ArthurHoaro <arthur@hoa.ro> | 2018-06-04 18:51:22 +0200 |
commit | d3f42ca487287447efb81061609644108044a038 (patch) | |
tree | 6e9d5a33290f857c8f7a04dbdf8cf0ca749db149 /tests/api/controllers/links/PostLinkTest.php | |
parent | 17e45b2e9c33c736751e059276fadb480f98e621 (diff) | |
download | Shaarli-d3f42ca487287447efb81061609644108044a038.tar.gz Shaarli-d3f42ca487287447efb81061609644108044a038.tar.zst Shaarli-d3f42ca487287447efb81061609644108044a038.zip |
Implements Tags endpoints for Shaarli's REST API
Endpoints:
* List All Tags [GET]
* Get a tag [GET]
* Update a tag [PUT]
* Delete a tag [DELETE]
Fixes #904
References shaarli/api-documentation#34
Diffstat (limited to 'tests/api/controllers/links/PostLinkTest.php')
-rw-r--r-- | tests/api/controllers/links/PostLinkTest.php | 218 |
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 | |||
3 | namespace Shaarli\Api\Controllers; | ||
4 | |||
5 | |||
6 | use PHPUnit\Framework\TestCase; | ||
7 | use Shaarli\Config\ConfigManager; | ||
8 | use Slim\Container; | ||
9 | use Slim\Http\Environment; | ||
10 | use Slim\Http\Request; | ||
11 | use Slim\Http\Response; | ||
12 | use Slim\Router; | ||
13 | |||
14 | /** | ||
15 | * Class PostLinkTest | ||
16 | * | ||
17 | * Test POST Link REST API service. | ||
18 | * | ||
19 | * @package Shaarli\Api\Controllers | ||
20 | */ | ||
21 | class 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 | } | ||