Skip to content

Commit 717cdf9

Browse files
committed
ACP2E-4132: [Cloud] Deactivate the old sitemap generation
1 parent 5b4fd10 commit 717cdf9

File tree

2 files changed

+333
-12
lines changed

2 files changed

+333
-12
lines changed

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

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,22 +53,39 @@ public function afterAfterSave(
5353
Sitemap $subject,
5454
mixed $result
5555
) {
56-
$time = $subject->getData('groups/generate/fields/time/value') ?:
57-
explode(
58-
',',
59-
$subject->getConfig()->getValue(
60-
'sitemap/generate/time',
61-
$subject->getScope(),
62-
$subject->getScopeId()
63-
) ?: '0,0,0'
56+
$config = $subject->getConfig();
57+
58+
$time = $subject->getData('groups/generate/fields/time/value');
59+
if (!$time && $config) {
60+
$timeConfig = $config->getValue(
61+
'sitemap/generate/time',
62+
$subject->getScope(),
63+
$subject->getScopeId()
6464
);
65+
$time = $timeConfig ? explode(',', $timeConfig) : ['0', '0', '0'];
66+
}
67+
6568
$frequency = $subject->getValue();
66-
$generationMethod = $subject->getData('groups/generate/fields/generation_method/value') ?:
67-
$subject->getConfig()->getValue(
69+
70+
$generationMethod = $subject->getData('groups/generate/fields/generation_method/value');
71+
72+
if (!$generationMethod && $config) {
73+
$generationMethod = $config->getValue(
6874
'sitemap/generate/generation_method',
6975
$subject->getScope(),
7076
$subject->getScopeId()
71-
) ?: GenerationMethod::STANDARD;
77+
);
78+
} elseif (!$generationMethod && !$config) {
79+
$configValue = $this->configValueFactory->create()->load(
80+
'sitemap/generate/generation_method',
81+
'path'
82+
);
83+
if ($configValue->getId()) {
84+
$generationMethod = $configValue->getValue();
85+
}
86+
}
87+
88+
$generationMethod = $generationMethod ?: GenerationMethod::STANDARD;
7289

7390
$cronExprArray = [
7491
(int)($time[1] ?? 0), //Minute
@@ -94,7 +111,6 @@ public function afterAfterSave(
94111
throw new LocalizedException(__('We can\'t save the cron expression.'));
95112
}
96113

97-
// Return the original result from the afterSave method
98114
return $result;
99115
}
100116

Lines changed: 305 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,305 @@
1+
<?php
2+
/**
3+
* Copyright 2025 Adobe
4+
* All Rights Reserved.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Sitemap\Plugin\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\Sitemap\Model\Config\Source\GenerationMethod;
16+
use Magento\TestFramework\Helper\Bootstrap;
17+
use PHPUnit\Framework\TestCase;
18+
19+
/**
20+
* Integration test for SitemapPlugin
21+
*
22+
* @magentoAppArea adminhtml
23+
*/
24+
class SitemapPluginTest extends TestCase
25+
{
26+
/**
27+
* Cron string path for sitemap schedule
28+
*/
29+
private const CRON_STRING_PATH = 'crontab/default/jobs/sitemap_generate/schedule/cron_expr';
30+
31+
/**
32+
* Cron model path
33+
*/
34+
private const CRON_MODEL_PATH = 'crontab/default/jobs/sitemap_generate/run/model';
35+
36+
/**
37+
* @var ObjectManagerInterface
38+
*/
39+
private $objectManager;
40+
41+
/**
42+
* @var ValueFactory
43+
*/
44+
private $configValueFactory;
45+
46+
/**
47+
* @inheritdoc
48+
*/
49+
protected function setUp(): void
50+
{
51+
$this->objectManager = Bootstrap::getObjectManager();
52+
$this->configValueFactory = $this->objectManager->get(ValueFactory::class);
53+
}
54+
55+
/**
56+
* @inheritdoc
57+
*/
58+
protected function tearDown(): void
59+
{
60+
$this->clearCronConfiguration(self::CRON_STRING_PATH);
61+
$this->clearCronConfiguration(self::CRON_MODEL_PATH);
62+
$this->clearConfig('sitemap/generate/generation_method');
63+
parent::tearDown();
64+
}
65+
66+
/**
67+
* Test that sitemap cron configuration uses standard observer when generation method is standard
68+
*
69+
* @dataProvider frequencyDataProvider
70+
*/
71+
public function testSitemapCronConfigurationWithStandardMethod(string $frequency, string $expectedCronExpr): void
72+
{
73+
$this->setConfig('sitemap/generate/generation_method', GenerationMethod::STANDARD);
74+
75+
$config = $this->objectManager->create(ConfigModel::class);
76+
$config->setSection('sitemap');
77+
$config->setGroups([
78+
'generate' => [
79+
'fields' => [
80+
'time' => ['value' => ['00', '00', '00']],
81+
'frequency' => ['value' => $frequency],
82+
'generation_method' => ['value' => GenerationMethod::STANDARD],
83+
],
84+
],
85+
]);
86+
$config->save();
87+
88+
$this->assertEquals($expectedCronExpr, $this->getCronExpression());
89+
90+
$this->assertEquals(
91+
'Magento\Sitemap\Model\Observer::scheduledGenerateSitemaps',
92+
$this->getCronModel()
93+
);
94+
}
95+
96+
/**
97+
* Test that sitemap cron configuration uses batch observer when generation method is batch
98+
*
99+
* @dataProvider frequencyDataProvider
100+
*/
101+
public function testSitemapCronConfigurationWithBatchMethod(string $frequency, string $expectedCronExpr): void
102+
{
103+
$this->setConfig('sitemap/generate/generation_method', GenerationMethod::BATCH);
104+
105+
$config = $this->objectManager->create(ConfigModel::class);
106+
$config->setSection('sitemap');
107+
$config->setGroups([
108+
'generate' => [
109+
'fields' => [
110+
'time' => ['value' => ['00', '00', '00']],
111+
'frequency' => ['value' => $frequency],
112+
'generation_method' => ['value' => GenerationMethod::BATCH],
113+
],
114+
],
115+
]);
116+
$config->save();
117+
118+
$this->assertEquals($expectedCronExpr, $this->getCronExpression());
119+
120+
$this->assertEquals(
121+
'Magento\Sitemap\Model\Batch\Observer::scheduledGenerateSitemaps',
122+
$this->getCronModel()
123+
);
124+
}
125+
126+
/**
127+
* Test that only one unified cron job is created regardless of generation method
128+
*/
129+
public function testUnifiedCronJobCreation(): void
130+
{
131+
$config = $this->objectManager->create(ConfigModel::class);
132+
$config->setSection('sitemap');
133+
$config->setGroups([
134+
'generate' => [
135+
'fields' => [
136+
'time' => ['value' => ['00', '00', '00']],
137+
'frequency' => ['value' => Frequency::CRON_DAILY],
138+
'generation_method' => ['value' => GenerationMethod::STANDARD],
139+
],
140+
],
141+
]);
142+
$config->save();
143+
144+
$this->assertNotEmpty($this->getCronExpression());
145+
$this->assertNotEmpty($this->getCronModel());
146+
147+
$batchCronExpr = $this->configValueFactory->create()
148+
->load('crontab/default/jobs/sitemap_generate_batch/schedule/cron_expr', 'path')
149+
->getValue();
150+
$this->assertEmpty($batchCronExpr);
151+
152+
$config->setGroups([
153+
'generate' => [
154+
'fields' => [
155+
'time' => ['value' => ['00', '00', '00']],
156+
'frequency' => ['value' => Frequency::CRON_DAILY],
157+
'generation_method' => ['value' => GenerationMethod::BATCH],
158+
],
159+
],
160+
]);
161+
$config->save();
162+
163+
$this->assertEquals('0 0 * * *', $this->getCronExpression());
164+
$this->assertEquals(
165+
'Magento\Sitemap\Model\Batch\Observer::scheduledGenerateSitemaps',
166+
$this->getCronModel()
167+
);
168+
169+
$batchCronExpr = $this->configValueFactory->create()
170+
->load('crontab/default/jobs/sitemap_generate_batch/schedule/cron_expr', 'path')
171+
->getValue();
172+
$this->assertEmpty($batchCronExpr);
173+
}
174+
175+
/**
176+
* Test that cron configuration with custom time works correctly
177+
*/
178+
public function testCustomTimeConfiguration(): void
179+
{
180+
$config = $this->objectManager->create(ConfigModel::class);
181+
$config->setSection('sitemap');
182+
$config->setGroups([
183+
'generate' => [
184+
'fields' => [
185+
'time' => ['value' => ['03', '30', '00']], // 3:30 AM
186+
'frequency' => ['value' => Frequency::CRON_DAILY],
187+
'generation_method' => ['value' => GenerationMethod::BATCH],
188+
],
189+
],
190+
]);
191+
$config->save();
192+
193+
// Verify custom time is applied
194+
$this->assertEquals('30 3 * * *', $this->getCronExpression());
195+
$this->assertEquals(
196+
'Magento\Sitemap\Model\Batch\Observer::scheduledGenerateSitemaps',
197+
$this->getCronModel()
198+
);
199+
}
200+
201+
/**
202+
* Test that direct save without admin context works
203+
*/
204+
public function testDirectSaveWithGenerationMethod(): void
205+
{
206+
$this->setConfig('sitemap/generate/generation_method', GenerationMethod::BATCH);
207+
208+
$preparedValueFactory = $this->objectManager->get(PreparedValueFactory::class);
209+
$sitemapValue = $preparedValueFactory->create(
210+
'sitemap/generate/frequency',
211+
Frequency::CRON_WEEKLY,
212+
'default',
213+
0
214+
);
215+
$sitemapValue->save();
216+
217+
$this->assertEquals('0 0 * * 1', $this->getCronExpression());
218+
$this->assertEquals(
219+
'Magento\Sitemap\Model\Batch\Observer::scheduledGenerateSitemaps',
220+
$this->getCronModel()
221+
);
222+
}
223+
224+
/**
225+
* @return array
226+
*/
227+
public static function frequencyDataProvider(): array
228+
{
229+
return [
230+
'daily' => [Frequency::CRON_DAILY, '0 0 * * *'],
231+
'weekly' => [Frequency::CRON_WEEKLY, '0 0 * * 1'],
232+
'monthly' => [Frequency::CRON_MONTHLY, '0 0 1 * *'],
233+
];
234+
}
235+
236+
/**
237+
* Get cron expression from configuration
238+
*
239+
* @return string
240+
*/
241+
private function getCronExpression(): string
242+
{
243+
$cronExprValue = $this->configValueFactory->create()
244+
->load(self::CRON_STRING_PATH, 'path');
245+
246+
return $cronExprValue->getValue() ?: '';
247+
}
248+
249+
/**
250+
* Get cron model from configuration
251+
*
252+
* @return string
253+
*/
254+
private function getCronModel(): string
255+
{
256+
$cronModelValue = $this->configValueFactory->create()
257+
->load(self::CRON_MODEL_PATH, 'path');
258+
259+
return $cronModelValue->getValue() ?: '';
260+
}
261+
262+
/**
263+
* Set configuration value
264+
*
265+
* @param string $path
266+
* @param mixed $value
267+
* @return void
268+
*/
269+
private function setConfig(string $path, $value): void
270+
{
271+
$this->configValueFactory->create()
272+
->load($path, 'path')
273+
->setValue($value)
274+
->setPath($path)
275+
->save();
276+
}
277+
278+
/**
279+
* Clear configuration value
280+
*
281+
* @param string $path
282+
* @return void
283+
*/
284+
private function clearConfig(string $path): void
285+
{
286+
$configValue = $this->configValueFactory->create()->load($path, 'path');
287+
if ($configValue->getId()) {
288+
$configValue->delete();
289+
}
290+
}
291+
292+
/**
293+
* Clear cron configuration value
294+
*
295+
* @param string $path
296+
* @return void
297+
*/
298+
private function clearCronConfiguration(string $path): void
299+
{
300+
$configValue = $this->configValueFactory->create()->load($path, 'path');
301+
if ($configValue->getId()) {
302+
$configValue->delete();
303+
}
304+
}
305+
}

0 commit comments

Comments
 (0)