diff options
author | Thomas Citharel <tcit@tcit.fr> | 2016-10-02 17:48:16 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-10-02 17:48:16 +0200 |
commit | dfbbf0e18ad585e318c2609e46963e4b9fd198ef (patch) | |
tree | a415df1d2e51a35e1501a15d27dbcc2fad971e10 | |
parent | 77557d289bafc088baf806e4744f110dfd959300 (diff) | |
parent | c3f8b428dd50578a6eba5f4673ea1c9edabd2512 (diff) | |
download | wallabag-dfbbf0e18ad585e318c2609e46963e4b9fd198ef.tar.gz wallabag-dfbbf0e18ad585e318c2609e46963e4b9fd198ef.tar.zst wallabag-dfbbf0e18ad585e318c2609e46963e4b9fd198ef.zip |
Merge pull request #2331 from wallabag/api-links
Fix parameters in API _links
3 files changed, 115 insertions, 4 deletions
diff --git a/src/Wallabag/ApiBundle/Controller/WallabagRestController.php b/src/Wallabag/ApiBundle/Controller/WallabagRestController.php index 104720a9..791bf80b 100644 --- a/src/Wallabag/ApiBundle/Controller/WallabagRestController.php +++ b/src/Wallabag/ApiBundle/Controller/WallabagRestController.php | |||
@@ -82,8 +82,8 @@ class WallabagRestController extends FOSRestController | |||
82 | $order = $request->query->get('order', 'desc'); | 82 | $order = $request->query->get('order', 'desc'); |
83 | $page = (int) $request->query->get('page', 1); | 83 | $page = (int) $request->query->get('page', 1); |
84 | $perPage = (int) $request->query->get('perPage', 30); | 84 | $perPage = (int) $request->query->get('perPage', 30); |
85 | $since = $request->query->get('since', 0); | ||
86 | $tags = $request->query->get('tags', ''); | 85 | $tags = $request->query->get('tags', ''); |
86 | $since = $request->query->get('since', 0); | ||
87 | 87 | ||
88 | $pager = $this->getDoctrine() | 88 | $pager = $this->getDoctrine() |
89 | ->getRepository('WallabagCoreBundle:Entry') | 89 | ->getRepository('WallabagCoreBundle:Entry') |
@@ -95,7 +95,20 @@ class WallabagRestController extends FOSRestController | |||
95 | $pagerfantaFactory = new PagerfantaFactory('page', 'perPage'); | 95 | $pagerfantaFactory = new PagerfantaFactory('page', 'perPage'); |
96 | $paginatedCollection = $pagerfantaFactory->createRepresentation( | 96 | $paginatedCollection = $pagerfantaFactory->createRepresentation( |
97 | $pager, | 97 | $pager, |
98 | new Route('api_get_entries', [], UrlGeneratorInterface::ABSOLUTE_URL) | 98 | new Route( |
99 | 'api_get_entries', | ||
100 | [ | ||
101 | 'archive' => $isArchived, | ||
102 | 'starred' => $isStarred, | ||
103 | 'sort' => $sort, | ||
104 | 'order' => $order, | ||
105 | 'page' => $page, | ||
106 | 'perPage' => $perPage, | ||
107 | 'tags' => $tags, | ||
108 | 'since' => $since, | ||
109 | ], | ||
110 | UrlGeneratorInterface::ABSOLUTE_URL | ||
111 | ) | ||
99 | ); | 112 | ); |
100 | 113 | ||
101 | $json = $this->get('serializer')->serialize($paginatedCollection, 'json'); | 114 | $json = $this->get('serializer')->serialize($paginatedCollection, 'json'); |
diff --git a/src/Wallabag/CoreBundle/Repository/EntryRepository.php b/src/Wallabag/CoreBundle/Repository/EntryRepository.php index 1b023e96..75127b7d 100644 --- a/src/Wallabag/CoreBundle/Repository/EntryRepository.php +++ b/src/Wallabag/CoreBundle/Repository/EntryRepository.php | |||
@@ -128,7 +128,7 @@ class EntryRepository extends EntityRepository | |||
128 | $qb->andWhere('e.isStarred =:isStarred')->setParameter('isStarred', (bool) $isStarred); | 128 | $qb->andWhere('e.isStarred =:isStarred')->setParameter('isStarred', (bool) $isStarred); |
129 | } | 129 | } |
130 | 130 | ||
131 | if ($since >= 0) { | 131 | if ($since > 0) { |
132 | $qb->andWhere('e.updatedAt > :since')->setParameter('since', new \DateTime(date('Y-m-d H:i:s', $since))); | 132 | $qb->andWhere('e.updatedAt > :since')->setParameter('since', new \DateTime(date('Y-m-d H:i:s', $since))); |
133 | } | 133 | } |
134 | 134 | ||
diff --git a/tests/Wallabag/ApiBundle/Controller/WallabagRestControllerTest.php b/tests/Wallabag/ApiBundle/Controller/WallabagRestControllerTest.php index 9b5760bc..fd72b8f2 100644 --- a/tests/Wallabag/ApiBundle/Controller/WallabagRestControllerTest.php +++ b/tests/Wallabag/ApiBundle/Controller/WallabagRestControllerTest.php | |||
@@ -78,6 +78,53 @@ class WallabagRestControllerTest extends WallabagApiTestCase | |||
78 | ); | 78 | ); |
79 | } | 79 | } |
80 | 80 | ||
81 | public function testGetEntriesWithFullOptions() | ||
82 | { | ||
83 | $this->client->request('GET', '/api/entries', [ | ||
84 | 'archive' => 1, | ||
85 | 'starred' => 1, | ||
86 | 'sort' => 'updated', | ||
87 | 'order' => 'asc', | ||
88 | 'page' => 1, | ||
89 | 'perPage' => 2, | ||
90 | 'tags' => 'foo', | ||
91 | 'since' => 1443274283, | ||
92 | ]); | ||
93 | |||
94 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | ||
95 | |||
96 | $content = json_decode($this->client->getResponse()->getContent(), true); | ||
97 | |||
98 | $this->assertGreaterThanOrEqual(1, count($content)); | ||
99 | $this->assertArrayHasKey('items', $content['_embedded']); | ||
100 | $this->assertGreaterThanOrEqual(0, $content['total']); | ||
101 | $this->assertEquals(1, $content['page']); | ||
102 | $this->assertEquals(2, $content['limit']); | ||
103 | $this->assertGreaterThanOrEqual(1, $content['pages']); | ||
104 | |||
105 | $this->assertArrayHasKey('_links', $content); | ||
106 | $this->assertArrayHasKey('self', $content['_links']); | ||
107 | $this->assertArrayHasKey('first', $content['_links']); | ||
108 | $this->assertArrayHasKey('last', $content['_links']); | ||
109 | |||
110 | foreach (['self', 'first', 'last'] as $link) { | ||
111 | $this->assertArrayHasKey('href', $content['_links'][$link]); | ||
112 | $this->assertContains('archive=1', $content['_links'][$link]['href']); | ||
113 | $this->assertContains('starred=1', $content['_links'][$link]['href']); | ||
114 | $this->assertContains('sort=updated', $content['_links'][$link]['href']); | ||
115 | $this->assertContains('order=asc', $content['_links'][$link]['href']); | ||
116 | $this->assertContains('tags=foo', $content['_links'][$link]['href']); | ||
117 | $this->assertContains('since=1443274283', $content['_links'][$link]['href']); | ||
118 | } | ||
119 | |||
120 | $this->assertTrue( | ||
121 | $this->client->getResponse()->headers->contains( | ||
122 | 'Content-Type', | ||
123 | 'application/json' | ||
124 | ) | ||
125 | ); | ||
126 | } | ||
127 | |||
81 | public function testGetStarredEntries() | 128 | public function testGetStarredEntries() |
82 | { | 129 | { |
83 | $this->client->request('GET', '/api/entries', ['starred' => 1, 'sort' => 'updated']); | 130 | $this->client->request('GET', '/api/entries', ['starred' => 1, 'sort' => 'updated']); |
@@ -92,6 +139,17 @@ class WallabagRestControllerTest extends WallabagApiTestCase | |||
92 | $this->assertEquals(1, $content['page']); | 139 | $this->assertEquals(1, $content['page']); |
93 | $this->assertGreaterThanOrEqual(1, $content['pages']); | 140 | $this->assertGreaterThanOrEqual(1, $content['pages']); |
94 | 141 | ||
142 | $this->assertArrayHasKey('_links', $content); | ||
143 | $this->assertArrayHasKey('self', $content['_links']); | ||
144 | $this->assertArrayHasKey('first', $content['_links']); | ||
145 | $this->assertArrayHasKey('last', $content['_links']); | ||
146 | |||
147 | foreach (['self', 'first', 'last'] as $link) { | ||
148 | $this->assertArrayHasKey('href', $content['_links'][$link]); | ||
149 | $this->assertContains('starred=1', $content['_links'][$link]['href']); | ||
150 | $this->assertContains('sort=updated', $content['_links'][$link]['href']); | ||
151 | } | ||
152 | |||
95 | $this->assertTrue( | 153 | $this->assertTrue( |
96 | $this->client->getResponse()->headers->contains( | 154 | $this->client->getResponse()->headers->contains( |
97 | 'Content-Type', | 155 | 'Content-Type', |
@@ -114,6 +172,16 @@ class WallabagRestControllerTest extends WallabagApiTestCase | |||
114 | $this->assertEquals(1, $content['page']); | 172 | $this->assertEquals(1, $content['page']); |
115 | $this->assertGreaterThanOrEqual(1, $content['pages']); | 173 | $this->assertGreaterThanOrEqual(1, $content['pages']); |
116 | 174 | ||
175 | $this->assertArrayHasKey('_links', $content); | ||
176 | $this->assertArrayHasKey('self', $content['_links']); | ||
177 | $this->assertArrayHasKey('first', $content['_links']); | ||
178 | $this->assertArrayHasKey('last', $content['_links']); | ||
179 | |||
180 | foreach (['self', 'first', 'last'] as $link) { | ||
181 | $this->assertArrayHasKey('href', $content['_links'][$link]); | ||
182 | $this->assertContains('archive=1', $content['_links'][$link]['href']); | ||
183 | } | ||
184 | |||
117 | $this->assertTrue( | 185 | $this->assertTrue( |
118 | $this->client->getResponse()->headers->contains( | 186 | $this->client->getResponse()->headers->contains( |
119 | 'Content-Type', | 187 | 'Content-Type', |
@@ -136,6 +204,16 @@ class WallabagRestControllerTest extends WallabagApiTestCase | |||
136 | $this->assertEquals(1, $content['page']); | 204 | $this->assertEquals(1, $content['page']); |
137 | $this->assertGreaterThanOrEqual(1, $content['pages']); | 205 | $this->assertGreaterThanOrEqual(1, $content['pages']); |
138 | 206 | ||
207 | $this->assertArrayHasKey('_links', $content); | ||
208 | $this->assertArrayHasKey('self', $content['_links']); | ||
209 | $this->assertArrayHasKey('first', $content['_links']); | ||
210 | $this->assertArrayHasKey('last', $content['_links']); | ||
211 | |||
212 | foreach (['self', 'first', 'last'] as $link) { | ||
213 | $this->assertArrayHasKey('href', $content['_links'][$link]); | ||
214 | $this->assertContains('tags='.urlencode('foo,bar'), $content['_links'][$link]['href']); | ||
215 | } | ||
216 | |||
139 | $this->assertTrue( | 217 | $this->assertTrue( |
140 | $this->client->getResponse()->headers->contains( | 218 | $this->client->getResponse()->headers->contains( |
141 | 'Content-Type', | 219 | 'Content-Type', |
@@ -146,7 +224,7 @@ class WallabagRestControllerTest extends WallabagApiTestCase | |||
146 | 224 | ||
147 | public function testGetDatedEntries() | 225 | public function testGetDatedEntries() |
148 | { | 226 | { |
149 | $this->client->request('GET', '/api/entries', ['since' => 1]); | 227 | $this->client->request('GET', '/api/entries', ['since' => 1443274283]); |
150 | 228 | ||
151 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | 229 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); |
152 | 230 | ||
@@ -158,6 +236,16 @@ class WallabagRestControllerTest extends WallabagApiTestCase | |||
158 | $this->assertEquals(1, $content['page']); | 236 | $this->assertEquals(1, $content['page']); |
159 | $this->assertGreaterThanOrEqual(1, $content['pages']); | 237 | $this->assertGreaterThanOrEqual(1, $content['pages']); |
160 | 238 | ||
239 | $this->assertArrayHasKey('_links', $content); | ||
240 | $this->assertArrayHasKey('self', $content['_links']); | ||
241 | $this->assertArrayHasKey('first', $content['_links']); | ||
242 | $this->assertArrayHasKey('last', $content['_links']); | ||
243 | |||
244 | foreach (['self', 'first', 'last'] as $link) { | ||
245 | $this->assertArrayHasKey('href', $content['_links'][$link]); | ||
246 | $this->assertContains('since=1443274283', $content['_links'][$link]['href']); | ||
247 | } | ||
248 | |||
161 | $this->assertTrue( | 249 | $this->assertTrue( |
162 | $this->client->getResponse()->headers->contains( | 250 | $this->client->getResponse()->headers->contains( |
163 | 'Content-Type', | 251 | 'Content-Type', |
@@ -181,6 +269,16 @@ class WallabagRestControllerTest extends WallabagApiTestCase | |||
181 | $this->assertEquals(1, $content['page']); | 269 | $this->assertEquals(1, $content['page']); |
182 | $this->assertEquals(1, $content['pages']); | 270 | $this->assertEquals(1, $content['pages']); |
183 | 271 | ||
272 | $this->assertArrayHasKey('_links', $content); | ||
273 | $this->assertArrayHasKey('self', $content['_links']); | ||
274 | $this->assertArrayHasKey('first', $content['_links']); | ||
275 | $this->assertArrayHasKey('last', $content['_links']); | ||
276 | |||
277 | foreach (['self', 'first', 'last'] as $link) { | ||
278 | $this->assertArrayHasKey('href', $content['_links'][$link]); | ||
279 | $this->assertContains('since='.($future->getTimestamp() + 1000), $content['_links'][$link]['href']); | ||
280 | } | ||
281 | |||
184 | $this->assertTrue( | 282 | $this->assertTrue( |
185 | $this->client->getResponse()->headers->contains( | 283 | $this->client->getResponse()->headers->contains( |
186 | 'Content-Type', | 284 | 'Content-Type', |