1212use Magento \Catalog \Controller \Category \View ;
1313use Magento \Catalog \Helper \Category ;
1414use Magento \Catalog \Model \Design ;
15+ use Magento \Catalog \Model \Product \ProductList \Toolbar ;
16+ use Magento \Catalog \Model \Product \ProductList \ToolbarMemorizer ;
1517use Magento \Framework \App \Action \Action ;
1618use Magento \Framework \App \RequestInterface ;
1719use Magento \Framework \App \ResponseInterface ;
20+ use Magento \Framework \App \Response \RedirectInterface ;
1821use Magento \Framework \App \ViewInterface ;
1922use Magento \Framework \Controller \ResultFactory ;
2023use Magento \Framework \DataObject ;
@@ -127,13 +130,21 @@ class ViewTest extends TestCase
127130 */
128131 protected $ pageConfig ;
129132
133+ /**
134+ * @var ToolbarMemorizer|MockObject
135+ */
136+ protected ToolbarMemorizer $ toolbarMemorizer ;
137+
130138 /**
131139 * @inheritDoc
132140 */
133141 protected function setUp (): void
134142 {
135143 $ this ->request = $ this ->getMockForAbstractClass (RequestInterface::class);
136- $ this ->response = $ this ->getMockForAbstractClass (ResponseInterface::class);
144+ $ this ->response = $ this ->getMockBuilder (ResponseInterface::class)
145+ ->addMethods (['setRedirect ' , 'isRedirect ' ])
146+ ->onlyMethods (['sendResponse ' ])
147+ ->getMock ();
137148
138149 $ this ->categoryHelper = $ this ->createMock (Category::class);
139150 $ this ->objectManager = $ this ->getMockForAbstractClass (ObjectManagerInterface::class);
@@ -180,6 +191,8 @@ protected function setUp(): void
180191 $ this ->context ->expects ($ this ->any ())->method ('getView ' )->willReturn ($ this ->view );
181192 $ this ->context ->expects ($ this ->any ())->method ('getResultFactory ' )
182193 ->willReturn ($ this ->resultFactory );
194+ $ this ->context ->expects ($ this ->once ())->method ('getRedirect ' )
195+ ->willReturn ($ this ->createMock (RedirectInterface::class));
183196
184197 $ this ->category = $ this ->createMock (\Magento \Catalog \Model \Category::class);
185198 $ this ->categoryRepository = $ this ->getMockForAbstractClass (CategoryRepositoryInterface::class);
@@ -198,6 +211,8 @@ protected function setUp(): void
198211 ->method ('create ' )
199212 ->willReturn ($ this ->page );
200213
214+ $ this ->toolbarMemorizer = $ this ->createMock (ToolbarMemorizer::class);
215+
201216 $ this ->action = (new ObjectManager ($ this ))->getObject (
202217 View::class,
203218 [
@@ -206,9 +221,46 @@ protected function setUp(): void
206221 'categoryRepository ' => $ this ->categoryRepository ,
207222 'storeManager ' => $ this ->storeManager ,
208223 'resultPageFactory ' => $ resultPageFactory ,
209- 'categoryHelper ' => $ this ->categoryHelper
224+ 'categoryHelper ' => $ this ->categoryHelper ,
225+ 'toolbarMemorizer ' => $ this ->toolbarMemorizer
226+ ]
227+ );
228+ }
229+
230+ public function testRedirectOnToolbarAction ()
231+ {
232+ $ categoryId = 123 ;
233+ $ this ->request ->expects ($ this ->any ())
234+ ->method ('getParams ' )
235+ ->willReturn ([Toolbar::LIMIT_PARAM_NAME => 12 ]);
236+ $ this ->request ->expects ($ this ->any ())->method ('getParam ' )->willReturnMap (
237+ [
238+ [Action::PARAM_NAME_URL_ENCODED ],
239+ ['id ' , false , $ categoryId ]
210240 ]
211241 );
242+ $ this ->categoryRepository ->expects ($ this ->any ())->method ('get ' )->with ($ categoryId )
243+ ->willReturn ($ this ->category );
244+ $ this ->categoryHelper ->expects ($ this ->once ())->method ('canShow ' )->with ($ this ->category )->willReturn (true );
245+ $ this ->toolbarMemorizer ->expects ($ this ->once ())->method ('memorizeParams ' );
246+ $ this ->toolbarMemorizer ->expects ($ this ->once ())->method ('isMemorizingAllowed ' )->willReturn (true );
247+ $ this ->response ->expects ($ this ->once ())->method ('setRedirect ' );
248+ $ settings = $ this ->getMockBuilder (DataObject::class)
249+ ->addMethods (['getPageLayout ' , 'getLayoutUpdates ' ])
250+ ->disableOriginalConstructor ()
251+ ->getMock ();
252+ $ this ->category
253+ ->method ('hasChildren ' )
254+ ->willReturnOnConsecutiveCalls (true );
255+ $ this ->category ->expects ($ this ->any ())
256+ ->method ('getDisplayMode ' )
257+ ->willReturn ('products ' );
258+
259+ $ settings ->expects ($ this ->atLeastOnce ())->method ('getPageLayout ' )->willReturn ('page_layout ' );
260+ $ settings ->expects ($ this ->once ())->method ('getLayoutUpdates ' )->willReturn (['update1 ' , 'update2 ' ]);
261+ $ this ->catalogDesign ->expects ($ this ->any ())->method ('getDesignSettings ' )->willReturn ($ settings );
262+
263+ $ this ->action ->execute ();
212264 }
213265
214266 /**
@@ -230,6 +282,9 @@ public function testApplyCustomLayoutUpdate(array $expectedData): void
230282 ['id ' , false , $ categoryId ]
231283 ]
232284 );
285+ $ this ->request ->expects ($ this ->any ())
286+ ->method ('getParams ' )
287+ ->willReturn ([]);
233288
234289 $ this ->categoryRepository ->expects ($ this ->any ())->method ('get ' )->with ($ categoryId )
235290 ->willReturn ($ this ->category );
0 commit comments