]>
Commit | Line | Data |
---|---|---|
0843848c A |
1 | <?php |
2 | ||
3 | ||
4 | namespace Shaarli\Api\Controllers; | |
5 | ||
fd1ddad9 | 6 | use malkusch\lock\mutex\NoMutex; |
e26e2060 | 7 | use Shaarli\Bookmark\BookmarkFileService; |
0843848c | 8 | use Shaarli\Config\ConfigManager; |
dea72c71 | 9 | use Shaarli\History; |
bcba6bd3 | 10 | use Shaarli\Plugin\PluginManager; |
0843848c A |
11 | use Slim\Container; |
12 | use Slim\Http\Environment; | |
13 | use Slim\Http\Request; | |
14 | use Slim\Http\Response; | |
15 | ||
a5a9cf23 | 16 | class DeleteLinkTest 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 | /** | |
54 | * @var Links controller instance. | |
55 | */ | |
56 | protected $controller; | |
57 | ||
fd1ddad9 A |
58 | /** @var NoMutex */ |
59 | protected $mutex; | |
60 | ||
bcba6bd3 A |
61 | /** @var PluginManager */ |
62 | protected $pluginManager; | |
63 | ||
0843848c | 64 | /** |
e26e2060 | 65 | * Before each test, instantiate a new Api with its config, plugins and bookmarks. |
0843848c | 66 | */ |
8f60e120 | 67 | protected function setUp(): void |
0843848c | 68 | { |
fd1ddad9 | 69 | $this->mutex = new NoMutex(); |
0843848c | 70 | $this->conf = new ConfigManager('tests/utils/config/configJson'); |
e26e2060 | 71 | $this->conf->set('resource.datastore', self::$testDatastore); |
0843848c A |
72 | $this->refDB = new \ReferenceLinkDB(); |
73 | $this->refDB->write(self::$testDatastore); | |
813849e5 A |
74 | $refHistory = new \ReferenceHistory(); |
75 | $refHistory->write(self::$testHistory); | |
dea72c71 | 76 | $this->history = new History(self::$testHistory); |
bcba6bd3 A |
77 | $this->pluginManager = new PluginManager($this->conf); |
78 | $this->bookmarkService = new BookmarkFileService( | |
79 | $this->conf, | |
80 | $this->pluginManager, | |
81 | $this->history, | |
82 | $this->mutex, | |
83 | true | |
84 | ); | |
e26e2060 | 85 | |
0843848c A |
86 | $this->container = new Container(); |
87 | $this->container['conf'] = $this->conf; | |
e26e2060 | 88 | $this->container['db'] = $this->bookmarkService; |
813849e5 | 89 | $this->container['history'] = $this->history; |
0843848c A |
90 | |
91 | $this->controller = new Links($this->container); | |
92 | } | |
93 | ||
94 | /** | |
95 | * After each test, remove the test datastore. | |
96 | */ | |
8f60e120 | 97 | protected function tearDown(): void |
0843848c A |
98 | { |
99 | @unlink(self::$testDatastore); | |
813849e5 | 100 | @unlink(self::$testHistory); |
0843848c A |
101 | } |
102 | ||
103 | /** | |
104 | * Test DELETE link endpoint: the link should be removed. | |
105 | */ | |
106 | public function testDeleteLinkValid() | |
107 | { | |
108 | $id = '41'; | |
e26e2060 | 109 | $this->assertTrue($this->bookmarkService->exists($id)); |
0843848c A |
110 | $env = Environment::mock([ |
111 | 'REQUEST_METHOD' => 'DELETE', | |
112 | ]); | |
113 | $request = Request::createFromEnvironment($env); | |
114 | ||
115 | $response = $this->controller->deleteLink($request, new Response(), ['id' => $id]); | |
116 | $this->assertEquals(204, $response->getStatusCode()); | |
117 | $this->assertEmpty((string) $response->getBody()); | |
118 | ||
bcba6bd3 A |
119 | $this->bookmarkService = new BookmarkFileService( |
120 | $this->conf, | |
121 | $this->pluginManager, | |
122 | $this->history, | |
123 | $this->mutex, | |
124 | true | |
125 | ); | |
e26e2060 | 126 | $this->assertFalse($this->bookmarkService->exists($id)); |
813849e5 A |
127 | |
128 | $historyEntry = $this->history->getHistory()[0]; | |
dea72c71 | 129 | $this->assertEquals(History::DELETED, $historyEntry['event']); |
813849e5 A |
130 | $this->assertTrue( |
131 | (new \DateTime())->add(\DateInterval::createFromDateString('-5 seconds')) < $historyEntry['datetime'] | |
132 | ); | |
133 | $this->assertEquals($id, $historyEntry['id']); | |
0843848c A |
134 | } |
135 | ||
136 | /** | |
137 | * Test DELETE link endpoint: reach not existing ID. | |
0843848c A |
138 | */ |
139 | public function testDeleteLink404() | |
140 | { | |
b1baca99 A |
141 | $this->expectException(\Shaarli\Api\Exceptions\ApiLinkNotFoundException::class); |
142 | ||
0843848c | 143 | $id = -1; |
e26e2060 | 144 | $this->assertFalse($this->bookmarkService->exists($id)); |
0843848c A |
145 | $env = Environment::mock([ |
146 | 'REQUEST_METHOD' => 'DELETE', | |
147 | ]); | |
148 | $request = Request::createFromEnvironment($env); | |
149 | ||
150 | $this->controller->deleteLink($request, new Response(), ['id' => $id]); | |
151 | } | |
152 | } |