|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © 2015 Magento. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +namespace Magento\Cms\Test\Unit\Block\Adminhtml\Page\Widget; |
| 7 | + |
| 8 | +/** |
| 9 | + * @covers \Magento\Cms\Block\Adminhtml\Page\Widget\Chooser |
| 10 | + */ |
| 11 | +class ChooserTest extends \PHPUnit_Framework_TestCase |
| 12 | +{ |
| 13 | + /** |
| 14 | + * @var \Magento\Cms\Block\Adminhtml\Page\Widget\Chooser |
| 15 | + */ |
| 16 | + protected $this; |
| 17 | + |
| 18 | + /** |
| 19 | + * @var \Magento\Backend\Block\Template\Context |
| 20 | + */ |
| 21 | + protected $context; |
| 22 | + |
| 23 | + /** |
| 24 | + * @var \Magento\Framework\Math\Random|\PHPUnit_Framework_MockObject_MockObject |
| 25 | + */ |
| 26 | + protected $mathRandomMock; |
| 27 | + |
| 28 | + /** |
| 29 | + * @var \Magento\Framework\UrlInterface|\PHPUnit_Framework_MockObject_MockObject |
| 30 | + */ |
| 31 | + protected $urlBuilderMock; |
| 32 | + |
| 33 | + /** |
| 34 | + * @var \Magento\Framework\Escaper|\PHPUnit_Framework_MockObject_MockObject |
| 35 | + */ |
| 36 | + protected $escaper; |
| 37 | + |
| 38 | + /** |
| 39 | + * @var \Magento\Cms\Model\Page|\PHPUnit_Framework_MockObject_MockObject |
| 40 | + */ |
| 41 | + protected $cmsPageMock; |
| 42 | + |
| 43 | + /** |
| 44 | + * @var \Magento\Framework\View\LayoutInterface|\PHPUnit_Framework_MockObject_MockObject |
| 45 | + */ |
| 46 | + protected $layoutMock; |
| 47 | + |
| 48 | + /** |
| 49 | + * @var \Magento\Cms\Model\PageFactory|\PHPUnit_Framework_MockObject_MockObject |
| 50 | + */ |
| 51 | + protected $pageFactoryMock; |
| 52 | + |
| 53 | + /** |
| 54 | + * @var \Magento\Framework\Data\Form\Element\AbstractElement|\PHPUnit_Framework_MockObject_MockObject |
| 55 | + */ |
| 56 | + protected $elementMock; |
| 57 | + |
| 58 | + /** |
| 59 | + * @var \Magento\Framework\View\Element\BlockInterface|\PHPUnit_Framework_MockObject_MockObject |
| 60 | + */ |
| 61 | + protected $chooserMock; |
| 62 | + |
| 63 | + protected function setUp() |
| 64 | + { |
| 65 | + $this->layoutMock = $this->getMockBuilder('Magento\Framework\View\LayoutInterface') |
| 66 | + ->disableOriginalConstructor() |
| 67 | + ->getMock(); |
| 68 | + $this->mathRandomMock = $this->getMockBuilder('Magento\Framework\Math\Random') |
| 69 | + ->disableOriginalConstructor() |
| 70 | + ->getMock(); |
| 71 | + $this->urlBuilderMock = $this->getMockBuilder('Magento\Framework\UrlInterface') |
| 72 | + ->disableOriginalConstructor() |
| 73 | + ->getMock(); |
| 74 | + $this->escaper = $this->getMockBuilder('Magento\Framework\Escaper') |
| 75 | + ->disableOriginalConstructor() |
| 76 | + ->setMethods( |
| 77 | + [ |
| 78 | + 'escapeHtml', |
| 79 | + ] |
| 80 | + ) |
| 81 | + ->getMock(); |
| 82 | + $this->pageFactoryMock = $this->getMockBuilder('Magento\Cms\Model\PageFactory') |
| 83 | + ->setMethods( |
| 84 | + [ |
| 85 | + 'create', |
| 86 | + ] |
| 87 | + ) |
| 88 | + ->disableOriginalConstructor() |
| 89 | + ->getMock(); |
| 90 | + $this->elementMock = $this->getMockBuilder('Magento\Framework\Data\Form\Element\AbstractElement') |
| 91 | + ->disableOriginalConstructor() |
| 92 | + ->setMethods( |
| 93 | + [ |
| 94 | + 'getId', |
| 95 | + 'getValue', |
| 96 | + 'setData', |
| 97 | + ] |
| 98 | + ) |
| 99 | + ->getMock(); |
| 100 | + $this->cmsPageMock = $this->getMockBuilder('Magento\Cms\Model\Page') |
| 101 | + ->disableOriginalConstructor() |
| 102 | + ->setMethods( |
| 103 | + [ |
| 104 | + 'getTitle', |
| 105 | + 'load', |
| 106 | + 'getId', |
| 107 | + ] |
| 108 | + ) |
| 109 | + ->getMock(); |
| 110 | + $this->chooserMock = $this->getMockBuilder('Magento\Framework\View\Element\BlockInterface') |
| 111 | + ->disableOriginalConstructor() |
| 112 | + ->setMethods( |
| 113 | + [ |
| 114 | + 'setElement', |
| 115 | + 'setConfig', |
| 116 | + 'setFieldsetId', |
| 117 | + 'setSourceUrl', |
| 118 | + 'setUniqId', |
| 119 | + 'setLabel', |
| 120 | + 'toHtml', |
| 121 | + ] |
| 122 | + ) |
| 123 | + ->getMock(); |
| 124 | + |
| 125 | + $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); |
| 126 | + $this->context = $objectManager->getObject( |
| 127 | + 'Magento\Backend\Block\Template\Context', |
| 128 | + [ |
| 129 | + 'layout' => $this->layoutMock, |
| 130 | + 'mathRandom' => $this->mathRandomMock, |
| 131 | + 'urlBuilder' => $this->urlBuilderMock, |
| 132 | + 'escaper' => $this->escaper, |
| 133 | + ] |
| 134 | + ); |
| 135 | + $this->this = $objectManager->getObject( |
| 136 | + 'Magento\Cms\Block\Adminhtml\Page\Widget\Chooser', |
| 137 | + [ |
| 138 | + 'context' => $this->context, |
| 139 | + 'pageFactory' => $this->pageFactoryMock |
| 140 | + ] |
| 141 | + ); |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * @covers \Magento\Cms\Block\Adminhtml\Block\Widget\Chooser::prepareElementHtml |
| 146 | + * |
| 147 | + * @param string $elementValue |
| 148 | + * @param integer|null $cmsPageId |
| 149 | + * |
| 150 | + * @dataProvider prepareElementHtmlDataProvider |
| 151 | + */ |
| 152 | + public function testPrepareElementHtml($elementValue, $cmsPageId) |
| 153 | + { |
| 154 | + //$elementValue = 12345; |
| 155 | + //$cmsPageId = 1; |
| 156 | + $elementId = 1; |
| 157 | + $uniqId = '126hj4h3j73hk7b347jhkl37gb34'; |
| 158 | + $sourceUrl = 'cms/page_widget/chooser/126hj4h3j73hk7b347jhkl37gb34'; |
| 159 | + $config = ['key1' => 'value1']; |
| 160 | + $fieldsetId = 2; |
| 161 | + $html = 'some html'; |
| 162 | + $title = 'some "><img src=y onerror=prompt(document.domain)>; title'; |
| 163 | + $titleEscaped = 'some "><img src=y onerror=prompt(document.domain)>; title'; |
| 164 | + |
| 165 | + $this->this->setConfig($config); |
| 166 | + $this->this->setFieldsetId($fieldsetId); |
| 167 | + |
| 168 | + $this->elementMock->expects($this->atLeastOnce()) |
| 169 | + ->method('getId') |
| 170 | + ->willReturn($elementId); |
| 171 | + $this->mathRandomMock->expects($this->atLeastOnce()) |
| 172 | + ->method('getUniqueHash') |
| 173 | + ->with($elementId) |
| 174 | + ->willReturn($uniqId); |
| 175 | + $this->urlBuilderMock->expects($this->atLeastOnce()) |
| 176 | + ->method('getUrl') |
| 177 | + ->with('cms/page_widget/chooser', ['uniq_id' => $uniqId]) |
| 178 | + ->willReturn($sourceUrl); |
| 179 | + $this->layoutMock->expects($this->atLeastOnce()) |
| 180 | + ->method('createBlock') |
| 181 | + ->with('Magento\Widget\Block\Adminhtml\Widget\Chooser') |
| 182 | + ->willReturn($this->chooserMock); |
| 183 | + $this->chooserMock->expects($this->atLeastOnce()) |
| 184 | + ->method('setElement') |
| 185 | + ->with($this->elementMock) |
| 186 | + ->willReturnSelf(); |
| 187 | + $this->chooserMock->expects($this->atLeastOnce()) |
| 188 | + ->method('setConfig') |
| 189 | + ->with($config) |
| 190 | + ->willReturnSelf(); |
| 191 | + $this->chooserMock->expects($this->atLeastOnce()) |
| 192 | + ->method('setFieldsetId') |
| 193 | + ->with($fieldsetId) |
| 194 | + ->willReturnSelf(); |
| 195 | + $this->chooserMock->expects($this->atLeastOnce()) |
| 196 | + ->method('setSourceUrl') |
| 197 | + ->with($sourceUrl) |
| 198 | + ->willReturnSelf(); |
| 199 | + $this->chooserMock->expects($this->atLeastOnce()) |
| 200 | + ->method('setUniqId') |
| 201 | + ->with($uniqId) |
| 202 | + ->willReturnSelf(); |
| 203 | + $this->elementMock->expects($this->atLeastOnce()) |
| 204 | + ->method('getValue') |
| 205 | + ->willReturn($elementValue); |
| 206 | + $this->pageFactoryMock->expects($this->any()) |
| 207 | + ->method('create') |
| 208 | + ->willReturn($this->cmsPageMock); |
| 209 | + $this->cmsPageMock->expects($this->any()) |
| 210 | + ->method('load') |
| 211 | + ->with((int)$elementValue) |
| 212 | + ->willReturnSelf(); |
| 213 | + $this->cmsPageMock->expects($this->any()) |
| 214 | + ->method('getId') |
| 215 | + ->willReturn($cmsPageId); |
| 216 | + $this->cmsPageMock->expects($this->any()) |
| 217 | + ->method('getTitle') |
| 218 | + ->willReturn($title); |
| 219 | + $this->chooserMock->expects($this->atLeastOnce()) |
| 220 | + ->method('toHtml') |
| 221 | + ->willReturn($html); |
| 222 | + if (!empty($elementValue) && !empty($cmsPageId)) { |
| 223 | + $this->escaper->expects(($this->atLeastOnce())) |
| 224 | + ->method('escapeHtml') |
| 225 | + ->willReturn($titleEscaped); |
| 226 | + $this->chooserMock->expects($this->atLeastOnce()) |
| 227 | + ->method('setLabel') |
| 228 | + ->with($titleEscaped) |
| 229 | + ->willReturnSelf(); |
| 230 | + } |
| 231 | + $this->elementMock->expects($this->atLeastOnce()) |
| 232 | + ->method('setData') |
| 233 | + ->with('after_element_html', $html) |
| 234 | + ->willReturnSelf(); |
| 235 | + |
| 236 | + $this->assertEquals($this->elementMock, $this->this->prepareElementHtml($this->elementMock)); |
| 237 | + } |
| 238 | + |
| 239 | + public function prepareElementHtmlDataProvider() |
| 240 | + { |
| 241 | + return [ |
| 242 | + 'elementValue NOT EMPTY, modelBlockId NOT EMPTY' => [ |
| 243 | + 'elementValue' => 'some value', |
| 244 | + 'cmsPageId' => 1, |
| 245 | + ], |
| 246 | + 'elementValue NOT EMPTY, modelBlockId IS EMPTY' => [ |
| 247 | + 'elementValue' => 'some value', |
| 248 | + 'cmsPageId' => null, |
| 249 | + ], |
| 250 | + 'elementValue IS EMPTY, modelBlockId NEVER REACHED' => [ |
| 251 | + 'elementValue' => '', |
| 252 | + 'cmsPageId' => 1, |
| 253 | + ] |
| 254 | + ]; |
| 255 | + } |
| 256 | + |
| 257 | + /** |
| 258 | + * @covers \Magento\Cms\Block\Adminhtml\Page\Widget\Chooser::getGridUrl |
| 259 | + */ |
| 260 | + public function testGetGridUrl() |
| 261 | + { |
| 262 | + $url = 'some url'; |
| 263 | + |
| 264 | + $this->urlBuilderMock->expects($this->atLeastOnce()) |
| 265 | + ->method('getUrl') |
| 266 | + ->with('cms/page_widget/chooser', ['_current' => true]) |
| 267 | + ->willReturn($url); |
| 268 | + |
| 269 | + $this->assertEquals($url, $this->this->getGridUrl()); |
| 270 | + } |
| 271 | +} |
0 commit comments