33 * Copyright © Magento, Inc. All rights reserved.
44 * See COPYING.txt for license details.
55 */
6+
67namespace Magento \CatalogUrlRewrite \Test \Unit \Observer ;
78
89use Magento \Framework \TestFramework \Unit \Helper \ObjectManager as ObjectManagerHelper ;
910
1011class CategoryUrlPathAutogeneratorObserverTest extends \PHPUnit \Framework \TestCase
1112{
12- /** @var \Magento\CatalogUrlRewrite\Observer\CategoryUrlPathAutogeneratorObserver */
13- protected $ categoryUrlPathAutogeneratorObserver ;
13+ /**
14+ * @var \Magento\CatalogUrlRewrite\Observer\CategoryUrlPathAutogeneratorObserver
15+ */
16+ private $ categoryUrlPathAutogeneratorObserver ;
1417
15- /** @var \PHPUnit_Framework_MockObject_MockObject */
16- protected $ categoryUrlPathGenerator ;
18+ /**
19+ * @var \PHPUnit_Framework_MockObject_MockObject
20+ */
21+ private $ categoryUrlPathGenerator ;
1722
18- /** @var \PHPUnit_Framework_MockObject_MockObject */
19- protected $ childrenCategoriesProvider ;
23+ /**
24+ * @var \PHPUnit_Framework_MockObject_MockObject
25+ */
26+ private $ childrenCategoriesProvider ;
2027
21- /** @var \PHPUnit_Framework_MockObject_MockObject */
22- protected $ observer ;
28+ /**
29+ * @var \PHPUnit_Framework_MockObject_MockObject
30+ */
31+ private $ observer ;
2332
24- /** @var \PHPUnit_Framework_MockObject_MockObject */
25- protected $ category ;
33+ /**
34+ * @var \PHPUnit_Framework_MockObject_MockObject
35+ */
36+ private $ category ;
2637
2738 /**
2839 * @var \Magento\CatalogUrlRewrite\Service\V1\StoreViewService|\PHPUnit_Framework_MockObject_MockObject
2940 */
30- protected $ storeViewService ;
41+ private $ storeViewService ;
3142
3243 /**
3344 * @var \Magento\Catalog\Model\ResourceModel\Category|\PHPUnit_Framework_MockObject_MockObject
3445 */
35- protected $ categoryResource ;
46+ private $ categoryResource ;
3647
48+ /**
49+ * @inheritDoc
50+ */
3751 protected function setUp ()
3852 {
3953 $ this ->observer = $ this ->createPartialMock (
4054 \Magento \Framework \Event \Observer::class,
4155 ['getEvent ' , 'getCategory ' ]
4256 );
4357 $ this ->categoryResource = $ this ->createMock (\Magento \Catalog \Model \ResourceModel \Category::class);
44- $ this ->category = $ this ->createPartialMock (\ Magento \ Catalog \ Model \Category::class, [
45- ' setUrlKey ' ,
46- ' setUrlPath ' ,
58+ $ this ->category = $ this ->createPartialMock (
59+ \ Magento \ Catalog \ Model \Category::class ,
60+ [
4761 'dataHasChangedFor ' ,
48- 'isObjectNew ' ,
4962 'getResource ' ,
50- 'getUrlKey ' ,
5163 'getStoreId ' ,
52- 'getData '
53- ]);
64+ 'formatUrlKey '
65+ ]
66+ );
5467 $ this ->category ->expects ($ this ->any ())->method ('getResource ' )->willReturn ($ this ->categoryResource );
5568 $ this ->observer ->expects ($ this ->any ())->method ('getEvent ' )->willReturnSelf ();
5669 $ this ->observer ->expects ($ this ->any ())->method ('getCategory ' )->willReturn ($ this ->category );
@@ -73,106 +86,125 @@ protected function setUp()
7386 );
7487 }
7588
76- public function testSetCategoryUrlAndCategoryPath ()
89+ /**
90+ * @param $isObjectNew
91+ * @throws \Magento\Framework\Exception\LocalizedException
92+ * @dataProvider shouldFormatUrlKeyAndGenerateUrlPathIfUrlKeyIsNotUsingDefaultValueDataProvider
93+ */
94+ public function testShouldFormatUrlKeyAndGenerateUrlPathIfUrlKeyIsNotUsingDefaultValue ($ isObjectNew )
7795 {
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-
96+ $ expectedUrlKey = 'formatted_url_key ' ;
97+ $ expectedUrlPath = 'generated_url_path ' ;
98+ $ categoryData = ['use_default ' => ['url_key ' => 0 ], 'url_key ' => 'some_key ' , 'url_path ' => '' ];
99+ $ this ->category ->setData ($ categoryData );
100+ $ this ->category ->isObjectNew ($ isObjectNew );
101+ $ this ->categoryUrlPathGenerator ->expects ($ this ->once ())->method ('getUrlKey ' )->willReturn ($ expectedUrlKey );
102+ $ this ->categoryUrlPathGenerator ->expects ($ this ->once ())->method ('getUrlPath ' )->willReturn ($ expectedUrlPath );
103+ $ this ->assertEquals ($ categoryData ['url_key ' ], $ this ->category ->getUrlKey ());
104+ $ this ->assertEquals ($ categoryData ['url_path ' ], $ this ->category ->getUrlPath ());
85105 $ this ->categoryUrlPathAutogeneratorObserver ->execute ($ this ->observer );
106+ $ this ->assertEquals ($ expectedUrlKey , $ this ->category ->getUrlKey ());
107+ $ this ->assertEquals ($ expectedUrlPath , $ this ->category ->getUrlPath ());
108+ $ this ->categoryResource ->expects ($ this ->never ())->method ('saveAttribute ' );
86109 }
87110
88- public function testExecuteWithoutUrlKeyAndUrlPathUpdating ()
111+ /**
112+ * @return array
113+ */
114+ public function shouldFormatUrlKeyAndGenerateUrlPathIfUrlKeyIsNotUsingDefaultValueDataProvider ()
89115 {
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 ) ;
116+ return [
117+ [ true ],
118+ [ false ],
119+ ] ;
94120 }
95121
96122 /**
97- * @expectedException \Magento\Framework\Exception\LocalizedException
98- * @expectedExceptionMessage Invalid URL key
123+ * @param $isObjectNew
124+ * @throws \Magento\Framework\Exception\LocalizedException
125+ * @dataProvider shouldResetUrlPathAndUrlKeyIfUrlKeyIsUsingDefaultValueDataProvider
99126 */
100- public function testExecuteWithException ( )
127+ public function testShouldResetUrlPathAndUrlKeyIfUrlKeyIsUsingDefaultValue ( $ isObjectNew )
101128 {
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 );
129+ $ categoryData = ['use_default ' => ['url_key ' => 1 ], 'url_key ' => 'some_key ' , 'url_path ' => 'some_path ' ];
130+ $ this ->category ->setData ($ categoryData );
131+ $ this ->category ->isObjectNew ($ isObjectNew );
132+ $ this ->category ->expects ($ this ->any ())->method ('formatUrlKey ' )->willReturn ('formatted_key ' );
133+ $ this ->assertEquals ($ categoryData ['url_key ' ], $ this ->category ->getUrlKey ());
134+ $ this ->assertEquals ($ categoryData ['url_path ' ], $ this ->category ->getUrlPath ());
113135 $ this ->categoryUrlPathAutogeneratorObserver ->execute ($ this ->observer );
136+ $ this ->assertNull ($ this ->category ->getUrlKey ());
137+ $ this ->assertNull ($ this ->category ->getUrlPath ());
114138 }
115139
116- public function testUrlKeyAndUrlPathUpdating ()
140+ /**
141+ * @return array
142+ */
143+ public function shouldResetUrlPathAndUrlKeyIfUrlKeyIsUsingDefaultValueDataProvider ()
117144 {
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 );
145+ return [
146+ [true ],
147+ [false ],
148+ ];
149+ }
128150
151+ /**
152+ * @param $useDefaultUrlKey
153+ * @param $isObjectNew
154+ * @throws \Magento\Framework\Exception\LocalizedException
155+ * @dataProvider shouldThrowExceptionIfUrlKeyIsEmptyDataProvider
156+ */
157+ public function testShouldThrowExceptionIfUrlKeyIsEmpty ($ useDefaultUrlKey , $ isObjectNew )
158+ {
159+ $ this ->expectExceptionMessage ('Invalid URL key ' );
160+ $ categoryData = ['use_default ' => ['url_key ' => $ useDefaultUrlKey ], 'url_key ' => '' , 'url_path ' => '' ];
161+ $ this ->category ->setData ($ categoryData );
162+ $ this ->category ->isObjectNew ($ isObjectNew );
163+ $ this ->assertEquals ($ isObjectNew , $ this ->category ->isObjectNew ());
164+ $ this ->assertEquals ($ categoryData ['url_key ' ], $ this ->category ->getUrlKey ());
165+ $ this ->assertEquals ($ categoryData ['url_path ' ], $ this ->category ->getUrlPath ());
129166 $ this ->categoryUrlPathAutogeneratorObserver ->execute ($ this ->observer );
167+ $ this ->assertEquals ($ categoryData ['url_key ' ], $ this ->category ->getUrlKey ());
168+ $ this ->assertEquals ($ categoryData ['url_path ' ], $ this ->category ->getUrlPath ());
130169 }
131170
132- public function testUrlPathAttributeNoUpdatingIfCategoryIsNew ()
171+ /**
172+ * @return array
173+ */
174+ public function shouldThrowExceptionIfUrlKeyIsEmptyDataProvider ()
133175 {
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 );
176+ return [
177+ [0 , false ],
178+ [0 , true ],
179+ [1 , false ],
180+ ];
145181 }
146182
147183 public function testUrlPathAttributeUpdating ()
148184 {
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-
185+ $ categoryData = ['url_key ' => 'some_key ' , 'url_path ' => '' ];
186+ $ this ->category ->setData ($ categoryData );
187+ $ this ->category ->isObjectNew (false );
188+ $ expectedUrlKey = 'formatted_url_key ' ;
189+ $ expectedUrlPath = 'generated_url_path ' ;
190+ $ this ->categoryUrlPathGenerator ->expects ($ this ->any ())->method ('getUrlKey ' )->willReturn ($ expectedUrlKey );
191+ $ this ->categoryUrlPathGenerator ->expects ($ this ->any ())->method ('getUrlPath ' )->willReturn ($ expectedUrlPath );
157192 $ this ->categoryResource ->expects ($ this ->once ())->method ('saveAttribute ' )->with ($ this ->category , 'url_path ' );
158-
159- // break code execution
160193 $ this ->category ->expects ($ this ->once ())->method ('dataHasChangedFor ' )->with ('url_path ' )->willReturn (false );
161-
162194 $ this ->categoryUrlPathAutogeneratorObserver ->execute ($ this ->observer );
163195 }
164196
165197 public function testChildrenUrlPathAttributeNoUpdatingIfParentUrlPathIsNotChanged ()
166198 {
199+ $ categoryData = ['url_key ' => 'some_key ' , 'url_path ' => '' ];
200+ $ this ->category ->setData ($ categoryData );
201+ $ this ->category ->isObjectNew (false );
202+
167203 $ this ->categoryUrlPathGenerator ->expects ($ this ->any ())->method ('getUrlKey ' )->willReturn ('url_key ' );
168204 $ this ->categoryUrlPathGenerator ->expects ($ this ->any ())->method ('getUrlPath ' )->willReturn ('url_path ' );
169205
170206 $ this ->categoryResource ->expects ($ this ->once ())->method ('saveAttribute ' )->with ($ this ->category , 'url_path ' );
171207
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 );
176208 // break code execution
177209 $ this ->category ->expects ($ this ->once ())->method ('dataHasChangedFor ' )->with ('url_path ' )->willReturn (false );
178210
@@ -181,29 +213,31 @@ public function testChildrenUrlPathAttributeNoUpdatingIfParentUrlPathIsNotChange
181213
182214 public function testChildrenUrlPathAttributeUpdatingForSpecificStore ()
183215 {
216+ $ categoryData = ['url_key ' => 'some_key ' , 'url_path ' => '' ];
217+ $ this ->category ->setData ($ categoryData );
218+ $ this ->category ->isObjectNew (false );
219+
184220 $ this ->categoryUrlPathGenerator ->expects ($ this ->any ())->method ('getUrlKey ' )->willReturn ('generated_url_key ' );
185221 $ 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 );
191222 $ this ->category ->expects ($ this ->any ())->method ('dataHasChangedFor ' )->willReturn (true );
192223 // only for specific store
193224 $ this ->category ->expects ($ this ->atLeastOnce ())->method ('getStoreId ' )->willReturn (1 );
194225
195226 $ childCategoryResource = $ this ->getMockBuilder (\Magento \Catalog \Model \ResourceModel \Category::class)
196227 ->disableOriginalConstructor ()->getMock ();
197228 $ 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 ();
229+ ->setMethods (
230+ [
231+ 'getUrlPath ' ,
232+ 'setUrlPath ' ,
233+ 'getResource ' ,
234+ 'getStore ' ,
235+ 'getStoreId ' ,
236+ 'setStoreId '
237+ ]
238+ )
239+ ->disableOriginalConstructor ()
240+ ->getMock ();
207241 $ childCategory ->expects ($ this ->any ())->method ('getResource ' )->willReturn ($ childCategoryResource );
208242 $ childCategory ->expects ($ this ->once ())->method ('setStoreId ' )->with (1 );
209243
0 commit comments