3 namespace Shaarli\Api\Controllers
;
5 use malkusch\lock\mutex\NoMutex
;
6 use Shaarli\Api\Exceptions\ApiBadParametersException
;
7 use Shaarli\Bookmark\BookmarkFileService
;
8 use Shaarli\Bookmark\LinkDB
;
9 use Shaarli\Config\ConfigManager
;
11 use Shaarli\Plugin\PluginManager
;
13 use Slim\Http\Environment
;
14 use Slim\Http\Request
;
15 use Slim\Http\Response
;
17 class PutTagTest
extends \Shaarli\TestCase
20 * @var string datastore to test write operations
22 protected static $testDatastore = 'sandbox/datastore.php';
25 * @var string datastore to test write operations
27 protected static $testHistory = 'sandbox/history.php';
30 * @var ConfigManager instance
35 * @var \ReferenceLinkDB instance.
37 protected $refDB = null;
40 * @var HistoryController instance.
45 * @var Container instance.
50 * @var BookmarkFileService instance.
52 protected $bookmarkService;
55 * @var Tags controller instance.
57 protected $controller;
59 /** @var PluginManager */
60 protected $pluginManager;
63 * Number of JSON field per link.
65 const NB_FIELDS_TAG
= 2;
68 * Before every test, instantiate a new Api with its config, plugins and bookmarks.
70 protected function setUp(): void
72 $mutex = new NoMutex();
73 $this->conf
= new ConfigManager('tests/utils/config/configJson');
74 $this->conf
->set('resource.datastore', self
::$testDatastore);
75 $this->refDB
= new \
ReferenceLinkDB();
76 $this->refDB
->write(self
::$testDatastore);
77 $refHistory = new \
ReferenceHistory();
78 $refHistory->write(self
::$testHistory);
79 $this->history
= new History(self
::$testHistory);
80 $this->pluginManager
= new PluginManager($this->conf
);
81 $this->bookmarkService
= new BookmarkFileService(
89 $this->container
= new Container();
90 $this->container
['conf'] = $this->conf
;
91 $this->container
['db'] = $this->bookmarkService
;
92 $this->container
['history'] = $this->history
;
94 $this->controller
= new Tags($this->container
);
98 * After every test, remove the test datastore.
100 protected function tearDown(): void
102 @unlink(self
::$testDatastore);
103 @unlink(self
::$testHistory);
109 public function testPutLinkValid()
111 $env = Environment
::mock([
112 'REQUEST_METHOD' => 'PUT',
115 $update = ['name' => $newName = 'newtag'];
116 $request = Request
::createFromEnvironment($env);
117 $request = $request->withParsedBody($update);
119 $response = $this->controller
->putTag($request, new Response(), ['tagName' => $tagName]);
120 $this->assertEquals(200, $response->getStatusCode());
121 $data = json_decode((string) $response->getBody(), true);
122 $this->assertEquals(self
::NB_FIELDS_TAG
, count($data));
123 $this->assertEquals($newName, $data['name']);
124 $this->assertEquals(2, $data['occurrences']);
126 $tags = $this->bookmarkService
->bookmarksCountPerTag();
127 $this->assertNotTrue(isset($tags[$tagName]));
128 $this->assertEquals(2, $tags[$newName]);
130 $historyEntry = $this->history
->getHistory()[0];
131 $this->assertEquals(History
::UPDATED
, $historyEntry['event']);
133 (new \
DateTime())->add(\DateInterval
::createFromDateString('-5 seconds')) < $historyEntry['datetime']
135 $historyEntry = $this->history
->getHistory()[1];
136 $this->assertEquals(History
::UPDATED
, $historyEntry['event']);
138 (new \
DateTime())->add(\DateInterval
::createFromDateString('-5 seconds')) < $historyEntry['datetime']
143 * Test tag update with an existing tag: they should be merged
145 public function testPutTagMerge()
150 $tags = $this->bookmarkService
->bookmarksCountPerTag();
151 $this->assertEquals(1, $tags[$newName]);
152 $this->assertEquals(2, $tags[$tagName]);
154 $env = Environment
::mock([
155 'REQUEST_METHOD' => 'PUT',
157 $update = ['name' => $newName];
158 $request = Request
::createFromEnvironment($env);
159 $request = $request->withParsedBody($update);
161 $response = $this->controller
->putTag($request, new Response(), ['tagName' => $tagName]);
162 $this->assertEquals(200, $response->getStatusCode());
163 $data = json_decode((string) $response->getBody(), true);
164 $this->assertEquals(self
::NB_FIELDS_TAG
, count($data));
165 $this->assertEquals($newName, $data['name']);
166 $this->assertEquals(3, $data['occurrences']);
168 $tags = $this->bookmarkService
->bookmarksCountPerTag();
169 $this->assertNotTrue(isset($tags[$tagName]));
170 $this->assertEquals(3, $tags[$newName]);
174 * Test tag update with an empty new tag name => ApiBadParametersException
176 public function testPutTagEmpty()
178 $this->expectException(\Shaarli\Api\Exceptions\ApiBadParametersException
::class);
179 $this->expectExceptionMessage('New tag name is required in the request body');
184 $tags = $this->bookmarkService
->bookmarksCountPerTag();
185 $this->assertEquals(2, $tags[$tagName]);
187 $env = Environment
::mock([
188 'REQUEST_METHOD' => 'PUT',
190 $request = Request
::createFromEnvironment($env);
192 $env = Environment
::mock([
193 'REQUEST_METHOD' => 'PUT',
195 $update = ['name' => $newName];
196 $request = Request
::createFromEnvironment($env);
197 $request = $request->withParsedBody($update);
200 $this->controller
->putTag($request, new Response(), ['tagName' => $tagName]);
201 } catch (ApiBadParametersException
$e) {
202 $tags = $this->bookmarkService
->bookmarksCountPerTag();
203 $this->assertEquals(2, $tags[$tagName]);
209 * Test tag update on non existent tag => ApiTagNotFoundException.
211 public function testPutTag404()
213 $this->expectException(\Shaarli\Api\Exceptions\ApiTagNotFoundException
::class);
214 $this->expectExceptionMessage('Tag not found');
216 $env = Environment
::mock([
217 'REQUEST_METHOD' => 'PUT',
219 $request = Request
::createFromEnvironment($env);
221 $this->controller
->putTag($request, new Response(), ['tagName' => 'nopenope']);