Skip to content

Commit

Permalink
MAGETWO-58383: [Backport] - [GitHub] Saving CMS page does not create …
Browse files Browse the repository at this point in the history
…URL rewrite in Magento 2.1.0 with single-store mode #5923 - for 2.1
  • Loading branch information
StasKozar committed Feb 9, 2018
1 parent 1ac2cb9 commit 3296659
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 79 deletions.
139 changes: 69 additions & 70 deletions app/code/Magento/Cms/Test/Unit/Controller/Adminhtml/Page/SaveTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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())
Expand All @@ -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');
Expand All @@ -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)
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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'])
Expand All @@ -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);
Expand All @@ -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]);

Expand All @@ -128,7 +131,7 @@ public function testGenerateForSpecificStores()
$this->assertEquals(
[
$initializesStores[0],
$initializesStores[1]
$initializesStores[1],
],
[
$urls[0]->getStoreId(),
Expand Down

0 comments on commit 3296659

Please sign in to comment.