Skip to content

Commit dfaee32

Browse files
committed
Merge remote-tracking branch 'origin/MC-29689' into 2.4-develop-com-pr2
2 parents 32320e2 + 27f7626 commit dfaee32

File tree

6 files changed

+354
-42
lines changed

6 files changed

+354
-42
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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\Controller\Adminhtml\Product\Save;
9+
10+
use Magento\Catalog\Api\ProductRepositoryInterface;
11+
use Magento\Customer\Api\Data\GroupInterface;
12+
use Magento\Framework\App\Request\Http;
13+
use Magento\Framework\Message\MessageInterface;
14+
use Magento\TestFramework\TestCase\AbstractBackendController;
15+
16+
/**
17+
* Test cases for set advanced price to product.
18+
*
19+
* @magentoAppArea adminhtml
20+
* @magentoDbIsolation enabled
21+
*/
22+
class AdvancedPricingTest extends AbstractBackendController
23+
{
24+
/**
25+
* @var ProductRepositoryInterface
26+
*/
27+
private $productRepository;
28+
29+
/**
30+
* @inheritdoc
31+
*/
32+
protected function setUp()
33+
{
34+
parent::setUp();
35+
$this->productRepository = $this->_objectManager->get(ProductRepositoryInterface::class);
36+
}
37+
38+
/**
39+
* Assert that special price correctly saved to product.
40+
*
41+
* @magentoDataFixture Magento/Catalog/_files/product_without_options.php
42+
*
43+
* @return void
44+
*/
45+
public function testAddSpecialPriceToProduct(): void
46+
{
47+
$product = $this->productRepository->get('simple');
48+
$postData = [
49+
'product' => [
50+
'special_price' => 8,
51+
],
52+
];
53+
$this->assertNull($product->getSpecialPrice());
54+
$this->dispatchWithData((int)$product->getEntityId(), $postData);
55+
$product = $this->productRepository->get('simple', false, null, true);
56+
$this->assertEquals(8, $product->getSpecialPrice());
57+
}
58+
59+
/**
60+
* Assert that tier price correctly saved to product.
61+
*
62+
* @magentoDataFixture Magento/Catalog/_files/product_without_options.php
63+
*
64+
* @return void
65+
*/
66+
public function testAddTierPriceToProduct(): void
67+
{
68+
$product = $this->productRepository->get('simple');
69+
$postData = [
70+
'product' => [
71+
'tier_price' => [
72+
[
73+
'website_id' => '0',
74+
'cust_group' => GroupInterface::CUST_GROUP_ALL,
75+
'price_qty' => '100',
76+
'price' => 5,
77+
'value_type' => 'fixed',
78+
]
79+
],
80+
],
81+
];
82+
$this->assertEquals(10, $product->getTierPrice(100));
83+
$this->dispatchWithData((int)$product->getEntityId(), $postData);
84+
$product = $this->productRepository->get('simple', false, null, true);
85+
$this->assertEquals(5, $product->getTierPrice(100));
86+
}
87+
88+
/**
89+
* Dispatch product save with data.
90+
*
91+
* @param int $productId
92+
* @param array $productPostData
93+
* @return void
94+
*/
95+
private function dispatchWithData(int $productId, array $productPostData): void
96+
{
97+
$this->getRequest()->setPostValue($productPostData);
98+
$this->getRequest()->setMethod(Http::METHOD_POST);
99+
$this->dispatch('backend/catalog/product/save/id/' . $productId);
100+
$this->assertSessionMessages(
101+
$this->contains('You saved the product.'),
102+
MessageInterface::TYPE_SUCCESS
103+
);
104+
}
105+
}

dev/tests/integration/testsuite/Magento/Catalog/Model/Product/Type/PriceTest.php

Lines changed: 151 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3,105 +3,214 @@
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\Model\Product\Type;
79

10+
use Magento\Catalog\Api\Data\ProductCustomOptionInterface;
11+
use Magento\Catalog\Api\ProductRepositoryInterface;
812
use Magento\Catalog\Model\Product;
13+
use Magento\Catalog\Model\Product\Option;
14+
use Magento\Customer\Model\Session;
15+
use Magento\Framework\DataObject;
16+
use Magento\TestFramework\Helper\Bootstrap;
17+
use Magento\TestFramework\ObjectManager;
18+
use PHPUnit\Framework\TestCase;
919

1020
/**
21+
* Simple product price test.
22+
*
1123
* @magentoDataFixture Magento/Catalog/_files/product_simple.php
24+
* @magentoDbIsolation enabled
1225
*/
13-
class PriceTest extends \PHPUnit\Framework\TestCase
26+
class PriceTest extends TestCase
1427
{
1528
/**
16-
* @var \Magento\Catalog\Model\Product\Type\Price
29+
* @var ObjectManager
30+
*/
31+
private $objectManager;
32+
33+
/**
34+
* @var Price
1735
*/
18-
protected $_model;
36+
private $productPrice;
1937

20-
protected function setUp()
38+
/**
39+
* @var ProductRepositoryInterface
40+
*/
41+
private $productRepository;
42+
43+
/**
44+
* @var Session
45+
*/
46+
private $customerSession;
47+
48+
/**
49+
* @inheritdoc
50+
*/
51+
protected function setUp(): void
2152
{
22-
$this->_model = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
23-
\Magento\Catalog\Model\Product\Type\Price::class
24-
);
53+
$this->objectManager = Bootstrap::getObjectManager();
54+
$this->productPrice = $this->objectManager->create(Price::class);
55+
$this->productRepository = $this->objectManager->create(ProductRepositoryInterface::class);
56+
$this->customerSession = $this->objectManager->get(Session::class);
2557
}
2658

27-
public function testGetPrice()
59+
/**
60+
* Assert that for logged user product price equal to price from catalog rule.
61+
*
62+
* @magentoDataFixture Magento/Catalog/_files/product_simple.php
63+
* @magentoDataFixture Magento/CatalogRule/_files/catalog_rule_6_off_logged_user.php
64+
* @magentoDataFixture Magento/Customer/_files/customer.php
65+
*
66+
* @magentoDbIsolation disabled
67+
* @magentoAppArea frontend
68+
* @magentoAppIsolation enabled
69+
*
70+
* @return void
71+
*/
72+
public function testPriceByRuleForLoggedUser(): void
2873
{
29-
$this->assertEquals('test', $this->_model->getPrice(new \Magento\Framework\DataObject(['price' => 'test'])));
74+
$product = $this->productRepository->get('simple');
75+
$this->assertEquals(10, $this->productPrice->getFinalPrice(1, $product));
76+
$this->customerSession->setCustomerId(1);
77+
try {
78+
$this->assertEquals(4, $this->productPrice->getFinalPrice(1, $product));
79+
} finally {
80+
$this->customerSession->setCustomerId(null);
81+
}
3082
}
3183

32-
public function testGetFinalPrice()
84+
/**
85+
* Assert price for different customer groups.
86+
*
87+
* @magentoDataFixture Magento/Catalog/_files/simple_product_with_tier_price_for_logged_user.php
88+
* @magentoDataFixture Magento/Customer/_files/customer.php
89+
*
90+
* @magentoAppIsolation enabled
91+
*
92+
* @return void
93+
*/
94+
public function testTierPriceWithDifferentCustomerGroups(): void
3395
{
34-
$repository = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
35-
\Magento\Catalog\Model\ProductRepository::class
36-
);
37-
$product = $repository->get('simple');
38-
// fixture
96+
$product = $this->productRepository->get('simple');
97+
$this->assertEquals(8, $this->productPrice->getFinalPrice(2, $product));
98+
$this->assertEquals(5, $this->productPrice->getFinalPrice(3, $product));
99+
$this->customerSession->setCustomerId(1);
100+
try {
101+
$this->assertEquals(1, $this->productPrice->getFinalPrice(3, $product));
102+
} finally {
103+
$this->customerSession->setCustomerId(null);
104+
}
105+
}
106+
107+
/**
108+
* Get price from custom object.
109+
*
110+
* @return void
111+
*/
112+
public function testGetPrice(): void
113+
{
114+
$objectWithPrice = $this->objectManager->create(DataObject::class, ['data' => ['price' => 'test']]);
115+
$this->assertEquals('test', $this->productPrice->getPrice($objectWithPrice));
116+
}
117+
118+
/**
119+
* Get product final price for different product count.
120+
*
121+
* @return void
122+
*/
123+
public function testGetFinalPrice(): void
124+
{
125+
$product = $this->productRepository->get('simple');
39126

40127
// regular & tier prices
41-
$this->assertEquals(10.0, $this->_model->getFinalPrice(1, $product));
42-
$this->assertEquals(8.0, $this->_model->getFinalPrice(2, $product));
43-
$this->assertEquals(5.0, $this->_model->getFinalPrice(5, $product));
128+
$this->assertEquals(10.0, $this->productPrice->getFinalPrice(1, $product));
129+
$this->assertEquals(8.0, $this->productPrice->getFinalPrice(2, $product));
130+
$this->assertEquals(5.0, $this->productPrice->getFinalPrice(5, $product));
44131

45132
// with options
46133
$buyRequest = $this->prepareBuyRequest($product);
47134
$product->getTypeInstance()->prepareForCart($buyRequest, $product);
48135

49136
//product price + options price(10+1+2+3+3)
50-
$this->assertEquals(19.0, $this->_model->getFinalPrice(1, $product));
137+
$this->assertEquals(19.0, $this->productPrice->getFinalPrice(1, $product));
51138

52139
//product tier price + options price(5+1+2+3+3)
53-
$this->assertEquals(14.0, $this->_model->getFinalPrice(5, $product));
140+
$this->assertEquals(14.0, $this->productPrice->getFinalPrice(5, $product));
54141
}
55142

56-
public function testGetFormatedPrice()
143+
/**
144+
* Assert that formated price is correct.
145+
*
146+
* @return void
147+
*/
148+
public function testGetFormatedPrice(): void
57149
{
58-
$repository = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
59-
\Magento\Catalog\Model\ProductRepository::class
60-
);
61-
$product = $repository->get('simple');
62-
// fixture
63-
$this->assertEquals('<span class="price">$10.00</span>', $this->_model->getFormatedPrice($product));
150+
$product = $this->productRepository->get('simple');
151+
$this->assertEquals('<span class="price">$10.00</span>', $this->productPrice->getFormatedPrice($product));
64152
}
65153

66-
public function testCalculatePrice()
154+
/**
155+
* Test calculate price by date.
156+
*
157+
* @return void
158+
*/
159+
public function testCalculatePrice(): void
67160
{
68-
$this->assertEquals(10, $this->_model->calculatePrice(10, 8, '1970-12-12 23:59:59', '1971-01-01 01:01:01'));
69-
$this->assertEquals(8, $this->_model->calculatePrice(10, 8, '1970-12-12 23:59:59', '2034-01-01 01:01:01'));
161+
$this->assertEquals(
162+
10,
163+
$this->productPrice->calculatePrice(10, 8, '1970-12-12 23:59:59', '1971-01-01 01:01:01')
164+
);
165+
$this->assertEquals(
166+
8,
167+
$this->productPrice->calculatePrice(10, 8, '1970-12-12 23:59:59', '2034-01-01 01:01:01')
168+
);
70169
}
71170

72-
public function testCalculateSpecialPrice()
171+
/**
172+
* Test calculate price by date.
173+
*
174+
* @return void
175+
*/
176+
public function testCalculateSpecialPrice(): void
73177
{
74178
$this->assertEquals(
75179
10,
76-
$this->_model->calculateSpecialPrice(10, 8, '1970-12-12 23:59:59', '1971-01-01 01:01:01')
180+
$this->productPrice->calculateSpecialPrice(10, 8, '1970-12-12 23:59:59', '1971-01-01 01:01:01')
77181
);
78182
$this->assertEquals(
79183
8,
80-
$this->_model->calculateSpecialPrice(10, 8, '1970-12-12 23:59:59', '2034-01-01 01:01:01')
184+
$this->productPrice->calculateSpecialPrice(10, 8, '1970-12-12 23:59:59', '2034-01-01 01:01:01')
81185
);
82186
}
83187

84-
public function testIsTierPriceFixed()
188+
/**
189+
* Assert that product tier price is fixed.
190+
*
191+
* @return void
192+
*/
193+
public function testIsTierPriceFixed(): void
85194
{
86-
$this->assertTrue($this->_model->isTierPriceFixed());
195+
$this->assertTrue($this->productPrice->isTierPriceFixed());
87196
}
88197

89198
/**
90-
* Build buy request based on product custom options
199+
* Build buy request based on product custom options.
91200
*
92201
* @param Product $product
93-
* @return \Magento\Framework\DataObject
202+
* @return DataObject
94203
*/
95-
private function prepareBuyRequest(Product $product)
204+
private function prepareBuyRequest(Product $product): DataObject
96205
{
97206
$options = [];
98-
/** @var $option \Magento\Catalog\Model\Product\Option */
207+
/** @var Option $option */
99208
foreach ($product->getOptions() as $option) {
100209
switch ($option->getGroupByType()) {
101-
case \Magento\Catalog\Api\Data\ProductCustomOptionInterface::OPTION_GROUP_DATE:
210+
case ProductCustomOptionInterface::OPTION_GROUP_DATE:
102211
$value = ['year' => 2013, 'month' => 8, 'day' => 9, 'hour' => 13, 'minute' => 35];
103212
break;
104-
case \Magento\Catalog\Api\Data\ProductCustomOptionInterface::OPTION_GROUP_SELECT:
213+
case ProductCustomOptionInterface::OPTION_GROUP_SELECT:
105214
$value = key($option->getValues());
106215
break;
107216
default:
@@ -111,6 +220,6 @@ private function prepareBuyRequest(Product $product)
111220
$options[$option->getId()] = $value;
112221
}
113222

114-
return new \Magento\Framework\DataObject(['qty' => 1, 'options' => $options]);
223+
return $this->objectManager->create(DataObject::class, ['data' => ['qty' => 1, 'options' => $options]]);
115224
}
116225
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
require __DIR__ . '/product_simple.php';
9+
10+
$product = $productRepository->get('simple', false, null, true);
11+
$tierPrices = $product->getTierPrices() ?? [];
12+
$tierPriceExtensionAttributes = $tpExtensionAttributesFactory->create()->setWebsiteId($adminWebsite->getId());
13+
$tierPrices[] = $tierPriceFactory->create(
14+
[
15+
'data' => [
16+
'customer_group_id' => 1,
17+
'qty' => 3,
18+
'value' => 1
19+
]
20+
]
21+
)->setExtensionAttributes($tierPriceExtensionAttributes);
22+
$product->setTierPrices($tierPrices);
23+
$productRepository->save($product);

0 commit comments

Comments
 (0)