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