2 namespace Shaarli\Api\Controllers
;
4 use Shaarli\Bookmark\BookmarkFileService
;
5 use Shaarli\Bookmark\LinkDB
;
6 use Shaarli\Config\ConfigManager
;
9 use Slim\Http\Environment
;
10 use Slim\Http\Request
;
11 use Slim\Http\Response
;
16 * Test get tag list REST API service.
18 * @package Shaarli\Api\Controllers
20 class GetTagsTest
extends \PHPUnit\Framework\TestCase
23 * @var string datastore to test write operations
25 protected static $testDatastore = 'sandbox/datastore.php';
28 * @var ConfigManager instance
33 * @var \ReferenceLinkDB instance.
35 protected $refDB = null;
38 * @var Container instance.
43 * @var BookmarkFileService instance.
45 protected $bookmarkService;
48 * @var Tags controller instance.
50 protected $controller;
53 * Number of JSON field per link.
55 const NB_FIELDS_TAG
= 2;
58 * Before every test, instantiate a new Api with its config, plugins and bookmarks.
60 protected function setUp(): void
62 $this->conf
= new ConfigManager('tests/utils/config/configJson');
63 $this->conf
->set('resource.datastore', self
::$testDatastore);
64 $this->refDB
= new \
ReferenceLinkDB();
65 $this->refDB
->write(self
::$testDatastore);
66 $history = new History('sandbox/history.php');
68 $this->bookmarkService
= new BookmarkFileService($this->conf
, $history, true);
70 $this->container
= new Container();
71 $this->container
['conf'] = $this->conf
;
72 $this->container
['db'] = $this->bookmarkService
;
73 $this->container
['history'] = null;
75 $this->controller
= new Tags($this->container
);
79 * After every test, remove the test datastore.
81 protected function tearDown(): void
83 @unlink(self
::$testDatastore);
87 * Test basic getTags service: returns all tags.
89 public function testGetTagsAll()
91 $tags = $this->bookmarkService
->bookmarksCountPerTag();
92 $env = Environment
::mock([
93 'REQUEST_METHOD' => 'GET',
95 $request = Request
::createFromEnvironment($env);
97 $response = $this->controller
->getTags($request, new Response());
98 $this->assertEquals(200, $response->getStatusCode());
99 $data = json_decode((string) $response->getBody(), true);
100 $this->assertEquals(count($tags), count($data));
103 $this->assertEquals(self
::NB_FIELDS_TAG
, count($data[0]));
104 $this->assertEquals('web', $data[0]['name']);
105 $this->assertEquals(4, $data[0]['occurrences']);
106 $this->assertEquals(self
::NB_FIELDS_TAG
, count($data[1]));
107 $this->assertEquals('cartoon', $data[1]['name']);
108 $this->assertEquals(3, $data[1]['occurrences']);
110 $this->assertEquals(self
::NB_FIELDS_TAG
, count($data[5]));
111 $this->assertEquals('sTuff', $data[5]['name']);
112 $this->assertEquals(2, $data[5]['occurrences']);
114 $this->assertEquals(self
::NB_FIELDS_TAG
, count($data[count($data) - 1]));
115 $this->assertEquals('w3c', $data[count($data) - 1]['name']);
116 $this->assertEquals(1, $data[count($data) - 1]['occurrences']);
120 * Test getTags service with offset and limit parameter:
121 * limit=1 and offset=1 should return only the second tag, cartoon with 3 occurrences
123 public function testGetTagsOffsetLimit()
125 $env = Environment
::mock([
126 'REQUEST_METHOD' => 'GET',
127 'QUERY_STRING' => 'offset=1&limit=1'
129 $request = Request
::createFromEnvironment($env);
130 $response = $this->controller
->getTags($request, new Response());
131 $this->assertEquals(200, $response->getStatusCode());
132 $data = json_decode((string) $response->getBody(), true);
133 $this->assertEquals(1, count($data));
134 $this->assertEquals(self
::NB_FIELDS_TAG
, count($data[0]));
135 $this->assertEquals('cartoon', $data[0]['name']);
136 $this->assertEquals(3, $data[0]['occurrences']);
140 * Test getTags with limit=all (return all tags).
142 public function testGetTagsLimitAll()
144 $tags = $this->bookmarkService
->bookmarksCountPerTag();
145 $env = Environment
::mock([
146 'REQUEST_METHOD' => 'GET',
147 'QUERY_STRING' => 'limit=all'
149 $request = Request
::createFromEnvironment($env);
150 $response = $this->controller
->getTags($request, new Response());
151 $this->assertEquals(200, $response->getStatusCode());
152 $data = json_decode((string) $response->getBody(), true);
153 $this->assertEquals(count($tags), count($data));
157 * Test getTags service with offset and limit parameter:
158 * limit=1 and offset=1 should not return any tag
160 public function testGetTagsOffsetTooHigh()
162 $env = Environment
::mock([
163 'REQUEST_METHOD' => 'GET',
164 'QUERY_STRING' => 'offset=100'
166 $request = Request
::createFromEnvironment($env);
167 $response = $this->controller
->getTags($request, new Response());
168 $this->assertEquals(200, $response->getStatusCode());
169 $data = json_decode((string) $response->getBody(), true);
170 $this->assertEmpty(count($data));
174 * Test getTags with visibility parameter set to private
176 public function testGetTagsVisibilityPrivate()
178 $tags = $this->bookmarkService
->bookmarksCountPerTag([], 'private');
179 $env = Environment
::mock([
180 'REQUEST_METHOD' => 'GET',
181 'QUERY_STRING' => 'visibility=private'
183 $request = Request
::createFromEnvironment($env);
184 $response = $this->controller
->getTags($request, new Response());
185 $this->assertEquals(200, $response->getStatusCode());
186 $data = json_decode((string) $response->getBody(), true);
187 $this->assertEquals(count($tags), count($data));
188 $this->assertEquals(self
::NB_FIELDS_TAG
, count($data[0]));
189 $this->assertEquals('Mercurial', $data[0]['name']);
190 $this->assertEquals(1, $data[0]['occurrences']);
194 * Test getTags with visibility parameter set to public
196 public function testGetTagsVisibilityPublic()
198 $tags = $this->bookmarkService
->bookmarksCountPerTag([], 'public');
199 $env = Environment
::mock(
201 'REQUEST_METHOD' => 'GET',
202 'QUERY_STRING' => 'visibility=public'
205 $request = Request
::createFromEnvironment($env);
206 $response = $this->controller
->getTags($request, new Response());
207 $this->assertEquals(200, $response->getStatusCode());
208 $data = json_decode((string)$response->getBody(), true);
209 $this->assertEquals(count($tags), count($data));
210 $this->assertEquals(self
::NB_FIELDS_TAG
, count($data[0]));
211 $this->assertEquals('web', $data[0]['name']);
212 $this->assertEquals(3, $data[0]['occurrences']);