Skip to content

Commit 764284f

Browse files
author
Karpenko, Oleksandr
committed
MAGETWO-63667: Can't get store-specific data via catalog API
1 parent 9224419 commit 764284f

File tree

5 files changed

+180
-0
lines changed

5 files changed

+180
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Catalog\Model\Api\SearchCriteria\CollectionProcessor\FilterProcessor;
7+
8+
use Magento\Catalog\Model\ResourceModel\Product\Collection;
9+
use Magento\Framework\Api\Filter;
10+
use Magento\Framework\Api\SearchCriteria\CollectionProcessor\FilterProcessor\CustomFilterInterface;
11+
use Magento\Framework\Data\Collection\AbstractDb;
12+
13+
class ProductStoreFilter implements CustomFilterInterface
14+
{
15+
/**
16+
* Apply store Filter to Product Collection
17+
*
18+
* @param Filter $filter
19+
* @param AbstractDb $collection
20+
* @return bool Whether the filter is applied
21+
*/
22+
public function apply(Filter $filter, AbstractDb $collection)
23+
{
24+
/** @var Collection $collection */
25+
$collection->addStoreFilter($filter->getValue());
26+
return true;
27+
}
28+
}

app/code/Magento/Catalog/etc/di.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -835,6 +835,7 @@
835835
<arguments>
836836
<argument name="customFilters" xsi:type="array">
837837
<item name="category_id" xsi:type="object">Magento\Catalog\Model\Api\SearchCriteria\CollectionProcessor\FilterProcessor\ProductCategoryFilter</item>
838+
<item name="store" xsi:type="object">Magento\Catalog\Model\Api\SearchCriteria\CollectionProcessor\FilterProcessor\ProductStoreFilter</item>
838839
</argument>
839840
</arguments>
840841
</virtualType>

dev/tests/api-functional/testsuite/Magento/Catalog/Api/ProductRepositoryInterfaceTest.php

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,85 @@ public function testGetList()
710710
$this->assertEquals($expectedResult, $response['items'][0]['custom_attributes'][$index]['value']);
711711
}
712712

713+
/**
714+
* @magentoApiDataFixture Magento/Catalog/_files/products_with_websites_and_stores.php
715+
* @dataProvider testGetListWithFilteringByStoreDataProvider
716+
*
717+
* @param array $searchCriteria
718+
* @param array $skus
719+
* @param int $expectedProductCount
720+
* @return void
721+
*/
722+
public function testGetListWithFilteringByStore(array $searchCriteria, array $skus, $expectedProductCount = null)
723+
{
724+
$serviceInfo = [
725+
'rest' => [
726+
'resourcePath' => self::RESOURCE_PATH . '?' . http_build_query($searchCriteria),
727+
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
728+
],
729+
'soap' => [
730+
'service' => self::SERVICE_NAME,
731+
'serviceVersion' => self::SERVICE_VERSION,
732+
'operation' => self::SERVICE_NAME . 'GetList',
733+
],
734+
];
735+
$response = $this->_webApiCall($serviceInfo, $searchCriteria);
736+
737+
$this->assertArrayHasKey('search_criteria', $response);
738+
$this->assertArrayHasKey('total_count', $response);
739+
$this->assertArrayHasKey('items', $response);
740+
if ($expectedProductCount) {
741+
$this->assertTrue(count($response['items']) == $expectedProductCount);
742+
}
743+
744+
$isResultValid = false;
745+
foreach ($skus as $sku){
746+
foreach ($response['items'] as $item) {
747+
if ($item['sku'] == $sku) {
748+
$isResultValid = true;
749+
}
750+
}
751+
$this->assertTrue($isResultValid);
752+
}
753+
}
754+
755+
public function testGetListWithFilteringByStoreDataProvider()
756+
{
757+
return [
758+
[
759+
[
760+
'searchCriteria' => [
761+
'filter_groups' => [
762+
[
763+
'filters' => [
764+
[
765+
'field' => 'store',
766+
'value' => 'fixture_second_store',
767+
'condition_type' => 'eq',
768+
],
769+
],
770+
],
771+
],
772+
'current_page' => 1,
773+
'page_size' => 10,
774+
],
775+
],
776+
['simple-2'],
777+
1,
778+
],
779+
[
780+
[
781+
'searchCriteria' => [
782+
'current_page' => 1,
783+
'page_size' => 10,
784+
],
785+
],
786+
['simple-2', 'simple-1'],
787+
null
788+
]
789+
];
790+
}
791+
713792
/**
714793
* @magentoApiDataFixture Magento/Catalog/_files/products_for_search.php
715794
*/
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
require __DIR__ . '/../../Store/_files/second_website_with_two_stores.php';
8+
9+
$website = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(\Magento\Store\Model\Website::class);
10+
/** @var $website \Magento\Store\Model\Website */
11+
$websiteId = $website->load('test', 'code')->getId();
12+
13+
/** @var $product \Magento\Catalog\Model\Product */
14+
$product = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(\Magento\Catalog\Model\Product::class);
15+
$product->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE)
16+
->setAttributeSetId(4)
17+
->setWebsiteIds([$websiteId])
18+
->setName('Simple Product on second website')
19+
->setSku('simple-2')
20+
->setPrice(10)
21+
->setDescription('Description with <b>html tag</b>')
22+
->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH)
23+
->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)
24+
->setCategoryIds([2])
25+
->setStockData(['use_config_manage_stock' => 1, 'qty' => 100, 'is_qty_decimal' => 0, 'is_in_stock' => 1])
26+
->save();
27+
28+
/** @var $product \Magento\Catalog\Model\Product */
29+
$product = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(\Magento\Catalog\Model\Product::class);
30+
$product->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE)
31+
->setAttributeSetId(4)
32+
->setWebsiteIds([1])
33+
->setName('Simple Product')
34+
->setSku('simple-1')
35+
->setPrice(10)
36+
->setDescription('Description with <b>html tag</b>')
37+
->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH)
38+
->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)
39+
->setCategoryIds([2])
40+
->setStockData(['use_config_manage_stock' => 1, 'qty' => 100, 'is_qty_decimal' => 0, 'is_in_stock' => 1])
41+
->save();
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
require __DIR__ . '/../../Store/_files/second_website_with_two_stores_rollback.php';
8+
9+
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
10+
$productRepository = $objectManager->create(\Magento\Catalog\Api\ProductRepositoryInterface::class);
11+
12+
/** @var \Magento\Framework\Registry $registry */
13+
$registry = $objectManager->get(\Magento\Framework\Registry::class);
14+
15+
$registry->unregister('isSecureArea');
16+
$registry->register('isSecureArea', true);
17+
18+
/** @var \Magento\Catalog\Api\ProductRepositoryInterface $productRepository */
19+
$productRepository = $objectManager->create(\Magento\Catalog\Api\ProductRepositoryInterface::class);
20+
21+
try {
22+
foreach (['simple-2', 'simple-1'] as $sku) {
23+
$product = $productRepository->get($sku, false, null, true);
24+
$productRepository->delete($product);
25+
}
26+
} catch (\Magento\Framework\Exception\NoSuchEntityException $exception) {
27+
//Product already removed
28+
}
29+
30+
$registry->unregister('isSecureArea');
31+
$registry->register('isSecureArea', false);

0 commit comments

Comments
 (0)