]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - tests/api/controllers/tags/DeleteTagTest.php
37f0722978c2e2ee4fc2a2a45589b4fd28f1ffe0
[github/shaarli/Shaarli.git] / tests / api / controllers / tags / DeleteTagTest.php
1 <?php
2
3
4 namespace Shaarli\Api\Controllers;
5
6 use malkusch\lock\mutex\NoMutex;
7 use Shaarli\Bookmark\BookmarkFileService;
8 use Shaarli\Bookmark\LinkDB;
9 use Shaarli\Config\ConfigManager;
10 use Shaarli\History;
11 use Slim\Container;
12 use Slim\Http\Environment;
13 use Slim\Http\Request;
14 use Slim\Http\Response;
15
16 class DeleteTagTest extends \Shaarli\TestCase
17 {
18 /**
19 * @var string datastore to test write operations
20 */
21 protected static $testDatastore = 'sandbox/datastore.php';
22
23 /**
24 * @var string datastore to test write operations
25 */
26 protected static $testHistory = 'sandbox/history.php';
27
28 /**
29 * @var ConfigManager instance
30 */
31 protected $conf;
32
33 /**
34 * @var \ReferenceLinkDB instance.
35 */
36 protected $refDB = null;
37
38 /**
39 * @var BookmarkFileService instance.
40 */
41 protected $bookmarkService;
42
43 /**
44 * @var HistoryController instance.
45 */
46 protected $history;
47
48 /**
49 * @var Container instance.
50 */
51 protected $container;
52
53 /**
54 * @var Tags controller instance.
55 */
56 protected $controller;
57
58 /** @var NoMutex */
59 protected $mutex;
60
61 /**
62 * Before each test, instantiate a new Api with its config, plugins and bookmarks.
63 */
64 protected function setUp(): void
65 {
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);
75
76 $this->container = new Container();
77 $this->container['conf'] = $this->conf;
78 $this->container['db'] = $this->bookmarkService;
79 $this->container['history'] = $this->history;
80
81 $this->controller = new Tags($this->container);
82 }
83
84 /**
85 * After each test, remove the test datastore.
86 */
87 protected function tearDown(): void
88 {
89 @unlink(self::$testDatastore);
90 @unlink(self::$testHistory);
91 }
92
93 /**
94 * Test DELETE tag endpoint: the tag should be removed.
95 */
96 public function testDeleteTagValid()
97 {
98 $tagName = 'gnu';
99 $tags = $this->bookmarkService->bookmarksCountPerTag();
100 $this->assertTrue($tags[$tagName] > 0);
101 $env = Environment::mock([
102 'REQUEST_METHOD' => 'DELETE',
103 ]);
104 $request = Request::createFromEnvironment($env);
105
106 $response = $this->controller->deleteTag($request, new Response(), ['tagName' => $tagName]);
107 $this->assertEquals(204, $response->getStatusCode());
108 $this->assertEmpty((string) $response->getBody());
109
110 $this->bookmarkService = new BookmarkFileService($this->conf, $this->history, $this->mutex, true);
111 $tags = $this->bookmarkService->bookmarksCountPerTag();
112 $this->assertFalse(isset($tags[$tagName]));
113
114 // 2 bookmarks affected
115 $historyEntry = $this->history->getHistory()[0];
116 $this->assertEquals(History::UPDATED, $historyEntry['event']);
117 $this->assertTrue(
118 (new \DateTime())->add(\DateInterval::createFromDateString('-5 seconds')) < $historyEntry['datetime']
119 );
120 $historyEntry = $this->history->getHistory()[1];
121 $this->assertEquals(History::UPDATED, $historyEntry['event']);
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';
133 $tags = $this->bookmarkService->bookmarksCountPerTag();
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
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);
148
149 $historyEntry = $this->history->getHistory()[0];
150 $this->assertEquals(History::UPDATED, $historyEntry['event']);
151 $this->assertTrue(
152 (new \DateTime())->add(\DateInterval::createFromDateString('-5 seconds')) < $historyEntry['datetime']
153 );
154 }
155
156 /**
157 * Test DELETE tag endpoint: reach not existing tag.
158 */
159 public function testDeleteLink404()
160 {
161 $this->expectException(\Shaarli\Api\Exceptions\ApiTagNotFoundException::class);
162 $this->expectExceptionMessage('Tag not found');
163
164 $tagName = 'nopenope';
165 $tags = $this->bookmarkService->bookmarksCountPerTag();
166 $this->assertFalse(isset($tags[$tagName]));
167 $env = Environment::mock([
168 'REQUEST_METHOD' => 'DELETE',
169 ]);
170 $request = Request::createFromEnvironment($env);
171
172 $this->controller->deleteTag($request, new Response(), ['tagName' => $tagName]);
173 }
174 }