From 3c589eaf71a87c8de885976698f5c18dba49ee85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Artur=20Jewu=C5=82a?= Date: Sun, 17 Sep 2017 12:00:43 +0200 Subject: [PATCH 1/5] #10765 add confirmation and lock_expires labels to customer grid CSV export --- .../Ui/Component/DataProvider/Document.php | 76 ++++++++++++++++++- 1 file changed, 75 insertions(+), 1 deletion(-) diff --git a/app/code/Magento/Customer/Ui/Component/DataProvider/Document.php b/app/code/Magento/Customer/Ui/Component/DataProvider/Document.php index f5cd5cac62d86..6255dc2b9eeda 100644 --- a/app/code/Magento/Customer/Ui/Component/DataProvider/Document.php +++ b/app/code/Magento/Customer/Ui/Component/DataProvider/Document.php @@ -6,9 +6,12 @@ namespace Magento\Customer\Ui\Component\DataProvider; use Magento\Customer\Api\CustomerMetadataInterface; +use Magento\Customer\Model\AccountManagement; use Magento\Framework\Api\AttributeValueFactory; +use Magento\Framework\App\Config\ScopeConfigInterface; use Magento\Framework\Exception\NoSuchEntityException; use Magento\Customer\Api\GroupRepositoryInterface; +use Magento\Store\Model\ScopeInterface; use Magento\Store\Model\StoreManagerInterface; /** @@ -31,6 +34,21 @@ class Document extends \Magento\Framework\View\Element\UiComponent\DataProvider\ */ private static $websiteAttributeCode = 'website_id'; + /** + * @var string + */ + private static $websiteIdAttributeCode = 'original_website_id'; + + /** + * @var string + */ + private static $confirmationAttributeCode = 'confirmation'; + + /** + * @var string + */ + private static $accountLockAttributeCode = 'lock_expires'; + /** * @var CustomerMetadataInterface */ @@ -46,23 +64,31 @@ class Document extends \Magento\Framework\View\Element\UiComponent\DataProvider\ */ private $storeManager; + /** + * @var ScopeConfigInterface + */ + private $scopeConfig; + /** * Document constructor. * @param AttributeValueFactory $attributeValueFactory * @param GroupRepositoryInterface $groupRepository * @param CustomerMetadataInterface $customerMetadata * @param StoreManagerInterface $storeManager + * @param ScopeConfigInterface $scopeConfig */ public function __construct( AttributeValueFactory $attributeValueFactory, GroupRepositoryInterface $groupRepository, CustomerMetadataInterface $customerMetadata, - StoreManagerInterface $storeManager + StoreManagerInterface $storeManager, + ScopeConfigInterface $scopeConfig ) { parent::__construct($attributeValueFactory); $this->customerMetadata = $customerMetadata; $this->groupRepository = $groupRepository; $this->storeManager = $storeManager; + $this->scopeConfig = $scopeConfig; } /** @@ -80,6 +106,12 @@ public function getCustomAttribute($attributeCode) case self::$websiteAttributeCode: $this->setWebsiteValue(); break; + case self::$confirmationAttributeCode: + $this->setConfirmationValue(); + break; + case self::$accountLockAttributeCode: + $this->setAccountLockValue(); + break; } return parent::getCustomAttribute($attributeCode); } @@ -133,5 +165,47 @@ private function setWebsiteValue() $value = $this->getData(self::$websiteAttributeCode); $list = $this->storeManager->getWebsites(); $this->setCustomAttribute(self::$websiteAttributeCode, $list[$value]->getName()); + $this->setCustomAttribute(self::$websiteIdAttributeCode, $value); + } + + /** + * Update confirmation value + * Method set confirmation text value to match what is shown in grid + * @return void + */ + private function setConfirmationValue() + { + $value = $this->getData(self::$confirmationAttributeCode); + $websiteId = $this->getData(self::$websiteIdAttributeCode) ?: $this->getData(self::$websiteAttributeCode); + $isConfirmationRequired = (bool)$this->scopeConfig->getValue( + AccountManagement::XML_PATH_IS_CONFIRM, + ScopeInterface::SCOPE_WEBSITES, + $websiteId); + + $valueText = !$isConfirmationRequired ? + __('Confirmation Not Required') + : ($value === null ? __('Confirmed') : __('Confirmation Required')); + + $this->setCustomAttribute(self::$confirmationAttributeCode, $valueText); + } + + /** + * Update lock expires value + * Method set account lock text value to match what is shown in grid + * @return void + */ + private function setAccountLockValue() + { + $value = $this->getDataByPath(self::$accountLockAttributeCode); + + $valueText = __('Unlocked'); + if ($value !== null) { + $lockExpires = new \DateTime($value); + if ($lockExpires > new \DateTime()) { + $valueText = __('Locked'); + } + } + + $this->setCustomAttribute(self::$accountLockAttributeCode, $valueText); } } From 0637eed27ffeb02fe86a3d5173ce21d070d256fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Artur=20Jewu=C5=82a?= Date: Sun, 17 Sep 2017 12:49:29 +0200 Subject: [PATCH 2/5] #10765 Add tests for added methods --- .../Component/DataProvider/DocumentTest.php | 48 ++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/app/code/Magento/Customer/Test/Unit/Ui/Component/DataProvider/DocumentTest.php b/app/code/Magento/Customer/Test/Unit/Ui/Component/DataProvider/DocumentTest.php index e9c91d8305e90..ec14c43219449 100644 --- a/app/code/Magento/Customer/Test/Unit/Ui/Component/DataProvider/DocumentTest.php +++ b/app/code/Magento/Customer/Test/Unit/Ui/Component/DataProvider/DocumentTest.php @@ -13,6 +13,8 @@ use Magento\Customer\Ui\Component\DataProvider\Document; use Magento\Framework\Api\AttributeValue; use Magento\Framework\Api\AttributeValueFactory; +use Magento\Framework\App\Config\ScopeConfigInterface; +use Magento\Framework\Phrase; use Magento\Store\Api\Data\WebsiteInterface; use Magento\Store\Model\StoreManagerInterface; use PHPUnit_Framework_MockObject_MockObject as MockObject; @@ -44,6 +46,11 @@ class DocumentTest extends \PHPUnit\Framework\TestCase */ private $storeManager; + /** + * @var ScopeConfigInterface|MockObject + */ + private $scopeConfig; + /** * @var Document */ @@ -59,11 +66,14 @@ protected function setUp() $this->storeManager = $this->getMockForAbstractClass(StoreManagerInterface::class); + $this->scopeConfig = $this->getMockForAbstractClass(ScopeConfigInterface::class); + $this->document = new Document( $this->attributeValueFactory, $this->groupRepository, $this->customerMetadata, - $this->storeManager + $this->storeManager, + $this->scopeConfig ); } @@ -156,6 +166,42 @@ public function testGetWebsiteAttribute() static::assertEquals('Main Website', $attribute->getValue()); } + /** + * @covers \Magento\Customer\Ui\Component\DataProvider\Document::getCustomAttribute + */ + public function testGetConfirmationAttribute() + { + $websiteId = 1; + $this->document->setData('original_website_id', $websiteId); + + $this->scopeConfig->expects(static::once()) + ->method('getValue') + ->with() + ->willReturn(true); + + $this->document->setData('confirmation', null); + $attribute = $this->document->getCustomAttribute('confirmation'); + + $value = $attribute->getValue(); + static::assertInstanceOf(Phrase::class, $value); + static::assertEquals('Confirmed', (string)$value); + } + + + /** + * @covers \Magento\Customer\Ui\Component\DataProvider\Document::getCustomAttribute + */ + public function testGetAccountLockValue() + { + $this->document->setData('lock_expires', null); + + $attribute = $this->document->getCustomAttribute('lock_expires'); + + $value = $attribute->getValue(); + static::assertInstanceOf(Phrase::class, $value); + static::assertEquals('Unlocked', (string)$value); + } + /** * Create mock for attribute value factory * @return void From a5680e6ec9c458fc5f0c531bb1b472cf39f69096 Mon Sep 17 00:00:00 2001 From: Zefiryn Date: Sun, 17 Sep 2017 13:31:21 +0200 Subject: [PATCH 3/5] 10765 code refactoring --- .../Customer/Ui/Component/DataProvider/Document.php | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/app/code/Magento/Customer/Ui/Component/DataProvider/Document.php b/app/code/Magento/Customer/Ui/Component/DataProvider/Document.php index 6255dc2b9eeda..7d66e54bd8b81 100644 --- a/app/code/Magento/Customer/Ui/Component/DataProvider/Document.php +++ b/app/code/Magento/Customer/Ui/Component/DataProvider/Document.php @@ -11,6 +11,7 @@ use Magento\Framework\App\Config\ScopeConfigInterface; use Magento\Framework\Exception\NoSuchEntityException; use Magento\Customer\Api\GroupRepositoryInterface; +use Magento\Framework\App\ObjectManager; use Magento\Store\Model\ScopeInterface; use Magento\Store\Model\StoreManagerInterface; @@ -82,13 +83,13 @@ public function __construct( GroupRepositoryInterface $groupRepository, CustomerMetadataInterface $customerMetadata, StoreManagerInterface $storeManager, - ScopeConfigInterface $scopeConfig + ScopeConfigInterface $scopeConfig = null ) { parent::__construct($attributeValueFactory); $this->customerMetadata = $customerMetadata; $this->groupRepository = $groupRepository; $this->storeManager = $storeManager; - $this->scopeConfig = $scopeConfig; + $this->scopeConfig = $scopeConfig ? $scopeConfig : ObjectManager::getInstance()->create(ScopeConfigInterface::class); } /** @@ -182,9 +183,10 @@ private function setConfirmationValue() ScopeInterface::SCOPE_WEBSITES, $websiteId); - $valueText = !$isConfirmationRequired ? - __('Confirmation Not Required') - : ($value === null ? __('Confirmed') : __('Confirmation Required')); + $valueText = __('Confirmation Not Required'); + if ($isConfirmationRequired) { + $valueText = $value === null ? __('Confirmed') : __('Confirmation Required'); + } $this->setCustomAttribute(self::$confirmationAttributeCode, $valueText); } From 20c3b4e1448a3bb0d748890cf1b5bf3508073d90 Mon Sep 17 00:00:00 2001 From: Zefiryn Date: Sun, 17 Sep 2017 16:06:49 +0200 Subject: [PATCH 4/5] 10765 fix coding standards --- .../Test/Unit/Ui/Component/DataProvider/DocumentTest.php | 1 - .../Magento/Customer/Ui/Component/DataProvider/Document.php | 5 +++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/app/code/Magento/Customer/Test/Unit/Ui/Component/DataProvider/DocumentTest.php b/app/code/Magento/Customer/Test/Unit/Ui/Component/DataProvider/DocumentTest.php index ec14c43219449..1d7905cca7941 100644 --- a/app/code/Magento/Customer/Test/Unit/Ui/Component/DataProvider/DocumentTest.php +++ b/app/code/Magento/Customer/Test/Unit/Ui/Component/DataProvider/DocumentTest.php @@ -187,7 +187,6 @@ public function testGetConfirmationAttribute() static::assertEquals('Confirmed', (string)$value); } - /** * @covers \Magento\Customer\Ui\Component\DataProvider\Document::getCustomAttribute */ diff --git a/app/code/Magento/Customer/Ui/Component/DataProvider/Document.php b/app/code/Magento/Customer/Ui/Component/DataProvider/Document.php index 7d66e54bd8b81..a9a5c5b17744e 100644 --- a/app/code/Magento/Customer/Ui/Component/DataProvider/Document.php +++ b/app/code/Magento/Customer/Ui/Component/DataProvider/Document.php @@ -89,7 +89,7 @@ public function __construct( $this->customerMetadata = $customerMetadata; $this->groupRepository = $groupRepository; $this->storeManager = $storeManager; - $this->scopeConfig = $scopeConfig ? $scopeConfig : ObjectManager::getInstance()->create(ScopeConfigInterface::class); + $this->scopeConfig = $scopeConfig ?: ObjectManager::getInstance()->create(ScopeConfigInterface::class); } /** @@ -181,7 +181,8 @@ private function setConfirmationValue() $isConfirmationRequired = (bool)$this->scopeConfig->getValue( AccountManagement::XML_PATH_IS_CONFIRM, ScopeInterface::SCOPE_WEBSITES, - $websiteId); + $websiteId + ); $valueText = __('Confirmation Not Required'); if ($isConfirmationRequired) { From 69151eae512a9ea89533537dac93c65585b43e6f Mon Sep 17 00:00:00 2001 From: Luke Rodgers Date: Fri, 13 Oct 2017 23:10:31 +0100 Subject: [PATCH 5/5] Reduce variable length name isConfirmationRequired -> isConfirmRequired Keep the variable name less than 20 chars --- .../Magento/Customer/Ui/Component/DataProvider/Document.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/code/Magento/Customer/Ui/Component/DataProvider/Document.php b/app/code/Magento/Customer/Ui/Component/DataProvider/Document.php index a9a5c5b17744e..6a197feb03c61 100644 --- a/app/code/Magento/Customer/Ui/Component/DataProvider/Document.php +++ b/app/code/Magento/Customer/Ui/Component/DataProvider/Document.php @@ -178,14 +178,14 @@ private function setConfirmationValue() { $value = $this->getData(self::$confirmationAttributeCode); $websiteId = $this->getData(self::$websiteIdAttributeCode) ?: $this->getData(self::$websiteAttributeCode); - $isConfirmationRequired = (bool)$this->scopeConfig->getValue( + $isConfirmRequired = (bool)$this->scopeConfig->getValue( AccountManagement::XML_PATH_IS_CONFIRM, ScopeInterface::SCOPE_WEBSITES, $websiteId ); $valueText = __('Confirmation Not Required'); - if ($isConfirmationRequired) { + if ($isConfirmRequired) { $valueText = $value === null ? __('Confirmed') : __('Confirmation Required'); }