|
3 | 3 | * Copyright © Magento, Inc. All rights reserved.
|
4 | 4 | * See COPYING.txt for license details.
|
5 | 5 | */
|
| 6 | +declare(strict_types=1); |
| 7 | + |
6 | 8 | namespace Magento\Catalog\Block\Product\ProductList;
|
7 | 9 |
|
8 |
| -class ToolbarTest extends \PHPUnit\Framework\TestCase |
| 10 | +use Magento\Framework\ObjectManagerInterface; |
| 11 | +use Magento\Framework\View\Element\Text; |
| 12 | +use Magento\Framework\View\LayoutInterface; |
| 13 | +use Magento\TestFramework\Helper\Bootstrap; |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | +use Magento\TestFramework\Helper\Xpath; |
| 16 | + |
| 17 | +/** |
| 18 | + * Checks product list toolbar. |
| 19 | + * |
| 20 | + * @see \Magento\Catalog\Block\Product\ProductList\Toolbar |
| 21 | + * @magentoAppArea frontend |
| 22 | + * @magentoDbIsolation enabled |
| 23 | + */ |
| 24 | +class ToolbarTest extends TestCase |
9 | 25 | {
|
10 |
| - public function testGetPagerHtml() |
| 26 | + /** @var string */ |
| 27 | + private const XPATH_TEMPLATE_FOR_NOT_VISIBLE_ICON_CASES = '//div[contains(@class, "modes")]/*[@data-value="%s"]'; |
| 28 | + |
| 29 | + /** @var string */ |
| 30 | + private const ACTIVE_MODE_XPATH_TEMPLATE = |
| 31 | + '//div[contains(@class, "modes")]/strong[@data-value="%s" and contains(@class, "active")]'; |
| 32 | + |
| 33 | + /** @var string */ |
| 34 | + private const NOT_ACTIVE_MODE_XPATH_TEMPLATE = '//div[contains(@class, "modes")]/a[@data-value="%s"]'; |
| 35 | + |
| 36 | + /** @var ObjectManagerInterface */ |
| 37 | + private $objectManager; |
| 38 | + |
| 39 | + /** @var LayoutInterface */ |
| 40 | + private $layout; |
| 41 | + |
| 42 | + /** @var Toolbar */ |
| 43 | + private $toolbarBlock; |
| 44 | + |
| 45 | + /** |
| 46 | + * @inheritdoc |
| 47 | + */ |
| 48 | + protected function setUp(): void |
11 | 49 | {
|
12 |
| - /** @var $layout \Magento\Framework\View\Layout */ |
13 |
| - $layout = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get( |
14 |
| - \Magento\Framework\View\LayoutInterface::class |
15 |
| - ); |
16 |
| - /** @var $block \Magento\Catalog\Block\Product\ProductList\Toolbar */ |
17 |
| - $block = $layout->createBlock(\Magento\Catalog\Block\Product\ProductList\Toolbar::class, 'block'); |
18 |
| - /** @var $childBlock \Magento\Framework\View\Element\Text */ |
19 |
| - $childBlock = $layout->addBlock( |
20 |
| - \Magento\Framework\View\Element\Text::class, |
| 50 | + parent::setUp(); |
| 51 | + |
| 52 | + $this->objectManager = Bootstrap::getObjectManager(); |
| 53 | + $this->layout = $this->objectManager->get(LayoutInterface::class); |
| 54 | + $this->toolbarBlock = $this->layout->createBlock(Toolbar::class); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * @return void |
| 59 | + */ |
| 60 | + public function testGetPagerHtml(): void |
| 61 | + { |
| 62 | + $this->toolbarBlock->setNameInLayout('block'); |
| 63 | + /** @var $childBlock Text */ |
| 64 | + $childBlock = $this->layout->addBlock( |
| 65 | + Text::class, |
21 | 66 | 'product_list_toolbar_pager',
|
22 | 67 | 'block'
|
23 | 68 | );
|
24 |
| - |
25 | 69 | $expectedHtml = '<b>Any text there</b>';
|
26 |
| - $this->assertNotEquals($expectedHtml, $block->getPagerHtml()); |
| 70 | + $this->assertNotEquals($expectedHtml, $this->toolbarBlock->getPagerHtml()); |
27 | 71 | $childBlock->setText($expectedHtml);
|
28 |
| - $this->assertEquals($expectedHtml, $block->getPagerHtml()); |
| 72 | + $this->assertEquals($expectedHtml, $this->toolbarBlock->getPagerHtml()); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * @magentoConfigFixture current_store catalog/frontend/list_mode grid |
| 77 | + * @return void |
| 78 | + */ |
| 79 | + public function testToHtmlGridOnly(): void |
| 80 | + { |
| 81 | + $htmlOutput = $this->getModeSwitcherHtml(); |
| 82 | + $this->assertNotEmpty($htmlOutput); |
| 83 | + $this->assertEquals( |
| 84 | + 0, |
| 85 | + Xpath::getElementsCountForXpath( |
| 86 | + sprintf(self::XPATH_TEMPLATE_FOR_NOT_VISIBLE_ICON_CASES, 'grid'), |
| 87 | + $htmlOutput |
| 88 | + ) |
| 89 | + ); |
| 90 | + $this->assertEquals( |
| 91 | + 0, |
| 92 | + Xpath::getElementsCountForXpath( |
| 93 | + sprintf(self::XPATH_TEMPLATE_FOR_NOT_VISIBLE_ICON_CASES, 'list'), |
| 94 | + $htmlOutput |
| 95 | + ) |
| 96 | + ); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * @magentoConfigFixture current_store catalog/frontend/list_mode list |
| 101 | + * @return void |
| 102 | + */ |
| 103 | + public function testToHtmlListOnly(): void |
| 104 | + { |
| 105 | + $htmlOutput = $this->getModeSwitcherHtml(); |
| 106 | + $this->assertNotEmpty($htmlOutput); |
| 107 | + $this->assertEquals( |
| 108 | + 0, |
| 109 | + Xpath::getElementsCountForXpath( |
| 110 | + sprintf(self::XPATH_TEMPLATE_FOR_NOT_VISIBLE_ICON_CASES, 'grid'), |
| 111 | + $htmlOutput |
| 112 | + ) |
| 113 | + ); |
| 114 | + $this->assertEquals( |
| 115 | + 0, |
| 116 | + Xpath::getElementsCountForXpath( |
| 117 | + sprintf(self::XPATH_TEMPLATE_FOR_NOT_VISIBLE_ICON_CASES, 'list'), |
| 118 | + $htmlOutput |
| 119 | + ) |
| 120 | + ); |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * @magentoConfigFixture current_store catalog/frontend/list_mode grid-list |
| 125 | + * @return void |
| 126 | + */ |
| 127 | + public function testToHtmlGridList(): void |
| 128 | + { |
| 129 | + $htmlOutput = $this->getModeSwitcherHtml(); |
| 130 | + $this->assertEquals( |
| 131 | + 1, |
| 132 | + Xpath::getElementsCountForXpath( |
| 133 | + sprintf(self::ACTIVE_MODE_XPATH_TEMPLATE, 'grid'), |
| 134 | + $htmlOutput |
| 135 | + ) |
| 136 | + ); |
| 137 | + $this->assertEquals( |
| 138 | + 1, |
| 139 | + Xpath::getElementsCountForXpath( |
| 140 | + sprintf(self::NOT_ACTIVE_MODE_XPATH_TEMPLATE, 'list'), |
| 141 | + $htmlOutput |
| 142 | + ) |
| 143 | + ); |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * @magentoConfigFixture current_store catalog/frontend/list_mode list-grid |
| 148 | + * @return void |
| 149 | + */ |
| 150 | + public function testToHtmlListGrid(): void |
| 151 | + { |
| 152 | + $htmlOutput = $this->getModeSwitcherHtml(); |
| 153 | + $this->assertEquals( |
| 154 | + 1, |
| 155 | + Xpath::getElementsCountForXpath( |
| 156 | + sprintf(self::ACTIVE_MODE_XPATH_TEMPLATE, 'list'), |
| 157 | + $htmlOutput |
| 158 | + ) |
| 159 | + ); |
| 160 | + $this->assertEquals( |
| 161 | + 1, |
| 162 | + Xpath::getElementsCountForXpath( |
| 163 | + sprintf(self::NOT_ACTIVE_MODE_XPATH_TEMPLATE, 'grid'), |
| 164 | + $htmlOutput |
| 165 | + ) |
| 166 | + ); |
| 167 | + } |
| 168 | + |
| 169 | + /** |
| 170 | + * Mode switcher html |
| 171 | + * |
| 172 | + * @return string |
| 173 | + */ |
| 174 | + private function getModeSwitcherHtml(): string |
| 175 | + { |
| 176 | + $this->toolbarBlock->setTemplate('Magento_Catalog::product/list/toolbar/viewmode.phtml'); |
| 177 | + |
| 178 | + return $this->toolbarBlock->toHtml(); |
29 | 179 | }
|
30 | 180 | }
|
0 commit comments