|
5 | 5 | */
|
6 | 6 | namespace Magento\Ui\Test\Unit\Controller\Adminhtml\Index\Render;
|
7 | 7 |
|
| 8 | +use Magento\Backend\App\Action\Context; |
| 9 | +use Magento\Framework\App\Request\Http; |
| 10 | +use Magento\Framework\App\ViewInterface; |
| 11 | +use Magento\Framework\AuthorizationInterface; |
| 12 | +use Magento\Framework\HTTP\PhpEnvironment\Response; |
| 13 | +use Magento\Framework\View\Element\Template; |
| 14 | +use Magento\Framework\View\Element\UiComponent\Config\ManagerInterface; |
| 15 | +use Magento\Framework\View\Element\UiComponentFactory; |
| 16 | +use Magento\Framework\View\LayoutInterface; |
| 17 | +use Magento\Ui\Component\Wrapper\UiComponent; |
8 | 18 | use Magento\Ui\Controller\Adminhtml\Index\Render\Handle;
|
9 | 19 |
|
| 20 | +/** |
| 21 | + * Test for Magento\Ui\Controller\Adminhtml\Index\Render\Handle class. |
| 22 | + */ |
10 | 23 | class HandleTest extends \PHPUnit_Framework_TestCase
|
11 | 24 | {
|
12 | 25 | /**
|
@@ -39,62 +52,181 @@ class HandleTest extends \PHPUnit_Framework_TestCase
|
39 | 52 | */
|
40 | 53 | protected $controller;
|
41 | 54 |
|
| 55 | + /** |
| 56 | + * @var ManagerInterface|\PHPUnit_Framework_MockObject_MockObject |
| 57 | + */ |
| 58 | + private $uiComponentManagerMock; |
| 59 | + |
| 60 | + /** |
| 61 | + * @var UiComponent|\PHPUnit_Framework_MockObject_MockObject |
| 62 | + */ |
| 63 | + private $uiComponentMock; |
| 64 | + |
| 65 | + /** |
| 66 | + * @var AuthorizationInterface|\PHPUnit_Framework_MockObject_MockObject |
| 67 | + */ |
| 68 | + private $authorizationMock; |
| 69 | + |
| 70 | + /** |
| 71 | + * @var LayoutInterface|\PHPUnit_Framework_MockObject_MockObject |
| 72 | + */ |
| 73 | + private $layoutMock; |
| 74 | + |
| 75 | + /** |
| 76 | + * @inheritdoc |
| 77 | + */ |
42 | 78 | public function setUp()
|
43 | 79 | {
|
44 |
| - $this->contextMock = $this->getMock('Magento\Backend\App\Action\Context', [], [], '', false); |
45 |
| - $this->componentFactoryMock = $this->getMock( |
46 |
| - 'Magento\Framework\View\Element\UiComponentFactory', |
47 |
| - [], |
48 |
| - [], |
49 |
| - '', |
50 |
| - false |
51 |
| - ); |
52 |
| - |
53 |
| - $this->requestMock = $this->getMock('Magento\Framework\App\RequestInterface'); |
| 80 | + $this->contextMock = $this->getMockBuilder(Context::class) |
| 81 | + ->disableOriginalConstructor() |
| 82 | + ->getMock(); |
| 83 | + $this->componentFactoryMock = $this->getMockBuilder(UiComponentFactory::class) |
| 84 | + ->disableOriginalConstructor() |
| 85 | + ->getMock(); |
| 86 | + $this->requestMock = $this->getMockBuilder(Http::class) |
| 87 | + ->disableOriginalConstructor() |
| 88 | + ->getMock(); |
54 | 89 | $this->contextMock->expects($this->atLeastOnce())->method('getRequest')->willReturn($this->requestMock);
|
55 |
| - |
56 |
| - $this->responseMock = $this->getMock('Magento\Framework\HTTP\PhpEnvironment\Response', [], [], '', false); |
| 90 | + $this->responseMock = $this->getMockBuilder(Response::class) |
| 91 | + ->disableOriginalConstructor() |
| 92 | + ->getMock(); |
57 | 93 | $this->contextMock->expects($this->atLeastOnce())->method('getResponse')->willReturn($this->responseMock);
|
58 |
| - |
59 |
| - $this->viewMock = $this->getMock('Magento\Framework\App\ViewInterface'); |
| 94 | + $this->authorizationMock = $this->getMock(AuthorizationInterface::class); |
| 95 | + $this->viewMock = $this->getMockBuilder(ViewInterface::class) |
| 96 | + ->disableOriginalConstructor() |
| 97 | + ->getMock(); |
60 | 98 | $this->contextMock->expects($this->atLeastOnce())->method('getView')->willReturn($this->viewMock);
|
61 |
| - |
62 |
| - $this->controller = new Handle($this->contextMock, $this->componentFactoryMock); |
| 99 | + $this->contextMock->expects($this->atLeastOnce()) |
| 100 | + ->method('getAuthorization') |
| 101 | + ->willReturn($this->authorizationMock); |
| 102 | + $this->uiComponentManagerMock = $this->getMockForAbstractClass(ManagerInterface::class); |
| 103 | + $this->uiComponentMock = $this->getMockBuilder(UiComponent::class) |
| 104 | + ->disableOriginalConstructor() |
| 105 | + ->getMock(); |
| 106 | + $this->layoutMock = $this->getMockBuilder(LayoutInterface::class) |
| 107 | + ->disableOriginalConstructor() |
| 108 | + ->getMockForAbstractClass(); |
| 109 | + |
| 110 | + $this->controller = new Handle($this->contextMock, $this->componentFactoryMock, $this->uiComponentManagerMock); |
63 | 111 |
|
64 | 112 | }
|
65 | 113 |
|
| 114 | + /** |
| 115 | + * @return void |
| 116 | + */ |
66 | 117 | public function testExecuteNoButtons()
|
67 | 118 | {
|
68 |
| - $result = ''; |
69 |
| - $this->requestMock->expects($this->exactly(3))->method('getParam')->willReturn($result); |
| 119 | + $isButtonExist = false; |
| 120 | + $isAllowed = true; |
| 121 | + $result = 'content'; |
70 | 122 |
|
71 |
| - $this->viewMock->expects($this->once()) |
72 |
| - ->method('loadLayout') |
73 |
| - ->with(['default', $result], true, true, false); |
74 |
| - $layoutMock = $this->getMock('\Magento\Framework\View\LayoutInterface'); |
75 |
| - $this->viewMock->expects($this->once())->method('getLayout')->willReturn($layoutMock); |
| 123 | + $this->prepareLayoutData($isButtonExist, $isAllowed); |
76 | 124 |
|
77 |
| - $layoutMock->expects($this->once())->method('getBlock'); |
| 125 | + $this->layoutMock->expects($this->once()) |
| 126 | + ->method('getBlock') |
| 127 | + ->with('customer_listing') |
| 128 | + ->willReturn($this->uiComponentMock); |
| 129 | + $this->uiComponentMock->expects($this->once())->method('toHtml')->willReturn($result); |
| 130 | + $this->responseMock->expects($this->once())->method('appendBody')->with($result); |
78 | 131 |
|
79 |
| - $this->responseMock->expects($this->once())->method('appendBody')->with(''); |
80 | 132 | $this->controller->execute();
|
81 | 133 | }
|
82 | 134 |
|
83 |
| - public function testExecute() |
| 135 | + /** |
| 136 | + * @return void |
| 137 | + */ |
| 138 | + public function testExecuteWithButtons() |
84 | 139 | {
|
85 |
| - $result = 'SomeRequestParam'; |
86 |
| - $this->requestMock->expects($this->exactly(3))->method('getParam')->willReturn($result); |
| 140 | + $isButtonExist = true; |
| 141 | + $isAllowed = true; |
| 142 | + $uiContent = 'content'; |
| 143 | + $buttonContent = 'button'; |
| 144 | + $templateMock = $this->getMockBuilder(Template::class) |
| 145 | + ->disableOriginalConstructor() |
| 146 | + ->getMock(); |
| 147 | + |
| 148 | + $this->prepareLayoutData($isButtonExist, $isAllowed); |
| 149 | + |
| 150 | + $this->layoutMock->expects($this->at(0)) |
| 151 | + ->method('getBlock') |
| 152 | + ->with('customer_listing') |
| 153 | + ->willReturn($this->uiComponentMock); |
| 154 | + $this->uiComponentMock->expects($this->once())->method('toHtml')->willReturn($uiContent); |
| 155 | + $this->layoutMock->expects($this->at(1)) |
| 156 | + ->method('getBlock') |
| 157 | + ->with('page.actions.toolbar') |
| 158 | + ->willReturn($templateMock); |
| 159 | + $templateMock->expects($this->once())->method('toHtml')->willReturn($buttonContent); |
| 160 | + $this->responseMock->expects($this->once())->method('appendBody')->with($uiContent . $buttonContent); |
87 | 161 |
|
88 |
| - $this->viewMock->expects($this->once()) |
89 |
| - ->method('loadLayout') |
90 |
| - ->with(['default', $result], true, true, false); |
91 |
| - |
92 |
| - $layoutMock = $this->getMock('\Magento\Framework\View\LayoutInterface'); |
93 |
| - $this->viewMock->expects($this->exactly(2))->method('getLayout')->willReturn($layoutMock); |
94 |
| - |
95 |
| - $layoutMock->expects($this->exactly(2))->method('getBlock'); |
| 162 | + $this->controller->execute(); |
| 163 | + } |
96 | 164 |
|
| 165 | + /** |
| 166 | + * @return void |
| 167 | + */ |
| 168 | + public function testExecuteWithoutPermissions() |
| 169 | + { |
| 170 | + $isButtonExist = false; |
| 171 | + $isAllowed = false; |
| 172 | + |
| 173 | + $this->prepareLayoutData($isButtonExist, $isAllowed); |
| 174 | + |
| 175 | + $this->requestMock->expects($this->once()) |
| 176 | + ->method('isAjax') |
| 177 | + ->willReturn(true); |
| 178 | + $this->layoutMock->expects($this->never()) |
| 179 | + ->method('getBlock') |
| 180 | + ->willReturn($this->uiComponentMock); |
| 181 | + $this->uiComponentMock->expects($this->never())->method('toHtml'); |
97 | 182 | $this->responseMock->expects($this->once())->method('appendBody')->with('');
|
| 183 | + |
98 | 184 | $this->controller->execute();
|
99 | 185 | }
|
| 186 | + |
| 187 | + /** |
| 188 | + * @param bool $isButtonExist |
| 189 | + * @param bool $isAllowed |
| 190 | + * @return void |
| 191 | + */ |
| 192 | + private function prepareLayoutData($isButtonExist, $isAllowed) |
| 193 | + { |
| 194 | + $aclResource = 'Magento_Customer::manage'; |
| 195 | + $handle = 'customer_index_index'; |
| 196 | + $namespace = 'customer_listing'; |
| 197 | + $componentConfig = [ |
| 198 | + $namespace => [ |
| 199 | + 'arguments' => [ |
| 200 | + 'data' => [ |
| 201 | + 'acl' => $aclResource, |
| 202 | + ], |
| 203 | + ], |
| 204 | + ], |
| 205 | + ]; |
| 206 | + |
| 207 | + $this->requestMock->expects($this->at(0)) |
| 208 | + ->method('getParam') |
| 209 | + ->with('handle', null) |
| 210 | + ->willReturn($handle); |
| 211 | + $this->requestMock->expects($this->at(1)) |
| 212 | + ->method('getParam') |
| 213 | + ->with('namespace', null) |
| 214 | + ->willReturn($namespace); |
| 215 | + $this->requestMock->expects($this->at(2)) |
| 216 | + ->method('getParam') |
| 217 | + ->with('buttons', false) |
| 218 | + ->willReturn($isButtonExist); |
| 219 | + $this->viewMock->expects($this->once()) |
| 220 | + ->method('loadLayout') |
| 221 | + ->with(['default', $handle], true, true, false); |
| 222 | + $this->viewMock->expects($this->once())->method('getLayout')->willReturn($this->layoutMock); |
| 223 | + $this->uiComponentManagerMock->expects($this->once()) |
| 224 | + ->method('getData') |
| 225 | + ->with($namespace) |
| 226 | + ->willReturn($componentConfig); |
| 227 | + $this->authorizationMock->expects($this->once()) |
| 228 | + ->method('isAllowed') |
| 229 | + ->with($aclResource) |
| 230 | + ->willReturn($isAllowed); |
| 231 | + } |
100 | 232 | }
|
0 commit comments