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