Skip to content

Commit 38f729b

Browse files
committed
MC-18954: Nightly build jobs were failing lately after un-skipping tests in MC-5777
1 parent 1873ddd commit 38f729b

File tree

3 files changed

+139
-105
lines changed

3 files changed

+139
-105
lines changed

app/code/Magento/CatalogRule/Model/Indexer/ReindexRuleProduct.php

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,18 @@
88

99
use Magento\CatalogRule\Model\Indexer\IndexerTableSwapperInterface as TableSwapper;
1010
use Magento\Catalog\Model\ResourceModel\Indexer\ActiveTableSwitcher;
11-
use Magento\Framework\App\ObjectManager;
11+
use Magento\CatalogRule\Model\Rule;
12+
use Magento\Framework\App\ResourceConnection;
13+
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
14+
use Magento\Store\Model\ScopeInterface;
1215

1316
/**
1417
* Reindex rule relations with products.
1518
*/
1619
class ReindexRuleProduct
1720
{
1821
/**
19-
* @var \Magento\Framework\App\ResourceConnection
22+
* @var ResourceConnection
2023
*/
2124
private $resource;
2225

@@ -31,36 +34,40 @@ class ReindexRuleProduct
3134
private $tableSwapper;
3235

3336
/**
34-
* @param \Magento\Framework\App\ResourceConnection $resource
37+
* @var TimezoneInterface
38+
*/
39+
private $localeDate;
40+
41+
/**
42+
* @param ResourceConnection $resource
3543
* @param ActiveTableSwitcher $activeTableSwitcher
36-
* @param TableSwapper|null $tableSwapper
44+
* @param TableSwapper $tableSwapper
45+
* @param TimezoneInterface $localeDate
3746
*/
3847
public function __construct(
39-
\Magento\Framework\App\ResourceConnection $resource,
48+
ResourceConnection $resource,
4049
ActiveTableSwitcher $activeTableSwitcher,
41-
TableSwapper $tableSwapper = null
50+
TableSwapper $tableSwapper,
51+
TimezoneInterface $localeDate
4252
) {
4353
$this->resource = $resource;
4454
$this->activeTableSwitcher = $activeTableSwitcher;
45-
$this->tableSwapper = $tableSwapper ??
46-
ObjectManager::getInstance()->get(TableSwapper::class);
55+
$this->tableSwapper = $tableSwapper;
56+
$this->localeDate = $localeDate;
4757
}
4858

4959
/**
5060
* Reindex information about rule relations with products.
5161
*
52-
* @param \Magento\CatalogRule\Model\Rule $rule
62+
* @param Rule $rule
5363
* @param int $batchCount
5464
* @param bool $useAdditionalTable
5565
* @return bool
5666
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
5767
* @SuppressWarnings(PHPMD.NPathComplexity)
5868
*/
59-
public function execute(
60-
\Magento\CatalogRule\Model\Rule $rule,
61-
$batchCount,
62-
$useAdditionalTable = false
63-
) {
69+
public function execute(Rule $rule, $batchCount, $useAdditionalTable = false)
70+
{
6471
if (!$rule->getIsActive() || empty($rule->getWebsiteIds())) {
6572
return false;
6673
}
@@ -84,21 +91,26 @@ public function execute(
8491

8592
$ruleId = $rule->getId();
8693
$customerGroupIds = $rule->getCustomerGroupIds();
87-
$fromTime = strtotime($rule->getFromDate());
88-
$toTime = strtotime($rule->getToDate());
89-
$toTime = $toTime ? $toTime + \Magento\CatalogRule\Model\Indexer\IndexBuilder::SECONDS_IN_DAY - 1 : 0;
9094
$sortOrder = (int)$rule->getSortOrder();
9195
$actionOperator = $rule->getSimpleAction();
9296
$actionAmount = $rule->getDiscountAmount();
9397
$actionStop = $rule->getStopRulesProcessing();
9498

9599
$rows = [];
100+
foreach ($websiteIds as $websiteId) {
101+
$scopeTz = new \DateTimeZone(
102+
$this->localeDate->getConfigTimezone(ScopeInterface::SCOPE_WEBSITE, $websiteId)
103+
);
104+
$fromTime = (new \DateTime($rule->getFromDate(), $scopeTz))->getTimestamp();
105+
$toTime = $rule->getToDate()
106+
? (new \DateTime($rule->getToDate(), $scopeTz))->getTimestamp() + IndexBuilder::SECONDS_IN_DAY - 1
107+
: 0;
96108

97-
foreach ($productIds as $productId => $validationByWebsite) {
98-
foreach ($websiteIds as $websiteId) {
109+
foreach ($productIds as $productId => $validationByWebsite) {
99110
if (empty($validationByWebsite[$websiteId])) {
100111
continue;
101112
}
113+
102114
foreach ($customerGroupIds as $customerGroupId) {
103115
$rows[] = [
104116
'rule_id' => $ruleId,
@@ -123,6 +135,7 @@ public function execute(
123135
if (!empty($rows)) {
124136
$connection->insertMultiple($indexTable, $rows);
125137
}
138+
126139
return true;
127140
}
128141
}

app/code/Magento/CatalogRule/Test/Unit/Model/Indexer/ReindexRuleProductPriceTest.php

Lines changed: 20 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use Magento\CatalogRule\Model\Indexer\ReindexRuleProductPrice;
1212
use Magento\CatalogRule\Model\Indexer\RuleProductPricesPersistor;
1313
use Magento\CatalogRule\Model\Indexer\RuleProductsSelectBuilder;
14-
use Magento\Framework\Stdlib\DateTime\DateTime;
1514
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
1615
use Magento\Store\Api\Data\GroupInterface;
1716
use Magento\Store\Api\Data\WebsiteInterface;
@@ -41,65 +40,60 @@ class ReindexRuleProductPriceTest extends \PHPUnit\Framework\TestCase
4140
private $productPriceCalculatorMock;
4241

4342
/**
44-
* @var DateTime|MockObject
43+
* @var TimezoneInterface|MockObject
4544
*/
46-
private $dateTimeMock;
45+
private $localeDate;
4746

4847
/**
4948
* @var RuleProductPricesPersistor|MockObject
5049
*/
5150
private $pricesPersistorMock;
5251

53-
/**
54-
* @var TimezoneInterface|MockObject
55-
*/
56-
private $localeDate;
57-
5852
protected function setUp()
5953
{
6054
$this->storeManagerMock = $this->createMock(StoreManagerInterface::class);
6155
$this->ruleProductsSelectBuilderMock = $this->createMock(RuleProductsSelectBuilder::class);
6256
$this->productPriceCalculatorMock = $this->createMock(ProductPriceCalculator::class);
63-
$this->dateTimeMock = $this->createMock(DateTime::class);
64-
$this->pricesPersistorMock = $this->createMock(RuleProductPricesPersistor::class);
6557
$this->localeDate = $this->createMock(TimezoneInterface::class);
58+
$this->pricesPersistorMock = $this->createMock(RuleProductPricesPersistor::class);
6659

6760
$this->model = new ReindexRuleProductPrice(
6861
$this->storeManagerMock,
6962
$this->ruleProductsSelectBuilderMock,
7063
$this->productPriceCalculatorMock,
71-
$this->dateTimeMock,
72-
$this->pricesPersistorMock,
73-
$this->localeDate
64+
$this->localeDate,
65+
$this->pricesPersistorMock
7466
);
7567
}
7668

7769
public function testExecute()
7870
{
7971
$websiteId = 234;
80-
$storeGroupId = 30;
81-
$storeId = 40;
82-
$productMock = $this->createMock(Product::class);
72+
$defaultGroupId = 11;
73+
$defaultStoreId = 22;
8374

8475
$websiteMock = $this->createMock(WebsiteInterface::class);
8576
$websiteMock->expects($this->once())
8677
->method('getId')
8778
->willReturn($websiteId);
8879
$websiteMock->expects($this->once())
8980
->method('getDefaultGroupId')
90-
->willReturn($storeGroupId);
81+
->willReturn($defaultGroupId);
9182
$this->storeManagerMock->expects($this->once())
9283
->method('getWebsites')
9384
->willReturn([$websiteMock]);
94-
$storeGroupMock = $this->createMock(GroupInterface::class);
95-
$storeGroupMock->expects($this->once())
85+
$groupMock = $this->createMock(GroupInterface::class);
86+
$groupMock->method('getId')
87+
->willReturn($defaultStoreId);
88+
$groupMock->expects($this->once())
9689
->method('getDefaultStoreId')
97-
->willReturn($storeId);
90+
->willReturn($defaultStoreId);
9891
$this->storeManagerMock->expects($this->once())
9992
->method('getGroup')
100-
->with($storeGroupId)
101-
->willReturn($storeGroupMock);
93+
->with($defaultGroupId)
94+
->willReturn($groupMock);
10295

96+
$productMock = $this->createMock(Product::class);
10397
$statementMock = $this->createMock(\Zend_Db_Statement_Interface::class);
10498
$this->ruleProductsSelectBuilderMock->expects($this->once())
10599
->method('build')
@@ -115,22 +109,10 @@ public function testExecute()
115109
'action_stop' => true
116110
];
117111

118-
$this->dateTimeMock->expects($this->at(0))
119-
->method('date')
120-
->with('Y-m-d 00:00:00', $ruleData['from_time'])
121-
->willReturn($ruleData['from_time']);
122-
$this->dateTimeMock->expects($this->at(1))
123-
->method('timestamp')
124-
->with($ruleData['from_time'])
125-
->willReturn($ruleData['from_time']);
126-
$this->dateTimeMock->expects($this->at(2))
127-
->method('date')
128-
->with('Y-m-d 00:00:00', $ruleData['to_time'])
129-
->willReturn($ruleData['to_time']);
130-
$this->dateTimeMock->expects($this->at(3))
131-
->method('timestamp')
132-
->with($ruleData['to_time'])
133-
->willReturn($ruleData['to_time']);
112+
$this->localeDate->expects($this->once())
113+
->method('scopeDate')
114+
->with($defaultStoreId, null, true)
115+
->willReturn(new \DateTime());
134116

135117
$statementMock->expects($this->at(0))
136118
->method('fetch')

0 commit comments

Comments
 (0)