Skip to content

Commit

Permalink
Merge pull request #6306 from magento-tsg-csl3/2.4-develop-pr44
Browse files Browse the repository at this point in the history
[TSG-CSL3] For 2.4 (pr44)
  • Loading branch information
gabrieldagama authored Nov 5, 2020
2 parents f472536 + b086b7d commit aa272e6
Show file tree
Hide file tree
Showing 36 changed files with 1,386 additions and 241 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,30 @@ public function getPositions(int $categoryId): array

return array_flip($connection->fetchCol($select));
}

/**
* Get category product minimum position
*
* @param int $categoryId
* @return int
*/
public function getMinPosition(int $categoryId): int
{
$connection = $this->getConnection();

$select = $connection->select()->from(
['cpe' => $this->getTable('catalog_product_entity')],
['position' => new \Zend_Db_Expr('MIN(position)')]
)->joinLeft(
['ccp' => $this->getTable('catalog_category_product')],
'ccp.product_id=cpe.entity_id'
)->where(
'ccp.category_id = ?',
$categoryId
)->order(
'ccp.product_id ' . \Magento\Framework\DB\Select::SQL_DESC
);

return (int)$connection->fetchOne($select);
}
}
40 changes: 40 additions & 0 deletions app/code/Magento/Catalog/Model/Product/Option/Type/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ public function validateUserValue($values)
$dateValid = true;
if ($this->_dateExists()) {
if ($this->useCalendar()) {
if (is_array($value) && $this->checkDateWithoutJSCalendar($value)) {
$value['date'] = sprintf("%s/%s/%s", $value['day'], $value['month'], $value['year']);
}
/* Fixed validation if the date was not saved correctly after re-saved the order
for example: "09\/24\/2020,2020-09-24 00:00:00" */
if (is_string($value) && preg_match('/^\d{1,4}.+\d{1,4}.+\d{1,4},+(\w|\W)*$/', $value)) {
Expand All @@ -81,6 +84,9 @@ public function validateUserValue($values)
}
$dateValid = isset($value['date']) && preg_match('/^\d{1,4}.+\d{1,4}.+\d{1,4}$/', $value['date']);
} else {
if (is_array($value)) {
$value = $this->prepareDateByDateInternal($value);
}
$dateValid = isset(
$value['day']
) && isset(
Expand Down Expand Up @@ -411,4 +417,38 @@ protected function _timeExists()
]
);
}

/**
* Check is date without JS Calendar
*
* @param array $value
*
* @return bool
*/
private function checkDateWithoutJSCalendar(array $value): bool
{
return empty($value['date'])
&& !empty($value['day'])
&& !empty($value['month'])
&& !empty($value['year']);
}

/**
* Prepare date by date internal
*
* @param array $value
* @return array
*/
private function prepareDateByDateInternal(array $value): array
{
if (!empty($value['date']) && !empty($value['date_internal'])) {
$formatDate = explode(' ', $value['date_internal']);
$date = explode('-', $formatDate[0]);
$value['year'] = $date[0];
$value['month'] = $date[1];
$value['day'] = $date[2];
}

return $value;
}
}
3 changes: 2 additions & 1 deletion app/code/Magento/Catalog/Model/Product/Type/AbstractType.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Catalog\Model\Product\Type;

Expand Down Expand Up @@ -620,7 +621,7 @@ protected function _prepareOptions(\Magento\Framework\DataObject $buyRequest, $p
}
}
if (count($results) > 0) {
throw new LocalizedException(__(implode("\n", $results)));
throw new LocalizedException(__(implode("\n", array_unique($results))));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,7 @@ public function prepareAttributesWithDefaultValueForSave(array $rowData, $withDe
if ($attrParams['is_static']) {
continue;
}
$attrCode = mb_strtolower($attrCode);
if (isset($rowData[$attrCode]) && strlen(trim($rowData[$attrCode]))) {
if (in_array($attrParams['type'], ['select', 'boolean'])) {
$resultAttrs[$attrCode] = $attrParams['options'][strtolower($rowData[$attrCode])];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
*/
namespace Magento\CatalogImportExport\Test\Unit\Model\Import\Product\Type;

use Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory as AttributeCollectionFactory;
use Magento\CatalogImportExport\Model\Import\Product;
use Magento\CatalogImportExport\Model\Import\Product\RowValidatorInterface;
use Magento\CatalogImportExport\Model\Import\Product\Type\AbstractType as AbstractType;
use Magento\CatalogImportExport\Model\Import\Product\Type\Simple;
use Magento\Eav\Model\Entity\Attribute;
use Magento\Eav\Model\Entity\Attribute\Set;
use Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\Collection;
use Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\CollectionFactory as AttributeSetCollectionFactory;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\DB\Adapter\AdapterInterface;
use Magento\Framework\DB\Adapter\Pdo\Mysql;
Expand Down Expand Up @@ -68,12 +70,12 @@ protected function setUp(): void
{
$this->entityModel = $this->createMock(Product::class);
$attrSetColFactory = $this->createPartialMock(
\Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\CollectionFactory::class,
AttributeSetCollectionFactory::class,
['create']
);
$attrSetCollection = $this->createMock(Collection::class);
$attrColFactory = $this->createPartialMock(
\Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory::class,
AttributeCollectionFactory::class,
['create']
);
$attributeSet = $this->createMock(Set::class);
Expand All @@ -100,14 +102,22 @@ protected function setUp(): void
)
->disableOriginalConstructor()
->getMock();
$attribute->expects($this->any())->method('getIsVisible')->willReturn(true);
$attribute->expects($this->any())->method('getIsGlobal')->willReturn(true);
$attribute->expects($this->any())->method('getIsRequired')->willReturn(true);
$attribute->expects($this->any())->method('getIsUnique')->willReturn(true);
$attribute->expects($this->any())->method('getFrontendLabel')->willReturn('frontend_label');
$attribute->expects($this->any())->method('getApplyTo')->willReturn(['simple']);
$attribute->expects($this->any())->method('getDefaultValue')->willReturn('default_value');
$attribute->expects($this->any())->method('usesSource')->willReturn(true);
$attribute->method('getIsVisible')
->willReturn(true);
$attribute->method('getIsGlobal')
->willReturn(true);
$attribute->method('getIsRequired')
->willReturn(true);
$attribute->method('getIsUnique')
->willReturn(true);
$attribute->method('getFrontendLabel')
->willReturn('frontend_label');
$attribute->method('getApplyTo')
->willReturn(['simple']);
$attribute->method('getDefaultValue')
->willReturn('default_value');
$attribute->method('usesSource')
->willReturn(true);

$entityAttributes = [
[
Expand All @@ -123,38 +133,54 @@ protected function setUp(): void
$attribute2 = clone $attribute;
$attribute3 = clone $attribute;

$attribute1->expects($this->any())->method('getId')->willReturn('1');
$attribute1->expects($this->any())->method('getAttributeCode')->willReturn('attr_code');
$attribute1->expects($this->any())->method('getFrontendInput')->willReturn('multiselect');
$attribute1->expects($this->any())->method('isStatic')->willReturn(true);

$attribute2->expects($this->any())->method('getId')->willReturn('2');
$attribute2->expects($this->any())->method('getAttributeCode')->willReturn('boolean_attribute');
$attribute2->expects($this->any())->method('getFrontendInput')->willReturn('boolean');
$attribute2->expects($this->any())->method('isStatic')->willReturn(false);

$attribute3->expects($this->any())->method('getId')->willReturn('3');
$attribute3->expects($this->any())->method('getAttributeCode')->willReturn('text_attribute');
$attribute3->expects($this->any())->method('getFrontendInput')->willReturn('text');
$attribute3->expects($this->any())->method('isStatic')->willReturn(false);

$this->entityModel->expects($this->any())->method('getEntityTypeId')->willReturn(3);
$this->entityModel->expects($this->any())->method('getAttributeOptions')->willReturnOnConsecutiveCalls(
['option1', 'option2'],
['yes' => 1, 'no' => 0]
);
$attrSetColFactory->expects($this->any())->method('create')->willReturn($attrSetCollection);
$attrSetCollection->expects($this->any())->method('setEntityTypeFilter')->willReturn([$attributeSet]);
$attrColFactory->expects($this->any())->method('create')->willReturn($attrCollection);
$attrCollection->expects($this->any())
->method('setAttributeSetFilter')
$attribute1->method('getId')
->willReturn('1');
$attribute1->method('getAttributeCode')
->willReturn('attr_code');
$attribute1->method('getFrontendInput')
->willReturn('multiselect');
$attribute1->method('isStatic')
->willReturn(true);

$attribute2->method('getId')
->willReturn('2');
$attribute2->method('getAttributeCode')
->willReturn('boolean_attribute');
$attribute2->method('getFrontendInput')
->willReturn('boolean');
$attribute2->method('isStatic')
->willReturn(false);

$attribute3->method('getId')
->willReturn('3');
$attribute3->method('getAttributeCode')
->willReturn('Text_attribute');
$attribute3->method('getFrontendInput')
->willReturn('text');
$attribute3->method('isStatic')
->willReturn(false);

$this->entityModel->method('getEntityTypeId')
->willReturn(3);
$this->entityModel->method('getAttributeOptions')
->willReturnOnConsecutiveCalls(
['option1', 'option2'],
['yes' => 1, 'no' => 0]
);
$attrSetColFactory->method('create')
->willReturn($attrSetCollection);
$attrSetCollection->method('setEntityTypeFilter')
->willReturn([$attributeSet]);
$attrColFactory->method('create')
->willReturn($attrCollection);
$attrCollection->method('setAttributeSetFilter')
->willReturn([$attribute1, $attribute2, $attribute3]);
$attributeSet->expects($this->any())->method('getId')->willReturn(1);
$attributeSet->expects($this->any())->method('getAttributeSetName')->willReturn('attribute_set_name');
$attributeSet->method('getId')
->willReturn(1);
$attributeSet->method('getAttributeSetName')
->willReturn('attribute_set_name');

$attrCollection
->expects($this->any())
->method('addFieldToFilter')
$attrCollection->method('addFieldToFilter')
->with(
['main_table.attribute_id', 'main_table.attribute_code'],
[
Expand Down Expand Up @@ -193,19 +219,26 @@ protected function setUp(): void
'getConnection',
]
);
$this->select->expects($this->any())->method('from')->willReturnSelf();
$this->select->expects($this->any())->method('where')->willReturnSelf();
$this->select->expects($this->any())->method('joinLeft')->willReturnSelf();
$this->connection->expects($this->any())->method('select')->willReturn($this->select);
$this->select->method('from')
->willReturnSelf();
$this->select->method('where')
->willReturnSelf();
$this->select->method('joinLeft')
->willReturnSelf();
$this->connection->method('select')
->willReturn($this->select);
$connection = $this->createMock(Mysql::class);
$connection->expects($this->any())->method('quoteInto')->willReturn('query');
$this->select->expects($this->any())->method('getConnection')->willReturn($connection);
$this->connection->expects($this->any())->method('insertOnDuplicate')->willReturnSelf();
$this->connection->expects($this->any())->method('delete')->willReturnSelf();
$this->connection->expects($this->any())->method('quoteInto')->willReturn('');
$this->connection
->expects($this->any())
->method('fetchAll')
$connection->method('quoteInto')
->willReturn('query');
$this->select->method('getConnection')
->willReturn($connection);
$this->connection->method('insertOnDuplicate')
->willReturnSelf();
$this->connection->method('delete')
->willReturnSelf();
$this->connection->method('quoteInto')
->willReturn('');
$this->connection->method('fetchAll')
->willReturn($entityAttributes);

$this->resource = $this->createPartialMock(
Expand All @@ -215,12 +248,10 @@ protected function setUp(): void
'getTableName',
]
);
$this->resource->expects($this->any())->method('getConnection')->willReturn(
$this->connection
);
$this->resource->expects($this->any())->method('getTableName')->willReturn(
'tableName'
);
$this->resource->method('getConnection')
->willReturn($this->connection);
$this->resource->method('getTableName')
->willReturn('tableName');

$this->objectManagerHelper = new ObjectManagerHelper($this);
$this->simpleType = $this->objectManagerHelper->getObject(
Expand All @@ -233,9 +264,7 @@ protected function setUp(): void
]
);

$this->abstractType = $this->getMockBuilder(
\Magento\CatalogImportExport\Model\Import\Product\Type\AbstractType::class
)
$this->abstractType = $this->getMockBuilder(AbstractType::class)
->disableOriginalConstructor()
->getMockForAbstractClass();
}
Expand Down Expand Up @@ -277,8 +306,10 @@ public function testIsRowValidSuccess()
{
$rowData = ['_attribute_set' => 'attribute_set_name'];
$rowNum = 1;
$this->entityModel->expects($this->any())->method('getRowScope')->willReturn(null);
$this->entityModel->expects($this->never())->method('addRowError');
$this->entityModel->method('getRowScope')
->willReturn(null);
$this->entityModel->expects($this->never())
->method('addRowError');
$this->setPropertyValue(
$this->simpleType,
'_attributes',
Expand All @@ -296,8 +327,9 @@ public function testIsRowValidError()
'sku' => 'sku'
];
$rowNum = 1;
$this->entityModel->expects($this->any())->method('getRowScope')->willReturn(1);
$this->entityModel->expects($this->once())->method('addRowError')
$this->entityModel->method('getRowScope')
->willReturn(1);
$this->entityModel->method('addRowError')
->with(
RowValidatorInterface::ERROR_VALUE_IS_REQUIRED,
1,
Expand Down
Loading

0 comments on commit aa272e6

Please sign in to comment.