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