]>
Commit | Line | Data |
---|---|---|
d3f42ca4 A |
1 | <?php |
2 | namespace Shaarli\Api\Controllers; | |
3 | ||
fd1ddad9 | 4 | use malkusch\lock\mutex\NoMutex; |
e26e2060 | 5 | use Shaarli\Bookmark\BookmarkFileService; |
dea72c71 | 6 | use Shaarli\Bookmark\LinkDB; |
d3f42ca4 | 7 | use Shaarli\Config\ConfigManager; |
e26e2060 | 8 | use Shaarli\History; |
bcba6bd3 | 9 | use Shaarli\Plugin\PluginManager; |
d3f42ca4 A |
10 | use Slim\Container; |
11 | use Slim\Http\Environment; | |
12 | use Slim\Http\Request; | |
13 | use Slim\Http\Response; | |
14 | ||
15 | /** | |
16 | * Class GetTagsTest | |
17 | * | |
18 | * Test get tag list REST API service. | |
19 | * | |
20 | * @package Shaarli\Api\Controllers | |
21 | */ | |
a5a9cf23 | 22 | class GetTagsTest extends \Shaarli\TestCase |
d3f42ca4 A |
23 | { |
24 | /** | |
25 | * @var string datastore to test write operations | |
26 | */ | |
27 | protected static $testDatastore = 'sandbox/datastore.php'; | |
28 | ||
29 | /** | |
30 | * @var ConfigManager instance | |
31 | */ | |
32 | protected $conf; | |
33 | ||
34 | /** | |
35 | * @var \ReferenceLinkDB instance. | |
36 | */ | |
37 | protected $refDB = null; | |
38 | ||
39 | /** | |
40 | * @var Container instance. | |
41 | */ | |
42 | protected $container; | |
43 | ||
44 | /** | |
e26e2060 | 45 | * @var BookmarkFileService instance. |
d3f42ca4 | 46 | */ |
e26e2060 | 47 | protected $bookmarkService; |
d3f42ca4 A |
48 | |
49 | /** | |
50 | * @var Tags controller instance. | |
51 | */ | |
52 | protected $controller; | |
53 | ||
bcba6bd3 A |
54 | /** @var PluginManager */ |
55 | protected $pluginManager; | |
56 | ||
d3f42ca4 A |
57 | /** |
58 | * Number of JSON field per link. | |
59 | */ | |
60 | const NB_FIELDS_TAG = 2; | |
61 | ||
62 | /** | |
e26e2060 | 63 | * Before every test, instantiate a new Api with its config, plugins and bookmarks. |
d3f42ca4 | 64 | */ |
8f60e120 | 65 | protected function setUp(): void |
d3f42ca4 | 66 | { |
fd1ddad9 | 67 | $mutex = new NoMutex(); |
d3f42ca4 | 68 | $this->conf = new ConfigManager('tests/utils/config/configJson'); |
e26e2060 | 69 | $this->conf->set('resource.datastore', self::$testDatastore); |
d3f42ca4 A |
70 | $this->refDB = new \ReferenceLinkDB(); |
71 | $this->refDB->write(self::$testDatastore); | |
e26e2060 | 72 | $history = new History('sandbox/history.php'); |
bcba6bd3 A |
73 | $this->pluginManager = new PluginManager($this->conf); |
74 | $this->bookmarkService = new BookmarkFileService( | |
75 | $this->conf, | |
76 | $this->pluginManager, | |
77 | $history, | |
78 | $mutex, | |
79 | true | |
80 | ); | |
d3f42ca4 A |
81 | $this->container = new Container(); |
82 | $this->container['conf'] = $this->conf; | |
e26e2060 | 83 | $this->container['db'] = $this->bookmarkService; |
d3f42ca4 A |
84 | $this->container['history'] = null; |
85 | ||
86 | $this->controller = new Tags($this->container); | |
87 | } | |
88 | ||
89 | /** | |
90 | * After every test, remove the test datastore. | |
91 | */ | |
8f60e120 | 92 | protected function tearDown(): void |
d3f42ca4 A |
93 | { |
94 | @unlink(self::$testDatastore); | |
95 | } | |
96 | ||
97 | /** | |
7c57bd95 | 98 | * Test basic getTags service: returns all tags. |
d3f42ca4 A |
99 | */ |
100 | public function testGetTagsAll() | |
101 | { | |
e26e2060 | 102 | $tags = $this->bookmarkService->bookmarksCountPerTag(); |
d3f42ca4 A |
103 | $env = Environment::mock([ |
104 | 'REQUEST_METHOD' => 'GET', | |
105 | ]); | |
106 | $request = Request::createFromEnvironment($env); | |
107 | ||
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)); | |
112 | ||
113 | // Check order | |
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']); | |
120 | // Case insensitive | |
7c57bd95 A |
121 | $this->assertEquals(self::NB_FIELDS_TAG, count($data[5])); |
122 | $this->assertEquals('sTuff', $data[5]['name']); | |
123 | $this->assertEquals(2, $data[5]['occurrences']); | |
d3f42ca4 A |
124 | // End |
125 | $this->assertEquals(self::NB_FIELDS_TAG, count($data[count($data) - 1])); | |
7c57bd95 | 126 | $this->assertEquals('w3c', $data[count($data) - 1]['name']); |
d3f42ca4 A |
127 | $this->assertEquals(1, $data[count($data) - 1]['occurrences']); |
128 | } | |
129 | ||
130 | /** | |
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 | |
133 | */ | |
134 | public function testGetTagsOffsetLimit() | |
135 | { | |
136 | $env = Environment::mock([ | |
137 | 'REQUEST_METHOD' => 'GET', | |
138 | 'QUERY_STRING' => 'offset=1&limit=1' | |
139 | ]); | |
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']); | |
148 | } | |
149 | ||
150 | /** | |
151 | * Test getTags with limit=all (return all tags). | |
152 | */ | |
153 | public function testGetTagsLimitAll() | |
154 | { | |
e26e2060 | 155 | $tags = $this->bookmarkService->bookmarksCountPerTag(); |
d3f42ca4 A |
156 | $env = Environment::mock([ |
157 | 'REQUEST_METHOD' => 'GET', | |
158 | 'QUERY_STRING' => 'limit=all' | |
159 | ]); | |
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)); | |
165 | } | |
166 | ||
167 | /** | |
168 | * Test getTags service with offset and limit parameter: | |
169 | * limit=1 and offset=1 should not return any tag | |
170 | */ | |
171 | public function testGetTagsOffsetTooHigh() | |
172 | { | |
173 | $env = Environment::mock([ | |
174 | 'REQUEST_METHOD' => 'GET', | |
175 | 'QUERY_STRING' => 'offset=100' | |
176 | ]); | |
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)); | |
182 | } | |
183 | ||
184 | /** | |
185 | * Test getTags with visibility parameter set to private | |
186 | */ | |
187 | public function testGetTagsVisibilityPrivate() | |
188 | { | |
e26e2060 | 189 | $tags = $this->bookmarkService->bookmarksCountPerTag([], 'private'); |
d3f42ca4 A |
190 | $env = Environment::mock([ |
191 | 'REQUEST_METHOD' => 'GET', | |
192 | 'QUERY_STRING' => 'visibility=private' | |
193 | ]); | |
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])); | |
7c57bd95 | 200 | $this->assertEquals('Mercurial', $data[0]['name']); |
d3f42ca4 A |
201 | $this->assertEquals(1, $data[0]['occurrences']); |
202 | } | |
203 | ||
204 | /** | |
205 | * Test getTags with visibility parameter set to public | |
206 | */ | |
207 | public function testGetTagsVisibilityPublic() | |
208 | { | |
e26e2060 | 209 | $tags = $this->bookmarkService->bookmarksCountPerTag([], 'public'); |
d3f42ca4 A |
210 | $env = Environment::mock( |
211 | [ | |
212 | 'REQUEST_METHOD' => 'GET', | |
213 | 'QUERY_STRING' => 'visibility=public' | |
214 | ] | |
215 | ); | |
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']); | |
224 | } | |
225 | } |