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