Skip to content

Commit b18698b

Browse files
author
Alexander Akimov
authored
Merge pull request #2405 from magento-tsg/2.3-develop-pr12
[TSG] Upporting for 2.3 (pr12) (2.3.0)
2 parents f5debf0 + edeb857 commit b18698b

File tree

34 files changed

+1634
-117
lines changed

34 files changed

+1634
-117
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,13 @@ public function __construct(
110110
private function preparePageMetadata(ResultPage $resultPage, $product)
111111
{
112112
$pageLayout = $resultPage->getLayout();
113-
$pageLayout->createBlock(\Magento\Catalog\Block\Breadcrumbs::class);
114-
115113
$pageConfig = $resultPage->getConfig();
116114

117115
$title = $product->getMetaTitle();
118116
if ($title) {
119117
$pageConfig->getTitle()->set($title);
118+
} else {
119+
$pageConfig->getTitle()->set($product->getName());
120120
}
121121

122122
$keyword = $product->getMetaKeyword();
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Catalog\Test\Unit\ViewModel\Product;
9+
10+
use Magento\Catalog\Helper\Data as CatalogHelper;
11+
use Magento\Catalog\Model\Product;
12+
use Magento\Catalog\ViewModel\Product\Breadcrumbs;
13+
use Magento\Framework\App\Config\ScopeConfigInterface;
14+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
15+
16+
/**
17+
* Unit test for Magento\Catalog\ViewModel\Product\Breadcrumbs.
18+
*/
19+
class BreadcrumbsTest extends \PHPUnit\Framework\TestCase
20+
{
21+
/**
22+
* @var Breadcrumbs
23+
*/
24+
private $viewModel;
25+
26+
/**
27+
* @var CatalogHelper|\PHPUnit_Framework_MockObject_MockObject
28+
*/
29+
private $catalogHelper;
30+
31+
/**
32+
* @var ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject
33+
*/
34+
private $scopeConfig;
35+
36+
/**
37+
* @var ObjectManager
38+
*/
39+
private $objectManager;
40+
41+
/**
42+
* @inheritdoc
43+
*/
44+
protected function setUp()
45+
{
46+
$this->catalogHelper = $this->getMockBuilder(CatalogHelper::class)
47+
->setMethods(['getProduct'])
48+
->disableOriginalConstructor()
49+
->getMock();
50+
51+
$this->scopeConfig = $this->getMockBuilder(ScopeConfigInterface::class)
52+
->setMethods(['getValue', 'isSetFlag'])
53+
->disableOriginalConstructor()
54+
->getMockForAbstractClass();
55+
56+
$this->viewModel = $this->getObjectManager()->getObject(
57+
Breadcrumbs::class,
58+
[
59+
'catalogData' => $this->catalogHelper,
60+
'scopeConfig' => $this->scopeConfig,
61+
]
62+
);
63+
}
64+
65+
/**
66+
* @return void
67+
*/
68+
public function testGetCategoryUrlSuffix()
69+
{
70+
$this->scopeConfig->expects($this->once())
71+
->method('getValue')
72+
->with('catalog/seo/category_url_suffix', \Magento\Store\Model\ScopeInterface::SCOPE_STORE)
73+
->willReturn('.html');
74+
75+
$this->assertEquals('.html', $this->viewModel->getCategoryUrlSuffix());
76+
}
77+
78+
/**
79+
* @return void
80+
*/
81+
public function testIsCategoryUsedInProductUrl()
82+
{
83+
$this->scopeConfig->expects($this->once())
84+
->method('isSetFlag')
85+
->with('catalog/seo/product_use_categories', \Magento\Store\Model\ScopeInterface::SCOPE_STORE)
86+
->willReturn(false);
87+
88+
$this->assertFalse($this->viewModel->isCategoryUsedInProductUrl());
89+
}
90+
91+
/**
92+
* @dataProvider productDataProvider
93+
*
94+
* @param Product|null $product
95+
* @param string $expectedName
96+
* @return void
97+
*/
98+
public function testGetProductName($product, string $expectedName)
99+
{
100+
$this->catalogHelper->expects($this->atLeastOnce())
101+
->method('getProduct')
102+
->willReturn($product);
103+
104+
$this->assertEquals($expectedName, $this->viewModel->getProductName());
105+
}
106+
107+
/**
108+
* @return array
109+
*/
110+
public function productDataProvider()
111+
{
112+
return [
113+
[$this->getObjectManager()->getObject(Product::class, ['data' => ['name' => 'Test']]), 'Test'],
114+
[null, ''],
115+
];
116+
}
117+
118+
/**
119+
* @return ObjectManager
120+
*/
121+
private function getObjectManager()
122+
{
123+
if (null === $this->objectManager) {
124+
$this->objectManager = new ObjectManager($this);
125+
}
126+
127+
return $this->objectManager;
128+
}
129+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Catalog\ViewModel\Product;
9+
10+
use Magento\Catalog\Helper\Data;
11+
use Magento\Framework\App\Config\ScopeConfigInterface;
12+
use Magento\Framework\DataObject;
13+
use Magento\Framework\View\Element\Block\ArgumentInterface;
14+
15+
/**
16+
* Product breadcrumbs view model.
17+
*/
18+
class Breadcrumbs extends DataObject implements ArgumentInterface
19+
{
20+
/**
21+
* Catalog data.
22+
*
23+
* @var Data
24+
*/
25+
private $catalogData;
26+
27+
/**
28+
* @var ScopeConfigInterface
29+
*/
30+
private $scopeConfig;
31+
32+
/**
33+
* @param Data $catalogData
34+
* @param ScopeConfigInterface $scopeConfig
35+
*/
36+
public function __construct(
37+
Data $catalogData,
38+
ScopeConfigInterface $scopeConfig
39+
) {
40+
parent::__construct();
41+
42+
$this->catalogData = $catalogData;
43+
$this->scopeConfig = $scopeConfig;
44+
}
45+
46+
/**
47+
* Returns category URL suffix.
48+
*
49+
* @return mixed
50+
*/
51+
public function getCategoryUrlSuffix()
52+
{
53+
return $this->scopeConfig->getValue(
54+
'catalog/seo/category_url_suffix',
55+
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
56+
);
57+
}
58+
59+
/**
60+
* Checks if categories path is used for product URLs.
61+
*
62+
* @return bool
63+
*/
64+
public function isCategoryUsedInProductUrl(): bool
65+
{
66+
return $this->scopeConfig->isSetFlag(
67+
'catalog/seo/product_use_categories',
68+
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
69+
);
70+
}
71+
72+
/**
73+
* Returns product name.
74+
*
75+
* @return string
76+
*/
77+
public function getProductName(): string
78+
{
79+
return $this->catalogData->getProduct() !== null
80+
? $this->catalogData->getProduct()->getName()
81+
: '';
82+
}
83+
}

app/code/Magento/Catalog/view/frontend/layout/catalog_product_view.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@
2828
<argument name="add_attribute" xsi:type="string">itemscope itemtype="http://schema.org/Product"</argument>
2929
</arguments>
3030
</referenceBlock>
31+
32+
<referenceBlock name="breadcrumbs" template="Magento_Catalog::product/breadcrumbs.phtml">
33+
<arguments>
34+
<argument name="viewModel" xsi:type="object">Magento\Catalog\ViewModel\Product\Breadcrumbs</argument>
35+
</arguments>
36+
</referenceBlock>
37+
3138
<referenceContainer name="content">
3239
<container name="product.info.main" htmlTag="div" htmlClass="product-info-main" before="-">
3340
<container name="product.info.price" label="Product info auxiliary container" htmlTag="div" htmlClass="product-info-price">

app/code/Magento/Catalog/view/frontend/requirejs-config.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,12 @@ var config = {
1818
priceUtils: 'Magento_Catalog/js/price-utils',
1919
catalogAddToCart: 'Magento_Catalog/js/catalog-add-to-cart'
2020
}
21+
},
22+
config: {
23+
mixins: {
24+
'Magento_Theme/js/view/breadcrumbs': {
25+
'Magento_Catalog/js/product/breadcrumbs': true
26+
}
27+
}
2128
}
2229
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
/** @var \Magento\Theme\Block\Html\Breadcrumbs $block */
7+
/** @var \Magento\Catalog\ViewModel\Product\Breadcrumbs $viewModel */
8+
$viewModel = $block->getData('viewModel');
9+
?>
10+
<div class="breadcrumbs" data-mage-init='{
11+
"breadcrumbs": {
12+
"categoryUrlSuffix": "<?= $block->escapeHtml($viewModel->getCategoryUrlSuffix()); ?>",
13+
"useCategoryPathInUrl": <?= (int)$viewModel->isCategoryUsedInProductUrl(); ?>,
14+
"product": "<?= $block->escapeHtml($viewModel->getProductName()); ?>"
15+
}
16+
}'>
17+
</div>

0 commit comments

Comments
 (0)