Skip to content

Commit ab6cc89

Browse files
author
Dmytro Vilchynskyi
committed
Merge remote-tracking branch 'origin/MAGETWO-63667' into PR_26042017
2 parents 24a130b + 519398d commit ab6cc89

File tree

7 files changed

+304
-0
lines changed

7 files changed

+304
-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+
}
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 ProductWebsiteFilterTest 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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -835,6 +835,8 @@
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>
839+
<item name="website_id" xsi:type="object">Magento\Catalog\Model\Api\SearchCriteria\CollectionProcessor\FilterProcessor\ProductWebsiteFilter</item>
838840
</argument>
839841
</arguments>
840842
</virtualType>

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

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,132 @@ 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+
$this->assertTrue(isset($response['items'][0]['sku']));
756+
$this->assertEquals('simple-2', $response['items'][0]['sku']);
757+
}
758+
759+
/**
760+
* @magentoApiDataFixture Magento/Catalog/_files/products_with_websites_and_stores.php
761+
* @dataProvider testGetListWithFilteringByStoreDataProvider
762+
*
763+
* @param array $searchCriteria
764+
* @param array $skus
765+
* @param int $expectedProductCount
766+
* @return void
767+
*/
768+
public function testGetListWithFilteringByStore(array $searchCriteria, array $skus, $expectedProductCount = null)
769+
{
770+
$serviceInfo = [
771+
'rest' => [
772+
'resourcePath' => self::RESOURCE_PATH . '?' . http_build_query($searchCriteria),
773+
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
774+
],
775+
'soap' => [
776+
'service' => self::SERVICE_NAME,
777+
'serviceVersion' => self::SERVICE_VERSION,
778+
'operation' => self::SERVICE_NAME . 'GetList',
779+
],
780+
];
781+
$response = $this->_webApiCall($serviceInfo, $searchCriteria);
782+
783+
$this->assertArrayHasKey('search_criteria', $response);
784+
$this->assertArrayHasKey('total_count', $response);
785+
$this->assertArrayHasKey('items', $response);
786+
if ($expectedProductCount) {
787+
$this->assertTrue(count($response['items']) == $expectedProductCount);
788+
}
789+
790+
$isResultValid = false;
791+
foreach ($skus as $sku) {
792+
foreach ($response['items'] as $item) {
793+
if ($item['sku'] == $sku) {
794+
$isResultValid = true;
795+
}
796+
}
797+
$this->assertTrue($isResultValid);
798+
}
799+
}
800+
801+
public function testGetListWithFilteringByStoreDataProvider()
802+
{
803+
return [
804+
[
805+
[
806+
'searchCriteria' => [
807+
'filter_groups' => [
808+
[
809+
'filters' => [
810+
[
811+
'field' => 'store',
812+
'value' => 'fixture_second_store',
813+
'condition_type' => 'eq',
814+
],
815+
],
816+
],
817+
],
818+
'current_page' => 1,
819+
'page_size' => 10,
820+
],
821+
],
822+
['simple-2'],
823+
1,
824+
],
825+
[
826+
[
827+
'searchCriteria' => [
828+
'filter_groups' => [],
829+
'current_page' => 1,
830+
'page_size' => 10,
831+
],
832+
],
833+
['simple-2', 'simple-1'],
834+
null
835+
]
836+
];
837+
}
838+
713839
/**
714840
* @magentoApiDataFixture Magento/Catalog/_files/products_for_search.php
715841
*/
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)