From c248bdf48e7d44e852527eca576e773b3bb04a80 Mon Sep 17 00:00:00 2001 From: "h.matsuo" Date: Wed, 24 Nov 2021 13:54:52 +0900 Subject: [PATCH 1/2] =?UTF-8?q?=E5=95=86=E5=93=81=E3=81=AE=E3=82=BF?= =?UTF-8?q?=E3=82=B0=E5=90=8D=E3=81=AB=E3=82=88=E3=82=8B=E6=A4=9C=E7=B4=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Eccube/Repository/ProductRepository.php | 14 +++++- ...ositoryGetQueryBuilderBySearchDataTest.php | 47 +++++++++++++++++++ 2 files changed, 59 insertions(+), 2 deletions(-) diff --git a/src/Eccube/Repository/ProductRepository.php b/src/Eccube/Repository/ProductRepository.php index f4bde076a9a..1875ad879d2 100644 --- a/src/Eccube/Repository/ProductRepository.php +++ b/src/Eccube/Repository/ProductRepository.php @@ -158,8 +158,18 @@ public function getQueryBuilderBySearchData($searchData) $qb ->andWhere(sprintf('NORMALIZE(p.name) LIKE NORMALIZE(:%s) OR NORMALIZE(p.search_word) LIKE NORMALIZE(:%s) OR - EXISTS (SELECT wpc%d FROM \Eccube\Entity\ProductClass wpc%d WHERE p = wpc%d.Product AND NORMALIZE(wpc%d.code) LIKE NORMALIZE(:%s))', - $key, $key, $index, $index, $index, $index, $key)) + EXISTS (SELECT wpc%d FROM \Eccube\Entity\ProductClass wpc%d WHERE p = wpc%d.Product AND NORMALIZE(wpc%d.code) LIKE NORMALIZE(:%s)) OR + EXISTS ( + SELECT wpt%d from \Eccube\Entity\ProductTag wpt%d + WHERE p = wpt%d.Product AND + wpt%d.Tag IN ( + SELECT wt%d FROM \Eccube\Entity\Tag wt%d + WHERE NORMALIZE(wt%d.name) LIKE NORMALIZE(:%s) + ) + )', + $key, $key, + $index, $index, $index, $index, $key, + $index, $index, $index, $index, $index, $index, $index, $key)) ->setParameter($key, '%'.$keyword.'%'); } } diff --git a/tests/Eccube/Tests/Repository/ProductRepositoryGetQueryBuilderBySearchDataTest.php b/tests/Eccube/Tests/Repository/ProductRepositoryGetQueryBuilderBySearchDataTest.php index 40ebe17b9f2..82f5838da38 100644 --- a/tests/Eccube/Tests/Repository/ProductRepositoryGetQueryBuilderBySearchDataTest.php +++ b/tests/Eccube/Tests/Repository/ProductRepositoryGetQueryBuilderBySearchDataTest.php @@ -393,4 +393,51 @@ public function test300ProductsList() } $this->verify(); } + + public function testTagSearch() + { + // データの事前準備 + // * 商品A に タグ 1 を設定 + // * 商品B に タグ 2, 2 を設定 + $Products = $this->productRepository->findAll(); + $Products[1]->setName('りんご'); + $this->setProductTags($Products[1], [1]); + $this->setProductTags($Products[2], [1, 2]); + $this->entityManager->flush(); + + // タグ 1 で検索 + $this->searchData = [ + 'name' => '新商品', + ]; + $this->scenario(); + $this->assertCount(2, $this->Results); + + // タグ 2 で検索 + $this->searchData = [ + 'name' => 'おすすめ商品', + ]; + $this->scenario(); + $this->assertCount(1, $this->Results); + + // タグ 1 and タグ 2 で検索 + $this->searchData = [ + 'name' => '新商品 おすすめ商品', + ]; + $this->scenario(); + $this->assertCount(1, $this->Results); + + // タグ 1 and 商品名 で検索 + $this->searchData = [ + 'name' => '新商品 りんご', + ]; + $this->scenario(); + $this->assertCount(1, $this->Results); + + // タグ 3 で検索 + $this->searchData = [ + 'name' => '限定品', + ]; + $this->scenario(); + $this->assertCount(0, $this->Results); + } } From 0bdb417bcb15b5ea82ba586bf630af3f9e37ce0c Mon Sep 17 00:00:00 2001 From: "h.matsuo" Date: Fri, 26 Nov 2021 10:08:13 +0900 Subject: [PATCH 2/2] =?UTF-8?q?=E3=83=A6=E3=83=8B=E3=83=83=E3=83=88?= =?UTF-8?q?=E3=83=86=E3=82=B9=E3=83=88=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Tests/Repository/AbstractProductRepositoryTestCase.php | 6 ++++++ ...roductRepositoryGetQueryBuilderBySearchDataAdminTest.php | 1 + .../ProductRepositoryGetQueryBuilderBySearchDataTest.php | 5 +++-- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/tests/Eccube/Tests/Repository/AbstractProductRepositoryTestCase.php b/tests/Eccube/Tests/Repository/AbstractProductRepositoryTestCase.php index 74db312be47..632033363ee 100644 --- a/tests/Eccube/Tests/Repository/AbstractProductRepositoryTestCase.php +++ b/tests/Eccube/Tests/Repository/AbstractProductRepositoryTestCase.php @@ -85,6 +85,12 @@ protected function createFavorites($Customer) */ protected function setProductTags(Product $Product, array $tagIds) { + $ProductTags = $Product->getProductTag(); + foreach ($ProductTags as $ProductTag) { + $Product->removeProductTag($ProductTag); + $this->entityManager->remove($ProductTag); + } + $Tags = $this->tagRepository->findBy(['id' => $tagIds]); foreach ($Tags as $Tag) { $ProductTag = new ProductTag(); diff --git a/tests/Eccube/Tests/Repository/ProductRepositoryGetQueryBuilderBySearchDataAdminTest.php b/tests/Eccube/Tests/Repository/ProductRepositoryGetQueryBuilderBySearchDataAdminTest.php index f9bede17e0b..c9c563cbc34 100644 --- a/tests/Eccube/Tests/Repository/ProductRepositoryGetQueryBuilderBySearchDataAdminTest.php +++ b/tests/Eccube/Tests/Repository/ProductRepositoryGetQueryBuilderBySearchDataAdminTest.php @@ -376,6 +376,7 @@ public function testTagSearch() $Products[0]->setName('りんご'); $this->setProductTags($Products[0], [1]); $this->setProductTags($Products[1], [1, 2]); + $this->setProductTags($Products[2], []); $this->entityManager->flush(); // タグ 1 で検索 diff --git a/tests/Eccube/Tests/Repository/ProductRepositoryGetQueryBuilderBySearchDataTest.php b/tests/Eccube/Tests/Repository/ProductRepositoryGetQueryBuilderBySearchDataTest.php index 82f5838da38..6a59d1fd331 100644 --- a/tests/Eccube/Tests/Repository/ProductRepositoryGetQueryBuilderBySearchDataTest.php +++ b/tests/Eccube/Tests/Repository/ProductRepositoryGetQueryBuilderBySearchDataTest.php @@ -401,8 +401,9 @@ public function testTagSearch() // * 商品B に タグ 2, 2 を設定 $Products = $this->productRepository->findAll(); $Products[1]->setName('りんご'); - $this->setProductTags($Products[1], [1]); - $this->setProductTags($Products[2], [1, 2]); + $this->setProductTags($Products[0], [1]); + $this->setProductTags($Products[1], [1, 2]); + $this->setProductTags($Products[2], []); $this->entityManager->flush(); // タグ 1 で検索