]>
Commit | Line | Data |
---|---|---|
1 | <?php | |
2 | namespace Shaarli\Api\Controllers; | |
3 | ||
4 | use malkusch\lock\mutex\NoMutex; | |
5 | use Shaarli\Bookmark\BookmarkFileService; | |
6 | use Shaarli\Bookmark\LinkDB; | |
7 | use Shaarli\Config\ConfigManager; | |
8 | use Shaarli\History; | |
9 | use Shaarli\Plugin\PluginManager; | |
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 | */ | |
22 | class GetTagsTest extends \Shaarli\TestCase | |
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 | /** | |
45 | * @var BookmarkFileService instance. | |
46 | */ | |
47 | protected $bookmarkService; | |
48 | ||
49 | /** | |
50 | * @var Tags controller instance. | |
51 | */ | |
52 | protected $controller; | |
53 | ||
54 | /** @var PluginManager */ | |
55 | protected $pluginManager; | |
56 | ||
57 | /** | |
58 | * Number of JSON field per link. | |
59 | */ | |
60 | const NB_FIELDS_TAG = 2; | |
61 | ||
62 | /** | |
63 | * Before every test, instantiate a new Api with its config, plugins and bookmarks. | |
64 | */ | |
65 | protected function setUp(): void | |
66 | { | |
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( | |
75 | $this->conf, | |
76 | $this->pluginManager, | |
77 | $history, | |
78 | $mutex, | |
79 | true | |
80 | ); | |
81 | $this->container = new Container(); | |
82 | $this->container['conf'] = $this->conf; | |
83 | $this->container['db'] = $this->bookmarkService; | |
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 | */ | |
92 | protected function tearDown(): void | |
93 | { | |
94 | @unlink(self::$testDatastore); | |
95 | } | |
96 | ||
97 | /** | |
98 | * Test basic getTags service: returns all tags. | |
99 | */ | |
100 | public function testGetTagsAll() | |
101 | { | |
102 | $tags = $this->bookmarkService->bookmarksCountPerTag(); | |
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 | |
121 | $this->assertEquals(self::NB_FIELDS_TAG, count($data[5])); | |
122 | $this->assertEquals('sTuff', $data[5]['name']); | |
123 | $this->assertEquals(2, $data[5]['occurrences']); | |
124 | // End | |
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']); | |
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 | { | |
155 | $tags = $this->bookmarkService->bookmarksCountPerTag(); | |
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 | { | |
189 | $tags = $this->bookmarkService->bookmarksCountPerTag([], 'private'); | |
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])); | |
200 | $this->assertEquals('Mercurial', $data[0]['name']); | |
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 | { | |
209 | $tags = $this->bookmarkService->bookmarksCountPerTag([], 'public'); | |
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 | } |