Skip to content

Commit 5f3ef1b

Browse files
hatimeria-artur-jewulaZefiryn
authored andcommitted
# This is a combination of 3 commits.
# This is the 1st commit message: magento#10765 add confirmation and lock_expires labels to customer grid CSV export # This is the commit message magento#2: magento#10765 Add tests for added methods # This is the commit message magento#3: 10765 code refactoring
1 parent a91df24 commit 5f3ef1b

File tree

2 files changed

+124
-2
lines changed

2 files changed

+124
-2
lines changed

app/code/Magento/Customer/Test/Unit/Ui/Component/DataProvider/DocumentTest.php

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
use Magento\Customer\Ui\Component\DataProvider\Document;
1414
use Magento\Framework\Api\AttributeValue;
1515
use Magento\Framework\Api\AttributeValueFactory;
16+
use Magento\Framework\App\Config\ScopeConfigInterface;
17+
use Magento\Framework\Phrase;
1618
use Magento\Sales\Model\Order\Invoice;
1719
use Magento\Store\Api\Data\WebsiteInterface;
1820
use Magento\Store\Model\StoreManagerInterface;
@@ -45,6 +47,11 @@ class DocumentTest extends \PHPUnit_Framework_TestCase
4547
*/
4648
private $storeManager;
4749

50+
/**
51+
* @var ScopeConfigInterface|MockObject
52+
*/
53+
private $scopeConfig;
54+
4855
/**
4956
* @var Document
5057
*/
@@ -60,11 +67,14 @@ protected function setUp()
6067

6168
$this->storeManager = $this->getMockForAbstractClass(StoreManagerInterface::class);
6269

70+
$this->scopeConfig = $this->getMockForAbstractClass(ScopeConfigInterface::class);
71+
6372
$this->document = new Document(
6473
$this->attributeValueFactory,
6574
$this->groupRepository,
6675
$this->customerMetadata,
67-
$this->storeManager
76+
$this->storeManager,
77+
$this->scopeConfig
6878
);
6979
}
7080

@@ -157,6 +167,42 @@ public function testGetWebsiteAttribute()
157167
static::assertEquals('Main Website', $attribute->getValue());
158168
}
159169

170+
/**
171+
* @covers \Magento\Customer\Ui\Component\DataProvider\Document::getCustomAttribute
172+
*/
173+
public function testGetConfirmationAttribute()
174+
{
175+
$websiteId = 1;
176+
$this->document->setData('original_website_id', $websiteId);
177+
178+
$this->scopeConfig->expects(static::once())
179+
->method('getValue')
180+
->with()
181+
->willReturn(true);
182+
183+
$this->document->setData('confirmation', null);
184+
$attribute = $this->document->getCustomAttribute('confirmation');
185+
186+
$value = $attribute->getValue();
187+
static::assertInstanceOf(Phrase::class, $value);
188+
static::assertEquals('Confirmed', (string)$value);
189+
}
190+
191+
192+
/**
193+
* @covers \Magento\Customer\Ui\Component\DataProvider\Document::getCustomAttribute
194+
*/
195+
public function testGetAccountLockValue()
196+
{
197+
$this->document->setData('lock_expires', null);
198+
199+
$attribute = $this->document->getCustomAttribute('lock_expires');
200+
201+
$value = $attribute->getValue();
202+
static::assertInstanceOf(Phrase::class, $value);
203+
static::assertEquals('Unlocked', (string)$value);
204+
}
205+
160206
/**
161207
* Create mock for attribute value factory
162208
* @return void

app/code/Magento/Customer/Ui/Component/DataProvider/Document.php

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@
66
namespace Magento\Customer\Ui\Component\DataProvider;
77

88
use Magento\Customer\Api\CustomerMetadataInterface;
9+
use Magento\Customer\Model\AccountManagement;
910
use Magento\Framework\Api\AttributeValueFactory;
11+
use Magento\Framework\App\Config\ScopeConfigInterface;
1012
use Magento\Framework\Exception\NoSuchEntityException;
1113
use Magento\Customer\Api\GroupRepositoryInterface;
14+
use Magento\Framework\App\ObjectManager;
15+
use Magento\Store\Model\ScopeInterface;
1216
use Magento\Store\Model\StoreManagerInterface;
1317

1418
/**
@@ -31,6 +35,21 @@ class Document extends \Magento\Framework\View\Element\UiComponent\DataProvider\
3135
*/
3236
private static $websiteAttributeCode = 'website_id';
3337

38+
/**
39+
* @var string
40+
*/
41+
private static $websiteIdAttributeCode = 'original_website_id';
42+
43+
/**
44+
* @var string
45+
*/
46+
private static $confirmationAttributeCode = 'confirmation';
47+
48+
/**
49+
* @var string
50+
*/
51+
private static $accountLockAttributeCode = 'lock_expires';
52+
3453
/**
3554
* @var CustomerMetadataInterface
3655
*/
@@ -46,23 +65,31 @@ class Document extends \Magento\Framework\View\Element\UiComponent\DataProvider\
4665
*/
4766
private $storeManager;
4867

68+
/**
69+
* @var ScopeConfigInterface
70+
*/
71+
private $scopeConfig;
72+
4973
/**
5074
* Document constructor.
5175
* @param AttributeValueFactory $attributeValueFactory
5276
* @param GroupRepositoryInterface $groupRepository
5377
* @param CustomerMetadataInterface $customerMetadata
5478
* @param StoreManagerInterface $storeManager
79+
* @param ScopeConfigInterface $scopeConfig
5580
*/
5681
public function __construct(
5782
AttributeValueFactory $attributeValueFactory,
5883
GroupRepositoryInterface $groupRepository,
5984
CustomerMetadataInterface $customerMetadata,
60-
StoreManagerInterface $storeManager
85+
StoreManagerInterface $storeManager,
86+
ScopeConfigInterface $scopeConfig = null
6187
) {
6288
parent::__construct($attributeValueFactory);
6389
$this->customerMetadata = $customerMetadata;
6490
$this->groupRepository = $groupRepository;
6591
$this->storeManager = $storeManager;
92+
$this->scopeConfig = $scopeConfig ? $scopeConfig : ObjectManager::getInstance()->create(ScopeConfigInterface::class);
6693
}
6794

6895
/**
@@ -80,6 +107,12 @@ public function getCustomAttribute($attributeCode)
80107
case self::$websiteAttributeCode:
81108
$this->setWebsiteValue();
82109
break;
110+
case self::$confirmationAttributeCode:
111+
$this->setConfirmationValue();
112+
break;
113+
case self::$accountLockAttributeCode:
114+
$this->setAccountLockValue();
115+
break;
83116
}
84117
return parent::getCustomAttribute($attributeCode);
85118
}
@@ -133,5 +166,48 @@ private function setWebsiteValue()
133166
$value = $this->getData(self::$websiteAttributeCode);
134167
$list = $this->storeManager->getWebsites();
135168
$this->setCustomAttribute(self::$websiteAttributeCode, $list[$value]->getName());
169+
$this->setCustomAttribute(self::$websiteIdAttributeCode, $value);
170+
}
171+
172+
/**
173+
* Update confirmation value
174+
* Method set confirmation text value to match what is shown in grid
175+
* @return void
176+
*/
177+
private function setConfirmationValue()
178+
{
179+
$value = $this->getData(self::$confirmationAttributeCode);
180+
$websiteId = $this->getData(self::$websiteIdAttributeCode) ?: $this->getData(self::$websiteAttributeCode);
181+
$isConfirmationRequired = (bool)$this->scopeConfig->getValue(
182+
AccountManagement::XML_PATH_IS_CONFIRM,
183+
ScopeInterface::SCOPE_WEBSITES,
184+
$websiteId);
185+
186+
$valueText = __('Confirmation Not Required');
187+
if ($isConfirmationRequired) {
188+
$valueText = $value === null ? __('Confirmed') : __('Confirmation Required');
189+
}
190+
191+
$this->setCustomAttribute(self::$confirmationAttributeCode, $valueText);
192+
}
193+
194+
/**
195+
* Update lock expires value
196+
* Method set account lock text value to match what is shown in grid
197+
* @return void
198+
*/
199+
private function setAccountLockValue()
200+
{
201+
$value = $this->getDataByPath(self::$accountLockAttributeCode);
202+
203+
$valueText = __('Unlocked');
204+
if ($value !== null) {
205+
$lockExpires = new \DateTime($value);
206+
if ($lockExpires > new \DateTime()) {
207+
$valueText = __('Locked');
208+
}
209+
}
210+
211+
$this->setCustomAttribute(self::$accountLockAttributeCode, $valueText);
136212
}
137213
}

0 commit comments

Comments
 (0)