Skip to content

Commit 55c58a6

Browse files
authored
Merge pull request magento#846 from magento-tango/MAGETWO-61549
MAGETWO-61549: Use default URL Key
2 parents 4148ee1 + 696d580 commit 55c58a6

File tree

8 files changed

+67
-16
lines changed

8 files changed

+67
-16
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ public function execute(\Magento\Framework\Event\Observer $observer)
4848
{
4949
/** @var Category $category */
5050
$category = $observer->getEvent()->getCategory();
51-
if ($category->getUrlKey() !== false) {
51+
$useDefaultAttribute = !$category->isObjectNew() && !empty($category->getData('use_default')['url_key']);
52+
if ($category->getUrlKey() !== false && !$useDefaultAttribute) {
5253
$resultUrlKey = $this->categoryUrlPathGenerator->getUrlKey($category);
5354
if (empty($resultUrlKey)) {
5455
throw new \Magento\Framework\Exception\LocalizedException(__('Invalid URL key'));

app/code/Magento/CatalogUrlRewrite/Test/Unit/Observer/CategoryUrlPathAutogeneratorObserverTest.php

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,16 @@ protected function setUp()
5252
);
5353
$this->category = $this->getMock(
5454
\Magento\Catalog\Model\Category::class,
55-
['setUrlKey', 'setUrlPath', 'dataHasChangedFor', 'isObjectNew', 'getResource', 'getUrlKey', 'getStoreId'],
55+
[
56+
'setUrlKey',
57+
'setUrlPath',
58+
'dataHasChangedFor',
59+
'isObjectNew',
60+
'getResource',
61+
'getUrlKey',
62+
'getStoreId',
63+
'getData'
64+
],
5665
[],
5766
'',
5867
false
@@ -96,7 +105,7 @@ public function testSetCategoryUrlAndCategoryPath()
96105
$this->category->expects($this->once())->method('setUrlKey')->with('urk_key')->willReturnSelf();
97106
$this->categoryUrlPathGenerator->expects($this->once())->method('getUrlPath')->willReturn('url_path');
98107
$this->category->expects($this->once())->method('setUrlPath')->with('url_path')->willReturnSelf();
99-
$this->category->expects($this->once())->method('isObjectNew')->willReturn(true);
108+
$this->category->expects($this->exactly(2))->method('isObjectNew')->willReturn(true);
100109

101110
$this->categoryUrlPathAutogeneratorObserver->execute($this->observer);
102111
}
@@ -109,6 +118,26 @@ public function testExecuteWithoutUrlKeyAndUrlPathUpdating()
109118
$this->categoryUrlPathAutogeneratorObserver->execute($this->observer);
110119
}
111120

121+
/**
122+
* @expectedException \Magento\Framework\Exception\LocalizedException
123+
* @expectedExceptionMessage Invalid URL key
124+
*/
125+
public function testExecuteWithException()
126+
{
127+
$categoryName = 'test';
128+
$categoryData = ['url_key' => 0];
129+
$this->category->expects($this->once())->method('getUrlKey')->willReturn($categoryName);
130+
$this->category->expects($this->once())
131+
->method('getData')
132+
->with('use_default')
133+
->willReturn($categoryData);
134+
$this->categoryUrlPathGenerator->expects($this->once())
135+
->method('getUrlKey')
136+
->with($this->category)
137+
->willReturn(null);
138+
$this->categoryUrlPathAutogeneratorObserver->execute($this->observer);
139+
}
140+
112141
public function testUrlKeyAndUrlPathUpdating()
113142
{
114143
$this->categoryUrlPathGenerator->expects($this->once())->method('getUrlKey')->with($this->category)
@@ -120,7 +149,7 @@ public function testUrlKeyAndUrlPathUpdating()
120149
$this->category->expects($this->once())->method('setUrlKey')->with('url_key')->willReturnSelf();
121150
$this->category->expects($this->once())->method('setUrlPath')->with('url_path')->willReturnSelf();
122151
// break code execution
123-
$this->category->expects($this->once())->method('isObjectNew')->willReturn(true);
152+
$this->category->expects($this->exactly(2))->method('isObjectNew')->willReturn(true);
124153

125154
$this->categoryUrlPathAutogeneratorObserver->execute($this->observer);
126155
}
@@ -134,7 +163,7 @@ public function testUrlPathAttributeNoUpdatingIfCategoryIsNew()
134163
$this->category->expects($this->any())->method('setUrlKey')->willReturnSelf();
135164
$this->category->expects($this->any())->method('setUrlPath')->willReturnSelf();
136165

137-
$this->category->expects($this->once())->method('isObjectNew')->willReturn(true);
166+
$this->category->expects($this->exactly(2))->method('isObjectNew')->willReturn(true);
138167
$this->categoryResource->expects($this->never())->method('saveAttribute');
139168

140169
$this->categoryUrlPathAutogeneratorObserver->execute($this->observer);
@@ -148,7 +177,7 @@ public function testUrlPathAttributeUpdating()
148177
$this->category->expects($this->any())->method('getUrlKey')->willReturn('not_formatted_url_key');
149178
$this->category->expects($this->any())->method('setUrlKey')->willReturnSelf();
150179
$this->category->expects($this->any())->method('setUrlPath')->willReturnSelf();
151-
$this->category->expects($this->once())->method('isObjectNew')->willReturn(false);
180+
$this->category->expects($this->exactly(2))->method('isObjectNew')->willReturn(false);
152181

153182
$this->categoryResource->expects($this->once())->method('saveAttribute')->with($this->category, 'url_path');
154183

@@ -168,7 +197,7 @@ public function testChildrenUrlPathAttributeNoUpdatingIfParentUrlPathIsNotChange
168197
$this->category->expects($this->any())->method('getUrlKey')->willReturn('not_formatted_url_key');
169198
$this->category->expects($this->any())->method('setUrlKey')->willReturnSelf();
170199
$this->category->expects($this->any())->method('setUrlPath')->willReturnSelf();
171-
$this->category->expects($this->once())->method('isObjectNew')->willReturn(false);
200+
$this->category->expects($this->exactly(2))->method('isObjectNew')->willReturn(false);
172201
// break code execution
173202
$this->category->expects($this->once())->method('dataHasChangedFor')->with('url_path')->willReturn(false);
174203

@@ -183,7 +212,7 @@ public function testChildrenUrlPathAttributeUpdatingForSpecificStore()
183212
$this->category->expects($this->any())->method('getUrlKey')->willReturn('not_formatted_url_key');
184213
$this->category->expects($this->any())->method('setUrlKey')->willReturnSelf();
185214
$this->category->expects($this->any())->method('setUrlPath')->willReturnSelf();
186-
$this->category->expects($this->any())->method('isObjectNew')->willReturn(false);
215+
$this->category->expects($this->exactly(2))->method('isObjectNew')->willReturn(false);
187216
$this->category->expects($this->any())->method('dataHasChangedFor')->willReturn(true);
188217
// only for specific store
189218
$this->category->expects($this->atLeastOnce())->method('getStoreId')->willReturn(1);

dev/tests/functional/tests/app/Magento/Catalog/Test/Block/Adminhtml/Category/Edit/CategoryForm.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ public function fill(FixtureInterface $fixture, SimpleElement $element = null)
5858
['element' => $modalElement]
5959
);
6060
$modal->acceptAlert();
61+
$modal->waitModalWindowToDisappear();
6162
}
6263
return parent::fill($fixture, $element);
6364
}

dev/tests/functional/tests/app/Magento/Catalog/Test/Block/Adminhtml/Category/Edit/CategoryForm.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
<selector>input[name='name']</selector>
2525
</name>
2626
<use_default_name>
27-
<selector>[name="use_default[]"][value="name"]</selector>
27+
<selector>[name="use_default[name]"]</selector>
2828
<input>checkbox</input>
2929
</use_default_name>
3030
</fields>

dev/tests/functional/tests/app/Magento/Catalog/Test/Constraint/AssertCategoryForm.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ class AssertCategoryForm extends AbstractAssertForm
2727
*/
2828
protected $skippedFixtureFields = [
2929
'parent_id',
30-
'id'
30+
'id',
31+
'store_id'
3132
];
3233

3334
/**
@@ -45,7 +46,10 @@ public function processAssert(
4546
) {
4647
$catalogCategoryIndex->open();
4748
$catalogCategoryIndex->getTreeCategories()->selectCategory($category, true);
48-
49+
if ($category->hasData('store_id')) {
50+
$storeName = $category->getStoreId()['source']->getName();
51+
$catalogCategoryEdit->getFormPageActions()->selectStoreView($storeName);
52+
}
4953
$fixtureData = $this->prepareFixtureData($category->getData());
5054
$formData = $catalogCategoryEdit->getEditForm()->getData($category);
5155
$error = $this->verifyData($this->sortData($fixtureData), $this->sortData($formData));

dev/tests/functional/tests/app/Magento/Catalog/Test/Repository/Category.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,17 @@
1717
</field>
1818
</dataset>
1919

20+
<dataset name="default_with_custom_url">
21+
<field name="name" xsi:type="string">Category%isolation%</field>
22+
<field name="url_key" xsi:type="string">custom%isolation%</field>
23+
<field name="is_active" xsi:type="string">Yes</field>
24+
<field name="include_in_menu" xsi:type="string">Yes</field>
25+
<field name="parent_id" xsi:type="array">
26+
<item name="dataset" xsi:type="string">default_category</item>
27+
</field>
28+
<field name="use_default_url_key" xsi:type="string">Yes</field>
29+
</dataset>
30+
2031
<dataset name="default_category">
2132
<field name="id" xsi:type="string">%id%</field>
2233
<field name="parent_id" xsi:type="string">%id%</field>

dev/tests/functional/tests/app/Magento/Catalog/Test/TestCase/Category/UpdateCategoryEntityTest.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,23 +60,19 @@ class UpdateCategoryEntityTest extends Injectable
6060
/**
6161
* Inject page end prepare default category
6262
*
63-
* @param Category $initialCategory
6463
* @param CatalogCategoryIndex $catalogCategoryIndex
6564
* @param CatalogCategoryEdit $catalogCategoryEdit
6665
* @param FixtureFactory $fixtureFactory
67-
* @return array
66+
* @return void
6867
*/
6968
public function __inject(
70-
Category $initialCategory,
7169
CatalogCategoryIndex $catalogCategoryIndex,
7270
CatalogCategoryEdit $catalogCategoryEdit,
7371
FixtureFactory $fixtureFactory
7472
) {
7573
$this->fixtureFactory = $fixtureFactory;
7674
$this->catalogCategoryIndex = $catalogCategoryIndex;
7775
$this->catalogCategoryEdit = $catalogCategoryEdit;
78-
$initialCategory->persist();
79-
return ['initialCategory' => $initialCategory];
8076
}
8177

8278
/**
@@ -88,6 +84,7 @@ public function __inject(
8884
*/
8985
public function test(Category $category, Category $initialCategory)
9086
{
87+
$initialCategory->persist();
9188
$this->catalogCategoryIndex->open();
9289
$this->catalogCategoryIndex->getTreeCategories()->selectCategory($initialCategory);
9390
$this->catalogCategoryEdit->getEditForm()->fill($category);

dev/tests/functional/tests/app/Magento/Catalog/Test/TestCase/Category/UpdateCategoryEntityTest.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,13 @@
5959
<constraint name="Magento\Catalog\Test\Constraint\AssertCategorySaveMessage" />
6060
<constraint name="Magento\Catalog\Test\Constraint\AssertCategoryOnCustomStore" />
6161
</variation>
62+
<variation name="UpdateCategoryEntityTestVariation6_CheckCategoryDefaultUrlOnStoreView" summary="Check default URL Key on the custom Store View." ticketId="MAGETWO-64337">
63+
<data name="initialCategory/dataset" xsi:type="string">default_with_custom_url</data>
64+
<data name="category/data/parent_id/dataset" xsi:type="string">default_category</data>
65+
<data name="category/data/store_id/dataset" xsi:type="string">custom</data>
66+
<data name="category/data/use_default_url_key" xsi:type="string">Yes</data>
67+
<constraint name="Magento\Catalog\Test\Constraint\AssertCategorySaveMessage" />
68+
<constraint name="Magento\Catalog\Test\Constraint\AssertCategoryForm" />
69+
</variation>
6270
</testCase>
6371
</config>

0 commit comments

Comments
 (0)