Skip to content

Commit

Permalink
Merge pull request #3124 from magento-tsg-csl3/2.2-develop-pr1
Browse files Browse the repository at this point in the history
[TSG-CSL3] Backporting 2.2 (pr1)
  • Loading branch information
viktym authored Sep 6, 2018
2 parents 679bdb5 + 4e3a4a9 commit b4b7538
Show file tree
Hide file tree
Showing 10 changed files with 290 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@
use Magento\CatalogInventory\Model\Stock;
use Magento\Framework\Event\Observer;
use Magento\Framework\Exception\LocalizedException;
use Magento\Quote\Model\Quote\Item;

/**
* @api
* @since 100.0.2
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class QuantityValidator
{
Expand Down Expand Up @@ -66,7 +68,7 @@ public function __construct(
* Add error information to Quote Item
*
* @param \Magento\Framework\DataObject $result
* @param \Magento\Quote\Model\Quote\Item $quoteItem
* @param Item $quoteItem
* @param bool $removeError
* @return void
*/
Expand Down Expand Up @@ -99,7 +101,7 @@ private function addErrorInfoToQuote($result, $quoteItem)
*/
public function validate(Observer $observer)
{
/* @var $quoteItem \Magento\Quote\Model\Quote\Item */
/* @var $quoteItem Item */
$quoteItem = $observer->getEvent()->getItem();
if (!$quoteItem ||
!$quoteItem->getProductId() ||
Expand Down Expand Up @@ -174,31 +176,7 @@ public function validate(Observer $observer)
$qty = $product->getTypeInstance()->prepareQuoteItemQty($qty, $product);
$quoteItem->setData('qty', $qty);
if ($stockStatus) {
$result = $this->stockState->checkQtyIncrements(
$product->getId(),
$qty,
$product->getStore()->getWebsiteId()
);
if ($result->getHasError()) {
$quoteItem->addErrorInfo(
'cataloginventory',
Data::ERROR_QTY_INCREMENTS,
$result->getMessage()
);

$quoteItem->getQuote()->addErrorInfo(
$result->getQuoteMessageIndex(),
'cataloginventory',
Data::ERROR_QTY_INCREMENTS,
$result->getQuoteMessage()
);
} else {
// Delete error from item and its quote, if it was set due to qty problems
$this->_removeErrorsFromQuoteAndItem(
$quoteItem,
Data::ERROR_QTY_INCREMENTS
);
}
$this->checkOptionsQtyIncrements($quoteItem, $options);
}
// variable to keep track if we have previously encountered an error in one of the options
$removeError = true;
Expand Down Expand Up @@ -227,10 +205,44 @@ public function validate(Observer $observer)
}
}

/**
* Verifies product options quantity increments.
*
* @param Item $quoteItem
* @param array $options
* @return void
*/
private function checkOptionsQtyIncrements(Item $quoteItem, array $options)
{
$removeErrors = true;
foreach ($options as $option) {
$result = $this->stockState->checkQtyIncrements(
$option->getProduct()->getId(),
$quoteItem->getData('qty'),
$option->getProduct()->getStore()->getWebsiteId()
);
if ($result->getHasError()) {
$quoteItem->getQuote()->addErrorInfo(
$result->getQuoteMessageIndex(),
'cataloginventory',
Data::ERROR_QTY_INCREMENTS,
$result->getQuoteMessage()
);

$removeErrors = false;
}
}

if ($removeErrors) {
// Delete error from item and its quote, if it was set due to qty problems
$this->_removeErrorsFromQuoteAndItem($quoteItem, Data::ERROR_QTY_INCREMENTS);
}
}

/**
* Removes error statuses from quote and item, set by this observer
*
* @param \Magento\Quote\Model\Quote\Item $item
* @param Item $item
* @param int $code
* @return void
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
use Magento\CatalogInventory\Api\StockStateInterface;
use Magento\CatalogInventory\Model\Quote\Item\QuantityValidator\QuoteItemQtyList;

/**
* Class for initialize quote item options.
*/
class Option
{
/**
Expand Down Expand Up @@ -67,10 +70,6 @@ public function getStockItem(
* define that stock item is child for composite product
*/
$stockItem->setIsChildItem(true);
/**
* don't check qty increments value for option product
*/
$stockItem->setSuppressCheckQtyIncrements(true);

return $stockItem;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
*/
namespace Magento\CatalogInventory\Test\Unit\Model\Quote\Item\QuantityValidator\Initializer;

/**
* Class OptionTest
*/
class OptionTest extends \PHPUnit\Framework\TestCase
{
/**
Expand Down Expand Up @@ -67,6 +70,9 @@ class OptionTest extends \PHPUnit\Framework\TestCase
*/
protected $websiteId = 111;

/**
* @inheritdoc
*/
protected function setUp()
{
$optionMethods = [
Expand Down Expand Up @@ -140,6 +146,9 @@ protected function setUp()
);
}

/**
* @return void
*/
public function testInitializeWhenResultIsDecimalGetBackordersMessageHasOptionQtyUpdate()
{
$optionValue = 5;
Expand All @@ -151,7 +160,6 @@ public function testInitializeWhenResultIsDecimalGetBackordersMessageHasOptionQt
$this->optionMock->expects($this->any())->method('getProduct')->will($this->returnValue($this->productMock));

$this->stockItemMock->expects($this->once())->method('setIsChildItem')->with(true);
$this->stockItemMock->expects($this->once())->method('setSuppressCheckQtyIncrements')->with(true);
$this->stockItemMock->expects($this->once())->method('getItemId')->will($this->returnValue(true));

$this->stockRegistry
Expand Down Expand Up @@ -212,6 +220,9 @@ public function testInitializeWhenResultIsDecimalGetBackordersMessageHasOptionQt
$this->validator->initialize($this->optionMock, $this->quoteItemMock, $qty);
}

/**
* @return void
*/
public function testInitializeWhenResultNotDecimalGetBackordersMessageHasOptionQtyUpdate()
{
$optionValue = 5;
Expand All @@ -222,7 +233,6 @@ public function testInitializeWhenResultNotDecimalGetBackordersMessageHasOptionQ
$this->optionMock->expects($this->any())->method('getProduct')->will($this->returnValue($this->productMock));

$this->stockItemMock->expects($this->once())->method('setIsChildItem')->with(true);
$this->stockItemMock->expects($this->once())->method('setSuppressCheckQtyIncrements')->with(true);
$this->stockItemMock->expects($this->once())->method('getItemId')->will($this->returnValue(true));

$this->stockRegistry
Expand Down Expand Up @@ -267,6 +277,8 @@ public function testInitializeWhenResultNotDecimalGetBackordersMessageHasOptionQ
}

/**
* @return void
*
* @expectedException \Magento\Framework\Exception\LocalizedException
* @expectedExceptionMessage The stock item for Product in option is not valid.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ class QuantityValidatorTest extends \PHPUnit\Framework\TestCase
*/
private $stockStatusMock;

/**
* @inheritdoc
*/
protected function setUp()
{
$objectManagerHelper = new ObjectManager($this);
Expand Down Expand Up @@ -278,11 +281,13 @@ public function testValidateWithOptions()
{
$optionMock = $this->getMockBuilder(OptionItem::class)
->disableOriginalConstructor()
->setMethods(['setHasError', 'getStockStateResult'])
->setMethods(['setHasError', 'getStockStateResult', 'getProduct'])
->getMock();
$optionMock->expects($this->once())
->method('getStockStateResult')
->willReturn($this->resultMock);
$optionMock->method('getProduct')
->willReturn($this->productMock);
$this->stockRegistryMock->expects($this->at(0))
->method('getStockItem')
->willReturn($this->stockItemMock);
Expand Down Expand Up @@ -319,7 +324,7 @@ public function testValidateWithOptionsAndError()
{
$optionMock = $this->getMockBuilder(OptionItem::class)
->disableOriginalConstructor()
->setMethods(['setHasError', 'getStockStateResult'])
->setMethods(['setHasError', 'getStockStateResult', 'getProduct'])
->getMock();
$this->stockRegistryMock->expects($this->at(0))
->method('getStockItem')
Expand All @@ -330,6 +335,8 @@ public function testValidateWithOptionsAndError()
$optionMock->expects($this->once())
->method('getStockStateResult')
->willReturn($this->resultMock);
$optionMock->method('getProduct')
->willReturn($this->productMock);
$options = [$optionMock];
$this->createInitialStub(1);
$this->setUpStubForQuantity(1, true);
Expand Down Expand Up @@ -360,7 +367,7 @@ public function testValidateAndRemoveErrorsFromQuote()
{
$optionMock = $this->getMockBuilder(OptionItem::class)
->disableOriginalConstructor()
->setMethods(['setHasError', 'getStockStateResult'])
->setMethods(['setHasError', 'getStockStateResult', 'getProduct'])
->getMock();
$quoteItem = $this->getMockBuilder(Item::class)
->disableOriginalConstructor()
Expand All @@ -369,6 +376,8 @@ public function testValidateAndRemoveErrorsFromQuote()
$optionMock->expects($this->once())
->method('getStockStateResult')
->willReturn($this->resultMock);
$optionMock->method('getProduct')
->willReturn($this->productMock);
$this->stockRegistryMock->expects($this->at(0))
->method('getStockItem')
->willReturn($this->stockItemMock);
Expand Down Expand Up @@ -544,6 +553,9 @@ private function createInitialStub($qty)
->willReturn($this->resultMock);
}

/**
* @return void
*/
private function setUpStubForRemoveError()
{
$quoteItems = [$this->quoteItemMock];
Expand Down
4 changes: 2 additions & 2 deletions app/code/Magento/Reports/Block/Adminhtml/Grid.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ protected function _prepareCollection()
* Validate from and to date
*/
try {
$from = $this->_localeDate->scopeDate(null, $this->getFilter('report_from'), false);
$to = $this->_localeDate->scopeDate(null, $this->getFilter('report_to'), false);
$from = $this->_localeDate->date($this->getFilter('report_from'), null, false, false);
$to = $this->_localeDate->date($this->getFilter('report_to'), null, false, false);

$collection->setInterval($from, $to);
} catch (\Exception $e) {
Expand Down

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Loading

0 comments on commit b4b7538

Please sign in to comment.