4 namespace Shaarli\Api\Controllers
;
6 use malkusch\lock\mutex\NoMutex
;
7 use Shaarli\Bookmark\BookmarkFileService
;
8 use Shaarli\Config\ConfigManager
;
11 use Slim\Http\Environment
;
12 use Slim\Http\Request
;
13 use Slim\Http\Response
;
15 class DeleteLinkTest
extends \Shaarli\TestCase
18 * @var string datastore to test write operations
20 protected static $testDatastore = 'sandbox/datastore.php';
23 * @var string datastore to test write operations
25 protected static $testHistory = 'sandbox/history.php';
28 * @var ConfigManager instance
33 * @var \ReferenceLinkDB instance.
35 protected $refDB = null;
38 * @var BookmarkFileService instance.
40 protected $bookmarkService;
43 * @var HistoryController instance.
48 * @var Container instance.
53 * @var Links controller instance.
55 protected $controller;
61 * Before each test, instantiate a new Api with its config, plugins and bookmarks.
63 protected function setUp(): void
65 $this->mutex
= new NoMutex();
66 $this->conf
= new ConfigManager('tests/utils/config/configJson');
67 $this->conf
->set('resource.datastore', self
::$testDatastore);
68 $this->refDB
= new \
ReferenceLinkDB();
69 $this->refDB
->write(self
::$testDatastore);
70 $refHistory = new \
ReferenceHistory();
71 $refHistory->write(self
::$testHistory);
72 $this->history
= new History(self
::$testHistory);
73 $this->bookmarkService
= new BookmarkFileService($this->conf
, $this->history
, $this->mutex
, true);
75 $this->container
= new Container();
76 $this->container
['conf'] = $this->conf
;
77 $this->container
['db'] = $this->bookmarkService
;
78 $this->container
['history'] = $this->history
;
80 $this->controller
= new Links($this->container
);
84 * After each test, remove the test datastore.
86 protected function tearDown(): void
88 @unlink(self
::$testDatastore);
89 @unlink(self
::$testHistory);
93 * Test DELETE link endpoint: the link should be removed.
95 public function testDeleteLinkValid()
98 $this->assertTrue($this->bookmarkService
->exists($id));
99 $env = Environment
::mock([
100 'REQUEST_METHOD' => 'DELETE',
102 $request = Request
::createFromEnvironment($env);
104 $response = $this->controller
->deleteLink($request, new Response(), ['id' => $id]);
105 $this->assertEquals(204, $response->getStatusCode());
106 $this->assertEmpty((string) $response->getBody());
108 $this->bookmarkService
= new BookmarkFileService($this->conf
, $this->history
, $this->mutex
, true);
109 $this->assertFalse($this->bookmarkService
->exists($id));
111 $historyEntry = $this->history
->getHistory()[0];
112 $this->assertEquals(History
::DELETED
, $historyEntry['event']);
114 (new \
DateTime())->add(\DateInterval
::createFromDateString('-5 seconds')) < $historyEntry['datetime']
116 $this->assertEquals($id, $historyEntry['id']);
120 * Test DELETE link endpoint: reach not existing ID.
122 public function testDeleteLink404()
124 $this->expectException(\Shaarli\Api\Exceptions\ApiLinkNotFoundException
::class);
127 $this->assertFalse($this->bookmarkService
->exists($id));
128 $env = Environment
::mock([
129 'REQUEST_METHOD' => 'DELETE',
131 $request = Request
::createFromEnvironment($env);
133 $this->controller
->deleteLink($request, new Response(), ['id' => $id]);