Skip to content

Commit 5f6781f

Browse files
author
Stanislav Idolov
authored
ENGCOM-675: [Port 2.3-develop] Load CMS Page using repository in save action #13815
2 parents 02770a0 + cd5cf94 commit 5f6781f

File tree

2 files changed

+36
-23
lines changed
  • app/code/Magento/Cms

2 files changed

+36
-23
lines changed

app/code/Magento/Cms/Controller/Adminhtml/Page/Save.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ class Save extends \Magento\Backend\App\Action
4646
* @param DataPersistorInterface $dataPersistor
4747
* @param \Magento\Cms\Model\PageFactory $pageFactory
4848
* @param \Magento\Cms\Api\PageRepositoryInterface $pageRepository
49-
*
5049
*/
5150
public function __construct(
5251
Action\Context $context,
@@ -90,11 +89,10 @@ public function execute()
9089

9190
$id = $this->getRequest()->getParam('page_id');
9291
if ($id) {
93-
$model->load($id);
94-
if (!$model->getId()) {
92+
try {
93+
$model = $this->pageRepository->getById($id);
94+
} catch (LocalizedException $e) {
9595
$this->messageManager->addErrorMessage(__('This page no longer exists.'));
96-
/** \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
97-
$resultRedirect = $this->resultRedirectFactory->create();
9896
return $resultRedirect->setPath('*/*/');
9997
}
10098
}

app/code/Magento/Cms/Test/Unit/Controller/Adminhtml/Page/SaveTest.php

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)