Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/MC-29689' into 2.4-develop-com-pr2
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanPletnyov committed Dec 27, 2019
2 parents 32320e2 + 27f7626 commit dfaee32
Show file tree
Hide file tree
Showing 6 changed files with 354 additions and 42 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Catalog\Controller\Adminhtml\Product\Save;

use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Customer\Api\Data\GroupInterface;
use Magento\Framework\App\Request\Http;
use Magento\Framework\Message\MessageInterface;
use Magento\TestFramework\TestCase\AbstractBackendController;

/**
* Test cases for set advanced price to product.
*
* @magentoAppArea adminhtml
* @magentoDbIsolation enabled
*/
class AdvancedPricingTest extends AbstractBackendController
{
/**
* @var ProductRepositoryInterface
*/
private $productRepository;

/**
* @inheritdoc
*/
protected function setUp()
{
parent::setUp();
$this->productRepository = $this->_objectManager->get(ProductRepositoryInterface::class);
}

/**
* Assert that special price correctly saved to product.
*
* @magentoDataFixture Magento/Catalog/_files/product_without_options.php
*
* @return void
*/
public function testAddSpecialPriceToProduct(): void
{
$product = $this->productRepository->get('simple');
$postData = [
'product' => [
'special_price' => 8,
],
];
$this->assertNull($product->getSpecialPrice());
$this->dispatchWithData((int)$product->getEntityId(), $postData);
$product = $this->productRepository->get('simple', false, null, true);
$this->assertEquals(8, $product->getSpecialPrice());
}

/**
* Assert that tier price correctly saved to product.
*
* @magentoDataFixture Magento/Catalog/_files/product_without_options.php
*
* @return void
*/
public function testAddTierPriceToProduct(): void
{
$product = $this->productRepository->get('simple');
$postData = [
'product' => [
'tier_price' => [
[
'website_id' => '0',
'cust_group' => GroupInterface::CUST_GROUP_ALL,
'price_qty' => '100',
'price' => 5,
'value_type' => 'fixed',
]
],
],
];
$this->assertEquals(10, $product->getTierPrice(100));
$this->dispatchWithData((int)$product->getEntityId(), $postData);
$product = $this->productRepository->get('simple', false, null, true);
$this->assertEquals(5, $product->getTierPrice(100));
}

/**
* Dispatch product save with data.
*
* @param int $productId
* @param array $productPostData
* @return void
*/
private function dispatchWithData(int $productId, array $productPostData): void
{
$this->getRequest()->setPostValue($productPostData);
$this->getRequest()->setMethod(Http::METHOD_POST);
$this->dispatch('backend/catalog/product/save/id/' . $productId);
$this->assertSessionMessages(
$this->contains('You saved the product.'),
MessageInterface::TYPE_SUCCESS
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,105 +3,214 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Catalog\Model\Product\Type;

use Magento\Catalog\Api\Data\ProductCustomOptionInterface;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Catalog\Model\Product;
use Magento\Catalog\Model\Product\Option;
use Magento\Customer\Model\Session;
use Magento\Framework\DataObject;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\ObjectManager;
use PHPUnit\Framework\TestCase;

/**
* Simple product price test.
*
* @magentoDataFixture Magento/Catalog/_files/product_simple.php
* @magentoDbIsolation enabled
*/
class PriceTest extends \PHPUnit\Framework\TestCase
class PriceTest extends TestCase
{
/**
* @var \Magento\Catalog\Model\Product\Type\Price
* @var ObjectManager
*/
private $objectManager;

/**
* @var Price
*/
protected $_model;
private $productPrice;

protected function setUp()
/**
* @var ProductRepositoryInterface
*/
private $productRepository;

/**
* @var Session
*/
private $customerSession;

/**
* @inheritdoc
*/
protected function setUp(): void
{
$this->_model = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
\Magento\Catalog\Model\Product\Type\Price::class
);
$this->objectManager = Bootstrap::getObjectManager();
$this->productPrice = $this->objectManager->create(Price::class);
$this->productRepository = $this->objectManager->create(ProductRepositoryInterface::class);
$this->customerSession = $this->objectManager->get(Session::class);
}

public function testGetPrice()
/**
* Assert that for logged user product price equal to price from catalog rule.
*
* @magentoDataFixture Magento/Catalog/_files/product_simple.php
* @magentoDataFixture Magento/CatalogRule/_files/catalog_rule_6_off_logged_user.php
* @magentoDataFixture Magento/Customer/_files/customer.php
*
* @magentoDbIsolation disabled
* @magentoAppArea frontend
* @magentoAppIsolation enabled
*
* @return void
*/
public function testPriceByRuleForLoggedUser(): void
{
$this->assertEquals('test', $this->_model->getPrice(new \Magento\Framework\DataObject(['price' => 'test'])));
$product = $this->productRepository->get('simple');
$this->assertEquals(10, $this->productPrice->getFinalPrice(1, $product));
$this->customerSession->setCustomerId(1);
try {
$this->assertEquals(4, $this->productPrice->getFinalPrice(1, $product));
} finally {
$this->customerSession->setCustomerId(null);
}
}

public function testGetFinalPrice()
/**
* Assert price for different customer groups.
*
* @magentoDataFixture Magento/Catalog/_files/simple_product_with_tier_price_for_logged_user.php
* @magentoDataFixture Magento/Customer/_files/customer.php
*
* @magentoAppIsolation enabled
*
* @return void
*/
public function testTierPriceWithDifferentCustomerGroups(): void
{
$repository = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
\Magento\Catalog\Model\ProductRepository::class
);
$product = $repository->get('simple');
// fixture
$product = $this->productRepository->get('simple');
$this->assertEquals(8, $this->productPrice->getFinalPrice(2, $product));
$this->assertEquals(5, $this->productPrice->getFinalPrice(3, $product));
$this->customerSession->setCustomerId(1);
try {
$this->assertEquals(1, $this->productPrice->getFinalPrice(3, $product));
} finally {
$this->customerSession->setCustomerId(null);
}
}

/**
* Get price from custom object.
*
* @return void
*/
public function testGetPrice(): void
{
$objectWithPrice = $this->objectManager->create(DataObject::class, ['data' => ['price' => 'test']]);
$this->assertEquals('test', $this->productPrice->getPrice($objectWithPrice));
}

/**
* Get product final price for different product count.
*
* @return void
*/
public function testGetFinalPrice(): void
{
$product = $this->productRepository->get('simple');

// regular & tier prices
$this->assertEquals(10.0, $this->_model->getFinalPrice(1, $product));
$this->assertEquals(8.0, $this->_model->getFinalPrice(2, $product));
$this->assertEquals(5.0, $this->_model->getFinalPrice(5, $product));
$this->assertEquals(10.0, $this->productPrice->getFinalPrice(1, $product));
$this->assertEquals(8.0, $this->productPrice->getFinalPrice(2, $product));
$this->assertEquals(5.0, $this->productPrice->getFinalPrice(5, $product));

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

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

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

public function testGetFormatedPrice()
/**
* Assert that formated price is correct.
*
* @return void
*/
public function testGetFormatedPrice(): void
{
$repository = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
\Magento\Catalog\Model\ProductRepository::class
);
$product = $repository->get('simple');
// fixture
$this->assertEquals('<span class="price">$10.00</span>', $this->_model->getFormatedPrice($product));
$product = $this->productRepository->get('simple');
$this->assertEquals('<span class="price">$10.00</span>', $this->productPrice->getFormatedPrice($product));
}

public function testCalculatePrice()
/**
* Test calculate price by date.
*
* @return void
*/
public function testCalculatePrice(): void
{
$this->assertEquals(10, $this->_model->calculatePrice(10, 8, '1970-12-12 23:59:59', '1971-01-01 01:01:01'));
$this->assertEquals(8, $this->_model->calculatePrice(10, 8, '1970-12-12 23:59:59', '2034-01-01 01:01:01'));
$this->assertEquals(
10,
$this->productPrice->calculatePrice(10, 8, '1970-12-12 23:59:59', '1971-01-01 01:01:01')
);
$this->assertEquals(
8,
$this->productPrice->calculatePrice(10, 8, '1970-12-12 23:59:59', '2034-01-01 01:01:01')
);
}

public function testCalculateSpecialPrice()
/**
* Test calculate price by date.
*
* @return void
*/
public function testCalculateSpecialPrice(): void
{
$this->assertEquals(
10,
$this->_model->calculateSpecialPrice(10, 8, '1970-12-12 23:59:59', '1971-01-01 01:01:01')
$this->productPrice->calculateSpecialPrice(10, 8, '1970-12-12 23:59:59', '1971-01-01 01:01:01')
);
$this->assertEquals(
8,
$this->_model->calculateSpecialPrice(10, 8, '1970-12-12 23:59:59', '2034-01-01 01:01:01')
$this->productPrice->calculateSpecialPrice(10, 8, '1970-12-12 23:59:59', '2034-01-01 01:01:01')
);
}

public function testIsTierPriceFixed()
/**
* Assert that product tier price is fixed.
*
* @return void
*/
public function testIsTierPriceFixed(): void
{
$this->assertTrue($this->_model->isTierPriceFixed());
$this->assertTrue($this->productPrice->isTierPriceFixed());
}

/**
* Build buy request based on product custom options
* Build buy request based on product custom options.
*
* @param Product $product
* @return \Magento\Framework\DataObject
* @return DataObject
*/
private function prepareBuyRequest(Product $product)
private function prepareBuyRequest(Product $product): DataObject
{
$options = [];
/** @var $option \Magento\Catalog\Model\Product\Option */
/** @var Option $option */
foreach ($product->getOptions() as $option) {
switch ($option->getGroupByType()) {
case \Magento\Catalog\Api\Data\ProductCustomOptionInterface::OPTION_GROUP_DATE:
case ProductCustomOptionInterface::OPTION_GROUP_DATE:
$value = ['year' => 2013, 'month' => 8, 'day' => 9, 'hour' => 13, 'minute' => 35];
break;
case \Magento\Catalog\Api\Data\ProductCustomOptionInterface::OPTION_GROUP_SELECT:
case ProductCustomOptionInterface::OPTION_GROUP_SELECT:
$value = key($option->getValues());
break;
default:
Expand All @@ -111,6 +220,6 @@ private function prepareBuyRequest(Product $product)
$options[$option->getId()] = $value;
}

return new \Magento\Framework\DataObject(['qty' => 1, 'options' => $options]);
return $this->objectManager->create(DataObject::class, ['data' => ['qty' => 1, 'options' => $options]]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

require __DIR__ . '/product_simple.php';

$product = $productRepository->get('simple', false, null, true);
$tierPrices = $product->getTierPrices() ?? [];
$tierPriceExtensionAttributes = $tpExtensionAttributesFactory->create()->setWebsiteId($adminWebsite->getId());
$tierPrices[] = $tierPriceFactory->create(
[
'data' => [
'customer_group_id' => 1,
'qty' => 3,
'value' => 1
]
]
)->setExtensionAttributes($tierPriceExtensionAttributes);
$product->setTierPrices($tierPrices);
$productRepository->save($product);
Loading

0 comments on commit dfaee32

Please sign in to comment.