Skip to content

Commit fbcb483

Browse files
author
Michael Logvin
committed
MAGETWO-44627: Cart Price Rules are applied irrespective of store Timezone
1 parent 4499554 commit fbcb483

File tree

3 files changed

+101
-5
lines changed

3 files changed

+101
-5
lines changed

app/code/Magento/SalesRule/Model/ResourceModel/Rule/Collection.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class Collection extends \Magento\Rule\Model\ResourceModel\Rule\Collection\Abstr
3434
];
3535

3636
/**
37-
* @var \Magento\Framework\Stdlib\DateTime\DateTime
37+
* @var \Magento\Framework\Stdlib\DateTime\TimezoneInterface
3838
*/
3939
protected $_date;
4040

@@ -43,7 +43,7 @@ class Collection extends \Magento\Rule\Model\ResourceModel\Rule\Collection\Abstr
4343
* @param \Psr\Log\LoggerInterface $logger
4444
* @param \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy
4545
* @param \Magento\Framework\Event\ManagerInterface $eventManager
46-
* @param \Magento\Framework\Stdlib\DateTime\DateTime $date
46+
* @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $date
4747
* @param mixed $connection
4848
* @param \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource
4949
*/
@@ -52,7 +52,7 @@ public function __construct(
5252
\Psr\Log\LoggerInterface $logger,
5353
\Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy,
5454
\Magento\Framework\Event\ManagerInterface $eventManager,
55-
\Magento\Framework\Stdlib\DateTime\DateTime $date,
55+
\Magento\Framework\Stdlib\DateTime\TimezoneInterface $date,
5656
\Magento\Framework\DB\Adapter\AdapterInterface $connection = null,
5757
\Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null
5858
) {
@@ -130,7 +130,7 @@ public function setValidationFilter($websiteId, $customerGroupId, $couponCode =
130130
),
131131
$connection->quoteInto(
132132
'(rule_coupons.expiration_date IS NULL OR rule_coupons.expiration_date >= ?)',
133-
$this->_date->date('Y-m-d')
133+
$this->_date->date()->format('Y-m-d')
134134
),
135135
];
136136

@@ -166,7 +166,7 @@ public function addWebsiteGroupDateFilter($websiteId, $customerGroupId, $now = n
166166
{
167167
if (!$this->getFlag('website_group_date_filter')) {
168168
if (is_null($now)) {
169-
$now = $this->_date->date('Y-m-d');
169+
$now = $this->_date->date()->format('Y-m-d');
170170
}
171171

172172
$this->addWebsiteFilter($websiteId);

dev/tests/integration/testsuite/Magento/SalesRule/Model/ResourceModel/Rule/CollectionTest.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,71 @@ public function setValidationFilterDataProvider()
4747
'Check result with wrong code' => ['wrong_code', ['#5']]
4848
];
4949
}
50+
51+
/**
52+
* Test checks timezone when timezone is not changed
53+
*
54+
* @magentoDbIsolation enabled
55+
* @magentoAppIsolation enabled
56+
* @magentoDataFixture Magento/SalesRule/_files/rule_specific_date.php
57+
* @magentoConfigFixture general/locale/timezone Europe/Kiev
58+
*/
59+
public function testMultiRulesWithTimezone()
60+
{
61+
$this->setSpecificTimezone('Europe/Kiev');
62+
$collection = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
63+
'Magento\SalesRule\Model\ResourceModel\Rule\Collection'
64+
);
65+
$collection->addWebsiteGroupDateFilter(1, 0);
66+
$items = array_values($collection->getItems());
67+
$this->assertNotEmpty($items);
68+
}
69+
70+
/**
71+
* Test checks timezone when timezone is shifted to locale where current day already +1
72+
* (e.g. existed rule: from day 2000-01-01 to day 2000-01-01, after changing timezone 2000-01-02)
73+
*
74+
* @magentoDbIsolation enabled
75+
* @magentoAppIsolation enabled
76+
* @magentoDataFixture Magento/SalesRule/_files/rule_specific_date.php
77+
* @magentoConfigFixture general/locale/timezone Australia/Sydney
78+
*/
79+
public function testMultiRulesWithDifferentTimezone()
80+
{
81+
$this->setSpecificTimezone('Australia/Sydney');
82+
$collection = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
83+
'Magento\SalesRule\Model\ResourceModel\Rule\Collection'
84+
);
85+
$collection->addWebsiteGroupDateFilter(1, 0);
86+
$items = array_values($collection->getItems());
87+
$this->assertEmpty($items);
88+
}
89+
90+
protected function setSpecificTimezone($timezone)
91+
{
92+
$localeData = [
93+
'section' => 'general',
94+
'website' => null,
95+
'store' => null,
96+
'groups' => [
97+
'locale' => [
98+
'fields' => [
99+
'timezone' => [
100+
'value' => $timezone
101+
]
102+
]
103+
]
104+
]
105+
];
106+
\Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create('Magento\Config\Model\Config\Factory')
107+
->create()
108+
->addData($localeData)
109+
->save();
110+
}
111+
112+
public function tearDown()
113+
{
114+
// restore default timezone
115+
$this->setSpecificTimezone('America/Los_Angeles');
116+
}
50117
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
/** @var \Magento\SalesRule\Model\Rule $rule */
8+
$rule = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create('Magento\SalesRule\Model\Rule');
9+
$rule->setName(
10+
'#1'
11+
)->setIsActive(
12+
1
13+
)->setStopRulesProcessing(
14+
0
15+
)->setIsAdvanced(
16+
1
17+
)->setCouponType(
18+
Magento\SalesRule\Model\Rule::COUPON_TYPE_NO_COUPON
19+
)->setUseAutoGeneration(
20+
0
21+
)->setWebsiteIds(
22+
'1'
23+
)->setCustomerGroupIds(
24+
'0'
25+
)->setFromDate(
26+
date('Y-m-d')
27+
)->setToDate(
28+
date('Y-m-d')
29+
)->save();

0 commit comments

Comments
 (0)