3 namespace Shaarli\Api\Controllers
;
5 use Shaarli\Api\Exceptions\ApiBadParametersException
;
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 PutTagTest
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 HistoryController instance.
43 * @var Container instance.
48 * @var BookmarkFileService instance.
50 protected $bookmarkService;
53 * @var Tags controller instance.
55 protected $controller;
58 * Number of JSON field per link.
60 const NB_FIELDS_TAG
= 2;
63 * Before every test, instantiate a new Api with its config, plugins and bookmarks.
65 protected function setUp(): void
67 $this->conf
= new ConfigManager('tests/utils/config/configJson');
68 $this->conf
->set('resource.datastore', self
::$testDatastore);
69 $this->refDB
= new \
ReferenceLinkDB();
70 $this->refDB
->write(self
::$testDatastore);
71 $refHistory = new \
ReferenceHistory();
72 $refHistory->write(self
::$testHistory);
73 $this->history
= new History(self
::$testHistory);
74 $this->bookmarkService
= new BookmarkFileService($this->conf
, $this->history
, true);
76 $this->container
= new Container();
77 $this->container
['conf'] = $this->conf
;
78 $this->container
['db'] = $this->bookmarkService
;
79 $this->container
['history'] = $this->history
;
81 $this->controller
= new Tags($this->container
);
85 * After every test, remove the test datastore.
87 protected function tearDown(): void
89 @unlink(self
::$testDatastore);
90 @unlink(self
::$testHistory);
96 public function testPutLinkValid()
98 $env = Environment
::mock([
99 'REQUEST_METHOD' => 'PUT',
102 $update = ['name' => $newName = 'newtag'];
103 $request = Request
::createFromEnvironment($env);
104 $request = $request->withParsedBody($update);
106 $response = $this->controller
->putTag($request, new Response(), ['tagName' => $tagName]);
107 $this->assertEquals(200, $response->getStatusCode());
108 $data = json_decode((string) $response->getBody(), true);
109 $this->assertEquals(self
::NB_FIELDS_TAG
, count($data));
110 $this->assertEquals($newName, $data['name']);
111 $this->assertEquals(2, $data['occurrences']);
113 $tags = $this->bookmarkService
->bookmarksCountPerTag();
114 $this->assertNotTrue(isset($tags[$tagName]));
115 $this->assertEquals(2, $tags[$newName]);
117 $historyEntry = $this->history
->getHistory()[0];
118 $this->assertEquals(History
::UPDATED
, $historyEntry['event']);
120 (new \
DateTime())->add(\DateInterval
::createFromDateString('-5 seconds')) < $historyEntry['datetime']
122 $historyEntry = $this->history
->getHistory()[1];
123 $this->assertEquals(History
::UPDATED
, $historyEntry['event']);
125 (new \
DateTime())->add(\DateInterval
::createFromDateString('-5 seconds')) < $historyEntry['datetime']
130 * Test tag update with an existing tag: they should be merged
132 public function testPutTagMerge()
137 $tags = $this->bookmarkService
->bookmarksCountPerTag();
138 $this->assertEquals(1, $tags[$newName]);
139 $this->assertEquals(2, $tags[$tagName]);
141 $env = Environment
::mock([
142 'REQUEST_METHOD' => 'PUT',
144 $update = ['name' => $newName];
145 $request = Request
::createFromEnvironment($env);
146 $request = $request->withParsedBody($update);
148 $response = $this->controller
->putTag($request, new Response(), ['tagName' => $tagName]);
149 $this->assertEquals(200, $response->getStatusCode());
150 $data = json_decode((string) $response->getBody(), true);
151 $this->assertEquals(self
::NB_FIELDS_TAG
, count($data));
152 $this->assertEquals($newName, $data['name']);
153 $this->assertEquals(3, $data['occurrences']);
155 $tags = $this->bookmarkService
->bookmarksCountPerTag();
156 $this->assertNotTrue(isset($tags[$tagName]));
157 $this->assertEquals(3, $tags[$newName]);
161 * Test tag update with an empty new tag name => ApiBadParametersException
163 * @expectedException Shaarli\Api\Exceptions\ApiBadParametersException
164 * @expectedExceptionMessage New tag name is required in the request body
166 public function testPutTagEmpty()
171 $tags = $this->bookmarkService
->bookmarksCountPerTag();
172 $this->assertEquals(2, $tags[$tagName]);
174 $env = Environment
::mock([
175 'REQUEST_METHOD' => 'PUT',
177 $request = Request
::createFromEnvironment($env);
179 $env = Environment
::mock([
180 'REQUEST_METHOD' => 'PUT',
182 $update = ['name' => $newName];
183 $request = Request
::createFromEnvironment($env);
184 $request = $request->withParsedBody($update);
187 $this->controller
->putTag($request, new Response(), ['tagName' => $tagName]);
188 } catch (ApiBadParametersException
$e) {
189 $tags = $this->bookmarkService
->bookmarksCountPerTag();
190 $this->assertEquals(2, $tags[$tagName]);
196 * Test tag update on non existent tag => ApiTagNotFoundException.
198 * @expectedException Shaarli\Api\Exceptions\ApiTagNotFoundException
199 * @expectedExceptionMessage Tag not found
201 public function testPutTag404()
203 $env = Environment
::mock([
204 'REQUEST_METHOD' => 'PUT',
206 $request = Request
::createFromEnvironment($env);
208 $this->controller
->putTag($request, new Response(), ['tagName' => 'nopenope']);