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