4 namespace Shaarli\Api\Controllers
;
6 use malkusch\lock\mutex\NoMutex
;
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 DeleteTagTest
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 BookmarkFileService instance.
42 protected $bookmarkService;
45 * @var HistoryController instance.
50 * @var Container instance.
55 * @var Tags controller instance.
57 protected $controller;
59 /** @var PluginManager */
60 protected $pluginManager;
66 * Before each test, instantiate a new Api with its config, plugins and bookmarks.
68 protected function setUp(): void
70 $this->mutex
= new NoMutex();
71 $this->conf
= new ConfigManager('tests/utils/config/configJson');
72 $this->conf
->set('resource.datastore', self
::$testDatastore);
73 $this->refDB
= new \
ReferenceLinkDB();
74 $this->refDB
->write(self
::$testDatastore);
75 $refHistory = new \
ReferenceHistory();
76 $refHistory->write(self
::$testHistory);
77 $this->history
= new History(self
::$testHistory);
78 $this->pluginManager
= new PluginManager($this->conf
);
79 $this->bookmarkService
= new BookmarkFileService(
87 $this->container
= new Container();
88 $this->container
['conf'] = $this->conf
;
89 $this->container
['db'] = $this->bookmarkService
;
90 $this->container
['history'] = $this->history
;
92 $this->controller
= new Tags($this->container
);
96 * After each test, remove the test datastore.
98 protected function tearDown(): void
100 @unlink(self
::$testDatastore);
101 @unlink(self
::$testHistory);
105 * Test DELETE tag endpoint: the tag should be removed.
107 public function testDeleteTagValid()
110 $tags = $this->bookmarkService
->bookmarksCountPerTag();
111 $this->assertTrue($tags[$tagName] > 0);
112 $env = Environment
::mock([
113 'REQUEST_METHOD' => 'DELETE',
115 $request = Request
::createFromEnvironment($env);
117 $response = $this->controller
->deleteTag($request, new Response(), ['tagName' => $tagName]);
118 $this->assertEquals(204, $response->getStatusCode());
119 $this->assertEmpty((string) $response->getBody());
121 $this->bookmarkService
= new BookmarkFileService(
123 $this->pluginManager
,
128 $tags = $this->bookmarkService
->bookmarksCountPerTag();
129 $this->assertFalse(isset($tags[$tagName]));
131 // 2 bookmarks affected
132 $historyEntry = $this->history
->getHistory()[0];
133 $this->assertEquals(History
::UPDATED
, $historyEntry['event']);
135 (new \
DateTime())->add(\DateInterval
::createFromDateString('-5 seconds')) < $historyEntry['datetime']
137 $historyEntry = $this->history
->getHistory()[1];
138 $this->assertEquals(History
::UPDATED
, $historyEntry['event']);
140 (new \
DateTime())->add(\DateInterval
::createFromDateString('-5 seconds')) < $historyEntry['datetime']
145 * Test DELETE tag endpoint: the tag should be removed.
147 public function testDeleteTagCaseSensitivity()
150 $tags = $this->bookmarkService
->bookmarksCountPerTag();
151 $this->assertTrue($tags[$tagName] > 0);
152 $env = Environment
::mock([
153 'REQUEST_METHOD' => 'DELETE',
155 $request = Request
::createFromEnvironment($env);
157 $response = $this->controller
->deleteTag($request, new Response(), ['tagName' => $tagName]);
158 $this->assertEquals(204, $response->getStatusCode());
159 $this->assertEmpty((string) $response->getBody());
161 $this->bookmarkService
= new BookmarkFileService(
163 $this->pluginManager
,
168 $tags = $this->bookmarkService
->bookmarksCountPerTag();
169 $this->assertFalse(isset($tags[$tagName]));
170 $this->assertTrue($tags[strtolower($tagName)] > 0);
172 $historyEntry = $this->history
->getHistory()[0];
173 $this->assertEquals(History
::UPDATED
, $historyEntry['event']);
175 (new \
DateTime())->add(\DateInterval
::createFromDateString('-5 seconds')) < $historyEntry['datetime']
180 * Test DELETE tag endpoint: reach not existing tag.
182 public function testDeleteLink404()
184 $this->expectException(\Shaarli\Api\Exceptions\ApiTagNotFoundException
::class);
185 $this->expectExceptionMessage('Tag not found');
187 $tagName = 'nopenope';
188 $tags = $this->bookmarkService
->bookmarksCountPerTag();
189 $this->assertFalse(isset($tags[$tagName]));
190 $env = Environment
::mock([
191 'REQUEST_METHOD' => 'DELETE',
193 $request = Request
::createFromEnvironment($env);
195 $this->controller
->deleteTag($request, new Response(), ['tagName' => $tagName]);