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
;
9 use Shaarli\Plugin\PluginManager
;
11 use Slim\Http\Environment
;
12 use Slim\Http\Request
;
13 use Slim\Http\Response
;
18 * Test get tag list REST API service.
20 * @package Shaarli\Api\Controllers
22 class GetTagsTest
extends \Shaarli\TestCase
25 * @var string datastore to test write operations
27 protected static $testDatastore = 'sandbox/datastore.php';
30 * @var ConfigManager instance
35 * @var \ReferenceLinkDB instance.
37 protected $refDB = null;
40 * @var Container instance.
45 * @var BookmarkFileService instance.
47 protected $bookmarkService;
50 * @var Tags controller instance.
52 protected $controller;
54 /** @var PluginManager */
55 protected $pluginManager;
58 * Number of JSON field per link.
60 const NB_FIELDS_TAG
= 2;
63 * Before every test, instantiate a new Api with its config, plugins and bookmarks.
65 protected function setUp(): void
67 $mutex = new NoMutex();
68 $this->conf
= new ConfigManager('tests/utils/config/configJson');
69 $this->conf
->set('resource.datastore', self
::$testDatastore);
70 $this->refDB
= new \
ReferenceLinkDB();
71 $this->refDB
->write(self
::$testDatastore);
72 $history = new History('sandbox/history.php');
73 $this->pluginManager
= new PluginManager($this->conf
);
74 $this->bookmarkService
= new BookmarkFileService(
81 $this->container
= new Container();
82 $this->container
['conf'] = $this->conf
;
83 $this->container
['db'] = $this->bookmarkService
;
84 $this->container
['history'] = null;
86 $this->controller
= new Tags($this->container
);
90 * After every test, remove the test datastore.
92 protected function tearDown(): void
94 @unlink(self
::$testDatastore);
98 * Test basic getTags service: returns all tags.
100 public function testGetTagsAll()
102 $tags = $this->bookmarkService
->bookmarksCountPerTag();
103 $env = Environment
::mock([
104 'REQUEST_METHOD' => 'GET',
106 $request = Request
::createFromEnvironment($env);
108 $response = $this->controller
->getTags($request, new Response());
109 $this->assertEquals(200, $response->getStatusCode());
110 $data = json_decode((string) $response->getBody(), true);
111 $this->assertEquals(count($tags), count($data));
114 $this->assertEquals(self
::NB_FIELDS_TAG
, count($data[0]));
115 $this->assertEquals('web', $data[0]['name']);
116 $this->assertEquals(4, $data[0]['occurrences']);
117 $this->assertEquals(self
::NB_FIELDS_TAG
, count($data[1]));
118 $this->assertEquals('cartoon', $data[1]['name']);
119 $this->assertEquals(3, $data[1]['occurrences']);
121 $this->assertEquals(self
::NB_FIELDS_TAG
, count($data[5]));
122 $this->assertEquals('sTuff', $data[5]['name']);
123 $this->assertEquals(2, $data[5]['occurrences']);
125 $this->assertEquals(self
::NB_FIELDS_TAG
, count($data[count($data) - 1]));
126 $this->assertEquals('w3c', $data[count($data) - 1]['name']);
127 $this->assertEquals(1, $data[count($data) - 1]['occurrences']);
131 * Test getTags service with offset and limit parameter:
132 * limit=1 and offset=1 should return only the second tag, cartoon with 3 occurrences
134 public function testGetTagsOffsetLimit()
136 $env = Environment
::mock([
137 'REQUEST_METHOD' => 'GET',
138 'QUERY_STRING' => 'offset=1&limit=1'
140 $request = Request
::createFromEnvironment($env);
141 $response = $this->controller
->getTags($request, new Response());
142 $this->assertEquals(200, $response->getStatusCode());
143 $data = json_decode((string) $response->getBody(), true);
144 $this->assertEquals(1, count($data));
145 $this->assertEquals(self
::NB_FIELDS_TAG
, count($data[0]));
146 $this->assertEquals('cartoon', $data[0]['name']);
147 $this->assertEquals(3, $data[0]['occurrences']);
151 * Test getTags with limit=all (return all tags).
153 public function testGetTagsLimitAll()
155 $tags = $this->bookmarkService
->bookmarksCountPerTag();
156 $env = Environment
::mock([
157 'REQUEST_METHOD' => 'GET',
158 'QUERY_STRING' => 'limit=all'
160 $request = Request
::createFromEnvironment($env);
161 $response = $this->controller
->getTags($request, new Response());
162 $this->assertEquals(200, $response->getStatusCode());
163 $data = json_decode((string) $response->getBody(), true);
164 $this->assertEquals(count($tags), count($data));
168 * Test getTags service with offset and limit parameter:
169 * limit=1 and offset=1 should not return any tag
171 public function testGetTagsOffsetTooHigh()
173 $env = Environment
::mock([
174 'REQUEST_METHOD' => 'GET',
175 'QUERY_STRING' => 'offset=100'
177 $request = Request
::createFromEnvironment($env);
178 $response = $this->controller
->getTags($request, new Response());
179 $this->assertEquals(200, $response->getStatusCode());
180 $data = json_decode((string) $response->getBody(), true);
181 $this->assertEmpty(count($data));
185 * Test getTags with visibility parameter set to private
187 public function testGetTagsVisibilityPrivate()
189 $tags = $this->bookmarkService
->bookmarksCountPerTag([], 'private');
190 $env = Environment
::mock([
191 'REQUEST_METHOD' => 'GET',
192 'QUERY_STRING' => 'visibility=private'
194 $request = Request
::createFromEnvironment($env);
195 $response = $this->controller
->getTags($request, new Response());
196 $this->assertEquals(200, $response->getStatusCode());
197 $data = json_decode((string) $response->getBody(), true);
198 $this->assertEquals(count($tags), count($data));
199 $this->assertEquals(self
::NB_FIELDS_TAG
, count($data[0]));
200 $this->assertEquals('Mercurial', $data[0]['name']);
201 $this->assertEquals(1, $data[0]['occurrences']);
205 * Test getTags with visibility parameter set to public
207 public function testGetTagsVisibilityPublic()
209 $tags = $this->bookmarkService
->bookmarksCountPerTag([], 'public');
210 $env = Environment
::mock(
212 'REQUEST_METHOD' => 'GET',
213 'QUERY_STRING' => 'visibility=public'
216 $request = Request
::createFromEnvironment($env);
217 $response = $this->controller
->getTags($request, new Response());
218 $this->assertEquals(200, $response->getStatusCode());
219 $data = json_decode((string)$response->getBody(), true);
220 $this->assertEquals(count($tags), count($data));
221 $this->assertEquals(self
::NB_FIELDS_TAG
, count($data[0]));
222 $this->assertEquals('web', $data[0]['name']);
223 $this->assertEquals(3, $data[0]['occurrences']);