Skip to content

Commit 681bc1b

Browse files
author
Korshenko, Olexii(okorshenko)
committed
Merge pull request #102 from magento-firedrakes/bugfixes
[Firedrakes] Bugfixes
2 parents 777f782 + 6342bf7 commit 681bc1b

File tree

27 files changed

+367
-304
lines changed

27 files changed

+367
-304
lines changed

app/code/Magento/Catalog/Model/ProductRepository.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -684,10 +684,21 @@ protected function addFilterGroupToCollection(
684684
Collection $collection
685685
) {
686686
$fields = [];
687+
$categoryFilter = [];
687688
foreach ($filterGroup->getFilters() as $filter) {
688-
$condition = $filter->getConditionType() ? $filter->getConditionType() : 'eq';
689-
$fields[] = ['attribute' => $filter->getField(), $condition => $filter->getValue()];
689+
$conditionType = $filter->getConditionType() ? $filter->getConditionType() : 'eq';
690+
691+
if ($filter->getField() == 'category_id') {
692+
$categoryFilter[$conditionType][] = $filter->getValue();
693+
continue;
694+
}
695+
$fields[] = ['attribute' => $filter->getField(), $conditionType => $filter->getValue()];
690696
}
697+
698+
if ($categoryFilter) {
699+
$collection->addCategoriesFilter($categoryFilter);
700+
}
701+
691702
if ($fields) {
692703
$collection->addFieldToFilter($fields);
693704
}

app/code/Magento/Catalog/Model/ResourceModel/Product/Collection.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -840,6 +840,40 @@ public function addCategoryFilter(\Magento\Catalog\Model\Category $category)
840840
return $this;
841841
}
842842

843+
/**
844+
* Filter Product by Categories
845+
*
846+
* @param array $categoriesFilter
847+
*/
848+
public function addCategoriesFilter(array $categoriesFilter)
849+
{
850+
foreach ($categoriesFilter as $conditionType => $values) {
851+
$categorySelect = $this->getConnection()->select()->from(
852+
['cat' => $this->getTable('catalog_category_product')],
853+
'cat.product_id'
854+
)->where($this->getConnection()->prepareSqlCondition('cat.category_id', ['in' => $values]));
855+
$selectCondition = [
856+
$this->mapConditionType($conditionType) => $categorySelect
857+
];
858+
$this->getSelect()->where($this->getConnection()->prepareSqlCondition('e.entity_id' , $selectCondition));
859+
}
860+
}
861+
862+
/**
863+
* Map equal and not equal conditions to in and not in
864+
*
865+
* @param string $conditionType
866+
* @return mixed
867+
*/
868+
private function mapConditionType($conditionType)
869+
{
870+
$conditionsMap = [
871+
'eq' => 'in',
872+
'neq' => 'nin'
873+
];
874+
return isset($conditionsMap[$conditionType]) ? $conditionsMap[$conditionType] : $conditionType;
875+
}
876+
843877
/**
844878
* Join minimal price attribute to result
845879
*

app/code/Magento/Catalog/Test/Unit/Model/ProductRepositoryTest.php

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,10 @@ public function testDeleteById()
553553
$this->assertTrue($this->model->deleteById($sku));
554554
}
555555

556-
public function testGetList()
556+
/**
557+
* @dataProvider fieldName
558+
*/
559+
public function testGetList($fieldName)
557560
{
558561
$searchCriteriaMock = $this->getMock('\Magento\Framework\Api\SearchCriteriaInterface', [], [], '', false);
559562
$attributeCode = 'attribute_code';
@@ -596,15 +599,14 @@ public function testGetList()
596599
$searchCriteriaMock->expects($this->once())->method('getFilterGroups')->willReturn([$filterGroupMock]);
597600
$filterGroupMock->expects($this->once())->method('getFilters')->willReturn([$filterGroupFilterMock]);
598601
$filterGroupFilterMock->expects($this->exactly(2))->method('getConditionType')->willReturn('eq');
599-
$filterGroupFilterMock->expects($this->once())->method('getField')->willReturn('field');
602+
$filterGroupFilterMock->expects($this->atLeastOnce())->method('getField')->willReturn($fieldName);
600603
$filterGroupFilterMock->expects($this->once())->method('getValue')->willReturn('value');
601-
$collectionMock->expects($this->once())->method('addFieldToFilter')
602-
->with([['attribute' => 'field', 'eq' => 'value']]);
604+
$this->expectAddToFilter($fieldName, $collectionMock);
603605
$searchCriteriaMock->expects($this->once())->method('getSortOrders')->willReturn([$sortOrderMock]);
604-
$sortOrderMock->expects($this->once())->method('getField')->willReturn('field');
606+
$sortOrderMock->expects($this->atLeastOnce())->method('getField')->willReturn($fieldName);
605607
$sortOrderMock->expects($this->once())->method('getDirection')
606608
->willReturn(SortOrder::SORT_ASC);
607-
$collectionMock->expects($this->once())->method('addOrder')->with('field', 'ASC');
609+
$collectionMock->expects($this->once())->method('addOrder')->with($fieldName, 'ASC');
608610
$searchCriteriaMock->expects($this->once())->method('getCurrentPage')->willReturn(4);
609611
$collectionMock->expects($this->once())->method('setCurPage')->with(4);
610612
$searchCriteriaMock->expects($this->once())->method('getPageSize')->willReturn(42);
@@ -1199,4 +1201,31 @@ public function testSaveExistingWithMediaGalleryEntries()
11991201
$this->model->save($this->productMock);
12001202
$this->assertEquals($expectedResult, $this->initializedProductMock->getMediaGallery('images'));
12011203
}
1204+
1205+
/**
1206+
* @param $fieldName
1207+
* @param $collectionMock
1208+
* @return void
1209+
*/
1210+
public function expectAddToFilter($fieldName, $collectionMock)
1211+
{
1212+
if ($fieldName == 'category_id') {
1213+
$collectionMock->expects($this->once())->method('addCategoriesFilter')
1214+
->with(['eq' => ['value']]);
1215+
} else {
1216+
$collectionMock->expects($this->once())->method('addFieldToFilter')
1217+
->with([['attribute' => $fieldName, 'eq' => 'value']]);
1218+
}
1219+
}
1220+
1221+
/**
1222+
* @return array
1223+
*/
1224+
public function fieldName()
1225+
{
1226+
return [
1227+
['category_id'],
1228+
['field']
1229+
];
1230+
}
12021231
}
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Catalog\Test\Unit\Model\ResourceModel\Product;
8+
9+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
10+
11+
/**
12+
* Class CollectionTest
13+
*/
14+
class CollectionTest extends \PHPUnit_Framework_TestCase
15+
{
16+
/**
17+
* @var \PHPUnit_Framework_MockObject_MockObject
18+
*/
19+
protected $selectMock;
20+
21+
/**
22+
* @var \PHPUnit_Framework_MockObject_MockObject
23+
*/
24+
protected $connectionMock;
25+
26+
/**
27+
* @var \Magento\Catalog\Model\ResourceModel\Product\Collection
28+
*/
29+
protected $collection;
30+
31+
/**
32+
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
33+
*/
34+
public function setUp()
35+
{
36+
$entityFactory = $this->getMock('Magento\Framework\Data\Collection\EntityFactory', [], [], '', false);
37+
$logger = $this->getMockBuilder('Psr\Log\LoggerInterface')
38+
->disableOriginalConstructor()
39+
->getMockForAbstractClass();
40+
$fetchStrategy = $this->getMockBuilder('Magento\Framework\Data\Collection\Db\FetchStrategyInterface')
41+
->disableOriginalConstructor()
42+
->getMockForAbstractClass();
43+
$eventManager = $this->getMockBuilder('Magento\Framework\Event\ManagerInterface')
44+
->disableOriginalConstructor()
45+
->getMockForAbstractClass();
46+
$eavConfig = $this->getMockBuilder('Magento\Eav\Model\Config')
47+
->disableOriginalConstructor()
48+
->getMock();
49+
$resource = $this->getMockBuilder('Magento\Framework\App\ResourceConnection')
50+
->disableOriginalConstructor()
51+
->getMock();
52+
$eavEntityFactory = $this->getMockBuilder('Magento\Eav\Model\EntityFactory')
53+
->disableOriginalConstructor()
54+
->getMock();
55+
$resourceHelper = $this->getMockBuilder('Magento\Catalog\Model\ResourceModel\Helper')
56+
->disableOriginalConstructor()
57+
->getMock();
58+
$universalFactory = $this->getMockBuilder('Magento\Framework\Validator\UniversalFactory')
59+
->disableOriginalConstructor()
60+
->getMock();
61+
$storeManager = $this->getMockBuilder('Magento\Store\Model\StoreManagerInterface')
62+
->disableOriginalConstructor()
63+
->setMethods(['getStore', 'getId'])
64+
->getMockForAbstractClass();
65+
$moduleManager = $this->getMockBuilder('Magento\Framework\Module\Manager')
66+
->disableOriginalConstructor()
67+
->getMock();
68+
$catalogProductFlatState = $this->getMockBuilder('Magento\Catalog\Model\Indexer\Product\Flat\State')
69+
->disableOriginalConstructor()
70+
->getMock();
71+
$scopeConfig = $this->getMockBuilder('Magento\Framework\App\Config\ScopeConfigInterface')
72+
->disableOriginalConstructor()
73+
->getMockForAbstractClass();
74+
$productOptionFactory = $this->getMockBuilder('Magento\Catalog\Model\Product\OptionFactory')
75+
->disableOriginalConstructor()
76+
->getMock();
77+
$catalogUrl = $this->getMockBuilder('Magento\Catalog\Model\ResourceModel\Url')
78+
->disableOriginalConstructor()
79+
->getMock();
80+
$localeDate = $this->getMockBuilder('Magento\Framework\Stdlib\DateTime\TimezoneInterface')
81+
->disableOriginalConstructor()
82+
->getMockForAbstractClass();
83+
$customerSession = $this->getMockBuilder('Magento\Customer\Model\Session')
84+
->disableOriginalConstructor()
85+
->getMock();
86+
$dateTime = $this->getMockBuilder('Magento\Framework\Stdlib\DateTime')
87+
->disableOriginalConstructor()
88+
->getMock();
89+
$groupManagement = $this->getMockBuilder('Magento\Customer\Api\GroupManagementInterface')
90+
->disableOriginalConstructor()
91+
->getMockForAbstractClass();
92+
93+
$this->connectionMock = $this->getMockBuilder('Magento\Framework\DB\Adapter\AdapterInterface')
94+
->disableOriginalConstructor()
95+
->getMockForAbstractClass();
96+
97+
$this->selectMock = $this->getMockBuilder('Magento\Framework\DB\Select')
98+
->disableOriginalConstructor()
99+
->getMock();
100+
101+
$entityMock = $this->getMockBuilder('Magento\Eav\Model\Entity\AbstractEntity')
102+
->disableOriginalConstructor()
103+
->getMock();
104+
105+
$storeManager->expects($this->any())->method('getId')->willReturn(1);
106+
$storeManager->expects($this->any())->method('getStore')->willReturnSelf();
107+
$universalFactory->expects($this->exactly(1))->method('create')->willReturnOnConsecutiveCalls(
108+
$entityMock
109+
);
110+
$entityMock->expects($this->once())->method('getConnection')->willReturn($this->connectionMock);
111+
$entityMock->expects($this->once())->method('getDefaultAttributes')->willReturn([]);
112+
$entityMock->expects($this->any())->method('getTable')->willReturnArgument(0);
113+
$this->connectionMock->expects($this->atLeastOnce())->method('select')->willReturn($this->selectMock);
114+
$helper = new ObjectManager($this);
115+
$this->collection = $helper->getObject(
116+
'Magento\Catalog\Model\ResourceModel\Product\Collection',
117+
[
118+
'entityFactory' => $entityFactory,
119+
'logger' => $logger,
120+
'fetchStrategy' => $fetchStrategy,
121+
'eventManager' => $eventManager,
122+
'eavConfig' => $eavConfig,
123+
'resource' => $resource,
124+
'eavEntityFactory' => $eavEntityFactory,
125+
'resourceHelper' => $resourceHelper,
126+
'universalFactory' => $universalFactory,
127+
'storeManager' => $storeManager,
128+
'moduleManager' => $moduleManager,
129+
'catalogProductFlatState' => $catalogProductFlatState,
130+
'scopeConfig' => $scopeConfig,
131+
'productOptionFactory' => $productOptionFactory,
132+
'catalogUrl' => $catalogUrl,
133+
'localeDate' => $localeDate,
134+
'customerSession' => $customerSession,
135+
'dateTime' => $dateTime,
136+
'groupManagement' => $groupManagement,
137+
'connection' => $this->connectionMock
138+
]
139+
);
140+
$this->collection->setConnection($this->connectionMock);
141+
}
142+
143+
public function testAddProductCategoriesFilter()
144+
{
145+
$condition = ['in' => [1,2]];
146+
$values = [1,2];
147+
$conditionType = 'nin';
148+
$preparedSql = "category_id IN(1,2)";
149+
$tableName = "catalog_category_product";
150+
$this->connectionMock->expects($this->any())->method('getId')->willReturn(1);
151+
$this->connectionMock->expects($this->exactly(2))->method('prepareSqlCondition')->withConsecutive(
152+
['cat.category_id', $condition],
153+
['e.entity_id', [$conditionType => $this->selectMock]]
154+
)->willReturnOnConsecutiveCalls(
155+
$preparedSql,
156+
'e.entity_id IN (1,2)'
157+
);
158+
$this->selectMock->expects($this->once())->method('from')->with(
159+
['cat' => $tableName],
160+
'cat.product_id'
161+
)->willReturnSelf();
162+
$this->selectMock->expects($this->exactly(2))->method('where')->withConsecutive(
163+
[$preparedSql],
164+
['e.entity_id IN (1,2)']
165+
)->willReturnSelf();
166+
$this->collection->addCategoriesFilter([$conditionType => $values]);
167+
}
168+
}

app/code/Magento/Payment/Gateway/Data/Order/OrderAdapter.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@
77

88
use Magento\Payment\Gateway\Data\AddressAdapterInterface;
99
use Magento\Payment\Gateway\Data\OrderAdapterInterface;
10-
use Magento\Sales\Api\Data\OrderInterface;
10+
use Magento\Sales\Model\Order;
1111

1212
/**
1313
* Class OrderAdapter
1414
*/
1515
class OrderAdapter implements OrderAdapterInterface
1616
{
1717
/**
18-
* @var OrderInterface
18+
* @var Order
1919
*/
2020
private $order;
2121

@@ -25,11 +25,11 @@ class OrderAdapter implements OrderAdapterInterface
2525
private $addressAdapterFactory;
2626

2727
/**
28-
* @param OrderInterface $order
28+
* @param Order $order
2929
* @param AddressAdapterFactory $addressAdapterFactory
3030
*/
3131
public function __construct(
32-
OrderInterface $order,
32+
Order $order,
3333
AddressAdapterFactory $addressAdapterFactory
3434
) {
3535
$this->order = $order;

app/code/Magento/Payment/Test/Unit/Gateway/Data/Order/OrderAdapterTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ class OrderAdapterTest extends \PHPUnit_Framework_TestCase
2929

3030
protected function setUp()
3131
{
32-
$this->orderMock = $this->getMockBuilder('Magento\Sales\Api\Data\OrderInterface')
33-
->getMockForAbstractClass();
32+
$this->orderMock = $this->getMockBuilder('Magento\Sales\Model\Order')
33+
->disableOriginalConstructor()
34+
->getMock();
3435

3536
$this->addressAdapterFactoryMock =
3637
$this->getMockBuilder('Magento\Payment\Gateway\Data\Order\AddressAdapterFactory')

app/code/Magento/Quote/Model/Quote/Address.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Magento\Customer\Api\AddressMetadataInterface;
99
use Magento\Customer\Api\Data\AddressInterfaceFactory;
1010
use Magento\Customer\Api\Data\RegionInterfaceFactory;
11+
use Magento\Quote\Api\Data\AddressInterface;
1112

1213
/**
1314
* Sales Quote address model
@@ -26,7 +27,6 @@
2627
* @method Address setFreeShipping(int $value)
2728
* @method int getCollectShippingRates()
2829
* @method Address setCollectShippingRates(int $value)
29-
* @method string getShippingMethod()
3030
* @method Address setShippingMethod(string $value)
3131
* @method string getShippingDescription()
3232
* @method Address setShippingDescription(string $value)
@@ -1676,6 +1676,16 @@ public function setExtensionAttributes(\Magento\Quote\Api\Data\AddressExtensionI
16761676
return $this->_setExtensionAttributes($extensionAttributes);
16771677
}
16781678

1679+
/**
1680+
* Shipping method
1681+
*
1682+
* @return string
1683+
*/
1684+
public function getShippingMethod()
1685+
{
1686+
return $this->getData('shipping_method');
1687+
}
1688+
16791689
/**
16801690
* {@inheritdoc}
16811691
*/

app/code/Magento/Quote/Model/Quote/Address/ToOrder.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,9 @@ public function convert(Address $object, $data = [])
6767
'to_order',
6868
$object
6969
);
70-
70+
/**
71+
* @var $order \Magento\Sales\Model\Order
72+
*/
7173
$order = $this->orderFactory->create();
7274
$this->dataObjectHelper->populateWithArray(
7375
$order,
@@ -77,7 +79,6 @@ public function convert(Address $object, $data = [])
7779
$order->setStoreId($object->getQuote()->getStoreId())
7880
->setQuoteId($object->getQuote()->getId())
7981
->setIncrementId($object->getQuote()->getReservedOrderId());
80-
8182
$this->objectCopyService->copyFieldsetToTarget('sales_convert_quote', 'to_order', $object->getQuote(), $order);
8283
$this->eventManager->dispatch(
8384
'sales_convert_quote_to_order',

0 commit comments

Comments
 (0)