diff --git a/src/Eccube/Repository/ProductRepository.php b/src/Eccube/Repository/ProductRepository.php index 66056d35883..8cda3600ba4 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/AbstractProductRepositoryTestCase.php b/tests/Eccube/Tests/Repository/AbstractProductRepositoryTestCase.php index 6116530db1d..d8dcf974db8 100644 --- a/tests/Eccube/Tests/Repository/AbstractProductRepositoryTestCase.php +++ b/tests/Eccube/Tests/Repository/AbstractProductRepositoryTestCase.php @@ -14,8 +14,11 @@ namespace Eccube\Tests\Repository; use Eccube\Entity\CustomerFavoriteProduct; +use Eccube\Entity\Product; +use Eccube\Entity\ProductTag; use Eccube\Tests\EccubeTestCase; use Eccube\Repository\ProductRepository; +use Eccube\Repository\TagRepository; /** * ProductRepository test cases. @@ -29,6 +32,11 @@ abstract class AbstractProductRepositoryTestCase extends EccubeTestCase */ protected $productRepository; + /** + * @var TagRepository + */ + protected $tagRepository; + /** * {@inheritdoc} */ @@ -36,7 +44,8 @@ public function setUp() { parent::setUp(); - $this->productRepository = $this->entityManager->getRepository(\Eccube\Entity\Product::class); + $this->productRepository = $this->container->get(ProductRepository::class); + $this->tagRepository = $this->container->get(TagRepository::class); $tables = [ 'dtb_product_image', @@ -67,4 +76,23 @@ protected function createFavorites($Customer) } $this->entityManager->flush(); } + + /** + * 商品にタグをつける + * @param Product $Product + * @param array $tagIds + */ + protected function setProductTags(Product $Product, array $tagIds) + { + $Tags = $this->tagRepository->findBy(['id' => $tagIds]); + foreach ($Tags as $Tag) { + $ProductTag = new ProductTag(); + $ProductTag + ->setProduct($Product) + ->setTag($Tag); + $Product->addProductTag($ProductTag); + $this->entityManager->persist($ProductTag); + } + $this->entityManager->flush(); + } } diff --git a/tests/Eccube/Tests/Repository/ProductRepositoryGetQueryBuilderBySearchDataTest.php b/tests/Eccube/Tests/Repository/ProductRepositoryGetQueryBuilderBySearchDataTest.php index 1c069c1f0e0..4aea5867f09 100644 --- a/tests/Eccube/Tests/Repository/ProductRepositoryGetQueryBuilderBySearchDataTest.php +++ b/tests/Eccube/Tests/Repository/ProductRepositoryGetQueryBuilderBySearchDataTest.php @@ -392,4 +392,53 @@ public function test300ProductsList() } $this->verify(); } + + public function testSearchByTagId() + { + // データの事前準備 + // * 商品1 に タグ 1 を設定 + // * 商品2 に タグ 1, 2 を設定 + $Products = $this->productRepository->findAll(); + $Products[0]->setName('りんご'); + $this->setProductTags($Products[0], [1]); + $this->setProductTags($Products[1], [1, 2]); + $this->entityManager->flush(); + + // タグ 1 で検索 + $this->searchData = [ + 'tag_id' => 1, + ]; + $this->scenario(); + $this->assertCount(2, $this->Results); + + // タグ 2 で検索 + $this->searchData = [ + 'tag_id' => 2, + ]; + $this->scenario(); + $this->assertCount(1, $this->Results); + + // タグ 3 で検索 + $this->searchData = [ + 'tag_id' => 3, + ]; + $this->scenario(); + $this->assertCount(0, $this->Results); + + // 文字列とのAND検索 + $this->searchData = [ + 'name' => 'りんご', + 'tag_id' => 1, + ]; + $this->scenario(); + $this->assertCount(1, $this->Results); + + // 文字列とのAND検索 + $this->searchData = [ + 'name' => 'りんご', + 'tag_id' => 2, + ]; + $this->scenario(); + $this->assertCount(0, $this->Results); + } }