39 $this->observer = $this->createPartialMock(
41 [
'getEvent',
'getCategory']
43 $this->categoryResource = $this->createMock(\
Magento\Catalog\Model\
ResourceModel\Category::class);
44 $this->category = $this->createPartialMock(\
Magento\Catalog\Model\Category::class, [
54 $this->category->expects($this->any())->method(
'getResource')->willReturn($this->categoryResource);
55 $this->observer->expects($this->any())->method(
'getEvent')->willReturnSelf();
56 $this->observer->expects($this->any())->method(
'getCategory')->willReturn($this->category);
57 $this->categoryUrlPathGenerator = $this->createMock(
58 \
Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator::class
60 $this->childrenCategoriesProvider = $this->createMock(
61 \
Magento\CatalogUrlRewrite\Model\
Category\ChildrenCategoriesProvider::class
64 $this->storeViewService = $this->createMock(\
Magento\CatalogUrlRewrite\Service\V1\StoreViewService::class);
66 $this->categoryUrlPathAutogeneratorObserver = (
new ObjectManagerHelper($this))->getObject(
67 \
Magento\CatalogUrlRewrite\
Observer\CategoryUrlPathAutogeneratorObserver::class,
69 'categoryUrlPathGenerator' => $this->categoryUrlPathGenerator,
70 'childrenCategoriesProvider' => $this->childrenCategoriesProvider,
71 'storeViewService' => $this->storeViewService,
78 $this->category->expects($this->once())->method(
'getUrlKey')->willReturn(
'category');
79 $this->categoryUrlPathGenerator->expects($this->once())->method(
'getUrlKey')->willReturn(
'urk_key');
80 $this->category->expects($this->once())->method(
'setUrlKey')->with(
'urk_key')->willReturnSelf();
81 $this->categoryUrlPathGenerator->expects($this->once())->method(
'getUrlPath')->willReturn(
'url_path');
82 $this->category->expects($this->once())->method(
'setUrlPath')->with(
'url_path')->willReturnSelf();
83 $this->category->expects($this->exactly(2))->method(
'isObjectNew')->willReturn(
true);
85 $this->categoryUrlPathAutogeneratorObserver->execute($this->observer);
90 $this->category->expects($this->once())->method(
'getUrlKey')->willReturn(
false);
91 $this->category->expects($this->never())->method(
'setUrlKey');
92 $this->category->expects($this->never())->method(
'setUrlPath');
93 $this->categoryUrlPathAutogeneratorObserver->execute($this->observer);
102 $categoryName =
'test';
103 $categoryData = [
'url_key' => 0];
104 $this->category->expects($this->once())->method(
'getUrlKey')->willReturn($categoryName);
105 $this->category->expects($this->once())
107 ->with(
'use_default')
108 ->willReturn($categoryData);
109 $this->categoryUrlPathGenerator->expects($this->once())
110 ->method(
'getUrlKey')
111 ->with($this->category)
113 $this->categoryUrlPathAutogeneratorObserver->execute($this->observer);
118 $this->categoryUrlPathGenerator->expects($this->once())->method(
'getUrlKey')->with($this->category)
119 ->willReturn(
'url_key');
120 $this->categoryUrlPathGenerator->expects($this->once())->method(
'getUrlPath')->with($this->category)
121 ->willReturn(
'url_path');
123 $this->category->expects($this->once())->method(
'getUrlKey')->willReturn(
'not_formatted_url_key');
124 $this->category->expects($this->once())->method(
'setUrlKey')->with(
'url_key')->willReturnSelf();
125 $this->category->expects($this->once())->method(
'setUrlPath')->with(
'url_path')->willReturnSelf();
127 $this->category->expects($this->exactly(2))->method(
'isObjectNew')->willReturn(
true);
129 $this->categoryUrlPathAutogeneratorObserver->execute($this->observer);
134 $this->categoryUrlPathGenerator->expects($this->any())->method(
'getUrlKey')->willReturn(
'url_key');
135 $this->categoryUrlPathGenerator->expects($this->any())->method(
'getUrlPath')->willReturn(
'url_path');
137 $this->category->expects($this->any())->method(
'getUrlKey')->willReturn(
'not_formatted_url_key');
138 $this->category->expects($this->any())->method(
'setUrlKey')->willReturnSelf();
139 $this->category->expects($this->any())->method(
'setUrlPath')->willReturnSelf();
141 $this->category->expects($this->exactly(2))->method(
'isObjectNew')->willReturn(
true);
142 $this->categoryResource->expects($this->never())->method(
'saveAttribute');
144 $this->categoryUrlPathAutogeneratorObserver->execute($this->observer);
149 $this->categoryUrlPathGenerator->expects($this->any())->method(
'getUrlKey')->willReturn(
'url_key');
150 $this->categoryUrlPathGenerator->expects($this->any())->method(
'getUrlPath')->willReturn(
'url_path');
152 $this->category->expects($this->any())->method(
'getUrlKey')->willReturn(
'not_formatted_url_key');
153 $this->category->expects($this->any())->method(
'setUrlKey')->willReturnSelf();
154 $this->category->expects($this->any())->method(
'setUrlPath')->willReturnSelf();
155 $this->category->expects($this->exactly(2))->method(
'isObjectNew')->willReturn(
false);
157 $this->categoryResource->expects($this->once())->method(
'saveAttribute')->with($this->category,
'url_path');
160 $this->category->expects($this->once())->method(
'dataHasChangedFor')->with(
'url_path')->willReturn(
false);
162 $this->categoryUrlPathAutogeneratorObserver->execute($this->observer);
167 $this->categoryUrlPathGenerator->expects($this->any())->method(
'getUrlKey')->willReturn(
'url_key');
168 $this->categoryUrlPathGenerator->expects($this->any())->method(
'getUrlPath')->willReturn(
'url_path');
170 $this->categoryResource->expects($this->once())->method(
'saveAttribute')->with($this->category,
'url_path');
172 $this->category->expects($this->any())->method(
'getUrlKey')->willReturn(
'not_formatted_url_key');
173 $this->category->expects($this->any())->method(
'setUrlKey')->willReturnSelf();
174 $this->category->expects($this->any())->method(
'setUrlPath')->willReturnSelf();
175 $this->category->expects($this->exactly(2))->method(
'isObjectNew')->willReturn(
false);
177 $this->category->expects($this->once())->method(
'dataHasChangedFor')->with(
'url_path')->willReturn(
false);
179 $this->categoryUrlPathAutogeneratorObserver->execute($this->observer);
184 $this->categoryUrlPathGenerator->expects($this->any())->method(
'getUrlKey')->willReturn(
'generated_url_key');
185 $this->categoryUrlPathGenerator->expects($this->any())->method(
'getUrlPath')->willReturn(
'generated_url_path');
187 $this->category->expects($this->any())->method(
'getUrlKey')->willReturn(
'not_formatted_url_key');
188 $this->category->expects($this->any())->method(
'setUrlKey')->willReturnSelf();
189 $this->category->expects($this->any())->method(
'setUrlPath')->willReturnSelf();
190 $this->category->expects($this->exactly(2))->method(
'isObjectNew')->willReturn(
false);
191 $this->category->expects($this->any())->method(
'dataHasChangedFor')->willReturn(
true);
193 $this->category->expects($this->atLeastOnce())->method(
'getStoreId')->willReturn(1);
195 $childCategoryResource = $this->getMockBuilder(\
Magento\Catalog\Model\
ResourceModel\Category::class)
196 ->disableOriginalConstructor()->getMock();
197 $childCategory = $this->getMockBuilder(\
Magento\Catalog\Model\Category::class)
206 ->disableOriginalConstructor()->getMock();
207 $childCategory->expects($this->any())->method(
'getResource')->willReturn($childCategoryResource);
208 $childCategory->expects($this->once())->method(
'setStoreId')->with(1);
210 $this->childrenCategoriesProvider->expects($this->once())->method(
'getChildren')->willReturn([$childCategory]);
211 $childCategory->expects($this->once())->method(
'setUrlPath')->with(
'generated_url_path')->willReturnSelf();
212 $childCategoryResource->expects($this->once())->method(
'saveAttribute')->with($childCategory,
'url_path');
214 $this->categoryUrlPathAutogeneratorObserver->execute($this->observer);
testUrlPathAttributeUpdating()
testExecuteWithException()
testSetCategoryUrlAndCategoryPath()
testChildrenUrlPathAttributeNoUpdatingIfParentUrlPathIsNotChanged()
testChildrenUrlPathAttributeUpdatingForSpecificStore()
testUrlKeyAndUrlPathUpdating()
testExecuteWithoutUrlKeyAndUrlPathUpdating()
$childrenCategoriesProvider
$categoryUrlPathGenerator
$categoryUrlPathAutogeneratorObserver
testUrlPathAttributeNoUpdatingIfCategoryIsNew()