resultRedirectFactory = $this->getMockBuilder(\Magento\Backend\Model\View\Result\RedirectFactory::class) ->disableOriginalConstructor() ->setMethods(['create']) ->getMock(); $this->resultRedirect = $this->getMockBuilder(\Magento\Backend\Model\View\Result\Redirect::class) ->disableOriginalConstructor() ->getMock(); $this->resultRedirectFactory->expects($this->atLeastOnce()) ->method('create') ->willReturn($this->resultRedirect); $this->dataProcessorMock = $this->getMockBuilder( \Magento\Cms\Controller\Adminhtml\Page\PostDataProcessor::class )->setMethods(['filter'])->disableOriginalConstructor()->getMock(); $this->dataPersistorMock = $this->getMockBuilder(\Magento\Framework\App\Request\DataPersistorInterface::class) ->getMock(); $this->requestMock = $this->getMockBuilder(\Magento\Framework\App\RequestInterface::class) ->setMethods(['getParam', 'getPostValue']) ->getMockForAbstractClass(); $this->messageManagerMock = $this->getMockBuilder(\Magento\Framework\Message\ManagerInterface::class) ->getMockForAbstractClass(); $this->eventManagerMock = $this->getMockBuilder(\Magento\Framework\Event\ManagerInterface::class) ->setMethods(['dispatch']) ->getMockForAbstractClass(); $this->pageFactory = $this->getMockBuilder(\Magento\Cms\Model\PageFactory::class) ->disableOriginalConstructor() ->setMethods(['create']) ->getMock(); $this->pageRepository = $this->getMockBuilder(\Magento\Cms\Api\PageRepositoryInterface::class) ->disableOriginalConstructor() ->getMockForAbstractClass(); $this->saveController = $objectManager->getObject( \Magento\Cms\Controller\Adminhtml\Page\Save::class, [ 'request' => $this->requestMock, 'messageManager' => $this->messageManagerMock, 'eventManager' => $this->eventManagerMock, 'resultRedirectFactory' => $this->resultRedirectFactory, 'dataProcessor' => $this->dataProcessorMock, 'dataPersistor' => $this->dataPersistorMock, 'pageFactory' => $this->pageFactory, 'pageRepository' => $this->pageRepository ] ); } public function testSaveAction() { $postData = [ 'title' => '">;', 'identifier' => 'unique_title_123', 'stores' => ['0'], 'is_active' => true, 'content' => '">', 'back' => 'close' ]; $filteredPostData = [ 'title' => '"><img src=y onerror=prompt(document.domain)>;', 'identifier' => 'unique_title_123', 'stores' => ['0'], 'is_active' => true, 'content' => '"><script>alert("cookie: "+document.cookie)</script>', 'back' => 'close' ]; $this->dataProcessorMock->expects($this->any()) ->method('filter') ->with($postData) ->willReturn($filteredPostData); $this->requestMock->expects($this->any())->method('getPostValue')->willReturn($postData); $this->requestMock->expects($this->atLeastOnce()) ->method('getParam') ->willReturnMap( [ ['page_id', null, $this->pageId], ['back', null, false], ] ); $page = $this->getMockBuilder(\Magento\Cms\Model\Page::class) ->disableOriginalConstructor() ->getMock(); $this->pageFactory->expects($this->atLeastOnce()) ->method('create') ->willReturn($page); $this->pageRepository->expects($this->once())->method('getById')->with($this->pageId)->willReturn($page); $page->expects($this->once())->method('setData'); $this->pageRepository->expects($this->once())->method('save')->with($page); $this->dataPersistorMock->expects($this->any()) ->method('clear') ->with('cms_page'); $this->messageManagerMock->expects($this->once()) ->method('addSuccessMessage') ->with(__('You saved the page.')); $this->resultRedirect->expects($this->atLeastOnce())->method('setPath')->with('*/*/') ->willReturnSelf(); $this->assertSame($this->resultRedirect, $this->saveController->execute()); } public function testSaveActionWithoutData() { $this->requestMock->expects($this->any())->method('getPostValue')->willReturn(false); $this->resultRedirect->expects($this->atLeastOnce())->method('setPath')->with('*/*/') ->willReturnSelf(); $this->assertSame($this->resultRedirect, $this->saveController->execute()); } public function testSaveActionNoId() { $this->requestMock->expects($this->any())->method('getPostValue')->willReturn(['page_id' => 1]); $this->requestMock->expects($this->atLeastOnce()) ->method('getParam') ->willReturnMap( [ ['page_id', null, 1], ['back', null, 'close'], ] ); $page = $this->getMockBuilder(\Magento\Cms\Model\Page::class) ->disableOriginalConstructor() ->getMock(); $this->pageFactory->expects($this->atLeastOnce()) ->method('create') ->willReturn($page); $this->pageRepository->expects($this->once()) ->method('getById') ->with($this->pageId) ->willThrowException(new \Magento\Framework\Exception\NoSuchEntityException(__('Error message'))); $this->messageManagerMock->expects($this->once()) ->method('addErrorMessage') ->with(__('This page no longer exists.')); $this->resultRedirect->expects($this->atLeastOnce())->method('setPath')->with('*/*/') ->willReturnSelf(); $this->assertSame($this->resultRedirect, $this->saveController->execute()); } public function testSaveAndContinue() { $postData = [ 'title' => '">;', 'identifier' => 'unique_title_123', 'stores' => ['0'], 'is_active' => true, 'content' => '">', 'back' => 'continue' ]; $this->requestMock->expects($this->any())->method('getPostValue')->willReturn($postData); $this->requestMock->expects($this->atLeastOnce()) ->method('getParam') ->willReturnMap( [ ['page_id', null, $this->pageId], ['back', null, 'continue'], ] ); $this->dataProcessorMock->expects($this->any()) ->method('filter') ->willReturnArgument(0); $page = $this->getMockBuilder(\Magento\Cms\Model\Page::class) ->disableOriginalConstructor() ->getMock(); $this->pageFactory->expects($this->atLeastOnce()) ->method('create') ->willReturn($page); $this->pageRepository->expects($this->once())->method('getById')->with($this->pageId)->willReturn($page); $page->expects($this->once())->method('setData'); $this->pageRepository->expects($this->once())->method('save')->with($page); $this->messageManagerMock->expects($this->once()) ->method('addSuccessMessage') ->with(__('You saved the page.')); $this->dataPersistorMock->expects($this->any()) ->method('clear') ->with('cms_page'); $this->resultRedirect->expects($this->atLeastOnce()) ->method('setPath') ->with('*/*/edit', ['page_id' => $this->pageId]) ->willReturnSelf(); $this->assertSame($this->resultRedirect, $this->saveController->execute()); } 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); $page = $this->getMockBuilder(\Magento\Cms\Model\Page::class) ->disableOriginalConstructor() ->getMock(); $this->pageFactory->expects($this->atLeastOnce()) ->method('create') ->willReturn($page); $this->pageRepository->expects($this->once())->method('getById')->with($this->pageId)->willReturn($page); $page->expects($this->once())->method('setData'); $this->pageRepository->expects($this->once())->method('save')->with($page) ->willThrowException(new \Exception('Error message.')); $this->messageManagerMock->expects($this->never()) ->method('addSuccessMessage'); $this->messageManagerMock->expects($this->once()) ->method('addExceptionMessage'); $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()); } }