Skip to content

Commit 81ee5a8

Browse files
committed
Merge remote-tracking branch 'magento-l3/ACP2E-77' into L3_PR_21-12-13
2 parents 595e5db + c7f204b commit 81ee5a8

File tree

2 files changed

+106
-4
lines changed
  • app/code/Magento/Cron/Model/Config/Backend
  • dev/tests/integration/testsuite/Magento/Cron/Model/Config/Backend

2 files changed

+106
-4
lines changed

app/code/Magento/Cron/Model/Config/Backend/Sitemap.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ class Sitemap extends \Magento\Framework\App\Config\Value
2121
/**
2222
* Cron string path for product alerts
2323
*/
24-
const CRON_STRING_PATH = 'crontab/default/jobs/sitemap_generate/schedule/cron_expr';
24+
public const CRON_STRING_PATH = 'crontab/default/jobs/sitemap_generate/schedule/cron_expr';
2525

2626
/**
2727
* Cron mode path
2828
*/
29-
const CRON_MODEL_PATH = 'crontab/default/jobs/sitemap_generate/run/model';
29+
public const CRON_MODEL_PATH = 'crontab/default/jobs/sitemap_generate/run/model';
3030

3131
/**
3232
* @var \Magento\Framework\App\Config\ValueFactory
@@ -73,8 +73,12 @@ public function __construct(
7373
*/
7474
public function afterSave()
7575
{
76-
$time = $this->getData('groups/generate/fields/time/value');
77-
$frequency = $this->getData('groups/generate/fields/frequency/value');
76+
$time = $this->getData('groups/generate/fields/time/value') ?:
77+
explode(
78+
',',
79+
$this->_config->getValue('sitemap/generate/time', $this->getScope(), $this->getScopeId()) ?: '0,0,0'
80+
);
81+
$frequency = $this->getValue();
7882

7983
$cronExprArray = [
8084
(int)($time[1] ?? 0), //Minute
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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\Cron\Model\Config\Backend;
9+
10+
use Magento\Config\Model\Config as ConfigModel;
11+
use Magento\Config\Model\PreparedValueFactory;
12+
use Magento\Cron\Model\Config\Source\Frequency;
13+
use Magento\Framework\App\Config\ValueFactory;
14+
use Magento\Framework\ObjectManagerInterface;
15+
use Magento\TestFramework\Helper\Bootstrap;
16+
use PHPUnit\Framework\TestCase;
17+
18+
/**
19+
* @magentoAppArea adminhtml
20+
*/
21+
class SitemapTest extends TestCase
22+
{
23+
/**
24+
* @var ObjectManagerInterface
25+
*/
26+
private $objectManager;
27+
28+
/**
29+
* @inheritdoc
30+
*/
31+
protected function setUp(): void
32+
{
33+
$this->objectManager = Bootstrap::getObjectManager();
34+
}
35+
36+
/**
37+
* @dataProvider frequencyDataProvider
38+
* @param string $frequency
39+
* @param string $expectedCronExpr
40+
*/
41+
public function testDirectSave(string $frequency, string $expectedCronExpr): void
42+
{
43+
$preparedValueFactory = $this->objectManager->get(PreparedValueFactory::class);
44+
/** @var Sitemap $sitemapValue */
45+
$sitemapValue = $preparedValueFactory->create('sitemap/generate/frequency', $frequency, 'default', 0);
46+
$sitemapValue->save();
47+
48+
self::assertEquals($expectedCronExpr, $this->getCronExpression());
49+
}
50+
51+
/**
52+
* @dataProvider frequencyDataProvider
53+
* @param string $frequency
54+
* @param string $expectedCronExpr
55+
*/
56+
public function testSaveFromAdmin(string $frequency, string $expectedCronExpr): void
57+
{
58+
$config = $this->objectManager->create(ConfigModel::class);
59+
$config->setSection('sitemap');
60+
$config->setGroups(
61+
[
62+
'generate' => [
63+
'fields' => [
64+
'time' => ['value' => ['00', '00', '00']],
65+
'frequency' => ['value' => $frequency],
66+
],
67+
],
68+
]
69+
);
70+
$config->save();
71+
72+
self::assertEquals($expectedCronExpr, $this->getCronExpression());
73+
}
74+
75+
/**
76+
* @return array
77+
*/
78+
public function frequencyDataProvider(): array
79+
{
80+
return [
81+
'daily' => [Frequency::CRON_DAILY, '0 0 * * *'],
82+
'weekly' => [Frequency::CRON_WEEKLY, '0 0 * * 1'],
83+
'monthly' => [Frequency::CRON_MONTHLY, '0 0 1 * *'],
84+
];
85+
}
86+
87+
/**
88+
* @return string
89+
*/
90+
private function getCronExpression(): string
91+
{
92+
$valueFactory = $this->objectManager->get(ValueFactory::class);
93+
$cronExprValue = $valueFactory->create()
94+
->load(Sitemap::CRON_STRING_PATH, 'path');
95+
96+
return $cronExprValue->getValue();
97+
}
98+
}

0 commit comments

Comments
 (0)