]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - tests/api/controllers/tags/DeleteTagTest.php
Optimize and cleanup imports
[github/shaarli/Shaarli.git] / tests / api / controllers / tags / DeleteTagTest.php
CommitLineData
0843848c
A
1<?php
2
3
4namespace Shaarli\Api\Controllers;
5
dea72c71 6use Shaarli\Bookmark\LinkDB;
0843848c 7use Shaarli\Config\ConfigManager;
dea72c71 8use Shaarli\History;
0843848c
A
9use Slim\Container;
10use Slim\Http\Environment;
11use Slim\Http\Request;
12use Slim\Http\Response;
13
dea72c71 14class DeleteTagTest extends \PHPUnit\Framework\TestCase
0843848c
A
15{
16 /**
17 * @var string datastore to test write operations
18 */
19 protected static $testDatastore = 'sandbox/datastore.php';
20
813849e5
A
21 /**
22 * @var string datastore to test write operations
23 */
24 protected static $testHistory = 'sandbox/history.php';
25
0843848c
A
26 /**
27 * @var ConfigManager instance
28 */
29 protected $conf;
30
31 /**
32 * @var \ReferenceLinkDB instance.
33 */
34 protected $refDB = null;
35
36 /**
dea72c71 37 * @var LinkDB instance.
0843848c
A
38 */
39 protected $linkDB;
40
813849e5 41 /**
dea72c71 42 * @var HistoryController instance.
813849e5
A
43 */
44 protected $history;
45
0843848c
A
46 /**
47 * @var Container instance.
48 */
49 protected $container;
50
51 /**
d3f42ca4 52 * @var Tags controller instance.
0843848c
A
53 */
54 protected $controller;
55
56 /**
57 * Before each test, instantiate a new Api with its config, plugins and links.
58 */
59 public function setUp()
60 {
61 $this->conf = new ConfigManager('tests/utils/config/configJson');
62 $this->refDB = new \ReferenceLinkDB();
63 $this->refDB->write(self::$testDatastore);
dea72c71 64 $this->linkDB = new LinkDB(self::$testDatastore, true, false);
813849e5
A
65 $refHistory = new \ReferenceHistory();
66 $refHistory->write(self::$testHistory);
dea72c71 67 $this->history = new History(self::$testHistory);
0843848c
A
68 $this->container = new Container();
69 $this->container['conf'] = $this->conf;
70 $this->container['db'] = $this->linkDB;
813849e5 71 $this->container['history'] = $this->history;
0843848c 72
d3f42ca4 73 $this->controller = new Tags($this->container);
0843848c
A
74 }
75
76 /**
77 * After each test, remove the test datastore.
78 */
79 public function tearDown()
80 {
81 @unlink(self::$testDatastore);
813849e5 82 @unlink(self::$testHistory);
0843848c
A
83 }
84
85 /**
d3f42ca4 86 * Test DELETE tag endpoint: the tag should be removed.
0843848c 87 */
d3f42ca4 88 public function testDeleteTagValid()
0843848c 89 {
d3f42ca4
A
90 $tagName = 'gnu';
91 $tags = $this->linkDB->linksCountPerTag();
92 $this->assertTrue($tags[$tagName] > 0);
0843848c
A
93 $env = Environment::mock([
94 'REQUEST_METHOD' => 'DELETE',
95 ]);
96 $request = Request::createFromEnvironment($env);
97
d3f42ca4 98 $response = $this->controller->deleteTag($request, new Response(), ['tagName' => $tagName]);
0843848c
A
99 $this->assertEquals(204, $response->getStatusCode());
100 $this->assertEmpty((string) $response->getBody());
101
dea72c71 102 $this->linkDB = new LinkDB(self::$testDatastore, true, false);
d3f42ca4
A
103 $tags = $this->linkDB->linksCountPerTag();
104 $this->assertFalse(isset($tags[$tagName]));
813849e5 105
d3f42ca4 106 // 2 links affected
813849e5 107 $historyEntry = $this->history->getHistory()[0];
dea72c71 108 $this->assertEquals(History::UPDATED, $historyEntry['event']);
d3f42ca4
A
109 $this->assertTrue(
110 (new \DateTime())->add(\DateInterval::createFromDateString('-5 seconds')) < $historyEntry['datetime']
111 );
112 $historyEntry = $this->history->getHistory()[1];
dea72c71 113 $this->assertEquals(History::UPDATED, $historyEntry['event']);
d3f42ca4
A
114 $this->assertTrue(
115 (new \DateTime())->add(\DateInterval::createFromDateString('-5 seconds')) < $historyEntry['datetime']
116 );
117 }
118
119 /**
120 * Test DELETE tag endpoint: the tag should be removed.
121 */
122 public function testDeleteTagCaseSensitivity()
123 {
124 $tagName = 'sTuff';
125 $tags = $this->linkDB->linksCountPerTag();
126 $this->assertTrue($tags[$tagName] > 0);
127 $env = Environment::mock([
128 'REQUEST_METHOD' => 'DELETE',
129 ]);
130 $request = Request::createFromEnvironment($env);
131
132 $response = $this->controller->deleteTag($request, new Response(), ['tagName' => $tagName]);
133 $this->assertEquals(204, $response->getStatusCode());
134 $this->assertEmpty((string) $response->getBody());
135
dea72c71 136 $this->linkDB = new LinkDB(self::$testDatastore, true, false);
d3f42ca4
A
137 $tags = $this->linkDB->linksCountPerTag();
138 $this->assertFalse(isset($tags[$tagName]));
139 $this->assertTrue($tags[strtolower($tagName)] > 0);
140
141 $historyEntry = $this->history->getHistory()[0];
dea72c71 142 $this->assertEquals(History::UPDATED, $historyEntry['event']);
813849e5
A
143 $this->assertTrue(
144 (new \DateTime())->add(\DateInterval::createFromDateString('-5 seconds')) < $historyEntry['datetime']
145 );
0843848c
A
146 }
147
148 /**
7c57bd95 149 * Test DELETE tag endpoint: reach not existing tag.
0843848c 150 *
d3f42ca4
A
151 * @expectedException Shaarli\Api\Exceptions\ApiTagNotFoundException
152 * @expectedExceptionMessage Tag not found
0843848c
A
153 */
154 public function testDeleteLink404()
155 {
d3f42ca4
A
156 $tagName = 'nopenope';
157 $tags = $this->linkDB->linksCountPerTag();
158 $this->assertFalse(isset($tags[$tagName]));
0843848c
A
159 $env = Environment::mock([
160 'REQUEST_METHOD' => 'DELETE',
161 ]);
162 $request = Request::createFromEnvironment($env);
163
d3f42ca4 164 $this->controller->deleteTag($request, new Response(), ['tagName' => $tagName]);
0843848c
A
165 }
166}