Skip to content

Commit 4cd9788

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

File tree

4 files changed

+129
-0
lines changed

4 files changed

+129
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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 ProductWebsiteFilter implements CustomFilterInterface
14+
{
15+
/**
16+
* Apply website 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+
$value = $filter->getValue();
25+
if (strpos($value, ',') !== false) {
26+
$value = explode(',', $value);
27+
}
28+
/** @var Collection $collection */
29+
$collection->addWebsiteFilter($value);
30+
return true;
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Catalog\Test\Unit\Model\Api\SearchCriteria\CollectionProcessor\FilterProcessor;
7+
8+
use Magento\Catalog\Model\Api\SearchCriteria\CollectionProcessor\FilterProcessor\ProductWebsiteFilter;
9+
use Magento\Catalog\Model\ResourceModel\Product\Collection;
10+
use Magento\Framework\Api\Filter;
11+
12+
class ProductCategoryFilterTest extends \PHPUnit_Framework_TestCase
13+
{
14+
/** @var ProductWebsiteFilter */
15+
private $model;
16+
17+
protected function setUp()
18+
{
19+
$this->model = new ProductWebsiteFilter();
20+
}
21+
22+
public function testApply()
23+
{
24+
/** @var Filter|\PHPUnit_Framework_MockObject_MockObject $filterMock */
25+
$filterMock = $this->getMockBuilder(Filter::class)
26+
->disableOriginalConstructor()
27+
->getMock();
28+
29+
/** @var Collection|\PHPUnit_Framework_MockObject_MockObject $collectionMock */
30+
$collectionMock = $this->getMockBuilder(Collection::class)
31+
->disableOriginalConstructor()
32+
->getMock();
33+
34+
$filterMock->expects($this->once())
35+
->method('getValue')
36+
->willReturn('1,2');
37+
38+
$collectionMock->expects($this->once())
39+
->method('addWebsiteFilter')
40+
->with(['1', '2']);
41+
42+
$this->assertTrue($this->model->apply($filterMock, $collectionMock));
43+
}
44+
}

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

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

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

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,58 @@ 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+
* @return void
716+
*/
717+
public function testGetListWithFilteringByWebsite()
718+
{
719+
$website = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(Website::class);
720+
$website->load('test', 'code');
721+
$searchCriteria = [
722+
'searchCriteria' => [
723+
'filter_groups' => [
724+
[
725+
'filters' => [
726+
[
727+
'field' => 'website_id',
728+
'value' => $website->getId(),
729+
'condition_type' => 'eq',
730+
],
731+
],
732+
],
733+
],
734+
'current_page' => 1,
735+
'page_size' => 10,
736+
],
737+
];
738+
$serviceInfo = [
739+
'rest' => [
740+
'resourcePath' => self::RESOURCE_PATH . '?' . http_build_query($searchCriteria),
741+
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
742+
],
743+
'soap' => [
744+
'service' => self::SERVICE_NAME,
745+
'serviceVersion' => self::SERVICE_VERSION,
746+
'operation' => self::SERVICE_NAME . 'GetList',
747+
],
748+
];
749+
$response = $this->_webApiCall($serviceInfo, $searchCriteria);
750+
751+
$this->assertArrayHasKey('search_criteria', $response);
752+
$this->assertArrayHasKey('total_count', $response);
753+
$this->assertArrayHasKey('items', $response);
754+
$this->assertTrue(count($response['items']) == 1);
755+
756+
$isSkuExists = false;
757+
foreach ($response['items'] as $item) {
758+
if ($item['sku'] == 'simple-2') {
759+
$isSkuExists = true;
760+
}
761+
}
762+
$this->assertTrue($isSkuExists);
763+
}
764+
713765
/**
714766
* @magentoApiDataFixture Magento/Catalog/_files/products_with_websites_and_stores.php
715767
* @dataProvider testGetListWithFilteringByStoreDataProvider

0 commit comments

Comments
 (0)