Skip to content

Commit 16b1cf4

Browse files
committed
MAGETWO-67321: Importing Products with images from external URL
1 parent d844e31 commit 16b1cf4

File tree

2 files changed

+64
-6
lines changed
  • app/code/Magento/CatalogImportExport

2 files changed

+64
-6
lines changed

app/code/Magento/CatalogImportExport/Model/Import/Product/Validator/Media.php

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,36 @@
66
namespace Magento\CatalogImportExport\Model\Import\Product\Validator;
77

88
use Magento\CatalogImportExport\Model\Import\Product\RowValidatorInterface;
9+
use Magento\Framework\App\ObjectManager;
10+
use Magento\Framework\Url\Validator;
911

1012
class Media extends AbstractImportValidator implements RowValidatorInterface
1113
{
14+
/**
15+
* @deprecated As this regexp doesn't give guarantee of correct url validation
16+
* @see \Magento\Framework\Url\Validator::isValid()
17+
*/
1218
const URL_REGEXP = '|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i';
1319

1420
const PATH_REGEXP = '#^(?!.*[\\/]\.{2}[\\/])(?!\.{2}[\\/])[-\w.\\/]+$#';
1521

1622
const ADDITIONAL_IMAGES = 'additional_images';
1723

24+
/**
25+
* The url validator. Checks if given url is valid.
26+
*
27+
* @var Validator
28+
*/
29+
private $validator;
30+
31+
/**
32+
* @param Validator $validator The url validator
33+
*/
34+
public function __construct(Validator $validator = null)
35+
{
36+
$this->validator = $validator ?: ObjectManager::getInstance()->get(Validator::class);
37+
}
38+
1839
/**
1940
* @deprecated
2041
* @see \Magento\CatalogImportExport\Model\Import\Product::getMultipleValueSeparator()
@@ -27,6 +48,8 @@ class Media extends AbstractImportValidator implements RowValidatorInterface
2748
/**
2849
* @param string $string
2950
* @return bool
51+
* @deprecated As this method doesn't give guarantee of correct url validation.
52+
* @see \Magento\Framework\Url\Validator::isValid() It provides better url validation.
3053
*/
3154
protected function checkValidUrl($string)
3255
{
@@ -64,7 +87,7 @@ public function isValid($value)
6487
$valid = true;
6588
foreach ($this->mediaAttributes as $attribute) {
6689
if (isset($value[$attribute]) && strlen($value[$attribute])) {
67-
if (!$this->checkPath($value[$attribute]) && !$this->checkValidUrl($value[$attribute])) {
90+
if (!$this->checkPath($value[$attribute]) && !$this->validator->isValid($value[$attribute])) {
6891
$this->_addMessages(
6992
[
7093
sprintf(
@@ -79,7 +102,7 @@ public function isValid($value)
79102
}
80103
if (isset($value[self::ADDITIONAL_IMAGES]) && strlen($value[self::ADDITIONAL_IMAGES])) {
81104
foreach (explode($this->context->getMultipleValueSeparator(), $value[self::ADDITIONAL_IMAGES]) as $image) {
82-
if (!$this->checkPath($image) && !$this->checkValidUrl($image)) {
105+
if (!$this->checkPath($image) && !$this->validator->isValid($image)) {
83106
$this->_addMessages(
84107
[
85108
sprintf(

app/code/Magento/CatalogImportExport/Test/Unit/Model/Import/Product/Validator/MediaTest.php

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
use Magento\CatalogImportExport\Model\Import\Product\Validator\Media;
1111
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
1212
use Magento\ImportExport\Model\Import;
13+
use Magento\Framework\Url\Validator;
14+
use PHPUnit_Framework_MockObject_MockObject as MockObject;
1315

1416
class MediaTest extends \PHPUnit_Framework_TestCase
1517
{
@@ -19,14 +21,22 @@ class MediaTest extends \PHPUnit_Framework_TestCase
1921
/** @var ObjectManagerHelper */
2022
protected $objectManagerHelper;
2123

24+
/**
25+
* @var Validator|MockObject
26+
*/
27+
private $validatorMock;
28+
2229
protected function setUp()
2330
{
24-
31+
$this->validatorMock = $this->getMockBuilder(Validator::class)
32+
->disableOriginalConstructor()
33+
->getMock();
34+
2535
$this->objectManagerHelper = new ObjectManagerHelper($this);
2636
$this->media = $this->objectManagerHelper->getObject(
2737
Media::class,
2838
[
29-
39+
'validator' => $this->validatorMock
3040
]
3141
);
3242
}
@@ -40,10 +50,15 @@ public function testInit()
4050
/**
4151
* @param array $data
4252
* @param array $expected
53+
* @param \Closure|null $validatorCallback
4354
* @dataProvider isMediaValidDataProvider
4455
*/
45-
public function testIsValid($data, $expected)
56+
public function testIsValid($data, $expected, \Closure $validatorCallback = null)
4657
{
58+
if ($validatorCallback !== null) {
59+
$validatorCallback($this->validatorMock);
60+
}
61+
4762
$contextMock = $this->getMockBuilder(Product::class)
4863
->disableOriginalConstructor()
4964
->getMock();
@@ -101,7 +116,27 @@ public function isMediaValidDataProvider()
101116
'additional_images_fail' => [
102117
['additional_images' => 'image1.png|image2.jpg|image3.gif'],
103118
['result' => false, 'messages' => [0 => 'additional_images']]
104-
]
119+
],
120+
'additional_images_wrong_domain' => [
121+
['additional_images' => 'https://example/images/some-name.jpg'],
122+
['result' => false, 'messages' => [0 => 'additional_images']],
123+
function ($validatorMock) {
124+
$validatorMock->expects($this->once())
125+
->method('isValid')
126+
->with('https://example/images/some-name.jpg')
127+
->willReturn(false);
128+
}
129+
],
130+
'additional_images_url_multiple_underscores' => [
131+
['additional_images' => 'https://example.com/images/some-name__with___multiple____underscores.jpg'],
132+
['result' => true, 'messages' => []],
133+
function ($validatorMock) {
134+
$validatorMock->expects($this->once())
135+
->method('isValid')
136+
->with('https://example.com/images/some-name__with___multiple____underscores.jpg')
137+
->willReturn(true);
138+
}
139+
],
105140
];
106141
}
107142
}

0 commit comments

Comments
 (0)