Skip to content

Commit c4cec78

Browse files
authored
Merge pull request #415 from magento-south/MAGETWO-58806
[SOUTH] MAGETWO-58806 The dot is missed at the end of success message if Merchant moves the Category
2 parents 8931b9c + b8613e5 commit c4cec78

File tree

1 file changed

+320
-0
lines changed
  • app/code/Magento/Catalog/Test/Unit/Controller/Category

1 file changed

+320
-0
lines changed
Lines changed: 320 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,320 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Catalog\Test\Unit\Controller\Category;
7+
8+
use Magento\Catalog\Controller\Adminhtml\Category\Move;
9+
use Magento\Framework\Message\ManagerInterface;
10+
use Magento\Framework\ObjectManagerInterface;
11+
use Magento\Framework\Registry;
12+
13+
/**
14+
* Class MoveTest
15+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
16+
*/
17+
class MoveTest extends \PHPUnit_Framework_TestCase
18+
{
19+
/**
20+
* @var \Magento\Framework\Controller\Result\JsonFactory | \PHPUnit_Framework_MockObject_MockObject
21+
*/
22+
private $resultJsonFactoryMock;
23+
24+
/**
25+
* @var \Magento\Framework\View\LayoutFactory | \PHPUnit_Framework_MockObject_MockObject
26+
*/
27+
private $layoutFactoryMock;
28+
29+
/**
30+
* @var \Psr\Log\LoggerInterface | \PHPUnit_Framework_MockObject_MockObject
31+
*/
32+
private $loggerMock;
33+
34+
/**
35+
* @var \Magento\Backend\App\Action\Context|\PHPUnit_Framework_MockObject_MockObject
36+
*/
37+
private $context;
38+
39+
/**
40+
* @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject
41+
*/
42+
private $request;
43+
44+
/**
45+
* @var \Magento\Catalog\Controller\Adminhtml\Category\Move
46+
*/
47+
private $moveController;
48+
49+
/**
50+
* @var ObjectManagerInterface | \PHPUnit_Framework_MockObject_MockObject
51+
*/
52+
private $objectManager;
53+
54+
/**
55+
* @var ManagerInterface | \PHPUnit_Framework_MockObject_MockObject
56+
*/
57+
private $messageManager;
58+
59+
public function setUp()
60+
{
61+
$this->resultJsonFactoryMock = $this->getMockBuilder(\Magento\Framework\Controller\Result\JsonFactory::class)
62+
->setMethods(['create'])
63+
->disableOriginalConstructor()
64+
->getMock();
65+
$this->layoutFactoryMock = $this->getMockBuilder(\Magento\Framework\View\LayoutFactory::class)
66+
->setMethods(['create'])
67+
->disableOriginalConstructor()
68+
->getMock();
69+
$this->context = $this->getMock(\Magento\Backend\App\Action\Context::class, [], [], '', false);
70+
$this->loggerMock = $this->getMock(\Psr\Log\LoggerInterface::class);
71+
$this->fillContext();
72+
73+
$this->moveController = new Move(
74+
$this->context,
75+
$this->resultJsonFactoryMock,
76+
$this->layoutFactoryMock,
77+
$this->loggerMock
78+
);
79+
$this->initObjectManager();
80+
}
81+
82+
private function fillContext()
83+
{
84+
$this->request = $this
85+
->getMockBuilder(\Magento\Framework\App\RequestInterface::class)
86+
->setMethods(['getPost'])
87+
->disableOriginalConstructor()
88+
->getMockForAbstractClass();
89+
$this->context->expects($this->once())->method('getRequest')->will($this->returnValue($this->request));
90+
$this->messageManager = $this->getMock(ManagerInterface::class);
91+
$this->context->expects($this->once())->method('getMessageManager')->willReturn($this->messageManager);
92+
}
93+
94+
private function initObjectManager()
95+
{
96+
$this->objectManager = $this->getMock(ObjectManagerInterface::class);
97+
$moveController = new \ReflectionClass($this->moveController);
98+
$objectManagerProp = $moveController->getProperty('_objectManager');
99+
$objectManagerProp->setAccessible(true);
100+
$objectManagerProp->setValue($this->moveController, $this->objectManager);
101+
}
102+
103+
public function testExecuteWithGenericException()
104+
{
105+
$messagesCollection = $this->getMockBuilder(\Magento\Framework\Message\Collection::class)
106+
->disableOriginalConstructor()
107+
->getMock();
108+
$messageBlock = $this->getMockBuilder(\Magento\Framework\View\Element\Messages::class)
109+
->disableOriginalConstructor()
110+
->getMock();
111+
$layoutMock = $this->getMock(\Magento\Framework\View\LayoutInterface::class);
112+
$this->layoutFactoryMock->expects($this->once())
113+
->method('create')
114+
->willReturn($layoutMock);
115+
$layoutMock->expects($this->once())
116+
->method('getMessagesBlock')
117+
->willReturn($messageBlock);
118+
$wysiwigConfig = $this->getMockBuilder(\Magento\Cms\Model\Wysiwyg\Config::class)
119+
->disableOriginalConstructor()
120+
->getMock();
121+
$registry = $this->getMockBuilder(Registry::class)
122+
->disableOriginalConstructor()
123+
->getMock();
124+
$categoryMock = $this->getMockBuilder(\Magento\Catalog\Model\Category::class)
125+
->disableOriginalConstructor()
126+
->getMock();
127+
$this->request->expects($this->exactly(2))
128+
->method('getPost')
129+
->withConsecutive(['pid', false], ['aid', false])
130+
->willReturnMap([['pid', false, 2], ['aid', false, 1]]);
131+
$this->objectManager->expects($this->once())
132+
->method('create')
133+
->with(\Magento\Catalog\Model\Category::class)
134+
->willReturn($categoryMock);
135+
$this->objectManager->expects($this->any())
136+
->method('get')
137+
->withConsecutive([Registry::class], [Registry::class], [\Magento\Cms\Model\Wysiwyg\Config::class])
138+
->willReturnMap([[Registry::class, $registry], [\Magento\Cms\Model\Wysiwyg\Config::class, $wysiwigConfig]]);
139+
$categoryMock->expects($this->once())
140+
->method('move')
141+
->willThrowException(new \Exception(
142+
__('Some exception')
143+
));
144+
$this->messageManager->expects($this->once())
145+
->method('addError')
146+
->with(__('There was a category move error.'));
147+
$this->messageManager->expects($this->once())
148+
->method('getMessages')
149+
->with(true)
150+
->willReturn($messagesCollection);
151+
$messageBlock->expects($this->once())
152+
->method('setMessages')
153+
->with($messagesCollection);
154+
$resultJsonMock = $this->getMockBuilder(\Magento\Framework\Controller\Result\Json::class)
155+
->disableOriginalConstructor()
156+
->getMock();
157+
$messageBlock->expects($this->once())
158+
->method('getGroupedHtml')
159+
->willReturn('<body></body>');
160+
$resultJsonMock->expects($this->once())
161+
->method('setData')
162+
->with(
163+
[
164+
'messages' => '<body></body>',
165+
'error' => true
166+
]
167+
)
168+
->willReturn(true);
169+
$this->resultJsonFactoryMock
170+
->expects($this->once())
171+
->method('create')
172+
->willReturn($resultJsonMock);
173+
$this->assertTrue($this->moveController->execute());
174+
}
175+
176+
public function testExecuteWithLocaliedException()
177+
{
178+
$exceptionMessage = 'Sorry, but we can\'t find the new category you selected.';
179+
$messagesCollection = $this->getMockBuilder(\Magento\Framework\Message\Collection::class)
180+
->disableOriginalConstructor()
181+
->getMock();
182+
$messageBlock = $this->getMockBuilder(\Magento\Framework\View\Element\Messages::class)
183+
->disableOriginalConstructor()
184+
->getMock();
185+
$layoutMock = $this->getMock(\Magento\Framework\View\LayoutInterface::class);
186+
$this->layoutFactoryMock->expects($this->once())
187+
->method('create')
188+
->willReturn($layoutMock);
189+
$layoutMock->expects($this->once())
190+
->method('getMessagesBlock')
191+
->willReturn($messageBlock);
192+
$wysiwigConfig = $this->getMockBuilder(\Magento\Cms\Model\Wysiwyg\Config::class)
193+
->disableOriginalConstructor()
194+
->getMock();
195+
$registry = $this->getMockBuilder(Registry::class)
196+
->disableOriginalConstructor()
197+
->getMock();
198+
$categoryMock = $this->getMockBuilder(\Magento\Catalog\Model\Category::class)
199+
->disableOriginalConstructor()
200+
->getMock();
201+
$this->request->expects($this->exactly(2))
202+
->method('getPost')
203+
->withConsecutive(['pid', false], ['aid', false])
204+
->willReturnMap([['pid', false, 2], ['aid', false, 1]]);
205+
$this->objectManager->expects($this->once())
206+
->method('create')
207+
->with(\Magento\Catalog\Model\Category::class)
208+
->willReturn($categoryMock);
209+
$this->objectManager->expects($this->any())
210+
->method('get')
211+
->withConsecutive([Registry::class], [Registry::class], [\Magento\Cms\Model\Wysiwyg\Config::class])
212+
->willReturnMap([[Registry::class, $registry], [\Magento\Cms\Model\Wysiwyg\Config::class, $wysiwigConfig]]);
213+
$this->messageManager->expects($this->once())
214+
->method('addError')
215+
->with($exceptionMessage);
216+
$this->messageManager->expects($this->once())
217+
->method('getMessages')
218+
->with(true)
219+
->willReturn($messagesCollection);
220+
$messageBlock->expects($this->once())
221+
->method('setMessages')
222+
->with($messagesCollection);
223+
$resultJsonMock = $this->getMockBuilder(\Magento\Framework\Controller\Result\Json::class)
224+
->disableOriginalConstructor()
225+
->getMock();
226+
$messageBlock->expects($this->once())
227+
->method('getGroupedHtml')
228+
->willReturn('<body></body>');
229+
$resultJsonMock->expects($this->once())
230+
->method('setData')
231+
->with(
232+
[
233+
'messages' => '<body></body>',
234+
'error' => true
235+
]
236+
)
237+
->willReturn(true);
238+
$categoryMock->expects($this->once())
239+
->method('move')
240+
->willThrowException(new \Magento\Framework\Exception\LocalizedException(
241+
__($exceptionMessage)
242+
));
243+
$this->resultJsonFactoryMock
244+
->expects($this->once())
245+
->method('create')
246+
->willReturn($resultJsonMock);
247+
$this->assertTrue($this->moveController->execute());
248+
}
249+
250+
public function testSuccessfullCategorySave()
251+
{
252+
$messagesCollection = $this->getMockBuilder(\Magento\Framework\Message\Collection::class)
253+
->disableOriginalConstructor()
254+
->getMock();
255+
$messageBlock = $this->getMockBuilder(\Magento\Framework\View\Element\Messages::class)
256+
->disableOriginalConstructor()
257+
->getMock();
258+
$layoutMock = $this->getMock(\Magento\Framework\View\LayoutInterface::class);
259+
$this->layoutFactoryMock->expects($this->once())
260+
->method('create')
261+
->willReturn($layoutMock);
262+
$layoutMock->expects($this->once())
263+
->method('getMessagesBlock')
264+
->willReturn($messageBlock);
265+
$wysiwigConfig = $this->getMockBuilder(\Magento\Cms\Model\Wysiwyg\Config::class)
266+
->disableOriginalConstructor()
267+
->getMock();
268+
$registry = $this->getMockBuilder(Registry::class)
269+
->disableOriginalConstructor()
270+
->getMock();
271+
$categoryMock = $this->getMockBuilder(\Magento\Catalog\Model\Category::class)
272+
->disableOriginalConstructor()
273+
->getMock();
274+
$this->request->expects($this->exactly(2))
275+
->method('getPost')
276+
->withConsecutive(['pid', false], ['aid', false])
277+
->willReturnMap([['pid', false, 2], ['aid', false, 1]]);
278+
$this->objectManager->expects($this->once())
279+
->method('create')
280+
->with(\Magento\Catalog\Model\Category::class)
281+
->willReturn($categoryMock);
282+
$this->objectManager->expects($this->any())
283+
->method('get')
284+
->withConsecutive([Registry::class], [Registry::class], [\Magento\Cms\Model\Wysiwyg\Config::class])
285+
->willReturnMap([[Registry::class, $registry], [\Magento\Cms\Model\Wysiwyg\Config::class, $wysiwigConfig]]);
286+
$this->messageManager->expects($this->once())
287+
->method('getMessages')
288+
->with(true)
289+
->willReturn($messagesCollection);
290+
$messageBlock->expects($this->once())
291+
->method('setMessages')
292+
->with($messagesCollection);
293+
$resultJsonMock = $this->getMockBuilder(\Magento\Framework\Controller\Result\Json::class)
294+
->disableOriginalConstructor()
295+
->getMock();
296+
$messageBlock->expects($this->once())
297+
->method('getGroupedHtml')
298+
->willReturn('<body></body>');
299+
$resultJsonMock->expects($this->once())
300+
->method('setData')
301+
->with(
302+
[
303+
'messages' => '<body></body>',
304+
'error' => false
305+
]
306+
)
307+
->willReturn(true);
308+
$this->messageManager->expects($this->once())
309+
->method('addSuccess')
310+
->with(__('You moved the category.'));
311+
$categoryMock->expects($this->once())
312+
->method('move')
313+
->with(2, 1);
314+
$this->resultJsonFactoryMock
315+
->expects($this->once())
316+
->method('create')
317+
->willReturn($resultJsonMock);
318+
$this->assertTrue($this->moveController->execute());
319+
}
320+
}

0 commit comments

Comments
 (0)