Skip to content

Commit 4b85971

Browse files
author
Dmytro Vilchynskyi
committed
Merge remote-tracking branch 'origin/2.2-develop' into MAGETWO-84397
2 parents a763c42 + dbcb0cd commit 4b85971

File tree

48 files changed

+2135
-82
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

+2135
-82
lines changed

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,6 @@ public function getWishlistOptions()
127127
*/
128128
protected function _prepareLayout()
129129
{
130-
$this->getLayout()->createBlock(\Magento\Catalog\Block\Breadcrumbs::class);
131130
$product = $this->getProduct();
132131
if (!$product) {
133132
return parent::_prepareLayout();

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

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1135,15 +1135,19 @@ public function afterDeleteCommit()
11351135
*/
11361136
public function getIdentities()
11371137
{
1138-
$identities = [
1139-
self::CACHE_TAG . '_' . $this->getId(),
1140-
];
1141-
if (!$this->getId() || $this->hasDataChanges()
1142-
|| $this->isDeleted() || $this->dataHasChangedFor(self::KEY_INCLUDE_IN_MENU)
1143-
) {
1144-
$identities[] = self::CACHE_TAG;
1145-
$identities[] = Product::CACHE_PRODUCT_CATEGORY_TAG . '_' . $this->getId();
1138+
$identities = [];
1139+
if ($this->getId()) {
1140+
$identities[] = self::CACHE_TAG . '_' . $this->getId();
1141+
1142+
if ($this->hasDataChanges() || $this->isDeleted() || $this->dataHasChangedFor(self::KEY_INCLUDE_IN_MENU)) {
1143+
$identities[] = Product::CACHE_PRODUCT_CATEGORY_TAG . '_' . $this->getId();
1144+
}
1145+
1146+
if ($this->isObjectNew()) {
1147+
$identities[] = self::CACHE_TAG;
1148+
}
11461149
}
1150+
11471151
return $identities;
11481152
}
11491153

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

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,7 +1065,12 @@ protected function _afterLoad()
10651065
*/
10661066
public function cleanCache()
10671067
{
1068-
$this->_cacheManager->clean('catalog_product_' . $this->getId());
1068+
if ($this->getId()) {
1069+
$this->_cacheManager->clean(
1070+
self::CACHE_TAG . '_' . $this->getId()
1071+
);
1072+
}
1073+
10691074
return $this;
10701075
}
10711076

@@ -2287,7 +2292,12 @@ public function getImage()
22872292
*/
22882293
public function getIdentities()
22892294
{
2290-
$identities = [self::CACHE_TAG . '_' . $this->getId()];
2295+
$identities = [];
2296+
2297+
if ($this->getId()) {
2298+
$identities[] = self::CACHE_TAG . '_' . $this->getId();
2299+
}
2300+
22912301
if ($this->getIsChangedCategories()) {
22922302
foreach ($this->getAffectedCategoryIds() as $categoryId) {
22932303
$identities[] = self::CACHE_PRODUCT_CATEGORY_TAG . '_' . $categoryId;
@@ -2299,6 +2309,7 @@ public function getIdentities()
22992309
$identities[] = self::CACHE_PRODUCT_CATEGORY_TAG . '_' . $categoryId;
23002310
}
23012311
}
2312+
23022313
if ($this->_appState->getAreaCode() == \Magento\Framework\App\Area::AREA_FRONTEND) {
23032314
$identities[] = self::CACHE_TAG;
23042315
}
@@ -2718,4 +2729,18 @@ public function setStockData($stockData)
27182729
$this->setData('stock_data', $stockData);
27192730
return $this;
27202731
}
2732+
2733+
/**
2734+
* {@inheritDoc}
2735+
*/
2736+
public function getCacheTags()
2737+
{
2738+
//Preferring individual tags over broad ones.
2739+
$individualTags = $this->getIdentities();
2740+
if ($individualTags) {
2741+
return $individualTags;
2742+
}
2743+
2744+
return parent::getCacheTags();
2745+
}
27212746
}

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,4 +506,20 @@ public function testGetImageWithoutAttributeCode()
506506

507507
$this->assertEquals('http://www.example.com/catalog/category/myimage', $result);
508508
}
509+
510+
/**
511+
* @return void
512+
*/
513+
public function testGetIdentities()
514+
{
515+
$category = $this->getCategoryModel();
516+
517+
//Without an ID no identities can be given.
518+
$this->assertEmpty($category->getIdentities());
519+
520+
//Now because ID is set we can get some
521+
$category->setId(42);
522+
523+
$this->assertNotEmpty($category->getIdentities());
524+
}
509525
}

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

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,11 @@ class ProductTest extends \PHPUnit\Framework\TestCase
200200
*/
201201
private $extensionAttributes;
202202

203+
/**
204+
* @var CacheInterface|\PHPUnit_Framework_MockObject_MockObject
205+
*/
206+
private $cacheInterfaceMock;
207+
203208
/**
204209
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
205210
*/
@@ -246,8 +251,7 @@ protected function setUp()
246251
\Magento\Framework\Model\ActionValidator\RemoveAction::class
247252
);
248253
$actionValidatorMock->expects($this->any())->method('isAllowed')->will($this->returnValue(true));
249-
$cacheInterfaceMock = $this->createMock(\Magento\Framework\App\CacheInterface::class);
250-
254+
$this->cacheInterfaceMock = $this->createMock(\Magento\Framework\App\CacheInterface::class);
251255
$contextMock = $this->createPartialMock(
252256
\Magento\Framework\Model\Context::class,
253257
['getEventDispatcher', 'getCacheManager', 'getAppState', 'getActionValidator'], [], '', false
@@ -258,7 +262,7 @@ protected function setUp()
258262
->will($this->returnValue($this->eventManagerMock));
259263
$contextMock->expects($this->any())
260264
->method('getCacheManager')
261-
->will($this->returnValue($cacheInterfaceMock));
265+
->will($this->returnValue($this->cacheInterfaceMock));
262266
$contextMock->expects($this->any())
263267
->method('getActionValidator')
264268
->will($this->returnValue($actionValidatorMock));
@@ -1434,4 +1438,40 @@ public function testGetOptionByIdForProductWithoutOptions()
14341438
{
14351439
$this->assertNull($this->model->getOptionById(100));
14361440
}
1441+
1442+
/**
1443+
* @dataProvider cleanCacheDataProvider
1444+
*/
1445+
public function testCleanCache($id, $method)
1446+
{
1447+
$this->model->setId($id);
1448+
$this->cacheInterfaceMock
1449+
->expects($this->$method())
1450+
->method('clean');
1451+
$this->model->cleanCache();
1452+
}
1453+
1454+
/**
1455+
* @return array
1456+
*/
1457+
public function cleanCacheDataProvider()
1458+
{
1459+
return [
1460+
[null, 'never'],
1461+
['1', 'once'],
1462+
];
1463+
}
1464+
1465+
public function testGetCacheTags()
1466+
{
1467+
//If entity is identified getCacheTags has to return the same values
1468+
//as getIdentities
1469+
$this->model->setId(null);
1470+
$this->assertEquals([Product::CACHE_TAG], $this->model->getCacheTags());
1471+
$this->model->setId(1);
1472+
$this->assertEquals(
1473+
$this->model->getIdentities(),
1474+
$this->model->getCacheTags()
1475+
);
1476+
}
14371477
}
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Catalog\Test\Unit\ViewModel\Product;
8+
9+
use Magento\Catalog\Helper\Data as CatalogHelper;
10+
use Magento\Catalog\Model\Product;
11+
use Magento\Catalog\ViewModel\Product\Breadcrumbs;
12+
use Magento\Framework\App\Config\ScopeConfigInterface;
13+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
14+
15+
/**
16+
* Unit test for Magento\Catalog\ViewModel\Product\Breadcrumbs.
17+
*/
18+
class BreadcrumbsTest extends \PHPUnit\Framework\TestCase
19+
{
20+
/**
21+
* @var Breadcrumbs
22+
*/
23+
private $viewModel;
24+
25+
/**
26+
* @var CatalogHelper|\PHPUnit_Framework_MockObject_MockObject
27+
*/
28+
private $catalogHelper;
29+
30+
/**
31+
* @var ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject
32+
*/
33+
private $scopeConfig;
34+
35+
/**
36+
* @var ObjectManager
37+
*/
38+
private $objectManager;
39+
40+
/**
41+
* @inheritdoc
42+
*/
43+
protected function setUp()
44+
{
45+
$this->catalogHelper = $this->getMockBuilder(CatalogHelper::class)
46+
->setMethods(['getProduct'])
47+
->disableOriginalConstructor()
48+
->getMock();
49+
50+
$this->scopeConfig = $this->getMockBuilder(ScopeConfigInterface::class)
51+
->setMethods(['getValue', 'isSetFlag'])
52+
->disableOriginalConstructor()
53+
->getMockForAbstractClass();
54+
55+
$this->viewModel = $this->getObjectManager()->getObject(
56+
Breadcrumbs::class,
57+
[
58+
'catalogData' => $this->catalogHelper,
59+
'scopeConfig' => $this->scopeConfig,
60+
]
61+
);
62+
}
63+
64+
/**
65+
* @return void
66+
*/
67+
public function testGetCategoryUrlSuffix()
68+
{
69+
$this->scopeConfig->expects($this->once())
70+
->method('getValue')
71+
->with('catalog/seo/category_url_suffix', \Magento\Store\Model\ScopeInterface::SCOPE_STORE)
72+
->willReturn('.html');
73+
74+
$this->assertEquals('.html', $this->viewModel->getCategoryUrlSuffix());
75+
}
76+
77+
/**
78+
* @return void
79+
*/
80+
public function testIsCategoryUsedInProductUrl()
81+
{
82+
$this->scopeConfig->expects($this->once())
83+
->method('isSetFlag')
84+
->with('catalog/seo/product_use_categories', \Magento\Store\Model\ScopeInterface::SCOPE_STORE)
85+
->willReturn(false);
86+
87+
$this->assertFalse($this->viewModel->isCategoryUsedInProductUrl());
88+
}
89+
90+
/**
91+
* @dataProvider productDataProvider
92+
*
93+
* @param Product|null $product
94+
* @param string $expectedName
95+
* @return void
96+
*/
97+
public function testGetProductName($product, $expectedName)
98+
{
99+
$this->catalogHelper->expects($this->atLeastOnce())
100+
->method('getProduct')
101+
->willReturn($product);
102+
103+
$this->assertEquals($expectedName, $this->viewModel->getProductName());
104+
}
105+
106+
/**
107+
* @return array
108+
*/
109+
public function productDataProvider()
110+
{
111+
return [
112+
[$this->getObjectManager()->getObject(Product::class, ['data' => ['name' => 'Test']]), 'Test'],
113+
[null, ''],
114+
];
115+
}
116+
117+
/**
118+
* @return ObjectManager
119+
*/
120+
private function getObjectManager()
121+
{
122+
if (null === $this->objectManager) {
123+
$this->objectManager = new ObjectManager($this);
124+
}
125+
126+
return $this->objectManager;
127+
}
128+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Catalog\ViewModel\Product;
7+
8+
use Magento\Catalog\Helper\Data;
9+
use Magento\Framework\App\Config\ScopeConfigInterface;
10+
use Magento\Framework\DataObject;
11+
use Magento\Framework\View\Element\Block\ArgumentInterface;
12+
13+
/**
14+
* Product breadcrumbs view model.
15+
*/
16+
class Breadcrumbs extends DataObject implements ArgumentInterface
17+
{
18+
/**
19+
* Catalog data.
20+
*
21+
* @var Data
22+
*/
23+
private $catalogData;
24+
25+
/**
26+
* @var ScopeConfigInterface
27+
*/
28+
private $scopeConfig;
29+
30+
/**
31+
* @param Data $catalogData
32+
* @param ScopeConfigInterface $scopeConfig
33+
*/
34+
public function __construct(
35+
Data $catalogData,
36+
ScopeConfigInterface $scopeConfig
37+
) {
38+
parent::__construct();
39+
40+
$this->catalogData = $catalogData;
41+
$this->scopeConfig = $scopeConfig;
42+
}
43+
44+
/**
45+
* Returns category URL suffix.
46+
*
47+
* @return mixed
48+
*/
49+
public function getCategoryUrlSuffix()
50+
{
51+
return $this->scopeConfig->getValue(
52+
'catalog/seo/category_url_suffix',
53+
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
54+
);
55+
}
56+
57+
/**
58+
* Checks if categories path is used for product URLs.
59+
*
60+
* @return bool
61+
*/
62+
public function isCategoryUsedInProductUrl()
63+
{
64+
return $this->scopeConfig->isSetFlag(
65+
'catalog/seo/product_use_categories',
66+
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
67+
);
68+
}
69+
70+
/**
71+
* Returns product name.
72+
*
73+
* @return string
74+
*/
75+
public function getProductName()
76+
{
77+
return $this->catalogData->getProduct() !== null
78+
? $this->catalogData->getProduct()->getName()
79+
: '';
80+
}
81+
}

0 commit comments

Comments
 (0)