From 60e99b589795f5e481b97f8eaf7cd30250635f24 Mon Sep 17 00:00:00 2001 From: Dawid Miklas Date: Wed, 20 Sep 2023 11:42:25 +0200 Subject: [PATCH] filters order --- app/Http/Controllers/FilterController.php | 12 ++++-- tests/Feature/FilterTest.php | 46 +++++++++++++++++++++++ 2 files changed, 54 insertions(+), 4 deletions(-) diff --git a/app/Http/Controllers/FilterController.php b/app/Http/Controllers/FilterController.php index 6641c4e6c..229934f09 100644 --- a/app/Http/Controllers/FilterController.php +++ b/app/Http/Controllers/FilterController.php @@ -16,10 +16,14 @@ public function indexBySetsIds(FilterIndexRequest $request): JsonResource } return AttributeResource::collection( - Attribute::whereHas( - 'productSets', - fn ($query) => $query->whereIn('product_set_id', $request->input('sets')), - )->orWhere('global', true)->with('options')->get() + Attribute::query() + ->whereHas( + 'productSets', + fn ($query) => $query->whereIn('product_set_id', $request->input('sets')), + ) + ->orWhere('global', true)->with('options') + ->orderBy('order') + ->get() ); } } diff --git a/tests/Feature/FilterTest.php b/tests/Feature/FilterTest.php index e6f5aac36..4c2dc3a2a 100644 --- a/tests/Feature/FilterTest.php +++ b/tests/Feature/FilterTest.php @@ -101,4 +101,50 @@ public function testFilterWithSetsIds($user): void 'name' => 'Not in query single option', ]); } + + /** + * @dataProvider authProvider + */ + public function testFiltersAndAttributesSameOrder($user): void + { + $this->{$user}->givePermissionTo('attributes.show'); + + $productSet = ProductSet::factory()->create(); + $productSet->attributes()->attach([ + $attr1 = Attribute::factory()->create([ + 'name' => 'Test A', + 'global' => 0, + 'order' => 1, + 'type' => 'number', + ])->getKey(), + $attr2 = Attribute::factory()->create([ + 'global' => 0, + 'name' => 'Test B', + 'order' => 2, + 'type' => 'date', + ])->getKey(), + $attr3 = Attribute::factory()->create([ + 'global' => 0, + 'order' => 0, + 'name' => 'Test C', + 'type' => 'date', + ])->getKey(), + ]); + + $this + ->actingAs($this->{$user}) + ->getJson('/attributes') + ->assertJsonPath('data.0.id', $attr3) + ->assertJsonPath('data.1.id', $attr1) + ->assertJsonPath('data.2.id', $attr2); + + $this + ->actingAs($this->{$user}) + ->json('GET', '/filters', [ + 'sets' => [$productSet->getKey()], + ]) + ->assertJsonPath('data.0.id', $attr3) + ->assertJsonPath('data.1.id', $attr1) + ->assertJsonPath('data.2.id', $attr2); + } }