Skip to content

Commit 5ee9e62

Browse files
authored
Merge pull request #6516 from magento-tsg/2.4-develop-sidecar-pr10
[Sidecar] Fixes for 2.4 (pr10)
2 parents 1e846a4 + bfcc24b commit 5ee9e62

File tree

7 files changed

+585
-91
lines changed

7 files changed

+585
-91
lines changed
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Catalog\Block\Adminhtml\Category\Tab;
9+
10+
use Magento\Catalog\Api\CategoryRepositoryInterface;
11+
use Magento\Catalog\Api\Data\CategoryInterface;
12+
use Magento\Catalog\Model\ResourceModel\Collection\AbstractCollection;
13+
use Magento\Framework\Registry;
14+
use Magento\Framework\View\LayoutInterface;
15+
use Magento\TestFramework\Helper\Bootstrap;
16+
use PHPUnit\Framework\TestCase;
17+
use Magento\Framework\ObjectManagerInterface;
18+
use Magento\Catalog\Api\Data\ProductInterface;
19+
20+
/**
21+
* Checks grid data on the tab 'Products in Category' category view page.
22+
*
23+
* @see \Magento\Catalog\Block\Adminhtml\Category\Tab\Product
24+
* @magentoAppArea adminhtml
25+
* @magentoDbIsolation enabled
26+
* @magentoAppIsolation enabled
27+
*/
28+
class ProductTest extends TestCase
29+
{
30+
/** @var ObjectManagerInterface */
31+
private $objectManager;
32+
33+
/** @var LayoutInterface */
34+
private $layout;
35+
36+
/** @var CategoryRepositoryInterface */
37+
private $categoryRepository;
38+
39+
/** @var Registry */
40+
private $registry;
41+
42+
/**
43+
* @inheritdoc
44+
*/
45+
protected function setUp(): void
46+
{
47+
parent::setUp();
48+
49+
$this->objectManager = Bootstrap::getObjectManager();
50+
$this->layout = $this->objectManager->get(LayoutInterface::class);
51+
$this->categoryRepository = $this->objectManager->get(CategoryRepositoryInterface::class);
52+
$this->registry = $this->objectManager->get(Registry::class);
53+
}
54+
55+
/**
56+
* @inheritdoc
57+
*/
58+
protected function tearDown(): void
59+
{
60+
$this->registry->unregister('category');
61+
62+
parent::tearDown();
63+
}
64+
65+
/**
66+
* @magentoDataFixture Magento/Catalog/_files/category_with_two_products.php
67+
* @magentoDataFixture Magento/Catalog/_files/product_associated.php
68+
* @magentoDataFixture Magento/Catalog/_files/simple_product_disabled.php
69+
* @dataProvider optionsFilterProvider
70+
* @param string $filterColumn
71+
* @param int $categoryId
72+
* @param int $storeId
73+
* @param array $items
74+
* @return void
75+
*/
76+
public function testFilterProductInCategory(string $filterColumn, int $categoryId, int $storeId, array $items): void
77+
{
78+
$collection = $this->filterProductInGrid($filterColumn, $categoryId, $storeId);
79+
$this->assertCount(count($items), $collection->getItems());
80+
foreach ($items as $item) {
81+
$this->assertNotNull($collection->getItemByColumnValue(ProductInterface::SKU, $item));
82+
}
83+
}
84+
85+
/**
86+
* @magentoDataFixture Magento/Catalog/_files/category.php
87+
* @return void
88+
*/
89+
public function testEmptyProductIdColumnFilter(): void
90+
{
91+
$this->assertCount(0, $this->filterProductInGrid('in_category=1', 333, 1)->getItems());
92+
}
93+
94+
/**
95+
* Different variations for filter test.
96+
*
97+
* @return array
98+
*/
99+
public function optionsFilterProvider(): array
100+
{
101+
return [
102+
'filter_yes' => [
103+
'filter_column' => 'in_category=1',
104+
'id_category' => 333,
105+
'store_id' => 1,
106+
'items' => [
107+
'simple333',
108+
'simple2',
109+
],
110+
],
111+
'filter_no' => [
112+
'filter_column' => 'in_category=0',
113+
'id_category' => 333,
114+
'store_id' => 1,
115+
'items' => [
116+
'product_disabled',
117+
'simple',
118+
],
119+
],
120+
'filter_any' => [
121+
'filter_column' => "",
122+
'id_category' => 333,
123+
'store_id' => 1,
124+
'items' => [
125+
'product_disabled',
126+
'simple333',
127+
'simple2',
128+
'simple',
129+
],
130+
],
131+
'flag_status' => [
132+
'filter_column' => 'status=1',
133+
'id_category' => 333,
134+
'store_id' => 1,
135+
'items' => [
136+
'simple333',
137+
'simple2',
138+
'simple',
139+
],
140+
],
141+
];
142+
}
143+
144+
/**
145+
* Filter product in grid
146+
*
147+
* @param string $filterOption
148+
* @param int $categoryId
149+
* @param int $storeId
150+
* @return AbstractCollection
151+
*/
152+
private function filterProductInGrid(string $filterOption, int $categoryId, int $storeId): AbstractCollection
153+
{
154+
$this->registerCategory($this->categoryRepository->get($categoryId));
155+
$block = $this->layout->createBlock(Product::class);
156+
$block->getRequest()->setParams([
157+
'id' => $categoryId,
158+
'filter' => base64_encode($filterOption),
159+
'store' => $storeId,
160+
]);
161+
$block->toHtml();
162+
163+
return $block->getCollection();
164+
}
165+
166+
/**
167+
* Register category in registry
168+
*
169+
* @param CategoryInterface $category
170+
* @return void
171+
*/
172+
private function registerCategory(CategoryInterface $category): void
173+
{
174+
$this->registry->unregister('category');
175+
$this->registry->register('category', $category);
176+
}
177+
}

dev/tests/integration/testsuite/Magento/Catalog/Block/Product/ProductList/ToolbarTest.php

Lines changed: 164 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,178 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
7+
68
namespace Magento\Catalog\Block\Product\ProductList;
79

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
925
{
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
1149
{
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,
2166
'product_list_toolbar_pager',
2267
'block'
2368
);
24-
2569
$expectedHtml = '<b>Any text there</b>';
26-
$this->assertNotEquals($expectedHtml, $block->getPagerHtml());
70+
$this->assertNotEquals($expectedHtml, $this->toolbarBlock->getPagerHtml());
2771
$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();
29179
}
30180
}

0 commit comments

Comments
 (0)