Skip to content

Commit

Permalink
Merge pull request #583 from magento-falcons/MAGETWO-60647
Browse files Browse the repository at this point in the history
Fixed issues:
- MAGETWO-60647: Delivery of bug fixes for Sample Data and Import/Export
- MAGETWO-56787: [GITHUB][PR] Added call to action to compile command error #4134
- MAGETWO-56786: [GITHUB][PR] Ensure composer.json exists #4121
- MAGETWO-57799: cannot upgrade 2.0 => 2.1 with auto_increment > 1
- MAGETWO-59715: Impossible to import additional_images with labels which have a comma separator
- MAGETWO-60633: [Github] .htaccess deny code execution not working for Apache + php-fpm #6766
- MAGETWO-46636: Nginx doesn't redirect to setup page when using port
  • Loading branch information
MomotenkoNatalia authored Nov 11, 2016
2 parents bbd4b4c + fa9ae9e commit d403ec9
Show file tree
Hide file tree
Showing 20 changed files with 433 additions and 94 deletions.
145 changes: 115 additions & 30 deletions app/code/Magento/CatalogImportExport/Model/Import/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -1416,7 +1416,7 @@ protected function initMediaGalleryResources()
}

/**
* Get existing images for current bucnh
* Get existing images for current bunch
*
* @param array $bunch
* @return array
Expand All @@ -1436,7 +1436,21 @@ protected function getExistingImages($bunch)
)->joinInner(
['mgvte' => $this->mediaGalleryEntityToValueTableName],
'(mg.value_id = mgvte.value_id)',
[$this->getProductEntityLinkField() => 'mgvte.' . $this->getProductEntityLinkField()]
[
$this->getProductEntityLinkField() => 'mgvte.' . $this->getProductEntityLinkField(),
'value_id' => 'mgvte.value_id'
]
)->joinLeft(
['mgv' => $this->mediaGalleryValueTableName],
sprintf(
'(mg.value_id = mgv.value_id AND mgv.%s = mgvte.%s AND mgv.store_id = %d)',
$this->getProductEntityLinkField(),
$this->getProductEntityLinkField(),
\Magento\Store\Model\Store::DEFAULT_STORE_ID
),
[
'label' => 'mgv.label'
]
)->joinInner(
['pe' => $this->productEntityTableName],
"(mgvte.{$this->getProductEntityLinkField()} = pe.{$this->getProductEntityLinkField()})",
Expand All @@ -1447,7 +1461,7 @@ protected function getExistingImages($bunch)
);

foreach ($this->_connection->fetchAll($select) as $image) {
$result[$image['sku']][$image['value']] = true;
$result[$image['sku']][$image['value']] = $image;
}

return $result;
Expand All @@ -1462,22 +1476,21 @@ public function getImagesFromRow(array $rowData)
$images = [];
$labels = [];
foreach ($this->_imagesArrayKeys as $column) {
$images[$column] = [];
$labels[$column] = [];
if (!empty($rowData[$column])) {
$images[$column] = array_unique(
explode($this->getMultipleValueSeparator(), $rowData[$column])
array_map(
'trim',
explode($this->getMultipleValueSeparator(), $rowData[$column])
)
);
}

if (!empty($rowData[$column . '_label'])) {
$labels[$column] = explode($this->getMultipleValueSeparator(), $rowData[$column . '_label']);
}
if (!empty($rowData[$column . '_label'])) {
$labels[$column] = $this->parseMultipleValues($rowData[$column . '_label']);

if (count($labels[$column]) > count($images[$column])) {
$labels[$column] = array_slice($labels[$column], 0, count($images[$column]));
} elseif (count($labels[$column]) < count($images[$column])) {
$labels[$column] = array_pad($labels[$column], count($images[$column]), '');
if (count($labels[$column]) > count($images[$column])) {
$labels[$column] = array_slice($labels[$column], 0, count($images[$column]));
}
}
}
}

Expand Down Expand Up @@ -1507,6 +1520,7 @@ protected function _saveProducts()
$this->categoriesCache = [];
$tierPrices = [];
$mediaGallery = [];
$labelsForUpdate = [];
$uploadedImages = [];
$previousType = null;
$prevAttributeSet = null;
Expand Down Expand Up @@ -1616,7 +1630,7 @@ protected function _saveProducts()
foreach ($rowImages as $column => $columnImages) {
foreach ($columnImages as $position => $columnImage) {
if (!isset($uploadedImages[$columnImage])) {
$uploadedFile = $this->uploadMediaFiles(trim($columnImage), true);
$uploadedFile = $this->uploadMediaFiles($columnImage, true);
if ($uploadedFile) {
$uploadedImages[$columnImage] = $uploadedFile;
} else {
Expand All @@ -1636,20 +1650,28 @@ protected function _saveProducts()
$rowData[$column] = $uploadedFile;
}

$imageNotAssigned = !isset($existingImages[$rowSku][$uploadedFile]);

if ($uploadedFile && $imageNotAssigned) {
if ($column == self::COL_MEDIA_IMAGE) {
$rowData[$column][] = $uploadedFile;
if ($uploadedFile && !isset($mediaGallery[$rowSku][$uploadedFile])) {
if (isset($existingImages[$rowSku][$uploadedFile])) {
if (isset($rowLabels[$column][$position])
&& $rowLabels[$column][$position] != $existingImages[$rowSku][$uploadedFile]['label']
) {
$labelsForUpdate[] = [
'label' => $rowLabels[$column][$position],
'imageData' => $existingImages[$rowSku][$uploadedFile]
];
}
} else {
if ($column == self::COL_MEDIA_IMAGE) {
$rowData[$column][] = $uploadedFile;
}
$mediaGallery[$rowSku][$uploadedFile] = [
'attribute_id' => $this->getMediaGalleryAttributeId(),
'label' => isset($rowLabels[$column][$position]) ? $rowLabels[$column][$position] : '',
'position' => $position + 1,
'disabled' => isset($disabledImages[$columnImage]) ? '1' : '0',
'value' => $uploadedFile,
];
}
$mediaGallery[$rowSku][] = [
'attribute_id' => $this->getMediaGalleryAttributeId(),
'label' => isset($rowLabels[$column][$position]) ? $rowLabels[$column][$position] : '',
'position' => $position + 1,
'disabled' => isset($disabledImages[$columnImage]) ? '1' : '0',
'value' => $uploadedFile,
];
$existingImages[$rowSku][$uploadedFile] = true;
}
}
}
Expand Down Expand Up @@ -1767,6 +1789,8 @@ protected function _saveProducts()
$mediaGallery
)->_saveProductAttributes(
$attributes
)->updateMediaGalleryLabels(
$labelsForUpdate
);

$this->_eventManager->dispatch(
Expand Down Expand Up @@ -2535,12 +2559,13 @@ private function parseAttributesWithWrappedValues($attributesData)
* Parse values of multiselect attributes depends on "Fields Enclosure" parameter
*
* @param string $values
* @param string $delimiter
* @return array
*/
public function parseMultiselectValues($values)
public function parseMultiselectValues($values, $delimiter = self::PSEUDO_MULTI_LINE_SEPARATOR)
{
if (empty($this->_parameters[Import::FIELDS_ENCLOSURE])) {
return explode(self::PSEUDO_MULTI_LINE_SEPARATOR, $values);
return explode($delimiter, $values);
}
if (preg_match_all('~"((?:[^"]|"")*)"~', $values, $matches)) {
return $values = array_map(function ($value) {
Expand Down Expand Up @@ -2752,4 +2777,64 @@ private function getProductIdentifierField()
}
return $this->productEntityIdentifierField;
}

/**
* Update media gallery labels
*
* @param array $labels
* @return void
*/
private function updateMediaGalleryLabels(array $labels)
{
if (empty($labels)) {
return;
}

$insertData = [];
foreach ($labels as $label) {
$imageData = $label['imageData'];

if ($imageData['label'] === null) {
$insertData[] = [
'label' => $label['label'],
$this->getProductEntityLinkField() => $imageData[$this->getProductEntityLinkField()],
'value_id' => $imageData['value_id'],
'store_id' => \Magento\Store\Model\Store::DEFAULT_STORE_ID
];
} else {
$this->_connection->update(
$this->mediaGalleryValueTableName,
[
'label' => $label['label']
],
[
$this->getProductEntityLinkField() . ' = ?' => $imageData[$this->getProductEntityLinkField()],
'value_id = ?' => $imageData['value_id'],
'store_id = ?' => \Magento\Store\Model\Store::DEFAULT_STORE_ID
]
);
}
}

if (!empty($insertData)) {
$this->_connection->insertMultiple(
$this->mediaGalleryValueTableName,
$insertData
);
}
}

/**
* Parse values from multiple attributes fields
*
* @param string $labelRow
* @return array
*/
private function parseMultipleValues($labelRow)
{
return $this->parseMultiselectValues(
$labelRow,
$this->getMultipleValueSeparator()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*/
namespace Magento\CatalogImportExport\Model\Import\Product\Validator;

use Magento\CatalogImportExport\Model\Import\Product\Validator\AbstractImportValidator;
use Magento\CatalogImportExport\Model\Import\Product\RowValidatorInterface;

class Media extends AbstractImportValidator implements RowValidatorInterface
Expand All @@ -16,19 +15,15 @@ class Media extends AbstractImportValidator implements RowValidatorInterface

const ADDITIONAL_IMAGES = 'additional_images';

/**
* @deprecated
* @see \Magento\CatalogImportExport\Model\Import\Product::getMultipleValueSeparator()
*/
const ADDITIONAL_IMAGES_DELIMITER = ',';

/** @var array */
protected $mediaAttributes = ['image', 'small_image', 'thumbnail'];

/**
* {@inheritdoc}
*/
public function init($context)
{
return parent::init($context);
}

/**
* @param string $string
* @return bool
Expand Down Expand Up @@ -83,7 +78,7 @@ public function isValid($value)
}
}
if (isset($value[self::ADDITIONAL_IMAGES]) && strlen($value[self::ADDITIONAL_IMAGES])) {
foreach (explode(self::ADDITIONAL_IMAGES_DELIMITER, $value[self::ADDITIONAL_IMAGES]) as $image) {
foreach (explode($this->context->getMultipleValueSeparator(), $value[self::ADDITIONAL_IMAGES]) as $image) {
if (!$this->checkPath($image) && !$this->checkValidUrl($image)) {
$this->_addMessages(
[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@

namespace Magento\CatalogImportExport\Test\Unit\Model\Import\Product\Validator;

use Magento\CatalogImportExport\Model\Import\Product;
use Magento\CatalogImportExport\Model\Import\Product\Validator\Media;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
use Magento\ImportExport\Model\Import;

class MediaTest extends \PHPUnit_Framework_TestCase
{
/** @var \Magento\CatalogImportExport\Model\Import\Product\Validator\Media */
/** @var Media */
protected $media;

/** @var ObjectManagerHelper */
Expand All @@ -21,7 +24,7 @@ protected function setUp()

$this->objectManagerHelper = new ObjectManagerHelper($this);
$this->media = $this->objectManagerHelper->getObject(
\Magento\CatalogImportExport\Model\Import\Product\Validator\Media::class,
Media::class,
[

]
Expand All @@ -41,6 +44,18 @@ public function testInit()
*/
public function testIsValid($data, $expected)
{
$contextMock = $this->getMockBuilder(Product::class)
->disableOriginalConstructor()
->getMock();
$contextMock->expects($this->any())
->method('getMultipleValueSeparator')
->willReturn(Import::DEFAULT_GLOBAL_MULTI_VALUE_SEPARATOR);
$contextMock->expects($this->any())
->method('retrieveMessageTemplate')
->with(Media::ERROR_INVALID_MEDIA_URL_OR_PATH)
->willReturn('%s');
$this->media->init($contextMock);

$result = $this->media->isValid($data);
$this->assertEquals($expected['result'], $result);
$messages = $this->media->getMessages();
Expand All @@ -50,7 +65,7 @@ public function testIsValid($data, $expected)
public function testIsValidClearMessagesCall()
{
$media = $this->getMock(
\Magento\CatalogImportExport\Model\Import\Product\Validator\Media::class,
Media::class,
['_clearMessages'],
[],
'',
Expand Down Expand Up @@ -78,6 +93,14 @@ public function isMediaValidDataProvider()
'invalid' => [
['_media_image' => 1],
['result' => true,'messages' => []],
],
'additional_images' => [
['additional_images' => 'image1.png,image2.jpg'],
['result' => true, 'messages' => []]
],
'additional_images_fail' => [
['additional_images' => 'image1.png|image2.jpg|image3.gif'],
['result' => false, 'messages' => [0 => 'additional_images']]
]
];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1280,6 +1280,43 @@ public function testValidateValidateOptionEntity()
$importProduct->validateRow($rowData, $rowNum);
}

/**
* @dataProvider getImagesFromRowDataProvider
*/
public function testGetImagesFromRow($rowData, $expectedResult)
{
$this->assertEquals(
$this->importProduct->getImagesFromRow($rowData),
$expectedResult
);
}

public function getImagesFromRowDataProvider()
{
return [
[
[],
[[], []]
],
[
[
'image' => 'image3.jpg',
'_media_image' => 'image1.jpg,image2.png',
'_media_image_label' => 'label1,label2'
],
[
[
'image' => ['image3.jpg'],
'_media_image' => ['image1.jpg', 'image2.png']
],
[
'_media_image' => ['label1', 'label2']
],
]
]
];
}

public function validateRowValidateNewProductTypeAddRowErrorCallDataProvider()
{
return [
Expand Down
8 changes: 7 additions & 1 deletion app/code/Magento/Cms/Setup/UpgradeData.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@

class UpgradeData implements UpgradeDataInterface
{
/**
* @deprecated
*/
const PRIVACY_COOKIE_PAGE_ID = 4;

/**
Expand Down Expand Up @@ -234,7 +237,10 @@ public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface
</table>
</div>
EOD;
$privacyAndCookiePolicyPage = $this->createPage()->load(self::PRIVACY_COOKIE_PAGE_ID);
$privacyAndCookiePolicyPage = $this->createPage()->load(
'privacy-policy-cookie-restriction-mode',
'identifier'
);
$privacyAndCookiePolicyPageId = $privacyAndCookiePolicyPage->getId();
if ($privacyAndCookiePolicyPageId) {
$privacyAndCookiePolicyPage->setContent($newPageContent);
Expand Down
Loading

0 comments on commit d403ec9

Please sign in to comment.