Skip to content

Commit 63eeefa

Browse files
author
Oleksii Korshenko
authored
Merge pull request magento#151 from magento-fearless-kiwis/FearlessKiwis-MAGETWO-53130-Category-shows-old-price-on-price-index-scheduled
[Fearless Kiwis] Bug fix. Category shows old price on price index scheduled
2 parents e7e4a88 + 81cb04c commit 63eeefa

File tree

7 files changed

+199
-12
lines changed

7 files changed

+199
-12
lines changed

app/code/Magento/Catalog/Block/Product/ListProduct.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,8 @@ public function getProductPrice(\Magento\Catalog\Model\Product $product)
363363
[
364364
'include_container' => true,
365365
'display_minimal_price' => true,
366-
'zone' => \Magento\Framework\Pricing\Render::ZONE_ITEM_LIST
366+
'zone' => \Magento\Framework\Pricing\Render::ZONE_ITEM_LIST,
367+
'list_category_page' => true
367368
]
368369
);
369370
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1858,6 +1858,9 @@ protected function _productLimitationPrice($joinLeft = false)
18581858
return $this;
18591859
}
18601860

1861+
// Preventing overriding price loaded from EAV because we want to use the one from index
1862+
$this->removeAttributeToSelect('price');
1863+
18611864
$connection = $this->getConnection();
18621865
$select = $this->getSelect();
18631866
$joinCond = join(

app/code/Magento/Catalog/Pricing/Render/FinalPriceBox.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,16 @@ public function showMinimalPrice()
119119
&& $minimalPriceAValue < $finalPriceValue;
120120
}
121121

122+
/**
123+
* Get Key for caching block content
124+
*
125+
* @return string
126+
*/
127+
public function getCacheKey()
128+
{
129+
return parent::getCacheKey() . ($this->getData('list_category_page') ? '-list-category-page': '');
130+
}
131+
122132
/**
123133
* {@inheritdoc}
124134
*

app/code/Magento/Catalog/Test/Unit/Pricing/Render/FinalPriceBoxTest.php

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -84,21 +84,24 @@ protected function setUp()
8484
$cacheState = $this->getMockBuilder(\Magento\Framework\App\Cache\StateInterface::class)
8585
->getMockForAbstractClass();
8686

87-
$storeManager = $this->getMockBuilder('\Magento\Store\Model\StoreManagerInterface')
88-
->setMethods(['getStore', 'getCode'])
89-
->getMockForAbstractClass();
90-
$storeManager->expects($this->any())->method('getStore')->willReturnSelf();
91-
92-
$appState = $this->getMockBuilder('\Magento\Framework\App\State')
87+
$appState = $this->getMockBuilder(\Magento\Framework\App\State::class)
9388
->disableOriginalConstructor()
9489
->getMock();
9590

96-
$resolver = $this->getMockBuilder('\Magento\Framework\View\Element\Template\File\Resolver')
91+
$resolver = $this->getMockBuilder(\Magento\Framework\View\Element\Template\File\Resolver::class)
9792
->disableOriginalConstructor()
9893
->getMock();
9994

100-
$urlBuilder = $this->getMockBuilder('\Magento\Framework\UrlInterface')
95+
$urlBuilder = $this->getMockBuilder(\Magento\Framework\UrlInterface::class)
96+
->getMockForAbstractClass();
97+
98+
$store = $this->getMockBuilder(\Magento\Store\Api\Data\StoreInterface::class)
99+
->getMockForAbstractClass();
100+
101+
$storeManager = $this->getMockBuilder('\Magento\Store\Model\StoreManagerInterface')
102+
->setMethods(['getStore', 'getCode'])
101103
->getMockForAbstractClass();
104+
$storeManager->expects($this->any())->method('getStore')->will($this->returnValue($store));
102105

103106
$scopeConfigMock = $this->getMockForAbstractClass('Magento\Framework\App\Config\ScopeConfigInterface');
104107
$context = $this->getMock('Magento\Framework\View\Element\Template\Context', [], [], '', false);
@@ -150,7 +153,7 @@ protected function setUp()
150153
'saleableItem' => $this->product,
151154
'rendererPool' => $this->rendererPool,
152155
'price' => $this->price,
153-
'data' => ['zone' => 'test_zone']
156+
'data' => ['zone' => 'test_zone', 'list_category_page' => true]
154157
]
155158
);
156159
}
@@ -247,6 +250,7 @@ public function testRenderAmountMinimal()
247250

248251
$arguments = [
249252
'zone' => 'test_zone',
253+
'list_category_page' => true,
250254
'display_label' => 'As low as',
251255
'price_id' => $priceId,
252256
'include_container' => false,
@@ -367,6 +371,12 @@ public function testHidePrice()
367371
$this->assertEmpty($this->object->toHtml());
368372
}
369373

374+
public function testGetCacheKey()
375+
{
376+
$result = $this->object->getCacheKey();
377+
$this->assertStringEndsWith('list-category-page', $result);
378+
}
379+
370380
public function testGetCacheKeyInfo()
371381
{
372382
$this->assertArrayHasKey('display_minimal_price', $this->object->getCacheKeyInfo());

app/code/Magento/Indexer/Model/Processor/CleanCache.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
*/
66
namespace Magento\Indexer\Model\Processor;
77

8+
use \Magento\Framework\App\CacheInterface;
9+
810
class CleanCache
911
{
1012
/**
@@ -17,6 +19,11 @@ class CleanCache
1719
*/
1820
protected $eventManager;
1921

22+
/**
23+
* @var \Magento\Framework\App\CacheInterface
24+
*/
25+
private $cache;
26+
2027
/**
2128
* @param \Magento\Framework\Indexer\CacheContext $context
2229
* @param \Magento\Framework\Event\Manager $eventManager
@@ -39,6 +46,9 @@ public function __construct(
3946
public function afterUpdateMview(\Magento\Indexer\Model\Processor $subject)
4047
{
4148
$this->eventManager->dispatch('clean_cache_after_reindex', ['object' => $this->context]);
49+
if (!empty($this->context->getIdentities())) {
50+
$this->getCache()->clean($this->context->getIdentities());
51+
}
4252
}
4353

4454
/**
@@ -51,5 +61,22 @@ public function afterUpdateMview(\Magento\Indexer\Model\Processor $subject)
5161
public function afterReindexAllInvalid(\Magento\Indexer\Model\Processor $subject)
5262
{
5363
$this->eventManager->dispatch('clean_cache_by_tags', ['object' => $this->context]);
64+
if (!empty($this->context->getIdentities())) {
65+
$this->getCache()->clean($this->context->getIdentities());
66+
}
67+
}
68+
69+
/**
70+
* Get cache interface
71+
*
72+
* @return \Magento\Framework\App\CacheInterface
73+
* @deprecated
74+
*/
75+
private function getCache()
76+
{
77+
if ($this->cache === null) {
78+
$this->cache = \Magento\Framework\App\ObjectManager::getInstance()->get(CacheInterface::class);
79+
}
80+
return $this->cache;
5481
}
5582
}

app/code/Magento/Indexer/Test/Unit/Model/Processor/CleanCacheTest.php

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@
55
*/
66
namespace Magento\Indexer\Test\Unit\Model\Processor;
77

8+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
9+
use Magento\Indexer\Model\Processor\CleanCache;
10+
811
class CleanCacheTest extends \PHPUnit_Framework_TestCase
912
{
1013
/**
1114
* Tested plugin
1215
*
13-
* @var \Magento\Indexer\Model\Processor\InvalidateCache
16+
* @var \Magento\Indexer\Model\Processor\CleanCache
1417
*/
1518
protected $plugin;
1619

@@ -35,18 +38,37 @@ class CleanCacheTest extends \PHPUnit_Framework_TestCase
3538
*/
3639
protected $eventManagerMock;
3740

41+
/**
42+
* Cache mock
43+
*
44+
* @var \Magento\Framework\App\CacheInterface|\PHPUnit_Framework_MockObject_MockObject
45+
*/
46+
protected $cacheMock;
47+
48+
/**
49+
* @var ObjectManager
50+
*/
51+
protected $objectManager;
52+
3853
/**
3954
* Set up
4055
*/
4156
protected function setUp()
4257
{
58+
$this->objectManager = new ObjectManager($this);
4359
$this->subjectMock = $this->getMock('Magento\Indexer\Model\Processor', [], [], '', false);
4460
$this->contextMock = $this->getMock('Magento\Framework\Indexer\CacheContext', [], [], '', false);
4561
$this->eventManagerMock = $this->getMock('Magento\Framework\Event\Manager', [], [], '', false);
46-
$this->plugin = new \Magento\Indexer\Model\Processor\CleanCache(
62+
$this->cacheMock = $this->getMock('Magento\Framework\App\CacheInterface', [], [], '', false);
63+
$this->plugin = new CleanCache(
4764
$this->contextMock,
4865
$this->eventManagerMock
4966
);
67+
$this->objectManager->setBackwardCompatibleProperty(
68+
$this->plugin,
69+
'cache',
70+
$this->cacheMock
71+
);
5072
}
5173

5274
/**
@@ -56,12 +78,22 @@ protected function setUp()
5678
*/
5779
public function testAfterUpdateMview()
5880
{
81+
$tags = ['tag_name1', 'tag_name2'];
5982
$this->eventManagerMock->expects($this->once())
6083
->method('dispatch')
6184
->with(
6285
$this->equalTo('clean_cache_after_reindex'),
6386
$this->equalTo(['object' => $this->contextMock])
6487
);
88+
89+
$this->contextMock->expects($this->atLeastOnce())
90+
->method('getIdentities')
91+
->willReturn($tags);
92+
93+
$this->cacheMock->expects($this->once())
94+
->method('clean')
95+
->with($tags);
96+
6597
$this->plugin->afterUpdateMview($this->subjectMock);
6698
}
6799
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Catalog\Model\ResourceModel\Product;
7+
8+
class CollectionTest extends \PHPUnit_Framework_TestCase
9+
{
10+
/**
11+
* @var \Magento\Catalog\Model\ResourceModel\Product\Collection
12+
*/
13+
protected $collection;
14+
15+
/**
16+
* @var \Magento\Catalog\Model\Indexer\Product\Price\Processor
17+
*/
18+
protected $processor;
19+
20+
/**
21+
* Sets up the fixture, for example, opens a network connection.
22+
* This method is called before a test is executed.
23+
*/
24+
protected function setUp()
25+
{
26+
$this->collection = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
27+
'Magento\Catalog\Model\ResourceModel\Product\Collection'
28+
);
29+
30+
$this->processor = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
31+
'Magento\Catalog\Model\Indexer\Product\Price\Processor'
32+
);
33+
}
34+
35+
36+
/**
37+
* @magentoDataFixture Magento/Catalog/_files/products.php
38+
* @magentoAppIsolation enabled
39+
*/
40+
public function testAddPriceDataOnSchedule()
41+
{
42+
$this->processor->getIndexer()->setScheduled(true);
43+
$this->assertTrue($this->processor->getIndexer()->isScheduled());
44+
$productRepository = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
45+
->create('Magento\Catalog\Api\ProductRepositoryInterface');
46+
/** @var \Magento\Catalog\Api\Data\ProductInterface $product */
47+
$product = $productRepository->get('simple');
48+
$this->assertEquals(10, $product->getPrice());
49+
$product->setPrice(15);
50+
$productRepository->save($product);
51+
$this->collection->addPriceData(0, 1);
52+
$this->collection->load();
53+
/** @var \Magento\Catalog\Api\Data\ProductInterface[] $product */
54+
$items = $this->collection->getItems();
55+
/** @var \Magento\Catalog\Api\Data\ProductInterface $product */
56+
$product = reset($items);
57+
$this->assertCount(2, $items);
58+
$this->assertEquals(10, $product->getPrice());
59+
60+
//reindexing
61+
$this->processor->getIndexer()->reindexList([1]);
62+
63+
$this->collection = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
64+
'Magento\Catalog\Model\ResourceModel\Product\Collection'
65+
);
66+
$this->collection->addPriceData(0, 1);
67+
$this->collection->load();
68+
69+
/** @var \Magento\Catalog\Api\Data\ProductInterface[] $product */
70+
$items = $this->collection->getItems();
71+
/** @var \Magento\Catalog\Api\Data\ProductInterface $product */
72+
$product = reset($items);
73+
$this->assertCount(2, $items);
74+
$this->assertEquals(15, $product->getPrice());
75+
$this->processor->getIndexer()->reindexList([1]);
76+
77+
$this->processor->getIndexer()->setScheduled(false);
78+
}
79+
80+
/**
81+
* @magentoDataFixture Magento/Catalog/_files/products.php
82+
* @magentoAppIsolation enabled
83+
*/
84+
public function testAddPriceDataOnSave()
85+
{
86+
$this->processor->getIndexer()->setScheduled(false);
87+
$this->assertFalse($this->processor->getIndexer()->isScheduled());
88+
$productRepository = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
89+
->create('Magento\Catalog\Api\ProductRepositoryInterface');
90+
/** @var \Magento\Catalog\Api\Data\ProductInterface $product */
91+
$product = $productRepository->get('simple');
92+
$this->assertNotEquals(15, $product->getPrice());
93+
$product->setPrice(15);
94+
$productRepository->save($product);
95+
$this->collection->addPriceData(0, 1);
96+
$this->collection->load();
97+
/** @var \Magento\Catalog\Api\Data\ProductInterface[] $product */
98+
$items = $this->collection->getItems();
99+
/** @var \Magento\Catalog\Api\Data\ProductInterface $product */
100+
$product = reset($items);
101+
$this->assertCount(2, $items);
102+
$this->assertEquals(15, $product->getPrice());
103+
}
104+
}

0 commit comments

Comments
 (0)