Skip to content

Commit afa3a6f

Browse files
committed
MAGETWO-47252: Some data is being exported as IDs, not names
- Added document data provider for custom fields
1 parent 8cf5dc6 commit afa3a6f

File tree

7 files changed

+518
-3
lines changed

7 files changed

+518
-3
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@
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+
protected $document = Document::class;
18+
1619
/**
1720
* Initialize dependencies.
1821
*
Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
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+
class DocumentTest extends \PHPUnit_Framework_TestCase
25+
{
26+
/**
27+
* @var GroupRepositoryInterface|MockObject
28+
*/
29+
private $groupRepository;
30+
31+
/**
32+
* @var AttributeValueFactory|MockObject
33+
*/
34+
private $attributeValueFactory;
35+
36+
/**
37+
* @var CustomerMetadataInterface|MockObject
38+
*/
39+
private $customerMetadata;
40+
41+
/**
42+
* @var StoreManagerInterface|MockObject
43+
*/
44+
private $storeManager;
45+
46+
/**
47+
* @var Document
48+
*/
49+
private $document;
50+
51+
protected function setUp()
52+
{
53+
$this->initAttributeValueFactoryMock();
54+
55+
$this->groupRepository = $this->getMockForAbstractClass(GroupRepositoryInterface::class);
56+
57+
$this->customerMetadata = $this->getMockForAbstractClass(CustomerMetadataInterface::class);
58+
59+
$this->storeManager = $this->getMockForAbstractClass(StoreManagerInterface::class);
60+
61+
$this->document = new Document(
62+
$this->attributeValueFactory,
63+
$this->groupRepository,
64+
$this->customerMetadata,
65+
$this->storeManager
66+
);
67+
}
68+
69+
/**
70+
* @covers \Magento\Customer\Ui\Component\DataProvider\Document::getCustomAttribute
71+
*/
72+
public function testGetGenderAttribute()
73+
{
74+
$genderId = 1;
75+
$this->document->setData('gender', $genderId);
76+
77+
$this->groupRepository->expects(static::never())
78+
->method('getById');
79+
80+
$this->storeManager->expects(static::never())
81+
->method('getWebsites');
82+
83+
$metadata = $this->getMockForAbstractClass(AttributeMetadataInterface::class);
84+
85+
$this->customerMetadata->expects(static::once())
86+
->method('getAttributeMetadata')
87+
->willReturn($metadata);
88+
89+
$option = $this->getMockForAbstractClass(OptionInterface::class);
90+
91+
$metadata->expects(static::once())
92+
->method('getOptions')
93+
->willReturn([$genderId => $option]);
94+
95+
$option->expects(static::once())
96+
->method('getLabel')
97+
->willReturn('Male');
98+
99+
$attribute = $this->document->getCustomAttribute('gender');
100+
static::assertEquals('Male', $attribute->getValue());
101+
}
102+
103+
/**
104+
* @covers \Magento\Customer\Ui\Component\DataProvider\Document::getCustomAttribute
105+
*/
106+
public function testGetGroupAttribute()
107+
{
108+
$this->document->setData('group_id', 1);
109+
110+
$this->customerMetadata->expects(static::never())
111+
->method('getAttributeMetadata');
112+
113+
$this->storeManager->expects(static::never())
114+
->method('getWebsites');
115+
116+
$group = $this->getMockForAbstractClass(GroupInterface::class);
117+
118+
$this->groupRepository->expects(static::once())
119+
->method('getById')
120+
->willReturn($group);
121+
122+
$group->expects(static::once())
123+
->method('getCode')
124+
->willReturn('General');
125+
126+
$attribute = $this->document->getCustomAttribute('group_id');
127+
static::assertEquals('General', $attribute->getValue());
128+
}
129+
130+
/**
131+
* @covers \Magento\Customer\Ui\Component\DataProvider\Document::getCustomAttribute
132+
*/
133+
public function testGetWebsiteAttribute()
134+
{
135+
$websiteId = 1;
136+
$this->document->setData('website_id', $websiteId);
137+
138+
$this->groupRepository->expects(static::never())
139+
->method('getById');
140+
141+
$this->customerMetadata->expects(static::never())
142+
->method('getAttributeMetadata');
143+
144+
$website = $this->getMockForAbstractClass(WebsiteInterface::class);
145+
146+
$this->storeManager->expects(static::once())
147+
->method('getWebsites')
148+
->willReturn([$websiteId => $website]);
149+
150+
$website->expects(static::once())
151+
->method('getName')
152+
->willReturn('Main Website');
153+
154+
$attribute = $this->document->getCustomAttribute('website_id');
155+
static::assertEquals('Main Website', $attribute->getValue());
156+
}
157+
158+
/**
159+
* Create mock for attribute value factory
160+
* @return void
161+
*/
162+
private function initAttributeValueFactoryMock()
163+
{
164+
$this->attributeValueFactory = $this->getMockBuilder(AttributeValueFactory::class)
165+
->disableOriginalConstructor()
166+
->setMethods(['create'])
167+
->getMock();
168+
169+
$attributeValue = new AttributeValue();
170+
171+
$this->attributeValueFactory->expects(static::once())
172+
->method('create')
173+
->willReturn($attributeValue);
174+
}
175+
}
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)