Skip to content

Commit 1d7b207

Browse files
MC-36376: Storefront:Configurable prices per websites
1 parent 1452016 commit 1d7b207

File tree

6 files changed

+443
-28
lines changed

6 files changed

+443
-28
lines changed

dev/tests/integration/testsuite/Magento/ConfigurableProduct/Block/Product/View/Type/ConfigurableProductPriceTest.php

Lines changed: 55 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@
1414
use Magento\Framework\Registry;
1515
use Magento\Framework\Serialize\SerializerInterface;
1616
use Magento\Framework\View\Result\Page;
17+
use Magento\Framework\View\Result\PageFactory;
18+
use Magento\Store\Model\StoreManagerInterface;
1719
use Magento\TestFramework\Helper\Bootstrap;
20+
use Magento\TestFramework\Store\ExecuteInStoreContext;
1821
use PHPUnit\Framework\TestCase;
1922

2023
/**
@@ -44,6 +47,12 @@ class ConfigurableProductPriceTest extends TestCase
4447
/** @var SerializerInterface */
4548
private $json;
4649

50+
/** @var StoreManagerInterface */
51+
private $storeManager;
52+
53+
/** @var ExecuteInStoreContext */
54+
private $executeInStoreContext;
55+
4756
/**
4857
* @inheritdoc
4958
*/
@@ -53,11 +62,13 @@ protected function setUp(): void
5362

5463
$this->objectManager = Bootstrap::getObjectManager();
5564
$this->registry = $this->objectManager->get(Registry::class);
56-
$this->page = $this->objectManager->get(Page::class);
65+
$this->page = $this->objectManager->get(PageFactory::class)->create();
5766
$this->productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
5867
$this->productRepository->cleanCache();
5968
$this->productCustomOption = $this->objectManager->get(ProductCustomOptionInterface::class);
6069
$this->json = $this->objectManager->get(SerializerInterface::class);
70+
$this->storeManager = $this->objectManager->get(StoreManagerInterface::class);
71+
$this->executeInStoreContext = $this->objectManager->get(ExecuteInStoreContext::class);
6172
}
6273

6374
/**
@@ -78,7 +89,20 @@ protected function tearDown(): void
7889
*/
7990
public function testConfigurablePrice(): void
8091
{
81-
$this->assertPrice($this->processPriceView('configurable'), 10.00);
92+
$this->assertPrice('configurable', 10.00);
93+
}
94+
95+
/**
96+
* @magentoDataFixture Magento/ConfigurableProduct/_files/configurable_product_with_price_on_second_website.php
97+
* @magentoDbIsolation disabled
98+
*
99+
* @return void
100+
*/
101+
public function testConfigurablePriceOnSecondWebsite(): void
102+
{
103+
$this->executeInStoreContext->execute('fixture_second_store', [$this, 'assertPrice'], 'configurable', 10.00);
104+
$this->resetPageLayout();
105+
$this->assertPrice('configurable', 150.00);
82106
}
83107

84108
/**
@@ -88,7 +112,7 @@ public function testConfigurablePrice(): void
88112
*/
89113
public function testConfigurablePriceWithDisabledFirstChild(): void
90114
{
91-
$this->assertPrice($this->processPriceView('configurable'), 20.00);
115+
$this->assertPrice('configurable', 20.00);
92116
}
93117

94118
/**
@@ -98,7 +122,7 @@ public function testConfigurablePriceWithDisabledFirstChild(): void
98122
*/
99123
public function testConfigurablePriceWithOutOfStockFirstChild(): void
100124
{
101-
$this->assertPrice($this->processPriceView('configurable'), 20.00);
125+
$this->assertPrice('configurable', 20.00);
102126
}
103127

104128
/**
@@ -110,7 +134,7 @@ public function testConfigurablePriceWithOutOfStockFirstChild(): void
110134
*/
111135
public function testConfigurablePriceWithCatalogRule(): void
112136
{
113-
$this->assertPrice($this->processPriceView('configurable'), 9.00);
137+
$this->assertPrice('configurable', 9.00);
114138
}
115139

116140
/**
@@ -120,7 +144,7 @@ public function testConfigurablePriceWithCatalogRule(): void
120144
*/
121145
public function testConfigurablePriceWithCustomOption(): void
122146
{
123-
$product = $this->productRepository->get('configurable');
147+
$product = $this->getProduct('configurable');
124148
$this->registerProduct($product);
125149
$this->preparePageLayout();
126150
$customOptionsBlock = $this->page->getLayout()
@@ -162,6 +186,16 @@ private function preparePageLayout(): void
162186
$this->page->getLayout()->generateXml();
163187
}
164188

189+
/**
190+
* Reset layout page to get new block html.
191+
*
192+
* @return void
193+
*/
194+
private function resetPageLayout(): void
195+
{
196+
$this->page = $this->objectManager->get(PageFactory::class)->create();
197+
}
198+
165199
/**
166200
* Process view product final price block html.
167201
*
@@ -170,7 +204,7 @@ private function preparePageLayout(): void
170204
*/
171205
private function processPriceView(string $sku): string
172206
{
173-
$product = $this->productRepository->get($sku);
207+
$product = $this->getProduct($sku);
174208
$this->registerProduct($product);
175209
$this->preparePageLayout();
176210

@@ -180,12 +214,13 @@ private function processPriceView(string $sku): string
180214
/**
181215
* Assert that html contain price label and expected final price amount.
182216
*
183-
* @param string $priceBlockHtml
217+
* @param string $sku
184218
* @param float $expectedPrice
185219
* @return void
186220
*/
187-
private function assertPrice(string $priceBlockHtml, float $expectedPrice): void
221+
public function assertPrice(string $sku, float $expectedPrice): void
188222
{
223+
$priceBlockHtml = $this->processPriceView($sku);
189224
$regexp = '/<span class="price-label">As low as<\/span>.*';
190225
$regexp .= '<span.*data-price-amount="%s".*<span class="price">\$%.2f<\/span><\/span>/';
191226
$this->assertMatchesRegularExpression(
@@ -208,4 +243,15 @@ private function assertJsonConfig(string $config, string $expectedPrice, int $op
208243
$this->assertNotNull($price);
209244
$this->assertEquals($expectedPrice, $price);
210245
}
246+
247+
/**
248+
* Loads product by sku.s
249+
*
250+
* @param string $sku
251+
* @return ProductInterface
252+
*/
253+
private function getProduct(string $sku): ProductInterface
254+
{
255+
return $this->productRepository->get($sku, false, $this->storeManager->getStore()->getId(), true);
256+
}
211257
}

dev/tests/integration/testsuite/Magento/ConfigurableProduct/Block/Product/View/Type/ConfigurableViewOnCategoryPageTest.php

Lines changed: 147 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,18 @@
77

88
namespace Magento\ConfigurableProduct\Block\Product\View\Type;
99

10+
use Magento\Catalog\Api\CategoryRepositoryInterface;
11+
use Magento\Catalog\Api\Data\ProductInterface;
12+
use Magento\Catalog\Api\ProductRepositoryInterface;
1013
use Magento\Catalog\Block\Product\ListProduct;
1114
use Magento\Eav\Model\Entity\Collection\AbstractCollection;
1215
use Magento\Framework\ObjectManagerInterface;
13-
use Magento\Framework\View\LayoutInterface;
16+
use Magento\Framework\Registry;
17+
use Magento\Framework\View\Result\Page;
18+
use Magento\Framework\View\Result\PageFactory;
19+
use Magento\Store\Model\StoreManagerInterface;
1420
use Magento\TestFramework\Helper\Bootstrap;
21+
use Magento\TestFramework\Store\ExecuteInStoreContext;
1522
use PHPUnit\Framework\TestCase;
1623

1724
/**
@@ -21,17 +28,30 @@
2128
* @magentoAppIsolation enabled
2229
* @magentoAppArea frontend
2330
* @magentoDataFixture Magento/ConfigurableProduct/_files/configurable_product_with_out_of_stock_children.php
31+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2432
*/
2533
class ConfigurableViewOnCategoryPageTest extends TestCase
2634
{
2735
/** @var ObjectManagerInterface */
2836
private $objectManager;
2937

30-
/** @var LayoutInterface */
31-
private $layout;
38+
/** @var ProductRepositoryInterface */
39+
private $productRepository;
3240

33-
/** @var ListProduct $listingBlock */
34-
private $listingBlock;
41+
/** @var CategoryRepositoryInterface */
42+
private $categoryRepository;
43+
44+
/** @var Page */
45+
private $page;
46+
47+
/** @var Registry */
48+
private $registry;
49+
50+
/** @var StoreManagerInterface */
51+
private $storeManager;
52+
53+
/** @var ExecuteInStoreContext */
54+
private $executeInStoreContext;
3555

3656
/**
3757
* @inheritdoc
@@ -41,9 +61,22 @@ protected function setUp(): void
4161
parent::setUp();
4262

4363
$this->objectManager = Bootstrap::getObjectManager();
44-
$this->layout = $this->objectManager->get(LayoutInterface::class);
45-
$this->listingBlock = $this->layout->createBlock(ListProduct::class);
46-
$this->listingBlock->setCategoryId(333);
64+
$this->productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
65+
$this->productRepository->cleanCache();
66+
$this->categoryRepository = $this->objectManager->get(CategoryRepositoryInterface::class);
67+
$this->page = $this->objectManager->get(PageFactory::class)->create();
68+
$this->registry = $this->objectManager->get(Registry::class);
69+
$this->storeManager = $this->objectManager->get(StoreManagerInterface::class);
70+
$this->executeInStoreContext = $this->objectManager->get(ExecuteInStoreContext::class);
71+
}
72+
73+
/**
74+
* @inheritdoc
75+
*/
76+
protected function tearDown(): void
77+
{
78+
$this->registry->unregister('current_category');
79+
parent::tearDown();
4780
}
4881

4982
/**
@@ -53,8 +86,8 @@ protected function setUp(): void
5386
*/
5487
public function testOutOfStockProductWithEnabledConfigView(): void
5588
{
56-
$collection = $this->listingBlock->getLoadedProductCollection();
57-
$this->assertCollectionSize(1, $collection);
89+
$this->preparePageLayout();
90+
$this->assertCollectionSize(1, $this->getListingBlock()->getLoadedProductCollection());
5891
}
5992

6093
/**
@@ -64,8 +97,50 @@ public function testOutOfStockProductWithEnabledConfigView(): void
6497
*/
6598
public function testOutOfStockProductWithDisabledConfigView(): void
6699
{
67-
$collection = $this->listingBlock->getLoadedProductCollection();
68-
$this->assertCollectionSize(0, $collection);
100+
$this->preparePageLayout();
101+
$this->assertCollectionSize(0, $this->getListingBlock()->getLoadedProductCollection());
102+
}
103+
104+
/**
105+
* @magentoDataFixture Magento/ConfigurableProduct/_files/configurable_product_with_category.php
106+
*
107+
* @return void
108+
*/
109+
public function testCheckConfigurablePrice(): void
110+
{
111+
$this->assertProductPrice('configurable', 'As low as $10.00');
112+
}
113+
114+
/**
115+
* @magentoDataFixture Magento/ConfigurableProduct/_files/configurable_product_with_price_on_second_website.php
116+
*
117+
* @return void
118+
*/
119+
public function testCheckConfigurablePriceOnSecondWebsite(): void
120+
{
121+
$this->executeInStoreContext->execute(
122+
'fixture_second_store',
123+
[$this, 'assertProductPrice'],
124+
'configurable',
125+
__('As low as') . ' $10.00'
126+
);
127+
$this->resetPageLayout();
128+
$this->assertProductPrice('configurable', __('As low as') . ' $150.00');
129+
}
130+
131+
/**
132+
* Checks product price.
133+
*
134+
* @param string $sku
135+
* @param string $priceString
136+
* @return void
137+
*/
138+
public function assertProductPrice(string $sku, string $priceString): void
139+
{
140+
$this->preparePageLayout();
141+
$this->assertCollectionSize(1, $this->getListingBlock()->getLoadedProductCollection());
142+
$priceHtml = $this->getListingBlock()->getProductPrice($this->getProduct($sku));
143+
$this->assertEquals($priceString, $this->clearPriceHtml($priceHtml));
69144
}
70145

71146
/**
@@ -80,4 +155,64 @@ private function assertCollectionSize(int $expectedSize, AbstractCollection $col
80155
$this->assertEquals($expectedSize, $collection->getSize());
81156
$this->assertCount($expectedSize, $collection->getItems());
82157
}
158+
159+
/**
160+
* Prepare category page.
161+
*
162+
* @return void
163+
*/
164+
private function preparePageLayout(): void
165+
{
166+
$this->registry->unregister('current_category');
167+
$this->registry->register(
168+
'current_category',
169+
$this->categoryRepository->get(333, $this->storeManager->getStore()->getId())
170+
);
171+
$this->page->addHandle(['default', 'catalog_category_view']);
172+
$this->page->getLayout()->generateXml();
173+
}
174+
175+
/**
176+
* Reset layout page to get new block html.
177+
*
178+
* @return void
179+
*/
180+
private function resetPageLayout(): void
181+
{
182+
$this->page = $this->objectManager->get(PageFactory::class)->create();
183+
}
184+
185+
/**
186+
* Removes html tags and spaces from price html string.
187+
*
188+
* @param string $priceHtml
189+
* @return string
190+
*/
191+
private function clearPriceHtml(string $priceHtml): string
192+
{
193+
return trim(preg_replace('/\s+/', ' ', strip_tags($priceHtml)));
194+
}
195+
196+
/**
197+
* Returns product list block.
198+
*
199+
* @return null|ListProduct
200+
*/
201+
private function getListingBlock(): ?ListProduct
202+
{
203+
$block = $this->page->getLayout()->getBlock('category.products.list');
204+
205+
return $block ? $block : null;
206+
}
207+
208+
/**
209+
* Loads product by sku.
210+
*
211+
* @param string $sku
212+
* @return ProductInterface
213+
*/
214+
private function getProduct(string $sku): ProductInterface
215+
{
216+
return $this->productRepository->get($sku, false, $this->storeManager->getStore()->getId(), true);
217+
}
83218
}

0 commit comments

Comments
 (0)