Skip to content

Commit f5ccdba

Browse files
MC-40579: [Magento Cloud] Customer Account created time not filtered correctly
1 parent cd857f9 commit f5ccdba

File tree

2 files changed

+51
-10
lines changed

2 files changed

+51
-10
lines changed

app/code/Magento/Customer/Model/ResourceModel/Grid/Collection.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77

88
use Magento\Customer\Model\ResourceModel\Customer;
99
use Magento\Customer\Ui\Component\DataProvider\Document;
10+
use Magento\Framework\App\ObjectManager;
1011
use Magento\Framework\Data\Collection\Db\FetchStrategyInterface as FetchStrategy;
1112
use Magento\Framework\Data\Collection\EntityFactoryInterface as EntityFactory;
1213
use Magento\Framework\Event\ManagerInterface as EventManager;
1314
use Magento\Framework\Locale\ResolverInterface;
15+
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
1416
use Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult;
1517
use Psr\Log\LoggerInterface as Logger;
1618

@@ -24,6 +26,11 @@ class Collection extends SearchResult
2426
*/
2527
private $localeResolver;
2628

29+
/**
30+
* @var TimezoneInterface
31+
*/
32+
private $timeZone;
33+
2734
/**
2835
* @inheritdoc
2936
*/
@@ -42,6 +49,7 @@ class Collection extends SearchResult
4249
* @param ResolverInterface $localeResolver
4350
* @param string $mainTable
4451
* @param string $resourceModel
52+
* @param TimezoneInterface|null $timeZone
4553
*/
4654
public function __construct(
4755
EntityFactory $entityFactory,
@@ -50,10 +58,13 @@ public function __construct(
5058
EventManager $eventManager,
5159
ResolverInterface $localeResolver,
5260
$mainTable = 'customer_grid_flat',
53-
$resourceModel = Customer::class
61+
$resourceModel = Customer::class,
62+
TimezoneInterface $timeZone = null
5463
) {
5564
$this->localeResolver = $localeResolver;
5665
parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $mainTable, $resourceModel);
66+
$this->timeZone = $timeZone ?: ObjectManager::getInstance()
67+
->get(TimezoneInterface::class);
5768
}
5869

5970
/**
@@ -81,6 +92,14 @@ public function addFieldToFilter($field, $condition = null)
8192
return $this;
8293
}
8394

95+
if ($field === 'created_at') {
96+
if (is_array($condition)) {
97+
foreach ($condition as $key => $value) {
98+
$condition[$key] = $this->timeZone->convertConfigTimeToUtc($value);
99+
}
100+
}
101+
}
102+
84103
if (is_string($field) && count(explode('.', $field)) === 1) {
85104
$field = 'main_table.' . $field;
86105
}

dev/tests/integration/testsuite/Magento/Customer/Model/ResourceModel/Grid/CollectionTest.php

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,19 @@
88

99
use Magento\Customer\Api\CustomerRepositoryInterface;
1010
use Magento\Customer\Api\Data\CustomerInterface;
11+
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
1112
use Magento\TestFramework\Helper\Bootstrap;
13+
use Magento\TestFramework\Indexer\TestCase;
1214

1315
/**
1416
* Customer grid collection tests.
1517
*/
16-
class CollectionTest extends \Magento\TestFramework\Indexer\TestCase
18+
class CollectionTest extends TestCase
1719
{
1820
public static function setUpBeforeClass(): void
1921
{
20-
$db = Bootstrap::getInstance()->getBootstrap()
22+
$db = Bootstrap::getInstance()
23+
->getBootstrap()
2124
->getApplication()
2225
->getDbInstance();
2326
if (!$db->isDbDumpExists()) {
@@ -42,12 +45,10 @@ public static function setUpBeforeClass(): void
4245
*/
4346
public function testGetItemByIdForUpdateOnSchedule()
4447
{
45-
$targetObject = Bootstrap::getObjectManager()->create(
46-
\Magento\Customer\Model\ResourceModel\Grid\Collection::class
47-
);
48-
$customerRepository = Bootstrap::getObjectManager()->create(
49-
CustomerRepositoryInterface::class
50-
);
48+
$targetObject = Bootstrap::getObjectManager()
49+
->create(Collection::class);
50+
$customerRepository = Bootstrap::getObjectManager()
51+
->create(CustomerRepositoryInterface::class);
5152
/** Verify after first save */
5253

5354
/** @var CustomerInterface $newCustomer */
@@ -68,7 +69,28 @@ public function testGetItemByIdForUpdateOnSchedule()
6869
}
6970

7071
/**
71-
* teardown
72+
* Verifies that filter condition date is being converted to config timezone before select sql query
73+
*
74+
* @return void
75+
*/
76+
public function testAddFieldToFilter(): void
77+
{
78+
$filterDate = "2021-01-26 00:00:00";
79+
/** @var TimezoneInterface $timeZone */
80+
$timeZone = Bootstrap::getObjectManager()
81+
->get(TimezoneInterface::class);
82+
/** @var Collection $gridCollection */
83+
$gridCollection = Bootstrap::getObjectManager()
84+
->get(Collection::class);
85+
$convertedDate = $timeZone->convertConfigTimeToUtc($filterDate);
86+
$collection = $gridCollection->addFieldToFilter('created_at', ['qteq' => $filterDate]);
87+
$expectedSelect = "WHERE (((`main_table`.`created_at` = '{$convertedDate}')))";
88+
89+
$this->assertStringContainsString($expectedSelect, $collection->getSelectSql(true));
90+
}
91+
92+
/**
93+
* @inheritDoc
7294
*/
7395
protected function tearDown(): void
7496
{

0 commit comments

Comments
 (0)