4 namespace Shaarli\Api\Controllers
;
6 use Shaarli\Bookmark\LinkDB
;
7 use Shaarli\Config\ConfigManager
;
10 use Slim\Http\Environment
;
11 use Slim\Http\Request
;
12 use Slim\Http\Response
;
14 class DeleteTagTest
extends \PHPUnit\Framework\TestCase
17 * @var string datastore to test write operations
19 protected static $testDatastore = 'sandbox/datastore.php';
22 * @var string datastore to test write operations
24 protected static $testHistory = 'sandbox/history.php';
27 * @var ConfigManager instance
32 * @var \ReferenceLinkDB instance.
34 protected $refDB = null;
37 * @var LinkDB instance.
42 * @var HistoryController instance.
47 * @var Container instance.
52 * @var Tags controller instance.
54 protected $controller;
57 * Before each test, instantiate a new Api with its config, plugins and links.
59 public function setUp()
61 $this->conf
= new ConfigManager('tests/utils/config/configJson');
62 $this->refDB
= new \
ReferenceLinkDB();
63 $this->refDB
->write(self
::$testDatastore);
64 $this->linkDB
= new LinkDB(self
::$testDatastore, true, false);
65 $refHistory = new \
ReferenceHistory();
66 $refHistory->write(self
::$testHistory);
67 $this->history
= new History(self
::$testHistory);
68 $this->container
= new Container();
69 $this->container
['conf'] = $this->conf
;
70 $this->container
['db'] = $this->linkDB
;
71 $this->container
['history'] = $this->history
;
73 $this->controller
= new Tags($this->container
);
77 * After each test, remove the test datastore.
79 public function tearDown()
81 @unlink(self
::$testDatastore);
82 @unlink(self
::$testHistory);
86 * Test DELETE tag endpoint: the tag should be removed.
88 public function testDeleteTagValid()
91 $tags = $this->linkDB
->linksCountPerTag();
92 $this->assertTrue($tags[$tagName] > 0);
93 $env = Environment
::mock([
94 'REQUEST_METHOD' => 'DELETE',
96 $request = Request
::createFromEnvironment($env);
98 $response = $this->controller
->deleteTag($request, new Response(), ['tagName' => $tagName]);
99 $this->assertEquals(204, $response->getStatusCode());
100 $this->assertEmpty((string) $response->getBody());
102 $this->linkDB
= new LinkDB(self
::$testDatastore, true, false);
103 $tags = $this->linkDB
->linksCountPerTag();
104 $this->assertFalse(isset($tags[$tagName]));
107 $historyEntry = $this->history
->getHistory()[0];
108 $this->assertEquals(History
::UPDATED
, $historyEntry['event']);
110 (new \
DateTime())->add(\DateInterval
::createFromDateString('-5 seconds')) < $historyEntry['datetime']
112 $historyEntry = $this->history
->getHistory()[1];
113 $this->assertEquals(History
::UPDATED
, $historyEntry['event']);
115 (new \
DateTime())->add(\DateInterval
::createFromDateString('-5 seconds')) < $historyEntry['datetime']
120 * Test DELETE tag endpoint: the tag should be removed.
122 public function testDeleteTagCaseSensitivity()
125 $tags = $this->linkDB
->linksCountPerTag();
126 $this->assertTrue($tags[$tagName] > 0);
127 $env = Environment
::mock([
128 'REQUEST_METHOD' => 'DELETE',
130 $request = Request
::createFromEnvironment($env);
132 $response = $this->controller
->deleteTag($request, new Response(), ['tagName' => $tagName]);
133 $this->assertEquals(204, $response->getStatusCode());
134 $this->assertEmpty((string) $response->getBody());
136 $this->linkDB
= new LinkDB(self
::$testDatastore, true, false);
137 $tags = $this->linkDB
->linksCountPerTag();
138 $this->assertFalse(isset($tags[$tagName]));
139 $this->assertTrue($tags[strtolower($tagName)] > 0);
141 $historyEntry = $this->history
->getHistory()[0];
142 $this->assertEquals(History
::UPDATED
, $historyEntry['event']);
144 (new \
DateTime())->add(\DateInterval
::createFromDateString('-5 seconds')) < $historyEntry['datetime']
149 * Test DELETE tag endpoint: reach not existing tag.
151 * @expectedException Shaarli\Api\Exceptions\ApiTagNotFoundException
152 * @expectedExceptionMessage Tag not found
154 public function testDeleteLink404()
156 $tagName = 'nopenope';
157 $tags = $this->linkDB
->linksCountPerTag();
158 $this->assertFalse(isset($tags[$tagName]));
159 $env = Environment
::mock([
160 'REQUEST_METHOD' => 'DELETE',
162 $request = Request
::createFromEnvironment($env);
164 $this->controller
->deleteTag($request, new Response(), ['tagName' => $tagName]);