|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © 2015 Magento. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +namespace Magento\Cms\Test\Unit\Controller\Adminhtml\Page; |
| 7 | + |
| 8 | +class DeleteTest extends \PHPUnit_Framework_TestCase |
| 9 | +{ |
| 10 | + /** @var \Magento\Cms\Controller\Adminhtml\Page\Delete */ |
| 11 | + protected $deleteController; |
| 12 | + |
| 13 | + /** @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager */ |
| 14 | + protected $objectManager; |
| 15 | + |
| 16 | + /** @var \Magento\Backend\App\Action\Context|\PHPUnit_Framework_MockObject_MockObject */ |
| 17 | + protected $contextMock; |
| 18 | + |
| 19 | + /** @var \Magento\Framework\Controller\ResultInterface|\PHPUnit_Framework_MockObject_MockObject */ |
| 20 | + protected $resultRedirectFactoryMock; |
| 21 | + |
| 22 | + /** @var \Magento\Backend\Model\View\Result\Redirect|\PHPUnit_Framework_MockObject_MockObject */ |
| 23 | + protected $resultRedirectMock; |
| 24 | + |
| 25 | + /** @var \Magento\Framework\Message\ManagerInterface|\PHPUnit_Framework_MockObject_MockObject */ |
| 26 | + protected $messageManagerMock; |
| 27 | + |
| 28 | + /** @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject */ |
| 29 | + protected $requestMock; |
| 30 | + |
| 31 | + /** @var \Magento\Framework\ObjectManager\ObjectManager|\PHPUnit_Framework_MockObject_MockObject */ |
| 32 | + protected $objectManagerMock; |
| 33 | + |
| 34 | + /** @var \Magento\Cms\Model\Page|\PHPUnit_Framework_MockObject_MockObject $pageMock */ |
| 35 | + protected $pageMock; |
| 36 | + |
| 37 | + /** @var \Magento\Framework\Event\ManagerInterface|\PHPUnit_Framework_MockObject_MockObject */ |
| 38 | + protected $eventManagerMock; |
| 39 | + |
| 40 | + /** @var string */ |
| 41 | + protected $title = 'This is the title of the page.'; |
| 42 | + |
| 43 | + /** @var int */ |
| 44 | + protected $pageId = 1; |
| 45 | + |
| 46 | + protected function setUp() |
| 47 | + { |
| 48 | + $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); |
| 49 | + |
| 50 | + $this->messageManagerMock = $this->getMock('Magento\Framework\Message\ManagerInterface', [], [], '', false); |
| 51 | + |
| 52 | + $this->requestMock = $this->getMockForAbstractClass( |
| 53 | + 'Magento\Framework\App\RequestInterface', |
| 54 | + [], |
| 55 | + '', |
| 56 | + false, |
| 57 | + true, |
| 58 | + true, |
| 59 | + ['getParam'] |
| 60 | + ); |
| 61 | + |
| 62 | + $this->pageMock = $this->getMockBuilder('Magento\Cms\Model\Page') |
| 63 | + ->disableOriginalConstructor() |
| 64 | + ->setMethods(['load', 'delete', 'getTitle']) |
| 65 | + ->getMock(); |
| 66 | + |
| 67 | + $this->objectManagerMock = $this->getMockBuilder('Magento\Framework\ObjectManager\ObjectManager') |
| 68 | + ->disableOriginalConstructor() |
| 69 | + ->setMethods(['create']) |
| 70 | + ->getMock(); |
| 71 | + |
| 72 | + $this->resultRedirectMock = $this->getMockBuilder('Magento\Backend\Model\View\Result\Redirect') |
| 73 | + ->setMethods(['setPath']) |
| 74 | + ->disableOriginalConstructor() |
| 75 | + ->getMock(); |
| 76 | + |
| 77 | + $this->resultRedirectFactoryMock = $this->getMockBuilder('Magento\Backend\Model\View\Result\RedirectFactory') |
| 78 | + ->disableOriginalConstructor() |
| 79 | + ->setMethods(['create']) |
| 80 | + ->getMock(); |
| 81 | + $this->resultRedirectFactoryMock->expects($this->atLeastOnce()) |
| 82 | + ->method('create') |
| 83 | + ->willReturn($this->resultRedirectMock); |
| 84 | + |
| 85 | + $this->eventManagerMock = $this->getMock('Magento\Framework\Event\ManagerInterface', [], [], '', false); |
| 86 | + |
| 87 | + $this->contextMock = $this->getMock( |
| 88 | + '\Magento\Backend\App\Action\Context', |
| 89 | + [], |
| 90 | + [], |
| 91 | + '', |
| 92 | + false |
| 93 | + ); |
| 94 | + |
| 95 | + $this->contextMock->expects($this->any())->method('getRequest')->willReturn($this->requestMock); |
| 96 | + $this->contextMock->expects($this->any())->method('getMessageManager')->willReturn($this->messageManagerMock); |
| 97 | + $this->contextMock->expects($this->any())->method('getObjectManager')->willReturn($this->objectManagerMock); |
| 98 | + $this->contextMock->expects($this->any())->method('getEventManager')->willReturn($this->eventManagerMock); |
| 99 | + $this->contextMock->expects($this->any()) |
| 100 | + ->method('getResultRedirectFactory') |
| 101 | + ->willReturn($this->resultRedirectFactoryMock); |
| 102 | + |
| 103 | + $this->deleteController = $this->objectManager->getObject( |
| 104 | + 'Magento\Cms\Controller\Adminhtml\Page\Delete', |
| 105 | + [ |
| 106 | + 'context' => $this->contextMock, |
| 107 | + ] |
| 108 | + ); |
| 109 | + } |
| 110 | + |
| 111 | + public function testDeleteAction() |
| 112 | + { |
| 113 | + $this->requestMock->expects($this->once()) |
| 114 | + ->method('getParam') |
| 115 | + ->willReturn($this->pageId); |
| 116 | + |
| 117 | + $this->objectManagerMock->expects($this->once()) |
| 118 | + ->method('create') |
| 119 | + ->with('Magento\Cms\Model\Page') |
| 120 | + ->willReturn($this->pageMock); |
| 121 | + |
| 122 | + $this->pageMock->expects($this->once()) |
| 123 | + ->method('load') |
| 124 | + ->with($this->pageId); |
| 125 | + $this->pageMock->expects($this->once()) |
| 126 | + ->method('getTitle') |
| 127 | + ->willReturn($this->title); |
| 128 | + $this->pageMock->expects($this->once()) |
| 129 | + ->method('delete'); |
| 130 | + |
| 131 | + $this->messageManagerMock->expects($this->once()) |
| 132 | + ->method('addSuccess') |
| 133 | + ->with(__('The page has been deleted.')); |
| 134 | + $this->messageManagerMock->expects($this->never()) |
| 135 | + ->method('addError'); |
| 136 | + |
| 137 | + $this->eventManagerMock->expects($this->once()) |
| 138 | + ->method('dispatch') |
| 139 | + ->with( |
| 140 | + 'adminhtml_cmspage_on_delete', |
| 141 | + ['title' => $this->title, 'status' => 'success'] |
| 142 | + ); |
| 143 | + |
| 144 | + $this->resultRedirectMock->expects($this->once()) |
| 145 | + ->method('setPath') |
| 146 | + ->with('*/*/') |
| 147 | + ->willReturnSelf(); |
| 148 | + |
| 149 | + $this->assertSame($this->resultRedirectMock, $this->deleteController->execute()); |
| 150 | + } |
| 151 | + |
| 152 | + public function testDeleteActionNoId() |
| 153 | + { |
| 154 | + $this->requestMock->expects($this->once()) |
| 155 | + ->method('getParam') |
| 156 | + ->willReturn(null); |
| 157 | + |
| 158 | + $this->messageManagerMock->expects($this->once()) |
| 159 | + ->method('addError') |
| 160 | + ->with(__('We can\'t find a page to delete.')); |
| 161 | + $this->messageManagerMock->expects($this->never()) |
| 162 | + ->method('addSuccess'); |
| 163 | + |
| 164 | + $this->resultRedirectMock->expects($this->once()) |
| 165 | + ->method('setPath') |
| 166 | + ->with('*/*/') |
| 167 | + ->willReturnSelf(); |
| 168 | + |
| 169 | + $this->assertSame($this->resultRedirectMock, $this->deleteController->execute()); |
| 170 | + } |
| 171 | + |
| 172 | + public function testDeleteActionThrowsException() |
| 173 | + { |
| 174 | + $errorMsg = 'Can\'t delete the page'; |
| 175 | + |
| 176 | + $this->requestMock->expects($this->once()) |
| 177 | + ->method('getParam') |
| 178 | + ->willReturn($this->pageId); |
| 179 | + |
| 180 | + $this->objectManagerMock->expects($this->once()) |
| 181 | + ->method('create') |
| 182 | + ->with('Magento\Cms\Model\Page') |
| 183 | + ->willReturn($this->pageMock); |
| 184 | + |
| 185 | + $this->pageMock->expects($this->once()) |
| 186 | + ->method('load') |
| 187 | + ->with($this->pageId); |
| 188 | + $this->pageMock->expects($this->once()) |
| 189 | + ->method('getTitle') |
| 190 | + ->willReturn($this->title); |
| 191 | + $this->pageMock->expects($this->once()) |
| 192 | + ->method('delete') |
| 193 | + ->willThrowException(new \Exception(__($errorMsg))); |
| 194 | + |
| 195 | + $this->eventManagerMock->expects($this->once()) |
| 196 | + ->method('dispatch') |
| 197 | + ->with( |
| 198 | + 'adminhtml_cmspage_on_delete', |
| 199 | + ['title' => $this->title, 'status' => 'fail'] |
| 200 | + ); |
| 201 | + |
| 202 | + $this->messageManagerMock->expects($this->once()) |
| 203 | + ->method('addError') |
| 204 | + ->with($errorMsg); |
| 205 | + $this->messageManagerMock->expects($this->never()) |
| 206 | + ->method('addSuccess'); |
| 207 | + |
| 208 | + $this->resultRedirectMock->expects($this->once()) |
| 209 | + ->method('setPath') |
| 210 | + ->with('*/*/edit', ['page_id' => $this->pageId]) |
| 211 | + ->willReturnSelf(); |
| 212 | + |
| 213 | + $this->assertSame($this->resultRedirectMock, $this->deleteController->execute()); |
| 214 | + } |
| 215 | +} |
0 commit comments