]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - tests/api/controllers/tags/PutTagTest.php
Merge tag 'v0.12.1' into latest
[github/shaarli/Shaarli.git] / tests / api / controllers / tags / PutTagTest.php
CommitLineData
d3f42ca4
A
1<?php
2
d3f42ca4
A
3namespace Shaarli\Api\Controllers;
4
fd1ddad9 5use malkusch\lock\mutex\NoMutex;
d3f42ca4 6use Shaarli\Api\Exceptions\ApiBadParametersException;
e26e2060 7use Shaarli\Bookmark\BookmarkFileService;
dea72c71 8use Shaarli\Bookmark\LinkDB;
d3f42ca4 9use Shaarli\Config\ConfigManager;
dea72c71 10use Shaarli\History;
d3f42ca4
A
11use Slim\Container;
12use Slim\Http\Environment;
13use Slim\Http\Request;
14use Slim\Http\Response;
15
a5a9cf23 16class PutTagTest extends \Shaarli\TestCase
d3f42ca4
A
17{
18 /**
19 * @var string datastore to test write operations
20 */
21 protected static $testDatastore = 'sandbox/datastore.php';
22
23 /**
24 * @var string datastore to test write operations
25 */
26 protected static $testHistory = 'sandbox/history.php';
27
28 /**
29 * @var ConfigManager instance
30 */
31 protected $conf;
32
33 /**
34 * @var \ReferenceLinkDB instance.
35 */
36 protected $refDB = null;
37
38 /**
dea72c71 39 * @var HistoryController instance.
d3f42ca4
A
40 */
41 protected $history;
42
43 /**
44 * @var Container instance.
45 */
46 protected $container;
47
48 /**
e26e2060 49 * @var BookmarkFileService instance.
d3f42ca4 50 */
e26e2060 51 protected $bookmarkService;
d3f42ca4
A
52
53 /**
54 * @var Tags controller instance.
55 */
56 protected $controller;
57
58 /**
59 * Number of JSON field per link.
60 */
61 const NB_FIELDS_TAG = 2;
62
63 /**
e26e2060 64 * Before every test, instantiate a new Api with its config, plugins and bookmarks.
d3f42ca4 65 */
8f60e120 66 protected function setUp(): void
d3f42ca4 67 {
fd1ddad9 68 $mutex = new NoMutex();
e26e2060
A
69 $this->conf = new ConfigManager('tests/utils/config/configJson');
70 $this->conf->set('resource.datastore', self::$testDatastore);
d3f42ca4
A
71 $this->refDB = new \ReferenceLinkDB();
72 $this->refDB->write(self::$testDatastore);
d3f42ca4
A
73 $refHistory = new \ReferenceHistory();
74 $refHistory->write(self::$testHistory);
dea72c71 75 $this->history = new History(self::$testHistory);
fd1ddad9 76 $this->bookmarkService = new BookmarkFileService($this->conf, $this->history, $mutex, true);
d3f42ca4
A
77
78 $this->container = new Container();
79 $this->container['conf'] = $this->conf;
e26e2060 80 $this->container['db'] = $this->bookmarkService;
d3f42ca4
A
81 $this->container['history'] = $this->history;
82
83 $this->controller = new Tags($this->container);
84 }
85
86 /**
87 * After every test, remove the test datastore.
88 */
8f60e120 89 protected function tearDown(): void
d3f42ca4
A
90 {
91 @unlink(self::$testDatastore);
92 @unlink(self::$testHistory);
93 }
94
95 /**
96 * Test tags update
97 */
98 public function testPutLinkValid()
99 {
100 $env = Environment::mock([
101 'REQUEST_METHOD' => 'PUT',
102 ]);
103 $tagName = 'gnu';
104 $update = ['name' => $newName = 'newtag'];
105 $request = Request::createFromEnvironment($env);
106 $request = $request->withParsedBody($update);
107
108 $response = $this->controller->putTag($request, new Response(), ['tagName' => $tagName]);
109 $this->assertEquals(200, $response->getStatusCode());
110 $data = json_decode((string) $response->getBody(), true);
111 $this->assertEquals(self::NB_FIELDS_TAG, count($data));
112 $this->assertEquals($newName, $data['name']);
113 $this->assertEquals(2, $data['occurrences']);
114
e26e2060 115 $tags = $this->bookmarkService->bookmarksCountPerTag();
d3f42ca4
A
116 $this->assertNotTrue(isset($tags[$tagName]));
117 $this->assertEquals(2, $tags[$newName]);
118
119 $historyEntry = $this->history->getHistory()[0];
dea72c71 120 $this->assertEquals(History::UPDATED, $historyEntry['event']);
d3f42ca4
A
121 $this->assertTrue(
122 (new \DateTime())->add(\DateInterval::createFromDateString('-5 seconds')) < $historyEntry['datetime']
123 );
124 $historyEntry = $this->history->getHistory()[1];
dea72c71 125 $this->assertEquals(History::UPDATED, $historyEntry['event']);
d3f42ca4
A
126 $this->assertTrue(
127 (new \DateTime())->add(\DateInterval::createFromDateString('-5 seconds')) < $historyEntry['datetime']
128 );
129 }
130
131 /**
132 * Test tag update with an existing tag: they should be merged
133 */
134 public function testPutTagMerge()
135 {
136 $tagName = 'gnu';
137 $newName = 'w3c';
138
e26e2060 139 $tags = $this->bookmarkService->bookmarksCountPerTag();
d3f42ca4
A
140 $this->assertEquals(1, $tags[$newName]);
141 $this->assertEquals(2, $tags[$tagName]);
142
143 $env = Environment::mock([
144 'REQUEST_METHOD' => 'PUT',
145 ]);
146 $update = ['name' => $newName];
147 $request = Request::createFromEnvironment($env);
148 $request = $request->withParsedBody($update);
149
150 $response = $this->controller->putTag($request, new Response(), ['tagName' => $tagName]);
151 $this->assertEquals(200, $response->getStatusCode());
152 $data = json_decode((string) $response->getBody(), true);
153 $this->assertEquals(self::NB_FIELDS_TAG, count($data));
154 $this->assertEquals($newName, $data['name']);
155 $this->assertEquals(3, $data['occurrences']);
156
e26e2060 157 $tags = $this->bookmarkService->bookmarksCountPerTag();
d3f42ca4
A
158 $this->assertNotTrue(isset($tags[$tagName]));
159 $this->assertEquals(3, $tags[$newName]);
160 }
161
162 /**
163 * Test tag update with an empty new tag name => ApiBadParametersException
d3f42ca4
A
164 */
165 public function testPutTagEmpty()
166 {
b1baca99
A
167 $this->expectException(\Shaarli\Api\Exceptions\ApiBadParametersException::class);
168 $this->expectExceptionMessage('New tag name is required in the request body');
169
d3f42ca4
A
170 $tagName = 'gnu';
171 $newName = '';
172
e26e2060 173 $tags = $this->bookmarkService->bookmarksCountPerTag();
d3f42ca4
A
174 $this->assertEquals(2, $tags[$tagName]);
175
176 $env = Environment::mock([
177 'REQUEST_METHOD' => 'PUT',
178 ]);
179 $request = Request::createFromEnvironment($env);
180
181 $env = Environment::mock([
182 'REQUEST_METHOD' => 'PUT',
183 ]);
184 $update = ['name' => $newName];
185 $request = Request::createFromEnvironment($env);
186 $request = $request->withParsedBody($update);
187
188 try {
189 $this->controller->putTag($request, new Response(), ['tagName' => $tagName]);
190 } catch (ApiBadParametersException $e) {
e26e2060 191 $tags = $this->bookmarkService->bookmarksCountPerTag();
d3f42ca4
A
192 $this->assertEquals(2, $tags[$tagName]);
193 throw $e;
194 }
195 }
196
197 /**
198 * Test tag update on non existent tag => ApiTagNotFoundException.
d3f42ca4
A
199 */
200 public function testPutTag404()
201 {
b1baca99
A
202 $this->expectException(\Shaarli\Api\Exceptions\ApiTagNotFoundException::class);
203 $this->expectExceptionMessage('Tag not found');
204
d3f42ca4
A
205 $env = Environment::mock([
206 'REQUEST_METHOD' => 'PUT',
207 ]);
208 $request = Request::createFromEnvironment($env);
209
210 $this->controller->putTag($request, new Response(), ['tagName' => 'nopenope']);
211 }
212}