11<?php
2+
23/**
34 * Copyright © Magento, Inc. All rights reserved.
45 * See COPYING.txt for license details.
78
89namespace Magento \Cms \Test \Unit \Controller \Adminhtml \Page ;
910
11+ use Magento \Backend \App \Action \Context ;
1012use Magento \Backend \Model \View \Result \Redirect ;
1113use Magento \Backend \Model \View \Result \RedirectFactory ;
1214use Magento \Cms \Api \PageRepositoryInterface ;
1820use Magento \Framework \App \RequestInterface ;
1921use Magento \Framework \Exception \NoSuchEntityException ;
2022use Magento \Framework \Message \ManagerInterface ;
21- use Magento \Framework \TestFramework \Unit \Helper \ObjectManager ;
2223use PHPUnit \Framework \MockObject \MockObject ;
2324use PHPUnit \Framework \TestCase ;
2425
@@ -82,13 +83,14 @@ class SaveTest extends TestCase
8283 */
8384 private $ pageId = 1 ;
8485
86+ /**
87+ * @inheirtDoc
88+ */
8589 protected function setUp (): void
8690 {
87- $ objectManager = new ObjectManager ($ this );
88-
8991 $ this ->resultRedirectFactory = $ this ->getMockBuilder (RedirectFactory::class)
9092 ->disableOriginalConstructor ()
91- ->setMethods (['create ' ])
93+ ->onlyMethods (['create ' ])
9294 ->getMock ();
9395 $ this ->resultRedirect = $ this ->getMockBuilder (Redirect::class)
9496 ->disableOriginalConstructor ()
@@ -98,7 +100,7 @@ protected function setUp(): void
98100 ->willReturn ($ this ->resultRedirect );
99101 $ this ->dataProcessorMock = $ this ->getMockBuilder (
100102 PostDataProcessor::class
101- )->setMethods (['filter ' ])->disableOriginalConstructor ()
103+ )->onlyMethods (['filter ' ])->disableOriginalConstructor ()
102104 ->getMock ();
103105 $ this ->dataPersistorMock = $ this ->getMockBuilder (DataPersistorInterface::class)
104106 ->getMock ();
@@ -108,27 +110,28 @@ protected function setUp(): void
108110 $ this ->messageManagerMock = $ this ->getMockBuilder (ManagerInterface::class)
109111 ->getMockForAbstractClass ();
110112 $ this ->eventManagerMock = $ this ->getMockBuilder (\Magento \Framework \Event \ManagerInterface::class)
111- ->setMethods (['dispatch ' ])
113+ ->onlyMethods (['dispatch ' ])
112114 ->getMockForAbstractClass ();
113115 $ this ->pageFactory = $ this ->getMockBuilder (PageFactory::class)
114116 ->disableOriginalConstructor ()
115- ->setMethods (['create ' ])
117+ ->onlyMethods (['create ' ])
116118 ->getMock ();
117119 $ this ->pageRepository = $ this ->getMockBuilder (PageRepositoryInterface::class)
118120 ->disableOriginalConstructor ()
119121 ->getMockForAbstractClass ();
120- $ this ->saveController = $ objectManager ->getObject (
121- Save::class,
122- [
123- 'request ' => $ this ->requestMock ,
124- 'messageManager ' => $ this ->messageManagerMock ,
125- 'eventManager ' => $ this ->eventManagerMock ,
126- 'resultRedirectFactory ' => $ this ->resultRedirectFactory ,
127- 'dataProcessor ' => $ this ->dataProcessorMock ,
128- 'dataPersistor ' => $ this ->dataPersistorMock ,
129- 'pageFactory ' => $ this ->pageFactory ,
130- 'pageRepository ' => $ this ->pageRepository
131- ]
122+ $ context = $ this ->getMockBuilder (Context::class)
123+ ->disableOriginalConstructor ()
124+ ->getMock ();
125+ $ context ->method ('getRequest ' )->willReturn ($ this ->requestMock );
126+ $ context ->method ('getMessageManager ' )->willReturn ($ this ->messageManagerMock );
127+ $ context ->method ('getEventManager ' )->willReturn ($ this ->eventManagerMock );
128+ $ context ->method ('getResultRedirectFactory ' )->willReturn ($ this ->resultRedirectFactory );
129+ $ this ->saveController = new Save (
130+ $ context ,
131+ $ this ->dataProcessorMock ,
132+ $ this ->dataPersistorMock ,
133+ $ this ->pageFactory ,
134+ $ this ->pageRepository
132135 );
133136 }
134137
@@ -140,7 +143,7 @@ public function testSaveAction()
140143 'stores ' => ['0 ' ],
141144 'is_active ' => true ,
142145 'content ' => '"><script>alert("cookie: "+document.cookie)</script> ' ,
143- 'back ' => 'close '
146+ 'back ' => 'close ' ,
144147 ];
145148
146149 $ filteredPostData = [
@@ -149,7 +152,7 @@ public function testSaveAction()
149152 'stores ' => ['0 ' ],
150153 'is_active ' => true ,
151154 'content ' => '"><script>alert("cookie: "+document.cookie)</script> ' ,
152- 'back ' => 'close '
155+ 'back ' => 'close ' ,
153156 ];
154157
155158 $ this ->dataProcessorMock ->expects ($ this ->any ())
@@ -236,7 +239,7 @@ public function testSaveAndContinue()
236239 'stores ' => ['0 ' ],
237240 'is_active ' => true ,
238241 'content ' => '"><script>alert("cookie: "+document.cookie)</script> ' ,
239- 'back ' => 'continue '
242+ 'back ' => 'continue ' ,
240243 ];
241244 $ this ->requestMock ->expects ($ this ->any ())->method ('getPostValue ' )->willReturn ($ postData );
242245 $ this ->requestMock ->expects ($ this ->atLeastOnce ())
@@ -304,12 +307,13 @@ public function testSaveActionThrowsException()
304307 $ this ->pageRepository ->expects ($ this ->once ())->method ('getById ' )->with ($ this ->pageId )->willReturn ($ page );
305308 $ page ->expects ($ this ->once ())->method ('setData ' );
306309 $ this ->pageRepository ->expects ($ this ->once ())->method ('save ' )->with ($ page )
307- ->willThrowException (new \Exception ('Error message. ' ));
310+ ->willThrowException (new \Error ('Error message. ' ));
308311
309312 $ this ->messageManagerMock ->expects ($ this ->never ())
310313 ->method ('addSuccessMessage ' );
311314 $ this ->messageManagerMock ->expects ($ this ->once ())
312- ->method ('addExceptionMessage ' );
315+ ->method ('addErrorMessage ' )
316+ ->with ('Something went wrong while saving the page. ' );
313317
314318 $ this ->dataPersistorMock ->expects ($ this ->any ())
315319 ->method ('set ' )
@@ -318,7 +322,7 @@ public function testSaveActionThrowsException()
318322 [
319323 'page_id ' => $ this ->pageId ,
320324 'layout_update_xml ' => null ,
321- 'custom_layout_update_xml ' => null
325+ 'custom_layout_update_xml ' => null ,
322326 ]
323327 );
324328
0 commit comments