]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - tests/api/controllers/links/PostLinkTest.php
API: update test regexes to comply with PCRE2
[github/shaarli/Shaarli.git] / tests / api / controllers / links / PostLinkTest.php
CommitLineData
68016e37
A
1<?php
2
3namespace Shaarli\Api\Controllers;
4
5617dcf9 5use PHPUnit\Framework\TestCase;
68016e37
A
6use Shaarli\Config\ConfigManager;
7use Slim\Container;
8use Slim\Http\Environment;
9use Slim\Http\Request;
10use Slim\Http\Response;
5617dcf9 11use Slim\Router;
68016e37
A
12
13/**
14 * Class PostLinkTest
15 *
16 * Test POST Link REST API service.
17 *
18 * @package Shaarli\Api\Controllers
19 */
5617dcf9 20class PostLinkTest extends TestCase
68016e37
A
21{
22 /**
23 * @var string datastore to test write operations
24 */
25 protected static $testDatastore = 'sandbox/datastore.php';
26
813849e5
A
27 /**
28 * @var string datastore to test write operations
29 */
30 protected static $testHistory = 'sandbox/history.php';
31
68016e37
A
32 /**
33 * @var ConfigManager instance
34 */
35 protected $conf;
36
37 /**
38 * @var \ReferenceLinkDB instance.
39 */
40 protected $refDB = null;
41
813849e5 42 /**
bdc5152d 43 * @var \Shaarli\History instance.
813849e5
A
44 */
45 protected $history;
46
68016e37
A
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
813849e5
A
71 $refHistory = new \ReferenceHistory();
72 $refHistory->write(self::$testHistory);
bdc5152d 73 $this->history = new \Shaarli\History(self::$testHistory);
813849e5 74
68016e37
A
75 $this->container = new Container();
76 $this->container['conf'] = $this->conf;
f24896b2 77 $this->container['db'] = new \Shaarli\Bookmark\LinkDB(self::$testDatastore, true, false);
bdc5152d 78 $this->container['history'] = new \Shaarli\History(self::$testHistory);
68016e37
A
79
80 $this->controller = new Links($this->container);
81
5617dcf9 82 $mock = $this->createMock(Router::class);
68016e37
A
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);
813849e5 104 @unlink(self::$testHistory);
68016e37
A
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']);
a43e7842 124 $this->assertRegExp('/[\w_-]{6}/', $data['shorturl']);
68016e37
A
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']);
9d9f6d75
V
130 $this->assertTrue(
131 new \DateTime('5 seconds ago') < \DateTime::createFromFormat(\DateTime::ATOM, $data['created'])
132 );
68016e37 133 $this->assertEquals('', $data['updated']);
813849e5
A
134
135 $historyEntry = $this->history->getHistory()[0];
bdc5152d 136 $this->assertEquals(\Shaarli\History::CREATED, $historyEntry['event']);
813849e5
A
137 $this->assertTrue(
138 (new \DateTime())->add(\DateInterval::createFromDateString('-5 seconds')) < $historyEntry['datetime']
139 );
140 $this->assertEquals(43, $historyEntry['id']);
68016e37
A
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']);
a43e7842 169 $this->assertRegExp('/[\w_-]{6}/', $data['shorturl']);
68016e37
A
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']);
9d9f6d75
V
175 $this->assertTrue(
176 new \DateTime('2 seconds ago') < \DateTime::createFromFormat(\DateTime::ATOM, $data['created'])
177 );
68016e37
A
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(
f24896b2 213 \DateTime::createFromFormat(\Shaarli\Bookmark\LinkDB::LINK_DATE_FORMAT, '20130614_184135'),
68016e37
A
214 \DateTime::createFromFormat(\DateTime::ATOM, $data['created'])
215 );
216 $this->assertEquals(
f24896b2 217 \DateTime::createFromFormat(\Shaarli\Bookmark\LinkDB::LINK_DATE_FORMAT, '20130615_184230'),
68016e37
A
218 \DateTime::createFromFormat(\DateTime::ATOM, $data['updated'])
219 );
220 }
221}