Skip to content

Commit

Permalink
# This is a combination of 3 commits.
Browse files Browse the repository at this point in the history
# 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
  • Loading branch information
hatimeria-artur-jewula authored and Zefiryn committed Nov 21, 2017
1 parent a91df24 commit 5f3ef1b
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 2 deletions.
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\Sales\Model\Order\Invoice;
use Magento\Store\Api\Data\WebsiteInterface;
use Magento\Store\Model\StoreManagerInterface;
Expand Down Expand Up @@ -45,6 +47,11 @@ class DocumentTest extends \PHPUnit_Framework_TestCase
*/
private $storeManager;

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

/**
* @var Document
*/
Expand All @@ -60,11 +67,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 @@ -157,6 +167,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
Expand Down
78 changes: 77 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 ? $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,48 @@ 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);
}
}

0 comments on commit 5f3ef1b

Please sign in to comment.