2 namespace Shaarli\Api\Controllers
;
4 use malkusch\lock\mutex\NoMutex
;
5 use Shaarli\Bookmark\BookmarkFileService
;
6 use Shaarli\Bookmark\LinkDB
;
7 use Shaarli\Config\ConfigManager
;
10 use Slim\Http\Environment
;
11 use Slim\Http\Request
;
12 use Slim\Http\Response
;
17 * Test get tag list REST API service.
19 * @package Shaarli\Api\Controllers
21 class GetTagsTest
extends \Shaarli\TestCase
24 * @var string datastore to test write operations
26 protected static $testDatastore = 'sandbox/datastore.php';
29 * @var ConfigManager instance
34 * @var \ReferenceLinkDB instance.
36 protected $refDB = null;
39 * @var Container instance.
44 * @var BookmarkFileService instance.
46 protected $bookmarkService;
49 * @var Tags controller instance.
51 protected $controller;
54 * Number of JSON field per link.
56 const NB_FIELDS_TAG
= 2;
59 * Before every test, instantiate a new Api with its config, plugins and bookmarks.
61 protected function setUp(): void
63 $mutex = new NoMutex();
64 $this->conf
= new ConfigManager('tests/utils/config/configJson');
65 $this->conf
->set('resource.datastore', self
::$testDatastore);
66 $this->refDB
= new \
ReferenceLinkDB();
67 $this->refDB
->write(self
::$testDatastore);
68 $history = new History('sandbox/history.php');
70 $this->bookmarkService
= new BookmarkFileService($this->conf
, $history, $mutex, true);
72 $this->container
= new Container();
73 $this->container
['conf'] = $this->conf
;
74 $this->container
['db'] = $this->bookmarkService
;
75 $this->container
['history'] = null;
77 $this->controller
= new Tags($this->container
);
81 * After every test, remove the test datastore.
83 protected function tearDown(): void
85 @unlink(self
::$testDatastore);
89 * Test basic getTags service: returns all tags.
91 public function testGetTagsAll()
93 $tags = $this->bookmarkService
->bookmarksCountPerTag();
94 $env = Environment
::mock([
95 'REQUEST_METHOD' => 'GET',
97 $request = Request
::createFromEnvironment($env);
99 $response = $this->controller
->getTags($request, new Response());
100 $this->assertEquals(200, $response->getStatusCode());
101 $data = json_decode((string) $response->getBody(), true);
102 $this->assertEquals(count($tags), count($data));
105 $this->assertEquals(self
::NB_FIELDS_TAG
, count($data[0]));
106 $this->assertEquals('web', $data[0]['name']);
107 $this->assertEquals(4, $data[0]['occurrences']);
108 $this->assertEquals(self
::NB_FIELDS_TAG
, count($data[1]));
109 $this->assertEquals('cartoon', $data[1]['name']);
110 $this->assertEquals(3, $data[1]['occurrences']);
112 $this->assertEquals(self
::NB_FIELDS_TAG
, count($data[5]));
113 $this->assertEquals('sTuff', $data[5]['name']);
114 $this->assertEquals(2, $data[5]['occurrences']);
116 $this->assertEquals(self
::NB_FIELDS_TAG
, count($data[count($data) - 1]));
117 $this->assertEquals('w3c', $data[count($data) - 1]['name']);
118 $this->assertEquals(1, $data[count($data) - 1]['occurrences']);
122 * Test getTags service with offset and limit parameter:
123 * limit=1 and offset=1 should return only the second tag, cartoon with 3 occurrences
125 public function testGetTagsOffsetLimit()
127 $env = Environment
::mock([
128 'REQUEST_METHOD' => 'GET',
129 'QUERY_STRING' => 'offset=1&limit=1'
131 $request = Request
::createFromEnvironment($env);
132 $response = $this->controller
->getTags($request, new Response());
133 $this->assertEquals(200, $response->getStatusCode());
134 $data = json_decode((string) $response->getBody(), true);
135 $this->assertEquals(1, count($data));
136 $this->assertEquals(self
::NB_FIELDS_TAG
, count($data[0]));
137 $this->assertEquals('cartoon', $data[0]['name']);
138 $this->assertEquals(3, $data[0]['occurrences']);
142 * Test getTags with limit=all (return all tags).
144 public function testGetTagsLimitAll()
146 $tags = $this->bookmarkService
->bookmarksCountPerTag();
147 $env = Environment
::mock([
148 'REQUEST_METHOD' => 'GET',
149 'QUERY_STRING' => 'limit=all'
151 $request = Request
::createFromEnvironment($env);
152 $response = $this->controller
->getTags($request, new Response());
153 $this->assertEquals(200, $response->getStatusCode());
154 $data = json_decode((string) $response->getBody(), true);
155 $this->assertEquals(count($tags), count($data));
159 * Test getTags service with offset and limit parameter:
160 * limit=1 and offset=1 should not return any tag
162 public function testGetTagsOffsetTooHigh()
164 $env = Environment
::mock([
165 'REQUEST_METHOD' => 'GET',
166 'QUERY_STRING' => 'offset=100'
168 $request = Request
::createFromEnvironment($env);
169 $response = $this->controller
->getTags($request, new Response());
170 $this->assertEquals(200, $response->getStatusCode());
171 $data = json_decode((string) $response->getBody(), true);
172 $this->assertEmpty(count($data));
176 * Test getTags with visibility parameter set to private
178 public function testGetTagsVisibilityPrivate()
180 $tags = $this->bookmarkService
->bookmarksCountPerTag([], 'private');
181 $env = Environment
::mock([
182 'REQUEST_METHOD' => 'GET',
183 'QUERY_STRING' => 'visibility=private'
185 $request = Request
::createFromEnvironment($env);
186 $response = $this->controller
->getTags($request, new Response());
187 $this->assertEquals(200, $response->getStatusCode());
188 $data = json_decode((string) $response->getBody(), true);
189 $this->assertEquals(count($tags), count($data));
190 $this->assertEquals(self
::NB_FIELDS_TAG
, count($data[0]));
191 $this->assertEquals('Mercurial', $data[0]['name']);
192 $this->assertEquals(1, $data[0]['occurrences']);
196 * Test getTags with visibility parameter set to public
198 public function testGetTagsVisibilityPublic()
200 $tags = $this->bookmarkService
->bookmarksCountPerTag([], 'public');
201 $env = Environment
::mock(
203 'REQUEST_METHOD' => 'GET',
204 'QUERY_STRING' => 'visibility=public'
207 $request = Request
::createFromEnvironment($env);
208 $response = $this->controller
->getTags($request, new Response());
209 $this->assertEquals(200, $response->getStatusCode());
210 $data = json_decode((string)$response->getBody(), true);
211 $this->assertEquals(count($tags), count($data));
212 $this->assertEquals(self
::NB_FIELDS_TAG
, count($data[0]));
213 $this->assertEquals('web', $data[0]['name']);
214 $this->assertEquals(3, $data[0]['occurrences']);