Skip to content

Commit

Permalink
Merge pull request #581 from magento-folks/bugfix
Browse files Browse the repository at this point in the history
[Folks] Bugs
  • Loading branch information
Alexander Akimov authored Nov 10, 2016
2 parents e643a67 + c67d8de commit b56df96
Show file tree
Hide file tree
Showing 31 changed files with 642 additions and 52 deletions.
31 changes: 11 additions & 20 deletions app/code/Magento/Checkout/Controller/Cart/CouponPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,26 +90,17 @@ public function execute()

if ($codeLength) {
$escaper = $this->_objectManager->get(\Magento\Framework\Escaper::class);
$coupon = $this->couponFactory->create();
$coupon->load($couponCode, 'code');
if (!$itemsCount) {
if ($isCodeLengthValid) {
$coupon = $this->couponFactory->create();
$coupon->load($couponCode, 'code');
if ($coupon->getId()) {
$this->_checkoutSession->getQuote()->setCouponCode($couponCode)->save();
$this->messageManager->addSuccess(
__(
'You used coupon code "%1".',
$escaper->escapeHtml($couponCode)
)
);
} else {
$this->messageManager->addError(
__(
'The coupon code "%1" is not valid.',
$escaper->escapeHtml($couponCode)
)
);
}
if ($isCodeLengthValid && $coupon->getId()) {
$this->_checkoutSession->getQuote()->setCouponCode($couponCode)->save();
$this->messageManager->addSuccess(
__(
'You used coupon code "%1".',
$escaper->escapeHtml($couponCode)
)
);
} else {
$this->messageManager->addError(
__(
Expand All @@ -119,7 +110,7 @@ public function execute()
);
}
} else {
if ($isCodeLengthValid && $couponCode == $cartQuote->getCouponCode()) {
if ($isCodeLengthValid && $coupon->getId() && $couponCode == $cartQuote->getCouponCode()) {
$this->messageManager->addSuccess(
__(
'You used coupon code "%1".',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,16 @@ class CouponPostTest extends \PHPUnit_Framework_TestCase
*/
protected $quoteRepository;

/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $redirect;

/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $redirectFactory;

/**
* @return void
*/
Expand Down Expand Up @@ -204,6 +214,12 @@ public function testExecuteWithGoodCouponAndItems()
->method('getCouponCode')
->willReturn('OLDCODE');

$coupon = $this->getMock(\Magento\SalesRule\Model\Coupon::class, [], [], '', false);
$this->couponFactory->expects($this->once())
->method('create')
->willReturn($coupon);
$coupon->expects($this->once())->method('load')->willReturnSelf();
$coupon->expects($this->once())->method('getId')->willReturn(1);
$this->quote->expects($this->any())
->method('getItemsCount')
->willReturn(1);
Expand Down
33 changes: 29 additions & 4 deletions app/code/Magento/Eav/Model/AttributeManagement.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@
*/
namespace Magento\Eav\Model;

use Magento\Framework\App\ObjectManager;
use Magento\Framework\Exception\InputException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Exception\StateException;

/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class AttributeManagement implements \Magento\Eav\Api\AttributeManagementInterface
{
/**
Expand All @@ -19,6 +23,7 @@ class AttributeManagement implements \Magento\Eav\Api\AttributeManagementInterfa

/**
* @var \Magento\Eav\Model\ResourceModel\Entity\Attribute\Collection
* @deprecated please use instead \Magento\Eav\Model\ResourceModel\Entity\Attribute\CollectionFactory
*/
protected $attributeCollection;

Expand Down Expand Up @@ -47,6 +52,11 @@ class AttributeManagement implements \Magento\Eav\Api\AttributeManagementInterfa
*/
protected $attributeResource;

/**
* @var \Magento\Eav\Model\ResourceModel\Entity\Attribute\CollectionFactory
*/
private $attributeCollectionFactory;

/**
* @param \Magento\Eav\Api\AttributeSetRepositoryInterface $setRepository
* @param \Magento\Eav\Model\ResourceModel\Entity\Attribute\Collection $attributeCollection
Expand Down Expand Up @@ -154,11 +164,26 @@ public function getAttributes($entityType, $attributeSetId)
if (!$attributeSet->getAttributeSetId() || $attributeSet->getEntityTypeId() != $requiredEntityTypeId) {
throw NoSuchEntityException::singleField('attributeSetId', $attributeSetId);
}

$attributeCollection = $this->attributeCollection
->setAttributeSetFilter($attributeSet->getAttributeSetId())
->load();
/** @var \Magento\Eav\Model\ResourceModel\Entity\Attribute\Collection $attributeCollection */
$attributeCollection = $this->getCollectionFactory()->create();
$attributeCollection->setAttributeSetFilter($attributeSet->getAttributeSetId())->load();

return $attributeCollection->getItems();
}

/**
* Retrieve collection factory
*
* @deprecated
* @return \Magento\Eav\Model\ResourceModel\Entity\Attribute\CollectionFactory
*/
private function getCollectionFactory()
{
if ($this->attributeCollectionFactory === null) {
$this->attributeCollectionFactory = ObjectManager::getInstance()->create(
\Magento\Eav\Model\ResourceModel\Entity\Attribute\CollectionFactory::class
);
}
return $this->attributeCollectionFactory;
}
}
18 changes: 18 additions & 0 deletions app/code/Magento/Eav/Test/Unit/Model/AttributeManagementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,24 @@ public function testGetAttributes()
$entityType = 'type';
$attributeSetId = 148;

$attributeCollectionFactoryMock = $this->getMock(
\Magento\Eav\Model\ResourceModel\Entity\Attribute\CollectionFactory::class,
['create'],
[],
'',
false
);
$attributeCollectionFactoryMock->expects($this->once())
->method('create')
->willReturn($this->attributeCollectionMock);

$objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
$objectManager->setBackwardCompatibleProperty(
$this->model,
'attributeCollectionFactory',
$attributeCollectionFactoryMock
);

$attributeSetMock = $this->getMock(\Magento\Eav\Api\Data\AttributeSetInterface::class, [], [], '', false);
$this->setRepositoryMock->expects($this->once())
->method('get')
Expand Down
2 changes: 1 addition & 1 deletion app/code/Magento/Sales/Model/Order/Payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -1289,7 +1289,7 @@ public function getAuthorizationTransaction()
*/
public function isCaptureFinal($amountToCapture)
{
$total = $this->getOrder()->getTotalDue();
$total = $this->getOrder()->getBaseTotalDue();
return $this->formatAmount($total, true) == $this->formatAmount($amountToCapture, true);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1548,7 +1548,7 @@ public function testIsCaptureFinal()
$partialAmount = 12.00;

$this->orderMock->expects(static::exactly(2))
->method('getTotalDue')
->method('getBaseTotalDue')
->willReturn($amount);

static::assertFalse($this->payment->isCaptureFinal($partialAmount));
Expand Down
30 changes: 29 additions & 1 deletion app/code/Magento/Swatches/Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ class Data
*/
protected $imageHelper;

/**
* Product metadata pool
*
* @var \Magento\Framework\EntityManager\MetadataPool
*/
private $metadataPool;

/**
* Data key which should populated to Attribute entity from "additional_data" field
*
Expand Down Expand Up @@ -196,7 +203,13 @@ public function loadVariationByFallback(Product $parentProduct, array $attribute
}

$productCollection = $this->productCollectionFactory->create();
$this->addFilterByParent($productCollection, $parentProduct->getId());

$productLinkedFiled = $this->getMetadataPool()
->getMetadata(\Magento\Catalog\Api\Data\ProductInterface::class)
->getLinkField();
$parentId = $parentProduct->getData($productLinkedFiled);

$this->addFilterByParent($productCollection, $parentId);

$configurableAttributes = $this->getAttributesFromConfigurable($parentProduct);
$allAttributesArray = [];
Expand Down Expand Up @@ -491,4 +504,19 @@ public function isTextSwatch(Attribute $attribute)
}
return $attribute->getData(Swatch::SWATCH_INPUT_TYPE_KEY) == Swatch::SWATCH_INPUT_TYPE_TEXT;
}

/**
* Get product metadata pool.
*
* @return \Magento\Framework\EntityManager\MetadataPool
* @deprecared
*/
protected function getMetadataPool()
{
if (!$this->metadataPool) {
$this->metadataPool = \Magento\Framework\App\ObjectManager::getInstance()
->get(\Magento\Framework\EntityManager\MetadataPool::class);
}
return $this->metadataPool;
}
}
22 changes: 20 additions & 2 deletions app/code/Magento/Swatches/Test/Unit/Helper/DataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ class DataTest extends \PHPUnit_Framework_TestCase
/** @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Catalog\Api\ProductRepositoryInterface */
protected $productRepoMock;

/** @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\EntityManager\MetadataPool*/
private $metaDataPoolMock;

protected function setUp()
{
$this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
Expand Down Expand Up @@ -108,7 +111,13 @@ protected function setUp()
'',
false
);

$this->metaDataPoolMock = $this->getMock(
\Magento\Framework\EntityManager\MetadataPool::class,
[],
[],
'',
false
);
$this->swatchHelperObject = $this->objectManager->getObject(
\Magento\Swatches\Helper\Data::class,
[
Expand All @@ -120,6 +129,11 @@ protected function setUp()
'imageHelper' => $this->imageHelperMock,
]
);
$this->objectManager->setBackwardCompatibleProperty(
$this->swatchHelperObject,
'metadataPool',
$this->metaDataPoolMock
);
}

public function dataForAdditionalData()
Expand Down Expand Up @@ -246,12 +260,16 @@ public function dataForVariationWithSwatchImage()
*/
public function testLoadVariationByFallback($product)
{
$metadataMock = $this->getMock(\Magento\Framework\EntityManager\EntityMetadataInterface::class);
$this->metaDataPoolMock->expects($this->once())->method('getMetadata')->willReturn($metadataMock);
$metadataMock->expects($this->once())->method('getLinkField')->willReturn('id');

$this->getSwatchAttributes($product);

$this->prepareVariationCollection();

$this->productCollectionMock->method('getFirstItem')->willReturn($this->productMock);
$this->productMock->method('getId')->willReturn(95);
$this->productMock->method('getData')->with('id')->willReturn(95);
$this->productModelFactoryMock->method('create')->willReturn($this->productMock);
$this->productMock->method('load')->with(95)->will($this->returnSelf());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
*/
namespace Magento\Widget\Model\ResourceModel\Widget\Instance\Options;

/**
* @deprecated created new class that correctly loads theme options and whose name follows naming convention
* @see \Magento\Widget\Model\ResourceModel\Widget\Instance\Options\Themes
*/
class ThemeId implements \Magento\Framework\Option\ArrayInterface
{
/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Widget\Model\ResourceModel\Widget\Instance\Options;

use Magento\Framework\Data\OptionSourceInterface;
use Magento\Theme\Model\ResourceModel\Theme\CollectionFactory as ThemeCollectionFactory;

/**
* Option source of the widget theme property.
*
* Can be used as a data provider for UI components that shows possible themes as a list.
*/
class Themes implements OptionSourceInterface
{
/**
* @var ThemeCollectionFactory
*/
private $themeCollectionFactory;

/**
* @param ThemeCollectionFactory $themeCollectionFactory
*/
public function __construct(ThemeCollectionFactory $themeCollectionFactory)
{
$this->themeCollectionFactory = $themeCollectionFactory;
}

/**
* Return array of options as value-label pairs
*
* @return array Format: array('<theme ID>' => '<theme label>', ...)
*/
public function toOptionArray()
{
// Load only visible themes that are used in frontend area
return $this->themeCollectionFactory->create()->loadRegisteredThemes()->toOptionHash();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Widget\Test\Unit\Model\ResourceModel\Widget\Instance\Options;

use Magento\Widget\Model\ResourceModel\Widget\Instance\Options\Themes;
use Magento\Theme\Model\ResourceModel\Theme\Collection as ThemeCollection;
use Magento\Theme\Model\ResourceModel\Theme\CollectionFactory as ThemeCollectionFactory;

/**
* Test class for \Magento\Widget\Model\ResourceModel\Widget\Instance\Options\Themes
*/
class ThemesTest extends \PHPUnit_Framework_TestCase
{
/**
* @var Themes
*/
private $model;

/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $themeCollectionFactoryMock;

/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $themeCollectionMock;

protected function setUp()
{
$this->themeCollectionMock = $this->getMock(ThemeCollection::class, [], [], '', false);
$this->themeCollectionFactoryMock = $this->getMock(ThemeCollectionFactory::class, ['create'], [], '', false);
$this->model = new Themes(
$this->themeCollectionFactoryMock
);
}

public function testToOptionArray()
{
$expectedResult = [
1 => 'Theme Label',
];
$this->themeCollectionFactoryMock->expects($this->once())
->method('create')
->willReturn($this->themeCollectionMock);

$this->themeCollectionMock->expects($this->once())->method('loadRegisteredThemes')->willReturnSelf();
$this->themeCollectionMock->expects($this->once())->method('toOptionHash')->willReturn($expectedResult);

$this->assertEquals($expectedResult, $this->model->toOptionArray());
}
}
Loading

0 comments on commit b56df96

Please sign in to comment.