Skip to content

Commit a4a767a

Browse files
committed
Merge branch 'develop' into MAGETWO-67048
2 parents f5fd74f + d816ce7 commit a4a767a

File tree

91 files changed

+3206
-941
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

91 files changed

+3206
-941
lines changed

app/code/Magento/Bundle/Model/CartItemProcessor.php

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -84,26 +84,28 @@ public function processOptions(CartItemInterface $cartItem)
8484
$productOptions = [];
8585
$bundleOptions = $cartItem->getBuyRequest()->getBundleOption();
8686
$bundleOptionsQty = $cartItem->getBuyRequest()->getBundleOptionQty();
87-
foreach ($bundleOptions as $optionId => $optionSelections) {
88-
if (empty($optionSelections)) {
89-
continue;
90-
}
91-
$optionSelections = is_array($optionSelections) ? $optionSelections : [$optionSelections];
92-
$optionQty = isset($bundleOptionsQty[$optionId]) ? $bundleOptionsQty[$optionId] : 1;
87+
if (is_array($bundleOptions)) {
88+
foreach ($bundleOptions as $optionId => $optionSelections) {
89+
if (empty($optionSelections)) {
90+
continue;
91+
}
92+
$optionSelections = is_array($optionSelections) ? $optionSelections : [$optionSelections];
93+
$optionQty = isset($bundleOptionsQty[$optionId]) ? $bundleOptionsQty[$optionId] : 1;
9394

94-
/** @var \Magento\Bundle\Api\Data\BundleOptionInterface $productOption */
95-
$productOption = $this->bundleOptionFactory->create();
96-
$productOption->setOptionId($optionId);
97-
$productOption->setOptionSelections($optionSelections);
98-
$productOption->setOptionQty($optionQty);
99-
$productOptions[] = $productOption;
100-
}
95+
/** @var \Magento\Bundle\Api\Data\BundleOptionInterface $productOption */
96+
$productOption = $this->bundleOptionFactory->create();
97+
$productOption->setOptionId($optionId);
98+
$productOption->setOptionSelections($optionSelections);
99+
$productOption->setOptionQty($optionQty);
100+
$productOptions[] = $productOption;
101+
}
101102

102-
$extension = $this->productOptionExtensionFactory->create()->setBundleOptions($productOptions);
103-
if (!$cartItem->getProductOption()) {
104-
$cartItem->setProductOption($this->productOptionFactory->create());
103+
$extension = $this->productOptionExtensionFactory->create()->setBundleOptions($productOptions);
104+
if (!$cartItem->getProductOption()) {
105+
$cartItem->setProductOption($this->productOptionFactory->create());
106+
}
107+
$cartItem->getProductOption()->setExtensionAttributes($extension);
105108
}
106-
$cartItem->getProductOption()->setExtensionAttributes($extension);
107109
return $cartItem;
108110
}
109111
}

app/code/Magento/Bundle/Test/Unit/Model/CartItemProcessorTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,4 +180,22 @@ public function testProcessProductOptionsInvalidType()
180180
$cartItemMock->expects($this->once())->method('getProductType')->willReturn(Type::TYPE_SIMPLE);
181181
$this->assertSame($cartItemMock, $this->model->processOptions($cartItemMock));
182182
}
183+
184+
public function testProcessProductOptionsifBundleOptionsNotExists()
185+
{
186+
$buyRequestMock = new \Magento\Framework\DataObject(
187+
[]
188+
);
189+
$methods = ['getProductType', 'getBuyRequest'];
190+
$cartItemMock = $this->getMock(
191+
\Magento\Quote\Model\Quote\Item::class,
192+
$methods,
193+
[],
194+
'',
195+
false
196+
);
197+
$cartItemMock->expects($this->once())->method('getProductType')->willReturn(Type::TYPE_BUNDLE);
198+
$cartItemMock->expects($this->exactly(2))->method('getBuyRequest')->willReturn($buyRequestMock);
199+
$this->assertSame($cartItemMock, $this->model->processOptions($cartItemMock));
200+
}
183201
}

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: 92 additions & 13 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,16 +21,35 @@ 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+
$contextMock = $this->getMockBuilder(Product::class)
35+
->disableOriginalConstructor()
36+
->getMock();
37+
$contextMock->expects($this->any())
38+
->method('getMultipleValueSeparator')
39+
->willReturn(Import::DEFAULT_GLOBAL_MULTI_VALUE_SEPARATOR);
40+
$contextMock->expects($this->any())
41+
->method('retrieveMessageTemplate')
42+
->with(Media::ERROR_INVALID_MEDIA_URL_OR_PATH)
43+
->willReturn('%s');
44+
2545
$this->objectManagerHelper = new ObjectManagerHelper($this);
2646
$this->media = $this->objectManagerHelper->getObject(
2747
Media::class,
2848
[
29-
49+
'validator' => $this->validatorMock
3050
]
3151
);
52+
$this->media->init($contextMock);
3253
}
3354

3455
public function testInit()
@@ -44,17 +65,8 @@ public function testInit()
4465
*/
4566
public function testIsValid($data, $expected)
4667
{
47-
$contextMock = $this->getMockBuilder(Product::class)
48-
->disableOriginalConstructor()
49-
->getMock();
50-
$contextMock->expects($this->any())
51-
->method('getMultipleValueSeparator')
52-
->willReturn(Import::DEFAULT_GLOBAL_MULTI_VALUE_SEPARATOR);
53-
$contextMock->expects($this->any())
54-
->method('retrieveMessageTemplate')
55-
->with(Media::ERROR_INVALID_MEDIA_URL_OR_PATH)
56-
->willReturn('%s');
57-
$this->media->init($contextMock);
68+
$this->validatorMock->expects($this->never())
69+
->method('isValid');
5870

5971
$result = $this->media->isValid($data);
6072
$this->assertEquals($expected['result'], $result);
@@ -76,6 +88,47 @@ public function testIsValidClearMessagesCall()
7688
$media->isValid([]);
7789
}
7890

91+
/**
92+
* @param array $data
93+
* @param array $expected
94+
* @dataProvider isValidAdditionalImagesPathDataProvider
95+
*/
96+
public function testIsValidAdditionalImagesPath($data, $expected)
97+
{
98+
if ($expected['result']) {
99+
$this->validatorMock->expects($this->never())
100+
->method('isValid');
101+
} else {
102+
$this->validatorMock->expects($this->once())
103+
->method('isValid')
104+
->with($data['additional_images'])
105+
->willReturn(false);
106+
}
107+
108+
$result = $this->media->isValid($data);
109+
$this->assertEquals($expected['result'], $result);
110+
$messages = $this->media->getMessages();
111+
$this->assertEquals($expected['messages'], $messages);
112+
}
113+
114+
/**
115+
* @param array $data
116+
* @param array $expected
117+
* @dataProvider isValidAdditionalImagesUrlDataProvider
118+
*/
119+
public function testIsValidAdditionalImagesUrl($data, $expected)
120+
{
121+
$this->validatorMock->expects($this->once())
122+
->method('isValid')
123+
->with($data['additional_images'])
124+
->willReturn($expected['result']);
125+
126+
$result = $this->media->isValid($data);
127+
$this->assertEquals($expected['result'], $result);
128+
$messages = $this->media->getMessages();
129+
$this->assertEquals($expected['messages'], $messages);
130+
}
131+
79132
/**
80133
* @return array
81134
*/
@@ -94,13 +147,39 @@ public function isMediaValidDataProvider()
94147
['_media_image' => 1],
95148
['result' => true,'messages' => []],
96149
],
150+
];
151+
}
152+
153+
/**
154+
* @return array
155+
*/
156+
public function isValidAdditionalImagesPathDataProvider()
157+
{
158+
return [
97159
'additional_images' => [
98160
['additional_images' => 'image1.png,image2.jpg'],
99161
['result' => true, 'messages' => []]
100162
],
101163
'additional_images_fail' => [
102164
['additional_images' => 'image1.png|image2.jpg|image3.gif'],
103165
['result' => false, 'messages' => [0 => 'additional_images']]
166+
],
167+
];
168+
}
169+
170+
/**
171+
* @return array
172+
*/
173+
public function isValidAdditionalImagesUrlDataProvider()
174+
{
175+
return [
176+
'additional_images_wrong_domain' => [
177+
['additional_images' => 'https://example/images/some-name.jpg'],
178+
['result' => false, 'messages' => [0 => 'additional_images']],
179+
],
180+
'additional_images_url_multiple_underscores' => [
181+
['additional_images' => 'https://example.com/images/some-name__with___multiple____underscores.jpg'],
182+
['result' => true, 'messages' => []]
104183
]
105184
];
106185
}

app/code/Magento/CatalogWidget/Model/Rule/Condition/Product.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ protected function addNotGlobalAttribute(
222222
public function getMappedSqlField()
223223
{
224224
$result = '';
225-
if ($this->getAttribute() == 'category_ids') {
225+
if (in_array($this->getAttribute(), ['category_ids', 'sku'])) {
226226
$result = parent::getMappedSqlField();
227227
} elseif (isset($this->joinedAttributes[$this->getAttribute()])) {
228228
$result = $this->joinedAttributes[$this->getAttribute()];

app/code/Magento/CatalogWidget/Test/Unit/Model/Rule/Condition/ProductTest.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ protected function setUp()
3636
'',
3737
false
3838
);
39-
$eavConfig->expects($this->once())->method('getAttribute')->willReturn($this->attributeMock);
39+
$eavConfig->expects($this->any())->method('getAttribute')->willReturn($this->attributeMock);
4040
$ruleMock = $this->getMock(\Magento\SalesRule\Model\Rule::class, [], [], '', false);
4141
$storeManager = $this->getMock(\Magento\Store\Model\StoreManagerInterface::class);
4242
$storeMock = $this->getMock(\Magento\Store\Api\Data\StoreInterface::class);
43-
$storeManager->expects($this->once())->method('getStore')->willReturn($storeMock);
43+
$storeManager->expects($this->any())->method('getStore')->willReturn($storeMock);
4444
$this->resourceMock = $this->getMock(
4545
\Magento\Indexer\Model\ResourceModel\FrontendResource::class,
4646
[],
@@ -49,8 +49,8 @@ protected function setUp()
4949
false
5050
);
5151
$productResource = $this->getMock(\Magento\Catalog\Model\ResourceModel\Product::class, [], [], '', false);
52-
$productResource->expects($this->once())->method('loadAllAttributes')->willReturnSelf();
53-
$productResource->expects($this->once())->method('getAttributesByCode')->willReturn([]);
52+
$productResource->expects($this->any())->method('loadAllAttributes')->willReturnSelf();
53+
$productResource->expects($this->any())->method('getAttributesByCode')->willReturn([]);
5454
$this->model = $objectManagerHelper->getObject(
5555
\Magento\CatalogWidget\Model\Rule\Condition\Product::class,
5656
[
@@ -88,4 +88,10 @@ public function testAddToCollection()
8888
$this->resourceMock->expects($this->once())->method('getMainTable')->willReturn('catalog_product_index_eav');
8989
$this->model->addToCollection($collectionMock);
9090
}
91+
92+
public function testGetMappedSqlFieldSku()
93+
{
94+
$this->model->setAttribute('sku');
95+
$this->assertEquals('e.sku', $this->model->getMappedSqlField());
96+
}
9197
}

0 commit comments

Comments
 (0)