|
| 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\Block; |
| 7 | + |
| 8 | +class EditTest extends \PHPUnit_Framework_TestCase |
| 9 | +{ |
| 10 | + /** |
| 11 | + * @var \Magento\Cms\Controller\Adminhtml\Block\Edit |
| 12 | + */ |
| 13 | + protected $editController; |
| 14 | + |
| 15 | + /** |
| 16 | + * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager |
| 17 | + */ |
| 18 | + protected $objectManager; |
| 19 | + |
| 20 | + /** |
| 21 | + * @var \Magento\Backend\App\Action\Context|\PHPUnit_Framework_MockObject_MockObject |
| 22 | + */ |
| 23 | + protected $contextMock; |
| 24 | + |
| 25 | + /** |
| 26 | + * @var \Magento\Framework\Controller\ResultInterface|\PHPUnit_Framework_MockObject_MockObject |
| 27 | + */ |
| 28 | + protected $resultRedirectFactoryMock; |
| 29 | + |
| 30 | + /** |
| 31 | + * @var \Magento\Backend\Model\View\Result\Redirect|\PHPUnit_Framework_MockObject_MockObject |
| 32 | + */ |
| 33 | + protected $resultRedirectMock; |
| 34 | + |
| 35 | + /** |
| 36 | + * @var \Magento\Framework\Message\ManagerInterface|\PHPUnit_Framework_MockObject_MockObject |
| 37 | + */ |
| 38 | + protected $messageManagerMock; |
| 39 | + |
| 40 | + /** |
| 41 | + * @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject |
| 42 | + */ |
| 43 | + protected $requestMock; |
| 44 | + |
| 45 | + /** |
| 46 | + * @var \Magento\Cms\Model\Block|\PHPUnit_Framework_MockObject_MockObject |
| 47 | + */ |
| 48 | + protected $blockMock; |
| 49 | + |
| 50 | + /** |
| 51 | + * @var \Magento\Framework\ObjectManager\ObjectManager|\PHPUnit_Framework_MockObject_MockObject |
| 52 | + */ |
| 53 | + protected $objectManagerMock; |
| 54 | + |
| 55 | + /** |
| 56 | + * @var \Magento\Framework\Registry|\PHPUnit_Framework_MockObject_MockObject |
| 57 | + */ |
| 58 | + protected $coreRegistryMock; |
| 59 | + |
| 60 | + /** |
| 61 | + * @var \Magento\Framework\View\Result\PageFactory|\PHPUnit_Framework_MockObject_MockObject |
| 62 | + */ |
| 63 | + protected $resultPageFactoryMock; |
| 64 | + |
| 65 | + protected function setUp() |
| 66 | + { |
| 67 | + $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); |
| 68 | + $this->messageManagerMock = $this->getMock('Magento\Framework\Message\ManagerInterface', [], [], '', false); |
| 69 | + $this->coreRegistryMock = $this->getMock('\Magento\Framework\Registry', [], [], '', false); |
| 70 | + |
| 71 | + $this->blockMock = $this->getMockBuilder('Magento\Cms\Model\Block') |
| 72 | + ->disableOriginalConstructor() |
| 73 | + ->getMock(); |
| 74 | + |
| 75 | + $this->objectManagerMock = $this->getMockBuilder('Magento\Framework\ObjectManager\ObjectManager') |
| 76 | + ->setMethods(['create', 'get']) |
| 77 | + ->disableOriginalConstructor() |
| 78 | + ->getMock(); |
| 79 | + $this->objectManagerMock->expects($this->once()) |
| 80 | + ->method('create') |
| 81 | + ->with('Magento\Cms\Model\Block') |
| 82 | + ->willReturn($this->blockMock); |
| 83 | + |
| 84 | + $this->resultRedirectMock = $this->getMockBuilder('Magento\Backend\Model\View\Result\Redirect') |
| 85 | + ->disableOriginalConstructor() |
| 86 | + ->getMock(); |
| 87 | + |
| 88 | + $this->resultRedirectFactoryMock = $this->getMockBuilder('Magento\Backend\Model\View\Result\RedirectFactory') |
| 89 | + ->disableOriginalConstructor() |
| 90 | + ->getMock(); |
| 91 | + |
| 92 | + $this->resultPageFactoryMock = $this->getMock('Magento\Framework\View\Result\PageFactory', [], [], '', false); |
| 93 | + |
| 94 | + $this->requestMock = $this->getMockForAbstractClass( |
| 95 | + 'Magento\Framework\App\RequestInterface', |
| 96 | + [], |
| 97 | + '', |
| 98 | + false, |
| 99 | + true, |
| 100 | + true, |
| 101 | + [] |
| 102 | + ); |
| 103 | + |
| 104 | + $this->contextMock = $this->getMock( |
| 105 | + '\Magento\Backend\App\Action\Context', |
| 106 | + [], |
| 107 | + [], |
| 108 | + '', |
| 109 | + false |
| 110 | + ); |
| 111 | + $this->contextMock->expects($this->once())->method('getRequest')->willReturn($this->requestMock); |
| 112 | + $this->contextMock->expects($this->once())->method('getObjectManager')->willReturn($this->objectManagerMock); |
| 113 | + $this->contextMock->expects($this->once())->method('getMessageManager')->willReturn($this->messageManagerMock); |
| 114 | + $this->contextMock->expects($this->once()) |
| 115 | + ->method('getResultRedirectFactory') |
| 116 | + ->willReturn($this->resultRedirectFactoryMock); |
| 117 | + |
| 118 | + $this->editController = $this->objectManager->getObject( |
| 119 | + 'Magento\Cms\Controller\Adminhtml\Block\Edit', |
| 120 | + [ |
| 121 | + 'context' => $this->contextMock, |
| 122 | + 'coreRegistry' => $this->coreRegistryMock, |
| 123 | + 'resultPageFactory' => $this->resultPageFactoryMock |
| 124 | + ] |
| 125 | + ); |
| 126 | + } |
| 127 | + |
| 128 | + public function testEditActionBlockNoExists() |
| 129 | + { |
| 130 | + $blockId = 1; |
| 131 | + |
| 132 | + $this->requestMock->expects($this->once()) |
| 133 | + ->method('getParam') |
| 134 | + ->with('block_id') |
| 135 | + ->willReturn($blockId); |
| 136 | + |
| 137 | + $this->blockMock->expects($this->once()) |
| 138 | + ->method('load') |
| 139 | + ->with($blockId); |
| 140 | + $this->blockMock->expects($this->once()) |
| 141 | + ->method('getId') |
| 142 | + ->willReturn(null); |
| 143 | + |
| 144 | + $this->messageManagerMock->expects($this->once()) |
| 145 | + ->method('addError') |
| 146 | + ->with(__('This block no longer exists.')); |
| 147 | + |
| 148 | + $this->resultRedirectFactoryMock->expects($this->atLeastOnce()) |
| 149 | + ->method('create') |
| 150 | + ->willReturn($this->resultRedirectMock); |
| 151 | + |
| 152 | + $this->resultRedirectMock->expects($this->once()) |
| 153 | + ->method('setPath') |
| 154 | + ->with('*/*/') |
| 155 | + ->willReturnSelf(); |
| 156 | + |
| 157 | + $this->assertSame($this->resultRedirectMock, $this->editController->execute()); |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * @param int $blockId |
| 162 | + * @param string $label |
| 163 | + * @param string $title |
| 164 | + * @dataProvider editActionData |
| 165 | + */ |
| 166 | + public function testEditAction($blockId, $label, $title) |
| 167 | + { |
| 168 | + $this->requestMock->expects($this->once()) |
| 169 | + ->method('getParam') |
| 170 | + ->with('block_id') |
| 171 | + ->willReturn($blockId); |
| 172 | + |
| 173 | + $this->blockMock->expects($this->any()) |
| 174 | + ->method('load') |
| 175 | + ->with($blockId); |
| 176 | + $this->blockMock->expects($this->any()) |
| 177 | + ->method('getId') |
| 178 | + ->willReturn($blockId); |
| 179 | + $this->blockMock->expects($this->any()) |
| 180 | + ->method('getTitle') |
| 181 | + ->willReturn('Test title'); |
| 182 | + |
| 183 | + $sessionManagerMock = $this->getMock('Magento\Backend\Model\Session', ['getFormData'], [], '', false); |
| 184 | + $this->objectManagerMock->expects($this->once()) |
| 185 | + ->method('get') |
| 186 | + ->with('Magento\Backend\Model\Session') |
| 187 | + ->willReturn($sessionManagerMock); |
| 188 | + |
| 189 | + $sessionManagerMock->expects($this->once()) |
| 190 | + ->method('getFormData') |
| 191 | + ->with(true); |
| 192 | + |
| 193 | + $this->coreRegistryMock->expects($this->once()) |
| 194 | + ->method('register') |
| 195 | + ->with('cms_block', $this->blockMock); |
| 196 | + |
| 197 | + $resultPageMock = $this->getMock('Magento\Backend\Model\View\Result\Page', [], [], '', false); |
| 198 | + |
| 199 | + $this->resultPageFactoryMock->expects($this->once()) |
| 200 | + ->method('create') |
| 201 | + ->willReturn($resultPageMock); |
| 202 | + |
| 203 | + $titleMock = $this->getMock('Magento\Framework\View\Page\Title', [], [], '', false); |
| 204 | + $titleMock->expects($this->at(0))->method('prepend')->with(__('Blocks')); |
| 205 | + $titleMock->expects($this->at(1))->method('prepend')->with($this->getTitle()); |
| 206 | + $pageConfigMock = $this->getMock('Magento\Framework\View\Page\Config', [], [], '', false); |
| 207 | + $pageConfigMock->expects($this->exactly(2))->method('getTitle')->willReturn($titleMock); |
| 208 | + |
| 209 | + $resultPageMock->expects($this->once()) |
| 210 | + ->method('setActiveMenu') |
| 211 | + ->willReturnSelf(); |
| 212 | + $resultPageMock->expects($this->any()) |
| 213 | + ->method('addBreadcrumb') |
| 214 | + ->willReturnSelf(); |
| 215 | + $resultPageMock->expects($this->at(3)) |
| 216 | + ->method('addBreadcrumb') |
| 217 | + ->with(__($label), __($title)) |
| 218 | + ->willReturnSelf(); |
| 219 | + $resultPageMock->expects($this->exactly(2)) |
| 220 | + ->method('getConfig') |
| 221 | + ->willReturn($pageConfigMock); |
| 222 | + |
| 223 | + $this->assertSame($resultPageMock, $this->editController->execute()); |
| 224 | + } |
| 225 | + |
| 226 | + /** |
| 227 | + * @return \Magento\Framework\Phrase|string |
| 228 | + */ |
| 229 | + protected function getTitle() |
| 230 | + { |
| 231 | + return $this->blockMock->getId() ? $this->blockMock->getTitle() : __('New Block'); |
| 232 | + } |
| 233 | + |
| 234 | + /** |
| 235 | + * @return array |
| 236 | + */ |
| 237 | + public function editActionData() |
| 238 | + { |
| 239 | + return [ |
| 240 | + [null, 'New Block', 'New Block'], |
| 241 | + [2, 'Edit Block', 'Edit Block'] |
| 242 | + ]; |
| 243 | + } |
| 244 | +} |
0 commit comments