/** * Validate post data * * @param array $pageData * @param \Magento\Cms\Model\Page $page * @param bool $error * @param array $messages * @return void */ protected function validatePost(array $pageData, \Magento\Cms\Model\Page $page, &$error, array &$messages) { if (!($this->dataProcessor->validate($pageData) && $this->dataProcessor->validateRequireEntry($pageData))) { $error = true; foreach ($this->messageManager->getMessages(true)->getItems() as $error) { $messages[] = $this->getErrorWithPageId($page, $error->getText()); } } }
public function prepareMocksForTestExecute() { $postData = [1 => ['title' => '404 Not Found', 'identifier' => 'no-route', 'custom_theme' => '1', 'custom_root_template' => '2']]; $this->request->expects($this->any())->method('getParam')->willReturnMap([['isAjax', null, true], ['items', [], $postData]]); $this->pageRepository->expects($this->once())->method('getById')->with(1)->willReturn($this->cmsPage); $this->dataProcessor->expects($this->once())->method('filter')->with($postData[1])->willReturnArgument(0); $this->dataProcessor->expects($this->once())->method('validate')->with($postData[1])->willReturn(false); $this->messageManager->expects($this->once())->method('getMessages')->with(true)->willReturn($this->messageCollection); $this->messageCollection->expects($this->once())->method('getItems')->willReturn([$this->message]); $this->message->expects($this->once())->method('getText')->willReturn('Error message'); $this->cmsPage->expects($this->atLeastOnce())->method('getId')->willReturn('1'); $this->cmsPage->expects($this->atLeastOnce())->method('getData')->willReturn(['layout' => '1column', 'identifier' => 'test-identifier']); $this->cmsPage->expects($this->once())->method('setData')->with(['layout' => '1column', 'title' => '404 Not Found', 'identifier' => 'no-route', 'custom_theme' => '1', 'custom_root_template' => '2']); $this->jsonFactory->expects($this->once())->method('create')->willReturn($this->resultJson); }
/** * Save action * * @return void */ public function execute() { // check if data sent $data = $this->getRequest()->getPost(); if ($data) { $data = $this->dataProcessor->filter($data); //init model and set data $model = $this->_objectManager->create('Magento\\Cms\\Model\\Page'); $id = $this->getRequest()->getParam('page_id'); if ($id) { $model->load($id); } $model->setData($data); $this->_eventManager->dispatch('cms_page_prepare_save', array('page' => $model, 'request' => $this->getRequest())); //validating if (!$this->dataProcessor->validate($data)) { $this->_redirect('*/*/edit', array('page_id' => $model->getId(), '_current' => true)); return; } // try to save it try { // save the data $model->save(); // display success message $this->messageManager->addSuccess(__('The page has been saved.')); // clear previously saved data from session $this->_objectManager->get('Magento\\Backend\\Model\\Session')->setFormData(false); // check if 'Save and Continue' if ($this->getRequest()->getParam('back')) { $this->_redirect('*/*/edit', array('page_id' => $model->getId(), '_current' => true)); return; } // go to grid $this->_redirect('*/*/'); return; } catch (\Magento\Framework\Model\Exception $e) { $this->messageManager->addError($e->getMessage()); } catch (\Exception $e) { $this->messageManager->addException($e, __('Something went wrong while saving the page.')); } $this->_getSession()->setFormData($data); $this->_redirect('*/*/edit', array('page_id' => $this->getRequest()->getParam('page_id'))); return; } $this->_redirect('*/*/'); }
/** * Save action * * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @return \Magento\Framework\Controller\ResultInterface */ public function execute() { $data = $this->getRequest()->getPostValue(); /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */ $resultRedirect = $this->resultRedirectFactory->create(); if ($data) { $data = $this->dataProcessor->filter($data); if (isset($data['is_active']) && $data['is_active'] === 'true') { $data['is_active'] = Page::STATUS_ENABLED; } if (empty($data['page_id'])) { $data['page_id'] = null; } $model = $this->_objectManager->create('Magento\\Cms\\Model\\Page'); $id = $this->getRequest()->getParam('page_id'); if ($id) { $model->load($id); } $model->setData($data); $this->_eventManager->dispatch('cms_page_prepare_save', ['page' => $model, 'request' => $this->getRequest()]); if (!$this->dataProcessor->validate($data)) { return $resultRedirect->setPath('*/*/edit', ['page_id' => $model->getId(), '_current' => true]); } try { $model->save(); $this->messageManager->addSuccess(__('You saved this page.')); $this->_objectManager->get('Magento\\Backend\\Model\\Session')->setFormData(false); if ($this->getRequest()->getParam('back')) { return $resultRedirect->setPath('*/*/edit', ['page_id' => $model->getId(), '_current' => true]); } return $resultRedirect->setPath('*/*/'); } catch (\Magento\Framework\Exception\LocalizedException $e) { $this->messageManager->addError($e->getMessage()); } catch (\RuntimeException $e) { $this->messageManager->addError($e->getMessage()); } catch (\Exception $e) { $this->messageManager->addException($e, __('Something went wrong while saving the page.')); } $this->_getSession()->setFormData($data); return $resultRedirect->setPath('*/*/edit', ['page_id' => $this->getRequest()->getParam('page_id')]); } return $resultRedirect->setPath('*/*/'); }
public function testSaveActionThrowsException() { $this->requestMock->expects($this->any())->method('getPostValue')->willReturn(['page_id' => $this->pageId]); $this->requestMock->expects($this->atLeastOnce())->method('getParam')->willReturnMap([['page_id', null, $this->pageId], ['back', null, true]]); $this->dataProcessorMock->expects($this->any())->method('filter')->willReturnArgument(0); $this->objectManagerMock->expects($this->atLeastOnce())->method('create')->with($this->equalTo('Magento\\Cms\\Model\\Page'))->willReturn($this->pageMock); $this->pageMock->expects($this->any())->method('load')->willReturnSelf(); $this->pageMock->expects($this->any())->method('getId')->willReturn(true); $this->pageMock->expects($this->once())->method('setData'); $this->pageMock->expects($this->once())->method('save')->willThrowException(new \Exception('Error message.')); $this->messageManagerMock->expects($this->never())->method('addSuccess'); $this->messageManagerMock->expects($this->once())->method('addException'); $this->dataPersistorMock->expects($this->any())->method('set')->with('cms_page', ['page_id' => $this->pageId]); $this->resultRedirect->expects($this->atLeastOnce())->method('setPath')->with('*/*/edit', ['page_id' => $this->pageId])->willReturnSelf(); $this->assertSame($this->resultRedirect, $this->saveController->execute()); }
public function testValidateRequireEntry() { $postData = ['title' => '']; $this->messageManager->expects($this->once())->method('addError')->with(__('To apply changes you should fill in hidden required "%1" field', 'Page Title')); $this->assertFalse($this->postDataProcessor->validateRequireEntry($postData)); }
public function testFilter() { $this->assertSame(['key' => 'value'], $this->postDataProcessor->filter(['key' => 'value'])); }