]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - tests/api/controllers/links/DeleteLinkTest.php
Merge pull request #1697 from ArthurHoaro/feature/pagination
[github/shaarli/Shaarli.git] / tests / api / controllers / links / DeleteLinkTest.php
1 <?php
2
3
4 namespace Shaarli\Api\Controllers;
5
6 use malkusch\lock\mutex\NoMutex;
7 use Shaarli\Bookmark\BookmarkFileService;
8 use Shaarli\Config\ConfigManager;
9 use Shaarli\History;
10 use Slim\Container;
11 use Slim\Http\Environment;
12 use Slim\Http\Request;
13 use Slim\Http\Response;
14
15 class DeleteLinkTest extends \Shaarli\TestCase
16 {
17 /**
18 * @var string datastore to test write operations
19 */
20 protected static $testDatastore = 'sandbox/datastore.php';
21
22 /**
23 * @var string datastore to test write operations
24 */
25 protected static $testHistory = 'sandbox/history.php';
26
27 /**
28 * @var ConfigManager instance
29 */
30 protected $conf;
31
32 /**
33 * @var \ReferenceLinkDB instance.
34 */
35 protected $refDB = null;
36
37 /**
38 * @var BookmarkFileService instance.
39 */
40 protected $bookmarkService;
41
42 /**
43 * @var HistoryController instance.
44 */
45 protected $history;
46
47 /**
48 * @var Container instance.
49 */
50 protected $container;
51
52 /**
53 * @var Links controller instance.
54 */
55 protected $controller;
56
57 /** @var NoMutex */
58 protected $mutex;
59
60 /**
61 * Before each test, instantiate a new Api with its config, plugins and bookmarks.
62 */
63 protected function setUp(): void
64 {
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);
74
75 $this->container = new Container();
76 $this->container['conf'] = $this->conf;
77 $this->container['db'] = $this->bookmarkService;
78 $this->container['history'] = $this->history;
79
80 $this->controller = new Links($this->container);
81 }
82
83 /**
84 * After each test, remove the test datastore.
85 */
86 protected function tearDown(): void
87 {
88 @unlink(self::$testDatastore);
89 @unlink(self::$testHistory);
90 }
91
92 /**
93 * Test DELETE link endpoint: the link should be removed.
94 */
95 public function testDeleteLinkValid()
96 {
97 $id = '41';
98 $this->assertTrue($this->bookmarkService->exists($id));
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
108 $this->bookmarkService = new BookmarkFileService($this->conf, $this->history, $this->mutex, true);
109 $this->assertFalse($this->bookmarkService->exists($id));
110
111 $historyEntry = $this->history->getHistory()[0];
112 $this->assertEquals(History::DELETED, $historyEntry['event']);
113 $this->assertTrue(
114 (new \DateTime())->add(\DateInterval::createFromDateString('-5 seconds')) < $historyEntry['datetime']
115 );
116 $this->assertEquals($id, $historyEntry['id']);
117 }
118
119 /**
120 * Test DELETE link endpoint: reach not existing ID.
121 */
122 public function testDeleteLink404()
123 {
124 $this->expectException(\Shaarli\Api\Exceptions\ApiLinkNotFoundException::class);
125
126 $id = -1;
127 $this->assertFalse($this->bookmarkService->exists($id));
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 }