]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - tests/api/controllers/tags/GetTagsTest.php
Merge pull request #1697 from ArthurHoaro/feature/pagination
[github/shaarli/Shaarli.git] / tests / api / controllers / tags / GetTagsTest.php
CommitLineData
d3f42ca4
A
1<?php
2namespace Shaarli\Api\Controllers;
3
fd1ddad9 4use malkusch\lock\mutex\NoMutex;
e26e2060 5use Shaarli\Bookmark\BookmarkFileService;
dea72c71 6use Shaarli\Bookmark\LinkDB;
d3f42ca4 7use Shaarli\Config\ConfigManager;
e26e2060 8use Shaarli\History;
d3f42ca4
A
9use Slim\Container;
10use Slim\Http\Environment;
11use Slim\Http\Request;
12use Slim\Http\Response;
13
14/**
15 * Class GetTagsTest
16 *
17 * Test get tag list REST API service.
18 *
19 * @package Shaarli\Api\Controllers
20 */
a5a9cf23 21class GetTagsTest extends \Shaarli\TestCase
d3f42ca4
A
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 /**
e26e2060 44 * @var BookmarkFileService instance.
d3f42ca4 45 */
e26e2060 46 protected $bookmarkService;
d3f42ca4
A
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 /**
e26e2060 59 * Before every test, instantiate a new Api with its config, plugins and bookmarks.
d3f42ca4 60 */
8f60e120 61 protected function setUp(): void
d3f42ca4 62 {
fd1ddad9 63 $mutex = new NoMutex();
d3f42ca4 64 $this->conf = new ConfigManager('tests/utils/config/configJson');
e26e2060 65 $this->conf->set('resource.datastore', self::$testDatastore);
d3f42ca4
A
66 $this->refDB = new \ReferenceLinkDB();
67 $this->refDB->write(self::$testDatastore);
e26e2060
A
68 $history = new History('sandbox/history.php');
69
fd1ddad9 70 $this->bookmarkService = new BookmarkFileService($this->conf, $history, $mutex, true);
d3f42ca4
A
71
72 $this->container = new Container();
73 $this->container['conf'] = $this->conf;
e26e2060 74 $this->container['db'] = $this->bookmarkService;
d3f42ca4
A
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 */
8f60e120 83 protected function tearDown(): void
d3f42ca4
A
84 {
85 @unlink(self::$testDatastore);
86 }
87
88 /**
7c57bd95 89 * Test basic getTags service: returns all tags.
d3f42ca4
A
90 */
91 public function testGetTagsAll()
92 {
e26e2060 93 $tags = $this->bookmarkService->bookmarksCountPerTag();
d3f42ca4
A
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
7c57bd95
A
112 $this->assertEquals(self::NB_FIELDS_TAG, count($data[5]));
113 $this->assertEquals('sTuff', $data[5]['name']);
114 $this->assertEquals(2, $data[5]['occurrences']);
d3f42ca4
A
115 // End
116 $this->assertEquals(self::NB_FIELDS_TAG, count($data[count($data) - 1]));
7c57bd95 117 $this->assertEquals('w3c', $data[count($data) - 1]['name']);
d3f42ca4
A
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 {
e26e2060 146 $tags = $this->bookmarkService->bookmarksCountPerTag();
d3f42ca4
A
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 {
e26e2060 180 $tags = $this->bookmarkService->bookmarksCountPerTag([], 'private');
d3f42ca4
A
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]));
7c57bd95 191 $this->assertEquals('Mercurial', $data[0]['name']);
d3f42ca4
A
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 {
e26e2060 200 $tags = $this->bookmarkService->bookmarksCountPerTag([], 'public');
d3f42ca4
A
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}