aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/api
diff options
context:
space:
mode:
authorArthurHoaro <arthur@hoa.ro>2020-10-13 12:05:08 +0200
committerArthurHoaro <arthur@hoa.ro>2020-10-13 12:05:08 +0200
commitb6f678a5a1d15acf284ebcec16c905e976671ce1 (patch)
tree33c7da831482ed79c44896ef19c73c72ada84f2e /tests/api
parentb14687036b9b800681197f51fdc47e62f0c88e2e (diff)
parent1c1520b6b98ab20201bfe15577782a52320339df (diff)
downloadShaarli-b6f678a5a1d15acf284ebcec16c905e976671ce1.tar.gz
Shaarli-b6f678a5a1d15acf284ebcec16c905e976671ce1.tar.zst
Shaarli-b6f678a5a1d15acf284ebcec16c905e976671ce1.zip
Merge branch 'v0.12' into latest
Diffstat (limited to 'tests/api')
-rw-r--r--tests/api/ApiMiddlewareTest.php73
-rw-r--r--tests/api/ApiUtilsTest.php145
-rw-r--r--tests/api/controllers/history/HistoryTest.php10
-rw-r--r--tests/api/controllers/info/InfoTest.php24
-rw-r--r--tests/api/controllers/links/DeleteLinkTest.php32
-rw-r--r--tests/api/controllers/links/GetLinkIdTest.php25
-rw-r--r--tests/api/controllers/links/GetLinksTest.php23
-rw-r--r--tests/api/controllers/links/PostLinkTest.php40
-rw-r--r--tests/api/controllers/links/PutLinkTest.php40
-rw-r--r--tests/api/controllers/tags/DeleteTagTest.php41
-rw-r--r--tests/api/controllers/tags/GetTagNameTest.php20
-rw-r--r--tests/api/controllers/tags/GetTagsTest.php29
-rw-r--r--tests/api/controllers/tags/PutTagTest.php43
13 files changed, 323 insertions, 222 deletions
diff --git a/tests/api/ApiMiddlewareTest.php b/tests/api/ApiMiddlewareTest.php
index 0b9b03f2..86700840 100644
--- a/tests/api/ApiMiddlewareTest.php
+++ b/tests/api/ApiMiddlewareTest.php
@@ -2,6 +2,7 @@
2namespace Shaarli\Api; 2namespace Shaarli\Api;
3 3
4use Shaarli\Config\ConfigManager; 4use Shaarli\Config\ConfigManager;
5use Shaarli\History;
5use Slim\Container; 6use Slim\Container;
6use Slim\Http\Environment; 7use Slim\Http\Environment;
7use Slim\Http\Request; 8use Slim\Http\Request;
@@ -17,7 +18,7 @@ use Slim\Http\Response;
17 * 18 *
18 * @package Api 19 * @package Api
19 */ 20 */
20class ApiMiddlewareTest extends \PHPUnit\Framework\TestCase 21class ApiMiddlewareTest extends \Shaarli\TestCase
21{ 22{
22 /** 23 /**
23 * @var string datastore to test write operations 24 * @var string datastore to test write operations
@@ -25,7 +26,7 @@ class ApiMiddlewareTest extends \PHPUnit\Framework\TestCase
25 protected static $testDatastore = 'sandbox/datastore.php'; 26 protected static $testDatastore = 'sandbox/datastore.php';
26 27
27 /** 28 /**
28 * @var \ConfigManager instance 29 * @var ConfigManager instance
29 */ 30 */
30 protected $conf; 31 protected $conf;
31 32
@@ -40,29 +41,79 @@ class ApiMiddlewareTest extends \PHPUnit\Framework\TestCase
40 protected $container; 41 protected $container;
41 42
42 /** 43 /**
43 * Before every test, instantiate a new Api with its config, plugins and links. 44 * Before every test, instantiate a new Api with its config, plugins and bookmarks.
44 */ 45 */
45 public function setUp() 46 protected function setUp(): void
46 { 47 {
47 $this->conf = new ConfigManager('tests/utils/config/configJson.json.php'); 48 $this->conf = new ConfigManager('tests/utils/config/configJson');
48 $this->conf->set('api.secret', 'NapoleonWasALizard'); 49 $this->conf->set('api.secret', 'NapoleonWasALizard');
49 50
50 $this->refDB = new \ReferenceLinkDB(); 51 $this->refDB = new \ReferenceLinkDB();
51 $this->refDB->write(self::$testDatastore); 52 $this->refDB->write(self::$testDatastore);
52 53
54 $history = new History('sandbox/history.php');
55
53 $this->container = new Container(); 56 $this->container = new Container();
54 $this->container['conf'] = $this->conf; 57 $this->container['conf'] = $this->conf;
58 $this->container['history'] = $history;
55 } 59 }
56 60
57 /** 61 /**
58 * After every test, remove the test datastore. 62 * After every test, remove the test datastore.
59 */ 63 */
60 public function tearDown() 64 protected function tearDown(): void
61 { 65 {
62 @unlink(self::$testDatastore); 66 @unlink(self::$testDatastore);
63 } 67 }
64 68
65 /** 69 /**
70 * Invoke the middleware with a valid token
71 */
72 public function testInvokeMiddlewareWithValidToken(): void
73 {
74 $next = function (Request $request, Response $response): Response {
75 return $response;
76 };
77 $mw = new ApiMiddleware($this->container);
78 $env = Environment::mock([
79 'REQUEST_METHOD' => 'GET',
80 'REQUEST_URI' => '/echo',
81 'HTTP_AUTHORIZATION'=> 'Bearer ' . ApiUtilsTest::generateValidJwtToken('NapoleonWasALizard'),
82 ]);
83 $request = Request::createFromEnvironment($env);
84 $response = new Response();
85 /** @var Response $response */
86 $response = $mw($request, $response, $next);
87
88 $this->assertEquals(200, $response->getStatusCode());
89 }
90
91 /**
92 * Invoke the middleware with a valid token
93 * Using specific Apache CGI redirected authorization.
94 */
95 public function testInvokeMiddlewareWithValidTokenFromRedirectedHeader(): void
96 {
97 $next = function (Request $request, Response $response): Response {
98 return $response;
99 };
100
101 $token = 'Bearer ' . ApiUtilsTest::generateValidJwtToken('NapoleonWasALizard');
102 $this->container->environment['REDIRECT_HTTP_AUTHORIZATION'] = $token;
103 $mw = new ApiMiddleware($this->container);
104 $env = Environment::mock([
105 'REQUEST_METHOD' => 'GET',
106 'REQUEST_URI' => '/echo',
107 ]);
108 $request = Request::createFromEnvironment($env);
109 $response = new Response();
110 /** @var Response $response */
111 $response = $mw($request, $response, $next);
112
113 $this->assertEquals(200, $response->getStatusCode());
114 }
115
116 /**
66 * Invoke the middleware with the API disabled: 117 * Invoke the middleware with the API disabled:
67 * should return a 401 error Unauthorized. 118 * should return a 401 error Unauthorized.
68 */ 119 */
@@ -105,7 +156,7 @@ class ApiMiddlewareTest extends \PHPUnit\Framework\TestCase
105 $this->assertEquals(401, $response->getStatusCode()); 156 $this->assertEquals(401, $response->getStatusCode());
106 $body = json_decode((string) $response->getBody()); 157 $body = json_decode((string) $response->getBody());
107 $this->assertEquals('Not authorized: API is disabled', $body->message); 158 $this->assertEquals('Not authorized: API is disabled', $body->message);
108 $this->assertContains('ApiAuthorizationException', $body->stacktrace); 159 $this->assertContainsPolyfill('ApiAuthorizationException', $body->stacktrace);
109 } 160 }
110 161
111 /** 162 /**
@@ -128,7 +179,7 @@ class ApiMiddlewareTest extends \PHPUnit\Framework\TestCase
128 $this->assertEquals(401, $response->getStatusCode()); 179 $this->assertEquals(401, $response->getStatusCode());
129 $body = json_decode((string) $response->getBody()); 180 $body = json_decode((string) $response->getBody());
130 $this->assertEquals('Not authorized: JWT token not provided', $body->message); 181 $this->assertEquals('Not authorized: JWT token not provided', $body->message);
131 $this->assertContains('ApiAuthorizationException', $body->stacktrace); 182 $this->assertContainsPolyfill('ApiAuthorizationException', $body->stacktrace);
132 } 183 }
133 184
134 /** 185 /**
@@ -153,7 +204,7 @@ class ApiMiddlewareTest extends \PHPUnit\Framework\TestCase
153 $this->assertEquals(401, $response->getStatusCode()); 204 $this->assertEquals(401, $response->getStatusCode());
154 $body = json_decode((string) $response->getBody()); 205 $body = json_decode((string) $response->getBody());
155 $this->assertEquals('Not authorized: Token secret must be set in Shaarli\'s administration', $body->message); 206 $this->assertEquals('Not authorized: Token secret must be set in Shaarli\'s administration', $body->message);
156 $this->assertContains('ApiAuthorizationException', $body->stacktrace); 207 $this->assertContainsPolyfill('ApiAuthorizationException', $body->stacktrace);
157 } 208 }
158 209
159 /** 210 /**
@@ -176,7 +227,7 @@ class ApiMiddlewareTest extends \PHPUnit\Framework\TestCase
176 $this->assertEquals(401, $response->getStatusCode()); 227 $this->assertEquals(401, $response->getStatusCode());
177 $body = json_decode((string) $response->getBody()); 228 $body = json_decode((string) $response->getBody());
178 $this->assertEquals('Not authorized: Invalid JWT header', $body->message); 229 $this->assertEquals('Not authorized: Invalid JWT header', $body->message);
179 $this->assertContains('ApiAuthorizationException', $body->stacktrace); 230 $this->assertContainsPolyfill('ApiAuthorizationException', $body->stacktrace);
180 } 231 }
181 232
182 /** 233 /**
@@ -202,6 +253,6 @@ class ApiMiddlewareTest extends \PHPUnit\Framework\TestCase
202 $this->assertEquals(401, $response->getStatusCode()); 253 $this->assertEquals(401, $response->getStatusCode());
203 $body = json_decode((string) $response->getBody()); 254 $body = json_decode((string) $response->getBody());
204 $this->assertEquals('Not authorized: Malformed JWT token', $body->message); 255 $this->assertEquals('Not authorized: Malformed JWT token', $body->message);
205 $this->assertContains('ApiAuthorizationException', $body->stacktrace); 256 $this->assertContainsPolyfill('ApiAuthorizationException', $body->stacktrace);
206 } 257 }
207} 258}
diff --git a/tests/api/ApiUtilsTest.php b/tests/api/ApiUtilsTest.php
index ea0ae500..7a143859 100644
--- a/tests/api/ApiUtilsTest.php
+++ b/tests/api/ApiUtilsTest.php
@@ -2,17 +2,18 @@
2 2
3namespace Shaarli\Api; 3namespace Shaarli\Api;
4 4
5use Shaarli\Bookmark\Bookmark;
5use Shaarli\Http\Base64Url; 6use Shaarli\Http\Base64Url;
6 7
7/** 8/**
8 * Class ApiUtilsTest 9 * Class ApiUtilsTest
9 */ 10 */
10class ApiUtilsTest extends \PHPUnit\Framework\TestCase 11class ApiUtilsTest extends \Shaarli\TestCase
11{ 12{
12 /** 13 /**
13 * Force the timezone for ISO datetimes. 14 * Force the timezone for ISO datetimes.
14 */ 15 */
15 public static function setUpBeforeClass() 16 public static function setUpBeforeClass(): void
16 { 17 {
17 date_default_timezone_set('UTC'); 18 date_default_timezone_set('UTC');
18 } 19 }
@@ -60,148 +61,148 @@ class ApiUtilsTest extends \PHPUnit\Framework\TestCase
60 public function testValidateJwtTokenValid() 61 public function testValidateJwtTokenValid()
61 { 62 {
62 $secret = 'WarIsPeace'; 63 $secret = 'WarIsPeace';
63 ApiUtils::validateJwtToken(self::generateValidJwtToken($secret), $secret); 64 $this->assertTrue(ApiUtils::validateJwtToken(self::generateValidJwtToken($secret), $secret));
64 } 65 }
65 66
66 /** 67 /**
67 * Test validateJwtToken() with a malformed JWT token. 68 * Test validateJwtToken() with a malformed JWT token.
68 *
69 * @expectedException \Shaarli\Api\Exceptions\ApiAuthorizationException
70 * @expectedExceptionMessage Malformed JWT token
71 */ 69 */
72 public function testValidateJwtTokenMalformed() 70 public function testValidateJwtTokenMalformed()
73 { 71 {
72 $this->expectException(\Shaarli\Api\Exceptions\ApiAuthorizationException::class);
73 $this->expectExceptionMessage('Malformed JWT token');
74
74 $token = 'ABC.DEF'; 75 $token = 'ABC.DEF';
75 ApiUtils::validateJwtToken($token, 'foo'); 76 ApiUtils::validateJwtToken($token, 'foo');
76 } 77 }
77 78
78 /** 79 /**
79 * Test validateJwtToken() with an empty JWT token. 80 * Test validateJwtToken() with an empty JWT token.
80 *
81 * @expectedException \Shaarli\Api\Exceptions\ApiAuthorizationException
82 * @expectedExceptionMessage Malformed JWT token
83 */ 81 */
84 public function testValidateJwtTokenMalformedEmpty() 82 public function testValidateJwtTokenMalformedEmpty()
85 { 83 {
84 $this->expectException(\Shaarli\Api\Exceptions\ApiAuthorizationException::class);
85 $this->expectExceptionMessage('Malformed JWT token');
86
86 $token = false; 87 $token = false;
87 ApiUtils::validateJwtToken($token, 'foo'); 88 ApiUtils::validateJwtToken($token, 'foo');
88 } 89 }
89 90
90 /** 91 /**
91 * Test validateJwtToken() with a JWT token without header. 92 * Test validateJwtToken() with a JWT token without header.
92 *
93 * @expectedException \Shaarli\Api\Exceptions\ApiAuthorizationException
94 * @expectedExceptionMessage Malformed JWT token
95 */ 93 */
96 public function testValidateJwtTokenMalformedEmptyHeader() 94 public function testValidateJwtTokenMalformedEmptyHeader()
97 { 95 {
96 $this->expectException(\Shaarli\Api\Exceptions\ApiAuthorizationException::class);
97 $this->expectExceptionMessage('Malformed JWT token');
98
98 $token = '.payload.signature'; 99 $token = '.payload.signature';
99 ApiUtils::validateJwtToken($token, 'foo'); 100 ApiUtils::validateJwtToken($token, 'foo');
100 } 101 }
101 102
102 /** 103 /**
103 * Test validateJwtToken() with a JWT token without payload 104 * Test validateJwtToken() with a JWT token without payload
104 *
105 * @expectedException \Shaarli\Api\Exceptions\ApiAuthorizationException
106 * @expectedExceptionMessage Malformed JWT token
107 */ 105 */
108 public function testValidateJwtTokenMalformedEmptyPayload() 106 public function testValidateJwtTokenMalformedEmptyPayload()
109 { 107 {
108 $this->expectException(\Shaarli\Api\Exceptions\ApiAuthorizationException::class);
109 $this->expectExceptionMessage('Malformed JWT token');
110
110 $token = 'header..signature'; 111 $token = 'header..signature';
111 ApiUtils::validateJwtToken($token, 'foo'); 112 ApiUtils::validateJwtToken($token, 'foo');
112 } 113 }
113 114
114 /** 115 /**
115 * Test validateJwtToken() with a JWT token with an empty signature. 116 * Test validateJwtToken() with a JWT token with an empty signature.
116 *
117 * @expectedException \Shaarli\Api\Exceptions\ApiAuthorizationException
118 * @expectedExceptionMessage Invalid JWT signature
119 */ 117 */
120 public function testValidateJwtTokenInvalidSignatureEmpty() 118 public function testValidateJwtTokenInvalidSignatureEmpty()
121 { 119 {
120 $this->expectException(\Shaarli\Api\Exceptions\ApiAuthorizationException::class);
121 $this->expectExceptionMessage('Invalid JWT signature');
122
122 $token = 'header.payload.'; 123 $token = 'header.payload.';
123 ApiUtils::validateJwtToken($token, 'foo'); 124 ApiUtils::validateJwtToken($token, 'foo');
124 } 125 }
125 126
126 /** 127 /**
127 * Test validateJwtToken() with a JWT token with an invalid signature. 128 * Test validateJwtToken() with a JWT token with an invalid signature.
128 *
129 * @expectedException \Shaarli\Api\Exceptions\ApiAuthorizationException
130 * @expectedExceptionMessage Invalid JWT signature
131 */ 129 */
132 public function testValidateJwtTokenInvalidSignature() 130 public function testValidateJwtTokenInvalidSignature()
133 { 131 {
132 $this->expectException(\Shaarli\Api\Exceptions\ApiAuthorizationException::class);
133 $this->expectExceptionMessage('Invalid JWT signature');
134
134 $token = 'header.payload.nope'; 135 $token = 'header.payload.nope';
135 ApiUtils::validateJwtToken($token, 'foo'); 136 ApiUtils::validateJwtToken($token, 'foo');
136 } 137 }
137 138
138 /** 139 /**
139 * Test validateJwtToken() with a JWT token with a signature generated with the wrong API secret. 140 * Test validateJwtToken() with a JWT token with a signature generated with the wrong API secret.
140 *
141 * @expectedException \Shaarli\Api\Exceptions\ApiAuthorizationException
142 * @expectedExceptionMessage Invalid JWT signature
143 */ 141 */
144 public function testValidateJwtTokenInvalidSignatureSecret() 142 public function testValidateJwtTokenInvalidSignatureSecret()
145 { 143 {
144 $this->expectException(\Shaarli\Api\Exceptions\ApiAuthorizationException::class);
145 $this->expectExceptionMessage('Invalid JWT signature');
146
146 ApiUtils::validateJwtToken(self::generateValidJwtToken('foo'), 'bar'); 147 ApiUtils::validateJwtToken(self::generateValidJwtToken('foo'), 'bar');
147 } 148 }
148 149
149 /** 150 /**
150 * Test validateJwtToken() with a JWT token with a an invalid header (not JSON). 151 * Test validateJwtToken() with a JWT token with a an invalid header (not JSON).
151 *
152 * @expectedException \Shaarli\Api\Exceptions\ApiAuthorizationException
153 * @expectedExceptionMessage Invalid JWT header
154 */ 152 */
155 public function testValidateJwtTokenInvalidHeader() 153 public function testValidateJwtTokenInvalidHeader()
156 { 154 {
155 $this->expectException(\Shaarli\Api\Exceptions\ApiAuthorizationException::class);
156 $this->expectExceptionMessage('Invalid JWT header');
157
157 $token = $this->generateCustomJwtToken('notJSON', '{"JSON":1}', 'secret'); 158 $token = $this->generateCustomJwtToken('notJSON', '{"JSON":1}', 'secret');
158 ApiUtils::validateJwtToken($token, 'secret'); 159 ApiUtils::validateJwtToken($token, 'secret');
159 } 160 }
160 161
161 /** 162 /**
162 * Test validateJwtToken() with a JWT token with a an invalid payload (not JSON). 163 * Test validateJwtToken() with a JWT token with a an invalid payload (not JSON).
163 *
164 * @expectedException \Shaarli\Api\Exceptions\ApiAuthorizationException
165 * @expectedExceptionMessage Invalid JWT payload
166 */ 164 */
167 public function testValidateJwtTokenInvalidPayload() 165 public function testValidateJwtTokenInvalidPayload()
168 { 166 {
167 $this->expectException(\Shaarli\Api\Exceptions\ApiAuthorizationException::class);
168 $this->expectExceptionMessage('Invalid JWT payload');
169
169 $token = $this->generateCustomJwtToken('{"JSON":1}', 'notJSON', 'secret'); 170 $token = $this->generateCustomJwtToken('{"JSON":1}', 'notJSON', 'secret');
170 ApiUtils::validateJwtToken($token, 'secret'); 171 ApiUtils::validateJwtToken($token, 'secret');
171 } 172 }
172 173
173 /** 174 /**
174 * Test validateJwtToken() with a JWT token without issued time. 175 * Test validateJwtToken() with a JWT token without issued time.
175 *
176 * @expectedException \Shaarli\Api\Exceptions\ApiAuthorizationException
177 * @expectedExceptionMessage Invalid JWT issued time
178 */ 176 */
179 public function testValidateJwtTokenInvalidTimeEmpty() 177 public function testValidateJwtTokenInvalidTimeEmpty()
180 { 178 {
179 $this->expectException(\Shaarli\Api\Exceptions\ApiAuthorizationException::class);
180 $this->expectExceptionMessage('Invalid JWT issued time');
181
181 $token = $this->generateCustomJwtToken('{"JSON":1}', '{"JSON":1}', 'secret'); 182 $token = $this->generateCustomJwtToken('{"JSON":1}', '{"JSON":1}', 'secret');
182 ApiUtils::validateJwtToken($token, 'secret'); 183 ApiUtils::validateJwtToken($token, 'secret');
183 } 184 }
184 185
185 /** 186 /**
186 * Test validateJwtToken() with an expired JWT token. 187 * Test validateJwtToken() with an expired JWT token.
187 *
188 * @expectedException \Shaarli\Api\Exceptions\ApiAuthorizationException
189 * @expectedExceptionMessage Invalid JWT issued time
190 */ 188 */
191 public function testValidateJwtTokenInvalidTimeExpired() 189 public function testValidateJwtTokenInvalidTimeExpired()
192 { 190 {
191 $this->expectException(\Shaarli\Api\Exceptions\ApiAuthorizationException::class);
192 $this->expectExceptionMessage('Invalid JWT issued time');
193
193 $token = $this->generateCustomJwtToken('{"JSON":1}', '{"iat":' . (time() - 600) . '}', 'secret'); 194 $token = $this->generateCustomJwtToken('{"JSON":1}', '{"iat":' . (time() - 600) . '}', 'secret');
194 ApiUtils::validateJwtToken($token, 'secret'); 195 ApiUtils::validateJwtToken($token, 'secret');
195 } 196 }
196 197
197 /** 198 /**
198 * Test validateJwtToken() with a JWT token issued in the future. 199 * Test validateJwtToken() with a JWT token issued in the future.
199 *
200 * @expectedException \Shaarli\Api\Exceptions\ApiAuthorizationException
201 * @expectedExceptionMessage Invalid JWT issued time
202 */ 200 */
203 public function testValidateJwtTokenInvalidTimeFuture() 201 public function testValidateJwtTokenInvalidTimeFuture()
204 { 202 {
203 $this->expectException(\Shaarli\Api\Exceptions\ApiAuthorizationException::class);
204 $this->expectExceptionMessage('Invalid JWT issued time');
205
205 $token = $this->generateCustomJwtToken('{"JSON":1}', '{"iat":' . (time() + 60) . '}', 'secret'); 206 $token = $this->generateCustomJwtToken('{"JSON":1}', '{"iat":' . (time() + 60) . '}', 'secret');
206 ApiUtils::validateJwtToken($token, 'secret'); 207 ApiUtils::validateJwtToken($token, 'secret');
207 } 208 }
@@ -212,7 +213,7 @@ class ApiUtilsTest extends \PHPUnit\Framework\TestCase
212 public function testFormatLinkComplete() 213 public function testFormatLinkComplete()
213 { 214 {
214 $indexUrl = 'https://domain.tld/sub/'; 215 $indexUrl = 'https://domain.tld/sub/';
215 $link = [ 216 $data = [
216 'id' => 12, 217 'id' => 12,
217 'url' => 'http://lol.lol', 218 'url' => 'http://lol.lol',
218 'shorturl' => 'abc', 219 'shorturl' => 'abc',
@@ -223,6 +224,8 @@ class ApiUtilsTest extends \PHPUnit\Framework\TestCase
223 'created' => \DateTime::createFromFormat('Ymd_His', '20170107_160102'), 224 'created' => \DateTime::createFromFormat('Ymd_His', '20170107_160102'),
224 'updated' => \DateTime::createFromFormat('Ymd_His', '20170107_160612'), 225 'updated' => \DateTime::createFromFormat('Ymd_His', '20170107_160612'),
225 ]; 226 ];
227 $bookmark = new Bookmark();
228 $bookmark->fromArray($data);
226 229
227 $expected = [ 230 $expected = [
228 'id' => 12, 231 'id' => 12,
@@ -236,7 +239,7 @@ class ApiUtilsTest extends \PHPUnit\Framework\TestCase
236 'updated' => '2017-01-07T16:06:12+00:00', 239 'updated' => '2017-01-07T16:06:12+00:00',
237 ]; 240 ];
238 241
239 $this->assertEquals($expected, ApiUtils::formatLink($link, $indexUrl)); 242 $this->assertEquals($expected, ApiUtils::formatLink($bookmark, $indexUrl));
240 } 243 }
241 244
242 /** 245 /**
@@ -245,7 +248,7 @@ class ApiUtilsTest extends \PHPUnit\Framework\TestCase
245 public function testFormatLinkMinimalNote() 248 public function testFormatLinkMinimalNote()
246 { 249 {
247 $indexUrl = 'https://domain.tld/sub/'; 250 $indexUrl = 'https://domain.tld/sub/';
248 $link = [ 251 $data = [
249 'id' => 12, 252 'id' => 12,
250 'url' => '?abc', 253 'url' => '?abc',
251 'shorturl' => 'abc', 254 'shorturl' => 'abc',
@@ -255,6 +258,8 @@ class ApiUtilsTest extends \PHPUnit\Framework\TestCase
255 'private' => '', 258 'private' => '',
256 'created' => \DateTime::createFromFormat('Ymd_His', '20170107_160102'), 259 'created' => \DateTime::createFromFormat('Ymd_His', '20170107_160102'),
257 ]; 260 ];
261 $bookmark = new Bookmark();
262 $bookmark->fromArray($data);
258 263
259 $expected = [ 264 $expected = [
260 'id' => 12, 265 'id' => 12,
@@ -268,7 +273,7 @@ class ApiUtilsTest extends \PHPUnit\Framework\TestCase
268 'updated' => '', 273 'updated' => '',
269 ]; 274 ];
270 275
271 $this->assertEquals($expected, ApiUtils::formatLink($link, $indexUrl)); 276 $this->assertEquals($expected, ApiUtils::formatLink($bookmark, $indexUrl));
272 } 277 }
273 278
274 /** 279 /**
@@ -277,7 +282,7 @@ class ApiUtilsTest extends \PHPUnit\Framework\TestCase
277 public function testUpdateLink() 282 public function testUpdateLink()
278 { 283 {
279 $created = \DateTime::createFromFormat('Ymd_His', '20170107_160102'); 284 $created = \DateTime::createFromFormat('Ymd_His', '20170107_160102');
280 $old = [ 285 $data = [
281 'id' => 12, 286 'id' => 12,
282 'url' => '?abc', 287 'url' => '?abc',
283 'shorturl' => 'abc', 288 'shorturl' => 'abc',
@@ -287,8 +292,10 @@ class ApiUtilsTest extends \PHPUnit\Framework\TestCase
287 'private' => '', 292 'private' => '',
288 'created' => $created, 293 'created' => $created,
289 ]; 294 ];
295 $old = new Bookmark();
296 $old->fromArray($data);
290 297
291 $new = [ 298 $data = [
292 'id' => 13, 299 'id' => 13,
293 'shorturl' => 'nope', 300 'shorturl' => 'nope',
294 'url' => 'http://somewhere.else', 301 'url' => 'http://somewhere.else',
@@ -299,17 +306,18 @@ class ApiUtilsTest extends \PHPUnit\Framework\TestCase
299 'created' => 'creation', 306 'created' => 'creation',
300 'updated' => 'updation', 307 'updated' => 'updation',
301 ]; 308 ];
309 $new = new Bookmark();
310 $new->fromArray($data);
302 311
303 $result = ApiUtils::updateLink($old, $new); 312 $result = ApiUtils::updateLink($old, $new);
304 $this->assertEquals(12, $result['id']); 313 $this->assertEquals(12, $result->getId());
305 $this->assertEquals('http://somewhere.else', $result['url']); 314 $this->assertEquals('http://somewhere.else', $result->getUrl());
306 $this->assertEquals('abc', $result['shorturl']); 315 $this->assertEquals('abc', $result->getShortUrl());
307 $this->assertEquals('Le Cid', $result['title']); 316 $this->assertEquals('Le Cid', $result->getTitle());
308 $this->assertEquals('Percé jusques au fond du cœur [...]', $result['description']); 317 $this->assertEquals('Percé jusques au fond du cœur [...]', $result->getDescription());
309 $this->assertEquals('corneille rodrigue', $result['tags']); 318 $this->assertEquals('corneille rodrigue', $result->getTagsString());
310 $this->assertEquals(true, $result['private']); 319 $this->assertEquals(true, $result->isPrivate());
311 $this->assertEquals($created, $result['created']); 320 $this->assertEquals($created, $result->getCreated());
312 $this->assertTrue(new \DateTime('5 seconds ago') < $result['updated']);
313 } 321 }
314 322
315 /** 323 /**
@@ -318,7 +326,7 @@ class ApiUtilsTest extends \PHPUnit\Framework\TestCase
318 public function testUpdateLinkMinimal() 326 public function testUpdateLinkMinimal()
319 { 327 {
320 $created = \DateTime::createFromFormat('Ymd_His', '20170107_160102'); 328 $created = \DateTime::createFromFormat('Ymd_His', '20170107_160102');
321 $old = [ 329 $data = [
322 'id' => 12, 330 'id' => 12,
323 'url' => '?abc', 331 'url' => '?abc',
324 'shorturl' => 'abc', 332 'shorturl' => 'abc',
@@ -328,24 +336,19 @@ class ApiUtilsTest extends \PHPUnit\Framework\TestCase
328 'private' => true, 336 'private' => true,
329 'created' => $created, 337 'created' => $created,
330 ]; 338 ];
339 $old = new Bookmark();
340 $old->fromArray($data);
331 341
332 $new = [ 342 $new = new Bookmark();
333 'url' => '',
334 'title' => '',
335 'description' => '',
336 'tags' => '',
337 'private' => false,
338 ];
339 343
340 $result = ApiUtils::updateLink($old, $new); 344 $result = ApiUtils::updateLink($old, $new);
341 $this->assertEquals(12, $result['id']); 345 $this->assertEquals(12, $result->getId());
342 $this->assertEquals('?abc', $result['url']); 346 $this->assertEquals('', $result->getUrl());
343 $this->assertEquals('abc', $result['shorturl']); 347 $this->assertEquals('abc', $result->getShortUrl());
344 $this->assertEquals('?abc', $result['title']); 348 $this->assertEquals('', $result->getTitle());
345 $this->assertEquals('', $result['description']); 349 $this->assertEquals('', $result->getDescription());
346 $this->assertEquals('', $result['tags']); 350 $this->assertEquals('', $result->getTagsString());
347 $this->assertEquals(false, $result['private']); 351 $this->assertEquals(false, $result->isPrivate());
348 $this->assertEquals($created, $result['created']); 352 $this->assertEquals($created, $result->getCreated());
349 $this->assertTrue(new \DateTime('5 seconds ago') < $result['updated']);
350 } 353 }
351} 354}
diff --git a/tests/api/controllers/history/HistoryTest.php b/tests/api/controllers/history/HistoryTest.php
index e287f239..84f8716e 100644
--- a/tests/api/controllers/history/HistoryTest.php
+++ b/tests/api/controllers/history/HistoryTest.php
@@ -11,7 +11,7 @@ use Slim\Http\Response;
11 11
12require_once 'tests/utils/ReferenceHistory.php'; 12require_once 'tests/utils/ReferenceHistory.php';
13 13
14class HistoryTest extends \PHPUnit\Framework\TestCase 14class HistoryTest extends \Shaarli\TestCase
15{ 15{
16 /** 16 /**
17 * @var string datastore to test write operations 17 * @var string datastore to test write operations
@@ -39,11 +39,11 @@ class HistoryTest extends \PHPUnit\Framework\TestCase
39 protected $controller; 39 protected $controller;
40 40
41 /** 41 /**
42 * Before every test, instantiate a new Api with its config, plugins and links. 42 * Before every test, instantiate a new Api with its config, plugins and bookmarks.
43 */ 43 */
44 public function setUp() 44 protected function setUp(): void
45 { 45 {
46 $this->conf = new ConfigManager('tests/utils/config/configJson.json.php'); 46 $this->conf = new ConfigManager('tests/utils/config/configJson');
47 $this->refHistory = new \ReferenceHistory(); 47 $this->refHistory = new \ReferenceHistory();
48 $this->refHistory->write(self::$testHistory); 48 $this->refHistory->write(self::$testHistory);
49 $this->container = new Container(); 49 $this->container = new Container();
@@ -57,7 +57,7 @@ class HistoryTest extends \PHPUnit\Framework\TestCase
57 /** 57 /**
58 * After every test, remove the test datastore. 58 * After every test, remove the test datastore.
59 */ 59 */
60 public function tearDown() 60 protected function tearDown(): void
61 { 61 {
62 @unlink(self::$testHistory); 62 @unlink(self::$testHistory);
63 } 63 }
diff --git a/tests/api/controllers/info/InfoTest.php b/tests/api/controllers/info/InfoTest.php
index e70d371b..1598e1e8 100644
--- a/tests/api/controllers/info/InfoTest.php
+++ b/tests/api/controllers/info/InfoTest.php
@@ -1,7 +1,10 @@
1<?php 1<?php
2namespace Shaarli\Api\Controllers; 2namespace Shaarli\Api\Controllers;
3 3
4use Shaarli\Bookmark\BookmarkFileService;
4use Shaarli\Config\ConfigManager; 5use Shaarli\Config\ConfigManager;
6use Shaarli\History;
7use Shaarli\TestCase;
5use Slim\Container; 8use Slim\Container;
6use Slim\Http\Environment; 9use Slim\Http\Environment;
7use Slim\Http\Request; 10use Slim\Http\Request;
@@ -14,7 +17,7 @@ use Slim\Http\Response;
14 * 17 *
15 * @package Api\Controllers 18 * @package Api\Controllers
16 */ 19 */
17class InfoTest extends \PHPUnit\Framework\TestCase 20class InfoTest extends TestCase
18{ 21{
19 /** 22 /**
20 * @var string datastore to test write operations 23 * @var string datastore to test write operations
@@ -42,17 +45,20 @@ class InfoTest extends \PHPUnit\Framework\TestCase
42 protected $controller; 45 protected $controller;
43 46
44 /** 47 /**
45 * Before every test, instantiate a new Api with its config, plugins and links. 48 * Before every test, instantiate a new Api with its config, plugins and bookmarks.
46 */ 49 */
47 public function setUp() 50 protected function setUp(): void
48 { 51 {
49 $this->conf = new ConfigManager('tests/utils/config/configJson.json.php'); 52 $this->conf = new ConfigManager('tests/utils/config/configJson');
53 $this->conf->set('resource.datastore', self::$testDatastore);
50 $this->refDB = new \ReferenceLinkDB(); 54 $this->refDB = new \ReferenceLinkDB();
51 $this->refDB->write(self::$testDatastore); 55 $this->refDB->write(self::$testDatastore);
52 56
57 $history = new History('sandbox/history.php');
58
53 $this->container = new Container(); 59 $this->container = new Container();
54 $this->container['conf'] = $this->conf; 60 $this->container['conf'] = $this->conf;
55 $this->container['db'] = new \Shaarli\Bookmark\LinkDB(self::$testDatastore, true, false); 61 $this->container['db'] = new BookmarkFileService($this->conf, $history, true);
56 $this->container['history'] = null; 62 $this->container['history'] = null;
57 63
58 $this->controller = new Info($this->container); 64 $this->controller = new Info($this->container);
@@ -61,7 +67,7 @@ class InfoTest extends \PHPUnit\Framework\TestCase
61 /** 67 /**
62 * After every test, remove the test datastore. 68 * After every test, remove the test datastore.
63 */ 69 */
64 public function tearDown() 70 protected function tearDown(): void
65 { 71 {
66 @unlink(self::$testDatastore); 72 @unlink(self::$testDatastore);
67 } 73 }
@@ -84,11 +90,11 @@ class InfoTest extends \PHPUnit\Framework\TestCase
84 $this->assertEquals(2, $data['private_counter']); 90 $this->assertEquals(2, $data['private_counter']);
85 $this->assertEquals('Shaarli', $data['settings']['title']); 91 $this->assertEquals('Shaarli', $data['settings']['title']);
86 $this->assertEquals('?', $data['settings']['header_link']); 92 $this->assertEquals('?', $data['settings']['header_link']);
87 $this->assertEquals('UTC', $data['settings']['timezone']); 93 $this->assertEquals('Europe/Paris', $data['settings']['timezone']);
88 $this->assertEquals(ConfigManager::$DEFAULT_PLUGINS, $data['settings']['enabled_plugins']); 94 $this->assertEquals(ConfigManager::$DEFAULT_PLUGINS, $data['settings']['enabled_plugins']);
89 $this->assertEquals(false, $data['settings']['default_private_links']); 95 $this->assertEquals(true, $data['settings']['default_private_links']);
90 96
91 $title = 'My links'; 97 $title = 'My bookmarks';
92 $headerLink = 'http://shaarli.tld'; 98 $headerLink = 'http://shaarli.tld';
93 $timezone = 'Europe/Paris'; 99 $timezone = 'Europe/Paris';
94 $enabledPlugins = array('foo', 'bar'); 100 $enabledPlugins = array('foo', 'bar');
diff --git a/tests/api/controllers/links/DeleteLinkTest.php b/tests/api/controllers/links/DeleteLinkTest.php
index 90193e28..cf9464f0 100644
--- a/tests/api/controllers/links/DeleteLinkTest.php
+++ b/tests/api/controllers/links/DeleteLinkTest.php
@@ -3,7 +3,7 @@
3 3
4namespace Shaarli\Api\Controllers; 4namespace Shaarli\Api\Controllers;
5 5
6use Shaarli\Bookmark\LinkDB; 6use Shaarli\Bookmark\BookmarkFileService;
7use Shaarli\Config\ConfigManager; 7use Shaarli\Config\ConfigManager;
8use Shaarli\History; 8use Shaarli\History;
9use Slim\Container; 9use Slim\Container;
@@ -11,7 +11,7 @@ use Slim\Http\Environment;
11use Slim\Http\Request; 11use Slim\Http\Request;
12use Slim\Http\Response; 12use Slim\Http\Response;
13 13
14class DeleteLinkTest extends \PHPUnit\Framework\TestCase 14class DeleteLinkTest extends \Shaarli\TestCase
15{ 15{
16 /** 16 /**
17 * @var string datastore to test write operations 17 * @var string datastore to test write operations
@@ -34,9 +34,9 @@ class DeleteLinkTest extends \PHPUnit\Framework\TestCase
34 protected $refDB = null; 34 protected $refDB = null;
35 35
36 /** 36 /**
37 * @var LinkDB instance. 37 * @var BookmarkFileService instance.
38 */ 38 */
39 protected $linkDB; 39 protected $bookmarkService;
40 40
41 /** 41 /**
42 * @var HistoryController instance. 42 * @var HistoryController instance.
@@ -54,20 +54,22 @@ class DeleteLinkTest extends \PHPUnit\Framework\TestCase
54 protected $controller; 54 protected $controller;
55 55
56 /** 56 /**
57 * Before each test, instantiate a new Api with its config, plugins and links. 57 * Before each test, instantiate a new Api with its config, plugins and bookmarks.
58 */ 58 */
59 public function setUp() 59 protected function setUp(): void
60 { 60 {
61 $this->conf = new ConfigManager('tests/utils/config/configJson'); 61 $this->conf = new ConfigManager('tests/utils/config/configJson');
62 $this->conf->set('resource.datastore', self::$testDatastore);
62 $this->refDB = new \ReferenceLinkDB(); 63 $this->refDB = new \ReferenceLinkDB();
63 $this->refDB->write(self::$testDatastore); 64 $this->refDB->write(self::$testDatastore);
64 $this->linkDB = new LinkDB(self::$testDatastore, true, false);
65 $refHistory = new \ReferenceHistory(); 65 $refHistory = new \ReferenceHistory();
66 $refHistory->write(self::$testHistory); 66 $refHistory->write(self::$testHistory);
67 $this->history = new History(self::$testHistory); 67 $this->history = new History(self::$testHistory);
68 $this->bookmarkService = new BookmarkFileService($this->conf, $this->history, true);
69
68 $this->container = new Container(); 70 $this->container = new Container();
69 $this->container['conf'] = $this->conf; 71 $this->container['conf'] = $this->conf;
70 $this->container['db'] = $this->linkDB; 72 $this->container['db'] = $this->bookmarkService;
71 $this->container['history'] = $this->history; 73 $this->container['history'] = $this->history;
72 74
73 $this->controller = new Links($this->container); 75 $this->controller = new Links($this->container);
@@ -76,7 +78,7 @@ class DeleteLinkTest extends \PHPUnit\Framework\TestCase
76 /** 78 /**
77 * After each test, remove the test datastore. 79 * After each test, remove the test datastore.
78 */ 80 */
79 public function tearDown() 81 protected function tearDown(): void
80 { 82 {
81 @unlink(self::$testDatastore); 83 @unlink(self::$testDatastore);
82 @unlink(self::$testHistory); 84 @unlink(self::$testHistory);
@@ -88,7 +90,7 @@ class DeleteLinkTest extends \PHPUnit\Framework\TestCase
88 public function testDeleteLinkValid() 90 public function testDeleteLinkValid()
89 { 91 {
90 $id = '41'; 92 $id = '41';
91 $this->assertTrue(isset($this->linkDB[$id])); 93 $this->assertTrue($this->bookmarkService->exists($id));
92 $env = Environment::mock([ 94 $env = Environment::mock([
93 'REQUEST_METHOD' => 'DELETE', 95 'REQUEST_METHOD' => 'DELETE',
94 ]); 96 ]);
@@ -98,8 +100,8 @@ class DeleteLinkTest extends \PHPUnit\Framework\TestCase
98 $this->assertEquals(204, $response->getStatusCode()); 100 $this->assertEquals(204, $response->getStatusCode());
99 $this->assertEmpty((string) $response->getBody()); 101 $this->assertEmpty((string) $response->getBody());
100 102
101 $this->linkDB = new LinkDB(self::$testDatastore, true, false); 103 $this->bookmarkService = new BookmarkFileService($this->conf, $this->history, true);
102 $this->assertFalse(isset($this->linkDB[$id])); 104 $this->assertFalse($this->bookmarkService->exists($id));
103 105
104 $historyEntry = $this->history->getHistory()[0]; 106 $historyEntry = $this->history->getHistory()[0];
105 $this->assertEquals(History::DELETED, $historyEntry['event']); 107 $this->assertEquals(History::DELETED, $historyEntry['event']);
@@ -111,13 +113,13 @@ class DeleteLinkTest extends \PHPUnit\Framework\TestCase
111 113
112 /** 114 /**
113 * Test DELETE link endpoint: reach not existing ID. 115 * Test DELETE link endpoint: reach not existing ID.
114 *
115 * @expectedException \Shaarli\Api\Exceptions\ApiLinkNotFoundException
116 */ 116 */
117 public function testDeleteLink404() 117 public function testDeleteLink404()
118 { 118 {
119 $this->expectException(\Shaarli\Api\Exceptions\ApiLinkNotFoundException::class);
120
119 $id = -1; 121 $id = -1;
120 $this->assertFalse(isset($this->linkDB[$id])); 122 $this->assertFalse($this->bookmarkService->exists($id));
121 $env = Environment::mock([ 123 $env = Environment::mock([
122 'REQUEST_METHOD' => 'DELETE', 124 'REQUEST_METHOD' => 'DELETE',
123 ]); 125 ]);
diff --git a/tests/api/controllers/links/GetLinkIdTest.php b/tests/api/controllers/links/GetLinkIdTest.php
index cb9b7f6a..99dc606f 100644
--- a/tests/api/controllers/links/GetLinkIdTest.php
+++ b/tests/api/controllers/links/GetLinkIdTest.php
@@ -2,7 +2,10 @@
2 2
3namespace Shaarli\Api\Controllers; 3namespace Shaarli\Api\Controllers;
4 4
5use Shaarli\Bookmark\Bookmark;
6use Shaarli\Bookmark\BookmarkFileService;
5use Shaarli\Config\ConfigManager; 7use Shaarli\Config\ConfigManager;
8use Shaarli\History;
6use Slim\Container; 9use Slim\Container;
7use Slim\Http\Environment; 10use Slim\Http\Environment;
8use Slim\Http\Request; 11use Slim\Http\Request;
@@ -17,7 +20,7 @@ use Slim\Http\Response;
17 * 20 *
18 * @package Shaarli\Api\Controllers 21 * @package Shaarli\Api\Controllers
19 */ 22 */
20class GetLinkIdTest extends \PHPUnit\Framework\TestCase 23class GetLinkIdTest extends \Shaarli\TestCase
21{ 24{
22 /** 25 /**
23 * @var string datastore to test write operations 26 * @var string datastore to test write operations
@@ -50,17 +53,19 @@ class GetLinkIdTest extends \PHPUnit\Framework\TestCase
50 const NB_FIELDS_LINK = 9; 53 const NB_FIELDS_LINK = 9;
51 54
52 /** 55 /**
53 * Before each test, instantiate a new Api with its config, plugins and links. 56 * Before each test, instantiate a new Api with its config, plugins and bookmarks.
54 */ 57 */
55 public function setUp() 58 protected function setUp(): void
56 { 59 {
57 $this->conf = new ConfigManager('tests/utils/config/configJson'); 60 $this->conf = new ConfigManager('tests/utils/config/configJson');
61 $this->conf->set('resource.datastore', self::$testDatastore);
58 $this->refDB = new \ReferenceLinkDB(); 62 $this->refDB = new \ReferenceLinkDB();
59 $this->refDB->write(self::$testDatastore); 63 $this->refDB->write(self::$testDatastore);
64 $history = new History('sandbox/history.php');
60 65
61 $this->container = new Container(); 66 $this->container = new Container();
62 $this->container['conf'] = $this->conf; 67 $this->container['conf'] = $this->conf;
63 $this->container['db'] = new \Shaarli\Bookmark\LinkDB(self::$testDatastore, true, false); 68 $this->container['db'] = new BookmarkFileService($this->conf, $history, true);
64 $this->container['history'] = null; 69 $this->container['history'] = null;
65 70
66 $this->controller = new Links($this->container); 71 $this->controller = new Links($this->container);
@@ -69,7 +74,7 @@ class GetLinkIdTest extends \PHPUnit\Framework\TestCase
69 /** 74 /**
70 * After each test, remove the test datastore. 75 * After each test, remove the test datastore.
71 */ 76 */
72 public function tearDown() 77 protected function tearDown(): void
73 { 78 {
74 @unlink(self::$testDatastore); 79 @unlink(self::$testDatastore);
75 } 80 }
@@ -97,7 +102,7 @@ class GetLinkIdTest extends \PHPUnit\Framework\TestCase
97 $this->assertEquals($id, $data['id']); 102 $this->assertEquals($id, $data['id']);
98 103
99 // Check link elements 104 // Check link elements
100 $this->assertEquals('http://domain.tld/?WDWyig', $data['url']); 105 $this->assertEquals('http://domain.tld/shaare/WDWyig', $data['url']);
101 $this->assertEquals('WDWyig', $data['shorturl']); 106 $this->assertEquals('WDWyig', $data['shorturl']);
102 $this->assertEquals('Link title: @website', $data['title']); 107 $this->assertEquals('Link title: @website', $data['title']);
103 $this->assertEquals( 108 $this->assertEquals(
@@ -107,7 +112,7 @@ class GetLinkIdTest extends \PHPUnit\Framework\TestCase
107 $this->assertEquals('sTuff', $data['tags'][0]); 112 $this->assertEquals('sTuff', $data['tags'][0]);
108 $this->assertEquals(false, $data['private']); 113 $this->assertEquals(false, $data['private']);
109 $this->assertEquals( 114 $this->assertEquals(
110 \DateTime::createFromFormat(\Shaarli\Bookmark\LinkDB::LINK_DATE_FORMAT, '20150310_114651')->format(\DateTime::ATOM), 115 \DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20150310_114651')->format(\DateTime::ATOM),
111 $data['created'] 116 $data['created']
112 ); 117 );
113 $this->assertEmpty($data['updated']); 118 $this->assertEmpty($data['updated']);
@@ -115,12 +120,12 @@ class GetLinkIdTest extends \PHPUnit\Framework\TestCase
115 120
116 /** 121 /**
117 * Test basic getLink service: get non existent link => ApiLinkNotFoundException. 122 * Test basic getLink service: get non existent link => ApiLinkNotFoundException.
118 *
119 * @expectedException Shaarli\Api\Exceptions\ApiLinkNotFoundException
120 * @expectedExceptionMessage Link not found
121 */ 123 */
122 public function testGetLink404() 124 public function testGetLink404()
123 { 125 {
126 $this->expectException(\Shaarli\Api\Exceptions\ApiLinkNotFoundException::class);
127 $this->expectExceptionMessage('Link not found');
128
124 $env = Environment::mock([ 129 $env = Environment::mock([
125 'REQUEST_METHOD' => 'GET', 130 'REQUEST_METHOD' => 'GET',
126 ]); 131 ]);
diff --git a/tests/api/controllers/links/GetLinksTest.php b/tests/api/controllers/links/GetLinksTest.php
index 711a3152..ca1bfc63 100644
--- a/tests/api/controllers/links/GetLinksTest.php
+++ b/tests/api/controllers/links/GetLinksTest.php
@@ -1,8 +1,11 @@
1<?php 1<?php
2namespace Shaarli\Api\Controllers; 2namespace Shaarli\Api\Controllers;
3 3
4use Shaarli\Bookmark\Bookmark;
5use Shaarli\Bookmark\BookmarkFileService;
4use Shaarli\Bookmark\LinkDB; 6use Shaarli\Bookmark\LinkDB;
5use Shaarli\Config\ConfigManager; 7use Shaarli\Config\ConfigManager;
8use Shaarli\History;
6use Slim\Container; 9use Slim\Container;
7use Slim\Http\Environment; 10use Slim\Http\Environment;
8use Slim\Http\Request; 11use Slim\Http\Request;
@@ -17,7 +20,7 @@ use Slim\Http\Response;
17 * 20 *
18 * @package Shaarli\Api\Controllers 21 * @package Shaarli\Api\Controllers
19 */ 22 */
20class GetLinksTest extends \PHPUnit\Framework\TestCase 23class GetLinksTest extends \Shaarli\TestCase
21{ 24{
22 /** 25 /**
23 * @var string datastore to test write operations 26 * @var string datastore to test write operations
@@ -50,17 +53,19 @@ class GetLinksTest extends \PHPUnit\Framework\TestCase
50 const NB_FIELDS_LINK = 9; 53 const NB_FIELDS_LINK = 9;
51 54
52 /** 55 /**
53 * Before every test, instantiate a new Api with its config, plugins and links. 56 * Before every test, instantiate a new Api with its config, plugins and bookmarks.
54 */ 57 */
55 public function setUp() 58 protected function setUp(): void
56 { 59 {
57 $this->conf = new ConfigManager('tests/utils/config/configJson'); 60 $this->conf = new ConfigManager('tests/utils/config/configJson');
61 $this->conf->set('resource.datastore', self::$testDatastore);
58 $this->refDB = new \ReferenceLinkDB(); 62 $this->refDB = new \ReferenceLinkDB();
59 $this->refDB->write(self::$testDatastore); 63 $this->refDB->write(self::$testDatastore);
64 $history = new History('sandbox/history.php');
60 65
61 $this->container = new Container(); 66 $this->container = new Container();
62 $this->container['conf'] = $this->conf; 67 $this->container['conf'] = $this->conf;
63 $this->container['db'] = new LinkDB(self::$testDatastore, true, false); 68 $this->container['db'] = new BookmarkFileService($this->conf, $history, true);
64 $this->container['history'] = null; 69 $this->container['history'] = null;
65 70
66 $this->controller = new Links($this->container); 71 $this->controller = new Links($this->container);
@@ -69,13 +74,13 @@ class GetLinksTest extends \PHPUnit\Framework\TestCase
69 /** 74 /**
70 * After every test, remove the test datastore. 75 * After every test, remove the test datastore.
71 */ 76 */
72 public function tearDown() 77 protected function tearDown(): void
73 { 78 {
74 @unlink(self::$testDatastore); 79 @unlink(self::$testDatastore);
75 } 80 }
76 81
77 /** 82 /**
78 * Test basic getLinks service: returns all links. 83 * Test basic getLinks service: returns all bookmarks.
79 */ 84 */
80 public function testGetLinks() 85 public function testGetLinks()
81 { 86 {
@@ -104,7 +109,7 @@ class GetLinksTest extends \PHPUnit\Framework\TestCase
104 109
105 // Check first element fields 110 // Check first element fields
106 $first = $data[2]; 111 $first = $data[2];
107 $this->assertEquals('http://domain.tld/?WDWyig', $first['url']); 112 $this->assertEquals('http://domain.tld/shaare/WDWyig', $first['url']);
108 $this->assertEquals('WDWyig', $first['shorturl']); 113 $this->assertEquals('WDWyig', $first['shorturl']);
109 $this->assertEquals('Link title: @website', $first['title']); 114 $this->assertEquals('Link title: @website', $first['title']);
110 $this->assertEquals( 115 $this->assertEquals(
@@ -114,7 +119,7 @@ class GetLinksTest extends \PHPUnit\Framework\TestCase
114 $this->assertEquals('sTuff', $first['tags'][0]); 119 $this->assertEquals('sTuff', $first['tags'][0]);
115 $this->assertEquals(false, $first['private']); 120 $this->assertEquals(false, $first['private']);
116 $this->assertEquals( 121 $this->assertEquals(
117 \DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20150310_114651')->format(\DateTime::ATOM), 122 \DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20150310_114651')->format(\DateTime::ATOM),
118 $first['created'] 123 $first['created']
119 ); 124 );
120 $this->assertEmpty($first['updated']); 125 $this->assertEmpty($first['updated']);
@@ -125,7 +130,7 @@ class GetLinksTest extends \PHPUnit\Framework\TestCase
125 130
126 // Update date 131 // Update date
127 $this->assertEquals( 132 $this->assertEquals(
128 \DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160803_093033')->format(\DateTime::ATOM), 133 \DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20160803_093033')->format(\DateTime::ATOM),
129 $link['updated'] 134 $link['updated']
130 ); 135 );
131 } 136 }
diff --git a/tests/api/controllers/links/PostLinkTest.php b/tests/api/controllers/links/PostLinkTest.php
index d683a984..fe3de66f 100644
--- a/tests/api/controllers/links/PostLinkTest.php
+++ b/tests/api/controllers/links/PostLinkTest.php
@@ -2,9 +2,11 @@
2 2
3namespace Shaarli\Api\Controllers; 3namespace Shaarli\Api\Controllers;
4 4
5use PHPUnit\Framework\TestCase; 5use Shaarli\Bookmark\Bookmark;
6use Shaarli\Bookmark\BookmarkFileService;
6use Shaarli\Config\ConfigManager; 7use Shaarli\Config\ConfigManager;
7use Shaarli\History; 8use Shaarli\History;
9use Shaarli\TestCase;
8use Slim\Container; 10use Slim\Container;
9use Slim\Http\Environment; 11use Slim\Http\Environment;
10use Slim\Http\Request; 12use Slim\Http\Request;
@@ -41,6 +43,11 @@ class PostLinkTest extends TestCase
41 protected $refDB = null; 43 protected $refDB = null;
42 44
43 /** 45 /**
46 * @var BookmarkFileService instance.
47 */
48 protected $bookmarkService;
49
50 /**
44 * @var HistoryController instance. 51 * @var HistoryController instance.
45 */ 52 */
46 protected $history; 53 protected $history;
@@ -61,29 +68,30 @@ class PostLinkTest extends TestCase
61 const NB_FIELDS_LINK = 9; 68 const NB_FIELDS_LINK = 9;
62 69
63 /** 70 /**
64 * Before every test, instantiate a new Api with its config, plugins and links. 71 * Before every test, instantiate a new Api with its config, plugins and bookmarks.
65 */ 72 */
66 public function setUp() 73 protected function setUp(): void
67 { 74 {
68 $this->conf = new ConfigManager('tests/utils/config/configJson.json.php'); 75 $this->conf = new ConfigManager('tests/utils/config/configJson');
76 $this->conf->set('resource.datastore', self::$testDatastore);
69 $this->refDB = new \ReferenceLinkDB(); 77 $this->refDB = new \ReferenceLinkDB();
70 $this->refDB->write(self::$testDatastore); 78 $this->refDB->write(self::$testDatastore);
71
72 $refHistory = new \ReferenceHistory(); 79 $refHistory = new \ReferenceHistory();
73 $refHistory->write(self::$testHistory); 80 $refHistory->write(self::$testHistory);
74 $this->history = new History(self::$testHistory); 81 $this->history = new History(self::$testHistory);
82 $this->bookmarkService = new BookmarkFileService($this->conf, $this->history, true);
75 83
76 $this->container = new Container(); 84 $this->container = new Container();
77 $this->container['conf'] = $this->conf; 85 $this->container['conf'] = $this->conf;
78 $this->container['db'] = new \Shaarli\Bookmark\LinkDB(self::$testDatastore, true, false); 86 $this->container['db'] = $this->bookmarkService;
79 $this->container['history'] = new History(self::$testHistory); 87 $this->container['history'] = $this->history;
80 88
81 $this->controller = new Links($this->container); 89 $this->controller = new Links($this->container);
82 90
83 $mock = $this->createMock(Router::class); 91 $mock = $this->createMock(Router::class);
84 $mock->expects($this->any()) 92 $mock->expects($this->any())
85 ->method('relativePathFor') 93 ->method('relativePathFor')
86 ->willReturn('api/v1/links/1'); 94 ->willReturn('api/v1/bookmarks/1');
87 95
88 // affect @property-read... seems to work 96 // affect @property-read... seems to work
89 $this->controller->getCi()->router = $mock; 97 $this->controller->getCi()->router = $mock;
@@ -99,7 +107,7 @@ class PostLinkTest extends TestCase
99 /** 107 /**
100 * After every test, remove the test datastore. 108 * After every test, remove the test datastore.
101 */ 109 */
102 public function tearDown() 110 protected function tearDown(): void
103 { 111 {
104 @unlink(self::$testDatastore); 112 @unlink(self::$testDatastore);
105 @unlink(self::$testHistory); 113 @unlink(self::$testHistory);
@@ -118,16 +126,16 @@ class PostLinkTest extends TestCase
118 126
119 $response = $this->controller->postLink($request, new Response()); 127 $response = $this->controller->postLink($request, new Response());
120 $this->assertEquals(201, $response->getStatusCode()); 128 $this->assertEquals(201, $response->getStatusCode());
121 $this->assertEquals('api/v1/links/1', $response->getHeader('Location')[0]); 129 $this->assertEquals('api/v1/bookmarks/1', $response->getHeader('Location')[0]);
122 $data = json_decode((string) $response->getBody(), true); 130 $data = json_decode((string) $response->getBody(), true);
123 $this->assertEquals(self::NB_FIELDS_LINK, count($data)); 131 $this->assertEquals(self::NB_FIELDS_LINK, count($data));
124 $this->assertEquals(43, $data['id']); 132 $this->assertEquals(43, $data['id']);
125 $this->assertRegExp('/[\w_-]{6}/', $data['shorturl']); 133 $this->assertRegExp('/[\w_-]{6}/', $data['shorturl']);
126 $this->assertEquals('http://domain.tld/?' . $data['shorturl'], $data['url']); 134 $this->assertEquals('http://domain.tld/shaare/' . $data['shorturl'], $data['url']);
127 $this->assertEquals('?' . $data['shorturl'], $data['title']); 135 $this->assertEquals('/shaare/' . $data['shorturl'], $data['title']);
128 $this->assertEquals('', $data['description']); 136 $this->assertEquals('', $data['description']);
129 $this->assertEquals([], $data['tags']); 137 $this->assertEquals([], $data['tags']);
130 $this->assertEquals(false, $data['private']); 138 $this->assertEquals(true, $data['private']);
131 $this->assertTrue( 139 $this->assertTrue(
132 new \DateTime('5 seconds ago') < \DateTime::createFromFormat(\DateTime::ATOM, $data['created']) 140 new \DateTime('5 seconds ago') < \DateTime::createFromFormat(\DateTime::ATOM, $data['created'])
133 ); 141 );
@@ -163,7 +171,7 @@ class PostLinkTest extends TestCase
163 $response = $this->controller->postLink($request, new Response()); 171 $response = $this->controller->postLink($request, new Response());
164 172
165 $this->assertEquals(201, $response->getStatusCode()); 173 $this->assertEquals(201, $response->getStatusCode());
166 $this->assertEquals('api/v1/links/1', $response->getHeader('Location')[0]); 174 $this->assertEquals('api/v1/bookmarks/1', $response->getHeader('Location')[0]);
167 $data = json_decode((string) $response->getBody(), true); 175 $data = json_decode((string) $response->getBody(), true);
168 $this->assertEquals(self::NB_FIELDS_LINK, count($data)); 176 $this->assertEquals(self::NB_FIELDS_LINK, count($data));
169 $this->assertEquals(43, $data['id']); 177 $this->assertEquals(43, $data['id']);
@@ -211,11 +219,11 @@ class PostLinkTest extends TestCase
211 $this->assertEquals(['gnu', 'media', 'web', '.hidden', 'hashtag'], $data['tags']); 219 $this->assertEquals(['gnu', 'media', 'web', '.hidden', 'hashtag'], $data['tags']);
212 $this->assertEquals(false, $data['private']); 220 $this->assertEquals(false, $data['private']);
213 $this->assertEquals( 221 $this->assertEquals(
214 \DateTime::createFromFormat(\Shaarli\Bookmark\LinkDB::LINK_DATE_FORMAT, '20130614_184135'), 222 \DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20130614_184135'),
215 \DateTime::createFromFormat(\DateTime::ATOM, $data['created']) 223 \DateTime::createFromFormat(\DateTime::ATOM, $data['created'])
216 ); 224 );
217 $this->assertEquals( 225 $this->assertEquals(
218 \DateTime::createFromFormat(\Shaarli\Bookmark\LinkDB::LINK_DATE_FORMAT, '20130615_184230'), 226 \DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20130615_184230'),
219 \DateTime::createFromFormat(\DateTime::ATOM, $data['updated']) 227 \DateTime::createFromFormat(\DateTime::ATOM, $data['updated'])
220 ); 228 );
221 } 229 }
diff --git a/tests/api/controllers/links/PutLinkTest.php b/tests/api/controllers/links/PutLinkTest.php
index cd815b66..a2e87c59 100644
--- a/tests/api/controllers/links/PutLinkTest.php
+++ b/tests/api/controllers/links/PutLinkTest.php
@@ -3,6 +3,8 @@
3 3
4namespace Shaarli\Api\Controllers; 4namespace Shaarli\Api\Controllers;
5 5
6use Shaarli\Bookmark\Bookmark;
7use Shaarli\Bookmark\BookmarkFileService;
6use Shaarli\Config\ConfigManager; 8use Shaarli\Config\ConfigManager;
7use Shaarli\History; 9use Shaarli\History;
8use Slim\Container; 10use Slim\Container;
@@ -10,7 +12,7 @@ use Slim\Http\Environment;
10use Slim\Http\Request; 12use Slim\Http\Request;
11use Slim\Http\Response; 13use Slim\Http\Response;
12 14
13class PutLinkTest extends \PHPUnit\Framework\TestCase 15class PutLinkTest extends \Shaarli\TestCase
14{ 16{
15 /** 17 /**
16 * @var string datastore to test write operations 18 * @var string datastore to test write operations
@@ -33,6 +35,11 @@ class PutLinkTest extends \PHPUnit\Framework\TestCase
33 protected $refDB = null; 35 protected $refDB = null;
34 36
35 /** 37 /**
38 * @var BookmarkFileService instance.
39 */
40 protected $bookmarkService;
41
42 /**
36 * @var HistoryController instance. 43 * @var HistoryController instance.
37 */ 44 */
38 protected $history; 45 protected $history;
@@ -53,22 +60,23 @@ class PutLinkTest extends \PHPUnit\Framework\TestCase
53 const NB_FIELDS_LINK = 9; 60 const NB_FIELDS_LINK = 9;
54 61
55 /** 62 /**
56 * Before every test, instantiate a new Api with its config, plugins and links. 63 * Before every test, instantiate a new Api with its config, plugins and bookmarks.
57 */ 64 */
58 public function setUp() 65 protected function setUp(): void
59 { 66 {
60 $this->conf = new ConfigManager('tests/utils/config/configJson.json.php'); 67 $this->conf = new ConfigManager('tests/utils/config/configJson');
68 $this->conf->set('resource.datastore', self::$testDatastore);
61 $this->refDB = new \ReferenceLinkDB(); 69 $this->refDB = new \ReferenceLinkDB();
62 $this->refDB->write(self::$testDatastore); 70 $this->refDB->write(self::$testDatastore);
63
64 $refHistory = new \ReferenceHistory(); 71 $refHistory = new \ReferenceHistory();
65 $refHistory->write(self::$testHistory); 72 $refHistory->write(self::$testHistory);
66 $this->history = new History(self::$testHistory); 73 $this->history = new History(self::$testHistory);
74 $this->bookmarkService = new BookmarkFileService($this->conf, $this->history, true);
67 75
68 $this->container = new Container(); 76 $this->container = new Container();
69 $this->container['conf'] = $this->conf; 77 $this->container['conf'] = $this->conf;
70 $this->container['db'] = new \Shaarli\Bookmark\LinkDB(self::$testDatastore, true, false); 78 $this->container['db'] = $this->bookmarkService;
71 $this->container['history'] = new History(self::$testHistory); 79 $this->container['history'] = $this->history;
72 80
73 $this->controller = new Links($this->container); 81 $this->controller = new Links($this->container);
74 82
@@ -83,7 +91,7 @@ class PutLinkTest extends \PHPUnit\Framework\TestCase
83 /** 91 /**
84 * After every test, remove the test datastore. 92 * After every test, remove the test datastore.
85 */ 93 */
86 public function tearDown() 94 protected function tearDown(): void
87 { 95 {
88 @unlink(self::$testDatastore); 96 @unlink(self::$testDatastore);
89 @unlink(self::$testHistory); 97 @unlink(self::$testHistory);
@@ -106,11 +114,11 @@ class PutLinkTest extends \PHPUnit\Framework\TestCase
106 $this->assertEquals(self::NB_FIELDS_LINK, count($data)); 114 $this->assertEquals(self::NB_FIELDS_LINK, count($data));
107 $this->assertEquals($id, $data['id']); 115 $this->assertEquals($id, $data['id']);
108 $this->assertEquals('WDWyig', $data['shorturl']); 116 $this->assertEquals('WDWyig', $data['shorturl']);
109 $this->assertEquals('http://domain.tld/?WDWyig', $data['url']); 117 $this->assertEquals('http://domain.tld/shaare/WDWyig', $data['url']);
110 $this->assertEquals('?WDWyig', $data['title']); 118 $this->assertEquals('/shaare/WDWyig', $data['title']);
111 $this->assertEquals('', $data['description']); 119 $this->assertEquals('', $data['description']);
112 $this->assertEquals([], $data['tags']); 120 $this->assertEquals([], $data['tags']);
113 $this->assertEquals(false, $data['private']); 121 $this->assertEquals(true, $data['private']);
114 $this->assertEquals( 122 $this->assertEquals(
115 \DateTime::createFromFormat('Ymd_His', '20150310_114651'), 123 \DateTime::createFromFormat('Ymd_His', '20150310_114651'),
116 \DateTime::createFromFormat(\DateTime::ATOM, $data['created']) 124 \DateTime::createFromFormat(\DateTime::ATOM, $data['created'])
@@ -199,23 +207,23 @@ class PutLinkTest extends \PHPUnit\Framework\TestCase
199 $this->assertEquals(['gnu', 'media', 'web', '.hidden', 'hashtag'], $data['tags']); 207 $this->assertEquals(['gnu', 'media', 'web', '.hidden', 'hashtag'], $data['tags']);
200 $this->assertEquals(false, $data['private']); 208 $this->assertEquals(false, $data['private']);
201 $this->assertEquals( 209 $this->assertEquals(
202 \DateTime::createFromFormat(\Shaarli\Bookmark\LinkDB::LINK_DATE_FORMAT, '20130614_184135'), 210 \DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20130614_184135'),
203 \DateTime::createFromFormat(\DateTime::ATOM, $data['created']) 211 \DateTime::createFromFormat(\DateTime::ATOM, $data['created'])
204 ); 212 );
205 $this->assertEquals( 213 $this->assertEquals(
206 \DateTime::createFromFormat(\Shaarli\Bookmark\LinkDB::LINK_DATE_FORMAT, '20130615_184230'), 214 \DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20130615_184230'),
207 \DateTime::createFromFormat(\DateTime::ATOM, $data['updated']) 215 \DateTime::createFromFormat(\DateTime::ATOM, $data['updated'])
208 ); 216 );
209 } 217 }
210 218
211 /** 219 /**
212 * Test link update on non existent link => ApiLinkNotFoundException. 220 * Test link update on non existent link => ApiLinkNotFoundException.
213 *
214 * @expectedException Shaarli\Api\Exceptions\ApiLinkNotFoundException
215 * @expectedExceptionMessage Link not found
216 */ 221 */
217 public function testGetLink404() 222 public function testGetLink404()
218 { 223 {
224 $this->expectException(\Shaarli\Api\Exceptions\ApiLinkNotFoundException::class);
225 $this->expectExceptionMessage('Link not found');
226
219 $env = Environment::mock([ 227 $env = Environment::mock([
220 'REQUEST_METHOD' => 'PUT', 228 'REQUEST_METHOD' => 'PUT',
221 ]); 229 ]);
diff --git a/tests/api/controllers/tags/DeleteTagTest.php b/tests/api/controllers/tags/DeleteTagTest.php
index 84e1d56e..1326eb47 100644
--- a/tests/api/controllers/tags/DeleteTagTest.php
+++ b/tests/api/controllers/tags/DeleteTagTest.php
@@ -3,6 +3,7 @@
3 3
4namespace Shaarli\Api\Controllers; 4namespace Shaarli\Api\Controllers;
5 5
6use Shaarli\Bookmark\BookmarkFileService;
6use Shaarli\Bookmark\LinkDB; 7use Shaarli\Bookmark\LinkDB;
7use Shaarli\Config\ConfigManager; 8use Shaarli\Config\ConfigManager;
8use Shaarli\History; 9use Shaarli\History;
@@ -11,7 +12,7 @@ use Slim\Http\Environment;
11use Slim\Http\Request; 12use Slim\Http\Request;
12use Slim\Http\Response; 13use Slim\Http\Response;
13 14
14class DeleteTagTest extends \PHPUnit\Framework\TestCase 15class DeleteTagTest extends \Shaarli\TestCase
15{ 16{
16 /** 17 /**
17 * @var string datastore to test write operations 18 * @var string datastore to test write operations
@@ -34,9 +35,9 @@ class DeleteTagTest extends \PHPUnit\Framework\TestCase
34 protected $refDB = null; 35 protected $refDB = null;
35 36
36 /** 37 /**
37 * @var LinkDB instance. 38 * @var BookmarkFileService instance.
38 */ 39 */
39 protected $linkDB; 40 protected $bookmarkService;
40 41
41 /** 42 /**
42 * @var HistoryController instance. 43 * @var HistoryController instance.
@@ -54,20 +55,22 @@ class DeleteTagTest extends \PHPUnit\Framework\TestCase
54 protected $controller; 55 protected $controller;
55 56
56 /** 57 /**
57 * Before each test, instantiate a new Api with its config, plugins and links. 58 * Before each test, instantiate a new Api with its config, plugins and bookmarks.
58 */ 59 */
59 public function setUp() 60 protected function setUp(): void
60 { 61 {
61 $this->conf = new ConfigManager('tests/utils/config/configJson'); 62 $this->conf = new ConfigManager('tests/utils/config/configJson');
63 $this->conf->set('resource.datastore', self::$testDatastore);
62 $this->refDB = new \ReferenceLinkDB(); 64 $this->refDB = new \ReferenceLinkDB();
63 $this->refDB->write(self::$testDatastore); 65 $this->refDB->write(self::$testDatastore);
64 $this->linkDB = new LinkDB(self::$testDatastore, true, false);
65 $refHistory = new \ReferenceHistory(); 66 $refHistory = new \ReferenceHistory();
66 $refHistory->write(self::$testHistory); 67 $refHistory->write(self::$testHistory);
67 $this->history = new History(self::$testHistory); 68 $this->history = new History(self::$testHistory);
69 $this->bookmarkService = new BookmarkFileService($this->conf, $this->history, true);
70
68 $this->container = new Container(); 71 $this->container = new Container();
69 $this->container['conf'] = $this->conf; 72 $this->container['conf'] = $this->conf;
70 $this->container['db'] = $this->linkDB; 73 $this->container['db'] = $this->bookmarkService;
71 $this->container['history'] = $this->history; 74 $this->container['history'] = $this->history;
72 75
73 $this->controller = new Tags($this->container); 76 $this->controller = new Tags($this->container);
@@ -76,7 +79,7 @@ class DeleteTagTest extends \PHPUnit\Framework\TestCase
76 /** 79 /**
77 * After each test, remove the test datastore. 80 * After each test, remove the test datastore.
78 */ 81 */
79 public function tearDown() 82 protected function tearDown(): void
80 { 83 {
81 @unlink(self::$testDatastore); 84 @unlink(self::$testDatastore);
82 @unlink(self::$testHistory); 85 @unlink(self::$testHistory);
@@ -88,7 +91,7 @@ class DeleteTagTest extends \PHPUnit\Framework\TestCase
88 public function testDeleteTagValid() 91 public function testDeleteTagValid()
89 { 92 {
90 $tagName = 'gnu'; 93 $tagName = 'gnu';
91 $tags = $this->linkDB->linksCountPerTag(); 94 $tags = $this->bookmarkService->bookmarksCountPerTag();
92 $this->assertTrue($tags[$tagName] > 0); 95 $this->assertTrue($tags[$tagName] > 0);
93 $env = Environment::mock([ 96 $env = Environment::mock([
94 'REQUEST_METHOD' => 'DELETE', 97 'REQUEST_METHOD' => 'DELETE',
@@ -99,11 +102,11 @@ class DeleteTagTest extends \PHPUnit\Framework\TestCase
99 $this->assertEquals(204, $response->getStatusCode()); 102 $this->assertEquals(204, $response->getStatusCode());
100 $this->assertEmpty((string) $response->getBody()); 103 $this->assertEmpty((string) $response->getBody());
101 104
102 $this->linkDB = new LinkDB(self::$testDatastore, true, false); 105 $this->bookmarkService = new BookmarkFileService($this->conf, $this->history, true);
103 $tags = $this->linkDB->linksCountPerTag(); 106 $tags = $this->bookmarkService->bookmarksCountPerTag();
104 $this->assertFalse(isset($tags[$tagName])); 107 $this->assertFalse(isset($tags[$tagName]));
105 108
106 // 2 links affected 109 // 2 bookmarks affected
107 $historyEntry = $this->history->getHistory()[0]; 110 $historyEntry = $this->history->getHistory()[0];
108 $this->assertEquals(History::UPDATED, $historyEntry['event']); 111 $this->assertEquals(History::UPDATED, $historyEntry['event']);
109 $this->assertTrue( 112 $this->assertTrue(
@@ -122,7 +125,7 @@ class DeleteTagTest extends \PHPUnit\Framework\TestCase
122 public function testDeleteTagCaseSensitivity() 125 public function testDeleteTagCaseSensitivity()
123 { 126 {
124 $tagName = 'sTuff'; 127 $tagName = 'sTuff';
125 $tags = $this->linkDB->linksCountPerTag(); 128 $tags = $this->bookmarkService->bookmarksCountPerTag();
126 $this->assertTrue($tags[$tagName] > 0); 129 $this->assertTrue($tags[$tagName] > 0);
127 $env = Environment::mock([ 130 $env = Environment::mock([
128 'REQUEST_METHOD' => 'DELETE', 131 'REQUEST_METHOD' => 'DELETE',
@@ -133,8 +136,8 @@ class DeleteTagTest extends \PHPUnit\Framework\TestCase
133 $this->assertEquals(204, $response->getStatusCode()); 136 $this->assertEquals(204, $response->getStatusCode());
134 $this->assertEmpty((string) $response->getBody()); 137 $this->assertEmpty((string) $response->getBody());
135 138
136 $this->linkDB = new LinkDB(self::$testDatastore, true, false); 139 $this->bookmarkService = new BookmarkFileService($this->conf, $this->history, true);
137 $tags = $this->linkDB->linksCountPerTag(); 140 $tags = $this->bookmarkService->bookmarksCountPerTag();
138 $this->assertFalse(isset($tags[$tagName])); 141 $this->assertFalse(isset($tags[$tagName]));
139 $this->assertTrue($tags[strtolower($tagName)] > 0); 142 $this->assertTrue($tags[strtolower($tagName)] > 0);
140 143
@@ -147,14 +150,14 @@ class DeleteTagTest extends \PHPUnit\Framework\TestCase
147 150
148 /** 151 /**
149 * Test DELETE tag endpoint: reach not existing tag. 152 * Test DELETE tag endpoint: reach not existing tag.
150 *
151 * @expectedException Shaarli\Api\Exceptions\ApiTagNotFoundException
152 * @expectedExceptionMessage Tag not found
153 */ 153 */
154 public function testDeleteLink404() 154 public function testDeleteLink404()
155 { 155 {
156 $this->expectException(\Shaarli\Api\Exceptions\ApiTagNotFoundException::class);
157 $this->expectExceptionMessage('Tag not found');
158
156 $tagName = 'nopenope'; 159 $tagName = 'nopenope';
157 $tags = $this->linkDB->linksCountPerTag(); 160 $tags = $this->bookmarkService->bookmarksCountPerTag();
158 $this->assertFalse(isset($tags[$tagName])); 161 $this->assertFalse(isset($tags[$tagName]));
159 $env = Environment::mock([ 162 $env = Environment::mock([
160 'REQUEST_METHOD' => 'DELETE', 163 'REQUEST_METHOD' => 'DELETE',
diff --git a/tests/api/controllers/tags/GetTagNameTest.php b/tests/api/controllers/tags/GetTagNameTest.php
index a2525c17..9c05954b 100644
--- a/tests/api/controllers/tags/GetTagNameTest.php
+++ b/tests/api/controllers/tags/GetTagNameTest.php
@@ -2,8 +2,10 @@
2 2
3namespace Shaarli\Api\Controllers; 3namespace Shaarli\Api\Controllers;
4 4
5use Shaarli\Bookmark\BookmarkFileService;
5use Shaarli\Bookmark\LinkDB; 6use Shaarli\Bookmark\LinkDB;
6use Shaarli\Config\ConfigManager; 7use Shaarli\Config\ConfigManager;
8use Shaarli\History;
7use Slim\Container; 9use Slim\Container;
8use Slim\Http\Environment; 10use Slim\Http\Environment;
9use Slim\Http\Request; 11use Slim\Http\Request;
@@ -16,7 +18,7 @@ use Slim\Http\Response;
16 * 18 *
17 * @package Shaarli\Api\Controllers 19 * @package Shaarli\Api\Controllers
18 */ 20 */
19class GetTagNameTest extends \PHPUnit\Framework\TestCase 21class GetTagNameTest extends \Shaarli\TestCase
20{ 22{
21 /** 23 /**
22 * @var string datastore to test write operations 24 * @var string datastore to test write operations
@@ -49,17 +51,19 @@ class GetTagNameTest extends \PHPUnit\Framework\TestCase
49 const NB_FIELDS_TAG = 2; 51 const NB_FIELDS_TAG = 2;
50 52
51 /** 53 /**
52 * Before each test, instantiate a new Api with its config, plugins and links. 54 * Before each test, instantiate a new Api with its config, plugins and bookmarks.
53 */ 55 */
54 public function setUp() 56 protected function setUp(): void
55 { 57 {
56 $this->conf = new ConfigManager('tests/utils/config/configJson'); 58 $this->conf = new ConfigManager('tests/utils/config/configJson');
59 $this->conf->set('resource.datastore', self::$testDatastore);
57 $this->refDB = new \ReferenceLinkDB(); 60 $this->refDB = new \ReferenceLinkDB();
58 $this->refDB->write(self::$testDatastore); 61 $this->refDB->write(self::$testDatastore);
62 $history = new History('sandbox/history.php');
59 63
60 $this->container = new Container(); 64 $this->container = new Container();
61 $this->container['conf'] = $this->conf; 65 $this->container['conf'] = $this->conf;
62 $this->container['db'] = new LinkDB(self::$testDatastore, true, false); 66 $this->container['db'] = new BookmarkFileService($this->conf, $history, true);
63 $this->container['history'] = null; 67 $this->container['history'] = null;
64 68
65 $this->controller = new Tags($this->container); 69 $this->controller = new Tags($this->container);
@@ -68,7 +72,7 @@ class GetTagNameTest extends \PHPUnit\Framework\TestCase
68 /** 72 /**
69 * After each test, remove the test datastore. 73 * After each test, remove the test datastore.
70 */ 74 */
71 public function tearDown() 75 protected function tearDown(): void
72 { 76 {
73 @unlink(self::$testDatastore); 77 @unlink(self::$testDatastore);
74 } 78 }
@@ -113,12 +117,12 @@ class GetTagNameTest extends \PHPUnit\Framework\TestCase
113 117
114 /** 118 /**
115 * Test basic getTag service: get non existent tag => ApiTagNotFoundException. 119 * Test basic getTag service: get non existent tag => ApiTagNotFoundException.
116 *
117 * @expectedException Shaarli\Api\Exceptions\ApiTagNotFoundException
118 * @expectedExceptionMessage Tag not found
119 */ 120 */
120 public function testGetTag404() 121 public function testGetTag404()
121 { 122 {
123 $this->expectException(\Shaarli\Api\Exceptions\ApiTagNotFoundException::class);
124 $this->expectExceptionMessage('Tag not found');
125
122 $env = Environment::mock([ 126 $env = Environment::mock([
123 'REQUEST_METHOD' => 'GET', 127 'REQUEST_METHOD' => 'GET',
124 ]); 128 ]);
diff --git a/tests/api/controllers/tags/GetTagsTest.php b/tests/api/controllers/tags/GetTagsTest.php
index 98628c98..3459fdfa 100644
--- a/tests/api/controllers/tags/GetTagsTest.php
+++ b/tests/api/controllers/tags/GetTagsTest.php
@@ -1,8 +1,10 @@
1<?php 1<?php
2namespace Shaarli\Api\Controllers; 2namespace Shaarli\Api\Controllers;
3 3
4use Shaarli\Bookmark\BookmarkFileService;
4use Shaarli\Bookmark\LinkDB; 5use Shaarli\Bookmark\LinkDB;
5use Shaarli\Config\ConfigManager; 6use Shaarli\Config\ConfigManager;
7use Shaarli\History;
6use Slim\Container; 8use Slim\Container;
7use Slim\Http\Environment; 9use Slim\Http\Environment;
8use Slim\Http\Request; 10use Slim\Http\Request;
@@ -15,7 +17,7 @@ use Slim\Http\Response;
15 * 17 *
16 * @package Shaarli\Api\Controllers 18 * @package Shaarli\Api\Controllers
17 */ 19 */
18class GetTagsTest extends \PHPUnit\Framework\TestCase 20class GetTagsTest extends \Shaarli\TestCase
19{ 21{
20 /** 22 /**
21 * @var string datastore to test write operations 23 * @var string datastore to test write operations
@@ -38,9 +40,9 @@ class GetTagsTest extends \PHPUnit\Framework\TestCase
38 protected $container; 40 protected $container;
39 41
40 /** 42 /**
41 * @var LinkDB instance. 43 * @var BookmarkFileService instance.
42 */ 44 */
43 protected $linkDB; 45 protected $bookmarkService;
44 46
45 /** 47 /**
46 * @var Tags controller instance. 48 * @var Tags controller instance.
@@ -53,18 +55,21 @@ class GetTagsTest extends \PHPUnit\Framework\TestCase
53 const NB_FIELDS_TAG = 2; 55 const NB_FIELDS_TAG = 2;
54 56
55 /** 57 /**
56 * Before every test, instantiate a new Api with its config, plugins and links. 58 * Before every test, instantiate a new Api with its config, plugins and bookmarks.
57 */ 59 */
58 public function setUp() 60 protected function setUp(): void
59 { 61 {
60 $this->conf = new ConfigManager('tests/utils/config/configJson'); 62 $this->conf = new ConfigManager('tests/utils/config/configJson');
63 $this->conf->set('resource.datastore', self::$testDatastore);
61 $this->refDB = new \ReferenceLinkDB(); 64 $this->refDB = new \ReferenceLinkDB();
62 $this->refDB->write(self::$testDatastore); 65 $this->refDB->write(self::$testDatastore);
66 $history = new History('sandbox/history.php');
67
68 $this->bookmarkService = new BookmarkFileService($this->conf, $history, true);
63 69
64 $this->container = new Container(); 70 $this->container = new Container();
65 $this->container['conf'] = $this->conf; 71 $this->container['conf'] = $this->conf;
66 $this->linkDB = new LinkDB(self::$testDatastore, true, false); 72 $this->container['db'] = $this->bookmarkService;
67 $this->container['db'] = $this->linkDB;
68 $this->container['history'] = null; 73 $this->container['history'] = null;
69 74
70 $this->controller = new Tags($this->container); 75 $this->controller = new Tags($this->container);
@@ -73,7 +78,7 @@ class GetTagsTest extends \PHPUnit\Framework\TestCase
73 /** 78 /**
74 * After every test, remove the test datastore. 79 * After every test, remove the test datastore.
75 */ 80 */
76 public function tearDown() 81 protected function tearDown(): void
77 { 82 {
78 @unlink(self::$testDatastore); 83 @unlink(self::$testDatastore);
79 } 84 }
@@ -83,7 +88,7 @@ class GetTagsTest extends \PHPUnit\Framework\TestCase
83 */ 88 */
84 public function testGetTagsAll() 89 public function testGetTagsAll()
85 { 90 {
86 $tags = $this->linkDB->linksCountPerTag(); 91 $tags = $this->bookmarkService->bookmarksCountPerTag();
87 $env = Environment::mock([ 92 $env = Environment::mock([
88 'REQUEST_METHOD' => 'GET', 93 'REQUEST_METHOD' => 'GET',
89 ]); 94 ]);
@@ -136,7 +141,7 @@ class GetTagsTest extends \PHPUnit\Framework\TestCase
136 */ 141 */
137 public function testGetTagsLimitAll() 142 public function testGetTagsLimitAll()
138 { 143 {
139 $tags = $this->linkDB->linksCountPerTag(); 144 $tags = $this->bookmarkService->bookmarksCountPerTag();
140 $env = Environment::mock([ 145 $env = Environment::mock([
141 'REQUEST_METHOD' => 'GET', 146 'REQUEST_METHOD' => 'GET',
142 'QUERY_STRING' => 'limit=all' 147 'QUERY_STRING' => 'limit=all'
@@ -170,7 +175,7 @@ class GetTagsTest extends \PHPUnit\Framework\TestCase
170 */ 175 */
171 public function testGetTagsVisibilityPrivate() 176 public function testGetTagsVisibilityPrivate()
172 { 177 {
173 $tags = $this->linkDB->linksCountPerTag([], 'private'); 178 $tags = $this->bookmarkService->bookmarksCountPerTag([], 'private');
174 $env = Environment::mock([ 179 $env = Environment::mock([
175 'REQUEST_METHOD' => 'GET', 180 'REQUEST_METHOD' => 'GET',
176 'QUERY_STRING' => 'visibility=private' 181 'QUERY_STRING' => 'visibility=private'
@@ -190,7 +195,7 @@ class GetTagsTest extends \PHPUnit\Framework\TestCase
190 */ 195 */
191 public function testGetTagsVisibilityPublic() 196 public function testGetTagsVisibilityPublic()
192 { 197 {
193 $tags = $this->linkDB->linksCountPerTag([], 'public'); 198 $tags = $this->bookmarkService->bookmarksCountPerTag([], 'public');
194 $env = Environment::mock( 199 $env = Environment::mock(
195 [ 200 [
196 'REQUEST_METHOD' => 'GET', 201 'REQUEST_METHOD' => 'GET',
diff --git a/tests/api/controllers/tags/PutTagTest.php b/tests/api/controllers/tags/PutTagTest.php
index 86106fc7..74edde78 100644
--- a/tests/api/controllers/tags/PutTagTest.php
+++ b/tests/api/controllers/tags/PutTagTest.php
@@ -3,6 +3,7 @@
3namespace Shaarli\Api\Controllers; 3namespace Shaarli\Api\Controllers;
4 4
5use Shaarli\Api\Exceptions\ApiBadParametersException; 5use Shaarli\Api\Exceptions\ApiBadParametersException;
6use Shaarli\Bookmark\BookmarkFileService;
6use Shaarli\Bookmark\LinkDB; 7use Shaarli\Bookmark\LinkDB;
7use Shaarli\Config\ConfigManager; 8use Shaarli\Config\ConfigManager;
8use Shaarli\History; 9use Shaarli\History;
@@ -11,7 +12,7 @@ use Slim\Http\Environment;
11use Slim\Http\Request; 12use Slim\Http\Request;
12use Slim\Http\Response; 13use Slim\Http\Response;
13 14
14class PutTagTest extends \PHPUnit\Framework\TestCase 15class PutTagTest extends \Shaarli\TestCase
15{ 16{
16 /** 17 /**
17 * @var string datastore to test write operations 18 * @var string datastore to test write operations
@@ -44,9 +45,9 @@ class PutTagTest extends \PHPUnit\Framework\TestCase
44 protected $container; 45 protected $container;
45 46
46 /** 47 /**
47 * @var LinkDB instance. 48 * @var BookmarkFileService instance.
48 */ 49 */
49 protected $linkDB; 50 protected $bookmarkService;
50 51
51 /** 52 /**
52 * @var Tags controller instance. 53 * @var Tags controller instance.
@@ -59,22 +60,22 @@ class PutTagTest extends \PHPUnit\Framework\TestCase
59 const NB_FIELDS_TAG = 2; 60 const NB_FIELDS_TAG = 2;
60 61
61 /** 62 /**
62 * Before every test, instantiate a new Api with its config, plugins and links. 63 * Before every test, instantiate a new Api with its config, plugins and bookmarks.
63 */ 64 */
64 public function setUp() 65 protected function setUp(): void
65 { 66 {
66 $this->conf = new ConfigManager('tests/utils/config/configJson.json.php'); 67 $this->conf = new ConfigManager('tests/utils/config/configJson');
68 $this->conf->set('resource.datastore', self::$testDatastore);
67 $this->refDB = new \ReferenceLinkDB(); 69 $this->refDB = new \ReferenceLinkDB();
68 $this->refDB->write(self::$testDatastore); 70 $this->refDB->write(self::$testDatastore);
69
70 $refHistory = new \ReferenceHistory(); 71 $refHistory = new \ReferenceHistory();
71 $refHistory->write(self::$testHistory); 72 $refHistory->write(self::$testHistory);
72 $this->history = new History(self::$testHistory); 73 $this->history = new History(self::$testHistory);
74 $this->bookmarkService = new BookmarkFileService($this->conf, $this->history, true);
73 75
74 $this->container = new Container(); 76 $this->container = new Container();
75 $this->container['conf'] = $this->conf; 77 $this->container['conf'] = $this->conf;
76 $this->linkDB = new LinkDB(self::$testDatastore, true, false); 78 $this->container['db'] = $this->bookmarkService;
77 $this->container['db'] = $this->linkDB;
78 $this->container['history'] = $this->history; 79 $this->container['history'] = $this->history;
79 80
80 $this->controller = new Tags($this->container); 81 $this->controller = new Tags($this->container);
@@ -83,7 +84,7 @@ class PutTagTest extends \PHPUnit\Framework\TestCase
83 /** 84 /**
84 * After every test, remove the test datastore. 85 * After every test, remove the test datastore.
85 */ 86 */
86 public function tearDown() 87 protected function tearDown(): void
87 { 88 {
88 @unlink(self::$testDatastore); 89 @unlink(self::$testDatastore);
89 @unlink(self::$testHistory); 90 @unlink(self::$testHistory);
@@ -109,7 +110,7 @@ class PutTagTest extends \PHPUnit\Framework\TestCase
109 $this->assertEquals($newName, $data['name']); 110 $this->assertEquals($newName, $data['name']);
110 $this->assertEquals(2, $data['occurrences']); 111 $this->assertEquals(2, $data['occurrences']);
111 112
112 $tags = $this->linkDB->linksCountPerTag(); 113 $tags = $this->bookmarkService->bookmarksCountPerTag();
113 $this->assertNotTrue(isset($tags[$tagName])); 114 $this->assertNotTrue(isset($tags[$tagName]));
114 $this->assertEquals(2, $tags[$newName]); 115 $this->assertEquals(2, $tags[$newName]);
115 116
@@ -133,7 +134,7 @@ class PutTagTest extends \PHPUnit\Framework\TestCase
133 $tagName = 'gnu'; 134 $tagName = 'gnu';
134 $newName = 'w3c'; 135 $newName = 'w3c';
135 136
136 $tags = $this->linkDB->linksCountPerTag(); 137 $tags = $this->bookmarkService->bookmarksCountPerTag();
137 $this->assertEquals(1, $tags[$newName]); 138 $this->assertEquals(1, $tags[$newName]);
138 $this->assertEquals(2, $tags[$tagName]); 139 $this->assertEquals(2, $tags[$tagName]);
139 140
@@ -151,23 +152,23 @@ class PutTagTest extends \PHPUnit\Framework\TestCase
151 $this->assertEquals($newName, $data['name']); 152 $this->assertEquals($newName, $data['name']);
152 $this->assertEquals(3, $data['occurrences']); 153 $this->assertEquals(3, $data['occurrences']);
153 154
154 $tags = $this->linkDB->linksCountPerTag(); 155 $tags = $this->bookmarkService->bookmarksCountPerTag();
155 $this->assertNotTrue(isset($tags[$tagName])); 156 $this->assertNotTrue(isset($tags[$tagName]));
156 $this->assertEquals(3, $tags[$newName]); 157 $this->assertEquals(3, $tags[$newName]);
157 } 158 }
158 159
159 /** 160 /**
160 * Test tag update with an empty new tag name => ApiBadParametersException 161 * Test tag update with an empty new tag name => ApiBadParametersException
161 *
162 * @expectedException Shaarli\Api\Exceptions\ApiBadParametersException
163 * @expectedExceptionMessage New tag name is required in the request body
164 */ 162 */
165 public function testPutTagEmpty() 163 public function testPutTagEmpty()
166 { 164 {
165 $this->expectException(\Shaarli\Api\Exceptions\ApiBadParametersException::class);
166 $this->expectExceptionMessage('New tag name is required in the request body');
167
167 $tagName = 'gnu'; 168 $tagName = 'gnu';
168 $newName = ''; 169 $newName = '';
169 170
170 $tags = $this->linkDB->linksCountPerTag(); 171 $tags = $this->bookmarkService->bookmarksCountPerTag();
171 $this->assertEquals(2, $tags[$tagName]); 172 $this->assertEquals(2, $tags[$tagName]);
172 173
173 $env = Environment::mock([ 174 $env = Environment::mock([
@@ -185,7 +186,7 @@ class PutTagTest extends \PHPUnit\Framework\TestCase
185 try { 186 try {
186 $this->controller->putTag($request, new Response(), ['tagName' => $tagName]); 187 $this->controller->putTag($request, new Response(), ['tagName' => $tagName]);
187 } catch (ApiBadParametersException $e) { 188 } catch (ApiBadParametersException $e) {
188 $tags = $this->linkDB->linksCountPerTag(); 189 $tags = $this->bookmarkService->bookmarksCountPerTag();
189 $this->assertEquals(2, $tags[$tagName]); 190 $this->assertEquals(2, $tags[$tagName]);
190 throw $e; 191 throw $e;
191 } 192 }
@@ -193,12 +194,12 @@ class PutTagTest extends \PHPUnit\Framework\TestCase
193 194
194 /** 195 /**
195 * Test tag update on non existent tag => ApiTagNotFoundException. 196 * Test tag update on non existent tag => ApiTagNotFoundException.
196 *
197 * @expectedException Shaarli\Api\Exceptions\ApiTagNotFoundException
198 * @expectedExceptionMessage Tag not found
199 */ 197 */
200 public function testPutTag404() 198 public function testPutTag404()
201 { 199 {
200 $this->expectException(\Shaarli\Api\Exceptions\ApiTagNotFoundException::class);
201 $this->expectExceptionMessage('Tag not found');
202
202 $env = Environment::mock([ 203 $env = Environment::mock([
203 'REQUEST_METHOD' => 'PUT', 204 'REQUEST_METHOD' => 'PUT',
204 ]); 205 ]);