]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - tests/api/controllers/links/PostLinkTest.php
Optimize and cleanup imports
[github/shaarli/Shaarli.git] / tests / api / controllers / links / PostLinkTest.php
1 <?php
2
3 namespace Shaarli\Api\Controllers;
4
5 use PHPUnit\Framework\TestCase;
6 use Shaarli\Config\ConfigManager;
7 use Shaarli\History;
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 HistoryController 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 \Shaarli\Bookmark\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(
132 new \DateTime('5 seconds ago') < \DateTime::createFromFormat(\DateTime::ATOM, $data['created'])
133 );
134 $this->assertEquals('', $data['updated']);
135
136 $historyEntry = $this->history->getHistory()[0];
137 $this->assertEquals(History::CREATED, $historyEntry['event']);
138 $this->assertTrue(
139 (new \DateTime())->add(\DateInterval::createFromDateString('-5 seconds')) < $historyEntry['datetime']
140 );
141 $this->assertEquals(43, $historyEntry['id']);
142 }
143
144 /**
145 * Test link creation with all available fields.
146 */
147 public function testPostLinkFull()
148 {
149 $link = [
150 'url' => 'website.tld/test?foo=bar',
151 'title' => 'new entry',
152 'description' => 'shaare description',
153 'tags' => ['one', 'two'],
154 'private' => true,
155 ];
156 $env = Environment::mock([
157 'REQUEST_METHOD' => 'POST',
158 'CONTENT_TYPE' => 'application/json'
159 ]);
160
161 $request = Request::createFromEnvironment($env);
162 $request = $request->withParsedBody($link);
163 $response = $this->controller->postLink($request, new Response());
164
165 $this->assertEquals(201, $response->getStatusCode());
166 $this->assertEquals('api/v1/links/1', $response->getHeader('Location')[0]);
167 $data = json_decode((string) $response->getBody(), true);
168 $this->assertEquals(self::NB_FIELDS_LINK, count($data));
169 $this->assertEquals(43, $data['id']);
170 $this->assertRegExp('/[\w_-]{6}/', $data['shorturl']);
171 $this->assertEquals('http://' . $link['url'], $data['url']);
172 $this->assertEquals($link['title'], $data['title']);
173 $this->assertEquals($link['description'], $data['description']);
174 $this->assertEquals($link['tags'], $data['tags']);
175 $this->assertEquals(true, $data['private']);
176 $this->assertTrue(
177 new \DateTime('2 seconds ago') < \DateTime::createFromFormat(\DateTime::ATOM, $data['created'])
178 );
179 $this->assertEquals('', $data['updated']);
180 }
181
182 /**
183 * Test link creation with an existing link (duplicate URL). Should return a 409 HTTP error and the existing link.
184 */
185 public function testPostLinkDuplicate()
186 {
187 $link = [
188 'url' => 'mediagoblin.org/',
189 'title' => 'new entry',
190 'description' => 'shaare description',
191 'tags' => ['one', 'two'],
192 'private' => true,
193 ];
194 $env = Environment::mock([
195 'REQUEST_METHOD' => 'POST',
196 'CONTENT_TYPE' => 'application/json'
197 ]);
198
199 $request = Request::createFromEnvironment($env);
200 $request = $request->withParsedBody($link);
201 $response = $this->controller->postLink($request, new Response());
202
203 $this->assertEquals(409, $response->getStatusCode());
204 $data = json_decode((string) $response->getBody(), true);
205 $this->assertEquals(self::NB_FIELDS_LINK, count($data));
206 $this->assertEquals(7, $data['id']);
207 $this->assertEquals('IuWvgA', $data['shorturl']);
208 $this->assertEquals('http://mediagoblin.org/', $data['url']);
209 $this->assertEquals('MediaGoblin', $data['title']);
210 $this->assertEquals('A free software media publishing platform #hashtagOther', $data['description']);
211 $this->assertEquals(['gnu', 'media', 'web', '.hidden', 'hashtag'], $data['tags']);
212 $this->assertEquals(false, $data['private']);
213 $this->assertEquals(
214 \DateTime::createFromFormat(\Shaarli\Bookmark\LinkDB::LINK_DATE_FORMAT, '20130614_184135'),
215 \DateTime::createFromFormat(\DateTime::ATOM, $data['created'])
216 );
217 $this->assertEquals(
218 \DateTime::createFromFormat(\Shaarli\Bookmark\LinkDB::LINK_DATE_FORMAT, '20130615_184230'),
219 \DateTime::createFromFormat(\DateTime::ATOM, $data['updated'])
220 );
221 }
222 }