aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/ApiBundle
diff options
context:
space:
mode:
authorJeremy Benoist <j0k3r@users.noreply.github.com>2016-03-16 23:23:42 +0100
committerJeremy Benoist <j0k3r@users.noreply.github.com>2016-03-16 23:23:42 +0100
commit1978d0e3b63286bbf329b967bf1e6b2e3abef1b4 (patch)
tree60297ba36d01f9ae25dad3febf3f7d54000d52cf /src/Wallabag/ApiBundle
parent3500d4099f88f7db22eba42fdb7e8926f1f1cdac (diff)
parentbc2b947cd54e49a03c267f3c0f13dc5b8a04d962 (diff)
downloadwallabag-1978d0e3b63286bbf329b967bf1e6b2e3abef1b4.tar.gz
wallabag-1978d0e3b63286bbf329b967bf1e6b2e3abef1b4.tar.zst
wallabag-1978d0e3b63286bbf329b967bf1e6b2e3abef1b4.zip
Merge pull request #1791 from wallabag/v2-fix-api-entry-properties
fix api properties and typo
Diffstat (limited to 'src/Wallabag/ApiBundle')
-rw-r--r--src/Wallabag/ApiBundle/Controller/WallabagRestController.php40
-rw-r--r--src/Wallabag/ApiBundle/Tests/Controller/WallabagRestControllerTest.php57
2 files changed, 73 insertions, 24 deletions
diff --git a/src/Wallabag/ApiBundle/Controller/WallabagRestController.php b/src/Wallabag/ApiBundle/Controller/WallabagRestController.php
index 2633a311..744e1a60 100644
--- a/src/Wallabag/ApiBundle/Controller/WallabagRestController.php
+++ b/src/Wallabag/ApiBundle/Controller/WallabagRestController.php
@@ -27,13 +27,13 @@ class WallabagRestController extends FOSRestController
27 * 27 *
28 * @ApiDoc( 28 * @ApiDoc(
29 * parameters={ 29 * parameters={
30 * {"name"="archive", "dataType"="boolean", "required"=false, "format"="true or false, all entries by default", "description"="filter by archived status."}, 30 * {"name"="archive", "dataType"="integer", "required"=false, "format"="1 or 0, all entries by default", "description"="filter by archived status."},
31 * {"name"="star", "dataType"="boolean", "required"=false, "format"="true or false, all entries by default", "description"="filter by starred status."}, 31 * {"name"="starred", "dataType"="integer", "required"=false, "format"="1 or 0, all entries by default", "description"="filter by starred status."},
32 * {"name"="sort", "dataType"="string", "required"=false, "format"="'created' or 'updated', default 'created'", "description"="sort entries by date."}, 32 * {"name"="sort", "dataType"="string", "required"=false, "format"="'created' or 'updated', default 'created'", "description"="sort entries by date."},
33 * {"name"="order", "dataType"="string", "required"=false, "format"="'asc' or 'desc', default 'desc'", "description"="order of sort."}, 33 * {"name"="order", "dataType"="string", "required"=false, "format"="'asc' or 'desc', default 'desc'", "description"="order of sort."},
34 * {"name"="page", "dataType"="integer", "required"=false, "format"="default '1'", "description"="what page you want."}, 34 * {"name"="page", "dataType"="integer", "required"=false, "format"="default '1'", "description"="what page you want."},
35 * {"name"="perPage", "dataType"="integer", "required"=false, "format"="default'30'", "description"="results per page."}, 35 * {"name"="perPage", "dataType"="integer", "required"=false, "format"="default'30'", "description"="results per page."},
36 * {"name"="tags", "dataType"="string", "required"=false, "format"="api%2Crest", "description"="a list of tags url encoded. Will returns entries that matches ALL tags."}, 36 * {"name"="tags", "dataType"="string", "required"=false, "format"="api,rest", "description"="a list of tags url encoded. Will returns entries that matches ALL tags."},
37 * } 37 * }
38 * ) 38 * )
39 * 39 *
@@ -43,8 +43,8 @@ class WallabagRestController extends FOSRestController
43 { 43 {
44 $this->validateAuthentication(); 44 $this->validateAuthentication();
45 45
46 $isArchived = $request->query->get('archive'); 46 $isArchived = (int) $request->query->get('archive');
47 $isStarred = $request->query->get('star'); 47 $isStarred = (int) $request->query->get('starred');
48 $sort = $request->query->get('sort', 'created'); 48 $sort = $request->query->get('sort', 'created');
49 $order = $request->query->get('order', 'desc'); 49 $order = $request->query->get('order', 'desc');
50 $page = (int) $request->query->get('page', 1); 50 $page = (int) $request->query->get('page', 1);
@@ -52,7 +52,7 @@ class WallabagRestController extends FOSRestController
52 52
53 $pager = $this->getDoctrine() 53 $pager = $this->getDoctrine()
54 ->getRepository('WallabagCoreBundle:Entry') 54 ->getRepository('WallabagCoreBundle:Entry')
55 ->findEntries($this->getUser()->getId(), $isArchived, $isStarred, $sort, $order); 55 ->findEntries($this->getUser()->getId(), (bool) $isArchived, (bool) $isStarred, $sort, $order);
56 56
57 $pager->setCurrentPage($page); 57 $pager->setCurrentPage($page);
58 $pager->setMaxPerPage($perPage); 58 $pager->setMaxPerPage($perPage);
@@ -97,8 +97,8 @@ class WallabagRestController extends FOSRestController
97 * {"name"="url", "dataType"="string", "required"=true, "format"="http://www.test.com/article.html", "description"="Url for the entry."}, 97 * {"name"="url", "dataType"="string", "required"=true, "format"="http://www.test.com/article.html", "description"="Url for the entry."},
98 * {"name"="title", "dataType"="string", "required"=false, "description"="Optional, we'll get the title from the page."}, 98 * {"name"="title", "dataType"="string", "required"=false, "description"="Optional, we'll get the title from the page."},
99 * {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."}, 99 * {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."},
100 * {"name"="starred", "dataType"="boolean", "required"=false, "format"="true or false", "description"="entry already starred"}, 100 * {"name"="starred", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="entry already starred"},
101 * {"name"="archive", "dataType"="boolean", "required"=false, "format"="true or false", "description"="entry already archived"}, 101 * {"name"="archive", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="entry already archived"},
102 * } 102 * }
103 * ) 103 * )
104 * 104 *
@@ -109,8 +109,8 @@ class WallabagRestController extends FOSRestController
109 $this->validateAuthentication(); 109 $this->validateAuthentication();
110 110
111 $url = $request->request->get('url'); 111 $url = $request->request->get('url');
112 $isArchived = $request->request->get('archive'); 112 $isArchived = (int) $request->request->get('archive');
113 $isStarred = $request->request->get('starred'); 113 $isStarred = (int) $request->request->get('starred');
114 114
115 $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId($url, $this->getUser()->getId()); 115 $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId($url, $this->getUser()->getId());
116 116
@@ -126,12 +126,12 @@ class WallabagRestController extends FOSRestController
126 $this->get('wallabag_core.content_proxy')->assignTagsToEntry($entry, $tags); 126 $this->get('wallabag_core.content_proxy')->assignTagsToEntry($entry, $tags);
127 } 127 }
128 128
129 if (true === (bool) $isStarred) { 129 if (!is_null($isStarred)) {
130 $entry->setStarred(true); 130 $entry->setStarred((bool) $isStarred);
131 } 131 }
132 132
133 if (true === (bool) $isArchived) { 133 if (!is_null($isArchived)) {
134 $entry->setArchived(true); 134 $entry->setArchived((bool) $isArchived);
135 } 135 }
136 136
137 $em = $this->getDoctrine()->getManager(); 137 $em = $this->getDoctrine()->getManager();
@@ -154,8 +154,8 @@ class WallabagRestController extends FOSRestController
154 * parameters={ 154 * parameters={
155 * {"name"="title", "dataType"="string", "required"=false}, 155 * {"name"="title", "dataType"="string", "required"=false},
156 * {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."}, 156 * {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."},
157 * {"name"="archive", "dataType"="boolean", "required"=false, "format"="true or false", "description"="archived the entry."}, 157 * {"name"="archive", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="archived the entry."},
158 * {"name"="star", "dataType"="boolean", "required"=false, "format"="true or false", "description"="starred the entry."}, 158 * {"name"="starred", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="starred the entry."},
159 * } 159 * }
160 * ) 160 * )
161 * 161 *
@@ -167,19 +167,19 @@ class WallabagRestController extends FOSRestController
167 $this->validateUserAccess($entry->getUser()->getId()); 167 $this->validateUserAccess($entry->getUser()->getId());
168 168
169 $title = $request->request->get('title'); 169 $title = $request->request->get('title');
170 $isArchived = $request->request->get('archive'); 170 $isArchived = (int) $request->request->get('archive');
171 $isStarred = $request->request->get('star'); 171 $isStarred = (int) $request->request->get('starred');
172 172
173 if (!is_null($title)) { 173 if (!is_null($title)) {
174 $entry->setTitle($title); 174 $entry->setTitle($title);
175 } 175 }
176 176
177 if (!is_null($isArchived)) { 177 if (!is_null($isArchived)) {
178 $entry->setArchived($isArchived); 178 $entry->setArchived((bool) $isArchived);
179 } 179 }
180 180
181 if (!is_null($isStarred)) { 181 if (!is_null($isStarred)) {
182 $entry->setStarred($isStarred); 182 $entry->setStarred((bool) $isStarred);
183 } 183 }
184 184
185 $tags = $request->request->get('tags', ''); 185 $tags = $request->request->get('tags', '');
diff --git a/src/Wallabag/ApiBundle/Tests/Controller/WallabagRestControllerTest.php b/src/Wallabag/ApiBundle/Tests/Controller/WallabagRestControllerTest.php
index 2e78d8b2..ccb72d23 100644
--- a/src/Wallabag/ApiBundle/Tests/Controller/WallabagRestControllerTest.php
+++ b/src/Wallabag/ApiBundle/Tests/Controller/WallabagRestControllerTest.php
@@ -188,8 +188,8 @@ class WallabagRestControllerTest extends WallabagApiTestCase
188 { 188 {
189 $this->client->request('POST', '/api/entries.json', array( 189 $this->client->request('POST', '/api/entries.json', array(
190 'url' => 'http://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html', 190 'url' => 'http://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html',
191 'archive' => true, 191 'archive' => '1',
192 'starred' => true, 192 'starred' => '1',
193 )); 193 ));
194 194
195 $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); 195 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
@@ -203,6 +203,24 @@ class WallabagRestControllerTest extends WallabagApiTestCase
203 $this->assertEquals(1, $content['user_id']); 203 $this->assertEquals(1, $content['user_id']);
204 } 204 }
205 205
206 public function testPostArchivedAndStarredEntryWithoutQuotes()
207 {
208 $this->client->request('POST', '/api/entries.json', array(
209 'url' => 'http://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html',
210 'archive' => 0,
211 'starred' => 1,
212 ));
213
214 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
215
216 $content = json_decode($this->client->getResponse()->getContent(), true);
217
218 $this->assertGreaterThan(0, $content['id']);
219 $this->assertEquals('http://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html', $content['url']);
220 $this->assertEquals(false, $content['is_archived']);
221 $this->assertEquals(true, $content['is_starred']);
222 }
223
206 public function testPatchEntry() 224 public function testPatchEntry()
207 { 225 {
208 $entry = $this->client->getContainer() 226 $entry = $this->client->getContainer()
@@ -220,8 +238,8 @@ class WallabagRestControllerTest extends WallabagApiTestCase
220 $this->client->request('PATCH', '/api/entries/'.$entry->getId().'.json', array( 238 $this->client->request('PATCH', '/api/entries/'.$entry->getId().'.json', array(
221 'title' => 'New awesome title', 239 'title' => 'New awesome title',
222 'tags' => 'new tag '.uniqid(), 240 'tags' => 'new tag '.uniqid(),
223 'star' => true, 241 'starred' => '1',
224 'archive' => false, 242 'archive' => '0',
225 )); 243 ));
226 244
227 $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); 245 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
@@ -235,6 +253,37 @@ class WallabagRestControllerTest extends WallabagApiTestCase
235 $this->assertEquals(1, $content['user_id']); 253 $this->assertEquals(1, $content['user_id']);
236 } 254 }
237 255
256 public function testPatchEntryWithoutQuotes()
257 {
258 $entry = $this->client->getContainer()
259 ->get('doctrine.orm.entity_manager')
260 ->getRepository('WallabagCoreBundle:Entry')
261 ->findOneByUser(1);
262
263 if (!$entry) {
264 $this->markTestSkipped('No content found in db.');
265 }
266
267 // hydrate the tags relations
268 $nbTags = count($entry->getTags());
269
270 $this->client->request('PATCH', '/api/entries/'.$entry->getId().'.json', array(
271 'title' => 'New awesome title',
272 'tags' => 'new tag '.uniqid(),
273 'starred' => 1,
274 'archive' => 0,
275 ));
276
277 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
278
279 $content = json_decode($this->client->getResponse()->getContent(), true);
280
281 $this->assertEquals($entry->getId(), $content['id']);
282 $this->assertEquals($entry->getUrl(), $content['url']);
283 $this->assertEquals('New awesome title', $content['title']);
284 $this->assertGreaterThan($nbTags, count($content['tags']));
285 }
286
238 public function testGetTagsEntry() 287 public function testGetTagsEntry()
239 { 288 {
240 $entry = $this->client->getContainer() 289 $entry = $this->client->getContainer()