4 namespace Shaarli\Api\Controllers
;
6 use Shaarli\Config\ConfigManager
;
8 use Slim\Http\Environment
;
10 use Slim\Http\Response
;
12 class DeleteTagTest
extends \PHPUnit_Framework_TestCase
15 * @var string datastore to test write operations
17 protected static $testDatastore = 'sandbox/datastore.php';
20 * @var string datastore to test write operations
22 protected static $testHistory = 'sandbox/history.php';
25 * @var ConfigManager instance
30 * @var \ReferenceLinkDB instance.
32 protected $refDB = null;
35 * @var \LinkDB instance.
40 * @var \History instance.
45 * @var Container instance.
50 * @var Tags controller instance.
52 protected $controller;
55 * Before each test, instantiate a new Api with its config, plugins and links.
57 public function setUp()
59 $this->conf
= new ConfigManager('tests/utils/config/configJson');
60 $this->refDB
= new \
ReferenceLinkDB();
61 $this->refDB
->write(self
::$testDatastore);
62 $this->linkDB
= new \
LinkDB(self
::$testDatastore, true, false);
63 $refHistory = new \
ReferenceHistory();
64 $refHistory->write(self
::$testHistory);
65 $this->history
= new \
History(self
::$testHistory);
66 $this->container
= new Container();
67 $this->container
['conf'] = $this->conf
;
68 $this->container
['db'] = $this->linkDB
;
69 $this->container
['history'] = $this->history
;
71 $this->controller
= new Tags($this->container
);
75 * After each test, remove the test datastore.
77 public function tearDown()
79 @unlink(self
::$testDatastore);
80 @unlink(self
::$testHistory);
84 * Test DELETE tag endpoint: the tag should be removed.
86 public function testDeleteTagValid()
89 $tags = $this->linkDB
->linksCountPerTag();
90 $this->assertTrue($tags[$tagName] > 0);
91 $env = Environment
::mock([
92 'REQUEST_METHOD' => 'DELETE',
94 $request = Request
::createFromEnvironment($env);
96 $response = $this->controller
->deleteTag($request, new Response(), ['tagName' => $tagName]);
97 $this->assertEquals(204, $response->getStatusCode());
98 $this->assertEmpty((string) $response->getBody());
100 $this->linkDB
= new \
LinkDB(self
::$testDatastore, true, false);
101 $tags = $this->linkDB
->linksCountPerTag();
102 $this->assertFalse(isset($tags[$tagName]));
105 $historyEntry = $this->history
->getHistory()[0];
106 $this->assertEquals(\History
::UPDATED
, $historyEntry['event']);
108 (new \
DateTime())->add(\DateInterval
::createFromDateString('-5 seconds')) < $historyEntry['datetime']
110 $historyEntry = $this->history
->getHistory()[1];
111 $this->assertEquals(\History
::UPDATED
, $historyEntry['event']);
113 (new \
DateTime())->add(\DateInterval
::createFromDateString('-5 seconds')) < $historyEntry['datetime']
118 * Test DELETE tag endpoint: the tag should be removed.
120 public function testDeleteTagCaseSensitivity()
123 $tags = $this->linkDB
->linksCountPerTag();
124 $this->assertTrue($tags[$tagName] > 0);
125 $env = Environment
::mock([
126 'REQUEST_METHOD' => 'DELETE',
128 $request = Request
::createFromEnvironment($env);
130 $response = $this->controller
->deleteTag($request, new Response(), ['tagName' => $tagName]);
131 $this->assertEquals(204, $response->getStatusCode());
132 $this->assertEmpty((string) $response->getBody());
134 $this->linkDB
= new \
LinkDB(self
::$testDatastore, true, false);
135 $tags = $this->linkDB
->linksCountPerTag();
136 $this->assertFalse(isset($tags[$tagName]));
137 $this->assertTrue($tags[strtolower($tagName)] > 0);
139 $historyEntry = $this->history
->getHistory()[0];
140 $this->assertEquals(\History
::UPDATED
, $historyEntry['event']);
142 (new \
DateTime())->add(\DateInterval
::createFromDateString('-5 seconds')) < $historyEntry['datetime']
147 * Test DELETE link endpoint: reach not existing ID.
149 * @expectedException Shaarli\Api\Exceptions\ApiTagNotFoundException
150 * @expectedExceptionMessage Tag not found
152 public function testDeleteLink404()
154 $tagName = 'nopenope';
155 $tags = $this->linkDB
->linksCountPerTag();
156 $this->assertFalse(isset($tags[$tagName]));
157 $env = Environment
::mock([
158 'REQUEST_METHOD' => 'DELETE',
160 $request = Request
::createFromEnvironment($env);
162 $this->controller
->deleteTag($request, new Response(), ['tagName' => $tagName]);