Skip to content

Commit 327f99f

Browse files
committed
Merge remote-tracking branch 'mpi/MAGETWO-47252' into pr-mpi-200416
2 parents 0260b1e + c5feedb commit 327f99f

File tree

7 files changed

+523
-3
lines changed

7 files changed

+523
-3
lines changed

app/code/Magento/Customer/Model/ResourceModel/Grid/Collection.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,19 @@
66

77
namespace Magento\Customer\Model\ResourceModel\Grid;
88

9+
use Magento\Customer\Ui\Component\DataProvider\Document;
910
use Magento\Framework\Data\Collection\Db\FetchStrategyInterface as FetchStrategy;
1011
use Magento\Framework\Data\Collection\EntityFactoryInterface as EntityFactory;
1112
use Magento\Framework\Event\ManagerInterface as EventManager;
1213
use Psr\Log\LoggerInterface as Logger;
1314

1415
class Collection extends \Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult
1516
{
17+
/**
18+
* @inheritdoc
19+
*/
20+
protected $document = Document::class;
21+
1622
/**
1723
* Initialize dependencies.
1824
*
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Customer\Test\Unit\Ui\Component\DataProvider;
7+
8+
use Magento\Customer\Api\CustomerMetadataInterface;
9+
use Magento\Customer\Api\Data\AttributeMetadataInterface;
10+
use Magento\Customer\Api\Data\GroupInterface;
11+
use Magento\Customer\Api\Data\OptionInterface;
12+
use Magento\Customer\Api\GroupRepositoryInterface;
13+
use Magento\Customer\Ui\Component\DataProvider\Document;
14+
use Magento\Framework\Api\AttributeValue;
15+
use Magento\Framework\Api\AttributeValueFactory;
16+
use Magento\Sales\Model\Order\Invoice;
17+
use Magento\Store\Api\Data\WebsiteInterface;
18+
use Magento\Store\Model\StoreManagerInterface;
19+
use PHPUnit_Framework_MockObject_MockObject as MockObject;
20+
21+
/**
22+
* Class DocumentTest
23+
*
24+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
25+
*/
26+
class DocumentTest extends \PHPUnit_Framework_TestCase
27+
{
28+
/**
29+
* @var GroupRepositoryInterface|MockObject
30+
*/
31+
private $groupRepository;
32+
33+
/**
34+
* @var AttributeValueFactory|MockObject
35+
*/
36+
private $attributeValueFactory;
37+
38+
/**
39+
* @var CustomerMetadataInterface|MockObject
40+
*/
41+
private $customerMetadata;
42+
43+
/**
44+
* @var StoreManagerInterface|MockObject
45+
*/
46+
private $storeManager;
47+
48+
/**
49+
* @var Document
50+
*/
51+
private $document;
52+
53+
protected function setUp()
54+
{
55+
$this->initAttributeValueFactoryMock();
56+
57+
$this->groupRepository = $this->getMockForAbstractClass(GroupRepositoryInterface::class);
58+
59+
$this->customerMetadata = $this->getMockForAbstractClass(CustomerMetadataInterface::class);
60+
61+
$this->storeManager = $this->getMockForAbstractClass(StoreManagerInterface::class);
62+
63+
$this->document = new Document(
64+
$this->attributeValueFactory,
65+
$this->groupRepository,
66+
$this->customerMetadata,
67+
$this->storeManager
68+
);
69+
}
70+
71+
/**
72+
* @covers \Magento\Customer\Ui\Component\DataProvider\Document::getCustomAttribute
73+
*/
74+
public function testGetGenderAttribute()
75+
{
76+
$genderId = 1;
77+
$this->document->setData('gender', $genderId);
78+
79+
$this->groupRepository->expects(static::never())
80+
->method('getById');
81+
82+
$this->storeManager->expects(static::never())
83+
->method('getWebsites');
84+
85+
$metadata = $this->getMockForAbstractClass(AttributeMetadataInterface::class);
86+
87+
$this->customerMetadata->expects(static::once())
88+
->method('getAttributeMetadata')
89+
->willReturn($metadata);
90+
91+
$option = $this->getMockForAbstractClass(OptionInterface::class);
92+
93+
$metadata->expects(static::once())
94+
->method('getOptions')
95+
->willReturn([$genderId => $option]);
96+
97+
$option->expects(static::once())
98+
->method('getLabel')
99+
->willReturn('Male');
100+
101+
$attribute = $this->document->getCustomAttribute('gender');
102+
static::assertEquals('Male', $attribute->getValue());
103+
}
104+
105+
/**
106+
* @covers \Magento\Customer\Ui\Component\DataProvider\Document::getCustomAttribute
107+
*/
108+
public function testGetGroupAttribute()
109+
{
110+
$this->document->setData('group_id', 1);
111+
112+
$this->customerMetadata->expects(static::never())
113+
->method('getAttributeMetadata');
114+
115+
$this->storeManager->expects(static::never())
116+
->method('getWebsites');
117+
118+
$group = $this->getMockForAbstractClass(GroupInterface::class);
119+
120+
$this->groupRepository->expects(static::once())
121+
->method('getById')
122+
->willReturn($group);
123+
124+
$group->expects(static::once())
125+
->method('getCode')
126+
->willReturn('General');
127+
128+
$attribute = $this->document->getCustomAttribute('group_id');
129+
static::assertEquals('General', $attribute->getValue());
130+
}
131+
132+
/**
133+
* @covers \Magento\Customer\Ui\Component\DataProvider\Document::getCustomAttribute
134+
*/
135+
public function testGetWebsiteAttribute()
136+
{
137+
$websiteId = 1;
138+
$this->document->setData('website_id', $websiteId);
139+
140+
$this->groupRepository->expects(static::never())
141+
->method('getById');
142+
143+
$this->customerMetadata->expects(static::never())
144+
->method('getAttributeMetadata');
145+
146+
$website = $this->getMockForAbstractClass(WebsiteInterface::class);
147+
148+
$this->storeManager->expects(static::once())
149+
->method('getWebsites')
150+
->willReturn([$websiteId => $website]);
151+
152+
$website->expects(static::once())
153+
->method('getName')
154+
->willReturn('Main Website');
155+
156+
$attribute = $this->document->getCustomAttribute('website_id');
157+
static::assertEquals('Main Website', $attribute->getValue());
158+
}
159+
160+
/**
161+
* Create mock for attribute value factory
162+
* @return void
163+
*/
164+
private function initAttributeValueFactoryMock()
165+
{
166+
$this->attributeValueFactory = $this->getMockBuilder(AttributeValueFactory::class)
167+
->disableOriginalConstructor()
168+
->setMethods(['create'])
169+
->getMock();
170+
171+
$attributeValue = new AttributeValue();
172+
173+
$this->attributeValueFactory->expects(static::once())
174+
->method('create')
175+
->willReturn($attributeValue);
176+
}
177+
}
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Customer\Ui\Component\DataProvider;
7+
8+
use Magento\Customer\Api\CustomerMetadataInterface;
9+
use Magento\Framework\Api\AttributeValueFactory;
10+
use Magento\Framework\Exception\NoSuchEntityException;
11+
use Magento\Customer\Api\GroupRepositoryInterface;
12+
use Magento\Store\Model\StoreManagerInterface;
13+
14+
/**
15+
* Class Document
16+
*/
17+
class Document extends \Magento\Framework\View\Element\UiComponent\DataProvider\Document
18+
{
19+
/**
20+
* @var string
21+
*/
22+
private static $genderAttributeCode = 'gender';
23+
24+
/**
25+
* @var string
26+
*/
27+
private static $groupAttributeCode = 'group_id';
28+
29+
/**
30+
* @var string
31+
*/
32+
private static $websiteAttributeCode = 'website_id';
33+
34+
/**
35+
* @var CustomerMetadataInterface
36+
*/
37+
private $customerMetadata;
38+
39+
/**
40+
* @var GroupRepositoryInterface
41+
*/
42+
private $groupRepository;
43+
44+
/**
45+
* @var StoreManagerInterface
46+
*/
47+
private $storeManager;
48+
49+
/**
50+
* Document constructor.
51+
* @param AttributeValueFactory $attributeValueFactory
52+
* @param GroupRepositoryInterface $groupRepository
53+
* @param CustomerMetadataInterface $customerMetadata
54+
* @param StoreManagerInterface $storeManager
55+
*/
56+
public function __construct(
57+
AttributeValueFactory $attributeValueFactory,
58+
GroupRepositoryInterface $groupRepository,
59+
CustomerMetadataInterface $customerMetadata,
60+
StoreManagerInterface $storeManager
61+
) {
62+
parent::__construct($attributeValueFactory);
63+
$this->customerMetadata = $customerMetadata;
64+
$this->groupRepository = $groupRepository;
65+
$this->storeManager = $storeManager;
66+
}
67+
68+
/**
69+
* @inheritdoc
70+
*/
71+
public function getCustomAttribute($attributeCode)
72+
{
73+
switch ($attributeCode) {
74+
case self::$genderAttributeCode:
75+
$this->setGenderValue();
76+
break;
77+
case self::$groupAttributeCode:
78+
$this->setCustomerGroupValue();
79+
break;
80+
case self::$websiteAttributeCode:
81+
$this->setWebsiteValue();
82+
break;
83+
}
84+
return parent::getCustomAttribute($attributeCode);
85+
}
86+
87+
/**
88+
* Update customer gender value
89+
* Method set gender label instead of id value
90+
* @return void
91+
*/
92+
private function setGenderValue()
93+
{
94+
$value = $this->getData(self::$genderAttributeCode);
95+
96+
if (!$value) {
97+
$this->setCustomAttribute(self::$genderAttributeCode, 'N/A');
98+
return;
99+
}
100+
101+
try {
102+
$attributeMetadata = $this->customerMetadata->getAttributeMetadata(self::$genderAttributeCode);
103+
$option = $attributeMetadata->getOptions()[$value];
104+
$this->setCustomAttribute(self::$genderAttributeCode, $option->getLabel());
105+
} catch (NoSuchEntityException $e) {
106+
$this->setCustomAttribute(self::$genderAttributeCode, 'N/A');
107+
}
108+
}
109+
110+
/**
111+
* Update customer group value
112+
* Method set group code instead id value
113+
* @return void
114+
*/
115+
private function setCustomerGroupValue()
116+
{
117+
$value = $this->getData(self::$groupAttributeCode);
118+
try {
119+
$group = $this->groupRepository->getById($value);
120+
$this->setCustomAttribute(self::$groupAttributeCode, $group->getCode());
121+
} catch (NoSuchEntityException $e) {
122+
$this->setCustomAttribute(self::$groupAttributeCode, 'N/A');
123+
}
124+
}
125+
126+
/**
127+
* Update website value
128+
* Method set website name instead id value
129+
* @return void
130+
*/
131+
private function setWebsiteValue()
132+
{
133+
$value = $this->getData(self::$websiteAttributeCode);
134+
$list = $this->storeManager->getWebsites();
135+
$this->setCustomAttribute(self::$websiteAttributeCode, $list[$value]->getName());
136+
}
137+
}

app/code/Magento/Sales/Model/ResourceModel/Order/Invoice/Grid/Collection.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,16 @@
99
use Magento\Framework\Data\Collection\Db\FetchStrategyInterface as FetchStrategy;
1010
use Magento\Framework\Data\Collection\EntityFactoryInterface as EntityFactory;
1111
use Magento\Framework\Event\ManagerInterface as EventManager;
12+
use Magento\Sales\Ui\Component\DataProvider\Document;
1213
use Psr\Log\LoggerInterface as Logger;
1314

1415
class Collection extends \Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult
1516
{
17+
/**
18+
* @inheritdoc
19+
*/
20+
protected $document = Document::class;
21+
1622
/**
1723
* Initialize dependencies.
1824
*

0 commit comments

Comments
 (0)