]>
Commit | Line | Data |
---|---|---|
d3f42ca4 A |
1 | <?php |
2 | namespace Shaarli\Api\Controllers; | |
3 | ||
4 | use Shaarli\Config\ConfigManager; | |
5 | ||
6 | use Slim\Container; | |
7 | use Slim\Http\Environment; | |
8 | use Slim\Http\Request; | |
9 | use Slim\Http\Response; | |
10 | ||
11 | /** | |
12 | * Class GetTagsTest | |
13 | * | |
14 | * Test get tag list REST API service. | |
15 | * | |
16 | * @package Shaarli\Api\Controllers | |
17 | */ | |
18 | class GetTagsTest extends \PHPUnit_Framework_TestCase | |
19 | { | |
20 | /** | |
21 | * @var string datastore to test write operations | |
22 | */ | |
23 | protected static $testDatastore = 'sandbox/datastore.php'; | |
24 | ||
25 | /** | |
26 | * @var ConfigManager instance | |
27 | */ | |
28 | protected $conf; | |
29 | ||
30 | /** | |
31 | * @var \ReferenceLinkDB instance. | |
32 | */ | |
33 | protected $refDB = null; | |
34 | ||
35 | /** | |
36 | * @var Container instance. | |
37 | */ | |
38 | protected $container; | |
39 | ||
40 | /** | |
41 | * @var \LinkDB instance. | |
42 | */ | |
43 | protected $linkDB; | |
44 | ||
45 | /** | |
46 | * @var Tags controller instance. | |
47 | */ | |
48 | protected $controller; | |
49 | ||
50 | /** | |
51 | * Number of JSON field per link. | |
52 | */ | |
53 | const NB_FIELDS_TAG = 2; | |
54 | ||
55 | /** | |
56 | * Before every test, instantiate a new Api with its config, plugins and links. | |
57 | */ | |
58 | public function setUp() | |
59 | { | |
60 | $this->conf = new ConfigManager('tests/utils/config/configJson'); | |
61 | $this->refDB = new \ReferenceLinkDB(); | |
62 | $this->refDB->write(self::$testDatastore); | |
63 | ||
64 | $this->container = new Container(); | |
65 | $this->container['conf'] = $this->conf; | |
66 | $this->linkDB = new \LinkDB(self::$testDatastore, true, false); | |
67 | $this->container['db'] = $this->linkDB; | |
68 | $this->container['history'] = null; | |
69 | ||
70 | $this->controller = new Tags($this->container); | |
71 | } | |
72 | ||
73 | /** | |
74 | * After every test, remove the test datastore. | |
75 | */ | |
76 | public function tearDown() | |
77 | { | |
78 | @unlink(self::$testDatastore); | |
79 | } | |
80 | ||
81 | /** | |
7c57bd95 | 82 | * Test basic getTags service: returns all tags. |
d3f42ca4 A |
83 | */ |
84 | public function testGetTagsAll() | |
85 | { | |
86 | $tags = $this->linkDB->linksCountPerTag(); | |
87 | $env = Environment::mock([ | |
88 | 'REQUEST_METHOD' => 'GET', | |
89 | ]); | |
90 | $request = Request::createFromEnvironment($env); | |
91 | ||
92 | $response = $this->controller->getTags($request, new Response()); | |
93 | $this->assertEquals(200, $response->getStatusCode()); | |
94 | $data = json_decode((string) $response->getBody(), true); | |
95 | $this->assertEquals(count($tags), count($data)); | |
96 | ||
97 | // Check order | |
98 | $this->assertEquals(self::NB_FIELDS_TAG, count($data[0])); | |
99 | $this->assertEquals('web', $data[0]['name']); | |
100 | $this->assertEquals(4, $data[0]['occurrences']); | |
101 | $this->assertEquals(self::NB_FIELDS_TAG, count($data[1])); | |
102 | $this->assertEquals('cartoon', $data[1]['name']); | |
103 | $this->assertEquals(3, $data[1]['occurrences']); | |
104 | // Case insensitive | |
7c57bd95 A |
105 | $this->assertEquals(self::NB_FIELDS_TAG, count($data[5])); |
106 | $this->assertEquals('sTuff', $data[5]['name']); | |
107 | $this->assertEquals(2, $data[5]['occurrences']); | |
d3f42ca4 A |
108 | // End |
109 | $this->assertEquals(self::NB_FIELDS_TAG, count($data[count($data) - 1])); | |
7c57bd95 | 110 | $this->assertEquals('w3c', $data[count($data) - 1]['name']); |
d3f42ca4 A |
111 | $this->assertEquals(1, $data[count($data) - 1]['occurrences']); |
112 | } | |
113 | ||
114 | /** | |
115 | * Test getTags service with offset and limit parameter: | |
116 | * limit=1 and offset=1 should return only the second tag, cartoon with 3 occurrences | |
117 | */ | |
118 | public function testGetTagsOffsetLimit() | |
119 | { | |
120 | $env = Environment::mock([ | |
121 | 'REQUEST_METHOD' => 'GET', | |
122 | 'QUERY_STRING' => 'offset=1&limit=1' | |
123 | ]); | |
124 | $request = Request::createFromEnvironment($env); | |
125 | $response = $this->controller->getTags($request, new Response()); | |
126 | $this->assertEquals(200, $response->getStatusCode()); | |
127 | $data = json_decode((string) $response->getBody(), true); | |
128 | $this->assertEquals(1, count($data)); | |
129 | $this->assertEquals(self::NB_FIELDS_TAG, count($data[0])); | |
130 | $this->assertEquals('cartoon', $data[0]['name']); | |
131 | $this->assertEquals(3, $data[0]['occurrences']); | |
132 | } | |
133 | ||
134 | /** | |
135 | * Test getTags with limit=all (return all tags). | |
136 | */ | |
137 | public function testGetTagsLimitAll() | |
138 | { | |
139 | $tags = $this->linkDB->linksCountPerTag(); | |
140 | $env = Environment::mock([ | |
141 | 'REQUEST_METHOD' => 'GET', | |
142 | 'QUERY_STRING' => 'limit=all' | |
143 | ]); | |
144 | $request = Request::createFromEnvironment($env); | |
145 | $response = $this->controller->getTags($request, new Response()); | |
146 | $this->assertEquals(200, $response->getStatusCode()); | |
147 | $data = json_decode((string) $response->getBody(), true); | |
148 | $this->assertEquals(count($tags), count($data)); | |
149 | } | |
150 | ||
151 | /** | |
152 | * Test getTags service with offset and limit parameter: | |
153 | * limit=1 and offset=1 should not return any tag | |
154 | */ | |
155 | public function testGetTagsOffsetTooHigh() | |
156 | { | |
157 | $env = Environment::mock([ | |
158 | 'REQUEST_METHOD' => 'GET', | |
159 | 'QUERY_STRING' => 'offset=100' | |
160 | ]); | |
161 | $request = Request::createFromEnvironment($env); | |
162 | $response = $this->controller->getTags($request, new Response()); | |
163 | $this->assertEquals(200, $response->getStatusCode()); | |
164 | $data = json_decode((string) $response->getBody(), true); | |
165 | $this->assertEmpty(count($data)); | |
166 | } | |
167 | ||
168 | /** | |
169 | * Test getTags with visibility parameter set to private | |
170 | */ | |
171 | public function testGetTagsVisibilityPrivate() | |
172 | { | |
173 | $tags = $this->linkDB->linksCountPerTag([], 'private'); | |
174 | $env = Environment::mock([ | |
175 | 'REQUEST_METHOD' => 'GET', | |
176 | 'QUERY_STRING' => 'visibility=private' | |
177 | ]); | |
178 | $request = Request::createFromEnvironment($env); | |
179 | $response = $this->controller->getTags($request, new Response()); | |
180 | $this->assertEquals(200, $response->getStatusCode()); | |
181 | $data = json_decode((string) $response->getBody(), true); | |
182 | $this->assertEquals(count($tags), count($data)); | |
183 | $this->assertEquals(self::NB_FIELDS_TAG, count($data[0])); | |
7c57bd95 | 184 | $this->assertEquals('Mercurial', $data[0]['name']); |
d3f42ca4 A |
185 | $this->assertEquals(1, $data[0]['occurrences']); |
186 | } | |
187 | ||
188 | /** | |
189 | * Test getTags with visibility parameter set to public | |
190 | */ | |
191 | public function testGetTagsVisibilityPublic() | |
192 | { | |
193 | $tags = $this->linkDB->linksCountPerTag([], 'public'); | |
194 | $env = Environment::mock( | |
195 | [ | |
196 | 'REQUEST_METHOD' => 'GET', | |
197 | 'QUERY_STRING' => 'visibility=public' | |
198 | ] | |
199 | ); | |
200 | $request = Request::createFromEnvironment($env); | |
201 | $response = $this->controller->getTags($request, new Response()); | |
202 | $this->assertEquals(200, $response->getStatusCode()); | |
203 | $data = json_decode((string)$response->getBody(), true); | |
204 | $this->assertEquals(count($tags), count($data)); | |
205 | $this->assertEquals(self::NB_FIELDS_TAG, count($data[0])); | |
206 | $this->assertEquals('web', $data[0]['name']); | |
207 | $this->assertEquals(3, $data[0]['occurrences']); | |
208 | } | |
209 | } |