Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prefix dropdown in checkout doesn't select empty value by default if required #28238

Merged
merged 6 commits into from
Aug 13, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
6 changes: 4 additions & 2 deletions app/code/Magento/Customer/Model/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,14 @@ private function prepareNamePrefixSuffixOptions($options, $isOptional = false)
if (empty($options)) {
return false;
}

$result = [];
$options = array_filter(explode(';', $options));
$options = explode(';', $options);
foreach ($options as $value) {
$value = $this->escaper->escapeHtml(trim($value));
$value = $this->escaper->escapeHtml(trim($value)) ?: ' ';
engcom-Charlie marked this conversation as resolved.
Show resolved Hide resolved
$result[$value] = $value;
}

if ($isOptional && trim(current($options))) {
$result = array_merge([' ' => ' '], $result);
engcom-Charlie marked this conversation as resolved.
Show resolved Hide resolved
}
Expand Down
156 changes: 156 additions & 0 deletions dev/tests/integration/testsuite/Magento/Customer/Model/OptionsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Magento\Customer\Model;

use Magento\Config\Model\Config\Source\Nooptreq;
use Magento\Customer\Helper\Address;
use Magento\Framework\App\Config\MutableScopeConfigInterface;
use Magento\Framework\ObjectManagerInterface;
use Magento\Store\Model\ScopeInterface;
use Magento\TestFramework\Helper\Bootstrap;
use PHPUnit\Framework\TestCase;

/**
* Test for \Magento\Customer\Model\Options.
* @magentoDbIsolation enabled
engcom-Charlie marked this conversation as resolved.
Show resolved Hide resolved
*/
class OptionsTest extends TestCase
{
private const XML_PATH_SUFFIX_SHOW = 'customer/address/suffix_show';
private const XML_PATH_SUFFIX_OPTIONS = 'customer/address/suffix_options';
private const XML_PATH_PREFIX_SHOW = 'customer/address/prefix_show';
private const XML_PATH_PREFIX_OPTIONS = 'customer/address/prefix_options';

/**
* @var Options
*/
private $model;

/**
* @var ObjectManagerInterface
*/
private $objectManager;

/**
* @inheritdoc
*/
protected function setUp(): void
{
$this->objectManager = Bootstrap::getObjectManager();
$this->model = $this->objectManager->create(Options::class);
}

/**
* Test suffix and prefix options
*
* @dataProvider optionsDataProvider
*
* @param string $optionType
* @param array $showOptionConfig
* @param array $optionValuesConfig
* @param array $expectedOptions
* @return void
*/
public function testOptions(
string $optionType,
array $showOptionConfig,
array $optionValuesConfig,
array $expectedOptions
): void {
$this->setConfig($showOptionConfig);
$this->setConfig($optionValuesConfig);

/** @var array $options */
$options = $optionType === 'prefix'
? $this->model->getNamePrefixOptions()
: $this->model->getNameSuffixOptions();

$this->assertEquals($expectedOptions, $options);
}

/**
* Set config param
*
* @param array $data
* @param string|null $scopeType
* @param string|null $scopeCode
* @return void
*/
private function setConfig(
array $data,
?string $scopeType = ScopeInterface::SCOPE_STORE,
?string $scopeCode = 'default'
): void {
$path = array_key_first($data);
$this->objectManager->get(MutableScopeConfigInterface::class)
->setValue($path, $data[$path], $scopeType, $scopeCode);
}

/**
* DataProvider for testOptions()
*
* @return array
*/
public function optionsDataProvider(): array
{
$optionPrefixName = 'prefix';
$optionSuffixName = 'suffix';
$optionValues = 'v1;v2';
$optionValuesWithBlank = ';v1;v2';
$expectedValuesWithBlank = [' ' => ' ', 'v1' => 'v1', 'v2' => 'v2'];
$expectedValues = ['v1' => 'v1', 'v2' => 'v2'];

return [
'prefix_required_with_blank_option' => [
$optionPrefixName,
[self::XML_PATH_PREFIX_SHOW => Nooptreq::VALUE_REQUIRED],
[self::XML_PATH_PREFIX_OPTIONS => $optionValuesWithBlank],
$expectedValuesWithBlank,
],
'prefix_required' => [
$optionPrefixName,
[self::XML_PATH_PREFIX_SHOW => Nooptreq::VALUE_REQUIRED],
[self::XML_PATH_PREFIX_OPTIONS => $optionValues],
$expectedValues,
],
'prefix_optional' => [
$optionPrefixName,
[self::XML_PATH_PREFIX_SHOW => Nooptreq::VALUE_OPTIONAL],
[self::XML_PATH_PREFIX_OPTIONS => $optionValues],
$expectedValuesWithBlank,
],
'suffix_optional' => [
$optionSuffixName,
[self::XML_PATH_SUFFIX_SHOW => Nooptreq::VALUE_OPTIONAL],
[self::XML_PATH_SUFFIX_OPTIONS => $optionValues],
$expectedValuesWithBlank,
],
'suffix_optional_with_blank_option' => [
$optionSuffixName,
[self::XML_PATH_SUFFIX_SHOW => Nooptreq::VALUE_OPTIONAL],
[self::XML_PATH_SUFFIX_OPTIONS => $optionValuesWithBlank],
$expectedValuesWithBlank,
],
'suffix_required_with_blank_option' => [
$optionSuffixName,
[self::XML_PATH_SUFFIX_SHOW => Nooptreq::VALUE_OPTIONAL],
[self::XML_PATH_SUFFIX_OPTIONS => $optionValuesWithBlank],
$expectedValuesWithBlank,
],
];
}

/**
* @inheritdoc
*/
protected function tearDown(): void
{
$this->objectManager->removeSharedInstance(Address::class);
}
}