|
| 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\Product; |
| 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 AlertTest 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 Alert $sitemapValue */ |
| 45 | + $alertValue = $preparedValueFactory->create('catalog/productalert_cron/frequency', $frequency, 'default', 0); |
| 46 | + $alertValue->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('catalog'); |
| 60 | + $config->setGroups( |
| 61 | + [ |
| 62 | + 'productalert_cron' => [ |
| 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(Alert::CRON_STRING_PATH, 'path'); |
| 95 | + |
| 96 | + return $cronExprValue->getValue(); |
| 97 | + } |
| 98 | +} |
0 commit comments