1
1
<?php
2
+
2
3
/**
3
4
* Copyright © Magento, Inc. All rights reserved.
4
5
* See COPYING.txt for license details.
7
8
8
9
namespace Magento \Cms \Test \Unit \Controller \Adminhtml \Page ;
9
10
11
+ use Magento \Backend \App \Action \Context ;
10
12
use Magento \Backend \Model \View \Result \Redirect ;
11
13
use Magento \Backend \Model \View \Result \RedirectFactory ;
12
14
use Magento \Cms \Api \PageRepositoryInterface ;
18
20
use Magento \Framework \App \RequestInterface ;
19
21
use Magento \Framework \Exception \NoSuchEntityException ;
20
22
use Magento \Framework \Message \ManagerInterface ;
21
- use Magento \Framework \TestFramework \Unit \Helper \ObjectManager ;
22
23
use PHPUnit \Framework \MockObject \MockObject ;
23
24
use PHPUnit \Framework \TestCase ;
24
25
@@ -82,13 +83,14 @@ class SaveTest extends TestCase
82
83
*/
83
84
private $ pageId = 1 ;
84
85
86
+ /**
87
+ * @inheirtDoc
88
+ */
85
89
protected function setUp (): void
86
90
{
87
- $ objectManager = new ObjectManager ($ this );
88
-
89
91
$ this ->resultRedirectFactory = $ this ->getMockBuilder (RedirectFactory::class)
90
92
->disableOriginalConstructor ()
91
- ->setMethods (['create ' ])
93
+ ->onlyMethods (['create ' ])
92
94
->getMock ();
93
95
$ this ->resultRedirect = $ this ->getMockBuilder (Redirect::class)
94
96
->disableOriginalConstructor ()
@@ -98,7 +100,7 @@ protected function setUp(): void
98
100
->willReturn ($ this ->resultRedirect );
99
101
$ this ->dataProcessorMock = $ this ->getMockBuilder (
100
102
PostDataProcessor::class
101
- )->setMethods (['filter ' ])->disableOriginalConstructor ()
103
+ )->onlyMethods (['filter ' ])->disableOriginalConstructor ()
102
104
->getMock ();
103
105
$ this ->dataPersistorMock = $ this ->getMockBuilder (DataPersistorInterface::class)
104
106
->getMock ();
@@ -108,27 +110,28 @@ protected function setUp(): void
108
110
$ this ->messageManagerMock = $ this ->getMockBuilder (ManagerInterface::class)
109
111
->getMockForAbstractClass ();
110
112
$ this ->eventManagerMock = $ this ->getMockBuilder (\Magento \Framework \Event \ManagerInterface::class)
111
- ->setMethods (['dispatch ' ])
113
+ ->onlyMethods (['dispatch ' ])
112
114
->getMockForAbstractClass ();
113
115
$ this ->pageFactory = $ this ->getMockBuilder (PageFactory::class)
114
116
->disableOriginalConstructor ()
115
- ->setMethods (['create ' ])
117
+ ->onlyMethods (['create ' ])
116
118
->getMock ();
117
119
$ this ->pageRepository = $ this ->getMockBuilder (PageRepositoryInterface::class)
118
120
->disableOriginalConstructor ()
119
121
->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
132
135
);
133
136
}
134
137
@@ -140,7 +143,7 @@ public function testSaveAction()
140
143
'stores ' => ['0 ' ],
141
144
'is_active ' => true ,
142
145
'content ' => '"><script>alert("cookie: "+document.cookie)</script> ' ,
143
- 'back ' => 'close '
146
+ 'back ' => 'close ' ,
144
147
];
145
148
146
149
$ filteredPostData = [
@@ -149,7 +152,7 @@ public function testSaveAction()
149
152
'stores ' => ['0 ' ],
150
153
'is_active ' => true ,
151
154
'content ' => '"><script>alert("cookie: "+document.cookie)</script> ' ,
152
- 'back ' => 'close '
155
+ 'back ' => 'close ' ,
153
156
];
154
157
155
158
$ this ->dataProcessorMock ->expects ($ this ->any ())
@@ -236,7 +239,7 @@ public function testSaveAndContinue()
236
239
'stores ' => ['0 ' ],
237
240
'is_active ' => true ,
238
241
'content ' => '"><script>alert("cookie: "+document.cookie)</script> ' ,
239
- 'back ' => 'continue '
242
+ 'back ' => 'continue ' ,
240
243
];
241
244
$ this ->requestMock ->expects ($ this ->any ())->method ('getPostValue ' )->willReturn ($ postData );
242
245
$ this ->requestMock ->expects ($ this ->atLeastOnce ())
@@ -304,12 +307,13 @@ public function testSaveActionThrowsException()
304
307
$ this ->pageRepository ->expects ($ this ->once ())->method ('getById ' )->with ($ this ->pageId )->willReturn ($ page );
305
308
$ page ->expects ($ this ->once ())->method ('setData ' );
306
309
$ this ->pageRepository ->expects ($ this ->once ())->method ('save ' )->with ($ page )
307
- ->willThrowException (new \Exception ('Error message. ' ));
310
+ ->willThrowException (new \Error ('Error message. ' ));
308
311
309
312
$ this ->messageManagerMock ->expects ($ this ->never ())
310
313
->method ('addSuccessMessage ' );
311
314
$ this ->messageManagerMock ->expects ($ this ->once ())
312
- ->method ('addExceptionMessage ' );
315
+ ->method ('addErrorMessage ' )
316
+ ->with ('Something went wrong while saving the page. ' );
313
317
314
318
$ this ->dataPersistorMock ->expects ($ this ->any ())
315
319
->method ('set ' )
@@ -318,7 +322,7 @@ public function testSaveActionThrowsException()
318
322
[
319
323
'page_id ' => $ this ->pageId ,
320
324
'layout_update_xml ' => null ,
321
- 'custom_layout_update_xml ' => null
325
+ 'custom_layout_update_xml ' => null ,
322
326
]
323
327
);
324
328
0 commit comments