|
| 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\Sales\Plugin\Model\ResourceModel\Order; |
| 9 | + |
| 10 | +use Magento\Framework\ObjectManagerInterface; |
| 11 | +use Magento\Framework\Stdlib\DateTime\TimezoneInterface; |
| 12 | +use Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult; |
| 13 | +use Magento\Sales\Model\ResourceModel\Order\Creditmemo; |
| 14 | +use Magento\Sales\Model\ResourceModel\Order\Invoice; |
| 15 | +use Magento\Sales\Model\ResourceModel\Order\Shipment; |
| 16 | +use Magento\TestFramework\Helper\Bootstrap; |
| 17 | +use PHPUnit\Framework\TestCase; |
| 18 | + |
| 19 | +class OrderGridCollectionFilterTest extends TestCase |
| 20 | +{ |
| 21 | + /** |
| 22 | + * @var ObjectManagerInterface |
| 23 | + */ |
| 24 | + private ObjectManagerInterface $objectManager; |
| 25 | + |
| 26 | + /** |
| 27 | + * @var TimezoneInterface |
| 28 | + */ |
| 29 | + private $timeZone; |
| 30 | + |
| 31 | + /** |
| 32 | + * @var OrderGridCollectionFilter |
| 33 | + */ |
| 34 | + private $plugin; |
| 35 | + |
| 36 | + /** |
| 37 | + * @var SearchResult |
| 38 | + */ |
| 39 | + private $searchResult; |
| 40 | + |
| 41 | + /** |
| 42 | + * @var \Closure |
| 43 | + */ |
| 44 | + private $proceed; |
| 45 | + |
| 46 | + /** |
| 47 | + * @inheritDoc |
| 48 | + */ |
| 49 | + protected function setUp(): void |
| 50 | + { |
| 51 | + $this->objectManager = Bootstrap::getObjectManager(); |
| 52 | + $this->timeZone = $this->objectManager->get(TimezoneInterface::class); |
| 53 | + $this->proceed = function () { |
| 54 | + $this->proceed; |
| 55 | + }; |
| 56 | + $this->plugin = $this->objectManager->create( |
| 57 | + OrderGridCollectionFilter::class, |
| 58 | + [ |
| 59 | + 'timezoneInterface' => $this->timeZone |
| 60 | + ] |
| 61 | + ); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Verifies that filter condition date is being converted to config timezone before select sql query |
| 66 | + * |
| 67 | + * @dataProvider getCollectionFiltersDataProvider |
| 68 | + * @param $mainTable |
| 69 | + * @param $resourceModel |
| 70 | + * @param $field |
| 71 | + * @throws \Magento\Framework\Exception\LocalizedException |
| 72 | + */ |
| 73 | + public function testAroundAddFieldToFilter($mainTable, $resourceModel, $field): void |
| 74 | + { |
| 75 | + $filterDate = "2021-12-13 00:00:00"; |
| 76 | + $convertedDate = $this->timeZone->convertConfigTimeToUtc($filterDate); |
| 77 | + |
| 78 | + $this->searchResult = $this->objectManager->create( |
| 79 | + SearchResult::class, |
| 80 | + [ |
| 81 | + 'mainTable' => $mainTable, |
| 82 | + 'resourceModel' => $resourceModel |
| 83 | + ] |
| 84 | + ); |
| 85 | + $result = $this->plugin->aroundAddFieldToFilter($this->searchResult, $this->proceed, $field, ['qteq' => $filterDate]); |
| 86 | + |
| 87 | + $expectedSelect = "SELECT `main_table`.* FROM `{$mainTable}` AS `main_table` " . |
| 88 | + "WHERE (((`{$field}` = '{$convertedDate}')))"; |
| 89 | + |
| 90 | + $this->assertEquals($expectedSelect, $result->getSelectSql(true)); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * @return array |
| 95 | + */ |
| 96 | + public function getCollectionFiltersDataProvider2(): array |
| 97 | + { |
| 98 | + return [ |
| 99 | + 'shipment_grid_collection_for_created_at' => [ |
| 100 | + 'mainTable' => 'sales_shipment_grid', |
| 101 | + 'resourceModel' => Shipment::class, |
| 102 | + 'field' => 'created_at', |
| 103 | + ], |
| 104 | + 'shipment_grid_collection_for_order_created_at' => [ |
| 105 | + 'mainTable' => 'sales_shipment_grid', |
| 106 | + 'resourceModel' => Shipment::class, |
| 107 | + 'field' => 'order_created_at', |
| 108 | + ] |
| 109 | + ]; |
| 110 | + } |
| 111 | + public function getCollectionFiltersDataProvider(): array |
| 112 | + { |
| 113 | + return [ |
| 114 | + 'invoice_grid_collection_for_created_at' => [ |
| 115 | + 'mainTable' => 'sales_invoice_grid', |
| 116 | + 'resourceModel' => Invoice::class, |
| 117 | + 'field' => 'created_at', |
| 118 | + ], |
| 119 | + 'invoice_grid_collection_for_order_created_at' => [ |
| 120 | + 'mainTable' => 'sales_invoice_grid', |
| 121 | + 'resourceModel' => Invoice::class, |
| 122 | + 'field' => 'order_created_at', |
| 123 | + ], |
| 124 | + 'shipment_grid_collection_for_created_at' => [ |
| 125 | + 'mainTable' => 'sales_shipment_grid', |
| 126 | + 'resourceModel' => Shipment::class, |
| 127 | + 'field' => 'created_at', |
| 128 | + ], |
| 129 | + 'shipment_grid_collection_for_order_created_at' => [ |
| 130 | + 'mainTable' => 'sales_shipment_grid', |
| 131 | + 'resourceModel' => Shipment::class, |
| 132 | + 'field' => 'order_created_at', |
| 133 | + ], |
| 134 | + 'creditmemo_grid_collection_for_created_at' => [ |
| 135 | + 'mainTable' => 'sales_creditmemo_grid', |
| 136 | + 'resourceModel' => Creditmemo::class, |
| 137 | + 'field' => 'created_at', |
| 138 | + ], |
| 139 | + 'creditmemo_grid_collection_for_order_created_at' => [ |
| 140 | + 'mainTable' => 'sales_creditmemo_grid', |
| 141 | + 'resourceModel' => Creditmemo::class, |
| 142 | + 'field' => 'order_created_at', |
| 143 | + ] |
| 144 | + ]; |
| 145 | + } |
| 146 | +} |
0 commit comments