Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#10765 add confirmation and lock expires data to customer grid CSV export #10915

Merged
merged 4 commits into from
Nov 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -44,6 +46,11 @@ class DocumentTest extends \PHPUnit\Framework\TestCase
*/
private $storeManager;

/**
* @var ScopeConfigInterface|MockObject
*/
private $scopeConfig;

/**
* @var Document
*/
Expand All @@ -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
);
}

Expand Down Expand Up @@ -156,6 +166,41 @@ 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
Expand Down
79 changes: 78 additions & 1 deletion app/code/Magento/Customer/Ui/Component/DataProvider/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@
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\Framework\App\ObjectManager;
use Magento\Store\Model\ScopeInterface;
use Magento\Store\Model\StoreManagerInterface;

/**
Expand All @@ -31,6 +35,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
*/
Expand All @@ -46,23 +65,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 = null
) {
parent::__construct($attributeValueFactory);
$this->customerMetadata = $customerMetadata;
$this->groupRepository = $groupRepository;
$this->storeManager = $storeManager;
$this->scopeConfig = $scopeConfig ?: ObjectManager::getInstance()->create(ScopeConfigInterface::class);
}

/**
Expand All @@ -80,6 +107,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);
}
Expand Down Expand Up @@ -133,5 +166,49 @@ 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 = __('Confirmation Not Required');
if ($isConfirmationRequired) {
$valueText = $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);
}
}