Commit 9121d439 authored by Oleksandr Gotgelf's avatar Oleksandr Gotgelf
Browse files

CREF-334: Date of birth input into German format

parent 68f47021
<?php
declare(strict_types=1);
namespace Trilix\CrefoPay\Model;
use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Customer\Block\Widget\Dob;
use Magento\Customer\Model\Session;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
class CrefoPayDob
{
/** @var CustomerRepositoryInterface */
private $customerRepository;
/** @var Session */
private $customerSession;
/** @var Dob */
private $dob;
/**
* CrefoPayDob constructor.
* @param CustomerRepositoryInterface $customerRepository
* @param Session $customerSession
* @param Dob $dob
*/
public function __construct(
CustomerRepositoryInterface $customerRepository,
Session $customerSession,
Dob $dob
) {
$this->customerRepository = $customerRepository;
$this->customerSession = $customerSession;
$this->dob = $dob;
}
/**
* @return string|null
*
* @throws LocalizedException
* @throws NoSuchEntityException
*/
public function getDob()
{
$dob = null;
$customerId = $this->customerSession->getCustomerId();
if ($customerId) {
$customer = $this->customerRepository->getById($customerId);
$dob = $customer->getDob();
}
return $dob;
}
/**
* @return string
*/
public function getDateFormatForJs()
{
return $this->dob->getDateFormat();
}
}
......@@ -6,6 +6,7 @@ namespace Trilix\CrefoPay\Model\Ui\Bill;
use Magento\Checkout\Model\ConfigProviderInterface;
use Trilix\CrefoPay\Gateway\Config\Config;
use Trilix\CrefoPay\Model\CrefoPayDob;
use Trilix\CrefoPay\Model\PaymentMethodsCodesMap;
/**
......@@ -13,21 +14,23 @@ use Trilix\CrefoPay\Model\PaymentMethodsCodesMap;
*/
class ConfigProvider implements ConfigProviderInterface
{
const CODE = 'crefopay_bill';
public const CODE = 'crefopay_bill';
/**
* @var Config
*/
/** @var Config */
private $config;
/** @var CrefoPayDob */
private $crefoPayDob;
/**
* Initialize dependencies.
*
* ConfigProvider constructor.
* @param Config $config
* @param CrefoPayDob $crefoPayDob
*/
public function __construct(Config $config)
public function __construct(Config $config, CrefoPayDob $crefoPayDob)
{
$this->config = $config;
$this->crefoPayDob = $crefoPayDob;
}
/**
......@@ -45,7 +48,9 @@ class ConfigProvider implements ConfigProviderInterface
'secureFieldsUrl' => $this->config->getSecureFieldsUrl(),
'smartSignupUrl' => $this->config->getSmartSignupUrl(),
'crefoPayMethodCode' => PaymentMethodsCodesMap::getCrefoPayLibraryCode(self::CODE),
'logo' => $this->config->getLogoUrl(self::CODE)
'logo' => $this->config->getLogoUrl(self::CODE),
'dob' => $this->crefoPayDob->getDob(),
'dateFormat' => $this->crefoPayDob->getDateFormatForJs()
]
]
];
......
......@@ -6,6 +6,7 @@ namespace Trilix\CrefoPay\Model\Ui\DirectDebit;
use Magento\Checkout\Model\ConfigProviderInterface;
use Trilix\CrefoPay\Gateway\Config\Config;
use Trilix\CrefoPay\Model\CrefoPayDob;
use Trilix\CrefoPay\Model\PaymentMethodsCodesMap;
/**
......@@ -13,23 +14,25 @@ use Trilix\CrefoPay\Model\PaymentMethodsCodesMap;
*/
class ConfigProvider implements ConfigProviderInterface
{
const CODE = 'crefopay_direct_debit';
public const CODE = 'crefopay_direct_debit';
const CC_VAULT_CODE = 'crefopay_direct_debit_vault';
public const CC_VAULT_CODE = 'crefopay_direct_debit_vault';
/**
* @var Config
*/
/** @var Config */
private $config;
/** @var CrefoPayDob */
private $crefoPayDob;
/**
* Initialize dependencies.
*
* ConfigProvider constructor.
* @param Config $config
* @param CrefoPayDob $crefoPayDob
*/
public function __construct(Config $config)
public function __construct(Config $config, CrefoPayDob $crefoPayDob)
{
$this->config = $config;
$this->crefoPayDob = $crefoPayDob;
}
/**
......@@ -48,7 +51,9 @@ class ConfigProvider implements ConfigProviderInterface
'smartSignupUrl' => $this->config->getSmartSignupUrl(),
'crefoPayMethodCode' => PaymentMethodsCodesMap::getCrefoPayLibraryCode(self::CODE),
'ccVaultCode' => self::CC_VAULT_CODE,
'logo' => $this->config->getLogoUrl(self::CODE)
'logo' => $this->config->getLogoUrl(self::CODE),
'dob' => $this->crefoPayDob->getDob(),
'dateFormat' => $this->crefoPayDob->getDateFormatForJs()
]
]
];
......
......@@ -52,6 +52,6 @@ class OrderTest extends \PHPUnit\Framework\TestCase
);
$order = $uut->getOrderByIncrementId('1');
$this->assertInstanceOf(OrderInterface::class, $order);
self::assertInstanceOf(OrderInterface::class, $order);
}
}
\ No newline at end of file
}
<?php
declare(strict_types=1);
namespace Trilix\CrefoPay\Test\Unit\Model;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Magento\Customer\Model\Session;
use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Customer\Block\Widget\Dob;
use Magento\Customer\Api\Data\CustomerInterface;
use Trilix\CrefoPay\Model\CrefoPayDob;
class CrefoPayDobTest extends TestCase
{
/** @var CrefoPayDob */
private $crefoPayDob;
/** @var CustomerRepositoryInterface|MockObject */
private $customerRepository;
/** @var Session|MockObject */
private $customerSession;
/** @var Dob|MockObject */
private $dob;
public function setUp(): void
{
$this->customerRepository = $this->createMock(CustomerRepositoryInterface::class);
$this->customerSession = $this->createMock(Session::class);
$this->dob = $this->createMock(Dob::class);
$this->crefoPayDob = new CrefoPayDob(
$this->customerRepository,
$this->customerSession,
$this->dob
);
}
public function testGetDobIfCustomerIsRegistered()
{
$customer = $this->createMock(CustomerInterface::class);
$this->customerSession->expects(self::once())
->method('getCustomerId')
->willReturn(1);
$this->customerRepository->expects(self::once())
->method('getById')
->with(1)
->willReturn($customer);
$customer->expects(self::once())
->method('getDob')
->willReturn('11.11.2011');
$actualResult = $this->crefoPayDob->getDob();
self::assertEquals('11.11.2011', $actualResult);
}
public function testGetDobIfCustomerIsGuest()
{
$this->customerSession->expects(self::once())
->method('getCustomerId')
->willReturn(null);
$this->customerRepository->expects(self::never())
->method('getById');
$actualResult = $this->crefoPayDob->getDob();
self::assertEquals(null, $actualResult);
}
public function testGetDateFormatForJs()
{
$this->dob->expects(self::once())
->method('getDateFormat')
->willReturn('dd.mm.yy');
$actualResult = $this->crefoPayDob->getDateFormatForJs();
self::assertEquals('dd.mm.yy', $actualResult);
}
}
......@@ -5,6 +5,7 @@ declare(strict_types=1);
namespace Trilix\CrefoPay\Test\Unit\Model\UI\Bill;
use PHPUnit\Framework\MockObject\MockObject;
use Trilix\CrefoPay\Model\CrefoPayDob;
use Trilix\CrefoPay\Model\Ui\Bill\ConfigProvider;
use Trilix\CrefoPay\Gateway\Config\Config;
use Trilix\CrefoPay\Model\PaymentMethodsCodesMap;
......@@ -18,21 +19,32 @@ class ConfigProviderTest extends \PHPUnit\Framework\TestCase
->disableOriginalConstructor()
->getMock();
$configProvider = new ConfigProvider($config);
$crefoPayDob = $this->createMock(CrefoPayDob::class);
$config->expects($this->once())
$configProvider = new ConfigProvider($config, $crefoPayDob);
$config->expects(self::once())
->method('isActive')
->willReturn(true);
$config->expects($this->once())
$config->expects(self::once())
->method('getPublicToken')
->willReturn('publicToken');
$config->expects($this->once())
$config->expects(self::once())
->method('getSecureFieldsUrl')
->willReturn('secureFieldsUrl');
$config->expects($this->once())
$config->expects(self::once())
->method('getSmartSignupUrl')
->willReturn('smartSignupUrl');
$config->expects(self::once())
->method('getLogoUrl')
->with(ConfigProvider::CODE)
->willReturn('logoUrl');
$crefoPayDob->expects(self::once())
->method('getDob')
->willReturn('11.11.2011');
$crefoPayDob->expects(self::once())
->method('getDateFormatForJs')
->willReturn('dd.mm.yy');
$actual = $configProvider->getConfig();
$expected = [
......@@ -42,11 +54,14 @@ class ConfigProviderTest extends \PHPUnit\Framework\TestCase
'publicToken' => 'publicToken',
'secureFieldsUrl' => 'secureFieldsUrl',
'crefoPayMethodCode' => PaymentMethodsCodesMap::getCrefoPayLibraryCode(ConfigProvider::CODE),
'logo' => 'logoUrl'
'logo' => 'logoUrl',
'smartSignupUrl' => 'smartSignupUrl',
'dob' => '11.11.2011',
'dateFormat' => 'dd.mm.yy',
]
]
];
$this->assertEquals($expected, $actual);
self::assertEquals($expected, $actual);
}
}
......@@ -20,16 +20,19 @@ class ConfigProviderTest extends \PHPUnit\Framework\TestCase
$configProvider = new ConfigProvider($config);
$config->expects($this->once())
$config->expects(self::once())
->method('isActive')
->willReturn(true);
$config->expects($this->once())
$config->expects(self::once())
->method('getPublicToken')
->willReturn('publicToken');
$config->expects($this->once())
$config->expects(self::once())
->method('getSecureFieldsUrl')
->willReturn('secureFieldsUrl');
$config->expects($this->once())
$config->expects(self::once())
->method('getSmartSignupUrl')
->willReturn('smartSignupUrl');
$config->expects(self::once())
->method('getLogoUrl')
->with(ConfigProvider::CODE)
->willReturn('logoUrl');
......@@ -42,11 +45,12 @@ class ConfigProviderTest extends \PHPUnit\Framework\TestCase
'publicToken' => 'publicToken',
'secureFieldsUrl' => 'secureFieldsUrl',
'crefoPayMethodCode' => PaymentMethodsCodesMap::getCrefoPayLibraryCode(ConfigProvider::CODE),
'logo' => 'logoUrl'
'logo' => 'logoUrl',
'smartSignupUrl' => 'smartSignupUrl'
]
]
];
$this->assertEquals($expected, $actual);
self::assertEquals($expected, $actual);
}
}
......@@ -29,6 +29,9 @@ class ConfigProviderTest extends \PHPUnit\Framework\TestCase
$config->expects($this->once())
->method('getSecureFieldsUrl')
->willReturn('secureFieldsUrl');
$config->expects(self::once())
->method('getSmartSignupUrl')
->willReturn('smartSignupUrl');
$config->expects($this->once())
->method('getLogoUrl')
->with(ConfigProvider::CODE)
......@@ -42,7 +45,8 @@ class ConfigProviderTest extends \PHPUnit\Framework\TestCase
'publicToken' => 'publicToken',
'secureFieldsUrl' => 'secureFieldsUrl',
'crefoPayMethodCode' => PaymentMethodsCodesMap::getCrefoPayLibraryCode(ConfigProvider::CODE),
'logo' => 'logoUrl'
'logo' => 'logoUrl',
'smartSignupUrl' => 'smartSignupUrl'
]
]
];
......
......@@ -29,6 +29,9 @@ class ConfigProviderTest extends \PHPUnit\Framework\TestCase
$config->expects($this->once())
->method('getSecureFieldsUrl')
->willReturn('secureFieldsUrl');
$config->expects(self::once())
->method('getSmartSignupUrl')
->willReturn('smartSignupUrl');
$config->expects($this->once())
->method('getLogoUrl')
->with(ConfigProvider::CODE)
......@@ -43,7 +46,8 @@ class ConfigProviderTest extends \PHPUnit\Framework\TestCase
'secureFieldsUrl' => 'secureFieldsUrl',
'crefoPayMethodCode' => PaymentMethodsCodesMap::getCrefoPayLibraryCode(ConfigProvider::CODE),
'ccVaultCode' => ConfigProvider::CC_VAULT_CODE,
'logo' => 'logoUrl'
'logo' => 'logoUrl',
'smartSignupUrl' => 'smartSignupUrl'
]
]
];
......
......@@ -5,6 +5,7 @@ declare(strict_types=1);
namespace Trilix\CrefoPay\Test\Unit\Model\UI\DirectDebit;
use PHPUnit\Framework\MockObject\MockObject;
use Trilix\CrefoPay\Model\CrefoPayDob;
use Trilix\CrefoPay\Model\Ui\DirectDebit\ConfigProvider;
use Trilix\CrefoPay\Gateway\Config\Config;
use Trilix\CrefoPay\Model\PaymentMethodsCodesMap;
......@@ -18,21 +19,32 @@ class ConfigProviderTest extends \PHPUnit\Framework\TestCase
->disableOriginalConstructor()
->getMock();
$configProvider = new ConfigProvider($config);
$crefoPayDob = $this->createMock(CrefoPayDob::class);
$config->expects($this->once())
$configProvider = new ConfigProvider($config, $crefoPayDob);
$config->expects(self::once())
->method('isActive')
->willReturn(true);
$config->expects($this->once())
$config->expects(self::once())
->method('getPublicToken')
->willReturn('publicToken');
$config->expects($this->once())
$config->expects(self::once())
->method('getSecureFieldsUrl')
->willReturn('secureFieldsUrl');
$config->expects($this->once())
$config->expects(self::once())
->method('getSmartSignupUrl')
->willReturn('smartSignupUrl');
$config->expects(self::once())
->method('getLogoUrl')
->with(ConfigProvider::CODE)
->willReturn('logoUrl');
$crefoPayDob->expects(self::once())
->method('getDob')
->willReturn('11.11.2011');
$crefoPayDob->expects(self::once())
->method('getDateFormatForJs')
->willReturn('dd.mm.yy');
$actual = $configProvider->getConfig();
$expected = [
......@@ -43,11 +55,14 @@ class ConfigProviderTest extends \PHPUnit\Framework\TestCase
'secureFieldsUrl' => 'secureFieldsUrl',
'crefoPayMethodCode' => PaymentMethodsCodesMap::getCrefoPayLibraryCode(ConfigProvider::CODE),
'ccVaultCode' => ConfigProvider::CC_VAULT_CODE,
'logo' => 'logoUrl'
'logo' => 'logoUrl',
'smartSignupUrl' => 'smartSignupUrl',
'dob' => '11.11.2011',
'dateFormat' => 'dd.mm.yy',
]
]
];
$this->assertEquals($expected, $actual);
self::assertEquals($expected, $actual);
}
}
......@@ -29,6 +29,9 @@ class ConfigProviderTest extends \PHPUnit\Framework\TestCase
$config->expects($this->once())
->method('getSecureFieldsUrl')
->willReturn('secureFieldsUrl');
$config->expects(self::once())
->method('getSmartSignupUrl')
->willReturn('smartSignupUrl');
$config->expects($this->once())
->method('getLogoUrl')
->with(ConfigProvider::CODE)
......@@ -42,7 +45,8 @@ class ConfigProviderTest extends \PHPUnit\Framework\TestCase
'publicToken' => 'publicToken',
'secureFieldsUrl' => 'secureFieldsUrl',
'crefoPayMethodCode' => PaymentMethodsCodesMap::getCrefoPayLibraryCode(ConfigProvider::CODE),
'logo' => 'logoUrl'
'logo' => 'logoUrl',
'smartSignupUrl' => 'smartSignupUrl'
]
]
];
......
......@@ -12,7 +12,6 @@ define(
'use strict';
return Component.extend({
dob: ko.observable(customerData.get('crefopay')().dob),
gender: ko.observable(customerData.get('crefopay')().gender),
defaults: {
......@@ -27,7 +26,7 @@ define(
return {
'method': this.getCode(),
'additional_data': {
'dateOfBirth': this.dob(),
'dateOfBirth': $('#crefopay-bill-dob-value').val(),
'salutation': this.gender()
}
};
......@@ -54,15 +53,27 @@ define(
initializeDobField: function () {
$('#crefopay-bill-dob').calendar({
dateFormat: window.checkoutConfig.payment[this.getCode()].dateFormat,
altField: '#crefopay-bill-dob-value',
altFormat: 'yy-mm-dd',
changeMonth: true,
changeYear: true,
showButtonPanel: true,
yearRange: "-120y:c+nn",
currentText: 'Go Today',
closeText: 'Close',
dateFormat: 'yyyy-mm-dd',
showWeek: true
showWeek: true,
});
$('#crefopay-bill-dob').datepicker('setDate', this.getCustomerDob());
},
getCustomerDob: function () {
let dob = null;
let registeredCustomerDob = window.checkoutConfig.payment[this.getCode()].dob;
if (registeredCustomerDob) {
dob = registeredCustomerDob;
}
return dob;
}
});
}
......
......@@ -13,7 +13,6 @@ define(
'use strict';
return Component.extend({
dob: ko.observable(customerData.get('crefopay')().dob),
gender: ko.observable(customerData.get('crefopay')().gender),
defaults: {
......@@ -54,7 +53,7 @@ define(
'method': this.getCode(),
'additional_data': {
'paymentInstrumentId': this.paymentInstrumentId,
'dateOfBirth': this.dob(),
'dateOfBirth': $('#crefopay-dd-dob-value').val(),
'salutation': this.gender()
}
};
......@@ -93,15 +92,27 @@ define(
initializeDobField: function () {
$('#crefopay-dd-dob').calendar({
dateFormat: window.checkoutConfig.payment[this.getCode()].dateFormat,
altField: '#crefopay-dd-dob-value',
altFormat: 'yy-mm-dd',
changeMonth: true,
changeYear: true,
showButtonPanel: true,
yearRange: "-120y:c+nn",
currentText: 'Go Today',
closeText: 'Close',
dateFormat: 'yyyy-mm-dd',
showWeek: true
showWeek: true,
});
$('#crefopay-dd-dob').datepicker('setDate', this.getCustomerDob());
},
getCustomerDob: function () {
let dob = null;
let registeredCustomerDob = window.checkoutConfig.payment[this.getCode()].dob;
if (registeredCustomerDob) {
dob = registeredCustomerDob;
}
return dob;
}
});
}
......
......@@ -52,7 +52,8 @@
</label>
<div class="control customer-dob">
<input type="text" class="input-text" id="crefopay-bill-dob"
style="width: auto" name="payment[dob]" data-bind="value: dob"/>
style="width: auto" name="payment[dob]"/>
<input type="hidden" class="date" id="crefopay-bill-dob-value" />
<button type="button" class="ui-datepicker-trigger v-middle">
<span>Select Date</span>
</button>
......
......@@ -53,7 +53,8 @@
</label>
<div class="control customer-dob">
<input type="text" class="input-text" id="crefopay-dd-dob"
style="width: auto" name="payment[dob]" data-bind="value: dob"/>
style="width: auto" name="payment[dob]"/>
<input type="hidden" class="date" id="crefopay-dd-dob-value" />
<button type="button" class="ui-datepicker-trigger v-middle">
<span>Select Date</span>