diff --git a/app/code/Magento/Catalog/Model/Product/Price/BasePriceStorage.php b/app/code/Magento/Catalog/Model/Product/Price/BasePriceStorage.php index b9ffb332e1cdd93e8c1597880e9a90b66281b2ee..ec1aeee42d175e30a30a4c2099b11f0d69215f89 100644 --- a/app/code/Magento/Catalog/Model/Product/Price/BasePriceStorage.php +++ b/app/code/Magento/Catalog/Model/Product/Price/BasePriceStorage.php @@ -210,7 +210,11 @@ class BasePriceStorage implements \Magento\Catalog\Api\BasePriceStorageInterface } 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()] ); } } diff --git a/app/code/Magento/Catalog/Model/Product/Price/CostStorage.php b/app/code/Magento/Catalog/Model/Product/Price/CostStorage.php index d5f9d08e9115ddb72351cde957df3b8dce7bee57..6b2575d5eaf7faaed793b225ba4b6b8fd51e7574 100644 --- a/app/code/Magento/Catalog/Model/Product/Price/CostStorage.php +++ b/app/code/Magento/Catalog/Model/Product/Price/CostStorage.php @@ -190,10 +190,10 @@ class CostStorage implements \Magento\Catalog\Api\CostStorageInterface $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 { @@ -201,7 +201,11 @@ class CostStorage implements \Magento\Catalog\Api\CostStorageInterface } 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()] ); } } diff --git a/app/code/Magento/Catalog/Model/Product/Price/InvalidSkuChecker.php b/app/code/Magento/Catalog/Model/Product/Price/InvalidSkuChecker.php index 8eb7c1415c69367d984dfdb6a25d47091c626959..c6a34bb803980268c93625b3e1e627fbff46b849 100644 --- a/app/code/Magento/Catalog/Model/Product/Price/InvalidSkuChecker.php +++ b/app/code/Magento/Catalog/Model/Product/Price/InvalidSkuChecker.php @@ -34,7 +34,12 @@ class InvalidSkuChecker 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) { @@ -42,7 +47,7 @@ class InvalidSkuChecker if ($allowedPriceTypeValue && $type == \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE - && $this->productRepository->get($sku)->getPriceType() != $allowedProductTypes + && $this->productRepository->get($sku)->getPriceType() != $allowedPriceTypeValue ) { $valueTypeIsAllowed = true; } diff --git a/app/code/Magento/Catalog/Model/Product/Price/SpecialPriceStorage.php b/app/code/Magento/Catalog/Model/Product/Price/SpecialPriceStorage.php index d3ec22571094d203b4a4fbc9ac9c578214d2725d..59220aaf22a1de0407ed355033474a7579f1327f 100644 --- a/app/code/Magento/Catalog/Model/Product/Price/SpecialPriceStorage.php +++ b/app/code/Magento/Catalog/Model/Product/Price/SpecialPriceStorage.php @@ -143,21 +143,47 @@ class SpecialPriceStorage implements \Magento\Catalog\Api\SpecialPriceStorageInt $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() + ] ); } } @@ -172,21 +198,35 @@ class SpecialPriceStorage implements \Magento\Catalog\Api\SpecialPriceStorageInt /** * 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() + ] ); } } @@ -195,20 +235,33 @@ class SpecialPriceStorage implements \Magento\Catalog\Api\SpecialPriceStorageInt * 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() + ] ); } } @@ -216,14 +269,14 @@ class SpecialPriceStorage implements \Magento\Catalog\Api\SpecialPriceStorageInt /** * 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; } } diff --git a/app/code/Magento/Catalog/Model/Product/Price/TierPriceStorage.php b/app/code/Magento/Catalog/Model/Product/Price/TierPriceStorage.php index b8e78380bbc31bcb1f4c1cc525146f6e4314adef..8f2b16542b1ec6706ea5066408884459cd77bdab 100644 --- a/app/code/Magento/Catalog/Model/Product/Price/TierPriceStorage.php +++ b/app/code/Magento/Catalog/Model/Product/Price/TierPriceStorage.php @@ -259,7 +259,7 @@ class TierPriceStorage implements \Magento\Catalog\Api\TierPriceStorageInterface * * @param array $price * @param array $existingPrices - * @return int|void + * @return int|null */ private function retrievePriceId(array $price, array $existingPrices) { @@ -275,6 +275,8 @@ class TierPriceStorage implements \Magento\Catalog\Api\TierPriceStorageInterface return $existingPrice['value_id']; } } + + return null; } /** diff --git a/app/code/Magento/Catalog/Model/ProductIdLocator.php b/app/code/Magento/Catalog/Model/ProductIdLocator.php index 3cc6f931ec4cd6172f667469e23be42aa272394a..4a25d2603da38c165f0d9bec3a179999a29a16f5 100644 --- a/app/code/Magento/Catalog/Model/ProductIdLocator.php +++ b/app/code/Magento/Catalog/Model/ProductIdLocator.php @@ -95,6 +95,10 @@ class ProductIdLocator implements \Magento\Catalog\Model\ProductIdLocatorInterfa } } - return array_intersect_key($this->idsBySku, array_flip($skus)); + return array_intersect_ukey( + $this->idsBySku, + array_flip($skus), + 'strcasecmp' + ); } } diff --git a/app/code/Magento/Catalog/Test/Unit/Model/Product/Price/CostStorageTest.php b/app/code/Magento/Catalog/Test/Unit/Model/Product/Price/CostStorageTest.php index 78f6c6540b6cb3487711c2d70949b2039ead5b14..a7c82b553365d4c8290e8b459a05a130d9d886eb 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/Product/Price/CostStorageTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/Product/Price/CostStorageTest.php @@ -252,7 +252,7 @@ class CostStorageTest extends \PHPUnit_Framework_TestCase 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') @@ -267,7 +267,7 @@ class CostStorageTest extends \PHPUnit_Framework_TestCase ->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'); diff --git a/app/code/Magento/Catalog/Test/Unit/Model/Product/Price/Validation/TierPriceValidatorTest.php b/app/code/Magento/Catalog/Test/Unit/Model/Product/Price/Validation/TierPriceValidatorTest.php index 249a3642024592c67d16eb056a78b7efa41f918e..0505d7c1b8175e6ef476d8846be7743c1a3a1181 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/Product/Price/Validation/TierPriceValidatorTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/Product/Price/Validation/TierPriceValidatorTest.php @@ -6,14 +6,23 @@ namespace Magento\Catalog\Test\Unit\Model\Product\Price\Validation; -use Magento\Catalog\Api\Data\TierPriceInterface; - /** - * Class TierPriceValidatorTest. + * Test for \Magento\Catalog\Model\Product\Price\Validation\TierPriceValidator. + * * @SuppressWarnings(PHPMD.CouplingBetweenObjects) */ class TierPriceValidatorTest extends \PHPUnit_Framework_TestCase { + /** + * @var ObjectManagerHelper + */ + private $objectManagerHelper; + + /** + * @var \Magento\Catalog\Model\Product\Price\Validation\TierPriceValidator + */ + private $tierPriceValidator; + /** * @var \Magento\Catalog\Model\ProductIdLocatorInterface|\PHPUnit_Framework_MockObject_MockObject */ @@ -45,9 +54,9 @@ class TierPriceValidatorTest extends \PHPUnit_Framework_TestCase private $tierPricePersistence; /** - * @var \Magento\Catalog\Api\Data\TierPriceInterface|\PHPUnit_Framework_MockObject_MockObject + * @var \Magento\Catalog\Model\Product\Price\Validation\Result|\PHPUnit_Framework_MockObject_MockObject */ - private $tierPriceInterface; + private $validationResult; /** * @var \Magento\Catalog\Model\Product\Price\InvalidSkuChecker|\PHPUnit_Framework_MockObject_MockObject @@ -55,90 +64,52 @@ class TierPriceValidatorTest extends \PHPUnit_Framework_TestCase private $invalidSkuChecker; /** - * @var \Magento\Catalog\Model\Product\Price\Validation\Result|\PHPUnit_Framework_MockObject_MockObject - */ - private $validationResult; - - /** - * @var \Magento\Catalog\Model\Product\Price\Validation\TierPriceValidator + * @var \Magento\Catalog\Api\Data\TierPriceInterface|\PHPUnit_Framework_MockObject_MockObject */ - private $model; + private $tierPrice; /** - * Set up. - * - * @return void + * {@inheritdoc} */ protected function setUp() { - $this->productIdLocator = $this->getMockForAbstractClass( - \Magento\Catalog\Model\ProductIdLocatorInterface::class, - [], - '', - false, - true, - true, - ['retrieveProductIdsBySkus'] - ); - $this->searchCriteriaBuilder = $this->getMock( - \Magento\Framework\Api\SearchCriteriaBuilder::class, - ['addFilters', 'create'], - [], - '', - false - ); - $this->filterBuilder = $this->getMock( - \Magento\Framework\Api\FilterBuilder::class, - ['setField', 'setValue', 'create'], - [], - '', - false - ); - $this->filterBuilder->method('setField')->willReturnSelf(); - $this->filterBuilder->method('setValue')->willReturnSelf(); - $this->customerGroupRepository = $this->getMockForAbstractClass( - \Magento\Customer\Api\GroupRepositoryInterface::class, - [], - '', - false, - true, - true, - ['getList'] - ); - $this->websiteRepository = $this->getMockForAbstractClass( - \Magento\Store\Api\WebsiteRepositoryInterface::class, - [], - '', - false, - true, - true, - ['getById'] - ); - $this->tierPricePersistence = $this->getMock( - \Magento\Catalog\Model\Product\Price\TierPricePersistence::class, - ['addFilters', 'create'], - [], - '', - false - ); - $this->tierPriceInterface = $this->getMockForAbstractClass( - \Magento\Catalog\Api\Data\TierPriceInterface::class, - [], - '', - false, - true, - true, - ['getSku', 'getPrice', 'getPriceType', 'getQuantity', 'getWebsiteId', 'getCustomerGroup'] - ); + $this->productIdLocator = $this->getMockBuilder(\Magento\Catalog\Model\ProductIdLocatorInterface::class) + ->setMethods(['retrieveProductIdsBySkus']) + ->disableOriginalConstructor()->getMockForAbstractClass(); + + $this->searchCriteriaBuilder = $this->getMockBuilder(\Magento\Framework\Api\SearchCriteriaBuilder::class) + ->setMethods(['addFilters', 'create']) + ->disableOriginalConstructor()->getMock(); + + $this->filterBuilder = $this->getMockBuilder(\Magento\Framework\Api\FilterBuilder::class) + ->setMethods(['setField', 'setValue', 'create']) + ->disableOriginalConstructor()->getMock(); + + $this->customerGroupRepository = $this->getMockBuilder(\Magento\Customer\Api\GroupRepositoryInterface::class) + ->disableOriginalConstructor()->getMockForAbstractClass(); + + $this->websiteRepository = $this->getMockBuilder(\Magento\Store\Api\WebsiteRepositoryInterface::class) + ->setMethods(['getById']) + ->disableOriginalConstructor()->getMockForAbstractClass(); + + $this->tierPricePersistence = $this + ->getMockBuilder(\Magento\Catalog\Model\Product\Price\TierPricePersistence::class) + ->disableOriginalConstructor()->getMock(); + $this->validationResult = $this->getMockBuilder(\Magento\Catalog\Model\Product\Price\Validation\Result::class) - ->disableOriginalConstructor() - ->getMock(); + ->setMethods(['addFailedItem']) + ->disableOriginalConstructor()->getMock(); + $this->invalidSkuChecker = $this->getMockBuilder(\Magento\Catalog\Model\Product\Price\InvalidSkuChecker::class) - ->disableOriginalConstructor() - ->getMock(); + ->setMethods(['isSkuListValid', 'retrieveInvalidSkuList']) + ->disableOriginalConstructor()->getMock(); + + $this->tierPrice = $this->getMockBuilder(\Magento\Catalog\Api\Data\TierPriceInterface::class) + ->setMethods(['getSku', 'getPrice', 'getPriceType', 'getQuantity', 'getWebsiteId']) + ->disableOriginalConstructor()->getMockForAbstractClass(); - $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); - $this->model = $objectManager->getObject( + $this->objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); + $this->tierPriceValidator = $this->objectManagerHelper->getObject( \Magento\Catalog\Model\Product\Price\Validation\TierPriceValidator::class, [ 'productIdLocator' => $this->productIdLocator, @@ -148,186 +119,182 @@ class TierPriceValidatorTest extends \PHPUnit_Framework_TestCase 'websiteRepository' => $this->websiteRepository, 'tierPricePersistence' => $this->tierPricePersistence, 'validationResult' => $this->validationResult, - 'invalidSkuChecker' => $this->invalidSkuChecker, - 'allowedProductTypes' => ['simple', 'virtual', 'bundle', 'downloadable'], + 'invalidSkuChecker' => $this->invalidSkuChecker ] ); } /** - * Test retrieveValidPrices method. + * Prepare CustomerGroupRepository mock. * + * @param array $returned * @return void */ - public function testRetrieveValidPrices() + private function prepareCustomerGroupRepositoryMock(array $returned) { - $sku = 'sku_1'; - $idsBySku = [ - 'sku_1' => [1 => \Magento\Catalog\Model\Product\Type::TYPE_SIMPLE], - 'sku_2' => [2 => \Magento\Catalog\Model\Product\Type::TYPE_VIRTUAL], - ]; - $this->productIdLocator->expects($this->once())->method('retrieveProductIdsBySkus')->willReturn($idsBySku); - $productPrice = 15; - $this->tierPriceInterface->expects($this->exactly(10))->method('getSku')->willReturn($sku); - $this->invalidSkuChecker->expects($this->once())->method('retrieveInvalidSkuList')->willReturn([]); - $this->tierPriceInterface->expects($this->exactly(2))->method('getPrice')->willReturn($productPrice); - $this->tierPriceInterface - ->expects($this->exactly(2)) - ->method('getPriceType') - ->willReturn(TierPriceInterface::PRICE_TYPE_FIXED); - $this->tierPriceInterface->expects($this->exactly(3))->method('getQuantity')->willReturn(2); - $this->checkWebsite($this->tierPriceInterface); - $this->checkGroup($this->tierPriceInterface); - $this->model->retrieveValidationResult([$this->tierPriceInterface], []); + $searchCriteria = $this + ->getMockBuilder(\Magento\Framework\Api\Search\SearchCriteriaInterface::class) + ->disableOriginalConstructor()->getMock(); + + $filter = $this->getMockBuilder(\Magento\Framework\Api\AbstractSimpleObject::class) + ->disableOriginalConstructor()->getMockForAbstractClass(); + + $this->filterBuilder->expects($this->atLeastOnce())->method('setField')->willReturnSelf(); + $this->filterBuilder->expects($this->atLeastOnce())->method('setValue')->willReturnSelf(); + $this->filterBuilder->expects($this->atLeastOnce())->method('create')->willReturn($filter); + + $this->searchCriteriaBuilder->expects($this->atLeastOnce())->method('addFilters')->willReturnSelf(); + $this->searchCriteriaBuilder->expects($this->atLeastOnce())->method('create')->willReturn($searchCriteria); + + $customerGroupSearchResults = $this + ->getMockBuilder(\Magento\Customer\Api\Data\GroupSearchResultsInterface::class) + ->disableOriginalConstructor()->getMock(); + $customerGroupSearchResults->expects($this->once())->method('getItems') + ->willReturn($returned['customerGroupSearchResults_getItems']); + + $this->customerGroupRepository->expects($this->atLeastOnce())->method('getList') + ->willReturn($customerGroupSearchResults); } /** - * Test retrieveValidPrices method with downloadable product. + * Prepare retrieveValidationResult(). + * + * @param string $sku + * @param array $returned + * @return void */ - public function testRetrieveValidPricesWithDownloadableProduct() + private function prepareRetrieveValidationResultMethod($sku, array $returned) { + $this->tierPrice->expects($this->atLeastOnce())->method('getSku')->willReturn($sku); + $tierPriceValue = 104; + $this->tierPrice->expects($this->atLeastOnce())->method('getPrice')->willReturn($tierPriceValue); + $this->tierPrice->expects($this->atLeastOnce())->method('getPriceType') + ->willReturn($returned['tierPrice_getPriceType']); + $qty = 0; + $this->tierPrice->expects($this->atLeastOnce())->method('getQuantity')->willReturn($qty); + $websiteId = 0; + $invalidWebsiteId = 4; + $this->tierPrice->expects($this->atLeastOnce())->method('getWebsiteId') + ->willReturnOnConsecutiveCalls($websiteId, $websiteId, $websiteId, $invalidWebsiteId, $websiteId); + $this->tierPrice->expects($this->atLeastOnce())->method('getCustomerGroup') + ->willReturn($returned['tierPrice_getCustomerGroup']); + + $skuDiff = [$sku]; + $this->invalidSkuChecker->expects($this->atLeastOnce())->method('retrieveInvalidSkuList')->willReturn($skuDiff); + + $productId = 3346346; + $productType = \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE; $idsBySku = [ - 'sku_1' => [1 => \Magento\Catalog\Model\Product\Type::TYPE_SIMPLE], - 'sku_2' => [2 => \Magento\Catalog\Model\Product\Type::TYPE_VIRTUAL], + $sku => [$productId => $productType] ]; - $this->productIdLocator->expects($this->once())->method('retrieveProductIdsBySkus')->willReturn($idsBySku); - $this->tierPriceInterface->expects($this->exactly(10))->method('getSku')->willReturn('sku_1'); - $this->invalidSkuChecker->expects($this->once())->method('retrieveInvalidSkuList')->willReturn([]); - $productPrice = 15; - $this->tierPriceInterface->expects($this->exactly(2))->method('getPrice')->willReturn($productPrice); - $this->tierPriceInterface - ->expects($this->exactly(2)) - ->method('getPriceType') - ->willReturn(TierPriceInterface::PRICE_TYPE_FIXED); - $this->tierPriceInterface->expects($this->exactly(3))->method('getQuantity')->willReturn(2); - $this->checkWebsite($this->tierPriceInterface); - $this->checkGroup($this->tierPriceInterface); - $this->validationResult - ->expects($this->never()) - ->method('addFailedItem'); - $this->model->retrieveValidationResult([$this->tierPriceInterface], []); + $this->productIdLocator->expects($this->atLeastOnce())->method('retrieveProductIdsBySkus') + ->willReturn($idsBySku); } /** - * Test method call with invalid values. + * Test for validateSkus(). + * + * @return void */ - public function testRetrieveValidPricesWithInvalidCall() + public function testValidateSkus() { - $idsBySku = [ - 'sku_1' => [1 => \Magento\Catalog\Model\Product\Type::TYPE_SIMPLE], - 'sku_2' => [2 => \Magento\Catalog\Model\Product\Type::TYPE_VIRTUAL], - 'invalid' => [3 => \Magento\Catalog\Model\Product\Type::TYPE_SIMPLE], - ]; + $skus = ['SDFS234234']; - $this->tierPriceInterface->expects($this->exactly(10))->method('getSku')->willReturn('sku_1'); - $this->invalidSkuChecker->expects($this->once())->method('retrieveInvalidSkuList')->willReturn(['invalid']); - $this->productIdLocator->expects($this->once())->method('retrieveProductIdsBySkus')->willReturn($idsBySku); - $this->validationResult - ->expects($this->exactly(5)) - ->method('addFailedItem'); - $this->tierPriceInterface->expects($this->exactly(3))->method('getPrice')->willReturn('-90'); - $this->tierPriceInterface->expects($this->exactly(2))->method('getPriceType')->willReturn('unknown'); - $this->tierPriceInterface->expects($this->exactly(4))->method('getQuantity')->willReturn('-90'); - $this->websiteRepository->method('getById') - ->willThrowException(new \Magento\Framework\Exception\NoSuchEntityException()); - $searchCriteria = $this->getMockForAbstractClass( - \Magento\Framework\Api\SearchCriteriaInterface::class, - [], - '', - false, - true, - true, - ['create'] - ); - $searchCriteria->method('create')->willReturnSelf(); - $this->searchCriteriaBuilder->method('addFilters')->willReturn($searchCriteria); - $this->searchCriteriaBuilder->expects($this->once())->method('addFilters')->willReturnSelf(); - $this->filterBuilder->expects($this->once())->method('setField')->with('customer_group_code')->willReturnSelf(); - $this->filterBuilder->expects($this->once())->method('setValue')->willReturnSelf(); - $searchResults = $this->getMockForAbstractClass( - \Magento\Customer\Api\Data\GroupSearchResultsInterface::class, - [], - '', - false, - true, - true, - ['getItems'] - ); - $this->filterBuilder->expects($this->atLeastOnce())->method('create')->willReturnSelf(); - $searchResults->expects($this->atLeastOnce())->method('getItems')->willReturn([]); - $this->customerGroupRepository - ->expects($this->atLeastOnce()) - ->method('getList') - ->willReturn($searchResults); - $this->model->retrieveValidationResult([$this->tierPriceInterface], []); + $this->invalidSkuChecker->expects($this->atLeastOnce())->method('isSkuListValid'); + + $expected = null; + $this->assertEquals($expected, $this->tierPriceValidator->validateSkus($skus)); } /** - * Check website. + * Test for retrieveValidationResult(). * - * @param \PHPUnit_Framework_MockObject_MockObject $price + * @param array $returned + * @dataProvider retrieveValidationResultDataProvider + * @return void */ - private function checkWebsite(\PHPUnit_Framework_MockObject_MockObject $price) + public function testRetrieveValidationResult(array $returned) { - $website = $this->getMockForAbstractClass( - \Magento\Store\Api\Data\WebsiteInterface::class, - [], - '', - false - ); - $price->expects($this->exactly(3))->method('getWebsiteId')->willReturn(1); - $this->websiteRepository->expects($this->once())->method('getById')->willReturn($website); + $sku = 'ASDF234234'; + + $prices = [$this->tierPrice]; + $existingPrices = [$this->tierPrice]; + + $this->prepareRetrieveValidationResultMethod($sku, $returned); + + $website = $this->getMockBuilder(\Magento\Store\Api\Data\WebsiteInterface::class) + ->disableOriginalConstructor()->getMockForAbstractClass(); + $this->websiteRepository->expects($this->atLeastOnce())->method('getById')->willReturn($website); + + $this->prepareCustomerGroupRepositoryMock($returned); + + $expects = $this->validationResult; + $this->assertEquals($expects, $this->tierPriceValidator->retrieveValidationResult($prices, $existingPrices)); } /** - * Check group. + * Data provider for retrieveValidationResult() test. * - * @param \PHPUnit_Framework_MockObject_MockObject $price + * @return array */ - private function checkGroup(\PHPUnit_Framework_MockObject_MockObject $price) + public function retrieveValidationResultDataProvider() { - $searchCriteria = $this->getMock( - \Magento\Framework\Api\SearchCriteria::class, - [], - [], - '', - false - ); - $searchResults = $this->getMockForAbstractClass( - \Magento\Customer\Api\Data\GroupSearchResultsInterface::class, - [], - '', - false, - true, - true, - ['getItems'] - ); - $group = $this->getMockForAbstractClass( - \Magento\Customer\Api\Data\GroupInterface::class, - [], - '', - false, - true, - true, - ['getCode', 'getId'] - ); + $customerGroupName = 'test_Group'; + + $customerGroup = $this->getMockBuilder(\Magento\Customer\Api\Data\GroupInterface::class) + ->setMethods(['getCode', 'getId']) + ->disableOriginalConstructor()->getMockForAbstractClass(); + + $customerGroup->expects($this->atLeastOnce())->method('getCode')->willReturn($customerGroupName); + $customerGroupId = 23; + $customerGroup->expects($this->atLeastOnce())->method('getId')->willReturn($customerGroupId); + + return [ + [ + [ + 'tierPrice_getCustomerGroup' => $customerGroupName, + 'tierPrice_getPriceType' => \Magento\Catalog\Api\Data\TierPriceInterface::PRICE_TYPE_DISCOUNT, + 'customerGroupSearchResults_getItems' => [$customerGroup] + ] + ], + [ + [ + 'tierPrice_getCustomerGroup' => $customerGroupName, + 'tierPrice_getPriceType' => \Magento\Catalog\Api\Data\TierPriceInterface::PRICE_TYPE_FIXED, + 'customerGroupSearchResults_getItems' => [] + ] + ] + ]; + } + + /** + * Test for retrieveValidationResult() with Exception. + * + * @return void + */ + public function testRetrieveValidationResultWithException() + { + $sku = 'ASDF234234'; + $customerGroupName = 'test_Group'; + + $prices = [$this->tierPrice]; + $existingPrices = [$this->tierPrice]; + + $returned = [ + 'tierPrice_getPriceType' => \Magento\Catalog\Api\Data\TierPriceInterface::PRICE_TYPE_DISCOUNT, + 'customerGroupSearchResults_getItems' => [], + 'tierPrice_getCustomerGroup' => $customerGroupName, + ]; + + $this->prepareRetrieveValidationResultMethod($sku, $returned); + + $exception = new \Magento\Framework\Exception\NoSuchEntityException(); + + $this->websiteRepository->expects($this->atLeastOnce())->method('getById')->willThrowException($exception); + + $this->prepareCustomerGroupRepositoryMock($returned); - $price->expects($this->exactly(3))->method('getCustomerGroup')->willReturn('wholesale'); - $this->searchCriteriaBuilder->expects($this->once())->method('addFilters')->willReturnSelf(); - $this->filterBuilder->expects($this->once())->method('setField')->with('customer_group_code')->willReturnSelf(); - $this->filterBuilder->expects($this->once())->method('setValue')->with('wholesale')->willReturnSelf(); - $this->filterBuilder->expects($this->once())->method('create')->willReturnSelf(); - $this->searchCriteriaBuilder - ->expects($this->once()) - ->method('create') - ->willReturn($searchCriteria); - $this->customerGroupRepository - ->expects($this->once()) - ->method('getList') - ->with($searchCriteria) - ->willReturn($searchResults); - $searchResults->expects($this->once())->method('getItems')->willReturn([$group]); - $group->expects($this->once())->method('getCode')->willReturn('wholesale'); - $group->expects($this->once())->method('getId')->willReturn(4); + $expects = $this->validationResult; + $this->assertEquals($expects, $this->tierPriceValidator->retrieveValidationResult($prices, $existingPrices)); } } diff --git a/dev/tests/api-functional/testsuite/Magento/Catalog/Api/BasePriceStorageTest.php b/dev/tests/api-functional/testsuite/Magento/Catalog/Api/BasePriceStorageTest.php index 3da02007eb5082fa8d056486670a8ec077554645..73500ff5ae24cfa395ffb2c10461a53737217ce2 100644 --- a/dev/tests/api-functional/testsuite/Magento/Catalog/Api/BasePriceStorageTest.php +++ b/dev/tests/api-functional/testsuite/Magento/Catalog/Api/BasePriceStorageTest.php @@ -180,8 +180,12 @@ class BasePriceStorageTest extends WebapiAbstract ] ], 2 => [ - 'message' => 'Requested store is not found.', - 'parameters' => [] + 'message' => + 'Requested store is not found. Row ID: SKU = not_existing_sku, Store ID: 9999.', + 'parameters' => [ + 'not_existing_sku', + '9999' + ] ] ]; diff --git a/dev/tests/api-functional/testsuite/Magento/Catalog/Api/CostStorageTest.php b/dev/tests/api-functional/testsuite/Magento/Catalog/Api/CostStorageTest.php index ff6a4f5283ff2a5f23d48da8f813df24342cc76f..f04097a17a0762c4f47ae1b1ebded3e512028e75 100644 --- a/dev/tests/api-functional/testsuite/Magento/Catalog/Api/CostStorageTest.php +++ b/dev/tests/api-functional/testsuite/Magento/Catalog/Api/CostStorageTest.php @@ -174,15 +174,19 @@ class CostStorageTest extends WebapiAbstract ] ], 1 => [ - 'message' => 'Invalid attribute %fieldName = %fieldValue.', + 'message' => 'Invalid attribute Cost = -9999. Row ID: SKU = not_existing_sku, Store ID: 9999.', 'parameters' => [ - 'Cost', '-9999', + 'not_existing_sku', + '9999' ] ], 2 => [ - 'message' => 'Requested store is not found.', - 'parameters' => [] + 'message' => 'Requested store is not found. Row ID: SKU = not_existing_sku, Store ID: 9999.', + 'parameters' => [ + 'not_existing_sku', + '9999' + ] ] ];