4 namespace Shaarli\Api\Controllers
;
6 use Shaarli\Bookmark\BookmarkFileService
;
7 use Shaarli\Bookmark\LinkDB
;
8 use Shaarli\Config\ConfigManager
;
11 use Slim\Http\Environment
;
12 use Slim\Http\Request
;
13 use Slim\Http\Response
;
15 class DeleteTagTest
extends \PHPUnit\Framework\TestCase
18 * @var string datastore to test write operations
20 protected static $testDatastore = 'sandbox/datastore.php';
23 * @var string datastore to test write operations
25 protected static $testHistory = 'sandbox/history.php';
28 * @var ConfigManager instance
33 * @var \ReferenceLinkDB instance.
35 protected $refDB = null;
38 * @var BookmarkFileService instance.
40 protected $bookmarkService;
43 * @var HistoryController instance.
48 * @var Container instance.
53 * @var Tags controller instance.
55 protected $controller;
58 * Before each test, instantiate a new Api with its config, plugins and bookmarks.
60 public function setUp()
62 $this->conf
= new ConfigManager('tests/utils/config/configJson');
63 $this->conf
->set('resource.datastore', self
::$testDatastore);
64 $this->refDB
= new \
ReferenceLinkDB();
65 $this->refDB
->write(self
::$testDatastore);
66 $refHistory = new \
ReferenceHistory();
67 $refHistory->write(self
::$testHistory);
68 $this->history
= new History(self
::$testHistory);
69 $this->bookmarkService
= new BookmarkFileService($this->conf
, $this->history
, true);
71 $this->container
= new Container();
72 $this->container
['conf'] = $this->conf
;
73 $this->container
['db'] = $this->bookmarkService
;
74 $this->container
['history'] = $this->history
;
76 $this->controller
= new Tags($this->container
);
80 * After each test, remove the test datastore.
82 public function tearDown()
84 @unlink(self
::$testDatastore);
85 @unlink(self
::$testHistory);
89 * Test DELETE tag endpoint: the tag should be removed.
91 public function testDeleteTagValid()
94 $tags = $this->bookmarkService
->bookmarksCountPerTag();
95 $this->assertTrue($tags[$tagName] > 0);
96 $env = Environment
::mock([
97 'REQUEST_METHOD' => 'DELETE',
99 $request = Request
::createFromEnvironment($env);
101 $response = $this->controller
->deleteTag($request, new Response(), ['tagName' => $tagName]);
102 $this->assertEquals(204, $response->getStatusCode());
103 $this->assertEmpty((string) $response->getBody());
105 $this->bookmarkService
= new BookmarkFileService($this->conf
, $this->history
, true);
106 $tags = $this->bookmarkService
->bookmarksCountPerTag();
107 $this->assertFalse(isset($tags[$tagName]));
109 // 2 bookmarks affected
110 $historyEntry = $this->history
->getHistory()[0];
111 $this->assertEquals(History
::UPDATED
, $historyEntry['event']);
113 (new \
DateTime())->add(\DateInterval
::createFromDateString('-5 seconds')) < $historyEntry['datetime']
115 $historyEntry = $this->history
->getHistory()[1];
116 $this->assertEquals(History
::UPDATED
, $historyEntry['event']);
118 (new \
DateTime())->add(\DateInterval
::createFromDateString('-5 seconds')) < $historyEntry['datetime']
123 * Test DELETE tag endpoint: the tag should be removed.
125 public function testDeleteTagCaseSensitivity()
128 $tags = $this->bookmarkService
->bookmarksCountPerTag();
129 $this->assertTrue($tags[$tagName] > 0);
130 $env = Environment
::mock([
131 'REQUEST_METHOD' => 'DELETE',
133 $request = Request
::createFromEnvironment($env);
135 $response = $this->controller
->deleteTag($request, new Response(), ['tagName' => $tagName]);
136 $this->assertEquals(204, $response->getStatusCode());
137 $this->assertEmpty((string) $response->getBody());
139 $this->bookmarkService
= new BookmarkFileService($this->conf
, $this->history
, true);
140 $tags = $this->bookmarkService
->bookmarksCountPerTag();
141 $this->assertFalse(isset($tags[$tagName]));
142 $this->assertTrue($tags[strtolower($tagName)] > 0);
144 $historyEntry = $this->history
->getHistory()[0];
145 $this->assertEquals(History
::UPDATED
, $historyEntry['event']);
147 (new \
DateTime())->add(\DateInterval
::createFromDateString('-5 seconds')) < $historyEntry['datetime']
152 * Test DELETE tag endpoint: reach not existing tag.
154 * @expectedException Shaarli\Api\Exceptions\ApiTagNotFoundException
155 * @expectedExceptionMessage Tag not found
157 public function testDeleteLink404()
159 $tagName = 'nopenope';
160 $tags = $this->bookmarkService
->bookmarksCountPerTag();
161 $this->assertFalse(isset($tags[$tagName]));
162 $env = Environment
::mock([
163 'REQUEST_METHOD' => 'DELETE',
165 $request = Request
::createFromEnvironment($env);
167 $this->controller
->deleteTag($request, new Response(), ['tagName' => $tagName]);