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