Skip to content

Problems with price scope and config.php issue fixed #39865

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 2.4-develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 131 additions & 0 deletions app/code/Magento/Catalog/Cron/DeleteOutdatedPriceValuesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Catalog\Cron;

use Magento\Catalog\Api\Data\ProductAttributeInterface;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Framework\App\Config\MutableScopeConfigInterface;
use Magento\Framework\App\Config\ReinitableConfigInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;

/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class DeleteOutdatedPriceValuesTest extends \PHPUnit\Framework\TestCase
{
/**
* @var \Magento\Catalog\Cron\DeleteOutdatedPriceValues
*/
private $cron;

/**
* @var ProductRepositoryInterface
*/
private $productRepository;

/**
* @var \Magento\Store\Model\Store
*/
private $store;

/**
* @var \Magento\Framework\ObjectManagerInterface
*/
private $objectManager;

protected function setUp(): void
{
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
$this->productRepository = $this->objectManager->create(ProductRepositoryInterface::class);
$this->store = $this->objectManager->create(\Magento\Store\Model\Store::class);
$this->cron = $this->objectManager->create(\Magento\Catalog\Cron\DeleteOutdatedPriceValues::class);
}

/**
* @magentoDataFixture Magento/Catalog/_files/product_simple.php
* @magentoDataFixture Magento/Store/_files/second_website_with_two_stores.php
* @magentoConfigFixture current_store catalog/price/scope 1
* @magentoDbIsolation disabled
* @magentoAppIsolation enabled
*/
public function testExecute()
{
$defaultStorePrice = 10.00;
$secondStorePrice = 9.99;
$secondStoreId = $this->store->load('fixture_second_store')->getId();
/** @var \Magento\Catalog\Model\Product\Action $productAction */
$productAction = $this->objectManager->create(
\Magento\Catalog\Model\Product\Action::class
);
/** @var ReinitableConfigInterface $reinitiableConfig */
$reinitiableConfig = $this->objectManager->get(ReinitableConfigInterface::class);
$reinitiableConfig->setValue(
'catalog/price/scope',
\Magento\Store\Model\Store::PRICE_SCOPE_WEBSITE
);

$reflection = new \ReflectionClass(\Magento\Catalog\Model\Attribute\ScopeOverriddenValue::class);
$paths = $reflection->getProperty('attributesValues');
$paths->setAccessible(true);
$paths->setValue($this->objectManager->get(\Magento\Catalog\Model\Attribute\ScopeOverriddenValue::class), null);
$paths->setAccessible(false);
$product = $this->productRepository->get('simple');
$productResource = $this->objectManager->create(\Magento\Catalog\Model\ResourceModel\Product::class);
$productId = $product->getId();
$productAction->updateWebsites(
[$productId],
[$this->store->load('fixture_second_store')->getWebsiteId()],
'add'
);
$product->setStoreId($secondStoreId);
$product->setPrice($secondStorePrice);
$productResource->save($product);
$attribute = $this->objectManager->get(\Magento\Eav\Model\Config::class)
->getAttribute(
'catalog_product',
'price'
);
$this->assertEquals(
$secondStorePrice,
$productResource->getAttributeRawValue($productId, $attribute->getId(), $secondStoreId)
);
/** @var MutableScopeConfigInterface $config */
$config = $this->objectManager->get(
MutableScopeConfigInterface::class
);
$config->setValue(
\Magento\Store\Model\Store::XML_PATH_PRICE_SCOPE,
null,
ScopeConfigInterface::SCOPE_TYPE_DEFAULT
);
$this->cron->execute();
$this->assertEquals(
$secondStorePrice,
$productResource->getAttributeRawValue($productId, $attribute->getId(), $secondStoreId)
);
$config->setValue(
\Magento\Store\Model\Store::XML_PATH_PRICE_SCOPE,
\Magento\Store\Model\Store::PRICE_SCOPE_GLOBAL,
ScopeConfigInterface::SCOPE_TYPE_DEFAULT
);
/** @var \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig */
$this->cron->execute();
$this->assertEquals(
$defaultStorePrice,
$productResource->getAttributeRawValue($productId, $attribute->getId(), $secondStoreId)
);
}
protected function tearDown(): void
{
parent::tearDown();
/** @var ReinitableConfigInterface $reinitiableConfig */
$reinitiableConfig = $this->objectManager->get(ReinitableConfigInterface::class);
$reinitiableConfig->setValue(
'catalog/price/scope',
\Magento\Store\Model\Store::PRICE_SCOPE_GLOBAL
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
*/
namespace Magento\Catalog\Model\Indexer\Product\Price\System\Config;

use Magento\Catalog\Model\Config\PriceScopeChange;

/**
* Price scope backend model
*/
Expand All @@ -15,15 +17,21 @@ class PriceScope extends \Magento\Framework\App\Config\Value
*/
protected $indexerRegistry;

/**
* @var PriceScopeChange
*/
private $priceScopeChange;

/**
* @param \Magento\Framework\Model\Context $context
* @param \Magento\Framework\Registry $registry
* @param \Magento\Framework\App\Config\ScopeConfigInterface $config
* @param \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList
* @param \Magento\Framework\Indexer\IndexerRegistry $indexerRegistry
* @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
* @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
* @param \Magento\Framework\Model\ResourceModel\AbstractResource|null $resource
* @param \Magento\Framework\Data\Collection\AbstractDb|null $resourceCollection
* @param array $data
* @param PriceScopeChange|null $priceScopeChange
*/
public function __construct(
\Magento\Framework\Model\Context $context,
Expand All @@ -33,10 +41,13 @@ public function __construct(
\Magento\Framework\Indexer\IndexerRegistry $indexerRegistry,
?\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
?\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
array $data = []
array $data = [],
PriceScopeChange $priceScopeChange = null
) {
parent::__construct($context, $registry, $config, $cacheTypeList, $resource, $resourceCollection, $data);
$this->indexerRegistry = $indexerRegistry;
$this->priceScopeChange = $priceScopeChange ?:
\Magento\Framework\App\ObjectManager::getInstance()->get(PriceScopeChange::class);
}

/**
Expand All @@ -61,5 +72,6 @@ public function processValue()
$this->indexerRegistry->get(\Magento\Catalog\Model\Indexer\Product\Price\Processor::INDEXER_ID)
->invalidate();
}
$this->priceScopeChange->changeScope((int)$this->getValue());
}
}
Loading