MassChangelogTest.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Indexer\Test\Unit\Controller\Adminhtml\Indexer;
  8. /**
  9. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  10. */
  11. class MassChangelogTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /**
  14. * @var \Magento\Indexer\Controller\Adminhtml\Indexer\MassChangelog
  15. */
  16. protected $model;
  17. /**
  18. * @var \Magento\Backend\App\Action\Context
  19. */
  20. protected $contextMock;
  21. /**
  22. * @var \Magento\Framework\App\ViewInterface
  23. */
  24. protected $view;
  25. /**
  26. * @var \Magento\Framework\View\Result\Page
  27. */
  28. protected $page;
  29. /**
  30. * @var \Magento\Framework\View\Page\Config
  31. */
  32. protected $config;
  33. /**
  34. * @var \Magento\Framework\View\Page\Title
  35. */
  36. protected $title;
  37. /**
  38. * @var \Magento\Framework\App\RequestInterface
  39. */
  40. protected $request;
  41. /**
  42. * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
  43. */
  44. protected $objectManager;
  45. /**
  46. * @var \Magento\Framework\Message\ManagerInterface
  47. */
  48. protected $messageManager;
  49. /**
  50. * @var \Magento\Framework\Indexer\IndexerRegistry
  51. */
  52. protected $indexReg;
  53. /**
  54. * @var \Magento\Framework\App\ResponseInterface
  55. */
  56. protected $response;
  57. /**
  58. * @var \Magento\Framework\App\ActionFlag
  59. */
  60. protected $actionFlag;
  61. /**
  62. * @var \Magento\Backend\Helper\Data
  63. */
  64. protected $helper;
  65. /**
  66. * @var \Magento\Backend\Model\Session
  67. */
  68. protected $session;
  69. /**
  70. * Set up test
  71. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  72. */
  73. protected function setUp()
  74. {
  75. $this->contextMock = $this->createPartialMock(\Magento\Backend\App\Action\Context::class, [
  76. 'getAuthorization',
  77. 'getSession',
  78. 'getActionFlag',
  79. 'getAuth',
  80. 'getView',
  81. 'getHelper',
  82. 'getBackendUrl',
  83. 'getFormKeyValidator',
  84. 'getLocaleResolver',
  85. 'getCanUseBaseUrl',
  86. 'getRequest',
  87. 'getResponse',
  88. 'getObjectManager',
  89. 'getMessageManager'
  90. ]);
  91. $this->response = $this->createPartialMock(
  92. \Magento\Framework\App\ResponseInterface::class,
  93. ['setRedirect', 'sendResponse']
  94. );
  95. $this->view = $this->createPartialMock(\Magento\Framework\App\ViewInterface::class, [
  96. 'loadLayout',
  97. 'getPage',
  98. 'getConfig',
  99. 'getTitle',
  100. 'renderLayout',
  101. 'loadLayoutUpdates',
  102. 'getDefaultLayoutHandle',
  103. 'addPageLayoutHandles',
  104. 'generateLayoutBlocks',
  105. 'generateLayoutXml',
  106. 'getLayout',
  107. 'addActionLayoutHandles',
  108. 'setIsLayoutLoaded',
  109. 'isLayoutLoaded'
  110. ]);
  111. $this->session = $this->createPartialMock(\Magento\Backend\Model\Session::class, ['setIsUrlNotice']);
  112. $this->session->expects($this->any())->method('setIsUrlNotice')->willReturn($this->objectManager);
  113. $this->actionFlag = $this->createPartialMock(\Magento\Framework\App\ActionFlag::class, ['get']);
  114. $this->actionFlag->expects($this->any())->method("get")->willReturn($this->objectManager);
  115. $this->objectManager = $this->createPartialMock(
  116. \Magento\Framework\TestFramework\Unit\Helper\ObjectManager::class,
  117. ['get']
  118. );
  119. $this->request = $this->getMockForAbstractClass(
  120. \Magento\Framework\App\RequestInterface::class,
  121. ['getParam', 'getRequest'],
  122. '',
  123. false
  124. );
  125. $this->response->expects($this->any())->method("setRedirect")->willReturn(1);
  126. $this->page = $this->createMock(\Magento\Framework\View\Result\Page::class);
  127. $this->config = $this->createMock(\Magento\Framework\View\Result\Page::class);
  128. $this->title = $this->createMock(\Magento\Framework\View\Page\Title::class);
  129. $this->messageManager = $this->getMockForAbstractClass(
  130. \Magento\Framework\Message\ManagerInterface::class,
  131. ['addError', 'addSuccess'],
  132. '',
  133. false
  134. );
  135. $this->indexReg = $this->createPartialMock(
  136. \Magento\Framework\Indexer\IndexerRegistry::class,
  137. ['get', 'setScheduled']
  138. );
  139. $this->helper = $this->createPartialMock(\Magento\Backend\Helper\Data::class, ['getUrl']);
  140. $this->contextMock->expects($this->any())->method("getObjectManager")->willReturn($this->objectManager);
  141. $this->contextMock->expects($this->any())->method("getRequest")->willReturn($this->request);
  142. $this->contextMock->expects($this->any())->method("getResponse")->willReturn($this->response);
  143. $this->contextMock->expects($this->any())->method("getMessageManager")->willReturn($this->messageManager);
  144. $this->contextMock->expects($this->any())->method("getSession")->willReturn($this->session);
  145. $this->contextMock->expects($this->any())->method("getActionFlag")->willReturn($this->actionFlag);
  146. $this->contextMock->expects($this->any())->method("getHelper")->willReturn($this->helper);
  147. }
  148. /**
  149. * @param array $indexerIds
  150. * @param \Exception $exception
  151. * @param array $expectsExceptionValues
  152. * @dataProvider executeDataProvider
  153. */
  154. public function testExecute($indexerIds, $exception, $expectsExceptionValues)
  155. {
  156. $this->model = new \Magento\Indexer\Controller\Adminhtml\Indexer\MassChangelog($this->contextMock);
  157. $this->request->expects($this->any())
  158. ->method('getParam')->with('indexer_ids')
  159. ->will($this->returnValue($indexerIds));
  160. if (!is_array($indexerIds)) {
  161. $this->messageManager->expects($this->once())
  162. ->method('addError')->with(__('Please select indexers.'))
  163. ->will($this->returnValue(1));
  164. } else {
  165. $this->objectManager->expects($this->any())
  166. ->method('get')->with(\Magento\Framework\Indexer\IndexerRegistry::class)
  167. ->will($this->returnValue($this->indexReg));
  168. $indexerInterface = $this->getMockForAbstractClass(
  169. \Magento\Framework\Indexer\IndexerInterface::class,
  170. ['setScheduled'],
  171. '',
  172. false
  173. );
  174. $this->indexReg->expects($this->any())
  175. ->method('get')->with(1)
  176. ->will($this->returnValue($indexerInterface));
  177. if ($exception !== null) {
  178. $indexerInterface->expects($this->any())
  179. ->method('setScheduled')->with(true)->will($this->throwException($exception));
  180. } else {
  181. $indexerInterface->expects($this->any())
  182. ->method('setScheduled')->with(true)->will($this->returnValue(1));
  183. }
  184. $this->messageManager->expects($this->any())->method('addSuccess')->will($this->returnValue(1));
  185. if ($exception !== null) {
  186. $this->messageManager
  187. ->expects($this->exactly($expectsExceptionValues[2]))
  188. ->method('addError')
  189. ->with($exception->getMessage());
  190. $this->messageManager->expects($this->exactly($expectsExceptionValues[1]))
  191. ->method('addException')
  192. ->with($exception, "We couldn't change indexer(s)' mode because of an error.");
  193. }
  194. }
  195. $this->helper->expects($this->any())->method("getUrl")->willReturn("magento.com");
  196. $this->response->expects($this->any())->method("setRedirect")->willReturn(1);
  197. $result = $this->model->execute();
  198. $this->assertNull($result);
  199. }
  200. /**
  201. * @return array
  202. */
  203. public function executeDataProvider()
  204. {
  205. return [
  206. 'set1' => [
  207. 'idexers' => 1,
  208. "exception" => null,
  209. "expectsValues" => [0, 0, 0]
  210. ],
  211. 'set2' => [
  212. 'idexers' => [1],
  213. "exception" => null,
  214. "expectsException" => [1, 0, 0]
  215. ],
  216. 'set3' => [
  217. 'idexers' => [1],
  218. "exception" => new \Magento\Framework\Exception\LocalizedException(__('Test Phrase')),
  219. "expectsException" => [0, 0, 1]
  220. ],
  221. 'set4' => [
  222. 'idexers' => [1],
  223. "exception" => new \Exception(),
  224. "expectsException" => [0, 1, 0]
  225. ]
  226. ];
  227. }
  228. }