]>
Commit | Line | Data |
---|---|---|
1 | <?php | |
2 | ||
3 | namespace Shaarli\Api\Controllers; | |
4 | ||
5 | use malkusch\lock\mutex\NoMutex; | |
6 | use Shaarli\Api\Exceptions\ApiBadParametersException; | |
7 | use Shaarli\Bookmark\BookmarkFileService; | |
8 | use Shaarli\Bookmark\LinkDB; | |
9 | use Shaarli\Config\ConfigManager; | |
10 | use Shaarli\History; | |
11 | use Slim\Container; | |
12 | use Slim\Http\Environment; | |
13 | use Slim\Http\Request; | |
14 | use Slim\Http\Response; | |
15 | ||
16 | class PutTagTest extends \Shaarli\TestCase | |
17 | { | |
18 | /** | |
19 | * @var string datastore to test write operations | |
20 | */ | |
21 | protected static $testDatastore = 'sandbox/datastore.php'; | |
22 | ||
23 | /** | |
24 | * @var string datastore to test write operations | |
25 | */ | |
26 | protected static $testHistory = 'sandbox/history.php'; | |
27 | ||
28 | /** | |
29 | * @var ConfigManager instance | |
30 | */ | |
31 | protected $conf; | |
32 | ||
33 | /** | |
34 | * @var \ReferenceLinkDB instance. | |
35 | */ | |
36 | protected $refDB = null; | |
37 | ||
38 | /** | |
39 | * @var HistoryController instance. | |
40 | */ | |
41 | protected $history; | |
42 | ||
43 | /** | |
44 | * @var Container instance. | |
45 | */ | |
46 | protected $container; | |
47 | ||
48 | /** | |
49 | * @var BookmarkFileService instance. | |
50 | */ | |
51 | protected $bookmarkService; | |
52 | ||
53 | /** | |
54 | * @var Tags controller instance. | |
55 | */ | |
56 | protected $controller; | |
57 | ||
58 | /** | |
59 | * Number of JSON field per link. | |
60 | */ | |
61 | const NB_FIELDS_TAG = 2; | |
62 | ||
63 | /** | |
64 | * Before every test, instantiate a new Api with its config, plugins and bookmarks. | |
65 | */ | |
66 | protected function setUp(): void | |
67 | { | |
68 | $mutex = new NoMutex(); | |
69 | $this->conf = new ConfigManager('tests/utils/config/configJson'); | |
70 | $this->conf->set('resource.datastore', self::$testDatastore); | |
71 | $this->refDB = new \ReferenceLinkDB(); | |
72 | $this->refDB->write(self::$testDatastore); | |
73 | $refHistory = new \ReferenceHistory(); | |
74 | $refHistory->write(self::$testHistory); | |
75 | $this->history = new History(self::$testHistory); | |
76 | $this->bookmarkService = new BookmarkFileService($this->conf, $this->history, $mutex, true); | |
77 | ||
78 | $this->container = new Container(); | |
79 | $this->container['conf'] = $this->conf; | |
80 | $this->container['db'] = $this->bookmarkService; | |
81 | $this->container['history'] = $this->history; | |
82 | ||
83 | $this->controller = new Tags($this->container); | |
84 | } | |
85 | ||
86 | /** | |
87 | * After every test, remove the test datastore. | |
88 | */ | |
89 | protected function tearDown(): void | |
90 | { | |
91 | @unlink(self::$testDatastore); | |
92 | @unlink(self::$testHistory); | |
93 | } | |
94 | ||
95 | /** | |
96 | * Test tags update | |
97 | */ | |
98 | public function testPutLinkValid() | |
99 | { | |
100 | $env = Environment::mock([ | |
101 | 'REQUEST_METHOD' => 'PUT', | |
102 | ]); | |
103 | $tagName = 'gnu'; | |
104 | $update = ['name' => $newName = 'newtag']; | |
105 | $request = Request::createFromEnvironment($env); | |
106 | $request = $request->withParsedBody($update); | |
107 | ||
108 | $response = $this->controller->putTag($request, new Response(), ['tagName' => $tagName]); | |
109 | $this->assertEquals(200, $response->getStatusCode()); | |
110 | $data = json_decode((string) $response->getBody(), true); | |
111 | $this->assertEquals(self::NB_FIELDS_TAG, count($data)); | |
112 | $this->assertEquals($newName, $data['name']); | |
113 | $this->assertEquals(2, $data['occurrences']); | |
114 | ||
115 | $tags = $this->bookmarkService->bookmarksCountPerTag(); | |
116 | $this->assertNotTrue(isset($tags[$tagName])); | |
117 | $this->assertEquals(2, $tags[$newName]); | |
118 | ||
119 | $historyEntry = $this->history->getHistory()[0]; | |
120 | $this->assertEquals(History::UPDATED, $historyEntry['event']); | |
121 | $this->assertTrue( | |
122 | (new \DateTime())->add(\DateInterval::createFromDateString('-5 seconds')) < $historyEntry['datetime'] | |
123 | ); | |
124 | $historyEntry = $this->history->getHistory()[1]; | |
125 | $this->assertEquals(History::UPDATED, $historyEntry['event']); | |
126 | $this->assertTrue( | |
127 | (new \DateTime())->add(\DateInterval::createFromDateString('-5 seconds')) < $historyEntry['datetime'] | |
128 | ); | |
129 | } | |
130 | ||
131 | /** | |
132 | * Test tag update with an existing tag: they should be merged | |
133 | */ | |
134 | public function testPutTagMerge() | |
135 | { | |
136 | $tagName = 'gnu'; | |
137 | $newName = 'w3c'; | |
138 | ||
139 | $tags = $this->bookmarkService->bookmarksCountPerTag(); | |
140 | $this->assertEquals(1, $tags[$newName]); | |
141 | $this->assertEquals(2, $tags[$tagName]); | |
142 | ||
143 | $env = Environment::mock([ | |
144 | 'REQUEST_METHOD' => 'PUT', | |
145 | ]); | |
146 | $update = ['name' => $newName]; | |
147 | $request = Request::createFromEnvironment($env); | |
148 | $request = $request->withParsedBody($update); | |
149 | ||
150 | $response = $this->controller->putTag($request, new Response(), ['tagName' => $tagName]); | |
151 | $this->assertEquals(200, $response->getStatusCode()); | |
152 | $data = json_decode((string) $response->getBody(), true); | |
153 | $this->assertEquals(self::NB_FIELDS_TAG, count($data)); | |
154 | $this->assertEquals($newName, $data['name']); | |
155 | $this->assertEquals(3, $data['occurrences']); | |
156 | ||
157 | $tags = $this->bookmarkService->bookmarksCountPerTag(); | |
158 | $this->assertNotTrue(isset($tags[$tagName])); | |
159 | $this->assertEquals(3, $tags[$newName]); | |
160 | } | |
161 | ||
162 | /** | |
163 | * Test tag update with an empty new tag name => ApiBadParametersException | |
164 | */ | |
165 | public function testPutTagEmpty() | |
166 | { | |
167 | $this->expectException(\Shaarli\Api\Exceptions\ApiBadParametersException::class); | |
168 | $this->expectExceptionMessage('New tag name is required in the request body'); | |
169 | ||
170 | $tagName = 'gnu'; | |
171 | $newName = ''; | |
172 | ||
173 | $tags = $this->bookmarkService->bookmarksCountPerTag(); | |
174 | $this->assertEquals(2, $tags[$tagName]); | |
175 | ||
176 | $env = Environment::mock([ | |
177 | 'REQUEST_METHOD' => 'PUT', | |
178 | ]); | |
179 | $request = Request::createFromEnvironment($env); | |
180 | ||
181 | $env = Environment::mock([ | |
182 | 'REQUEST_METHOD' => 'PUT', | |
183 | ]); | |
184 | $update = ['name' => $newName]; | |
185 | $request = Request::createFromEnvironment($env); | |
186 | $request = $request->withParsedBody($update); | |
187 | ||
188 | try { | |
189 | $this->controller->putTag($request, new Response(), ['tagName' => $tagName]); | |
190 | } catch (ApiBadParametersException $e) { | |
191 | $tags = $this->bookmarkService->bookmarksCountPerTag(); | |
192 | $this->assertEquals(2, $tags[$tagName]); | |
193 | throw $e; | |
194 | } | |
195 | } | |
196 | ||
197 | /** | |
198 | * Test tag update on non existent tag => ApiTagNotFoundException. | |
199 | */ | |
200 | public function testPutTag404() | |
201 | { | |
202 | $this->expectException(\Shaarli\Api\Exceptions\ApiTagNotFoundException::class); | |
203 | $this->expectExceptionMessage('Tag not found'); | |
204 | ||
205 | $env = Environment::mock([ | |
206 | 'REQUEST_METHOD' => 'PUT', | |
207 | ]); | |
208 | $request = Request::createFromEnvironment($env); | |
209 | ||
210 | $this->controller->putTag($request, new Response(), ['tagName' => 'nopenope']); | |
211 | } | |
212 | } |