DeleteOutdatedPriceValuesTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Catalog\Cron;
  7. use Magento\Catalog\Api\Data\ProductAttributeInterface;
  8. use Magento\Catalog\Api\ProductRepositoryInterface;
  9. use Magento\Catalog\Observer\SwitchPriceAttributeScopeOnConfigChange;
  10. use Magento\Framework\App\Config\MutableScopeConfigInterface;
  11. use Magento\Framework\App\Config\ReinitableConfigInterface;
  12. use Magento\Framework\App\Config\ScopeConfigInterface;
  13. /**
  14. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  15. */
  16. class DeleteOutdatedPriceValuesTest extends \PHPUnit\Framework\TestCase
  17. {
  18. /**
  19. * @var \Magento\Catalog\Cron\DeleteOutdatedPriceValues
  20. */
  21. private $cron;
  22. /**
  23. * @var ProductRepositoryInterface
  24. */
  25. private $productRepository;
  26. /**
  27. * @var \Magento\Store\Model\Store
  28. */
  29. private $store;
  30. /**
  31. * @var \Magento\Framework\ObjectManagerInterface
  32. */
  33. private $objectManager;
  34. public function setUp()
  35. {
  36. $this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  37. $this->productRepository = $this->objectManager->create(ProductRepositoryInterface::class);
  38. $this->store = $this->objectManager->create(\Magento\Store\Model\Store::class);
  39. $this->cron = $this->objectManager->create(\Magento\Catalog\Cron\DeleteOutdatedPriceValues::class);
  40. }
  41. /**
  42. * @magentoDataFixture Magento/Catalog/_files/product_simple.php
  43. * @magentoDataFixture Magento/Store/_files/second_website_with_two_stores.php
  44. * @magentoConfigFixture current_store catalog/price/scope 1
  45. * @magentoDbIsolation disabled
  46. * @magentoAppIsolation enabled
  47. */
  48. public function testExecute()
  49. {
  50. $defaultStorePrice = 10.00;
  51. $secondStorePrice = 9.99;
  52. $secondStoreId = $this->store->load('fixture_second_store')->getId();
  53. /** @var \Magento\Catalog\Model\Product\Action $productAction */
  54. $productAction = $this->objectManager->create(
  55. \Magento\Catalog\Model\Product\Action::class
  56. );
  57. /** @var ReinitableConfigInterface $reinitiableConfig */
  58. $reinitiableConfig = $this->objectManager->get(ReinitableConfigInterface::class);
  59. $reinitiableConfig->setValue(
  60. 'catalog/price/scope',
  61. \Magento\Store\Model\Store::PRICE_SCOPE_WEBSITE
  62. );
  63. $observer = $this->objectManager->get(\Magento\Framework\Event\Observer::class);
  64. $this->objectManager->get(SwitchPriceAttributeScopeOnConfigChange::class)
  65. ->execute($observer);
  66. $reflection = new \ReflectionClass(\Magento\Catalog\Model\Attribute\ScopeOverriddenValue::class);
  67. $paths = $reflection->getProperty('attributesValues');
  68. $paths->setAccessible(true);
  69. $paths->setValue($this->objectManager->get(\Magento\Catalog\Model\Attribute\ScopeOverriddenValue::class), null);
  70. $paths->setAccessible(false);
  71. $product = $this->productRepository->get('simple');
  72. $productResource = $this->objectManager->create(\Magento\Catalog\Model\ResourceModel\Product::class);
  73. $productId = $product->getId();
  74. $productAction->updateWebsites(
  75. [$productId],
  76. [$this->store->load('fixture_second_store')->getWebsiteId()],
  77. 'add'
  78. );
  79. $product->setStoreId($secondStoreId);
  80. $product->setPrice($secondStorePrice);
  81. $productResource->save($product);
  82. $attribute = $this->objectManager->get(\Magento\Eav\Model\Config::class)
  83. ->getAttribute(
  84. 'catalog_product',
  85. 'price'
  86. );
  87. $this->assertEquals(
  88. $secondStorePrice,
  89. $productResource->getAttributeRawValue($productId, $attribute->getId(), $secondStoreId)
  90. );
  91. /** @var MutableScopeConfigInterface $config */
  92. $config = $this->objectManager->get(
  93. MutableScopeConfigInterface::class
  94. );
  95. $config->setValue(
  96. \Magento\Store\Model\Store::XML_PATH_PRICE_SCOPE,
  97. null,
  98. ScopeConfigInterface::SCOPE_TYPE_DEFAULT
  99. );
  100. $this->cron->execute();
  101. $this->assertEquals(
  102. $secondStorePrice,
  103. $productResource->getAttributeRawValue($productId, $attribute->getId(), $secondStoreId)
  104. );
  105. $config->setValue(
  106. \Magento\Store\Model\Store::XML_PATH_PRICE_SCOPE,
  107. \Magento\Store\Model\Store::PRICE_SCOPE_GLOBAL,
  108. ScopeConfigInterface::SCOPE_TYPE_DEFAULT
  109. );
  110. /** @var \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig */
  111. $this->cron->execute();
  112. $this->assertEquals(
  113. $defaultStorePrice,
  114. $productResource->getAttributeRawValue($productId, $attribute->getId(), $secondStoreId)
  115. );
  116. }
  117. public function tearDown()
  118. {
  119. parent::tearDown();
  120. /** @var ReinitableConfigInterface $reinitiableConfig */
  121. $reinitiableConfig = $this->objectManager->get(ReinitableConfigInterface::class);
  122. $reinitiableConfig->setValue(
  123. 'catalog/price/scope',
  124. \Magento\Store\Model\Store::PRICE_SCOPE_GLOBAL
  125. );
  126. $observer = $this->objectManager->get(\Magento\Framework\Event\Observer::class);
  127. $this->objectManager->get(SwitchPriceAttributeScopeOnConfigChange::class)
  128. ->execute($observer);
  129. }
  130. }