4 namespace Shaarli\Api\Controllers
;
6 use Shaarli\Bookmark\BookmarkFileService
;
7 use Shaarli\Config\ConfigManager
;
10 use Slim\Http\Environment
;
11 use Slim\Http\Request
;
12 use Slim\Http\Response
;
14 class DeleteLinkTest
extends \Shaarli\TestCase
17 * @var string datastore to test write operations
19 protected static $testDatastore = 'sandbox/datastore.php';
22 * @var string datastore to test write operations
24 protected static $testHistory = 'sandbox/history.php';
27 * @var ConfigManager instance
32 * @var \ReferenceLinkDB instance.
34 protected $refDB = null;
37 * @var BookmarkFileService instance.
39 protected $bookmarkService;
42 * @var HistoryController instance.
47 * @var Container instance.
52 * @var Links controller instance.
54 protected $controller;
57 * Before each test, instantiate a new Api with its config, plugins and bookmarks.
59 protected function setUp(): void
61 $this->conf
= new ConfigManager('tests/utils/config/configJson');
62 $this->conf
->set('resource.datastore', self
::$testDatastore);
63 $this->refDB
= new \
ReferenceLinkDB();
64 $this->refDB
->write(self
::$testDatastore);
65 $refHistory = new \
ReferenceHistory();
66 $refHistory->write(self
::$testHistory);
67 $this->history
= new History(self
::$testHistory);
68 $this->bookmarkService
= new BookmarkFileService($this->conf
, $this->history
, true);
70 $this->container
= new Container();
71 $this->container
['conf'] = $this->conf
;
72 $this->container
['db'] = $this->bookmarkService
;
73 $this->container
['history'] = $this->history
;
75 $this->controller
= new Links($this->container
);
79 * After each test, remove the test datastore.
81 protected function tearDown(): void
83 @unlink(self
::$testDatastore);
84 @unlink(self
::$testHistory);
88 * Test DELETE link endpoint: the link should be removed.
90 public function testDeleteLinkValid()
93 $this->assertTrue($this->bookmarkService
->exists($id));
94 $env = Environment
::mock([
95 'REQUEST_METHOD' => 'DELETE',
97 $request = Request
::createFromEnvironment($env);
99 $response = $this->controller
->deleteLink($request, new Response(), ['id' => $id]);
100 $this->assertEquals(204, $response->getStatusCode());
101 $this->assertEmpty((string) $response->getBody());
103 $this->bookmarkService
= new BookmarkFileService($this->conf
, $this->history
, true);
104 $this->assertFalse($this->bookmarkService
->exists($id));
106 $historyEntry = $this->history
->getHistory()[0];
107 $this->assertEquals(History
::DELETED
, $historyEntry['event']);
109 (new \
DateTime())->add(\DateInterval
::createFromDateString('-5 seconds')) < $historyEntry['datetime']
111 $this->assertEquals($id, $historyEntry['id']);
115 * Test DELETE link endpoint: reach not existing ID.
117 public function testDeleteLink404()
119 $this->expectException(\Shaarli\Api\Exceptions\ApiLinkNotFoundException
::class);
122 $this->assertFalse($this->bookmarkService
->exists($id));
123 $env = Environment
::mock([
124 'REQUEST_METHOD' => 'DELETE',
126 $request = Request
::createFromEnvironment($env);
128 $this->controller
->deleteLink($request, new Response(), ['id' => $id]);