4 namespace Shaarli\Api\Controllers
;
6 use malkusch\lock\mutex\NoMutex
;
7 use Shaarli\Bookmark\BookmarkFileService
;
8 use Shaarli\Config\ConfigManager
;
10 use Shaarli\Plugin\PluginManager
;
12 use Slim\Http\Environment
;
13 use Slim\Http\Request
;
14 use Slim\Http\Response
;
16 class DeleteLinkTest
extends \Shaarli\TestCase
19 * @var string datastore to test write operations
21 protected static $testDatastore = 'sandbox/datastore.php';
24 * @var string datastore to test write operations
26 protected static $testHistory = 'sandbox/history.php';
29 * @var ConfigManager instance
34 * @var \ReferenceLinkDB instance.
36 protected $refDB = null;
39 * @var BookmarkFileService instance.
41 protected $bookmarkService;
44 * @var HistoryController instance.
49 * @var Container instance.
54 * @var Links controller instance.
56 protected $controller;
61 /** @var PluginManager */
62 protected $pluginManager;
65 * Before each test, instantiate a new Api with its config, plugins and bookmarks.
67 protected function setUp(): void
69 $this->mutex
= new NoMutex();
70 $this->conf
= new ConfigManager('tests/utils/config/configJson');
71 $this->conf
->set('resource.datastore', self
::$testDatastore);
72 $this->refDB
= new \
ReferenceLinkDB();
73 $this->refDB
->write(self
::$testDatastore);
74 $refHistory = new \
ReferenceHistory();
75 $refHistory->write(self
::$testHistory);
76 $this->history
= new History(self
::$testHistory);
77 $this->pluginManager
= new PluginManager($this->conf
);
78 $this->bookmarkService
= new BookmarkFileService(
86 $this->container
= new Container();
87 $this->container
['conf'] = $this->conf
;
88 $this->container
['db'] = $this->bookmarkService
;
89 $this->container
['history'] = $this->history
;
91 $this->controller
= new Links($this->container
);
95 * After each test, remove the test datastore.
97 protected function tearDown(): void
99 @unlink(self
::$testDatastore);
100 @unlink(self
::$testHistory);
104 * Test DELETE link endpoint: the link should be removed.
106 public function testDeleteLinkValid()
109 $this->assertTrue($this->bookmarkService
->exists($id));
110 $env = Environment
::mock([
111 'REQUEST_METHOD' => 'DELETE',
113 $request = Request
::createFromEnvironment($env);
115 $response = $this->controller
->deleteLink($request, new Response(), ['id' => $id]);
116 $this->assertEquals(204, $response->getStatusCode());
117 $this->assertEmpty((string) $response->getBody());
119 $this->bookmarkService
= new BookmarkFileService(
121 $this->pluginManager
,
126 $this->assertFalse($this->bookmarkService
->exists($id));
128 $historyEntry = $this->history
->getHistory()[0];
129 $this->assertEquals(History
::DELETED
, $historyEntry['event']);
131 (new \
DateTime())->add(\DateInterval
::createFromDateString('-5 seconds')) < $historyEntry['datetime']
133 $this->assertEquals($id, $historyEntry['id']);
137 * Test DELETE link endpoint: reach not existing ID.
139 public function testDeleteLink404()
141 $this->expectException(\Shaarli\Api\Exceptions\ApiLinkNotFoundException
::class);
144 $this->assertFalse($this->bookmarkService
->exists($id));
145 $env = Environment
::mock([
146 'REQUEST_METHOD' => 'DELETE',
148 $request = Request
::createFromEnvironment($env);
150 $this->controller
->deleteLink($request, new Response(), ['id' => $id]);