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