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
;
12 use Slim\Http\Environment
;
13 use Slim\Http\Request
;
14 use Slim\Http\Response
;
16 class PutTagTest
extends \Shaarli\TestCase
19 * @var string datastore to test write operations
21 protected static $testDatastore = 'sandbox/datastore.php';
24 * @var string datastore to test write operations
26 protected static $testHistory = 'sandbox/history.php';
29 * @var ConfigManager instance
34 * @var \ReferenceLinkDB instance.
36 protected $refDB = null;
39 * @var HistoryController instance.
44 * @var Container instance.
49 * @var BookmarkFileService instance.
51 protected $bookmarkService;
54 * @var Tags controller instance.
56 protected $controller;
59 * Number of JSON field per link.
61 const NB_FIELDS_TAG
= 2;
64 * Before every test, instantiate a new Api with its config, plugins and bookmarks.
66 protected function setUp(): void
68 $mutex = new NoMutex();
69 $this->conf
= new ConfigManager('tests/utils/config/configJson');
70 $this->conf
->set('resource.datastore', self
::$testDatastore);
71 $this->refDB
= new \
ReferenceLinkDB();
72 $this->refDB
->write(self
::$testDatastore);
73 $refHistory = new \
ReferenceHistory();
74 $refHistory->write(self
::$testHistory);
75 $this->history
= new History(self
::$testHistory);
76 $this->bookmarkService
= new BookmarkFileService($this->conf
, $this->history
, $mutex, true);
78 $this->container
= new Container();
79 $this->container
['conf'] = $this->conf
;
80 $this->container
['db'] = $this->bookmarkService
;
81 $this->container
['history'] = $this->history
;
83 $this->controller
= new Tags($this->container
);
87 * After every test, remove the test datastore.
89 protected function tearDown(): void
91 @unlink(self
::$testDatastore);
92 @unlink(self
::$testHistory);
98 public function testPutLinkValid()
100 $env = Environment
::mock([
101 'REQUEST_METHOD' => 'PUT',
104 $update = ['name' => $newName = 'newtag'];
105 $request = Request
::createFromEnvironment($env);
106 $request = $request->withParsedBody($update);
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']);
115 $tags = $this->bookmarkService
->bookmarksCountPerTag();
116 $this->assertNotTrue(isset($tags[$tagName]));
117 $this->assertEquals(2, $tags[$newName]);
119 $historyEntry = $this->history
->getHistory()[0];
120 $this->assertEquals(History
::UPDATED
, $historyEntry['event']);
122 (new \
DateTime())->add(\DateInterval
::createFromDateString('-5 seconds')) < $historyEntry['datetime']
124 $historyEntry = $this->history
->getHistory()[1];
125 $this->assertEquals(History
::UPDATED
, $historyEntry['event']);
127 (new \
DateTime())->add(\DateInterval
::createFromDateString('-5 seconds')) < $historyEntry['datetime']
132 * Test tag update with an existing tag: they should be merged
134 public function testPutTagMerge()
139 $tags = $this->bookmarkService
->bookmarksCountPerTag();
140 $this->assertEquals(1, $tags[$newName]);
141 $this->assertEquals(2, $tags[$tagName]);
143 $env = Environment
::mock([
144 'REQUEST_METHOD' => 'PUT',
146 $update = ['name' => $newName];
147 $request = Request
::createFromEnvironment($env);
148 $request = $request->withParsedBody($update);
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']);
157 $tags = $this->bookmarkService
->bookmarksCountPerTag();
158 $this->assertNotTrue(isset($tags[$tagName]));
159 $this->assertEquals(3, $tags[$newName]);
163 * Test tag update with an empty new tag name => ApiBadParametersException
165 public function testPutTagEmpty()
167 $this->expectException(\Shaarli\Api\Exceptions\ApiBadParametersException
::class);
168 $this->expectExceptionMessage('New tag name is required in the request body');
173 $tags = $this->bookmarkService
->bookmarksCountPerTag();
174 $this->assertEquals(2, $tags[$tagName]);
176 $env = Environment
::mock([
177 'REQUEST_METHOD' => 'PUT',
179 $request = Request
::createFromEnvironment($env);
181 $env = Environment
::mock([
182 'REQUEST_METHOD' => 'PUT',
184 $update = ['name' => $newName];
185 $request = Request
::createFromEnvironment($env);
186 $request = $request->withParsedBody($update);
189 $this->controller
->putTag($request, new Response(), ['tagName' => $tagName]);
190 } catch (ApiBadParametersException
$e) {
191 $tags = $this->bookmarkService
->bookmarksCountPerTag();
192 $this->assertEquals(2, $tags[$tagName]);
198 * Test tag update on non existent tag => ApiTagNotFoundException.
200 public function testPutTag404()
202 $this->expectException(\Shaarli\Api\Exceptions\ApiTagNotFoundException
::class);
203 $this->expectExceptionMessage('Tag not found');
205 $env = Environment
::mock([
206 'REQUEST_METHOD' => 'PUT',
208 $request = Request
::createFromEnvironment($env);
210 $this->controller
->putTag($request, new Response(), ['tagName' => 'nopenope']);