]>
Commit | Line | Data |
---|---|---|
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 Shaarli\Plugin\PluginManager; | |
12 | use Slim\Container; | |
13 | use Slim\Http\Environment; | |
14 | use Slim\Http\Request; | |
15 | use Slim\Http\Response; | |
16 | ||
17 | class DeleteTagTest extends \Shaarli\TestCase | |
18 | { | |
19 | /** | |
20 | * @var string datastore to test write operations | |
21 | */ | |
22 | protected static $testDatastore = 'sandbox/datastore.php'; | |
23 | ||
24 | /** | |
25 | * @var string datastore to test write operations | |
26 | */ | |
27 | protected static $testHistory = 'sandbox/history.php'; | |
28 | ||
29 | /** | |
30 | * @var ConfigManager instance | |
31 | */ | |
32 | protected $conf; | |
33 | ||
34 | /** | |
35 | * @var \ReferenceLinkDB instance. | |
36 | */ | |
37 | protected $refDB = null; | |
38 | ||
39 | /** | |
40 | * @var BookmarkFileService instance. | |
41 | */ | |
42 | protected $bookmarkService; | |
43 | ||
44 | /** | |
45 | * @var HistoryController instance. | |
46 | */ | |
47 | protected $history; | |
48 | ||
49 | /** | |
50 | * @var Container instance. | |
51 | */ | |
52 | protected $container; | |
53 | ||
54 | /** | |
55 | * @var Tags controller instance. | |
56 | */ | |
57 | protected $controller; | |
58 | ||
59 | /** @var PluginManager */ | |
60 | protected $pluginManager; | |
61 | ||
62 | /** @var NoMutex */ | |
63 | protected $mutex; | |
64 | ||
65 | /** | |
66 | * Before each test, instantiate a new Api with its config, plugins and bookmarks. | |
67 | */ | |
68 | protected function setUp(): void | |
69 | { | |
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( | |
80 | $this->conf, | |
81 | $this->pluginManager, | |
82 | $this->history, | |
83 | $this->mutex, | |
84 | true | |
85 | ); | |
86 | ||
87 | $this->container = new Container(); | |
88 | $this->container['conf'] = $this->conf; | |
89 | $this->container['db'] = $this->bookmarkService; | |
90 | $this->container['history'] = $this->history; | |
91 | ||
92 | $this->controller = new Tags($this->container); | |
93 | } | |
94 | ||
95 | /** | |
96 | * After each test, remove the test datastore. | |
97 | */ | |
98 | protected function tearDown(): void | |
99 | { | |
100 | @unlink(self::$testDatastore); | |
101 | @unlink(self::$testHistory); | |
102 | } | |
103 | ||
104 | /** | |
105 | * Test DELETE tag endpoint: the tag should be removed. | |
106 | */ | |
107 | public function testDeleteTagValid() | |
108 | { | |
109 | $tagName = 'gnu'; | |
110 | $tags = $this->bookmarkService->bookmarksCountPerTag(); | |
111 | $this->assertTrue($tags[$tagName] > 0); | |
112 | $env = Environment::mock([ | |
113 | 'REQUEST_METHOD' => 'DELETE', | |
114 | ]); | |
115 | $request = Request::createFromEnvironment($env); | |
116 | ||
117 | $response = $this->controller->deleteTag($request, new Response(), ['tagName' => $tagName]); | |
118 | $this->assertEquals(204, $response->getStatusCode()); | |
119 | $this->assertEmpty((string) $response->getBody()); | |
120 | ||
121 | $this->bookmarkService = new BookmarkFileService( | |
122 | $this->conf, | |
123 | $this->pluginManager, | |
124 | $this->history, | |
125 | $this->mutex, | |
126 | true | |
127 | ); | |
128 | $tags = $this->bookmarkService->bookmarksCountPerTag(); | |
129 | $this->assertFalse(isset($tags[$tagName])); | |
130 | ||
131 | // 2 bookmarks affected | |
132 | $historyEntry = $this->history->getHistory()[0]; | |
133 | $this->assertEquals(History::UPDATED, $historyEntry['event']); | |
134 | $this->assertTrue( | |
135 | (new \DateTime())->add(\DateInterval::createFromDateString('-5 seconds')) < $historyEntry['datetime'] | |
136 | ); | |
137 | $historyEntry = $this->history->getHistory()[1]; | |
138 | $this->assertEquals(History::UPDATED, $historyEntry['event']); | |
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'; | |
150 | $tags = $this->bookmarkService->bookmarksCountPerTag(); | |
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 | ||
161 | $this->bookmarkService = new BookmarkFileService( | |
162 | $this->conf, | |
163 | $this->pluginManager, | |
164 | $this->history, | |
165 | $this->mutex, | |
166 | true | |
167 | ); | |
168 | $tags = $this->bookmarkService->bookmarksCountPerTag(); | |
169 | $this->assertFalse(isset($tags[$tagName])); | |
170 | $this->assertTrue($tags[strtolower($tagName)] > 0); | |
171 | ||
172 | $historyEntry = $this->history->getHistory()[0]; | |
173 | $this->assertEquals(History::UPDATED, $historyEntry['event']); | |
174 | $this->assertTrue( | |
175 | (new \DateTime())->add(\DateInterval::createFromDateString('-5 seconds')) < $historyEntry['datetime'] | |
176 | ); | |
177 | } | |
178 | ||
179 | /** | |
180 | * Test DELETE tag endpoint: reach not existing tag. | |
181 | */ | |
182 | public function testDeleteLink404() | |
183 | { | |
184 | $this->expectException(\Shaarli\Api\Exceptions\ApiTagNotFoundException::class); | |
185 | $this->expectExceptionMessage('Tag not found'); | |
186 | ||
187 | $tagName = 'nopenope'; | |
188 | $tags = $this->bookmarkService->bookmarksCountPerTag(); | |
189 | $this->assertFalse(isset($tags[$tagName])); | |
190 | $env = Environment::mock([ | |
191 | 'REQUEST_METHOD' => 'DELETE', | |
192 | ]); | |
193 | $request = Request::createFromEnvironment($env); | |
194 | ||
195 | $this->controller->deleteTag($request, new Response(), ['tagName' => $tagName]); | |
196 | } | |
197 | } |