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