Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CategoryUrlPathAutogeneratorObserverTest.php
Go to the documentation of this file.
1 <?php
7 
9 
10 class CategoryUrlPathAutogeneratorObserverTest extends \PHPUnit\Framework\TestCase
11 {
14 
17 
20 
22  protected $observer;
23 
25  protected $category;
26 
30  protected $storeViewService;
31 
35  protected $categoryResource;
36 
37  protected function setUp()
38  {
39  $this->observer = $this->createPartialMock(
40  \Magento\Framework\Event\Observer::class,
41  ['getEvent', 'getCategory']
42  );
43  $this->categoryResource = $this->createMock(\Magento\Catalog\Model\ResourceModel\Category::class);
44  $this->category = $this->createPartialMock(\Magento\Catalog\Model\Category::class, [
45  'setUrlKey',
46  'setUrlPath',
47  'dataHasChangedFor',
48  'isObjectNew',
49  'getResource',
50  'getUrlKey',
51  'getStoreId',
52  'getData'
53  ]);
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
59  );
60  $this->childrenCategoriesProvider = $this->createMock(
61  \Magento\CatalogUrlRewrite\Model\Category\ChildrenCategoriesProvider::class
62  );
63 
64  $this->storeViewService = $this->createMock(\Magento\CatalogUrlRewrite\Service\V1\StoreViewService::class);
65 
66  $this->categoryUrlPathAutogeneratorObserver = (new ObjectManagerHelper($this))->getObject(
67  \Magento\CatalogUrlRewrite\Observer\CategoryUrlPathAutogeneratorObserver::class,
68  [
69  'categoryUrlPathGenerator' => $this->categoryUrlPathGenerator,
70  'childrenCategoriesProvider' => $this->childrenCategoriesProvider,
71  'storeViewService' => $this->storeViewService,
72  ]
73  );
74  }
75 
77  {
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);
84 
85  $this->categoryUrlPathAutogeneratorObserver->execute($this->observer);
86  }
87 
89  {
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);
94  }
95 
100  public function testExecuteWithException()
101  {
102  $categoryName = 'test';
103  $categoryData = ['url_key' => 0];
104  $this->category->expects($this->once())->method('getUrlKey')->willReturn($categoryName);
105  $this->category->expects($this->once())
106  ->method('getData')
107  ->with('use_default')
108  ->willReturn($categoryData);
109  $this->categoryUrlPathGenerator->expects($this->once())
110  ->method('getUrlKey')
111  ->with($this->category)
112  ->willReturn(null);
113  $this->categoryUrlPathAutogeneratorObserver->execute($this->observer);
114  }
115 
117  {
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');
122 
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();
126  // break code execution
127  $this->category->expects($this->exactly(2))->method('isObjectNew')->willReturn(true);
128 
129  $this->categoryUrlPathAutogeneratorObserver->execute($this->observer);
130  }
131 
133  {
134  $this->categoryUrlPathGenerator->expects($this->any())->method('getUrlKey')->willReturn('url_key');
135  $this->categoryUrlPathGenerator->expects($this->any())->method('getUrlPath')->willReturn('url_path');
136 
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();
140 
141  $this->category->expects($this->exactly(2))->method('isObjectNew')->willReturn(true);
142  $this->categoryResource->expects($this->never())->method('saveAttribute');
143 
144  $this->categoryUrlPathAutogeneratorObserver->execute($this->observer);
145  }
146 
148  {
149  $this->categoryUrlPathGenerator->expects($this->any())->method('getUrlKey')->willReturn('url_key');
150  $this->categoryUrlPathGenerator->expects($this->any())->method('getUrlPath')->willReturn('url_path');
151 
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);
156 
157  $this->categoryResource->expects($this->once())->method('saveAttribute')->with($this->category, 'url_path');
158 
159  // break code execution
160  $this->category->expects($this->once())->method('dataHasChangedFor')->with('url_path')->willReturn(false);
161 
162  $this->categoryUrlPathAutogeneratorObserver->execute($this->observer);
163  }
164 
166  {
167  $this->categoryUrlPathGenerator->expects($this->any())->method('getUrlKey')->willReturn('url_key');
168  $this->categoryUrlPathGenerator->expects($this->any())->method('getUrlPath')->willReturn('url_path');
169 
170  $this->categoryResource->expects($this->once())->method('saveAttribute')->with($this->category, 'url_path');
171 
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);
176  // break code execution
177  $this->category->expects($this->once())->method('dataHasChangedFor')->with('url_path')->willReturn(false);
178 
179  $this->categoryUrlPathAutogeneratorObserver->execute($this->observer);
180  }
181 
183  {
184  $this->categoryUrlPathGenerator->expects($this->any())->method('getUrlKey')->willReturn('generated_url_key');
185  $this->categoryUrlPathGenerator->expects($this->any())->method('getUrlPath')->willReturn('generated_url_path');
186 
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);
192  // only for specific store
193  $this->category->expects($this->atLeastOnce())->method('getStoreId')->willReturn(1);
194 
195  $childCategoryResource = $this->getMockBuilder(\Magento\Catalog\Model\ResourceModel\Category::class)
196  ->disableOriginalConstructor()->getMock();
197  $childCategory = $this->getMockBuilder(\Magento\Catalog\Model\Category::class)
198  ->setMethods([
199  'getUrlPath',
200  'setUrlPath',
201  'getResource',
202  'getStore',
203  'getStoreId',
204  'setStoreId'
205  ])
206  ->disableOriginalConstructor()->getMock();
207  $childCategory->expects($this->any())->method('getResource')->willReturn($childCategoryResource);
208  $childCategory->expects($this->once())->method('setStoreId')->with(1);
209 
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');
213 
214  $this->categoryUrlPathAutogeneratorObserver->execute($this->observer);
215  }
216 }