Skip to content

Commit

Permalink
MAGETWO-57835: [Github] Cannot save customer dob attribute if admin i…
Browse files Browse the repository at this point in the history
…nterface locale is en_GB #6323

- MAGETWO-70327
- functional test
  • Loading branch information
vgoncharenko committed Jun 29, 2017
1 parent 009365d commit 75c9faf
Show file tree
Hide file tree
Showing 12 changed files with 194 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@
<settings>
<timezone>false</timezone>
<dateFormat>MMM d, y</dateFormat>
<skipTime>true</skipTime>
<filter>dateRange</filter>
<dataType>date</dataType>
<label translate="true">Date of Birth</label>
Expand Down
6 changes: 5 additions & 1 deletion app/code/Magento/Ui/Component/Filters/Type/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Ui\Component\Filters\Type;

use Magento\Ui\Component\Form\Element\DataType\Date as DataTypeDate;
Expand Down Expand Up @@ -79,7 +80,10 @@ protected function applyFilter()

if (is_array($value)) {
if (isset($value['from'])) {
$this->applyFilterByType('gteq', $this->wrappedComponent->convertDate($value['from']));
$this->applyFilterByType(
'gteq',
$this->wrappedComponent->convertDate($value['from'], 0, 0, 0, $this->getData('config/skipTime'))
);
}

if (isset($value['to'])) {
Expand Down
6 changes: 5 additions & 1 deletion app/code/Magento/Ui/Component/Form/Element/DataType/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,10 @@ public function getComponentName()
* @param int $hour
* @param int $minute
* @param int $second
* @param bool $skipTime
* @return \DateTime|null
*/
public function convertDate($date, $hour = 0, $minute = 0, $second = 0)
public function convertDate($date, $hour = 0, $minute = 0, $second = 0, $skipTime = false)
{
try {
$dateObj = $this->localeDate->date(
Expand All @@ -116,6 +117,9 @@ public function convertDate($date, $hour = 0, $minute = 0, $second = 0)
$dateObj->setTime($hour, $minute, $second);
//convert store date to default date in UTC timezone without DST
$dateObj->setTimezone(new \DateTimeZone('UTC'));
if ($skipTime) {
$dateObj->setTime(0, 0, 0);
}
return $dateObj;
} catch (\Exception $e) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@
<item name="dateFormat" type="string" xsi:type="xpath">settings/dateFormat</item>
<item name="timeFormat" type="string" xsi:type="xpath">settings/timeFormat</item>
<item name="timezone" type="string" xsi:type="xpath">settings/timezone</item>
<item name="skipTime" type="boolean" xsi:type="xpath">settings/skipTime</item>
<item name="editor" xsi:type="array">
<item name="editorType" type="string" xsi:type="xpath">settings/editor/editorType</item>
<item name="validation" type="item" xsi:type="converter">settings/editor/validation</item>
Expand Down Expand Up @@ -239,6 +240,7 @@
<item name="showsTime" type="boolean" xsi:type="xpath">settings/showsTime</item>
<item name="dateFormat" type="string" xsi:type="xpath">settings/dateFormat</item>
<item name="timeFormat" type="string" xsi:type="xpath">settings/timeFormat</item>
<item name="skipTime" type="string" xsi:type="xpath">settings/skipTime</item>
</item>
</argument>
</schema>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,13 @@
</xs:annotation>
</xs:element>
<xs:element name="timeFormat" type="xs:string"/>
<xs:element name="skipTime" type="xs:boolean">
<xs:annotation>
<xs:documentation>
For the Date column: If time not important for filtering.
</xs:documentation>
</xs:annotation>
</xs:element>
<xs:element name="timezone" type="xs:boolean">
<xs:annotation>
<xs:documentation>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,13 @@ class CustomerGrid extends DataGrid
'entity_id_to' => [
'selector' => '[name="entity_id[to]"]',
],
'dob_from' => [
'selector' => '[name="dob[from]"]',
'input' => 'datepicker',
],
'dob_to' => [
'selector' => '[name="dob[to]"]',
'input' => 'datepicker',
],
];
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,42 @@ class AssertCustomerForm extends AbstractConstraint
'group_id'
];

/**
* Locale map.
*
* @var array
*/
private $localeMap = [
'en_GB' => 'd/m/Y'
];

/**
* Format date for current locale.
*
* @var string
*/
private $localeFormat = 'm/d/Y';

/**
* Assert that displayed customer data on edit page(backend) equals passed from fixture.
*
* @param Customer $customer
* @param CustomerIndex $pageCustomerIndex
* @param CustomerIndexEdit $pageCustomerIndexEdit
* @param Address $address[optional]
* @param Address $address [optional]
* @param string $locale
* @return void
*/
public function processAssert(
Customer $customer,
CustomerIndex $pageCustomerIndex,
CustomerIndexEdit $pageCustomerIndexEdit,
Address $address = null
Address $address = null,
$locale = ''
) {
$this->localeFormat = '' !== $locale && isset($this->localeMap[$locale])
? $this->localeMap[$locale]
: $this->localeFormat;
$data = [];
$filter = [];

Expand All @@ -60,6 +81,9 @@ public function processAssert(
} else {
$data['addresses'] = [];
}
if (isset($data['customer']['dob'])) {
$data['customer']['dob'] = date($this->localeFormat, strtotime($data['customer']['dob']));
}
$filter['email'] = $data['customer']['email'];

$pageCustomerIndex->open();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,34 +26,59 @@ class AssertCustomerInGrid extends AbstractConstraint
* @param Customer $customer
* @param CustomerIndex $pageCustomerIndex
* @return void
*
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
public function processAssert(
Customer $customer,
CustomerIndex $pageCustomerIndex
) {
$customer = $customer->getData();
$name = (isset($customer['prefix']) ? $customer['prefix'] . ' ' : '')
. $customer['firstname']
. (isset($customer['middlename']) ? ' ' . $customer['middlename'] : '')
. ' ' . $customer['lastname']
. (isset($customer['suffix']) ? ' ' . $customer['suffix'] : '');
$customerData = $customer->getData();
$name = (isset($customerData['prefix']) ? $customerData['prefix'] . ' ' : '')
. $customerData['firstname']
. (isset($customerData['middlename']) ? ' ' . $customerData['middlename'] : '')
. ' ' . $customerData['lastname']
. (isset($customerData['suffix']) ? ' ' . $customerData['suffix'] : '');
$filter = [
'name' => $name,
'email' => $customer['email'],
'email' => $customerData['email'],
];
$errorMessage = 'Customer with '
. 'name \'' . $filter['name'] . '\', '
. 'email \'' . $filter['email'] . '\'';

if ($customer->hasData('dob')) {
$filter['dob_from'] = $customer->getData('dob');
$filter['dob_to'] = $customer->getData('dob');
}

$pageCustomerIndex->open();
$pageCustomerIndex->getCustomerGridBlock()->isRowVisible($filter);
if ($customer->hasData('dob')) {
unset($filter['dob_from']);
unset($filter['dob_to']);
$filter['dob'] = $this->prepareDob($customer->getData('dob'));
$errorMessage .= ', dob \'' . $filter['dob'] . '\' ';
}

$errorMessage .= 'is absent in Customer grid.';

\PHPUnit_Framework_Assert::assertTrue(
$pageCustomerIndex->getCustomerGridBlock()->isRowVisible($filter),
'Customer with '
. 'name \'' . $filter['name'] . '\', '
. 'email \'' . $filter['email'] . '\' '
. 'is absent in Customer grid.'
$pageCustomerIndex->getCustomerGridBlock()->isRowVisible($filter, false),
$errorMessage
);
}

/**
* Prepare dob string to grid date format.
*
* @param string $date
* @return false|string
*/
private function prepareDob($date)
{
return date('M d, Y', strtotime($date));
}

/**
* Text success exist Customer in grid
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
use Magento\Customer\Test\Fixture\Customer;
use Magento\Customer\Test\Page\Adminhtml\CustomerIndex;
use Magento\Customer\Test\Page\Adminhtml\CustomerIndexNew;
use Magento\User\Test\Fixture\User;
use Magento\User\Test\Page\Adminhtml\UserEdit;
use Magento\User\Test\Page\Adminhtml\UserIndex;

/**
* Steps:
Expand Down Expand Up @@ -67,21 +70,45 @@ class CreateCustomerBackendEntityTest extends Injectable
/** @var FixtureFactory */
private $fixtureFactory;

/**
* @var UserEdit
*/
private $userEdit;

/**
* @var UserIndex
*/
private $userIndex;

/**
* Array of steps.
*
* @var array
*/
private $steps;

/**
* Inject customer pages.
*
* @param CustomerIndex $pageCustomerIndex
* @param CustomerIndexNew $pageCustomerIndexNew
* @param FixtureFactory $fixtureFactory
* @param UserEdit $userEdit
* @param UserIndex $userIndex
* @return void
*/
public function __inject(
CustomerIndex $pageCustomerIndex,
CustomerIndexNew $pageCustomerIndexNew,
\Magento\Mtf\Fixture\FixtureFactory $fixtureFactory
\Magento\Mtf\Fixture\FixtureFactory $fixtureFactory,
UserEdit $userEdit,
UserIndex $userIndex
) {
$this->pageCustomerIndex = $pageCustomerIndex;
$this->pageCustomerIndexNew = $pageCustomerIndexNew;
$this->fixtureFactory = $fixtureFactory;
$this->userEdit = $userEdit;
$this->userIndex = $userIndex;
}

/**
Expand All @@ -90,6 +117,8 @@ public function __inject(
* @param Customer $customer
* @param string $customerAction
* @param Address $address
* @param array $steps
* @param array $beforeActionCallback
* @return void
*/
public function test(
Expand All @@ -99,6 +128,7 @@ public function test(
array $steps = [],
array $beforeActionCallback = []
) {
$this->steps = $steps;
///Process initialize steps
foreach ($steps as $methodName => $stepData) {
if (method_exists($this, $methodName)) {
Expand Down Expand Up @@ -229,4 +259,49 @@ protected function configureAllowedCountries(array $countryList = [])
];
}
}

/**
* Change Admin locale.
*
* @param array $userData
*/
private function changeAdminLocale(array $userData)
{
/** @var User $adminUser */
$adminUser = $this->fixtureFactory->createByCode('user', ['data' => $userData]);
$this->userIndex->open();
$this->userIndex->getUserGrid()->searchAndOpen(['username' => $adminUser->getUsername()]);
$this->userEdit->getUserForm()->fill($adminUser);
$this->userEdit->getPageActions()->save();
}

/**
* Revert Admin locale.
*
* @param array $userData
*/
private function changeAdminLocaleRollback(array $userData)
{
/** @var User $defaultAdminUser */
$defaultAdminUser = $this->fixtureFactory->createByCode('user');
$adminUserData = $defaultAdminUser->getData();
unset($adminUserData['user_id']);
$defaultAdminUser = $this->fixtureFactory->createByCode('user', ['data' => $adminUserData]);
$this->userIndex->open();
$this->userIndex->getUserGrid()->searchAndOpen(['username' => $defaultAdminUser->getUsername()]);
$this->userEdit->getUserForm()->fill($defaultAdminUser);
$this->userEdit->getPageActions()->save();
}

/**
* @inheritdoc
*/
protected function tearDown()
{
foreach ($this->steps as $key => $stepData) {
if (method_exists($this, $key . 'Rollback')) {
call_user_func_array([$this, $key . 'Rollback'], $stepData);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -159,5 +159,27 @@
</data>
<constraint name="Magento\Customer\Test\Constraint\AssertCustomerSuccessSaveMessage" />
</variation>
<variation name="CreateCustomerBackendEntityTestVariation11" summary="Create customer if admin interface locale is en_GB.">
<data name="customerAction" xsi:type="string">save</data>
<data name="customer/data/website_id" xsi:type="string">Main Website</data>
<data name="customer/data/group_id/dataset" xsi:type="string">General</data>
<data name="customer/data/firstname" xsi:type="string">John%isolation%</data>
<data name="customer/data/lastname" xsi:type="string">Doe%isolation%</data>
<data name="customer/data/email" xsi:type="string">JohnDoe%isolation%@example.com</data>
<data name="customer/data/dob" xsi:type="string">03/29/2004</data>
<data name="steps" xsi:type="array">
<item name="changeAdminLocale" xsi:type="array">
<item name="data" xsi:type="array">
<item name="username" xsi:type="string">admin</item>
<item name="interface_locale" xsi:type="string">English (United Kingdom)</item>
<item name="current_password" xsi:type="string">123123q</item>
</item>
</item>
</data>
<data name="locale" xsi:type="string">en_GB</data>
<constraint name="Magento\Customer\Test\Constraint\AssertCustomerSuccessSaveMessage" />
<constraint name="Magento\Customer\Test\Constraint\AssertCustomerInGrid" />
<constraint name="Magento\Customer\Test\Constraint\AssertCustomerForm" />
</variation>
</testCase>
</config>
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
<email />
<password />
<password_confirmation />
<interface_locale>
<input>select</input>
</interface_locale>
<current_password />
<is_active>
<input>select</input>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<field name="password" xsi:type="string">123123q</field>
<field name="password_confirmation" xsi:type="string">123123q</field>
<field name="current_password" xsi:type="string">%current_password%</field>
<field name="interface_locale" xsi:type="string">English (United States)</field>
</dataset>

<dataset name="custom_admin">
Expand Down

0 comments on commit 75c9faf

Please sign in to comment.