diff --git a/app/code/Magento/Cms/Test/Unit/Controller/Adminhtml/Page/SaveTest.php b/app/code/Magento/Cms/Test/Unit/Controller/Adminhtml/Page/SaveTest.php index afe164a72d890..789c67b825a2b 100644 --- a/app/code/Magento/Cms/Test/Unit/Controller/Adminhtml/Page/SaveTest.php +++ b/app/code/Magento/Cms/Test/Unit/Controller/Adminhtml/Page/SaveTest.php @@ -81,7 +81,9 @@ protected function setUp() ->willReturn($this->resultRedirect); $this->dataProcessorMock = $this->getMockBuilder( \Magento\Cms\Controller\Adminhtml\Page\PostDataProcessor::class - )->setMethods(['filter'])->disableOriginalConstructor()->getMock(); + )->disableOriginalConstructor() + ->setMethods(['filter']) + ->getMock(); $this->dataPersistorMock = $this->getMockBuilder(\Magento\Framework\App\Request\DataPersistorInterface::class) ->getMock(); $this->requestMock = $this->getMockBuilder(\Magento\Framework\App\RequestInterface::class) @@ -132,34 +134,19 @@ public function testSaveAction() 'content' => '"><script>alert("cookie: "+document.cookie)</script>' ]; + $params = [ + ['page_id', null, $this->pageId], + ['back', null, false], + ]; + $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); - - $page->expects($this->any()) - ->method('load') - ->willReturnSelf(); - $page->expects($this->any()) - ->method('getId') - ->willReturn(true); - $page->expects($this->once())->method('setData'); + $this->processRequest($postData, $params); + /** @var \Magento\Cms\Model\Page|\PHPUnit_Framework_MockObject_MockObject $cmsPage */ + $page = $this->getPageMock(); $this->pageRepository->expects($this->once())->method('save')->with($page); $this->dataPersistorMock->expects($this->any()) @@ -184,39 +171,25 @@ public function testSaveActionWithoutData() public function testSaveAndContinue() { - $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], - ] - ); + $postData = ['page_id' => $this->pageId]; + $params = [ + ['page_id', null, $this->pageId], + ['back', null, true], + ]; + + $this->processRequest($postData, $params); $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); + /** @var \Magento\Cms\Model\Page|\PHPUnit_Framework_MockObject_MockObject $cmsPage */ + $page = $this->getPageMock(); - $page->expects($this->any()) - ->method('load') - ->willReturnSelf(); - $page->expects($this->any()) - ->method('getId') - ->willReturn(true); - $page->expects($this->once())->method('setData'); $this->pageRepository->expects($this->once())->method('save')->with($page); $this->messageManagerMock->expects($this->once()) ->method('addSuccess') ->with(__('You saved the page.')); - $this->dataPersistorMock->expects($this->any()) ->method('clear') ->with('cms_page'); @@ -231,33 +204,19 @@ public function testSaveAndContinue() 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], - ] - ); + $postData = ['page_id' => $this->pageId]; + $params = [ + ['page_id', null, $this->pageId], + ['back', null, true], + ]; + + $this->processRequest($postData, $params); $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); - - $page->expects($this->any()) - ->method('load') - ->willReturnSelf(); - $page->expects($this->any()) - ->method('getId') - ->willReturn(true); - $page->expects($this->once())->method('setData'); + /** @var \Magento\Cms\Model\Page|\PHPUnit_Framework_MockObject_MockObject $cmsPage */ + $page = $this->getPageMock(); $this->pageRepository->expects($this->once()) ->method('save') ->with($page) @@ -279,4 +238,44 @@ public function testSaveActionThrowsException() $this->assertSame($this->resultRedirect, $this->saveController->execute()); } + + /** + * Create Cms Page Mock. + * + * @return \Magento\Cms\Model\Page|\PHPUnit_Framework_MockObject_MockObject + */ + private function getPageMock() + { + $page = $this->getMockBuilder(\Magento\Cms\Model\Page::class) + ->disableOriginalConstructor() + ->getMock(); + $this->pageFactory->expects($this->atLeastOnce()) + ->method('create') + ->willReturn($page); + + $page->expects($this->any()) + ->method('load') + ->willReturnSelf(); + $page->expects($this->any()) + ->method('getId') + ->willReturn(true); + $page->expects($this->once())->method('setData'); + + return $page; + } + + /** + * Process save page action request. + * + * @param array $postData + * @param array $params + * @return void + */ + private function processRequest($postData, $params) + { + $this->requestMock->expects($this->any())->method('getPostValue')->willReturn($postData); + $this->requestMock->expects($this->atLeastOnce()) + ->method('getParam') + ->willReturnMap($params); + } } diff --git a/app/code/Magento/CmsUrlRewrite/Test/Unit/Model/CmsPageUrlRewriteGeneratorTest.php b/app/code/Magento/CmsUrlRewrite/Test/Unit/Model/CmsPageUrlRewriteGeneratorTest.php index 03c0b24a4b9f9..b13ddb79b8529 100644 --- a/app/code/Magento/CmsUrlRewrite/Test/Unit/Model/CmsPageUrlRewriteGeneratorTest.php +++ b/app/code/Magento/CmsUrlRewrite/Test/Unit/Model/CmsPageUrlRewriteGeneratorTest.php @@ -68,7 +68,6 @@ public function testGenerateForAllStores() $cmsPage = $this->getMockBuilder(\Magento\Cms\Model\Page::class) ->disableOriginalConstructor() ->getMock(); - $cmsPage->expects($this->any())->method('getStores')->willReturn($initializesStores); $store = $this->getMockBuilder(\Magento\Store\Api\Data\StoreInterface::class) ->setMethods(['getStoreId']) @@ -80,7 +79,9 @@ public function testGenerateForAllStores() $this->urlRewriteFactory->expects($this->any())->method('create')->willReturn($urlRewrite); $cmsPage->expects($this->any())->method('getId')->willReturn($cmsPageId); $cmsPage->expects($this->any())->method('getIdentifier')->willReturn('request_path'); - $this->urlPathGenerator->expects($this->any())->method('getCanonicalUrlPath')->with($cmsPage) + $this->urlPathGenerator->expects($this->any()) + ->method('getCanonicalUrlPath') + ->with($cmsPage) ->willReturn('cms/page/view/page_id/' . $cmsPageId); $urls = $this->urlRewriteGenerator->generate($cmsPage); @@ -104,12 +105,14 @@ public function testGenerateForSpecificStores() $secondStore = $this->getMockBuilder(\Magento\Store\Api\Data\StoreInterface::class) ->setMethods(['getStoreId']) ->getMockForAbstractClass(); - $this->storeManager->expects($this->any())->method('getStores')->willReturn( - [ - 1 => $firstStore, - 2 => $secondStore - ] - ); + $this->storeManager->expects($this->any()) + ->method('getStores') + ->willReturn( + [ + 1 => $firstStore, + 2 => $secondStore, + ] + ); $firstStore->expects($this->any())->method('getStoreId')->willReturn($initializesStores[0]); $secondStore->expects($this->any())->method('getStoreId')->willReturn($initializesStores[1]); @@ -128,7 +131,7 @@ public function testGenerateForSpecificStores() $this->assertEquals( [ $initializesStores[0], - $initializesStores[1] + $initializesStores[1], ], [ $urls[0]->getStoreId(),