Skip to content

Commit 7c9fd81

Browse files
committed
ACP2E-56 : [On Prem] Magento invoice order date filter not working
1 parent b6ef22c commit 7c9fd81

File tree

6 files changed

+216
-15
lines changed

6 files changed

+216
-15
lines changed

app/code/Magento/Sales/Model/ResourceModel/Order/CollectionFilter.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,24 @@ public function __construct(
4444
$this->timeZone = $timeZone;
4545
}
4646

47+
/**
48+
* @inheritdoc
49+
*/
50+
public function _initSelect()
51+
{
52+
parent::_initSelect();
53+
54+
$tableDescription = $this->getConnection()->describeTable($this->getMainTable());
55+
56+
if ($tableDescription) {
57+
foreach ($tableDescription as $columnInfo) {
58+
$this->addFilterToMap($columnInfo['COLUMN_NAME'], 'main_table.' . $columnInfo['COLUMN_NAME']);
59+
}
60+
}
61+
62+
return $this;
63+
}
64+
4765
/**
4866
* @inheritDoc
4967
*/

app/code/Magento/Sales/Model/ResourceModel/Order/Invoice/Grid/CollectionFilter.php

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
67
namespace Magento\Sales\Model\ResourceModel\Order\Invoice\Grid;
78

8-
use Magento\Framework\App\ObjectManager;
99
use Magento\Framework\Data\Collection\Db\FetchStrategyInterface as FetchStrategy;
1010
use Magento\Framework\Data\Collection\EntityFactoryInterface as EntityFactory;
1111
use Magento\Framework\Event\ManagerInterface as EventManager;
@@ -27,25 +27,42 @@ class CollectionFilter extends SearchResult
2727
/**
2828
* Initialize dependencies.
2929
*
30-
* @param EntityFactory $entityFactory
31-
* @param Logger $logger
32-
* @param FetchStrategy $fetchStrategy
33-
* @param EventManager $eventManager
34-
* @param string $mainTable
35-
* @param string $resourceModel
36-
* @param TimezoneInterface|null $timeZone
30+
* @param EntityFactory $entityFactory
31+
* @param Logger $logger
32+
* @param FetchStrategy $fetchStrategy
33+
* @param EventManager $eventManager
34+
* @param TimezoneInterface $timeZone
35+
* @param string $mainTable
36+
* @param string $resourceModel
3737
*/
3838
public function __construct(
3939
EntityFactory $entityFactory,
4040
Logger $logger,
4141
FetchStrategy $fetchStrategy,
4242
EventManager $eventManager,
43+
TimezoneInterface $timeZone,
4344
$mainTable = 'sales_invoice_grid',
44-
$resourceModel = Invoice::class,
45-
TimezoneInterface $timeZone = null
45+
$resourceModel = Invoice::class
4646
) {
4747
parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $mainTable, $resourceModel);
48-
$this->timeZone = $timeZone ?: ObjectManager::getInstance()->get(TimezoneInterface::class);
48+
$this->timeZone = $timeZone;
49+
}
50+
51+
/**
52+
* @inheritdoc
53+
*/
54+
public function _initSelect()
55+
{
56+
parent::_initSelect();
57+
58+
$tableDescription = $this->getConnection()->describeTable($this->getMainTable());
59+
if ($tableDescription) {
60+
foreach ($tableDescription as $columnInfo) {
61+
$this->addFilterToMap($columnInfo['COLUMN_NAME'], 'main_table.'.$columnInfo['COLUMN_NAME']);
62+
}
63+
}
64+
65+
return $this;
4966
}
5067

5168
/**
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Cms\Model\ResourceModel\Block\Grid;
9+
10+
use Magento\Cms\Model\ResourceModel\Page;
11+
use Magento\Framework\ObjectManagerInterface;
12+
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
13+
use Magento\TestFramework\Helper\Bootstrap;
14+
use PHPUnit\Framework\TestCase;
15+
16+
class CollectionTest extends TestCase
17+
{
18+
/**
19+
* @var ObjectManagerInterface
20+
*/
21+
private $objectManager;
22+
23+
/**
24+
* @inheritDoc
25+
*/
26+
protected function setUp(): void
27+
{
28+
$this->objectManager = Bootstrap::getObjectManager();
29+
}
30+
31+
/**
32+
* Verifies that filter condition date is being converted to config timezone before select sql query
33+
*
34+
* @dataProvider getCollectionFiltersDataProvider
35+
* @return void
36+
*/
37+
public function testAddFieldToFilter($field): void
38+
{
39+
$filterDate = "2021-12-05 00:00:00";
40+
/** @var TimezoneInterface $timeZone */
41+
$timeZone = $this->objectManager->get(TimezoneInterface::class);
42+
/** @var Collection $gridCollection */
43+
$gridCollection = $this->objectManager->create(
44+
Collection::class,
45+
[
46+
'mainTable' => 'cms_block',
47+
'resourceModel' => Page::class
48+
]
49+
);
50+
$convertedDate = $timeZone->convertConfigTimeToUtc($filterDate);
51+
52+
$collection = $gridCollection->addFieldToFilter($field, ['qteq' => $filterDate]);
53+
$expectedSelect = "SELECT `main_table`.* FROM `cms_block` AS `main_table` " .
54+
"WHERE ((((`{$field}` = '{$convertedDate}')))) AND (main_table.created_in <= 1) AND (main_table.updated_in > 1)";
55+
56+
$this->assertEquals($expectedSelect, $collection->getSelectSql(true));
57+
}
58+
59+
/**
60+
* @return array
61+
*/
62+
public function getCollectionFiltersDataProvider(): array
63+
{
64+
return [
65+
'cms_page_collection_for_creation_time' => [
66+
'field' => 'creation_time',
67+
],
68+
'cms_page_collection_for_order_update_time' => [
69+
'field' => 'update_time',
70+
],
71+
];
72+
}
73+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Cms\Model\ResourceModel\Page\Grid;
9+
10+
use Magento\Cms\Model\ResourceModel\Page;
11+
use Magento\Framework\ObjectManagerInterface;
12+
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
13+
use Magento\TestFramework\Helper\Bootstrap;
14+
use PHPUnit\Framework\TestCase;
15+
16+
class CollectionTest extends TestCase
17+
{
18+
/**
19+
* @var ObjectManagerInterface
20+
*/
21+
private $objectManager;
22+
23+
/**
24+
* @inheritDoc
25+
*/
26+
protected function setUp(): void
27+
{
28+
$this->objectManager = Bootstrap::getObjectManager();
29+
}
30+
31+
/**
32+
* Verifies that filter condition date is being converted to config timezone before select sql query
33+
*
34+
* @dataProvider getCollectionFiltersDataProvider
35+
* @return void
36+
*/
37+
public function testAddFieldToFilter($field): void
38+
{
39+
$filterDate = "2021-12-06 00:00:00";
40+
/** @var TimezoneInterface $timeZone */
41+
$timeZone = $this->objectManager->get(TimezoneInterface::class);
42+
/** @var Collection $gridCollection */
43+
$gridCollection = $this->objectManager->create(
44+
Collection::class,
45+
[
46+
'mainTable' => 'cms_page',
47+
'resourceModel' => Page::class
48+
]
49+
);
50+
$convertedDate = $timeZone->convertConfigTimeToUtc($filterDate);
51+
52+
$collection = $gridCollection->addFieldToFilter($field, ['qteq' => $filterDate]);
53+
$expectedSelect = "SELECT `main_table`.* FROM `cms_page` AS `main_table` " .
54+
"WHERE ((((`{$field}` = '{$convertedDate}')))) AND (main_table.created_in <= 1) AND (main_table.updated_in > 1)";
55+
56+
$this->assertEquals($expectedSelect, $collection->getSelectSql(true));
57+
}
58+
59+
/**
60+
* @return array
61+
*/
62+
public function getCollectionFiltersDataProvider(): array
63+
{
64+
return [
65+
'cms_page_collection_for_creation_time' => [
66+
'field' => 'creation_time',
67+
],
68+
'cms_page_collection_for_order_update_time' => [
69+
'field' => 'update_time',
70+
],
71+
];
72+
}
73+
}

dev/tests/integration/testsuite/Magento/Sales/Model/ResourceModel/Order/CollectionFilterTest.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,20 @@ protected function setUp(): void
3131
/**
3232
* Tests collection properties.
3333
*
34+
* @dataProvider getCollectionFiltersDataProvider
3435
* @throws \ReflectionException
3536
* @return void
3637
*/
37-
public function testCollectionCreate(): void
38+
public function testCollectionCreate($mainTable, $resourceModel): void
3839
{
3940
/** @var Collection $gridCollection */
40-
$gridCollection = $this->objectManager->get(Collection::class);
41+
$gridCollection = $this->objectManager->create(
42+
Collection::class,
43+
[
44+
'mainTable' => $mainTable,
45+
'resourceModel' => $resourceModel
46+
]
47+
);
4148
$tableDescription = $gridCollection->getConnection()
4249
->describeTable($gridCollection->getMainTable());
4350

dev/tests/integration/testsuite/Magento/Sales/Model/ResourceModel/Order/Invoice/Grid/CollectionFilterTest.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
use Magento\Framework\ObjectManagerInterface;
1111
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
12+
use Magento\Sales\Model\ResourceModel\Order\Invoice;
1213
use Magento\Sales\Model\ResourceModel\Order\Invoice\Grid\CollectionFilter as Collection;
1314
use Magento\TestFramework\Helper\Bootstrap;
1415
use PHPUnit\Framework\TestCase;
@@ -37,7 +38,13 @@ protected function setUp(): void
3738
public function testCollectionCreate(): void
3839
{
3940
/** @var Collection $gridCollection */
40-
$gridCollection = $this->objectManager->get(Collection::class);
41+
$gridCollection = $this->objectManager->create(
42+
Collection::class,
43+
[
44+
'mainTable' => 'sales_invoice_grid',
45+
'resourceModel' => Invoice::class
46+
]
47+
);
4148
$tableDescription = $gridCollection->getConnection()
4249
->describeTable($gridCollection->getMainTable());
4350

@@ -69,7 +76,13 @@ public function testAddFieldToFilter(): void
6976
/** @var TimezoneInterface $timeZone */
7077
$timeZone = $this->objectManager->get(TimezoneInterface::class);
7178
/** @var Collection $gridCollection */
72-
$gridCollection = $this->objectManager->get(Collection::class);
79+
$gridCollection = $this->objectManager->create(
80+
Collection::class,
81+
[
82+
'mainTable' => 'sales_invoice_grid',
83+
'resourceModel' => Invoice::class
84+
]
85+
);
7386
$convertedDate = $timeZone->convertConfigTimeToUtc($filterDate);
7487

7588
$collection = $gridCollection->addFieldToFilter('created_at', ['qteq' => $filterDate]);

0 commit comments

Comments
 (0)