]>
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 Slim\Container; | |
10 | use Slim\Http\Environment; | |
11 | use Slim\Http\Request; | |
12 | use Slim\Http\Response; | |
13 | ||
14 | /** | |
15 | * Class GetTagsTest | |
16 | * | |
17 | * Test get tag list REST API service. | |
18 | * | |
19 | * @package Shaarli\Api\Controllers | |
20 | */ | |
21 | class GetTagsTest extends \Shaarli\TestCase | |
22 | { | |
23 | /** | |
24 | * @var string datastore to test write operations | |
25 | */ | |
26 | protected static $testDatastore = 'sandbox/datastore.php'; | |
27 | ||
28 | /** | |
29 | * @var ConfigManager instance | |
30 | */ | |
31 | protected $conf; | |
32 | ||
33 | /** | |
34 | * @var \ReferenceLinkDB instance. | |
35 | */ | |
36 | protected $refDB = null; | |
37 | ||
38 | /** | |
39 | * @var Container instance. | |
40 | */ | |
41 | protected $container; | |
42 | ||
43 | /** | |
44 | * @var BookmarkFileService instance. | |
45 | */ | |
46 | protected $bookmarkService; | |
47 | ||
48 | /** | |
49 | * @var Tags controller instance. | |
50 | */ | |
51 | protected $controller; | |
52 | ||
53 | /** | |
54 | * Number of JSON field per link. | |
55 | */ | |
56 | const NB_FIELDS_TAG = 2; | |
57 | ||
58 | /** | |
59 | * Before every test, instantiate a new Api with its config, plugins and bookmarks. | |
60 | */ | |
61 | protected function setUp(): void | |
62 | { | |
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'); | |
69 | ||
70 | $this->bookmarkService = new BookmarkFileService($this->conf, $history, $mutex, true); | |
71 | ||
72 | $this->container = new Container(); | |
73 | $this->container['conf'] = $this->conf; | |
74 | $this->container['db'] = $this->bookmarkService; | |
75 | $this->container['history'] = null; | |
76 | ||
77 | $this->controller = new Tags($this->container); | |
78 | } | |
79 | ||
80 | /** | |
81 | * After every test, remove the test datastore. | |
82 | */ | |
83 | protected function tearDown(): void | |
84 | { | |
85 | @unlink(self::$testDatastore); | |
86 | } | |
87 | ||
88 | /** | |
89 | * Test basic getTags service: returns all tags. | |
90 | */ | |
91 | public function testGetTagsAll() | |
92 | { | |
93 | $tags = $this->bookmarkService->bookmarksCountPerTag(); | |
94 | $env = Environment::mock([ | |
95 | 'REQUEST_METHOD' => 'GET', | |
96 | ]); | |
97 | $request = Request::createFromEnvironment($env); | |
98 | ||
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)); | |
103 | ||
104 | // Check order | |
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']); | |
111 | // Case insensitive | |
112 | $this->assertEquals(self::NB_FIELDS_TAG, count($data[5])); | |
113 | $this->assertEquals('sTuff', $data[5]['name']); | |
114 | $this->assertEquals(2, $data[5]['occurrences']); | |
115 | // End | |
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']); | |
119 | } | |
120 | ||
121 | /** | |
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 | |
124 | */ | |
125 | public function testGetTagsOffsetLimit() | |
126 | { | |
127 | $env = Environment::mock([ | |
128 | 'REQUEST_METHOD' => 'GET', | |
129 | 'QUERY_STRING' => 'offset=1&limit=1' | |
130 | ]); | |
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']); | |
139 | } | |
140 | ||
141 | /** | |
142 | * Test getTags with limit=all (return all tags). | |
143 | */ | |
144 | public function testGetTagsLimitAll() | |
145 | { | |
146 | $tags = $this->bookmarkService->bookmarksCountPerTag(); | |
147 | $env = Environment::mock([ | |
148 | 'REQUEST_METHOD' => 'GET', | |
149 | 'QUERY_STRING' => 'limit=all' | |
150 | ]); | |
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)); | |
156 | } | |
157 | ||
158 | /** | |
159 | * Test getTags service with offset and limit parameter: | |
160 | * limit=1 and offset=1 should not return any tag | |
161 | */ | |
162 | public function testGetTagsOffsetTooHigh() | |
163 | { | |
164 | $env = Environment::mock([ | |
165 | 'REQUEST_METHOD' => 'GET', | |
166 | 'QUERY_STRING' => 'offset=100' | |
167 | ]); | |
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)); | |
173 | } | |
174 | ||
175 | /** | |
176 | * Test getTags with visibility parameter set to private | |
177 | */ | |
178 | public function testGetTagsVisibilityPrivate() | |
179 | { | |
180 | $tags = $this->bookmarkService->bookmarksCountPerTag([], 'private'); | |
181 | $env = Environment::mock([ | |
182 | 'REQUEST_METHOD' => 'GET', | |
183 | 'QUERY_STRING' => 'visibility=private' | |
184 | ]); | |
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']); | |
193 | } | |
194 | ||
195 | /** | |
196 | * Test getTags with visibility parameter set to public | |
197 | */ | |
198 | public function testGetTagsVisibilityPublic() | |
199 | { | |
200 | $tags = $this->bookmarkService->bookmarksCountPerTag([], 'public'); | |
201 | $env = Environment::mock( | |
202 | [ | |
203 | 'REQUEST_METHOD' => 'GET', | |
204 | 'QUERY_STRING' => 'visibility=public' | |
205 | ] | |
206 | ); | |
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']); | |
215 | } | |
216 | } |