BaseUrlConfigPluginTest.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Analytics\Model\Plugin;
  7. use Magento\Analytics\Model\Config\Backend\Baseurl\SubscriptionUpdateHandler;
  8. use Magento\Config\Model\PreparedValueFactory;
  9. use Magento\Config\Model\ResourceModel\Config\Data as ConfigData;
  10. use Magento\Framework\App\Config\ScopeConfigInterface;
  11. use Magento\Framework\FlagManager;
  12. use Magento\Framework\ObjectManagerInterface;
  13. use Magento\Store\Model\ScopeInterface;
  14. use Magento\Store\Model\Store;
  15. use Magento\TestFramework\Helper\Bootstrap;
  16. /**
  17. * @magentoAppArea adminhtml
  18. */
  19. class BaseUrlConfigPluginTest extends \PHPUnit\Framework\TestCase
  20. {
  21. /**
  22. * @var PreparedValueFactory
  23. */
  24. private $preparedValueFactory;
  25. /**
  26. * @var ConfigData
  27. */
  28. private $configValueResourceModel;
  29. /**
  30. * @var ScopeConfigInterface
  31. */
  32. private $scopeConfig;
  33. /**
  34. * @var FlagManager
  35. */
  36. private $flagManager;
  37. /**
  38. * @var ObjectManagerInterface
  39. */
  40. private $objectManager;
  41. /**
  42. * @return void
  43. */
  44. protected function setUp()
  45. {
  46. $this->objectManager = Bootstrap::getObjectManager();
  47. $this->preparedValueFactory = $this->objectManager->get(PreparedValueFactory::class);
  48. $this->configValueResourceModel = $this->objectManager->get(ConfigData::class);
  49. $this->scopeConfig = $this->objectManager->get(ScopeConfigInterface::class);
  50. $this->flagManager = $this->objectManager->get(FlagManager::class);
  51. }
  52. /**
  53. * @magentoDbIsolation enabled
  54. */
  55. public function testAfterSaveNotSecureUrl()
  56. {
  57. $this->saveConfigValue(
  58. Store::XML_PATH_UNSECURE_BASE_URL,
  59. 'http://store.com/',
  60. ScopeConfigInterface::SCOPE_TYPE_DEFAULT
  61. );
  62. $this->assertCronWasNotSet();
  63. }
  64. /**
  65. * @magentoDbIsolation enabled
  66. */
  67. public function testAfterSaveSecureUrlNotInDefaultScope()
  68. {
  69. $this->saveConfigValue(
  70. Store::XML_PATH_SECURE_BASE_URL,
  71. 'https://store.com/',
  72. ScopeInterface::SCOPE_STORES
  73. );
  74. $this->assertCronWasNotSet();
  75. }
  76. /**
  77. * @magentoDbIsolation enabled
  78. * @magentoAdminConfigFixture web/secure/base_url https://previous.example.com/
  79. */
  80. public function testAfterSaveSecureUrlInDefaultScopeOnDoesNotRegisteredInstance()
  81. {
  82. $this->saveConfigValue(
  83. Store::XML_PATH_SECURE_BASE_URL,
  84. 'https://store.com/',
  85. ScopeConfigInterface::SCOPE_TYPE_DEFAULT
  86. );
  87. $this->assertCronWasNotSet();
  88. }
  89. /**
  90. * @magentoDbIsolation enabled
  91. * @magentoAdminConfigFixture web/secure/base_url https://previous.example.com/
  92. * @magentoAdminConfigFixture analytics/general/token MBI_token
  93. */
  94. public function testAfterSaveSecureUrlInDefaultScopeOnRegisteredInstance()
  95. {
  96. $this->saveConfigValue(
  97. Store::XML_PATH_SECURE_BASE_URL,
  98. 'https://store.com/',
  99. ScopeConfigInterface::SCOPE_TYPE_DEFAULT
  100. );
  101. $this->assertCronWasSet();
  102. }
  103. /**
  104. * @magentoDbIsolation enabled
  105. * @magentoAdminConfigFixture web/secure/base_url https://previous.example.com/
  106. * @magentoAdminConfigFixture analytics/general/token MBI_token
  107. */
  108. public function testAfterSaveMultipleBaseUrlChanges()
  109. {
  110. $this->saveConfigValue(
  111. Store::XML_PATH_SECURE_BASE_URL,
  112. 'https://store.com/',
  113. ScopeConfigInterface::SCOPE_TYPE_DEFAULT
  114. );
  115. $this->saveConfigValue(
  116. Store::XML_PATH_SECURE_BASE_URL,
  117. 'https://store10.com/',
  118. ScopeConfigInterface::SCOPE_TYPE_DEFAULT
  119. );
  120. $this->assertCronWasSet();
  121. }
  122. /**
  123. * @param string $path The configuration path in format section/group/field_name
  124. * @param string $value The configuration value
  125. * @param string $scope The configuration scope (default, website, or store)
  126. * @return void
  127. */
  128. private function saveConfigValue(string $path, string $value, string $scope)
  129. {
  130. $value = $this->preparedValueFactory->create(
  131. $path,
  132. $value,
  133. $scope
  134. );
  135. $this->configValueResourceModel->save($value);
  136. }
  137. /**
  138. * @return void
  139. */
  140. private function assertCronWasNotSet()
  141. {
  142. $this->assertNull($this->getSubscriptionUpdateSchedule());
  143. $this->assertNull($this->getPreviousUpdateUrl());
  144. $this->assertNull($this->getUpdateReverseCounter());
  145. }
  146. /**
  147. * @return void
  148. */
  149. private function assertCronWasSet()
  150. {
  151. $this->assertSame(
  152. '0 * * * *',
  153. $this->getSubscriptionUpdateSchedule(),
  154. 'Subscription update schedule has not been set'
  155. );
  156. $this->assertSame(
  157. 'https://previous.example.com/',
  158. $this->getPreviousUpdateUrl(),
  159. 'The previous URL stored for update is not correct'
  160. );
  161. $this->assertSame(48, $this->getUpdateReverseCounter());
  162. }
  163. /**
  164. * @return mixed
  165. */
  166. private function getSubscriptionUpdateSchedule()
  167. {
  168. return $this->scopeConfig->getValue(
  169. SubscriptionUpdateHandler::UPDATE_CRON_STRING_PATH,
  170. ScopeConfigInterface::SCOPE_TYPE_DEFAULT
  171. );
  172. }
  173. /**
  174. * @return mixed
  175. */
  176. private function getPreviousUpdateUrl()
  177. {
  178. return $this->flagManager->getFlagData(SubscriptionUpdateHandler::PREVIOUS_BASE_URL_FLAG_CODE);
  179. }
  180. /**
  181. * @return mixed
  182. */
  183. private function getUpdateReverseCounter()
  184. {
  185. return $this->flagManager
  186. ->getFlagData(SubscriptionUpdateHandler::SUBSCRIPTION_UPDATE_REVERSE_COUNTER_FLAG_CODE);
  187. }
  188. }