@@ -153,12 +153,7 @@ public function testSaveAction()
153153 ->method ('create ' )
154154 ->willReturn ($ page );
155155
156- $ page ->expects ($ this ->any ())
157- ->method ('load ' )
158- ->willReturnSelf ();
159- $ page ->expects ($ this ->any ())
160- ->method ('getId ' )
161- ->willReturn (true );
156+ $ this ->pageRepository ->expects ($ this ->once ())->method ('getById ' )->with ($ this ->pageId )->willReturn ($ page );
162157 $ page ->expects ($ this ->once ())->method ('setData ' );
163158 $ this ->pageRepository ->expects ($ this ->once ())->method ('save ' )->with ($ page );
164159
@@ -182,6 +177,36 @@ public function testSaveActionWithoutData()
182177 $ this ->assertSame ($ this ->resultRedirect , $ this ->saveController ->execute ());
183178 }
184179
180+ public function testSaveActionNoId ()
181+ {
182+ $ this ->requestMock ->expects ($ this ->any ())->method ('getPostValue ' )->willReturn (['page_id ' => 1 ]);
183+ $ this ->requestMock ->expects ($ this ->atLeastOnce ())
184+ ->method ('getParam ' )
185+ ->willReturnMap (
186+ [
187+ ['page_id ' , null , 1 ],
188+ ['back ' , null , false ],
189+ ]
190+ );
191+
192+ $ page = $ this ->getMockBuilder (\Magento \Cms \Model \Page::class)
193+ ->disableOriginalConstructor ()
194+ ->getMock ();
195+
196+ $ this ->pageFactory ->expects ($ this ->atLeastOnce ())
197+ ->method ('create ' )
198+ ->willReturn ($ page );
199+ $ this ->pageRepository ->expects ($ this ->once ())
200+ ->method ('getById ' )
201+ ->with ($ this ->pageId )
202+ ->willThrowException (new \Magento \Framework \Exception \NoSuchEntityException (__ ('Error message ' )));
203+ $ this ->messageManagerMock ->expects ($ this ->once ())
204+ ->method ('addErrorMessage ' )
205+ ->with (__ ('This page no longer exists. ' ));
206+ $ this ->resultRedirect ->expects ($ this ->atLeastOnce ())->method ('setPath ' )->with ('*/*/ ' ) ->willReturnSelf ();
207+ $ this ->assertSame ($ this ->resultRedirect , $ this ->saveController ->execute ());
208+ }
209+
185210 public function testSaveAndContinue ()
186211 {
187212 $ this ->requestMock ->expects ($ this ->any ())->method ('getPostValue ' )->willReturn (['page_id ' => $ this ->pageId ]);
@@ -204,12 +229,7 @@ public function testSaveAndContinue()
204229 ->method ('create ' )
205230 ->willReturn ($ page );
206231
207- $ page ->expects ($ this ->any ())
208- ->method ('load ' )
209- ->willReturnSelf ();
210- $ page ->expects ($ this ->any ())
211- ->method ('getId ' )
212- ->willReturn (true );
232+ $ this ->pageRepository ->expects ($ this ->once ())->method ('getById ' )->with ($ this ->pageId )->willReturn ($ page );
213233 $ page ->expects ($ this ->once ())->method ('setData ' );
214234 $ this ->pageRepository ->expects ($ this ->once ())->method ('save ' )->with ($ page );
215235
@@ -251,12 +271,7 @@ public function testSaveActionThrowsException()
251271 ->method ('create ' )
252272 ->willReturn ($ page );
253273
254- $ page ->expects ($ this ->any ())
255- ->method ('load ' )
256- ->willReturnSelf ();
257- $ page ->expects ($ this ->any ())
258- ->method ('getId ' )
259- ->willReturn (true );
274+ $ this ->pageRepository ->expects ($ this ->once ())->method ('getById ' )->with ($ this ->pageId )->willReturn ($ page );
260275 $ page ->expects ($ this ->once ())->method ('setData ' );
261276 $ this ->pageRepository ->expects ($ this ->once ())->method ('save ' )->with ($ page )
262277 ->willThrowException (new \Exception ('Error message. ' ));
0 commit comments