]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - tests/api/controllers/tags/DeleteTagTest.php
Merge pull request #1698 from ArthurHoaro/feature/plugins-search-filter
[github/shaarli/Shaarli.git] / tests / api / controllers / tags / DeleteTagTest.php
CommitLineData
0843848c
A
1<?php
2
3
4namespace Shaarli\Api\Controllers;
5
fd1ddad9 6use malkusch\lock\mutex\NoMutex;
e26e2060 7use Shaarli\Bookmark\BookmarkFileService;
dea72c71 8use Shaarli\Bookmark\LinkDB;
0843848c 9use Shaarli\Config\ConfigManager;
dea72c71 10use Shaarli\History;
bcba6bd3 11use Shaarli\Plugin\PluginManager;
0843848c
A
12use Slim\Container;
13use Slim\Http\Environment;
14use Slim\Http\Request;
15use Slim\Http\Response;
16
a5a9cf23 17class DeleteTagTest extends \Shaarli\TestCase
0843848c
A
18{
19 /**
20 * @var string datastore to test write operations
21 */
22 protected static $testDatastore = 'sandbox/datastore.php';
23
813849e5
A
24 /**
25 * @var string datastore to test write operations
26 */
27 protected static $testHistory = 'sandbox/history.php';
28
0843848c
A
29 /**
30 * @var ConfigManager instance
31 */
32 protected $conf;
33
34 /**
35 * @var \ReferenceLinkDB instance.
36 */
37 protected $refDB = null;
38
39 /**
e26e2060 40 * @var BookmarkFileService instance.
0843848c 41 */
e26e2060 42 protected $bookmarkService;
0843848c 43
813849e5 44 /**
dea72c71 45 * @var HistoryController instance.
813849e5
A
46 */
47 protected $history;
48
0843848c
A
49 /**
50 * @var Container instance.
51 */
52 protected $container;
53
54 /**
d3f42ca4 55 * @var Tags controller instance.
0843848c
A
56 */
57 protected $controller;
58
bcba6bd3
A
59 /** @var PluginManager */
60 protected $pluginManager;
61
fd1ddad9
A
62 /** @var NoMutex */
63 protected $mutex;
64
0843848c 65 /**
e26e2060 66 * Before each test, instantiate a new Api with its config, plugins and bookmarks.
0843848c 67 */
8f60e120 68 protected function setUp(): void
0843848c 69 {
fd1ddad9 70 $this->mutex = new NoMutex();
0843848c 71 $this->conf = new ConfigManager('tests/utils/config/configJson');
e26e2060 72 $this->conf->set('resource.datastore', self::$testDatastore);
0843848c
A
73 $this->refDB = new \ReferenceLinkDB();
74 $this->refDB->write(self::$testDatastore);
813849e5
A
75 $refHistory = new \ReferenceHistory();
76 $refHistory->write(self::$testHistory);
dea72c71 77 $this->history = new History(self::$testHistory);
bcba6bd3
A
78 $this->pluginManager = new PluginManager($this->conf);
79 $this->bookmarkService = new BookmarkFileService(
80 $this->conf,
81 $this->pluginManager,
82 $this->history,
83 $this->mutex,
84 true
85 );
e26e2060 86
0843848c
A
87 $this->container = new Container();
88 $this->container['conf'] = $this->conf;
e26e2060 89 $this->container['db'] = $this->bookmarkService;
813849e5 90 $this->container['history'] = $this->history;
0843848c 91
d3f42ca4 92 $this->controller = new Tags($this->container);
0843848c
A
93 }
94
95 /**
96 * After each test, remove the test datastore.
97 */
8f60e120 98 protected function tearDown(): void
0843848c
A
99 {
100 @unlink(self::$testDatastore);
813849e5 101 @unlink(self::$testHistory);
0843848c
A
102 }
103
104 /**
d3f42ca4 105 * Test DELETE tag endpoint: the tag should be removed.
0843848c 106 */
d3f42ca4 107 public function testDeleteTagValid()
0843848c 108 {
d3f42ca4 109 $tagName = 'gnu';
e26e2060 110 $tags = $this->bookmarkService->bookmarksCountPerTag();
d3f42ca4 111 $this->assertTrue($tags[$tagName] > 0);
0843848c
A
112 $env = Environment::mock([
113 'REQUEST_METHOD' => 'DELETE',
114 ]);
115 $request = Request::createFromEnvironment($env);
116
d3f42ca4 117 $response = $this->controller->deleteTag($request, new Response(), ['tagName' => $tagName]);
0843848c
A
118 $this->assertEquals(204, $response->getStatusCode());
119 $this->assertEmpty((string) $response->getBody());
120
bcba6bd3
A
121 $this->bookmarkService = new BookmarkFileService(
122 $this->conf,
123 $this->pluginManager,
124 $this->history,
125 $this->mutex,
126 true
127 );
e26e2060 128 $tags = $this->bookmarkService->bookmarksCountPerTag();
d3f42ca4 129 $this->assertFalse(isset($tags[$tagName]));
813849e5 130
e26e2060 131 // 2 bookmarks affected
813849e5 132 $historyEntry = $this->history->getHistory()[0];
dea72c71 133 $this->assertEquals(History::UPDATED, $historyEntry['event']);
d3f42ca4
A
134 $this->assertTrue(
135 (new \DateTime())->add(\DateInterval::createFromDateString('-5 seconds')) < $historyEntry['datetime']
136 );
137 $historyEntry = $this->history->getHistory()[1];
dea72c71 138 $this->assertEquals(History::UPDATED, $historyEntry['event']);
d3f42ca4
A
139 $this->assertTrue(
140 (new \DateTime())->add(\DateInterval::createFromDateString('-5 seconds')) < $historyEntry['datetime']
141 );
142 }
143
144 /**
145 * Test DELETE tag endpoint: the tag should be removed.
146 */
147 public function testDeleteTagCaseSensitivity()
148 {
149 $tagName = 'sTuff';
e26e2060 150 $tags = $this->bookmarkService->bookmarksCountPerTag();
d3f42ca4
A
151 $this->assertTrue($tags[$tagName] > 0);
152 $env = Environment::mock([
153 'REQUEST_METHOD' => 'DELETE',
154 ]);
155 $request = Request::createFromEnvironment($env);
156
157 $response = $this->controller->deleteTag($request, new Response(), ['tagName' => $tagName]);
158 $this->assertEquals(204, $response->getStatusCode());
159 $this->assertEmpty((string) $response->getBody());
160
bcba6bd3
A
161 $this->bookmarkService = new BookmarkFileService(
162 $this->conf,
163 $this->pluginManager,
164 $this->history,
165 $this->mutex,
166 true
167 );
e26e2060 168 $tags = $this->bookmarkService->bookmarksCountPerTag();
d3f42ca4
A
169 $this->assertFalse(isset($tags[$tagName]));
170 $this->assertTrue($tags[strtolower($tagName)] > 0);
171
172 $historyEntry = $this->history->getHistory()[0];
dea72c71 173 $this->assertEquals(History::UPDATED, $historyEntry['event']);
813849e5
A
174 $this->assertTrue(
175 (new \DateTime())->add(\DateInterval::createFromDateString('-5 seconds')) < $historyEntry['datetime']
176 );
0843848c
A
177 }
178
179 /**
7c57bd95 180 * Test DELETE tag endpoint: reach not existing tag.
0843848c
A
181 */
182 public function testDeleteLink404()
183 {
b1baca99
A
184 $this->expectException(\Shaarli\Api\Exceptions\ApiTagNotFoundException::class);
185 $this->expectExceptionMessage('Tag not found');
186
d3f42ca4 187 $tagName = 'nopenope';
e26e2060 188 $tags = $this->bookmarkService->bookmarksCountPerTag();
d3f42ca4 189 $this->assertFalse(isset($tags[$tagName]));
0843848c
A
190 $env = Environment::mock([
191 'REQUEST_METHOD' => 'DELETE',
192 ]);
193 $request = Request::createFromEnvironment($env);
194
d3f42ca4 195 $this->controller->deleteTag($request, new Response(), ['tagName' => $tagName]);
0843848c
A
196 }
197}