MassDisableTest.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Backend\Test\Unit\Controller\Adminhtml\Cache;
  7. use PHPUnit_Framework_MockObject_MockObject as MockObject;
  8. use Magento\Backend\Controller\Adminhtml\Cache\MassDisable;
  9. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
  10. use Magento\Framework\App\State;
  11. use Magento\Backend\App\Action\Context;
  12. use Magento\Framework\Message\ManagerInterface as MessageManager;
  13. use Magento\Framework\Controller\ResultFactory;
  14. use Magento\Backend\Model\View\Result\Redirect;
  15. use Magento\Framework\App\RequestInterface as Request;
  16. use Magento\Framework\App\Cache\TypeListInterface as CacheTypeList;
  17. use Magento\Framework\App\Cache\StateInterface as CacheState;
  18. /**
  19. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  20. */
  21. class MassDisableTest extends \PHPUnit\Framework\TestCase
  22. {
  23. /**
  24. * @var MassDisable
  25. */
  26. private $controller;
  27. /**
  28. * @var State|MockObject
  29. */
  30. private $stateMock;
  31. /**
  32. * @var MessageManager|MockObject
  33. */
  34. private $messageManagerMock;
  35. /**
  36. * @var Redirect|MockObject
  37. */
  38. private $redirectMock;
  39. /**
  40. * @var Request|MockObject
  41. */
  42. private $requestMock;
  43. /**
  44. * @var CacheTypeList|MockObject
  45. */
  46. private $cacheTypeListMock;
  47. /**
  48. * @var CacheState|MockObject
  49. */
  50. private $cacheStateMock;
  51. protected function setUp()
  52. {
  53. $objectManagerHelper = new ObjectManagerHelper($this);
  54. $this->stateMock = $this->getMockBuilder(State::class)
  55. ->disableOriginalConstructor()
  56. ->disableOriginalClone()
  57. ->getMock();
  58. $this->messageManagerMock = $this->getMockBuilder(MessageManager::class)
  59. ->getMockForAbstractClass();
  60. $this->requestMock = $this->getMockBuilder(Request::class)
  61. ->getMockForAbstractClass();
  62. $this->cacheTypeListMock = $this->getMockBuilder(CacheTypeList::class)
  63. ->getMockForAbstractClass();
  64. $this->cacheStateMock = $this->getMockBuilder(CacheState::class)
  65. ->getMockForAbstractClass();
  66. $this->redirectMock = $this->getMockBuilder(Redirect::class)
  67. ->disableOriginalConstructor()
  68. ->disableOriginalClone()
  69. ->getMock();
  70. $this->redirectMock->expects($this->once())
  71. ->method('setPath')
  72. ->with('adminhtml/*')
  73. ->willReturnSelf();
  74. $resultFactoryMock = $this->getMockBuilder(ResultFactory::class)
  75. ->disableOriginalConstructor()
  76. ->disableOriginalClone()
  77. ->getMock();
  78. $resultFactoryMock->expects($this->once())
  79. ->method('create')
  80. ->with(ResultFactory::TYPE_REDIRECT)
  81. ->willReturn($this->redirectMock);
  82. $contextMock = $this->getMockBuilder(Context::class)
  83. ->disableOriginalConstructor()
  84. ->disableOriginalClone()
  85. ->getMock();
  86. $contextMock->expects($this->once())
  87. ->method('getMessageManager')
  88. ->willReturn($this->messageManagerMock);
  89. $contextMock->expects($this->once())
  90. ->method('getResultFactory')
  91. ->willReturn($resultFactoryMock);
  92. $contextMock->expects($this->once())
  93. ->method('getRequest')
  94. ->willReturn($this->requestMock);
  95. $this->controller = $objectManagerHelper->getObject(
  96. MassDisable::class,
  97. [
  98. 'context' => $contextMock,
  99. 'cacheTypeList' => $this->cacheTypeListMock,
  100. 'cacheState' => $this->cacheStateMock
  101. ]
  102. );
  103. $objectManagerHelper->setBackwardCompatibleProperty($this->controller, 'state', $this->stateMock);
  104. }
  105. public function testExecuteInProductionMode()
  106. {
  107. $this->stateMock->expects($this->once())
  108. ->method('getMode')
  109. ->willReturn(State::MODE_PRODUCTION);
  110. $this->messageManagerMock->expects($this->once())
  111. ->method('addErrorMessage')
  112. ->with('You can\'t change status of cache type(s) in production mode', null)
  113. ->willReturnSelf();
  114. $this->assertSame($this->redirectMock, $this->controller->execute());
  115. }
  116. public function testExecuteInvalidTypeCache()
  117. {
  118. $this->stateMock->expects($this->once())
  119. ->method('getMode')
  120. ->willReturn(State::MODE_DEVELOPER);
  121. $this->cacheTypeListMock->expects($this->once())
  122. ->method('getTypes')
  123. ->willReturn([
  124. 'pageCache' => [
  125. 'id' => 'pageCache',
  126. 'label' => 'Cache of Page'
  127. ]
  128. ]);
  129. $this->requestMock->expects($this->once())
  130. ->method('getParam')
  131. ->with('types')
  132. ->willReturn(['someCache']);
  133. $this->messageManagerMock->expects($this->once())
  134. ->method('addErrorMessage')
  135. ->with('These cache type(s) don\'t exist: someCache')
  136. ->willReturnSelf();
  137. $this->assertSame($this->redirectMock, $this->controller->execute());
  138. }
  139. public function testExecuteWithException()
  140. {
  141. $exception = new \Exception();
  142. $this->stateMock->expects($this->once())
  143. ->method('getMode')
  144. ->willReturn(State::MODE_DEVELOPER);
  145. $this->requestMock->expects($this->once())
  146. ->method('getParam')
  147. ->willThrowException($exception);
  148. $this->messageManagerMock->expects($this->once())
  149. ->method('addExceptionMessage')
  150. ->with($exception, 'An error occurred while disabling cache.')
  151. ->willReturnSelf();
  152. $this->assertSame($this->redirectMock, $this->controller->execute());
  153. }
  154. public function testExecuteSuccess()
  155. {
  156. $cacheType = 'pageCache';
  157. $this->stateMock->expects($this->once())
  158. ->method('getMode')
  159. ->willReturn(State::MODE_DEVELOPER);
  160. $this->cacheTypeListMock->expects($this->once())
  161. ->method('getTypes')
  162. ->willReturn([
  163. 'pageCache' => [
  164. 'id' => 'pageCache',
  165. 'label' => 'Cache of Page'
  166. ]
  167. ]);
  168. $this->requestMock->expects($this->once())
  169. ->method('getParam')
  170. ->with('types')
  171. ->willReturn([$cacheType]);
  172. $this->cacheStateMock->expects($this->once())
  173. ->method('isEnabled')
  174. ->with($cacheType)
  175. ->willReturn(true);
  176. $this->cacheStateMock->expects($this->once())
  177. ->method('setEnabled')
  178. ->with($cacheType, false);
  179. $this->cacheStateMock->expects($this->once())
  180. ->method('persist');
  181. $this->messageManagerMock->expects($this->once())
  182. ->method('addSuccessMessage')
  183. ->with('1 cache type(s) disabled.')
  184. ->willReturnSelf();
  185. $this->assertSame($this->redirectMock, $this->controller->execute());
  186. }
  187. }