Skip to content

Commit fe24d04

Browse files
MAGETWO-88608: Wrong behavior of static content deploy
1 parent f34f1f0 commit fe24d04

File tree

2 files changed

+244
-3
lines changed

2 files changed

+244
-3
lines changed

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

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55
*/
66
namespace Magento\Deploy\Console;
77

8-
use Magento\Setup\Console\Command\DeployStaticContentCommand;
8+
use Magento\Framework\App\ObjectManager;
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\Validator\RegexFactory;
1213

1314
/**
1415
* Command input arguments validator class
@@ -55,21 +56,32 @@ class InputValidator
5556
*/
5657
private $localeValidator;
5758

59+
/**
60+
* @var RegexFactory
61+
*/
62+
private $versionValidatorFactory;
63+
5864
/**
5965
* InputValidator constructor
6066
*
6167
* @param Locale $localeValidator
68+
* @param RegexFactory|null $versionValidatorFactory
6269
*/
63-
public function __construct(Locale $localeValidator)
64-
{
70+
public function __construct(
71+
Locale $localeValidator,
72+
RegexFactory $versionValidatorFactory = null
73+
) {
6574
$this->localeValidator = $localeValidator;
75+
$this->versionValidatorFactory = $versionValidatorFactory ?: ObjectManager::getInstance()
76+
->get(RegexFactory::class);
6677
}
6778

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

0 commit comments

Comments
 (0)