|
5 | 5 | */
|
6 | 6 | namespace Magento\CatalogSearch\Block;
|
7 | 7 |
|
| 8 | +use Magento\Framework\App\RequestInterface; |
| 9 | +use Magento\Framework\ObjectManagerInterface; |
| 10 | +use Magento\Framework\View\Element\Template; |
| 11 | +use Magento\Framework\View\Element\Text; |
| 12 | +use Magento\Framework\View\LayoutInterface; |
| 13 | +use Magento\Search\Model\QueryFactory; |
| 14 | +use Magento\TestFramework\Helper\Bootstrap; |
| 15 | + |
8 | 16 | class ResultTest extends \PHPUnit\Framework\TestCase
|
9 | 17 | {
|
| 18 | + /** |
| 19 | + * @var ObjectManagerInterface |
| 20 | + */ |
| 21 | + private $objectManager; |
| 22 | + |
| 23 | + /** |
| 24 | + * @var LayoutInterface |
| 25 | + */ |
| 26 | + private $layout; |
| 27 | + |
| 28 | + /** |
| 29 | + * @inheritdoc |
| 30 | + */ |
| 31 | + protected function setUp() |
| 32 | + { |
| 33 | + $this->objectManager = Bootstrap::getObjectManager(); |
| 34 | + $this->layout = $this->objectManager->get(LayoutInterface::class); |
| 35 | + } |
| 36 | + |
10 | 37 | public function testSetListOrders()
|
11 | 38 | {
|
12 |
| - /** @var $layout \Magento\Framework\View\Layout */ |
13 |
| - $layout = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create( |
14 |
| - \Magento\Framework\View\LayoutInterface::class |
15 |
| - ); |
16 |
| - $layout->addBlock(\Magento\Framework\View\Element\Text::class, 'head'); |
| 39 | + $this->layout->addBlock(Text::class, 'head'); |
17 | 40 | // The tested block is using head block
|
18 |
| - /** @var $block \Magento\CatalogSearch\Block\Result */ |
19 |
| - $block = $layout->addBlock(\Magento\CatalogSearch\Block\Result::class, 'block'); |
20 |
| - $childBlock = $layout->addBlock(\Magento\Framework\View\Element\Text::class, 'search_result_list', 'block'); |
| 41 | + /** @var $block Result */ |
| 42 | + $block = $this->layout->addBlock(Result::class, 'block'); |
| 43 | + $childBlock = $this->layout->addBlock(Text::class, 'search_result_list', 'block'); |
21 | 44 |
|
22 | 45 | $this->assertSame($childBlock, $block->getListBlock());
|
23 | 46 | }
|
| 47 | + |
| 48 | + /** |
| 49 | + * Verify search value escaping process |
| 50 | + * |
| 51 | + * @magentoConfigFixture default/catalog/search/engine elasticsearch6 |
| 52 | + * @dataProvider toEscapeSearchTextDataProvider |
| 53 | + * @magentoAppArea frontend |
| 54 | + * @param string $searchValue |
| 55 | + * @param string $expectedOutput |
| 56 | + * @param string $unexpectedOutput |
| 57 | + * @return void |
| 58 | + */ |
| 59 | + public function testEscapeSearchText(string $searchValue, string $expectedOutput, string $unexpectedOutput): void |
| 60 | + { |
| 61 | + /** @var Result $searchResultBlock */ |
| 62 | + $searchResultBlock = $this->layout->createBlock(Result::class); |
| 63 | + /** @var Template $searchBlock */ |
| 64 | + $searchBlock = $this->layout->createBlock(Template::class); |
| 65 | + $searchBlock->setTemplate('Magento_Search::form.mini.phtml'); |
| 66 | + /** @var RequestInterface $request */ |
| 67 | + $request = $this->objectManager->get(RequestInterface::class); |
| 68 | + |
| 69 | + $request->setParam(QueryFactory::QUERY_VAR_NAME, $searchValue); |
| 70 | + $searchHtml = $searchBlock->toHtml(); |
| 71 | + |
| 72 | + $this->assertContains('value=' . '"' . $expectedOutput . '"', $searchHtml); |
| 73 | + $this->assertNotContains($unexpectedOutput, $searchHtml); |
| 74 | + |
| 75 | + $resultTitle = $searchResultBlock->getSearchQueryText()->render(); |
| 76 | + $this->assertContains("Search results for: '{$expectedOutput}'", $resultTitle); |
| 77 | + $this->assertNotContains($unexpectedOutput, $resultTitle); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * DataProvider for testEscapeSearchText() |
| 82 | + * |
| 83 | + * @return array |
| 84 | + */ |
| 85 | + public function toEscapeSearchTextDataProvider(): array |
| 86 | + { |
| 87 | + return [ |
| 88 | + 'less_than_sign_escaped' => ['<', '<', '&lt;'], |
| 89 | + 'greater_than_sign_escaped' => ['>', '>', '&gt;'], |
| 90 | + 'ampersand_sign_escaped' => ['&', '&', '&amp;'], |
| 91 | + 'double_quote_sign_escaped' => ['"', '"', '&quot;'], |
| 92 | + 'single_quote_sign_escaped' => ["'", ''', '&#039;'], |
| 93 | + 'plus_sign_not_escaped' => ['+', '+', '&+;'], |
| 94 | + 'characters_not_escaped' => ['abc', 'abc', '&abc;'], |
| 95 | + 'numbers_not_escaped' => ['123', '123', '&123;'], |
| 96 | + ]; |
| 97 | + } |
24 | 98 | }
|
0 commit comments