Skip to content

Commit 20a3ebe

Browse files
author
Alexander Akimov
authored
Merge pull request #3404 from magento-tsg/2.2-develop-pr56
[TSG] Backporting for 2.2 (pr56) (2.2.8)
2 parents e9b525a + 09d6e3a commit 20a3ebe

File tree

12 files changed

+266
-29
lines changed

12 files changed

+266
-29
lines changed

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

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
use Magento\UrlRewrite\Model\UrlFinderInterface;
99
use Magento\UrlRewrite\Service\V1\Data\UrlRewrite;
10+
use Magento\Framework\App\Config\ScopeConfigInterface;
1011

1112
/**
1213
* Product Url model
@@ -45,38 +46,37 @@ class Url extends \Magento\Framework\DataObject
4546
*/
4647
protected $urlFinder;
4748

49+
/**
50+
* @var ScopeConfigInterface
51+
*/
52+
private $scopeConfig;
53+
4854
/**
4955
* @param \Magento\Framework\UrlFactory $urlFactory
5056
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
5157
* @param \Magento\Framework\Filter\FilterManager $filter
5258
* @param \Magento\Framework\Session\SidResolverInterface $sidResolver
5359
* @param UrlFinderInterface $urlFinder
5460
* @param array $data
61+
* @param ScopeConfigInterface|null $scopeConfig
5562
*/
5663
public function __construct(
5764
\Magento\Framework\UrlFactory $urlFactory,
5865
\Magento\Store\Model\StoreManagerInterface $storeManager,
5966
\Magento\Framework\Filter\FilterManager $filter,
6067
\Magento\Framework\Session\SidResolverInterface $sidResolver,
6168
UrlFinderInterface $urlFinder,
62-
array $data = []
69+
array $data = [],
70+
ScopeConfigInterface $scopeConfig = null
6371
) {
6472
parent::__construct($data);
6573
$this->urlFactory = $urlFactory;
6674
$this->storeManager = $storeManager;
6775
$this->filter = $filter;
6876
$this->sidResolver = $sidResolver;
6977
$this->urlFinder = $urlFinder;
70-
}
71-
72-
/**
73-
* Retrieve URL Instance
74-
*
75-
* @return \Magento\Framework\UrlInterface
76-
*/
77-
private function getUrlInstance()
78-
{
79-
return $this->urlFactory->create();
78+
$this->scopeConfig = $scopeConfig ?:
79+
\Magento\Framework\App\ObjectManager::getInstance()->get(ScopeConfigInterface::class);
8080
}
8181

8282
/**
@@ -157,10 +157,19 @@ public function getUrl(\Magento\Catalog\Model\Product $product, $params = [])
157157
UrlRewrite::ENTITY_TYPE => \Magento\CatalogUrlRewrite\Model\ProductUrlRewriteGenerator::ENTITY_TYPE,
158158
UrlRewrite::STORE_ID => $storeId,
159159
];
160-
if ($categoryId) {
160+
$useCategories = $this->scopeConfig->getValue(
161+
\Magento\Catalog\Helper\Product::XML_PATH_PRODUCT_URL_USE_CATEGORY,
162+
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
163+
);
164+
165+
if ($useCategories && $categoryId) {
161166
$filterData[UrlRewrite::METADATA]['category_id'] = $categoryId;
167+
} elseif (!$useCategories) {
168+
$filterData[UrlRewrite::METADATA]['category_id'] = '';
162169
}
170+
163171
$rewrite = $this->urlFinder->findOneByData($filterData);
172+
164173
if ($rewrite) {
165174
$requestPath = $rewrite->getRequestPath();
166175
$product->setRequestPath($requestPath);
@@ -194,6 +203,8 @@ public function getUrl(\Magento\Catalog\Model\Product $product, $params = [])
194203
$routeParams['_query'] = [];
195204
}
196205

197-
return $this->getUrlInstance()->setScope($storeId)->getUrl($routePath, $routeParams);
206+
$url = $this->urlFactory->create()->setScope($storeId);
207+
208+
return $url->getUrl($routePath, $routeParams);
198209
}
199210
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1422,6 +1422,11 @@ protected function _addUrlRewrite()
14221422
['cu' => $this->getTable('catalog_url_rewrite_product_category')],
14231423
'u.url_rewrite_id=cu.url_rewrite_id'
14241424
)->where('cu.category_id IN (?)', $this->_urlRewriteCategory);
1425+
} else {
1426+
$select->joinLeft(
1427+
['cu' => $this->getTable('catalog_url_rewrite_product_category')],
1428+
'u.url_rewrite_id=cu.url_rewrite_id'
1429+
)->where('cu.url_rewrite_id IS NULL');
14251430
}
14261431

14271432
// more priority is data with category id

app/code/Magento/Newsletter/Controller/Subscriber/Unsubscribe.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,17 @@
44
* Copyright © Magento, Inc. All rights reserved.
55
* See COPYING.txt for license details.
66
*/
7+
78
namespace Magento\Newsletter\Controller\Subscriber;
89

10+
/**
11+
* Controller for unsubscribing customers.
12+
*/
913
class Unsubscribe extends \Magento\Newsletter\Controller\Subscriber
1014
{
1115
/**
1216
* Unsubscribe newsletter
13-
* @return void
17+
* @return \Magento\Backend\Model\View\Result\Redirect
1418
*/
1519
public function execute()
1620
{
@@ -27,6 +31,10 @@ public function execute()
2731
$this->messageManager->addException($e, __('Something went wrong while unsubscribing you.'));
2832
}
2933
}
30-
$this->getResponse()->setRedirect($this->_redirect->getRedirectUrl());
34+
/** @var \Magento\Backend\Model\View\Result\Redirect $redirect */
35+
$redirect = $this->resultFactory->create(\Magento\Framework\Controller\ResultFactory::TYPE_REDIRECT);
36+
$redirectUrl = $this->_redirect->getRedirectUrl();
37+
38+
return $redirect->setUrl($redirectUrl);
3139
}
3240
}

app/code/Magento/Reports/Model/ResourceModel/Quote/Collection.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@
55
*/
66
namespace Magento\Reports\Model\ResourceModel\Quote;
77

8+
use Magento\Store\Model\Store;
9+
810
/**
11+
* Collection of abandoned quotes with reports join.
12+
*
913
* @api
1014
* @since 100.0.2
1115
*/
@@ -48,6 +52,24 @@ public function __construct(
4852
$this->customerResource = $customerResource;
4953
}
5054

55+
/**
56+
* Filter collections by stores.
57+
*
58+
* @param array $storeIds
59+
* @param bool $withAdmin
60+
* @return $this
61+
*/
62+
public function addStoreFilter(array $storeIds, bool $withAdmin = true)
63+
{
64+
if ($withAdmin) {
65+
$storeIds[] = Store::DEFAULT_STORE_ID;
66+
}
67+
68+
$this->addFieldToFilter('store_id', ['in' => $storeIds]);
69+
70+
return $this;
71+
}
72+
5173
/**
5274
* Prepare for abandoned report
5375
*

app/code/Magento/Sales/Block/Adminhtml/Order/View/History.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ public function getStatuses()
8888
*/
8989
public function canSendCommentEmail()
9090
{
91-
return $this->_salesData->canSendOrderCommentEmail($this->getOrder()->getStore()->getId());
91+
return $this->_salesData->canSendOrderCommentEmail($this->getOrder()->getStore()->getId())
92+
&& $this->_authorization->isAllowed('Magento_Sales::email');
9293
}
9394

9495
/**

app/code/Magento/Swatches/view/frontend/web/css/swatches.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
/*width: 30px;*/
3636
padding: 1px 2px;
3737
min-width: 30px;
38-
max-width: 90px;
38+
max-width: 100%;
3939
height: 20px;
4040
float: left;
4141
margin: 0 10px 5px 0;

dev/tests/integration/testsuite/Magento/Catalog/Model/Product/UrlTest.php

Lines changed: 92 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ class UrlTest extends \PHPUnit\Framework\TestCase
2323
*/
2424
protected $urlPathGenerator;
2525

26+
/**
27+
* @var \Magento\Framework\Registry
28+
*/
29+
private $registry;
30+
2631
protected function setUp()
2732
{
2833
$this->_model = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
@@ -31,23 +36,22 @@ protected function setUp()
3136
$this->urlPathGenerator = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
3237
\Magento\CatalogUrlRewrite\Model\ProductUrlPathGenerator::class
3338
);
39+
40+
/** @var \Magento\Framework\Registry $registry */
41+
$this->registry = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
42+
\Magento\Framework\Registry::class
43+
);
3444
}
3545

3646
public function testGetUrlInStore()
3747
{
38-
$repository = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
39-
\Magento\Catalog\Model\ProductRepository::class
40-
);
41-
$product = $repository->get('simple');
48+
$product = $this->getProduct();
4249
$this->assertStringEndsWith('simple-product.html', $this->_model->getUrlInStore($product));
4350
}
4451

4552
public function testGetProductUrl()
4653
{
47-
$repository = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
48-
\Magento\Catalog\Model\ProductRepository::class
49-
);
50-
$product = $repository->get('simple');
54+
$product = $this->getProduct();
5155
$this->assertStringEndsWith('simple-product.html', $this->_model->getProductUrl($product));
5256
}
5357

@@ -80,10 +84,7 @@ public function testGetUrlPath()
8084
*/
8185
public function testGetUrl()
8286
{
83-
$repository = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
84-
\Magento\Catalog\Model\ProductRepository::class
85-
);
86-
$product = $repository->get('simple');
87+
$product = $this->getProduct();
8788
$this->assertStringEndsWith('simple-product.html', $this->_model->getUrl($product));
8889

8990
$product = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
@@ -92,4 +93,83 @@ public function testGetUrl()
9293
$product->setId(100);
9394
$this->assertContains('catalog/product/view/id/100/', $this->_model->getUrl($product));
9495
}
96+
97+
/**
98+
* @magentoAppArea frontend
99+
*
100+
* @magentoDataFixture Magento/Catalog/_files/config_use_category_in_url.php
101+
* @magentoDataFixture Magento/Catalog/_files/url_rewrites.php
102+
*
103+
* @return void
104+
*/
105+
public function testGetUrlWithCategoryInUrl()
106+
{
107+
$product = $this->getProduct();
108+
$category = $this->getCategory($product);
109+
110+
$this->assertStringEndsWith(
111+
$category->getUrlKey() . '/' . $product->getUrlKey() . '.html',
112+
$this->_model->getUrl($product)
113+
);
114+
}
115+
116+
/**
117+
* @magentoAppArea frontend
118+
*
119+
* @return void
120+
*/
121+
public function testGetUrlWithoutCategoryInUrl()
122+
{
123+
$product = $this->getProduct();
124+
$category = $this->getCategory($product);
125+
126+
$url = $this->_model->getUrl($product);
127+
128+
$this->assertStringEndsWith($product->getUrlKey() . '.html', $url);
129+
$this->assertNotContains($url, $category->getUrlKey());
130+
}
131+
132+
/**
133+
* @return \Magento\Catalog\Api\Data\ProductInterface
134+
*/
135+
private function getProduct(): \Magento\Catalog\Api\Data\ProductInterface
136+
{
137+
/** @var \Magento\Catalog\Model\ProductRepository $productRepository */
138+
$productRepository = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
139+
\Magento\Catalog\Model\ProductRepository::class
140+
);
141+
142+
return $productRepository->get('simple');
143+
}
144+
145+
/**
146+
* @param \Magento\Catalog\Api\Data\ProductInterface $product
147+
* @return \Magento\Catalog\Api\Data\CategoryInterface
148+
*/
149+
private function getCategory(
150+
\Magento\Catalog\Api\Data\ProductInterface $product
151+
): \Magento\Catalog\Api\Data\CategoryInterface {
152+
/** @var \Magento\Catalog\Model\CategoryRepository $categoryRepository */
153+
$categoryRepository = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
154+
\Magento\Catalog\Model\CategoryRepository::class
155+
);
156+
157+
$categoryId = $product->getCategoryIds()[0];
158+
159+
$category = $categoryRepository->get($categoryId);
160+
$this->registry->unregister('current_category');
161+
$this->registry->register('current_category', $category);
162+
163+
return $category;
164+
}
165+
166+
/**
167+
* @inheritdoc
168+
*/
169+
protected function tearDown()
170+
{
171+
$this->registry->unregister('current_category');
172+
173+
parent::tearDown();
174+
}
95175
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
use Magento\Catalog\Helper\Product;
8+
use Magento\TestFramework\Helper\Bootstrap;
9+
10+
$objectManager = Bootstrap::getObjectManager();
11+
12+
/** @var \Magento\Config\Model\Config $config */
13+
$config = $objectManager->get(\Magento\Config\Model\Config::class);
14+
$config->setDataByPath(Product::XML_PATH_PRODUCT_URL_USE_CATEGORY, 1);
15+
$config->save();
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
use Magento\Catalog\Helper\Product;
8+
use Magento\TestFramework\Helper\Bootstrap;
9+
10+
$objectManager = Bootstrap::getObjectManager();
11+
12+
/** @var \Magento\Config\Model\Config $config */
13+
$config = $objectManager->get(\Magento\Config\Model\Config::class);
14+
$config->setDataByPath(Product::XML_PATH_PRODUCT_URL_USE_CATEGORY, 0);
15+
$config->save();

dev/tests/integration/testsuite/Magento/CatalogImportExport/Model/Import/ProductTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1070,6 +1070,7 @@ public function testProductsWithMultipleStores()
10701070
*
10711071
* @magentoDataFixture Magento/Store/_files/core_fixturestore.php
10721072
* @magentoDataFixture Magento/Catalog/_files/category_with_two_stores.php
1073+
* @magentoDataFixture Magento/Catalog/_files/config_use_category_in_url.php
10731074
* @magentoDbIsolation enabled
10741075
* @magentoAppIsolation enabled
10751076
*/

0 commit comments

Comments
 (0)