From 764284f9dd41b00f18401fd4c701366a7d4c9a65 Mon Sep 17 00:00:00 2001 From: "Karpenko, Oleksandr" Date: Wed, 19 Apr 2017 13:42:44 +0300 Subject: [PATCH] MAGETWO-63667: Can't get store-specific data via catalog API --- .../FilterProcessor/ProductStoreFilter.php | 28 +++++++ app/code/Magento/Catalog/etc/di.xml | 1 + .../Api/ProductRepositoryInterfaceTest.php | 79 +++++++++++++++++++ .../products_with_websites_and_stores.php | 41 ++++++++++ ...ucts_with_websites_and_stores_rollback.php | 31 ++++++++ 5 files changed, 180 insertions(+) create mode 100644 app/code/Magento/Catalog/Model/Api/SearchCriteria/CollectionProcessor/FilterProcessor/ProductStoreFilter.php create mode 100644 dev/tests/integration/testsuite/Magento/Catalog/_files/products_with_websites_and_stores.php create mode 100644 dev/tests/integration/testsuite/Magento/Catalog/_files/products_with_websites_and_stores_rollback.php diff --git a/app/code/Magento/Catalog/Model/Api/SearchCriteria/CollectionProcessor/FilterProcessor/ProductStoreFilter.php b/app/code/Magento/Catalog/Model/Api/SearchCriteria/CollectionProcessor/FilterProcessor/ProductStoreFilter.php new file mode 100644 index 0000000000000..7465ced6ab2d8 --- /dev/null +++ b/app/code/Magento/Catalog/Model/Api/SearchCriteria/CollectionProcessor/FilterProcessor/ProductStoreFilter.php @@ -0,0 +1,28 @@ +addStoreFilter($filter->getValue()); + return true; + } +} diff --git a/app/code/Magento/Catalog/etc/di.xml b/app/code/Magento/Catalog/etc/di.xml index ca0403121cb1d..9e897ec098ada 100644 --- a/app/code/Magento/Catalog/etc/di.xml +++ b/app/code/Magento/Catalog/etc/di.xml @@ -835,6 +835,7 @@ Magento\Catalog\Model\Api\SearchCriteria\CollectionProcessor\FilterProcessor\ProductCategoryFilter + Magento\Catalog\Model\Api\SearchCriteria\CollectionProcessor\FilterProcessor\ProductStoreFilter diff --git a/dev/tests/api-functional/testsuite/Magento/Catalog/Api/ProductRepositoryInterfaceTest.php b/dev/tests/api-functional/testsuite/Magento/Catalog/Api/ProductRepositoryInterfaceTest.php index de03082189256..9106938ee690a 100644 --- a/dev/tests/api-functional/testsuite/Magento/Catalog/Api/ProductRepositoryInterfaceTest.php +++ b/dev/tests/api-functional/testsuite/Magento/Catalog/Api/ProductRepositoryInterfaceTest.php @@ -710,6 +710,85 @@ public function testGetList() $this->assertEquals($expectedResult, $response['items'][0]['custom_attributes'][$index]['value']); } + /** + * @magentoApiDataFixture Magento/Catalog/_files/products_with_websites_and_stores.php + * @dataProvider testGetListWithFilteringByStoreDataProvider + * + * @param array $searchCriteria + * @param array $skus + * @param int $expectedProductCount + * @return void + */ + public function testGetListWithFilteringByStore(array $searchCriteria, array $skus, $expectedProductCount = null) + { + $serviceInfo = [ + 'rest' => [ + 'resourcePath' => self::RESOURCE_PATH . '?' . http_build_query($searchCriteria), + 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET, + ], + 'soap' => [ + 'service' => self::SERVICE_NAME, + 'serviceVersion' => self::SERVICE_VERSION, + 'operation' => self::SERVICE_NAME . 'GetList', + ], + ]; + $response = $this->_webApiCall($serviceInfo, $searchCriteria); + + $this->assertArrayHasKey('search_criteria', $response); + $this->assertArrayHasKey('total_count', $response); + $this->assertArrayHasKey('items', $response); + if ($expectedProductCount) { + $this->assertTrue(count($response['items']) == $expectedProductCount); + } + + $isResultValid = false; + foreach ($skus as $sku){ + foreach ($response['items'] as $item) { + if ($item['sku'] == $sku) { + $isResultValid = true; + } + } + $this->assertTrue($isResultValid); + } + } + + public function testGetListWithFilteringByStoreDataProvider() + { + return [ + [ + [ + 'searchCriteria' => [ + 'filter_groups' => [ + [ + 'filters' => [ + [ + 'field' => 'store', + 'value' => 'fixture_second_store', + 'condition_type' => 'eq', + ], + ], + ], + ], + 'current_page' => 1, + 'page_size' => 10, + ], + ], + ['simple-2'], + 1, + ], + [ + [ + 'searchCriteria' => [ + 'current_page' => 1, + 'page_size' => 10, + ], + ], + ['simple-2', 'simple-1'], + null + ] + ]; + } + /** * @magentoApiDataFixture Magento/Catalog/_files/products_for_search.php */ diff --git a/dev/tests/integration/testsuite/Magento/Catalog/_files/products_with_websites_and_stores.php b/dev/tests/integration/testsuite/Magento/Catalog/_files/products_with_websites_and_stores.php new file mode 100644 index 0000000000000..9161221633b66 --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/Catalog/_files/products_with_websites_and_stores.php @@ -0,0 +1,41 @@ +create(\Magento\Store\Model\Website::class); +/** @var $website \Magento\Store\Model\Website */ +$websiteId = $website->load('test', 'code')->getId(); + +/** @var $product \Magento\Catalog\Model\Product */ +$product = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(\Magento\Catalog\Model\Product::class); +$product->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE) + ->setAttributeSetId(4) + ->setWebsiteIds([$websiteId]) + ->setName('Simple Product on second website') + ->setSku('simple-2') + ->setPrice(10) + ->setDescription('Description with html tag') + ->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH) + ->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED) + ->setCategoryIds([2]) + ->setStockData(['use_config_manage_stock' => 1, 'qty' => 100, 'is_qty_decimal' => 0, 'is_in_stock' => 1]) + ->save(); + +/** @var $product \Magento\Catalog\Model\Product */ +$product = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(\Magento\Catalog\Model\Product::class); +$product->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE) + ->setAttributeSetId(4) + ->setWebsiteIds([1]) + ->setName('Simple Product') + ->setSku('simple-1') + ->setPrice(10) + ->setDescription('Description with html tag') + ->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH) + ->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED) + ->setCategoryIds([2]) + ->setStockData(['use_config_manage_stock' => 1, 'qty' => 100, 'is_qty_decimal' => 0, 'is_in_stock' => 1]) + ->save(); diff --git a/dev/tests/integration/testsuite/Magento/Catalog/_files/products_with_websites_and_stores_rollback.php b/dev/tests/integration/testsuite/Magento/Catalog/_files/products_with_websites_and_stores_rollback.php new file mode 100644 index 0000000000000..ae070745318cc --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/Catalog/_files/products_with_websites_and_stores_rollback.php @@ -0,0 +1,31 @@ +create(\Magento\Catalog\Api\ProductRepositoryInterface::class); + +/** @var \Magento\Framework\Registry $registry */ +$registry = $objectManager->get(\Magento\Framework\Registry::class); + +$registry->unregister('isSecureArea'); +$registry->register('isSecureArea', true); + +/** @var \Magento\Catalog\Api\ProductRepositoryInterface $productRepository */ +$productRepository = $objectManager->create(\Magento\Catalog\Api\ProductRepositoryInterface::class); + +try { + foreach (['simple-2', 'simple-1'] as $sku) { + $product = $productRepository->get($sku, false, null, true); + $productRepository->delete($product); + } +} catch (\Magento\Framework\Exception\NoSuchEntityException $exception) { + //Product already removed +} + +$registry->unregister('isSecureArea'); +$registry->register('isSecureArea', false);