MassActionTest.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Backend\Controller\Adminhtml\Cache;
  7. use Magento\Framework\App\Cache\State;
  8. use Magento\TestFramework\Helper\Bootstrap;
  9. use Magento\Framework\Config\File\ConfigFilePool;
  10. use Magento\Framework\App\Filesystem\DirectoryList;
  11. use Magento\TestFramework\App\State as AppState;
  12. class MassActionTest extends \Magento\TestFramework\TestCase\AbstractBackendController
  13. {
  14. /**
  15. * Configuration of cache types
  16. *
  17. * @var array
  18. */
  19. private static $typesConfig;
  20. /**
  21. * @var string
  22. */
  23. private $mageState;
  24. public static function setUpBeforeClass()
  25. {
  26. /** @var \Magento\Framework\App\DeploymentConfig $config */
  27. $config = Bootstrap::getObjectManager()->get(\Magento\Framework\App\DeploymentConfig::class);
  28. self::$typesConfig = $config->get(State::CACHE_KEY);
  29. }
  30. protected function setUp()
  31. {
  32. parent::setUp();
  33. $this->mageState = Bootstrap::getObjectManager()->get(AppState::class)->getMode();
  34. }
  35. protected function tearDown()
  36. {
  37. Bootstrap::getObjectManager()->get(AppState::class)->setMode($this->mageState);
  38. /** @var $cacheState \Magento\Framework\App\Cache\StateInterface */
  39. $cacheState = Bootstrap::getObjectManager()->get(\Magento\Framework\App\Cache\StateInterface::class);
  40. foreach (self::$typesConfig as $type => $value) {
  41. $cacheState->setEnabled($type, $value);
  42. }
  43. $cacheState->persist();
  44. parent::tearDown();
  45. }
  46. /**
  47. * @dataProvider massActionsDataProvider
  48. * @param array $typesToEnable
  49. */
  50. public function testMassEnableActionDeveloperMode($typesToEnable = [])
  51. {
  52. $this->setAll(false);
  53. $this->getRequest()->setParams(['types' => $typesToEnable]);
  54. $this->dispatch('backend/admin/cache/massEnable');
  55. foreach ($this->getCacheStates() as $cacheType => $cacheState) {
  56. if (in_array($cacheType, $typesToEnable)) {
  57. $this->assertEquals(1, $cacheState, "Type '{$cacheType}' has not been enabled");
  58. } else {
  59. $this->assertEquals(0, $cacheState, "Type '{$cacheType}' must remain disabled");
  60. }
  61. }
  62. }
  63. /**
  64. * @dataProvider massActionsDataProvider
  65. * @param array $typesToEnable
  66. */
  67. public function testMassEnableActionProductionMode($typesToEnable = [])
  68. {
  69. Bootstrap::getObjectManager()->get(AppState::class)->setMode(AppState::MODE_PRODUCTION);
  70. $this->setAll(false);
  71. $this->getRequest()->setParams(['types' => $typesToEnable]);
  72. $this->dispatch('backend/admin/cache/massEnable');
  73. foreach ($this->getCacheStates() as $cacheType => $cacheState) {
  74. $this->assertEquals(0, $cacheState, "Type '{$cacheType}' must remain disabled");
  75. }
  76. }
  77. /**
  78. * @dataProvider massActionsDataProvider
  79. * @param array $typesToDisable
  80. */
  81. public function testMassDisableActionDeveloperMode($typesToDisable = [])
  82. {
  83. $this->setAll(true);
  84. $this->getRequest()->setParams(['types' => $typesToDisable]);
  85. $this->dispatch('backend/admin/cache/massDisable');
  86. foreach ($this->getCacheStates() as $cacheType => $cacheState) {
  87. if (in_array($cacheType, $typesToDisable)) {
  88. $this->assertEquals(0, $cacheState, "Type '{$cacheType}' has not been disabled");
  89. } else {
  90. $this->assertEquals(1, $cacheState, "Type '{$cacheType}' must remain enabled");
  91. }
  92. }
  93. }
  94. /**
  95. * @dataProvider massActionsDataProvider
  96. * @param array $typesToDisable
  97. */
  98. public function testMassDisableActionProductionMode($typesToDisable = [])
  99. {
  100. Bootstrap::getObjectManager()->get(AppState::class)->setMode(AppState::MODE_PRODUCTION);
  101. $this->setAll(true);
  102. $this->getRequest()->setParams(['types' => $typesToDisable]);
  103. $this->dispatch('backend/admin/cache/massDisable');
  104. foreach ($this->getCacheStates() as $cacheType => $cacheState) {
  105. $this->assertEquals(1, $cacheState, "Type '{$cacheType}' must remain enabled");
  106. }
  107. }
  108. /**
  109. * Retrieve cache states (enabled/disabled) information
  110. *
  111. * Access configuration file directly as it is not possible to re-include modified file under HHVM
  112. * @link https://github.com/facebook/hhvm/issues/1447
  113. *
  114. * @return array
  115. * @SuppressWarnings(PHPMD.EvalExpression)
  116. */
  117. protected function getCacheStates()
  118. {
  119. $configFilePool = new ConfigFilePool();
  120. $configPath = Bootstrap::getInstance()->getAppTempDir() . '/'. DirectoryList::CONFIG .'/'
  121. . $configFilePool->getPath($configFilePool::APP_ENV);
  122. $configData = eval(str_replace('<?php', '', file_get_contents($configPath)));
  123. return $configData[State::CACHE_KEY];
  124. }
  125. /**
  126. * Sets all cache types to enabled or disabled state
  127. *
  128. * @param bool $isEnabled
  129. * @return void
  130. */
  131. private function setAll($isEnabled)
  132. {
  133. /** @var $cacheState \Magento\Framework\App\Cache\StateInterface */
  134. $cacheState = Bootstrap::getObjectManager()->get(\Magento\Framework\App\Cache\StateInterface::class);
  135. foreach (array_keys(self::$typesConfig) as $type) {
  136. $cacheState->setEnabled($type, $isEnabled);
  137. }
  138. $cacheState->persist();
  139. }
  140. /**
  141. * @magentoDataFixture Magento/Backend/controllers/_files/cache/all_types_invalidated.php
  142. * @dataProvider massActionsDataProvider
  143. * @param array $typesToRefresh
  144. */
  145. public function testMassRefreshAction($typesToRefresh = [])
  146. {
  147. $this->getRequest()->setParams(['types' => $typesToRefresh]);
  148. $this->dispatch('backend/admin/cache/massRefresh');
  149. /** @var $cacheTypeList \Magento\Framework\App\Cache\TypeListInterface */
  150. $cacheTypeList = Bootstrap::getObjectManager()->get(\Magento\Framework\App\Cache\TypeListInterface::class);
  151. $invalidatedTypes = array_keys($cacheTypeList->getInvalidated());
  152. $failed = array_intersect($typesToRefresh, $invalidatedTypes);
  153. $this->assertEmpty($failed, 'Could not refresh following cache types: ' . implode(', ', $failed));
  154. }
  155. /**
  156. * @return array
  157. */
  158. public function massActionsDataProvider()
  159. {
  160. return [
  161. 'no types' => [[]],
  162. 'existing types' => [
  163. [
  164. \Magento\Framework\App\Cache\Type\Config::TYPE_IDENTIFIER,
  165. \Magento\Framework\App\Cache\Type\Layout::TYPE_IDENTIFIER,
  166. \Magento\Framework\App\Cache\Type\Block::TYPE_IDENTIFIER,
  167. ],
  168. ]
  169. ];
  170. }
  171. }