Skip to content

Commit c9214e9

Browse files
committed
Merge remote-tracking branch 'origin/2.1-develop' into 2.1-develop-pr35
2 parents a4603d2 + b78362f commit c9214e9

File tree

48 files changed

+1659
-184
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+1659
-184
lines changed

app/code/Magento/Bundle/Test/Unit/Pricing/Price/TierPriceTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ public function providerForGetterTierPriceList()
183183
*/
184184
public function testGetSavePercent($baseAmount, $savePercent)
185185
{
186+
/** @var \Magento\Framework\Pricing\Amount\AmountInterface|\PHPUnit_Framework_MockObject_MockObject $amount */
186187
$amount = $this->getMockForAbstractClass('Magento\Framework\Pricing\Amount\AmountInterface');
187188
$amount->expects($this->once())->method('getBaseAmount')->willReturn($baseAmount);
188189

app/code/Magento/Catalog/Model/Category.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,15 +1127,23 @@ public function afterDeleteCommit()
11271127
*/
11281128
public function getIdentities()
11291129
{
1130-
$identities = [
1131-
self::CACHE_TAG . '_' . $this->getId(),
1132-
];
1133-
if (!$this->getId() || $this->hasDataChanges()
1134-
|| $this->isDeleted() || $this->dataHasChangedFor(self::KEY_INCLUDE_IN_MENU)
1130+
$identities = [];
1131+
if ($this->getId()) {
1132+
$identities[] = self::CACHE_TAG . '_' . $this->getId();
1133+
}
1134+
if ($this->getId()
1135+
&& (
1136+
$this->hasDataChanges()
1137+
|| $this->isDeleted()
1138+
|| $this->dataHasChangedFor(self::KEY_INCLUDE_IN_MENU)
1139+
)
11351140
) {
1136-
$identities[] = self::CACHE_TAG;
11371141
$identities[] = Product::CACHE_PRODUCT_CATEGORY_TAG . '_' . $this->getId();
11381142
}
1143+
if ($this->getId() && $this->isObjectNew()) {
1144+
$identities[] = self::CACHE_TAG;
1145+
}
1146+
11391147
return $identities;
11401148
}
11411149

app/code/Magento/Catalog/Model/Product.php

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,7 +1058,11 @@ protected function _afterLoad()
10581058
*/
10591059
public function cleanCache()
10601060
{
1061-
$this->_cacheManager->clean('catalog_product_' . $this->getId());
1061+
if ($this->getId()) {
1062+
$this->_cacheManager->clean(
1063+
self::CACHE_TAG . '_' . $this->getId()
1064+
);
1065+
}
10621066
return $this;
10631067
}
10641068

@@ -2278,13 +2282,16 @@ public function getImage()
22782282
*/
22792283
public function getIdentities()
22802284
{
2281-
$identities = [self::CACHE_TAG . '_' . $this->getId()];
2285+
$identities = [];
2286+
2287+
if ($this->getId()) {
2288+
$identities[] = self::CACHE_TAG . '_' . $this->getId();
2289+
}
22822290
if ($this->getIsChangedCategories()) {
22832291
foreach ($this->getAffectedCategoryIds() as $categoryId) {
22842292
$identities[] = self::CACHE_PRODUCT_CATEGORY_TAG . '_' . $categoryId;
22852293
}
22862294
}
2287-
22882295
if (($this->getOrigData('status') != $this->getData('status')) || $this->isStockStatusChanged()) {
22892296
foreach ($this->getCategoryIds() as $categoryId) {
22902297
$identities[] = self::CACHE_PRODUCT_CATEGORY_TAG . '_' . $categoryId;
@@ -2634,4 +2641,18 @@ public function setAssociatedProductIds(array $productIds)
26342641
{
26352642
$this->getExtensionAttributes()->setConfigurableProductLinks($productIds);
26362643
}
2644+
2645+
/**
2646+
* {@inheritDoc}
2647+
*/
2648+
public function getCacheTags()
2649+
{
2650+
//Preferring individual tags over broad ones.
2651+
$individualTags = $this->getIdentities();
2652+
if ($individualTags) {
2653+
return $individualTags;
2654+
}
2655+
2656+
return parent::getCacheTags();
2657+
}
26372658
}

app/code/Magento/Catalog/Pricing/Price/TierPrice.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,14 +203,20 @@ protected function getBasePrice()
203203
}
204204

205205
/**
206+
* Calculates savings percentage according to the given tier price amount
207+
* and related product price amount.
208+
*
206209
* @param AmountInterface $amount
207210
* @return float
208211
*/
209212
public function getSavePercent(AmountInterface $amount)
210213
{
211-
return ceil(
212-
100 - ((100 / $this->priceInfo->getPrice(FinalPrice::PRICE_CODE)->getValue())
213-
* $amount->getBaseAmount())
214+
$productPriceAmount = $this->priceInfo->getPrice(
215+
FinalPrice::PRICE_CODE
216+
)->getAmount();
217+
218+
return round(
219+
100 - ((100 / $productPriceAmount->getValue()) * $amount->getValue())
214220
);
215221
}
216222

app/code/Magento/Catalog/Test/Unit/Model/CategoryTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -640,4 +640,19 @@ public function testGetImageWithoutAttributeCode()
640640
$result = $model->getImageUrl();
641641
$this->assertEquals('http://www.example.com/catalog/category/myimage', $result);
642642
}
643+
644+
/**
645+
* @return void
646+
*/
647+
public function testGetIdentities()
648+
{
649+
$category = $this->getCategoryModel();
650+
651+
//Without an ID no identities can be given.
652+
$this->assertEmpty($category->getIdentities());
653+
654+
//Now because ID is set we can get some
655+
$category->setId(42);
656+
$this->assertNotEmpty($category->getIdentities());
657+
}
643658
}

app/code/Magento/Catalog/Test/Unit/Model/ProductTest.php

Lines changed: 69 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Magento\Catalog\Model\Product;
1212
use Magento\Framework\Api\Data\ImageContentInterface;
1313
use Magento\Framework\Api\ExtensibleDataInterface;
14+
use Magento\Framework\App\CacheInterface;
1415
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
1516
use Magento\Catalog\Model\Product\Attribute\Source\Status as Status;
1617

@@ -172,6 +173,11 @@ class ProductTest extends \PHPUnit_Framework_TestCase
172173
*/
173174
private $appStateMock;
174175

176+
/**
177+
* @var CacheInterface|\PHPUnit_Framework_MockObject_MockObject
178+
*/
179+
private $cacheManagerMock;
180+
175181
/**
176182
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
177183
*/
@@ -234,7 +240,8 @@ protected function setUp()
234240
false
235241
);
236242
$actionValidatorMock->expects($this->any())->method('isAllowed')->will($this->returnValue(true));
237-
$cacheInterfaceMock = $this->getMock('Magento\Framework\App\CacheInterface');
243+
$this->cacheManagerMock
244+
= $this->getMock('Magento\Framework\App\CacheInterface');
238245

239246
$contextMock = $this->getMock(
240247
'\Magento\Framework\Model\Context',
@@ -246,7 +253,7 @@ protected function setUp()
246253
->will($this->returnValue($this->eventManagerMock));
247254
$contextMock->expects($this->any())
248255
->method('getCacheManager')
249-
->will($this->returnValue($cacheInterfaceMock));
256+
->will($this->returnValue($this->cacheManagerMock));
250257
$contextMock->expects($this->any())
251258
->method('getActionValidator')
252259
->will($this->returnValue($actionValidatorMock));
@@ -644,11 +651,21 @@ public function testGetIdentities($expected, $origData, $data, $isDeleted = fals
644651
$this->model->setIdFieldName('id');
645652
if (is_array($origData)) {
646653
foreach ($origData as $key => $value) {
647-
$this->model->setOrigData($key, $value);
654+
//Because ID's not changing if you just use setOrigData
655+
if ($key === 'id') {
656+
$this->model->setId($value);
657+
} else {
658+
$this->model->setOrigData($key, $value);
659+
}
648660
}
649661
}
650662
foreach ($data as $key => $value) {
651-
$this->model->setData($key, $value);
663+
//Because ID's not changing if you just use setData
664+
if ($key === 'id') {
665+
$this->model->setId($value);
666+
} else {
667+
$this->model->setData($key, $value);
668+
}
652669
}
653670
$this->model->isDeleted($isDeleted);
654671
$this->assertEquals($expected, $this->model->getIdentities());
@@ -737,7 +754,7 @@ public function getIdentitiesProvider()
737754
ExtensibleDataInterface::EXTENSION_ATTRIBUTES_KEY => $extensionAttributesMock,
738755
],
739756
],
740-
'no stock status data 1' => [
757+
'no stock status data 1' => [
741758
[0 => 'catalog_product_1'],
742759
['id' => 1, 'name' => 'value', 'category_ids' => [1], 'status' => 1],
743760
[
@@ -748,7 +765,7 @@ public function getIdentitiesProvider()
748765
ExtensibleDataInterface::EXTENSION_ATTRIBUTES_KEY => $extensionAttributesMock,
749766
],
750767
],
751-
'no stock status data 2' => [
768+
'no stock status data 2' => [
752769
[0 => 'catalog_product_1'],
753770
['id' => 1, 'name' => 'value', 'category_ids' => [1], 'status' => 1],
754771
[
@@ -759,7 +776,24 @@ public function getIdentitiesProvider()
759776
'stock_data' => ['is_in_stock' => true],
760777
],
761778
],
762-
'stock status changes' => [
779+
'new product with ID not set yet' => [
780+
[0 => 'catalog_category_product_1'],
781+
[
782+
'id' => null,
783+
'name' => 'value',
784+
'category_ids' => [1],
785+
'status' => null,
786+
],
787+
[
788+
'id' => null,
789+
'name' => 'value',
790+
'category_ids' => [1],
791+
'status' => 1,
792+
'affected_category_ids' => [1],
793+
'is_changed_categories' => true,
794+
],
795+
],
796+
'stock status changes' => [
763797
[0 => 'catalog_product_1', 1 => 'catalog_category_product_1'],
764798
['id' => 1, 'name' => 'value', 'category_ids' => [1], 'status' => 1],
765799
[
@@ -1356,4 +1390,32 @@ public function testGetOptionByIdForProductWithoutOptions()
13561390
{
13571391
$this->assertNull($this->model->getOptionById(100));
13581392
}
1393+
1394+
public function testCleanCache()
1395+
{
1396+
//Without an ID cleanCache won't clean anything because the entity is
1397+
//not identified and it will be called later exactly once.
1398+
$this->model->setId(null);
1399+
$this->cacheManagerMock
1400+
->expects($this->once())
1401+
->method('clean');
1402+
$this->model->cleanCache();
1403+
1404+
//Now that ID is set clean will be called.
1405+
$this->model->setId(1);
1406+
$this->model->cleanCache();
1407+
}
1408+
1409+
public function testGetCacheTags()
1410+
{
1411+
//If entity is identified getCacheTags has to return the same values
1412+
//as getIdentities
1413+
$this->model->setId(null);
1414+
$this->assertEquals([Product::CACHE_TAG], $this->model->getCacheTags());
1415+
$this->model->setId(1);
1416+
$this->assertEquals(
1417+
$this->model->getIdentities(),
1418+
$this->model->getCacheTags()
1419+
);
1420+
}
13591421
}

app/code/Magento/Catalog/Test/Unit/Pricing/Price/TierPriceTest.php

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
use Magento\Catalog\Pricing\Price\TierPrice;
1212
use Magento\Catalog\Pricing\Price\FinalPrice;
13+
use Magento\Framework\Pricing\Amount\AmountInterface;
14+
use Magento\Framework\Pricing\Price\PriceInterface;
1315
use Magento\Customer\Model\Group;
1416
use Magento\Customer\Model\GroupManagement;
1517

@@ -361,27 +363,37 @@ public function providerForGetterTierPriceList()
361363
}
362364

363365
/**
364-
* @covers \Magento\Catalog\Pricing\Price\TierPrice::__construct
365-
* @covers \Magento\Catalog\Pricing\Price\TierPrice::getSavePercent
366-
* @covers \Magento\Catalog\Pricing\Price\TierPrice::getBasePrice
366+
* @param float $basePrice
367+
* @param float $tierPrice
368+
* @param float $savedPercent
369+
*
367370
* @dataProvider dataProviderGetSavePercent
368371
*/
369372
public function testGetSavePercent($basePrice, $tierPrice, $savedPercent)
370373
{
371-
$price = $this->getMock('Magento\Framework\Pricing\Price\PriceInterface');
374+
/** @var AmountInterface|\PHPUnit_Framework_MockObject_MockObject $amount */
375+
$amount = $this->getMockForAbstractClass(AmountInterface::class);
372376

373-
$this->priceInfo->expects(static::atLeastOnce())
374-
->method('getPrice')
375-
->with(FinalPrice::PRICE_CODE)
376-
->willReturn($price);
377-
$price->expects(static::atLeastOnce())
377+
$amount->expects($this->any())
378+
->method('getValue')
379+
->willReturn($tierPrice);
380+
381+
$basePriceAmount = $this->getMockForAbstractClass(AmountInterface::class);
382+
383+
$basePriceAmount->expects($this->any())
378384
->method('getValue')
379385
->willReturn($basePrice);
380386

381-
$amount = $this->getMockForAbstractClass('Magento\Framework\Pricing\Amount\AmountInterface');
382-
$amount->expects($this->atLeastOnce())
383-
->method('getBaseAmount')
384-
->will($this->returnValue($tierPrice));
387+
$price = $this->getMockForAbstractClass(PriceInterface::class);
388+
389+
$price->expects($this->any())
390+
->method('getAmount')
391+
->willReturn($basePriceAmount);
392+
393+
$this->priceInfo->expects($this->any())
394+
->method('getPrice')
395+
->with(FinalPrice::PRICE_CODE)
396+
->willReturn($price);
385397

386398
$this->assertEquals($savedPercent, $this->model->getSavePercent($amount));
387399
}

0 commit comments

Comments
 (0)