Skip to content

Commit 6bc0715

Browse files
Merge branch 'MAGETWO-91812' into 2.3-develop-pr5
2 parents 13bf294 + a7d0ea0 commit 6bc0715

File tree

18 files changed

+630
-128
lines changed

18 files changed

+630
-128
lines changed

app/code/Magento/CatalogUrlRewrite/Model/ProductUrlPathGenerator.php

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

8-
use Magento\Store\Model\Store;
9-
108
class ProductUrlPathGenerator
119
{
1210
const XML_PATH_PRODUCT_URL_SUFFIX = 'catalog/seo/product_url_suffix';
@@ -120,11 +118,12 @@ public function getCanonicalUrlPath($product, $category = null)
120118
* Generate product url key based on url_key entered by merchant or product name
121119
*
122120
* @param \Magento\Catalog\Model\Product $product
123-
* @return string
121+
* @return string|null
124122
*/
125123
public function getUrlKey($product)
126124
{
127-
return $product->getUrlKey() === false ? false : $this->prepareProductUrlKey($product);
125+
$generatedProductUrlKey = $this->prepareProductUrlKey($product);
126+
return ($product->getUrlKey() === false || empty($generatedProductUrlKey)) ? null : $generatedProductUrlKey;
128127
}
129128

130129
/**

app/code/Magento/CatalogUrlRewrite/Observer/ProductUrlKeyAutogeneratorObserver.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ public function execute(\Magento\Framework\Event\Observer $observer)
3333
{
3434
/** @var Product $product */
3535
$product = $observer->getEvent()->getProduct();
36-
$product->setUrlKey($this->productUrlPathGenerator->getUrlKey($product));
36+
$urlKey = $this->productUrlPathGenerator->getUrlKey($product);
37+
if (null !== $urlKey) {
38+
$product->setUrlKey($urlKey);
39+
}
3740
}
3841
}

app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/ProductUrlPathGeneratorTest.php

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
7+
68
namespace Magento\CatalogUrlRewrite\Test\Unit\Model;
79

810
use Magento\CatalogUrlRewrite\Model\ProductUrlPathGenerator;
@@ -32,7 +34,10 @@ class ProductUrlPathGeneratorTest extends \PHPUnit\Framework\TestCase
3234
/** @var \Magento\Catalog\Model\Category|\PHPUnit_Framework_MockObject_MockObject */
3335
protected $category;
3436

35-
protected function setUp()
37+
/**
38+
* @inheritdoc
39+
*/
40+
protected function setUp(): void
3641
{
3742
$this->category = $this->createMock(\Magento\Catalog\Model\Category::class);
3843
$productMethods = [
@@ -69,7 +74,7 @@ protected function setUp()
6974
/**
7075
* @return array
7176
*/
72-
public function getUrlPathDataProvider()
77+
public function getUrlPathDataProvider(): array
7378
{
7479
return [
7580
'path based on url key' => ['url-key', null, 'url-key'],
@@ -84,8 +89,9 @@ public function getUrlPathDataProvider()
8489
* @param string|null|bool $urlKey
8590
* @param string|null|bool $productName
8691
* @param string $result
92+
* @return void
8793
*/
88-
public function testGetUrlPath($urlKey, $productName, $result)
94+
public function testGetUrlPath($urlKey, $productName, $result): void
8995
{
9096
$this->product->expects($this->once())->method('getData')->with('url_path')
9197
->will($this->returnValue(null));
@@ -99,22 +105,23 @@ public function testGetUrlPath($urlKey, $productName, $result)
99105
/**
100106
* @param string|bool $productUrlKey
101107
* @param string|bool $expectedUrlKey
108+
* @return void
102109
* @dataProvider getUrlKeyDataProvider
103110
*/
104-
public function testGetUrlKey($productUrlKey, $expectedUrlKey)
111+
public function testGetUrlKey($productUrlKey, $expectedUrlKey): void
105112
{
106113
$this->product->expects($this->any())->method('getUrlKey')->will($this->returnValue($productUrlKey));
107114
$this->product->expects($this->any())->method('formatUrlKey')->will($this->returnValue($productUrlKey));
108-
$this->assertEquals($expectedUrlKey, $this->productUrlPathGenerator->getUrlKey($this->product));
115+
$this->assertSame($expectedUrlKey, $this->productUrlPathGenerator->getUrlKey($this->product));
109116
}
110117

111118
/**
112119
* @return array
113120
*/
114-
public function getUrlKeyDataProvider()
121+
public function getUrlKeyDataProvider(): array
115122
{
116123
return [
117-
'URL Key use default' => [false, false],
124+
'URL Key use default' => [false, null],
118125
'URL Key empty' => ['product-url', 'product-url'],
119126
];
120127
}
@@ -123,9 +130,10 @@ public function getUrlKeyDataProvider()
123130
* @param string|null|bool $storedUrlKey
124131
* @param string|null|bool $productName
125132
* @param string $expectedUrlKey
133+
* @return void
126134
* @dataProvider getUrlPathDefaultUrlKeyDataProvider
127135
*/
128-
public function testGetUrlPathDefaultUrlKey($storedUrlKey, $productName, $expectedUrlKey)
136+
public function testGetUrlPathDefaultUrlKey($storedUrlKey, $productName, $expectedUrlKey): void
129137
{
130138
$this->product->expects($this->once())->method('getData')->with('url_path')
131139
->will($this->returnValue(null));
@@ -138,15 +146,18 @@ public function testGetUrlPathDefaultUrlKey($storedUrlKey, $productName, $expect
138146
/**
139147
* @return array
140148
*/
141-
public function getUrlPathDefaultUrlKeyDataProvider()
149+
public function getUrlPathDefaultUrlKeyDataProvider(): array
142150
{
143151
return [
144152
['default-store-view-url-key', null, 'default-store-view-url-key'],
145153
[false, 'default-store-view-product-name', 'default-store-view-product-name']
146154
];
147155
}
148156

149-
public function testGetUrlPathWithCategory()
157+
/**
158+
* @return void
159+
*/
160+
public function testGetUrlPathWithCategory(): void
150161
{
151162
$this->product->expects($this->once())->method('getData')->with('url_path')
152163
->will($this->returnValue('product-path'));
@@ -159,7 +170,10 @@ public function testGetUrlPathWithCategory()
159170
);
160171
}
161172

162-
public function testGetUrlPathWithSuffix()
173+
/**
174+
* @return void
175+
*/
176+
public function testGetUrlPathWithSuffix(): void
163177
{
164178
$storeId = 1;
165179
$this->product->expects($this->once())->method('getData')->with('url_path')
@@ -177,7 +191,10 @@ public function testGetUrlPathWithSuffix()
177191
);
178192
}
179193

180-
public function testGetUrlPathWithSuffixAndCategoryAndStore()
194+
/**
195+
* @return void
196+
*/
197+
public function testGetUrlPathWithSuffixAndCategoryAndStore(): void
181198
{
182199
$storeId = 1;
183200
$this->product->expects($this->once())->method('getData')->with('url_path')
@@ -195,7 +212,10 @@ public function testGetUrlPathWithSuffixAndCategoryAndStore()
195212
);
196213
}
197214

198-
public function testGetCanonicalUrlPath()
215+
/**
216+
* @return void
217+
*/
218+
public function testGetCanonicalUrlPath(): void
199219
{
200220
$this->product->expects($this->once())->method('getId')->will($this->returnValue(1));
201221

@@ -205,7 +225,10 @@ public function testGetCanonicalUrlPath()
205225
);
206226
}
207227

208-
public function testGetCanonicalUrlPathWithCategory()
228+
/**
229+
* @return void
230+
*/
231+
public function testGetCanonicalUrlPathWithCategory(): void
209232
{
210233
$this->product->expects($this->once())->method('getId')->will($this->returnValue(1));
211234
$this->category->expects($this->once())->method('getId')->will($this->returnValue(1));
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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\CatalogUrlRewrite\Test\Unit\Observer;
9+
10+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
11+
use \Magento\CatalogUrlRewrite\Model\ProductUrlPathGenerator;
12+
13+
/**
14+
* Unit tests for \Magento\CatalogUrlRewrite\Observer\ProductUrlKeyAutogeneratorObserver class
15+
*/
16+
class ProductUrlKeyAutogeneratorObserverTest extends \PHPUnit\Framework\TestCase
17+
{
18+
/**
19+
* @var \Magento\CatalogUrlRewrite\Model\ProductUrlPathGenerator|\PHPUnit_Framework_MockObject_MockObject
20+
*/
21+
private $productUrlPathGenerator;
22+
23+
/** @var \Magento\CatalogUrlRewrite\Observer\ProductUrlKeyAutogeneratorObserver */
24+
private $productUrlKeyAutogeneratorObserver;
25+
26+
/**
27+
* @inheritdoc
28+
*/
29+
protected function setUp(): void
30+
{
31+
$this->productUrlPathGenerator = $this->getMockBuilder(ProductUrlPathGenerator::class)
32+
->disableOriginalConstructor()
33+
->setMethods(['getUrlKey'])
34+
->getMock();
35+
36+
$this->productUrlKeyAutogeneratorObserver = (new ObjectManagerHelper($this))->getObject(
37+
\Magento\CatalogUrlRewrite\Observer\ProductUrlKeyAutogeneratorObserver::class,
38+
[
39+
'productUrlPathGenerator' => $this->productUrlPathGenerator
40+
]
41+
);
42+
}
43+
44+
/**
45+
* @return void
46+
*/
47+
public function testExecuteWithUrlKey(): void
48+
{
49+
$urlKey = 'product_url_key';
50+
51+
$product = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
52+
->disableOriginalConstructor()
53+
->setMethods(['setUrlKey'])
54+
->getMock();
55+
$product->expects($this->atLeastOnce())->method('setUrlKey')->with($urlKey);
56+
$event = $this->getMockBuilder(\Magento\Framework\Event::class)
57+
->disableOriginalConstructor()
58+
->setMethods(['getProduct'])
59+
->getMock();
60+
$event->expects($this->atLeastOnce())->method('getProduct')->willReturn($product);
61+
/** @var \Magento\Framework\Event\Observer|\PHPUnit_Framework_MockObject_MockObject $observer */
62+
$observer = $this->getMockBuilder(\Magento\Framework\Event\Observer::class)
63+
->disableOriginalConstructor()
64+
->setMethods(['getEvent'])
65+
->getMock();
66+
$observer->expects($this->atLeastOnce())->method('getEvent')->willReturn($event);
67+
$this->productUrlPathGenerator->expects($this->atLeastOnce())->method('getUrlKey')->with($product)
68+
->willReturn($urlKey);
69+
70+
$this->productUrlKeyAutogeneratorObserver->execute($observer);
71+
}
72+
73+
/**
74+
* @return void
75+
*/
76+
public function testExecuteWithEmptyUrlKey(): void
77+
{
78+
$product = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
79+
->disableOriginalConstructor()
80+
->setMethods(['setUrlKey'])
81+
->getMock();
82+
$product->expects($this->never())->method('setUrlKey');
83+
$event = $this->getMockBuilder(\Magento\Framework\Event::class)
84+
->disableOriginalConstructor()
85+
->setMethods(['getProduct'])
86+
->getMock();
87+
$event->expects($this->atLeastOnce())->method('getProduct')->willReturn($product);
88+
/** @var \Magento\Framework\Event\Observer|\PHPUnit_Framework_MockObject_MockObject $observer */
89+
$observer = $this->getMockBuilder(\Magento\Framework\Event\Observer::class)
90+
->disableOriginalConstructor()
91+
->setMethods(['getEvent'])
92+
->getMock();
93+
$observer->expects($this->atLeastOnce())->method('getEvent')->willReturn($event);
94+
$this->productUrlPathGenerator->expects($this->atLeastOnce())->method('getUrlKey')->with($product)
95+
->willReturn(null);
96+
97+
$this->productUrlKeyAutogeneratorObserver->execute($observer);
98+
}
99+
}

app/code/Magento/Ui/view/base/web/js/form/element/abstract.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,10 @@ define([
450450
*/
451451
toggleUseDefault: function (state) {
452452
this.disabled(state);
453+
454+
if (this.source && this.hasService()) {
455+
this.source.set('data.use_default.' + this.index, Number(state));
456+
}
453457
},
454458

455459
/**

app/code/Magento/Ui/view/base/web/js/form/element/single-checkbox-use-config.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ define([
3636
*/
3737
toggleElement: function () {
3838
this.disabled(this.isUseDefault() || this.isUseConfig());
39+
40+
if (this.source) {
41+
this.source.set('data.use_default.' + this.index, Number(this.isUseDefault()));
42+
}
3943
}
4044
});
4145
});

app/code/Magento/Ui/view/base/web/templates/form/element/helper/service.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
attr="
1111
id: $data.uid + '_default',
1212
name: 'use_default[' + $data.index + ']',
13-
'data-form-part': $data.ns
1413
"
1514
ko-checked="isUseDefault"
1615
ko-disabled="$data.serviceDisabled">

dev/tests/js/jasmine/tests/app/code/Magento/Ui/base/js/form/element/abstract.test.js

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,50 @@
55

66
/*eslint max-nested-callbacks: 0*/
77
define([
8-
'Magento_Ui/js/form/element/abstract'
9-
], function (Abstract) {
8+
'squire'
9+
], function (Squire) {
1010
'use strict';
1111

1212
describe('Magento_Ui/js/form/element/abstract', function () {
13-
var params, model;
14-
15-
beforeEach(function () {
13+
var injector = new Squire(),
14+
providerMock = {
15+
get: jasmine.createSpy(),
16+
set: jasmine.createSpy()
17+
},
18+
mocks = {
19+
'Magento_Ui/js/lib/registry/registry': {
20+
/** Method stub. */
21+
get: function () {
22+
return providerMock;
23+
},
24+
create: jasmine.createSpy(),
25+
set: jasmine.createSpy(),
26+
async: jasmine.createSpy()
27+
},
28+
'/mage/utils/wrapper': jasmine.createSpy()
29+
},
30+
dataScope = 'abstract',
1631
params = {
17-
dataScope: 'abstract'
18-
};
19-
model = new Abstract(params);
20-
model.source = jasmine.createSpyObj('model.source', ['set']);
32+
provider: 'provName',
33+
name: '',
34+
index: 'testIndex',
35+
dataScope: dataScope,
36+
service: {
37+
template: 'ui/form/element/helper/service'
38+
}
39+
},
40+
model;
41+
42+
beforeEach(function (done) {
43+
injector.mock(mocks);
44+
injector.require([
45+
'Magento_Ui/js/form/element/abstract',
46+
'knockoutjs/knockout-es5'
47+
], function (Constr) {
48+
model = new Constr(params);
49+
50+
done();
51+
});
2152
});
2253

2354
describe('initialize method', function () {
@@ -50,8 +81,10 @@ define([
5081
var expectedValue = 1;
5182

5283
spyOn(model, 'getInitialValue').and.returnValue(expectedValue);
84+
model.service = true;
5385
expect(model.setInitialValue()).toEqual(model);
5486
expect(model.getInitialValue).toHaveBeenCalled();
87+
expect(model.source.set).toHaveBeenCalledWith('data.use_default.' + model.index, 0);
5588
expect(model.value()).toEqual(expectedValue);
5689
});
5790
});

0 commit comments

Comments
 (0)