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