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