From b3eed68f5fe3cdce0537e9c28931e88376e7999c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Witold=20Wi=C5=9Bniewski?= Date: Mon, 25 Sep 2023 14:17:30 +0200 Subject: [PATCH 1/2] Fix product filter and sort --- app/Criteria/ProductSearch.php | 8 +++--- tests/Feature/ProductSearchDatabaseTest.php | 28 +++++++++++++++++++++ 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/app/Criteria/ProductSearch.php b/app/Criteria/ProductSearch.php index b548657c5..0846f7de5 100644 --- a/app/Criteria/ProductSearch.php +++ b/app/Criteria/ProductSearch.php @@ -10,10 +10,10 @@ class ProductSearch extends Criterion public function query(Builder $query): Builder { return $query->whereFullText([ - 'name', - 'description_html', - 'description_short', - 'search_values', + 'products.name', + 'products.description_html', + 'products.description_short', + 'products.search_values', ], $this->value); } } diff --git a/tests/Feature/ProductSearchDatabaseTest.php b/tests/Feature/ProductSearchDatabaseTest.php index 3ddb0382a..22eacf7a6 100644 --- a/tests/Feature/ProductSearchDatabaseTest.php +++ b/tests/Feature/ProductSearchDatabaseTest.php @@ -80,6 +80,34 @@ public function testIndexIdsSearch($user): void ->assertJsonCount(2, 'data'); } + /** + * @dataProvider authProvider + */ + public function testIndexSearch($user): void + { + $this->{$user}->givePermissionTo('products.show'); + + Product::factory()->create([ + 'public' => true, + 'name' => 'First', + ]); + + Product::factory()->create([ + 'public' => true, + 'created_at' => Carbon::now()->addHour(), + 'name' => 'Second', + ]); + + // This test check if there is no SQL error that 'name' is ambiguous + $this + ->actingAs($this->{$user}) + ->json('GET', '/products', [ + 'search' => 'First', + 'sort' => 'attribute.data-wydania:desc', + ]) + ->assertOk(); + } + /** * @dataProvider authProvider */ From 09c0ffdfd4757f5657d6cd6f5033305433200f21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Witold=20Wi=C5=9Bniewski?= Date: Mon, 25 Sep 2023 14:42:18 +0200 Subject: [PATCH 2/2] Fix tests --- .../Products/ProductSearchValuesTest.php | 256 ------------------ 1 file changed, 256 deletions(-) delete mode 100644 tests/Feature/Products/ProductSearchValuesTest.php diff --git a/tests/Feature/Products/ProductSearchValuesTest.php b/tests/Feature/Products/ProductSearchValuesTest.php deleted file mode 100644 index 0182eb10e..000000000 --- a/tests/Feature/Products/ProductSearchValuesTest.php +++ /dev/null @@ -1,256 +0,0 @@ -product = Product::factory()->create([ - 'name' => 'Searched product', - 'public' => true, - 'description_html' => 'Lorem ipsum', - 'description_short' => 'short', - ]); - } - - /** - * @dataProvider authProvider - */ - public function testUpdateTagSearchValues(string $user): void - { - $this->{$user}->givePermissionTo('tags.edit'); - - $tag = Tag::factory()->create(); - $this->product->tags()->sync($tag->getKey()); - - $this - ->actingAs($this->{$user}) - ->json('PATCH', 'tags/id:' . $tag->getKey(), [ - 'name' => 'Tag updated', - ]) - ->assertOk(); - - $this->assertDatabaseHas('products', [ - 'id' => $this->product->getKey(), - 'name' => 'Searched product', - 'description_html' => 'Lorem ipsum', - 'search_values' => 'Tag updated', - ]); - } - - /** - * @dataProvider authProvider - */ - public function testDeleteTagSearchValues(string $user): void - { - $this->{$user}->givePermissionTo('tags.remove'); - - $tag = Tag::factory()->create(); - $this->product->tags()->sync($tag->getKey()); - - $this - ->actingAs($this->{$user}) - ->json('DELETE', 'tags/id:' . $tag->getKey()) - ->assertNoContent(); - - $this->assertDatabaseHas('products', [ - 'id' => $this->product->getKey(), - 'name' => 'Searched product', - 'description_html' => 'Lorem ipsum', - 'search_values' => '', - ]); - } - - /** - * @dataProvider authProvider - */ - public function testUpdateProductSetSearchValues(string $user): void - { - $this->{$user}->givePermissionTo('product_sets.edit'); - - $set = ProductSet::factory()->create([ - 'public' => true, - ]); - $this->product->sets()->sync($set->getKey()); - - $this - ->actingAs($this->{$user}) - ->json('PATCH', 'product-sets/id:' . $set->getKey(), [ - 'name' => 'set name', - 'parent_id' => null, - 'children_ids' => [], - ]) - ->assertOk(); - - $this->assertDatabaseHas('products', [ - 'id' => $this->product->getKey(), - 'name' => 'Searched product', - 'description_html' => 'Lorem ipsum', - 'search_values' => 'set name', - ]); - } - - /** - * @dataProvider authProvider - */ - public function testDeleteProductSetSearchValues(string $user): void - { - $this->{$user}->givePermissionTo('product_sets.remove'); - - $set = ProductSet::factory()->create([ - 'public' => true, - ]); - $this->product->sets()->sync($set->getKey()); - - $this - ->actingAs($this->{$user}) - ->json('DELETE', 'product-sets/id:' . $set->getKey()) - ->assertNoContent(); - - $this->assertDatabaseHas('products', [ - 'id' => $this->product->getKey(), - 'name' => 'Searched product', - 'description_html' => 'Lorem ipsum', - 'search_values' => '', - ]); - } - - /** - * @dataProvider authProvider - */ - public function testUpdateAttributeSearchValues(string $user): void - { - $this->{$user}->givePermissionTo('attributes.edit'); - - /** @var Attribute $attribute */ - $attribute = Attribute::factory()->create(); - $this->product->attributes()->attach($attribute->getKey()); - - $this - ->actingAs($this->{$user}) - ->json('PATCH', 'attributes/id:' . $attribute->getKey(), [ - 'name' => 'updated attribute', - 'slug' => $attribute->slug, - 'type' => $attribute->type, - 'global' => true, - 'sortable' => true, - ]) - ->assertOk(); - - $this->assertDatabaseHas('products', [ - 'id' => $this->product->getKey(), - 'name' => 'Searched product', - 'description_html' => 'Lorem ipsum', - 'search_values' => 'updated attribute', - ]); - } - - /** - * @dataProvider authProvider - */ - public function testDeleteAttributeSearchValues(string $user): void - { - $this->{$user}->givePermissionTo('attributes.remove'); - - /** @var Attribute $attribute */ - $attribute = Attribute::factory()->create(); - $this->product->attributes()->attach($attribute->getKey()); - - $this - ->actingAs($this->{$user}) - ->json('DELETE', 'attributes/id:' . $attribute->getKey()) - ->assertNoContent(); - - $this->assertDatabaseHas('products', [ - 'id' => $this->product->getKey(), - 'name' => 'Searched product', - 'description_html' => 'Lorem ipsum', - 'search_values' => '', - ]); - } - - /** - * @dataProvider authProvider - */ - public function testUpdateAttributeOptionSearchValues(string $user): void - { - $this->{$user}->givePermissionTo('attributes.edit'); - - /** @var Attribute $attribute */ - $attribute = Attribute::factory()->create([ - 'name' => 'Attribute', - 'type' => AttributeType::NUMBER->value, - ]); - - $option = AttributeOption::factory()->create([ - 'index' => 1, - 'attribute_id' => $attribute->getKey(), - ]); - - $this->product->attributes()->attach($attribute->getKey()); - $this->product->attributes->first()->pivot->options()->attach($option->getKey()); - - $this - ->actingAs($this->{$user}) - ->json('PATCH', 'attributes/id:' . $attribute->getKey() . '/options/id:' . $option->getKey(), [ - 'name' => 'option 1', - 'value_number' => 10, - 'value_date' => '2023-09-08', - ]) - ->assertOk(); - - $this->assertDatabaseHas('products', [ - 'id' => $this->product->getKey(), - 'name' => 'Searched product', - 'description_html' => 'Lorem ipsum', - 'search_values' => 'Attribute option 1 10 2023-09-08', - ]); - } - - /** - * @dataProvider authProvider - */ - public function testDeleteAttributeOptionSearchValues(string $user): void - { - $this->{$user}->givePermissionTo('attributes.edit'); - - /** @var Attribute $attribute */ - $attribute = Attribute::factory()->create([ - 'name' => 'Attribute', - 'type' => AttributeType::NUMBER->value, - ]); - - $option = AttributeOption::factory()->create([ - 'index' => 1, - 'attribute_id' => $attribute->getKey(), - ]); - - $this->product->attributes()->attach($attribute->getKey()); - $this->product->attributes->first()->pivot->options()->attach($option->getKey()); - - $this - ->actingAs($this->{$user}) - ->json('DELETE', 'attributes/id:' . $attribute->getKey() . '/options/id:' . $option->getKey()) - ->assertNoContent(); - - $this->assertDatabaseHas('products', [ - 'id' => $this->product->getKey(), - 'name' => 'Searched product', - 'description_html' => 'Lorem ipsum', - 'search_values' => 'Attribute', - ]); - } -}