Skip to content

Commit 54dbcfa

Browse files
committed
Merge remote-tracking branch 'origin/MC-38717' into 2.4-develop-pr46
2 parents 3eedb82 + ac6bdfb commit 54dbcfa

File tree

3 files changed

+105
-15
lines changed

3 files changed

+105
-15
lines changed

app/code/Magento/Sales/Model/EmailSenderHandler.php

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@
55
*/
66
namespace Magento\Sales\Model;
77

8+
use Magento\Framework\App\Config\ValueFactory;
9+
use Magento\Framework\App\Config\ValueInterface;
10+
use Magento\Framework\App\ObjectManager;
811
use Magento\Sales\Model\Order\Email\Container\IdentityInterface;
12+
use Magento\Sales\Model\ResourceModel\Collection\AbstractCollection;
913

1014
/**
1115
* Sales emails sending
@@ -32,7 +36,7 @@ class EmailSenderHandler
3236
/**
3337
* Entity collection model.
3438
*
35-
* @var \Magento\Sales\Model\ResourceModel\Collection\AbstractCollection
39+
* @var AbstractCollection
3640
*/
3741
protected $entityCollection;
3842

@@ -53,32 +57,50 @@ class EmailSenderHandler
5357
*/
5458
private $storeManager;
5559

60+
/**
61+
* Config data factory
62+
*
63+
* @var ValueFactory
64+
*/
65+
private $configValueFactory;
66+
67+
/**
68+
* @var string
69+
*/
70+
private $modifyStartFromDate;
71+
5672
/**
5773
* @param \Magento\Sales\Model\Order\Email\Sender $emailSender
5874
* @param \Magento\Sales\Model\ResourceModel\EntityAbstract $entityResource
59-
* @param \Magento\Sales\Model\ResourceModel\Collection\AbstractCollection $entityCollection
75+
* @param AbstractCollection $entityCollection
6076
* @param \Magento\Framework\App\Config\ScopeConfigInterface $globalConfig
6177
* @param IdentityInterface|null $identityContainer
62-
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
63-
* @throws \InvalidArgumentException
78+
* @param \Magento\Store\Model\StoreManagerInterface|null $storeManager
79+
* @param ValueFactory|null $configValueFactory
80+
* @param string|null $modifyStartFromDate
6481
*/
6582
public function __construct(
6683
\Magento\Sales\Model\Order\Email\Sender $emailSender,
6784
\Magento\Sales\Model\ResourceModel\EntityAbstract $entityResource,
68-
\Magento\Sales\Model\ResourceModel\Collection\AbstractCollection $entityCollection,
85+
AbstractCollection $entityCollection,
6986
\Magento\Framework\App\Config\ScopeConfigInterface $globalConfig,
7087
IdentityInterface $identityContainer = null,
71-
\Magento\Store\Model\StoreManagerInterface $storeManager = null
88+
\Magento\Store\Model\StoreManagerInterface $storeManager = null,
89+
?ValueFactory $configValueFactory = null,
90+
?string $modifyStartFromDate = null
7291
) {
7392
$this->emailSender = $emailSender;
7493
$this->entityResource = $entityResource;
7594
$this->entityCollection = $entityCollection;
7695
$this->globalConfig = $globalConfig;
7796

78-
$this->identityContainer = $identityContainer ?: \Magento\Framework\App\ObjectManager::getInstance()
97+
$this->identityContainer = $identityContainer ?: ObjectManager::getInstance()
7998
->get(\Magento\Sales\Model\Order\Email\Container\NullIdentity::class);
80-
$this->storeManager = $storeManager ?: \Magento\Framework\App\ObjectManager::getInstance()
99+
$this->storeManager = $storeManager ?: ObjectManager::getInstance()
81100
->get(\Magento\Store\Model\StoreManagerInterface::class);
101+
102+
$this->configValueFactory = $configValueFactory ?: ObjectManager::getInstance()->get(ValueFactory::class);
103+
$this->modifyStartFromDate = $modifyStartFromDate ?: $this->modifyStartFromDate;
82104
}
83105

84106
/**
@@ -90,6 +112,7 @@ public function sendEmails()
90112
if ($this->globalConfig->getValue('sales_email/general/async_sending')) {
91113
$this->entityCollection->addFieldToFilter('send_email', ['eq' => 1]);
92114
$this->entityCollection->addFieldToFilter('email_sent', ['null' => true]);
115+
$this->filterCollectionByStartFromDate($this->entityCollection);
93116
$this->entityCollection->setPageSize(
94117
$this->globalConfig->getValue('sales_email/general/sending_limit')
95118
);
@@ -126,7 +149,7 @@ public function sendEmails()
126149
* @throws \Magento\Framework\Exception\NoSuchEntityException
127150
*/
128151
private function getStores(
129-
\Magento\Sales\Model\ResourceModel\Collection\AbstractCollection $entityCollection
152+
AbstractCollection $entityCollection
130153
): array {
131154
$stores = [];
132155

@@ -140,4 +163,26 @@ private function getStores(
140163

141164
return $stores;
142165
}
166+
167+
/**
168+
* Filter collection by start from date
169+
*
170+
* @param AbstractCollection $collection
171+
* @return void
172+
*/
173+
private function filterCollectionByStartFromDate(AbstractCollection $collection): void
174+
{
175+
/** @var $configValue ValueInterface */
176+
$configValue = $this->configValueFactory->create();
177+
$configValue->load('sales_email/general/async_sending', 'path');
178+
179+
if ($configValue->getId()) {
180+
$startFromDate = date(
181+
'Y-m-d H:i:s',
182+
strtotime($configValue->getUpdatedAt() . ' ' . $this->modifyStartFromDate)
183+
);
184+
185+
$collection->addFieldToFilter('created_at', ['from' => $startFromDate]);
186+
}
187+
}
143188
}

app/code/Magento/Sales/Test/Unit/Model/EmailSenderHandlerTest.php

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77

88
namespace Magento\Sales\Test\Unit\Model;
99

10+
use Magento\Config\Model\Config\Backend\Encrypted;
1011
use Magento\Framework\App\Config;
12+
use Magento\Framework\App\Config\Value;
13+
use Magento\Framework\App\Config\ValueFactory;
1114
use Magento\Framework\DB\Select;
1215
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
1316
use Magento\Sales\Model\AbstractModel;
@@ -71,6 +74,16 @@ class EmailSenderHandlerTest extends TestCase
7174
*/
7275
private $storeManagerMock;
7376

77+
/**
78+
* @var ValueFactory|MockObject
79+
*/
80+
private $configValueFactory;
81+
82+
/**
83+
* @var string
84+
*/
85+
private $modifyStartFromDate = '-1 day';
86+
7487
protected function setUp(): void
7588
{
7689
$objectManager = new ObjectManager($this);
@@ -110,15 +123,21 @@ protected function setUp(): void
110123
StoreManagerInterface::class
111124
);
112125

126+
$this->configValueFactory = $this->createMock(
127+
ValueFactory::class
128+
);
129+
113130
$this->object = $objectManager->getObject(
114131
EmailSenderHandler::class,
115132
[
116-
'emailSender' => $this->emailSender,
117-
'entityResource' => $this->entityResource,
118-
'entityCollection' => $this->entityCollection,
119-
'globalConfig' => $this->globalConfig,
120-
'identityContainer' => $this->identityContainerMock,
121-
'storeManager' => $this->storeManagerMock,
133+
'emailSender' => $this->emailSender,
134+
'entityResource' => $this->entityResource,
135+
'entityCollection' => $this->entityCollection,
136+
'globalConfig' => $this->globalConfig,
137+
'identityContainer' => $this->identityContainerMock,
138+
'storeManager' => $this->storeManagerMock,
139+
'configValueFactory' => $this->configValueFactory,
140+
'modifyStartFromDate' => $this->modifyStartFromDate
122141
]
123142
);
124143
}
@@ -151,6 +170,13 @@ public function testExecute($configValue, $collectionItems, $emailSendingResult)
151170
->method('addFieldToFilter')
152171
->with('email_sent', ['null' => true]);
153172

173+
$nowDate = date('Y-m-d H:i:s');
174+
$fromDate = date('Y-m-d H:i:s', strtotime($nowDate . ' ' . $this->modifyStartFromDate));
175+
$this->entityCollection
176+
->expects($this->at(2))
177+
->method('addFieldToFilter')
178+
->with('created_at', ['from' => $fromDate]);
179+
154180
$this->entityCollection
155181
->expects($this->any())
156182
->method('addAttributeToSelect')
@@ -175,6 +201,20 @@ public function testExecute($configValue, $collectionItems, $emailSendingResult)
175201
->method('getItems')
176202
->willReturn($collectionItems);
177203

204+
/** @var Value|Encrypted|MockObject $valueMock */
205+
$backendModelMock = $this->getMockBuilder(Value::class)
206+
->disableOriginalConstructor()
207+
->onlyMethods(['load', 'getId'])
208+
->addMethods(['getUpdatedAt'])
209+
->getMock();
210+
$backendModelMock->expects($this->once())->method('load')->willReturnSelf();
211+
$backendModelMock->expects($this->once())->method('getId')->willReturn(1);
212+
$backendModelMock->expects($this->once())->method('getUpdatedAt')->willReturn($nowDate);
213+
214+
$this->configValueFactory->expects($this->once())
215+
->method('create')
216+
->willReturn($backendModelMock);
217+
178218
if ($collectionItems) {
179219

180220
/** @var AbstractModel|MockObject $collectionItem */

app/code/Magento/Sales/etc/di.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,11 @@
216216
</argument>
217217
</arguments>
218218
</type>
219+
<type name="Magento\Sales\Model\EmailSenderHandler">
220+
<arguments>
221+
<argument name="modifyStartFromDate" xsi:type="string">-1 day</argument>
222+
</arguments>
223+
</type>
219224
<virtualType name="SalesOrderIndexGridSyncRemove" type="Magento\Sales\Observer\GridSyncRemoveObserver">
220225
<arguments>
221226
<argument name="entityGrid" xsi:type="object">Magento\Sales\Model\ResourceModel\Order\Grid</argument>

0 commit comments

Comments
 (0)