Skip to content

Commit 012c190

Browse files
ENGCOM-6753: Escape dollar sign for saving content into crontab #26462
- Merge Pull Request #26462 from Erfans/magento2:2.4-develop - Merged commits: 1. 1044f25 2. d4d1553 3. 5c79a9e
2 parents d71050a + 5c79a9e commit 012c190

File tree

2 files changed

+52
-19
lines changed

2 files changed

+52
-19
lines changed

lib/internal/Magento/Framework/Crontab/CrontabManager.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ private function getCrontabContent()
203203
*/
204204
private function save($content)
205205
{
206-
$content = str_replace(['%', '"'], ['%%', '\"'], $content);
206+
$content = str_replace(['%', '"', '$'], ['%%', '\"', '\$'], $content);
207207

208208
try {
209209
$this->shell->execute('echo "' . $content . '" | crontab -');

lib/internal/Magento/Framework/Crontab/Test/Unit/CrontabManagerTest.php

Lines changed: 51 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,34 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6-
6+
declare(strict_types=1);
77

88
namespace Magento\Framework\Crontab\Test\Unit;
99

10+
use Magento\Framework\App\Filesystem\DirectoryList;
1011
use Magento\Framework\Crontab\CrontabManager;
1112
use Magento\Framework\Crontab\CrontabManagerInterface;
12-
use Magento\Framework\ShellInterface;
13-
use Magento\Framework\Phrase;
1413
use Magento\Framework\Exception\LocalizedException;
1514
use Magento\Framework\Filesystem;
16-
use Magento\Framework\App\Filesystem\DirectoryList;
1715
use Magento\Framework\Filesystem\Directory\ReadInterface;
1816
use Magento\Framework\Filesystem\DriverPool;
17+
use Magento\Framework\Phrase;
18+
use Magento\Framework\ShellInterface;
19+
use PHPUnit\Framework\TestCase;
20+
use PHPUnit_Framework_MockObject_MockObject as MockObject;
1921

2022
/**
2123
* Tests crontab manager functionality.
2224
*/
23-
class CrontabManagerTest extends \PHPUnit\Framework\TestCase
25+
class CrontabManagerTest extends TestCase
2426
{
2527
/**
26-
* @var ShellInterface|\PHPUnit_Framework_MockObject_MockObject
28+
* @var ShellInterface|MockObject
2729
*/
2830
private $shellMock;
2931

3032
/**
31-
* @var Filesystem|\PHPUnit_Framework_MockObject_MockObject
33+
* @var Filesystem|MockObject
3234
*/
3335
private $filesystemMock;
3436

@@ -38,7 +40,7 @@ class CrontabManagerTest extends \PHPUnit\Framework\TestCase
3840
private $crontabManager;
3941

4042
/**
41-
* @return void
43+
* @inheritDoc
4244
*/
4345
protected function setUp()
4446
{
@@ -53,9 +55,11 @@ protected function setUp()
5355
}
5456

5557
/**
58+
* Verify get tasks without cronetab.
59+
*
5660
* @return void
5761
*/
58-
public function testGetTasksNoCrontab()
62+
public function testGetTasksNoCrontab(): void
5963
{
6064
$exception = new \Exception('crontab: no crontab for user');
6165
$localizedException = new LocalizedException(new Phrase('Some error'), $exception);
@@ -69,12 +73,14 @@ public function testGetTasksNoCrontab()
6973
}
7074

7175
/**
76+
* Verify get tasks.
77+
*
7278
* @param string $content
7379
* @param array $tasks
7480
* @return void
7581
* @dataProvider getTasksDataProvider
7682
*/
77-
public function testGetTasks($content, $tasks)
83+
public function testGetTasks($content, $tasks): void
7884
{
7985
$this->shellMock->expects($this->once())
8086
->method('execute')
@@ -85,9 +91,11 @@ public function testGetTasks($content, $tasks)
8591
}
8692

8793
/**
94+
* Data provider to get tasks.
95+
*
8896
* @return array
8997
*/
90-
public function getTasksDataProvider()
98+
public function getTasksDataProvider(): array
9199
{
92100
return [
93101
[
@@ -120,11 +128,13 @@ public function getTasksDataProvider()
120128
}
121129

122130
/**
131+
* Verify remove tasks with exception.
132+
*
123133
* @return void
124134
* @expectedException \Magento\Framework\Exception\LocalizedException
125135
* @expectedExceptionMessage Shell error
126136
*/
127-
public function testRemoveTasksWithException()
137+
public function testRemoveTasksWithException(): void
128138
{
129139
$exception = new \Exception('Shell error');
130140
$localizedException = new LocalizedException(new Phrase('Some error'), $exception);
@@ -143,12 +153,14 @@ public function testRemoveTasksWithException()
143153
}
144154

145155
/**
156+
* Verify remove tasks.
157+
*
146158
* @param string $contentBefore
147159
* @param string $contentAfter
148160
* @return void
149161
* @dataProvider removeTasksDataProvider
150162
*/
151-
public function testRemoveTasks($contentBefore, $contentAfter)
163+
public function testRemoveTasks($contentBefore, $contentAfter): void
152164
{
153165
$this->shellMock->expects($this->at(0))
154166
->method('execute')
@@ -163,9 +175,11 @@ public function testRemoveTasks($contentBefore, $contentAfter)
163175
}
164176

165177
/**
178+
* Data provider to remove tasks.
179+
*
166180
* @return array
167181
*/
168-
public function removeTasksDataProvider()
182+
public function removeTasksDataProvider(): array
169183
{
170184
return [
171185
[
@@ -195,11 +209,13 @@ public function removeTasksDataProvider()
195209
}
196210

197211
/**
212+
* Verify save tasks with empty tasks list.
213+
*
198214
* @return void
199215
* @expectedException \Magento\Framework\Exception\LocalizedException
200216
* @expectedExceptionMessage The list of tasks is empty. Add tasks and try again.
201217
*/
202-
public function testSaveTasksWithEmptyTasksList()
218+
public function testSaveTasksWithEmptyTasksList(): void
203219
{
204220
$baseDirMock = $this->getMockBuilder(ReadInterface::class)
205221
->getMockForAbstractClass();
@@ -222,11 +238,13 @@ public function testSaveTasksWithEmptyTasksList()
222238
}
223239

224240
/**
241+
* Verify save tasks with out command.
242+
*
225243
* @return void
226244
* @expectedException \Magento\Framework\Exception\LocalizedException
227245
* @expectedExceptionMessage The command shouldn't be empty. Enter and try again.
228246
*/
229-
public function testSaveTasksWithoutCommand()
247+
public function testSaveTasksWithoutCommand(): void
230248
{
231249
$baseDirMock = $this->getMockBuilder(ReadInterface::class)
232250
->getMockForAbstractClass();
@@ -252,13 +270,15 @@ public function testSaveTasksWithoutCommand()
252270
}
253271

254272
/**
273+
* Verify sava task.
274+
*
255275
* @param array $tasks
256276
* @param string $content
257277
* @param string $contentToSave
258278
* @return void
259279
* @dataProvider saveTasksDataProvider
260280
*/
261-
public function testSaveTasks($tasks, $content, $contentToSave)
281+
public function testSaveTasks($tasks, $content, $contentToSave): void
262282
{
263283
$baseDirMock = $this->getMockBuilder(ReadInterface::class)
264284
->getMockForAbstractClass();
@@ -291,9 +311,11 @@ public function testSaveTasks($tasks, $content, $contentToSave)
291311
}
292312

293313
/**
314+
* Data provider to save tasks.
315+
*
294316
* @return array
295317
*/
296-
public function saveTasksDataProvider()
318+
public function saveTasksDataProvider(): array
297319
{
298320
$content = '* * * * * /bin/php /var/www/cron.php' . PHP_EOL
299321
. CrontabManagerInterface::TASKS_BLOCK_START . ' ' . hash("sha256", BP) . PHP_EOL
@@ -354,6 +376,17 @@ public function saveTasksDataProvider()
354376
. ' %% cron:run | grep -v \"Ran \'jobs\' by schedule\"' . PHP_EOL
355377
. CrontabManagerInterface::TASKS_BLOCK_END . ' ' . hash("sha256", BP) . PHP_EOL,
356378
],
379+
[
380+
'tasks' => [
381+
['command' => '{magentoRoot}run.php mysqldump db > db-$(date +%F).sql']
382+
],
383+
'content' => '* * * * * /bin/php /var/www/cron.php',
384+
'contentToSave' => '* * * * * /bin/php /var/www/cron.php' . PHP_EOL
385+
. CrontabManagerInterface::TASKS_BLOCK_START . ' ' . hash("sha256", BP) . PHP_EOL
386+
. '* * * * * ' . PHP_BINARY . ' /var/www/magento2/run.php'
387+
. ' mysqldump db > db-\$(date +%%F).sql' . PHP_EOL
388+
. CrontabManagerInterface::TASKS_BLOCK_END . ' ' . hash("sha256", BP) . PHP_EOL,
389+
],
357390
];
358391
}
359392
}

0 commit comments

Comments
 (0)