Skip to content
Open
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
16 changes: 12 additions & 4 deletions app/code/Magento/Customer/Block/Widget/Name.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,12 @@ public function getPrefixOptions()
if ($this->getObject() && !empty($prefixOptions)) {
$prefixOption = $this->getObject()->getPrefix();
$oldPrefix = $this->escapeHtml(trim($prefixOption ?? ''));
if ($prefixOption !== null && !isset($prefixOptions[$oldPrefix]) && !isset($prefixOptions[$prefixOption])) {
$prefixOptions[$oldPrefix] = $oldPrefix;
// Options are a numerically indexed list of values; check membership by value.
if ($prefixOption !== null
&& !in_array($oldPrefix, $prefixOptions, true)
&& !in_array((string)$prefixOption, $prefixOptions, true)
) {
$prefixOptions[] = $oldPrefix;
}
}
return $prefixOptions;
Expand Down Expand Up @@ -166,8 +170,12 @@ public function getSuffixOptions()
if ($this->getObject() && !empty($suffixOptions)) {
$suffixOption = $this->getObject()->getSuffix();
$oldSuffix = $this->escapeHtml(trim($suffixOption ?? ''));
if ($suffixOption !== null && !isset($suffixOptions[$oldSuffix]) && !isset($suffixOptions[$suffixOption])) {
$suffixOptions[$oldSuffix] = $oldSuffix;
// Options are a numerically indexed list of values; check membership by value.
if ($suffixOption !== null
&& !in_array($oldSuffix, $suffixOptions, true)
&& !in_array((string)$suffixOption, $suffixOptions, true)
) {
$suffixOptions[] = $oldSuffix;
}
}
return $suffixOptions;
Expand Down
61 changes: 55 additions & 6 deletions app/code/Magento/Customer/Test/Unit/Block/Widget/NameTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ public function testIsSuffixRequired(): void
/**
* @return void
*/
public function testGetPrefixOptionsNotEmpty(): void
public function testGetPrefixOptionsAppendsMissingValue(): void
{
/**
* Added some padding so that the trim() call on Customer::getPrefix() will remove it. Also added
Expand All @@ -269,11 +269,12 @@ public function testGetPrefixOptionsNotEmpty(): void

$this->_block->setObject($customer);

$prefixOptions = ['Mrs' => 'Mrs', 'Ms' => 'Ms', 'Miss' => 'Miss'];
// Numerically indexed list matching Options::prepareNamePrefixSuffixOptions()
$prefixOptions = ['Mrs', 'Ms', 'Miss'];

$prefix = '<' . self::PREFIX . '>';
$expectedOptions = $prefixOptions;
$expectedOptions[$prefix] = $prefix;
$expectedOptions[] = $prefix;

$this->_options->expects(
$this->once()
Expand All @@ -287,6 +288,30 @@ public function testGetPrefixOptionsNotEmpty(): void
$this->assertSame($expectedOptions, $this->_block->getPrefixOptions());
}

/**
* When the saved prefix is already present in the options list, do not append a duplicate entry.
*
* @return void
*/
public function testGetPrefixOptionsDoesNotDuplicateExistingValue(): void
{
$customer = $this->createMock(CustomerInterface::class);
$customer->expects($this->once())->method('getPrefix')->willReturn(self::PREFIX);
$this->_block->setObject($customer);

$prefixOptions = [' ', 'Mr', 'Mrs', 'Ms'];

$this->_options->expects($this->once())
->method('getNamePrefixOptions')
->willReturn($prefixOptions);
$this->_escaper->expects($this->once())
->method('escapeHtml')
->with(self::PREFIX)
->willReturn(self::PREFIX);

$this->assertSame($prefixOptions, $this->_block->getPrefixOptions());
}

/**
* @return void
*/
Expand All @@ -311,7 +336,7 @@ public function testGetPrefixOptionsEmpty(): void
/**
* @return void
*/
public function testGetSuffixOptionsNotEmpty(): void
public function testGetSuffixOptionsAppendsMissingValue(): void
{
/**
* Added padding and special characters to show that trim() works on Customer::getSuffix() and that
Expand All @@ -323,11 +348,11 @@ public function testGetSuffixOptionsNotEmpty(): void
$customer->expects($this->once())->method('getSuffix')->willReturn(' <' . self::SUFFIX . '> ');
$this->_block->setObject($customer);

$suffixOptions = ['Sr' => 'Sr'];
$suffixOptions = ['Sr'];

$suffix = '&lt;' . self::SUFFIX . '&gt;';
$expectedOptions = $suffixOptions;
$expectedOptions[$suffix] = $suffix;
$expectedOptions[] = $suffix;

$this->_options->expects(
$this->once()
Expand All @@ -341,6 +366,30 @@ public function testGetSuffixOptionsNotEmpty(): void
$this->assertSame($expectedOptions, $this->_block->getSuffixOptions());
}

/**
* When the saved suffix is already present in the options list, do not append a duplicate entry.
*
* @return void
*/
public function testGetSuffixOptionsDoesNotDuplicateExistingValue(): void
{
$customer = $this->createMock(CustomerInterface::class);
$customer->expects($this->once())->method('getSuffix')->willReturn(self::SUFFIX);
$this->_block->setObject($customer);

$suffixOptions = [' ', 'Jr', 'Sr'];

$this->_options->expects($this->once())
->method('getNameSuffixOptions')
->willReturn($suffixOptions);
$this->_escaper->expects($this->once())
->method('escapeHtml')
->with(self::SUFFIX)
->willReturn(self::SUFFIX);

$this->assertSame($suffixOptions, $this->_block->getSuffixOptions());
}

/**
* @return void
*/
Expand Down