Skip to content

Commit 40952c3

Browse files
Merge MAGETWO-88607 into 2.3-bugfixes-230119
2 parents 599d368 + b545f2d commit 40952c3

File tree

3 files changed

+259
-2
lines changed

3 files changed

+259
-2
lines changed

app/code/Magento/Deploy/Console/InputValidator.php

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
use Magento\Deploy\Console\DeployStaticOptions as Options;
1010
use Magento\Framework\Validator\Locale;
1111
use Symfony\Component\Console\Input\InputInterface;
12+
use Magento\Framework\App\ObjectManager;
13+
use Magento\Framework\Validator\RegexFactory;
1214

1315
/**
1416
* Command input arguments validator class
@@ -55,14 +57,24 @@ class InputValidator
5557
*/
5658
private $localeValidator;
5759

60+
/**
61+
* @var RegexFactory
62+
*/
63+
private $versionValidatorFactory;
64+
5865
/**
5966
* InputValidator constructor
6067
*
6168
* @param Locale $localeValidator
69+
* @param RegexFactory $versionValidatorFactory
6270
*/
63-
public function __construct(Locale $localeValidator)
64-
{
71+
public function __construct(
72+
Locale $localeValidator,
73+
?RegexFactory $versionValidatorFactory = null
74+
) {
6575
$this->localeValidator = $localeValidator;
76+
$this->versionValidatorFactory = $versionValidatorFactory ?:
77+
ObjectManager::getInstance()->get(RegexFactory::class);
6678
}
6779

6880
/**
@@ -85,6 +97,9 @@ public function validate(InputInterface $input)
8597
$input->getArgument(Options::LANGUAGES_ARGUMENT) ?: ['all'],
8698
$input->getOption(Options::EXCLUDE_LANGUAGE)
8799
);
100+
$this->checkVersionInput(
101+
$input->getOption(Options::CONTENT_VERSION) ?: ''
102+
);
88103
}
89104

90105
/**
@@ -147,4 +162,29 @@ private function checkLanguagesInput(array $languagesInclude, array $languagesEx
147162
}
148163
}
149164
}
165+
166+
/**
167+
* Version input checks
168+
*
169+
* @param string $contentVersion
170+
* @throws \InvalidArgumentException
171+
*/
172+
private function checkVersionInput(string $contentVersion): void
173+
{
174+
if ($contentVersion) {
175+
$versionValidator = $this->versionValidatorFactory->create(
176+
[
177+
'pattern' => '/^[A-Za-z0-9_.]+$/'
178+
]
179+
);
180+
181+
if (!$versionValidator->isValid($contentVersion)) {
182+
throw new \InvalidArgumentException(
183+
'Argument "' .
184+
Options::CONTENT_VERSION
185+
. '" has invalid value, content version should contain only characters, digits and dots'
186+
);
187+
}
188+
}
189+
}
150190
}

app/code/Magento/Deploy/Service/DeployStaticContent.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@
99
use Magento\Deploy\Process\QueueFactory;
1010
use Magento\Deploy\Console\DeployStaticOptions as Options;
1111
use Magento\Framework\App\View\Deployment\Version\StorageInterface;
12+
use Magento\Framework\Exception\LocalizedException;
1213
use Magento\Framework\ObjectManagerInterface;
1314
use Psr\Log\LoggerInterface;
1415

1516
/**
1617
* Main service for static content deployment
1718
*
1819
* Aggregates services to deploy static files, static files bundles, translations and minified templates
20+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1921
*/
2022
class DeployStaticContent
2123
{
@@ -71,6 +73,7 @@ public function __construct(
7173
* Run deploy procedure
7274
*
7375
* @param array $options
76+
* @throws LocalizedException
7477
* @return void
7578
*/
7679
public function deploy(array $options)
@@ -133,6 +136,8 @@ public function deploy(array $options)
133136
}
134137

135138
/**
139+
* Returns jobs count
140+
*
136141
* @param array $options
137142
* @return int
138143
*/
@@ -142,6 +147,8 @@ private function getProcessesAmount(array $options)
142147
}
143148

144149
/**
150+
* Check if '--refresh-content-version-only' argument was inserted
151+
*
145152
* @param array $options
146153
* @return bool
147154
*/
Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\Deploy\Test\Unit\Console;
10+
11+
use Magento\Framework\Validator\Regex;
12+
use Magento\Framework\Validator\RegexFactory;
13+
use PHPUnit\Framework\TestCase;
14+
use Magento\Deploy\Console\InputValidator;
15+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
16+
use Magento\Deploy\Console\DeployStaticOptions as Options;
17+
use Magento\Framework\Validator\Locale;
18+
use Symfony\Component\Console\Input\InputOption;
19+
use Symfony\Component\Console\Input\InputDefinition;
20+
use Symfony\Component\Console\Input\ArrayInput;
21+
use InvalidArgumentException;
22+
use Symfony\Component\Console\Input\InputArgument;
23+
24+
/**
25+
* Class InputValidatorTest
26+
* @package Magento\Deploy\Test\Unit\Console
27+
* * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
28+
*/
29+
class InputValidatorTest extends TestCase
30+
{
31+
/**
32+
* @var ObjectManagerHelper
33+
*/
34+
protected $objectManagerHelper;
35+
36+
/**
37+
* @var InputValidator
38+
*/
39+
protected $inputValidator;
40+
41+
/**
42+
* @var Locale
43+
*/
44+
protected $localeValidator;
45+
46+
/**
47+
* @throws \Zend_Validate_Exception
48+
*/
49+
protected function setUp()
50+
{
51+
$this->objectManagerHelper = new ObjectManagerHelper($this);
52+
53+
$regexFactoryMock = $this->getMockBuilder(RegexFactory::class)
54+
->disableOriginalConstructor()
55+
->setMethods(['create'])
56+
->getMock();
57+
58+
$regexObject = new Regex('/^[A-Za-z0-9_.]+$/');
59+
60+
$regexFactoryMock->expects($this->any())->method('create')
61+
->willReturn($regexObject);
62+
63+
$localeObjectMock = $this->getMockBuilder(Locale::class)->setMethods(['isValid'])
64+
->disableOriginalConstructor()
65+
->getMock();
66+
67+
$localeObjectMock->expects($this->any())->method('isValid')
68+
->with('en_US')
69+
->will($this->returnValue(true));
70+
71+
$this->inputValidator = $this->objectManagerHelper->getObject(
72+
InputValidator::class,
73+
[
74+
'localeValidator' => $localeObjectMock,
75+
'versionValidatorFactory' => $regexFactoryMock
76+
]
77+
);
78+
}
79+
80+
/**
81+
* @throws \Zend_Validate_Exception
82+
*/
83+
public function testValidate()
84+
{
85+
$input = $this->getMockBuilder(ArrayInput::class)
86+
->disableOriginalConstructor()
87+
->setMethods(['getOption', 'getArgument'])
88+
->getMock();
89+
90+
$input->expects($this->atLeastOnce())->method('getArgument')->willReturn(['all']);
91+
92+
$input->expects($this->atLeastOnce())->method('getOption')
93+
->willReturnMap(
94+
[
95+
[Options::AREA, ['all']],
96+
[Options::EXCLUDE_AREA, ['none']],
97+
[Options::THEME, ['all']],
98+
[Options::EXCLUDE_THEME, ['none']],
99+
[Options::EXCLUDE_LANGUAGE, ['none']],
100+
[Options::CONTENT_VERSION, '12345']
101+
]
102+
);
103+
104+
/** @noinspection PhpParamsInspection */
105+
$this->inputValidator->validate($input);
106+
}
107+
108+
/**
109+
* @covers \Magento\Deploy\Console\InputValidator::checkAreasInput()
110+
*/
111+
public function testCheckAreasInputException()
112+
{
113+
$options = [
114+
new InputOption(Options::AREA, null, 4, '', ['test']),
115+
new InputOption(Options::EXCLUDE_AREA, null, 4, '', ['test'])
116+
];
117+
118+
$inputDefinition = new InputDefinition($options);
119+
120+
try {
121+
$this->inputValidator->validate(
122+
new ArrayInput([], $inputDefinition)
123+
);
124+
} catch (\Exception $e) {
125+
$this->assertContains('--area (-a) and --exclude-area cannot be used at the same time', $e->getMessage());
126+
$this->assertInstanceOf(InvalidArgumentException::class, $e);
127+
}
128+
}
129+
130+
/**
131+
* @covers \Magento\Deploy\Console\InputValidator::checkThemesInput()
132+
*/
133+
public function testCheckThemesInputException()
134+
{
135+
$options = [
136+
new InputOption(Options::AREA, null, 4, '', ['all']),
137+
new InputOption(Options::EXCLUDE_AREA, null, 4, '', ['none']),
138+
new InputOption(Options::THEME, null, 4, '', ['blank']),
139+
new InputOption(Options::EXCLUDE_THEME, null, 4, '', ['luma'])
140+
];
141+
142+
$inputDefinition = new InputDefinition($options);
143+
144+
try {
145+
$this->inputValidator->validate(
146+
new ArrayInput([], $inputDefinition)
147+
);
148+
} catch (\Exception $e) {
149+
$this->assertContains('--theme (-t) and --exclude-theme cannot be used at the same time', $e->getMessage());
150+
$this->assertInstanceOf(InvalidArgumentException::class, $e);
151+
}
152+
}
153+
154+
public function testCheckLanguagesInputException()
155+
{
156+
$options = [
157+
new InputOption(Options::AREA, null, 4, '', ['all']),
158+
new InputOption(Options::EXCLUDE_AREA, '', 4, '', ['none']),
159+
new InputOption(Options::THEME, null, 4, '', ['all']),
160+
new InputOption(Options::EXCLUDE_THEME, null, 4, '', ['none']),
161+
new InputArgument(Options::LANGUAGES_ARGUMENT, 2, '', ['en_US']),
162+
new InputOption(Options::EXCLUDE_LANGUAGE, null, 4, '', ['all'])
163+
];
164+
165+
$inputDefinition = new InputDefinition($options);
166+
167+
try {
168+
$this->inputValidator->validate(
169+
new ArrayInput([], $inputDefinition)
170+
);
171+
} catch (\Exception $e) {
172+
$this->assertContains(
173+
'--language (-l) and --exclude-language cannot be used at the same time',
174+
$e->getMessage()
175+
);
176+
177+
$this->assertInstanceOf(InvalidArgumentException::class, $e);
178+
}
179+
}
180+
181+
public function testCheckVersionInputException()
182+
{
183+
$options = [
184+
new InputOption(Options::AREA, null, 4, '', ['all']),
185+
new InputOption(Options::EXCLUDE_AREA, null, 4, '', ['none']),
186+
new InputOption(Options::THEME, null, 4, '', ['all']),
187+
new InputOption(Options::EXCLUDE_THEME, null, 4, '', ['none']),
188+
new InputArgument(Options::LANGUAGES_ARGUMENT, 2, '', ['en_US']),
189+
new InputOption(Options::EXCLUDE_LANGUAGE, null, 4, '', ['none']),
190+
new InputOption(Options::CONTENT_VERSION, null, 4, '', '/*!#')
191+
];
192+
193+
$inputDefinition = new InputDefinition($options);
194+
195+
try {
196+
$this->inputValidator->validate(
197+
new ArrayInput([], $inputDefinition)
198+
);
199+
} catch (\Exception $e) {
200+
$this->assertContains(
201+
'Argument "' .
202+
Options::CONTENT_VERSION
203+
. '" has invalid value, content version should contain only characters, digits and dots',
204+
$e->getMessage()
205+
);
206+
207+
$this->assertInstanceOf(InvalidArgumentException::class, $e);
208+
}
209+
}
210+
}

0 commit comments

Comments
 (0)