Skip to content

Commit 71c046f

Browse files
author
Andrii Kasian
committed
Merge remote-tracking branch 'origin/MAGETWO-36987' into S67
2 parents a81307f + 2b80526 commit 71c046f

File tree

3 files changed

+312
-32
lines changed

3 files changed

+312
-32
lines changed

app/code/Magento/Search/Controller/Adminhtml/Term/Save.php

Lines changed: 72 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,31 @@
66
*/
77
namespace Magento\Search\Controller\Adminhtml\Term;
88

9+
use Magento\Backend\App\Action\Context;
10+
use Magento\Framework\View\Result\PageFactory;
11+
use Magento\Search\Model\QueryFactory;
12+
913
class Save extends \Magento\Search\Controller\Adminhtml\Term
1014
{
15+
/**
16+
* @var QueryFactory
17+
*/
18+
private $queryFactory;
19+
20+
/**
21+
* @param Context $context
22+
* @param PageFactory $resultPageFactory
23+
* @param QueryFactory $queryFactory
24+
*/
25+
public function __construct(
26+
Context $context,
27+
PageFactory $resultPageFactory,
28+
QueryFactory $queryFactory
29+
) {
30+
parent::__construct($context, $resultPageFactory);
31+
$this->queryFactory = $queryFactory;
32+
}
33+
1134
/**
1235
* Save search query
1336
*
@@ -16,52 +39,69 @@ class Save extends \Magento\Search\Controller\Adminhtml\Term
1639
*/
1740
public function execute()
1841
{
19-
$hasError = false;
2042
$data = $this->getRequest()->getPostValue();
21-
$queryId = $this->getRequest()->getPost('query_id', null);
22-
/** @var \Magento\Backend\Model\View\Result\Redirect $redirectResult */
23-
$redirectResult = $this->resultRedirectFactory->create();
2443
if ($this->getRequest()->isPost() && $data) {
25-
/* @var $model \Magento\Search\Model\Query */
26-
$model = $this->_objectManager->create('Magento\Search\Model\Query');
27-
28-
// validate query
29-
$queryText = $this->getRequest()->getPost('query_text', false);
30-
$storeId = $this->getRequest()->getPost('store_id', false);
31-
3244
try {
33-
if ($queryText) {
34-
$model->setStoreId($storeId);
35-
$model->loadByQueryText($queryText);
36-
if ($model->getId() && $model->getId() != $queryId) {
37-
throw new \Magento\Framework\Exception\LocalizedException(
38-
__('You already have an identical search term query.')
39-
);
40-
} elseif (!$model->getId() && $queryId) {
41-
$model->load($queryId);
42-
}
43-
} elseif ($queryId) {
44-
$model->load($queryId);
45-
}
46-
45+
$model = $this->loadQuery();
4746
$model->addData($data);
4847
$model->setIsProcessed(0);
4948
$model->save();
5049
$this->messageManager->addSuccess(__('You saved the search term.'));
5150
} catch (\Magento\Framework\Exception\LocalizedException $e) {
5251
$this->messageManager->addError($e->getMessage());
53-
$hasError = true;
52+
return $this->proceedToEdit($data);
5453
} catch (\Exception $e) {
5554
$this->messageManager->addException($e, __('Something went wrong while saving the search query.'));
56-
$hasError = true;
55+
return $this->proceedToEdit($data);
5756
}
5857
}
5958

60-
if ($hasError) {
61-
$this->_getSession()->setPageData($data);
62-
return $redirectResult->setPath('search/*/edit', ['id' => $queryId]);
63-
} else {
64-
return $redirectResult->setPath('search/*');
59+
/** @var \Magento\Backend\Model\View\Result\Redirect $redirectResult */
60+
$redirectResult = $this->resultRedirectFactory->create();
61+
return $redirectResult->setPath('search/*');
62+
}
63+
64+
/**
65+
* Create\Load Query model instance
66+
*
67+
* @return \Magento\Search\Model\Query
68+
* @throws \Magento\Framework\Exception\LocalizedException
69+
*/
70+
private function loadQuery()
71+
{
72+
//validate query
73+
$queryText = $this->getRequest()->getPost('query_text', false);
74+
$queryId = $this->getRequest()->getPost('query_id', null);
75+
76+
/* @var $model \Magento\Search\Model\Query */
77+
$model = $this->queryFactory->create();
78+
if ($queryText) {
79+
$storeId = $this->getRequest()->getPost('store_id', false);
80+
$model->setStoreId($storeId);
81+
$model->loadByQueryText($queryText);
82+
if ($model->getId() && $model->getId() != $queryId) {
83+
throw new \Magento\Framework\Exception\LocalizedException(
84+
__('You already have an identical search term query.')
85+
);
86+
}
87+
}
88+
if ($queryId && !$model->getId()) {
89+
$model->load($queryId);
6590
}
91+
return $model;
92+
}
93+
94+
/**
95+
* Redirect to Edit page
96+
*
97+
* @param array $data
98+
* @return \Magento\Backend\Model\View\Result\Redirect
99+
*/
100+
private function proceedToEdit($data)
101+
{
102+
$this->_getSession()->setPageData($data);
103+
/** @var \Magento\Backend\Model\View\Result\Redirect $redirectResult */
104+
$redirectResult = $this->resultRedirectFactory->create();
105+
return $redirectResult->setPath('search/*/edit', ['id' => $this->getRequest()->getPost('query_id', null)]);
66106
}
67107
}

app/code/Magento/Search/Test/Unit/Controller/Adminhtml/Term/MassDeleteTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class MassDeleteTest extends \PHPUnit_Framework_TestCase
3636

3737
/** @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject */
3838
private $request;
39+
3940
/** @var \Magento\Backend\Model\View\Result\Redirect|\PHPUnit_Framework_MockObject_MockObject */
4041
private $redirect;
4142

Lines changed: 239 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,239 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Search\Test\Unit\Controller\Adminhtml\Term;
8+
9+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
10+
11+
class SaveTest extends \PHPUnit_Framework_TestCase
12+
{
13+
/** @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject */
14+
private $request;
15+
16+
/** @var \Magento\Backend\Model\View\Result\Redirect|\PHPUnit_Framework_MockObject_MockObject */
17+
private $redirect;
18+
19+
/** @var \Magento\Framework\Message\ManagerInterface|\PHPUnit_Framework_MockObject_MockObject */
20+
private $messageManager;
21+
22+
/** @var \Magento\Backend\Model\Session|\PHPUnit_Framework_MockObject_MockObject */
23+
private $session;
24+
25+
/** @var \Magento\Backend\App\Action\Context|\PHPUnit_Framework_MockObject_MockObject */
26+
private $context;
27+
28+
/** @var \Magento\Search\Model\Query|\PHPUnit_Framework_MockObject_MockObject */
29+
private $query;
30+
31+
/** @var \Magento\Search\Controller\Adminhtml\Term\Save */
32+
private $controller;
33+
34+
protected function setUp()
35+
{
36+
$objectManagerHelper = new ObjectManagerHelper($this);
37+
38+
$this->context = $this->getMockBuilder('Magento\Backend\App\Action\Context')
39+
->disableOriginalConstructor()
40+
->getMock();
41+
42+
$this->redirect = $this->getMockBuilder('Magento\Backend\Model\View\Result\Redirect')
43+
->setMethods(['setPath'])
44+
->disableOriginalConstructor()
45+
->getMock();
46+
$redirectFactory = $this->getMockBuilder('Magento\Backend\Model\View\Result\RedirectFactory')
47+
->setMethods(['create'])
48+
->disableOriginalConstructor()
49+
->getMock();
50+
$redirectFactory->expects($this->any())
51+
->method('create')
52+
->will($this->returnValue($this->redirect));
53+
$this->context->expects($this->any())
54+
->method('getResultRedirectFactory')
55+
->willReturn($redirectFactory);
56+
57+
$this->request = $this->getMockBuilder('\Magento\Framework\App\RequestInterface')
58+
->disableOriginalConstructor()
59+
->setMethods(['getPostValue', 'isPost', 'getPost'])
60+
->getMockForAbstractClass();
61+
$this->context->expects($this->atLeastOnce())
62+
->method('getRequest')
63+
->willReturn($this->request);
64+
65+
$objectManager = $this->getMockBuilder('\Magento\Framework\ObjectManagerInterface')
66+
->disableOriginalConstructor()
67+
->setMethods(['create'])
68+
->getMockForAbstractClass();
69+
$this->context->expects($this->any())
70+
->method('getObjectManager')
71+
->willReturn($objectManager);
72+
73+
$this->messageManager = $this->getMockBuilder('\Magento\Framework\Message\ManagerInterface')
74+
->disableOriginalConstructor()
75+
->setMethods(['addSuccess', 'addError', 'addException'])
76+
->getMockForAbstractClass();
77+
$this->context->expects($this->any())
78+
->method('getMessageManager')
79+
->willReturn($this->messageManager);
80+
81+
$this->session = $this->getMockBuilder('\Magento\Backend\Model\Session')
82+
->disableOriginalConstructor()
83+
->setMethods(['setPageData'])
84+
->getMock();
85+
$this->context->expects($this->any())
86+
->method('getSession')
87+
->willReturn($this->session);
88+
89+
$pageFactory = $this->getMockBuilder('Magento\Framework\View\Result\PageFactory')
90+
->setMethods([])
91+
->disableOriginalConstructor()
92+
->getMock();
93+
94+
$this->query = $this->getMockBuilder('Magento\Search\Model\Query')
95+
->disableOriginalConstructor()
96+
->setMethods(['getId', 'load', 'addData', 'setIsProcessed', 'save', 'loadByQueryText', 'setStoreId'])
97+
->getMock();
98+
$queryFactory = $this->getMockBuilder('Magento\Search\Model\QueryFactory')
99+
->setMethods(['create'])
100+
->disableOriginalConstructor()
101+
->getMock();
102+
$queryFactory->expects($this->any())
103+
->method('create')
104+
->will($this->returnValue($this->query));
105+
106+
$this->controller = $objectManagerHelper->getObject(
107+
'Magento\Search\Controller\Adminhtml\Term\Save',
108+
[
109+
'context' => $this->context,
110+
'resultPageFactory' => $pageFactory,
111+
'queryFactory' => $queryFactory,
112+
]
113+
);
114+
}
115+
116+
/**
117+
* @param bool $isPost
118+
* @param array $data
119+
* @dataProvider executeIsPostDataDataProvider
120+
*/
121+
public function testExecuteIsPostData($isPost, $data)
122+
{
123+
$this->request->expects($this->at(0))->method('getPostValue')->willReturn($data);
124+
$this->request->expects($this->at(1))->method('isPost')->willReturn($isPost);
125+
$this->redirect->expects($this->once())->method('setPath')->willReturnSelf();
126+
$this->assertSame($this->redirect, $this->controller->execute());
127+
}
128+
129+
/**
130+
* @return array
131+
*/
132+
public function executeIsPostDataDataProvider()
133+
{
134+
return [
135+
[false, ['0' => '0']],
136+
[true, []]
137+
];
138+
}
139+
140+
public function testExecuteLoadQueryQueryId()
141+
{
142+
$queryId = 1;
143+
$queryText = '';
144+
$this->mockGetRequestData($queryText, $queryId);
145+
146+
$this->query->expects($this->once())->method('getId')->willReturn(false);
147+
$this->query->expects($this->once())->method('load')->with($queryId);
148+
149+
$this->messageManager->expects($this->once())->method('addSuccess');
150+
151+
$this->redirect->expects($this->once())->method('setPath')->willReturnSelf();
152+
$this->assertSame($this->redirect, $this->controller->execute());
153+
}
154+
155+
public function testExecuteLoadQueryQueryIdQueryText()
156+
{
157+
$queryId = 1;
158+
$queryText = 'search';
159+
$this->mockGetRequestData($queryText, $queryId);
160+
161+
$this->request->expects($this->at(4))->method('getPost')->with('store_id', false)->willReturn(1);
162+
163+
$this->query->expects($this->once())->method('setStoreId');
164+
$this->query->expects($this->once())->method('loadByQueryText')->with($queryText);
165+
$this->query->expects($this->any())->method('getId')->willReturn($queryId);
166+
167+
$this->messageManager->expects($this->once())->method('addSuccess');
168+
169+
$this->redirect->expects($this->once())->method('setPath')->willReturnSelf();
170+
$this->assertSame($this->redirect, $this->controller->execute());
171+
}
172+
173+
public function testExecuteLoadQueryQueryIdQueryText2()
174+
{
175+
$queryId = 1;
176+
$queryText = 'search';
177+
$this->mockGetRequestData($queryText, $queryId);
178+
179+
$this->request->expects($this->at(4))->method('getPost')->with('store_id', false)->willReturn(1);
180+
181+
$this->query->expects($this->once())->method('setStoreId');
182+
$this->query->expects($this->once())->method('loadByQueryText')->with($queryText);
183+
$this->query->expects($this->any())->method('getId')->willReturn(false);
184+
$this->query->expects($this->once())->method('load')->with($queryId);
185+
186+
$this->messageManager->expects($this->once())->method('addSuccess');
187+
188+
$this->redirect->expects($this->once())->method('setPath')->willReturnSelf();
189+
$this->assertSame($this->redirect, $this->controller->execute());
190+
}
191+
192+
public function testExecuteLoadQueryQueryIdQueryTextException()
193+
{
194+
$queryId = 1;
195+
$anotherQueryId = 2;
196+
$queryText = 'search';
197+
$this->mockGetRequestData($queryText, $queryId);
198+
199+
$this->request->expects($this->at(4))->method('getPost')->with('store_id', false)->willReturn(1);
200+
201+
$this->query->expects($this->once())->method('setStoreId');
202+
$this->query->expects($this->once())->method('loadByQueryText')->with($queryText);
203+
$this->query->expects($this->any())->method('getId')->willReturn($anotherQueryId);
204+
205+
$this->messageManager->expects($this->once())->method('addError');
206+
$this->session->expects($this->once())->method('setPageData');
207+
$this->redirect->expects($this->once())->method('setPath')->willReturnSelf();
208+
$this->assertSame($this->redirect, $this->controller->execute());
209+
}
210+
211+
public function testExecuteException()
212+
{
213+
$queryId = 1;
214+
$queryText = 'search';
215+
$this->mockGetRequestData($queryText, $queryId);
216+
217+
$this->request->expects($this->at(4))->method('getPost')->with('store_id', false)->willReturn(1);
218+
219+
$this->query->expects($this->once())->method('setStoreId');
220+
$this->query->expects($this->once())->method('loadByQueryText')->willThrowException(new \Exception());
221+
222+
$this->messageManager->expects($this->once())->method('addException');
223+
$this->session->expects($this->once())->method('setPageData');
224+
$this->redirect->expects($this->once())->method('setPath')->willReturnSelf();
225+
$this->assertSame($this->redirect, $this->controller->execute());
226+
}
227+
228+
/**
229+
* @param string $queryText
230+
* @param int $queryId
231+
*/
232+
private function mockGetRequestData($queryText, $queryId)
233+
{
234+
$this->request->expects($this->at(0))->method('getPostValue')->willReturn(['0' => '0']);
235+
$this->request->expects($this->at(1))->method('isPost')->willReturn(true);
236+
$this->request->expects($this->at(2))->method('getPost')->with('query_text', false)->willReturn($queryText);
237+
$this->request->expects($this->at(3))->method('getPost')->with('query_id', null)->willReturn($queryId);
238+
}
239+
}

0 commit comments

Comments
 (0)