Skip to content

Commit

Permalink
Merge pull request #731 from magento-nord/SPRINT-27-NORD
Browse files Browse the repository at this point in the history
- MAGETWO-63042: Make price error aggregator messages more detailed
  • Loading branch information
balex13 authored Jan 11, 2017
2 parents bbb180f + f5e521b commit cac91b5
Show file tree
Hide file tree
Showing 10 changed files with 311 additions and 264 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,11 @@ private function retrieveValidPrices(array $prices)
} catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
$this->validationResult->addFailedItem(
$id,
__('Requested store is not found.')
__(
'Requested store is not found. Row ID: SKU = %SKU, Store ID: %storeId.',
['SKU' => $price->getSku(), 'storeId' => $price->getStoreId()]
),
['SKU' => $price->getSku(), 'storeId' => $price->getStoreId()]
);
}
}
Expand Down
12 changes: 8 additions & 4 deletions app/code/Magento/Catalog/Model/Product/Price/CostStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,18 +190,22 @@ private function retrieveValidPrices(array $prices)
$this->validationResult->addFailedItem(
$id,
__(
'Invalid attribute %fieldName = %fieldValue.',
['fieldName' => '%fieldName', 'fieldValue' => '%fieldValue']
'Invalid attribute Cost = %cost. Row ID: SKU = %SKU, Store ID: %storeId.',
['cost' => $price->getCost(), 'SKU' => $price->getSku(), 'storeId' => $price->getStoreId()]
),
['fieldName' => 'Cost', 'fieldValue' => $price->getCost()]
['cost' => $price->getCost(), 'SKU' => $price->getSku(), 'storeId' => $price->getStoreId()]
);
}
try {
$this->storeRepository->getById($price->getStoreId());
} catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
$this->validationResult->addFailedItem(
$id,
__('Requested store is not found.')
__(
'Requested store is not found. Row ID: SKU = %SKU, Store ID: %storeId.',
['SKU' => $price->getSku(), 'storeId' => $price->getStoreId()]
),
['SKU' => $price->getSku(), 'storeId' => $price->getStoreId()]
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,20 @@ public function __construct(
public function retrieveInvalidSkuList(array $skus, array $allowedProductTypes, $allowedPriceTypeValue = false)
{
$idsBySku = $this->productIdLocator->retrieveProductIdsBySkus($skus);
$skuDiff = array_diff($skus, array_keys($idsBySku));
$existingSkus = array_keys($idsBySku);
$skuDiff = array_udiff(
$skus,
$existingSkus,
'strcasecmp'
);

foreach ($idsBySku as $sku => $ids) {
foreach ($ids as $type) {
$valueTypeIsAllowed = false;

if ($allowedPriceTypeValue
&& $type == \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE
&& $this->productRepository->get($sku)->getPriceType() != $allowedProductTypes
&& $this->productRepository->get($sku)->getPriceType() != $allowedPriceTypeValue
) {
$valueTypeIsAllowed = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,21 +143,47 @@ private function retrieveValidPrices(array $prices)
$this->validationResult->addFailedItem(
$key,
__(
'Requested product doesn\'t exist: %sku',
['sku' => '%sku']
'Requested product doesn\'t exist. '
. 'Row ID: SKU = %SKU, Store ID: %storeId, Price From: %priceFrom, Price To: %priceTo.',
[
'SKU' => $price->getSku(),
'storeId' => $price->getStoreId(),
'priceFrom' => $price->getPriceFrom(),
'priceTo' => $price->getPriceTo()
]
),
['sku' => $price->getSku()]
[
'SKU' => $price->getSku(),
'storeId' => $price->getStoreId(),
'priceFrom' => $price->getPriceFrom(),
'priceTo' => $price->getPriceTo()
]
);
}
$this->checkPrice($price->getPrice(), $key);
$this->checkDate($price->getPriceFrom(), 'Price From', $key);
$this->checkDate($price->getPriceTo(), 'Price To', $key);
$this->checkPrice($price, $key);
$this->checkDate($price, $price->getPriceFrom(), 'Price From', $key);
$this->checkDate($price, $price->getPriceTo(), 'Price To', $key);
try {
$this->storeRepository->getById($price->getStoreId());
} catch (NoSuchEntityException $e) {
$this->validationResult->addFailedItem(
$key,
__('Requested store is not found.')
__(
'Requested store is not found. '
. 'Row ID: SKU = %SKU, Store ID: %storeId, Price From: %priceFrom, Price To: %priceTo.',
[
'SKU' => $price->getSku(),
'storeId' => $price->getStoreId(),
'priceFrom' => $price->getPriceFrom(),
'priceTo' => $price->getPriceTo()
]
),
[
'SKU' => $price->getSku(),
'storeId' => $price->getStoreId(),
'priceFrom' => $price->getPriceFrom(),
'priceTo' => $price->getPriceTo()
]
);
}
}
Expand All @@ -172,21 +198,35 @@ private function retrieveValidPrices(array $prices)
/**
* Check that date value is correct and add error to aggregator if it contains incorrect data.
*
* @param \Magento\Catalog\Api\Data\SpecialPriceInterface $price
* @param string $value
* @param string $label
* @param int $key
* @return void
*/
private function checkDate($value, $label, $key)
private function checkDate(\Magento\Catalog\Api\Data\SpecialPriceInterface $price, $value, $label, $key)
{
if ($value && !$this->isCorrectDateValue($value)) {
$this->validationResult->addFailedItem(
$key,
__(
'Invalid attribute %fieldName = %fieldValue.',
['fieldName' => '%fieldName', 'fieldValue' => '%fieldValue']
'Invalid attribute %label = %priceTo. '
. 'Row ID: SKU = %SKU, Store ID: %storeId, Price From: %priceFrom, Price To: %priceTo.',
[
'label' => $label,
'SKU' => $price->getSku(),
'storeId' => $price->getStoreId(),
'priceFrom' => $price->getPriceFrom(),
'priceTo' => $price->getPriceTo()
]
),
['fieldName' => $label, 'fieldValue' => $value]
[
'label' => $label,
'SKU' => $price->getSku(),
'storeId' => $price->getStoreId(),
'priceFrom' => $price->getPriceFrom(),
'priceTo' => $price->getPriceTo()
]
);
}
}
Expand All @@ -195,35 +235,48 @@ private function checkDate($value, $label, $key)
* Check that provided price value is not empty and not lower then zero and add error to aggregator if price
* contains not valid data.
*
* @param float $price
* @param \Magento\Catalog\Api\Data\SpecialPriceInterface $price
* @param int $key
* @return void
*/
private function checkPrice($price, $key)
private function checkPrice(\Magento\Catalog\Api\Data\SpecialPriceInterface $price, $key)
{
if (null === $price || $price < 0) {
if (null === $price->getPrice() || $price->getPrice() < 0) {
$this->validationResult->addFailedItem(
$key,
__(
'Invalid attribute %fieldName = %fieldValue.',
['fieldName' => '%fieldName', 'fieldValue' => '%fieldValue']
'Invalid attribute Price = %price. '
. 'Row ID: SKU = %SKU, Store ID: %storeId, Price From: %priceFrom, Price To: %priceTo.',
[
'price' => $price->getPrice(),
'SKU' => $price->getSku(),
'storeId' => $price->getStoreId(),
'priceFrom' => $price->getPriceFrom(),
'priceTo' => $price->getPriceTo()
]
),
['fieldName' => 'Price', 'fieldValue' => $price]
[
'price' => $price->getPrice(),
'SKU' => $price->getSku(),
'storeId' => $price->getStoreId(),
'priceFrom' => $price->getPriceFrom(),
'priceTo' => $price->getPriceTo()
]
);
}
}

/**
* Retrieve SKU by product ID.
*
* @param int $id
* @param int $productId
* @param array $skus
* @return int|null
* @return string|null
*/
private function retrieveSkuById($id, array $skus)
private function retrieveSkuById($productId, array $skus)
{
foreach ($this->productIdLocator->retrieveProductIdsBySkus($skus) as $sku => $ids) {
if (false !== array_key_exists($id, $ids)) {
if (isset($ids[$productId])) {
return $sku;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ private function retrieveAffectedPriceIds(array $prices)
*
* @param array $price
* @param array $existingPrices
* @return int|void
* @return int|null
*/
private function retrievePriceId(array $price, array $existingPrices)
{
Expand All @@ -275,6 +275,8 @@ private function retrievePriceId(array $price, array $existingPrices)
return $existingPrice['value_id'];
}
}

return null;
}

/**
Expand Down
6 changes: 5 additions & 1 deletion app/code/Magento/Catalog/Model/ProductIdLocator.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ public function retrieveProductIdsBySkus(array $skus)
}
}

return array_intersect_key($this->idsBySku, array_flip($skus));
return array_intersect_ukey(
$this->idsBySku,
array_flip($skus),
'strcasecmp'
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ public function testUpdate()
public function testUpdateWithNegativeCost()
{
$sku = 'sku_1';
$this->costInterface->expects($this->exactly(3))->method('getSku')->willReturn($sku);
$this->costInterface->expects($this->exactly(5))->method('getSku')->willReturn($sku);
$this->invalidSkuChecker
->expects($this->exactly(1))
->method('retrieveInvalidSkuList')
Expand All @@ -267,7 +267,7 @@ public function testUpdateWithNegativeCost()
->with(['attributeCode' => 'cost'])
->willReturn($this->pricePersistence);
$this->pricePersistence->expects($this->atLeastOnce())->method('update');
$this->costInterface->expects($this->exactly(3))->method('getCost')->willReturn(-15);
$this->costInterface->expects($this->exactly(4))->method('getCost')->willReturn(-15);
$this->validationResult
->expects($this->atLeastOnce())
->method('getFailedItems');
Expand Down
Loading

0 comments on commit cac91b5

Please sign in to comment.