|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2025 Adobe |
| 4 | + * All Rights Reserved. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\Sales\Plugin\SearchCriteria; |
| 9 | + |
| 10 | +use Magento\Framework\Api\Filter; |
| 11 | +use Magento\Framework\ObjectManagerInterface; |
| 12 | +use Magento\Framework\View\Element\UiComponent\DataProvider\FulltextFilter as UiFulltextFilter; |
| 13 | +use Magento\Sales\Model\ResourceModel\Order\Grid\Collection as OrderGridCollection; |
| 14 | +use Magento\TestFramework\Helper\Bootstrap; |
| 15 | +use PHPUnit\Framework\TestCase; |
| 16 | + |
| 17 | +class FulltextFilterPluginTest extends TestCase |
| 18 | +{ |
| 19 | + /** @var ObjectManagerInterface */ |
| 20 | + private $objectManager; |
| 21 | + |
| 22 | + protected function setUp(): void |
| 23 | + { |
| 24 | + $this->objectManager = Bootstrap::getObjectManager(); |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * @magentoAppArea adminhtml |
| 29 | + */ |
| 30 | + public function testApplyOnOrderGridUsesLikeConditionsAndNoMatchAgainst(): void |
| 31 | + { |
| 32 | + /** @var UiFulltextFilter $fulltextFilter */ |
| 33 | + $fulltextFilter = $this->objectManager->get(UiFulltextFilter::class); |
| 34 | + |
| 35 | + /** @var OrderGridCollection $collection */ |
| 36 | + $collection = $this->objectManager->create(OrderGridCollection::class); |
| 37 | + |
| 38 | + /** @var Filter $filter */ |
| 39 | + $filter = $this->objectManager->create(Filter::class); |
| 40 | + $filter->setValue('Will'); |
| 41 | + |
| 42 | + $fulltextFilter->apply($collection, $filter); |
| 43 | + |
| 44 | + $sql = $collection->getSelectSql(true); |
| 45 | + |
| 46 | + $this->assertStringContainsString("`main_table`.`increment_id` LIKE '%Will%'", $sql); |
| 47 | + $this->assertStringContainsString("`main_table`.`billing_name` LIKE '%Will%'", $sql); |
| 48 | + $this->assertStringContainsString("`main_table`.`shipping_name` LIKE '%Will%'", $sql); |
| 49 | + $this->assertStringContainsString("`main_table`.`customer_email` LIKE '%Will%'", $sql); |
| 50 | + |
| 51 | + $this->assertStringNotContainsString('MATCH(', $sql); |
| 52 | + $this->assertStringNotContainsString('AGAINST(', $sql); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * @magentoAppArea adminhtml |
| 57 | + */ |
| 58 | + public function testApplyOnOrderGridWithEmptyValueDoesNothing(): void |
| 59 | + { |
| 60 | + /** @var UiFulltextFilter $fulltextFilter */ |
| 61 | + $fulltextFilter = $this->objectManager->get(UiFulltextFilter::class); |
| 62 | + |
| 63 | + /** @var OrderGridCollection $collection */ |
| 64 | + $collection = $this->objectManager->create(OrderGridCollection::class); |
| 65 | + $initialSql = $collection->getSelectSql(true); |
| 66 | + |
| 67 | + /** @var Filter $filter */ |
| 68 | + $filter = $this->objectManager->create(Filter::class); |
| 69 | + $filter->setValue(' '); // empty after trim |
| 70 | + |
| 71 | + $fulltextFilter->apply($collection, $filter); |
| 72 | + |
| 73 | + $sql = $collection->getSelectSql(true); |
| 74 | + |
| 75 | + // No LIKE or MATCH added |
| 76 | + $this->assertSame($initialSql, $sql); |
| 77 | + $this->assertStringNotContainsString('LIKE', $sql); |
| 78 | + $this->assertStringNotContainsString('MATCH(', $sql); |
| 79 | + $this->assertStringNotContainsString('AGAINST(', $sql); |
| 80 | + } |
| 81 | +} |
0 commit comments