Skip to content

Commit 353b4b6

Browse files
author
olysenko
committed
Merge remote-tracking branch 'origin/MAGETWO-91737' into chaika_august2
2 parents 7a3e795 + 0f44045 commit 353b4b6

File tree

11 files changed

+338
-120
lines changed

11 files changed

+338
-120
lines changed

app/code/Magento/Customer/Controller/Address/FormPost.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
67
namespace Magento\Customer\Controller\Address;
78

89
use Magento\Customer\Api\AddressRepositoryInterface;
@@ -197,17 +198,17 @@ public function execute()
197198
try {
198199
$address = $this->_extractAddress();
199200
$this->_addressRepository->save($address);
200-
$this->messageManager->addSuccess(__('You saved the address.'));
201+
$this->messageManager->addSuccessMessage(__('You saved the address.'));
201202
$url = $this->_buildUrl('*/*/index', ['_secure' => true]);
202203
return $this->resultRedirectFactory->create()->setUrl($this->_redirect->success($url));
203204
} catch (InputException $e) {
204-
$this->messageManager->addError($e->getMessage());
205+
$this->messageManager->addErrorMessage($e->getMessage());
205206
foreach ($e->getErrors() as $error) {
206-
$this->messageManager->addError($error->getMessage());
207+
$this->messageManager->addErrorMessage($error->getMessage());
207208
}
208209
} catch (\Exception $e) {
209210
$redirectUrl = $this->_buildUrl('*/*/index');
210-
$this->messageManager->addException($e, __('We can\'t save the address.'));
211+
$this->messageManager->addExceptionMessage($e, __('We can\'t save the address.'));
211212
}
212213

213214
$url = $redirectUrl;

app/code/Magento/Customer/Model/Metadata/Form/Text.php

Lines changed: 48 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
* Copyright © Magento, Inc. All rights reserved.
66
* See COPYING.txt for license details.
77
*/
8+
89
namespace Magento\Customer\Model\Metadata\Form;
910

11+
use Magento\Customer\Api\Data\AttributeMetadataInterface;
1012
use Magento\Framework\Api\ArrayObjectSearch;
1113

1214
class Text extends AbstractData
@@ -19,7 +21,7 @@ class Text extends AbstractData
1921
/**
2022
* @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate
2123
* @param \Psr\Log\LoggerInterface $logger
22-
* @param \Magento\Customer\Api\Data\AttributeMetadataInterface $attribute
24+
* @param AttributeMetadataInterface $attribute
2325
* @param \Magento\Framework\Locale\ResolverInterface $localeResolver
2426
* @param string $value
2527
* @param string $entityTypeCode
@@ -29,7 +31,7 @@ class Text extends AbstractData
2931
public function __construct(
3032
\Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate,
3133
\Psr\Log\LoggerInterface $logger,
32-
\Magento\Customer\Api\Data\AttributeMetadataInterface $attribute,
34+
AttributeMetadataInterface $attribute,
3335
\Magento\Framework\Locale\ResolverInterface $localeResolver,
3436
$value,
3537
$entityTypeCode,
@@ -41,15 +43,15 @@ public function __construct(
4143
}
4244

4345
/**
44-
* {@inheritdoc}
46+
* @inheritdoc
4547
*/
4648
public function extractValue(\Magento\Framework\App\RequestInterface $request)
4749
{
4850
return $this->_applyInputFilter($this->_getRequestValue($request));
4951
}
5052

5153
/**
52-
* {@inheritdoc}
54+
* @inheritdoc
5355
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
5456
* @SuppressWarnings(PHPMD.NPathComplexity)
5557
*/
@@ -72,26 +74,7 @@ public function validateValue($value)
7274
return true;
7375
}
7476

75-
// validate length
76-
$length = $this->_string->strlen(trim($value));
77-
78-
$validateRules = $attribute->getValidationRules();
79-
80-
$minTextLength = ArrayObjectSearch::getArrayElementByName(
81-
$validateRules,
82-
'min_text_length'
83-
);
84-
if ($minTextLength !== null && $length < $minTextLength) {
85-
$errors[] = __('"%1" length must be equal or greater than %2 characters.', $label, $minTextLength);
86-
}
87-
88-
$maxTextLength = ArrayObjectSearch::getArrayElementByName(
89-
$validateRules,
90-
'max_text_length'
91-
);
92-
if ($maxTextLength !== null && $length > $maxTextLength) {
93-
$errors[] = __('"%1" length must be equal or less than %2 characters.', $label, $maxTextLength);
94-
}
77+
$errors = $this->validateLength($value, $attribute, $errors);
9578

9679
$result = $this->_validateInputRule($value);
9780
if ($result !== true) {
@@ -105,26 +88,64 @@ public function validateValue($value)
10588
}
10689

10790
/**
108-
* {@inheritdoc}
91+
* @inheritdoc
10992
*/
11093
public function compactValue($value)
11194
{
11295
return $value;
11396
}
11497

11598
/**
116-
* {@inheritdoc}
99+
* @inheritdoc
117100
*/
118101
public function restoreValue($value)
119102
{
120103
return $this->compactValue($value);
121104
}
122105

123106
/**
124-
* {@inheritdoc}
107+
* @inheritdoc
125108
*/
126109
public function outputValue($format = \Magento\Customer\Model\Metadata\ElementFactory::OUTPUT_FORMAT_TEXT)
127110
{
128111
return $this->_applyOutputFilter($this->_value);
129112
}
113+
114+
/**
115+
* Length validation
116+
*
117+
* @param mixed $value
118+
* @param AttributeMetadataInterface $attribute
119+
* @param array $errors
120+
* @return array
121+
*/
122+
private function validateLength($value, AttributeMetadataInterface $attribute, array $errors): array
123+
{
124+
// validate length
125+
$label = __($attribute->getStoreLabel());
126+
127+
$length = $this->_string->strlen(trim($value));
128+
129+
$validateRules = $attribute->getValidationRules();
130+
131+
if (!empty(ArrayObjectSearch::getArrayElementByName($validateRules, 'input_validation'))) {
132+
$minTextLength = ArrayObjectSearch::getArrayElementByName(
133+
$validateRules,
134+
'min_text_length'
135+
);
136+
if ($minTextLength !== null && $length < $minTextLength) {
137+
$errors[] = __('"%1" length must be equal or greater than %2 characters.', $label, $minTextLength);
138+
}
139+
140+
$maxTextLength = ArrayObjectSearch::getArrayElementByName(
141+
$validateRules,
142+
'max_text_length'
143+
);
144+
if ($maxTextLength !== null && $length > $maxTextLength) {
145+
$errors[] = __('"%1" length must be equal or less than %2 characters.', $label, $maxTextLength);
146+
}
147+
}
148+
149+
return $errors;
150+
}
130151
}

app/code/Magento/Customer/Test/Unit/Controller/Address/FormPostTest.php

Lines changed: 45 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
67
namespace Magento\Customer\Test\Unit\Controller\Address;
78

89
use Magento\Customer\Api\AddressRepositoryInterface;
@@ -162,6 +163,9 @@ class FormPostTest extends \PHPUnit\Framework\TestCase
162163
*/
163164
private $customerAddressMapper;
164165

166+
/**
167+
* {@inheritDoc}
168+
*/
165169
protected function setUp()
166170
{
167171
$this->prepareContext();
@@ -230,7 +234,10 @@ protected function setUp()
230234
);
231235
}
232236

233-
protected function prepareContext()
237+
/**
238+
* Prepares context
239+
*/
240+
protected function prepareContext(): void
234241
{
235242
$this->context = $this->getMockBuilder(\Magento\Framework\App\Action\Context::class)
236243
->disableOriginalConstructor()
@@ -284,7 +291,10 @@ protected function prepareContext()
284291
->willReturn($this->messageManager);
285292
}
286293

287-
protected function prepareAddress()
294+
/**
295+
* Prepare address
296+
*/
297+
protected function prepareAddress(): void
288298
{
289299
$this->addressRepository = $this->getMockBuilder(\Magento\Customer\Api\AddressRepositoryInterface::class)
290300
->getMockForAbstractClass();
@@ -303,7 +313,10 @@ protected function prepareAddress()
303313
->willReturn($this->addressData);
304314
}
305315

306-
protected function prepareRegion()
316+
/**
317+
* Prepare region
318+
*/
319+
protected function prepareRegion(): void
307320
{
308321
$this->region = $this->getMockBuilder(\Magento\Directory\Model\Region::class)
309322
->disableOriginalConstructor()
@@ -335,7 +348,10 @@ protected function prepareRegion()
335348
->willReturn($this->regionData);
336349
}
337350

338-
protected function prepareForm()
351+
/**
352+
* Prepare form
353+
*/
354+
protected function prepareForm(): void
339355
{
340356
$this->form = $this->getMockBuilder(\Magento\Customer\Model\Metadata\Form::class)
341357
->disableOriginalConstructor()
@@ -346,7 +362,10 @@ protected function prepareForm()
346362
->getMock();
347363
}
348364

349-
public function testExecuteNoFormKey()
365+
/**
366+
* Test form without formKey
367+
*/
368+
public function testExecuteNoFormKey(): void
350369
{
351370
$this->formKeyValidator->expects($this->once())
352371
->method('validate')
@@ -361,7 +380,10 @@ public function testExecuteNoFormKey()
361380
$this->assertEquals($this->resultRedirect, $this->model->execute());
362381
}
363382

364-
public function testExecuteNoPostData()
383+
/**
384+
* Test executing without post data
385+
*/
386+
public function testExecuteNoPostData(): void
365387
{
366388
$postValue = 'post_value';
367389
$url = 'url';
@@ -409,10 +431,11 @@ public function testExecuteNoPostData()
409431
}
410432

411433
/**
434+
* Tests executing
435+
*
412436
* @param int $addressId
413437
* @param int $countryId
414438
* @param int $customerId
415-
* @param bool $isRegionRequired
416439
* @param int $regionId
417440
* @param string $region
418441
* @param string $regionCode
@@ -433,7 +456,7 @@ public function testExecute(
433456
$newRegionId,
434457
$newRegion,
435458
$newRegionCode
436-
) {
459+
): void {
437460
$existingAddressData = [
438461
'country_id' => $countryId,
439462
'region_id' => $regionId,
@@ -517,7 +540,8 @@ public function testExecute(
517540
->willReturnMap([
518541
[
519542
$this->regionData,
520-
$regionData, \Magento\Customer\Api\Data\RegionInterface::class,
543+
$regionData,
544+
\Magento\Customer\Api\Data\RegionInterface::class,
521545
$this->dataObjectHelper,
522546
],
523547
[
@@ -549,7 +573,7 @@ public function testExecute(
549573
->willReturnSelf();
550574

551575
$this->messageManager->expects($this->once())
552-
->method('addSuccess')
576+
->method('addSuccessMessage')
553577
->with(__('You saved the address.'))
554578
->willReturnSelf();
555579

@@ -581,7 +605,7 @@ public function testExecute(
581605
/**
582606
* @return array
583607
*/
584-
public function dataProviderTestExecute()
608+
public function dataProviderTestExecute(): array
585609
{
586610
return [
587611
[1, 1, 1, null, '', null, '', null, ''],
@@ -612,7 +636,10 @@ public function dataProviderTestExecute()
612636
];
613637
}
614638

615-
public function testExecuteInputException()
639+
/**
640+
* Tests input exception
641+
*/
642+
public function testExecuteInputException(): void
616643
{
617644
$addressId = 1;
618645
$postValue = 'post_value';
@@ -640,7 +667,7 @@ public function testExecuteInputException()
640667
->willThrowException(new InputException(__('InputException')));
641668

642669
$this->messageManager->expects($this->once())
643-
->method('addError')
670+
->method('addErrorMessage')
644671
->with('InputException')
645672
->willReturnSelf();
646673

@@ -674,7 +701,10 @@ public function testExecuteInputException()
674701
$this->assertEquals($this->resultRedirect, $this->model->execute());
675702
}
676703

677-
public function testExecuteException()
704+
/**
705+
* Tests exception
706+
*/
707+
public function testExecuteException(): void
678708
{
679709
$addressId = 1;
680710
$postValue = 'post_value';
@@ -703,7 +733,7 @@ public function testExecuteException()
703733
->willThrowException($exception);
704734

705735
$this->messageManager->expects($this->once())
706-
->method('addException')
736+
->method('addExceptionMessage')
707737
->with($exception, __('We can\'t save the address.'))
708738
->willReturnSelf();
709739

0 commit comments

Comments
 (0)