Skip to content

Commit

Permalink
Merge pull request #352 from magento-nord/develop
Browse files Browse the repository at this point in the history
Bugs:
MAGETWO-56534 Price in cart does not update correctly when price is changed in admin. - for Mainline
MAGETWO-57135 Product import with images not working
MAGETWO-56054 [GitHub] Admin >New order: 'Recently Viewed Products' has 0 price value #5810
  • Loading branch information
VladimirZaets authored Sep 8, 2016
2 parents 25ee7e2 + 32ed255 commit c614b9c
Show file tree
Hide file tree
Showing 8 changed files with 215 additions and 48 deletions.
10 changes: 5 additions & 5 deletions app/code/Magento/CatalogImportExport/Model/Import/Uploader.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,8 @@ public function move($fileName, $renameFileOff = false)

$filePath = $this->_directory->getRelativePath($this->getTmpDir() . '/' . $fileName);
$this->_setUploadFile($filePath);
$result = $this->save($this->getDestDir());
$destDir = $this->_directory->getAbsolutePath($this->getDestDir());
$result = $this->save($destDir);
$result['name'] = self::getCorrectFileName($result['name']);
return $result;
}
Expand Down Expand Up @@ -305,11 +306,10 @@ protected function _moveFile($tmpPath, $destPath)
$tmpRealPath = $this->_directory->getDriver()->getRealPath(
$this->_directory->getAbsolutePath($tmpPath)
);
$destinationRealPath = $this->_directory->getDriver()->getRealPath(
$this->_directory->getAbsolutePath($destPath)
);
$destinationRealPath = $this->_directory->getDriver()->getRealPath($destPath);
$relativeDestPath = $this->_directory->getRelativePath($destPath);
$isSameFile = $tmpRealPath === $destinationRealPath;
return $isSameFile ?: $this->_directory->copyFile($tmpPath, $destPath);
return $isSameFile ?: $this->_directory->copyFile($tmpPath, $relativeDestPath);
} else {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,11 @@ protected function setUp()

$this->readFactory = $this->getMockBuilder(\Magento\Framework\Filesystem\File\ReadFactory::class)
->disableOriginalConstructor()
->setMethods(['create'])
->getMock();

$this->directoryMock = $this->getMockBuilder(\Magento\Framework\Filesystem\Directory\Writer::class)
->setMethods(['writeFile', 'getRelativePath'])
->setMethods(['writeFile', 'getRelativePath', 'isWritable', 'getAbsolutePath'])
->disableOriginalConstructor()
->getMock();

Expand All @@ -92,6 +93,7 @@ protected function setUp()
$this->filesystem,
$this->readFactory,
])
->setMethods(['_setUploadFile', 'save', 'getTmpDir'])
->getMock();
}

Expand All @@ -100,10 +102,14 @@ protected function setUp()
*/
public function testMoveFileUrl($fileUrl, $expectedHost, $expectedFileName)
{
$destDir = 'var/dest/dir';
$expectedRelativeFilePath = $this->uploader->getTmpDir() . '/' . $expectedFileName;
$this->directoryMock->expects($this->once())->method('isWritable')->with($destDir)->willReturn(true);
$this->directoryMock->expects($this->any())->method('getRelativePath')->with($expectedRelativeFilePath);
$this->directoryMock->expects($this->once())->method('getAbsolutePath')->with($destDir)
->willReturn($destDir . '/' . $expectedFileName);
// Check writeFile() method invoking.
$this->directoryMock->expects($this->any())->method('writeFile')->will($this->returnValue(null));
$this->directoryMock->expects($this->any())->method('writeFile')->will($this->returnValue($expectedFileName));

// Create adjusted reader which does not validate path.
$readMock = $this->getMockBuilder(\Magento\Framework\Filesystem\File\Read::class)
Expand All @@ -113,59 +119,37 @@ public function testMoveFileUrl($fileUrl, $expectedHost, $expectedFileName)
// Check readAll() method invoking.
$readMock->expects($this->once())->method('readAll')->will($this->returnValue(null));

$this->readFactory = $this->getMockBuilder(\Magento\Framework\Filesystem\File\ReadFactory::class)
->disableOriginalConstructor()
->setMethods(['create'])
->getMock();
// Check create() method invoking with expected argument.
$this->readFactory->expects($this->once())
->method('create')
->will($this->returnValue($readMock))->with($expectedHost);

$uploaderMock = $this->getMockBuilder(\Magento\CatalogImportExport\Model\Import\Uploader::class)
->setConstructorArgs([
$this->coreFileStorageDb,
$this->coreFileStorage,
$this->imageFactory,
$this->validator,
$this->filesystem,
$this->readFactory,
])
->setMethods(['_setUploadFile', 'save', 'getTmpDir'])
->getMock();

//Check invoking of getTmpDir(), _setUploadFile(), save() methods.
$uploaderMock->expects($this->any())->method('getTmpDir')->will($this->returnValue(''));
$uploaderMock->expects($this->once())->method('_setUploadFile')->will($this->returnSelf());
$uploaderMock->expects($this->once())->method('save')->will($this->returnValue(['name' => null]));
$this->uploader->expects($this->any())->method('getTmpDir')->will($this->returnValue(''));
$this->uploader->expects($this->once())->method('_setUploadFile')->will($this->returnSelf());
$this->uploader->expects($this->once())->method('save')->with($destDir . '/' . $expectedFileName)
->willReturn(['name' => $expectedFileName]);

$uploaderMock->move($fileUrl);
$this->uploader->setDestDir($destDir);
$this->assertEquals(['name' => $expectedFileName], $this->uploader->move($fileUrl));
}

public function testMoveFileName()
{
$destDir = 'var/dest/dir';
$fileName = 'test_uploader_file';
$expectedRelativeFilePath = $this->uploader->getTmpDir() . '/' . $fileName;
$this->directoryMock->expects($this->once())->method('isWritable')->with($destDir)->willReturn(true);
$this->directoryMock->expects($this->any())->method('getRelativePath')->with($expectedRelativeFilePath);

$uploaderMock = $this->getMockBuilder(\Magento\CatalogImportExport\Model\Import\Uploader::class)
->setConstructorArgs([
$this->coreFileStorageDb,
$this->coreFileStorage,
$this->imageFactory,
$this->validator,
$this->filesystem,
$this->readFactory,
])
->setMethods(['_setUploadFile', 'save', 'getTmpDir'])
->getMock();

$this->directoryMock->expects($this->once())->method('getAbsolutePath')->with($destDir)
->willReturn($destDir . '/' . $fileName);
//Check invoking of getTmpDir(), _setUploadFile(), save() methods.
$uploaderMock->expects($this->once())->method('getTmpDir')->will($this->returnValue(''));
$uploaderMock->expects($this->once())->method('_setUploadFile')->will($this->returnSelf());
$uploaderMock->expects($this->once())->method('save')->will($this->returnValue(['name' => null]));
$this->uploader->expects($this->once())->method('getTmpDir')->will($this->returnValue(''));
$this->uploader->expects($this->once())->method('_setUploadFile')->will($this->returnSelf());
$this->uploader->expects($this->once())->method('save')->with($destDir . '/' . $fileName)
->willReturn(['name' => $fileName]);

$uploaderMock->move($fileName);
$this->uploader->setDestDir($destDir);
$this->assertEquals(['name' => $fileName], $this->uploader->move($fileName));
}

public function moveFileUrlDataProvider()
Expand Down
42 changes: 42 additions & 0 deletions app/code/Magento/Quote/Model/Product/Plugin/UpdateQuoteItems.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Quote\Model\Product\Plugin;

class UpdateQuoteItems
{
/**
* @var \Magento\Quote\Model\ResourceModel\Quote
*/
private $resource;

/**
* @param \Magento\Quote\Model\ResourceModel\Quote $resource
*/
public function __construct(
\Magento\Quote\Model\ResourceModel\Quote $resource
) {
$this->resource = $resource;
}

/**
* @param \Magento\Catalog\Model\ResourceModel\Product $subject
* @param \Magento\Catalog\Model\ResourceModel\Product $result
* @param \Magento\Framework\Model\AbstractModel $product
* @return \Magento\Catalog\Model\ResourceModel\Product
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function afterSave(
\Magento\Catalog\Model\ResourceModel\Product $subject,
\Magento\Catalog\Model\ResourceModel\Product $result,
\Magento\Framework\Model\AbstractModel $product
) {
$originalPrice = $product->getOrigData('price');
if (!empty($originalPrice) && ($originalPrice != $product->getPrice())) {
$this->resource->markQuotesRecollect($product->getId());
}
return $result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Quote\Test\Unit\Model\Product\Plugin;

class UpdateQuoteItemsTest extends \PHPUnit_Framework_TestCase
{
/**
* @var \Magento\Quote\Model\Product\Plugin\UpdateQuoteItems
*/
private $model;

/**
* @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Quote\Model\ResourceModel\Quote
*/
private $quoteResource ;

protected function setUp()
{
$this->quoteResource = $this->getMockBuilder(\Magento\Quote\Model\ResourceModel\Quote::class)
->disableOriginalConstructor()
->getMock();
$this->model = new \Magento\Quote\Model\Product\Plugin\UpdateQuoteItems($this->quoteResource);
}

/**
* @dataProvider aroundUpdateDataProvider
* @param int $originalPrice
* @param int $newPrice
* @param bool $callMethod
*/
public function testAfterUpdate($originalPrice, $newPrice, $callMethod)
{
$productResourceMock = $this->getMock(\Magento\Catalog\Model\ResourceModel\Product::class, [], [], '', false);
$productMock = $this->getMockBuilder(\Magento\Framework\Model\AbstractModel::class)
->disableOriginalConstructor()
->setMethods(['getOrigData', 'getPrice', 'getId'])
->getMockForAbstractClass();
$productId = 1;
$productMock->expects($this->any())->method('getOrigData')->with('price')->willReturn($originalPrice);
$productMock->expects($this->any())->method('getPrice')->willReturn($newPrice);
$productMock->expects($this->any())->method('getId')->willReturn($productId);
$this->quoteResource->expects($this->$callMethod())->method('markQuotesRecollect')->with($productId);
$result = $this->model->afterSave($productResourceMock, $productResourceMock, $productMock);
$this->assertEquals($result, $productResourceMock);
}

public function aroundUpdateDataProvider()
{
return [
[10, 20, 'once'],
[null, 10, 'never'],
[10, 10, 'never']
];
}
}
1 change: 1 addition & 0 deletions app/code/Magento/Quote/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,6 @@
<preference for="Magento\Quote\Model\Product\QuoteItemsCleanerInterface" type="Magento\Quote\Model\Product\QuoteItemsCleaner" />
<type name="Magento\Catalog\Model\ResourceModel\Product">
<plugin name="clean_quote_items_after_product_delete" type="Magento\Quote\Model\Product\Plugin\RemoveQuoteItems"/>
<plugin name="update_quote_items_after_product_save" type="Magento\Quote\Model\Product\Plugin\UpdateQuoteItems"/>
</type>
</config>
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
namespace Magento\Sales\Block\Adminhtml\Order\Create;

use Magento\Framework\Pricing\PriceCurrencyInterface;
use Magento\Catalog\Model\Product;
use Magento\Catalog\Pricing\Price\FinalPrice;

/**
* Adminhtml sales order create abstract block
Expand Down Expand Up @@ -130,12 +132,22 @@ public function formatPrice($value)
);
}

/**
* @param Product $product
* @return string
*/
public function getItemPrice(Product $product)
{
$price = $product->getPriceInfo()->getPrice(FinalPrice::PRICE_CODE)->getValue();
return $this->convertPrice($price);
}

/**
* Convert price
*
* @param float $value
* @param int|float $value
* @param bool $format
* @return float
* @return string|int|float
*/
public function convertPrice($value, $format = true)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Sales\Test\Unit\Block\Adminhtml\Order\Create;

use Magento\Catalog\Pricing\Price\FinalPrice;

class AbstractCreateTest extends \PHPUnit_Framework_TestCase
{
/**
* @var \Magento\Sales\Block\Adminhtml\Order\Create\AbstractCreate|\PHPUnit_Framework_MockObject_MockObject
*/
protected $model;

/**
* @var \Magento\Catalog\Model\Product|\PHPUnit_Framework_MockObject_MockObject
*/
protected $productMock;

/**
* @var \Magento\Framework\Pricing\PriceInfo\Base|\PHPUnit_Framework_MockObject_MockObject
*/
protected $priceInfoMock;

/**
* @var \Magento\Downloadable\Pricing\Price\LinkPrice|\PHPUnit_Framework_MockObject_MockObject
*/
protected $linkPriceMock;

protected function setUp()
{
$this->model = $this->getMockBuilder(\Magento\Sales\Block\Adminhtml\Order\Create\AbstractCreate::class)
->setMethods(['convertPrice'])
->disableOriginalConstructor()
->getMockForAbstractClass();
$this->priceInfoMock = $this->getMockBuilder(\Magento\Framework\Pricing\PriceInfo\Base::class)
->disableOriginalConstructor()
->getMock();
$this->productMock = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
->disableOriginalConstructor()
->getMock();
$this->linkPriceMock = $this->getMockBuilder(\Magento\Downloadable\Pricing\Price\LinkPrice::class)
->disableOriginalConstructor()
->getMock();
$this->productMock->expects($this->any())
->method('getPriceInfo')
->willReturn($this->priceInfoMock);
}

public function testGetItemPrice()
{
$price = 5.6;
$resultPrice = 9.3;

$this->linkPriceMock->expects($this->once())
->method('getValue')
->willReturn($price);
$this->priceInfoMock->expects($this->once())
->method('getPrice')
->with(FinalPrice::PRICE_CODE)
->willReturn($this->linkPriceMock);
$this->model->expects($this->once())
->method('convertPrice')
->with($price)
->willReturn($resultPrice);
$this->assertEquals($resultPrice, $this->model->getItemPrice($this->productMock));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
<?php endif; ?>

<?php if ($block->canDisplayPrice()): ?>
<td class="col-price"><?php /* @escapeNotVerified */ echo $block->convertPrice($_item->getPrice()) ?></td>
<td class="col-price"><?php /* @noEscape */ echo $block->getItemPrice($_item) ?></td>
<?php endif; ?>

<?php if ($block->canRemoveItems()): ?>
Expand Down

0 comments on commit c614b9c

Please sign in to comment.