From 4da21463c1d8e22c566cc0b340a0d3de8f79f261 Mon Sep 17 00:00:00 2001 From: Sami Mazouz Date: Sat, 9 Jul 2022 10:36:48 +0100 Subject: [PATCH] fix: multiple createdAt columns in query causes conflicts (#3506) * test: list posts with mentions filter and `createdAt` sort * fix: multiple `createdAt` columns in query causes conflicts * chore: link to pull request for context Signed-off-by: Sami Mazouz --- .../tests/integration/api/ListPostsTest.php | 24 ++++++++++++++++++- .../Api/Controller/ListPostsController.php | 14 +++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/extensions/mentions/tests/integration/api/ListPostsTest.php b/extensions/mentions/tests/integration/api/ListPostsTest.php index 5ee3e7e43..b3fd0b551 100644 --- a/extensions/mentions/tests/integration/api/ListPostsTest.php +++ b/extensions/mentions/tests/integration/api/ListPostsTest.php @@ -14,7 +14,7 @@ use Flarum\Testing\integration\RetrievesAuthorizedUsers; use Flarum\Testing\integration\TestCase; use Illuminate\Support\Arr; -class ListTest extends TestCase +class ListPostsTest extends TestCase { use RetrievesAuthorizedUsers; @@ -85,4 +85,26 @@ class ListTest extends TestCase $ids = Arr::pluck($data, 'id'); $this->assertEqualsCanonicalizing(['4'], $ids, 'IDs do not match'); } + + /** + * @test + */ + public function mentioned_filter_works_with_sort() + { + $response = $this->send( + $this->request('GET', '/api/posts') + ->withQueryParams([ + 'filter' => ['mentioned' => 1], + 'sort' => '-createdAt' + ]) + ); + + $data = json_decode($response->getBody()->getContents(), true)['data']; + + $this->assertEquals(200, $response->getStatusCode()); + + // Order-independent comparison + $ids = Arr::pluck($data, 'id'); + $this->assertEqualsCanonicalizing(['3', '2'], $ids, 'IDs do not match'); + } } diff --git a/framework/core/src/Api/Controller/ListPostsController.php b/framework/core/src/Api/Controller/ListPostsController.php index 0c9ef05a9..f38cd71ab 100644 --- a/framework/core/src/Api/Controller/ListPostsController.php +++ b/framework/core/src/Api/Controller/ListPostsController.php @@ -113,6 +113,20 @@ class ListPostsController extends AbstractListController return $results; } + /** + * @link https://github.com/flarum/framework/pull/3506 + */ + protected function extractSort(ServerRequestInterface $request) + { + $sort = []; + + foreach ((parent::extractSort($request) ?: []) as $field => $direction) { + $sort["posts.$field"] = $direction; + } + + return $sort; + } + /** * {@inheritdoc} */