Skip to content

Commit 577efe0

Browse files
committed
ACP2E-77: app:config:import stop when retrieve a warning or notice on php code
1 parent 70900ca commit 577efe0

File tree

2 files changed

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

2 files changed

+106
-9
lines changed

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

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
class Sitemap extends \Magento\Framework\App\Config\Value
1818
{
1919
/**
20-
* Cron string path
20+
* Cron expression path
2121
*/
2222
const CRON_STRING_PATH = 'crontab/default/jobs/sitemap_generate/schedule/cron_expr';
2323

@@ -71,14 +71,12 @@ public function __construct(
7171
*/
7272
public function afterSave()
7373
{
74-
$time = $this->getData('groups/generate/fields/time/value');
75-
if (null === $time) {
76-
$time = explode(',', $this->_config->getValue('sitemap/generate/time'));
77-
}
78-
$frequency = $this->getData('groups/generate/fields/frequency/value');
79-
if (null === $frequency) {
80-
$frequency = $this->getValue();
81-
}
74+
$time = $this->getData('groups/generate/fields/time/value') ?:
75+
explode(
76+
',',
77+
$this->_config->getValue('sitemap/generate/time', $this->getScope(), $this->getScopeId()) ?: '0,0,0'
78+
);
79+
$frequency = $this->getValue();
8280

8381
$cronExprArray = [
8482
(int)$time[1], //Minute
@@ -108,6 +106,7 @@ public function afterSave()
108106
self::CRON_MODEL_PATH
109107
)->save();
110108
} catch (\Exception $e) {
109+
// phpcs:ignore Magento2.Exceptions.DirectThrow
111110
throw new \Exception(__('We can\'t save the cron expression.'));
112111
}
113112
return parent::afterSave();
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)