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
;
12 use Slim\Http\Environment
;
13 use Slim\Http\Request
;
14 use Slim\Http\Response
;
16 class DeleteTagTest
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 BookmarkFileService instance.
41 protected $bookmarkService;
44 * @var HistoryController instance.
49 * @var Container instance.
54 * @var Tags controller instance.
56 protected $controller;
62 * Before each test, instantiate a new Api with its config, plugins and bookmarks.
64 protected function setUp(): void
66 $this->mutex
= new NoMutex();
67 $this->conf
= new ConfigManager('tests/utils/config/configJson');
68 $this->conf
->set('resource.datastore', self
::$testDatastore);
69 $this->refDB
= new \
ReferenceLinkDB();
70 $this->refDB
->write(self
::$testDatastore);
71 $refHistory = new \
ReferenceHistory();
72 $refHistory->write(self
::$testHistory);
73 $this->history
= new History(self
::$testHistory);
74 $this->bookmarkService
= new BookmarkFileService($this->conf
, $this->history
, $this->mutex
, true);
76 $this->container
= new Container();
77 $this->container
['conf'] = $this->conf
;
78 $this->container
['db'] = $this->bookmarkService
;
79 $this->container
['history'] = $this->history
;
81 $this->controller
= new Tags($this->container
);
85 * After each test, remove the test datastore.
87 protected function tearDown(): void
89 @unlink(self
::$testDatastore);
90 @unlink(self
::$testHistory);
94 * Test DELETE tag endpoint: the tag should be removed.
96 public function testDeleteTagValid()
99 $tags = $this->bookmarkService
->bookmarksCountPerTag();
100 $this->assertTrue($tags[$tagName] > 0);
101 $env = Environment
::mock([
102 'REQUEST_METHOD' => 'DELETE',
104 $request = Request
::createFromEnvironment($env);
106 $response = $this->controller
->deleteTag($request, new Response(), ['tagName' => $tagName]);
107 $this->assertEquals(204, $response->getStatusCode());
108 $this->assertEmpty((string) $response->getBody());
110 $this->bookmarkService
= new BookmarkFileService($this->conf
, $this->history
, $this->mutex
, true);
111 $tags = $this->bookmarkService
->bookmarksCountPerTag();
112 $this->assertFalse(isset($tags[$tagName]));
114 // 2 bookmarks affected
115 $historyEntry = $this->history
->getHistory()[0];
116 $this->assertEquals(History
::UPDATED
, $historyEntry['event']);
118 (new \
DateTime())->add(\DateInterval
::createFromDateString('-5 seconds')) < $historyEntry['datetime']
120 $historyEntry = $this->history
->getHistory()[1];
121 $this->assertEquals(History
::UPDATED
, $historyEntry['event']);
123 (new \
DateTime())->add(\DateInterval
::createFromDateString('-5 seconds')) < $historyEntry['datetime']
128 * Test DELETE tag endpoint: the tag should be removed.
130 public function testDeleteTagCaseSensitivity()
133 $tags = $this->bookmarkService
->bookmarksCountPerTag();
134 $this->assertTrue($tags[$tagName] > 0);
135 $env = Environment
::mock([
136 'REQUEST_METHOD' => 'DELETE',
138 $request = Request
::createFromEnvironment($env);
140 $response = $this->controller
->deleteTag($request, new Response(), ['tagName' => $tagName]);
141 $this->assertEquals(204, $response->getStatusCode());
142 $this->assertEmpty((string) $response->getBody());
144 $this->bookmarkService
= new BookmarkFileService($this->conf
, $this->history
, $this->mutex
, true);
145 $tags = $this->bookmarkService
->bookmarksCountPerTag();
146 $this->assertFalse(isset($tags[$tagName]));
147 $this->assertTrue($tags[strtolower($tagName)] > 0);
149 $historyEntry = $this->history
->getHistory()[0];
150 $this->assertEquals(History
::UPDATED
, $historyEntry['event']);
152 (new \
DateTime())->add(\DateInterval
::createFromDateString('-5 seconds')) < $historyEntry['datetime']
157 * Test DELETE tag endpoint: reach not existing tag.
159 public function testDeleteLink404()
161 $this->expectException(\Shaarli\Api\Exceptions\ApiTagNotFoundException
::class);
162 $this->expectExceptionMessage('Tag not found');
164 $tagName = 'nopenope';
165 $tags = $this->bookmarkService
->bookmarksCountPerTag();
166 $this->assertFalse(isset($tags[$tagName]));
167 $env = Environment
::mock([
168 'REQUEST_METHOD' => 'DELETE',
170 $request = Request
::createFromEnvironment($env);
172 $this->controller
->deleteTag($request, new Response(), ['tagName' => $tagName]);