Skip to content

Commit

Permalink
Merge pull request #257 from magento-south/MAGETWO-24139
Browse files Browse the repository at this point in the history
Stories:
* MAGETWO-24139: Resolve TODO's related to Customer Service or create stories to resolve them
* MAGETWO-56007: Initialize default values in customer custom attributes metadata
* MAGETWO-56008: Moving getStoreByWebsite to Store Module
  • Loading branch information
magicbunneh authored Aug 18, 2016
2 parents 6e64d1c + 7a71c1c commit 25b6600
Show file tree
Hide file tree
Showing 14 changed files with 437 additions and 31 deletions.
10 changes: 9 additions & 1 deletion app/code/Magento/Customer/Model/Address/AbstractAddress.php
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,6 @@ public function getRegionId()
if (!$regionId) {
if (is_numeric($region)) {
$this->setData('region_id', $region);
//@TODO method unsRegion() is neither defined in abstract model nor in it's children
$this->unsRegion();
} else {
$regionModel = $this->_createRegionInstance()->loadByCode(
Expand Down Expand Up @@ -629,4 +628,13 @@ protected function _createCountryInstance()
{
return $this->_countryFactory->create();
}

/**
* Unset Region from address
* @return $this
*/
public function unsRegion()
{
return $this->unsetData("region");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Magento\Customer\Api\Data\OptionInterfaceFactory;
use Magento\Customer\Api\Data\ValidationRuleInterfaceFactory;
use Magento\Customer\Api\Data\AttributeMetadataInterfaceFactory;
use Magento\Eav\Api\Data\AttributeDefaultValueInterface;

/**
* Converter for AttributeMetadata
Expand Down Expand Up @@ -93,7 +94,13 @@ public function createMetadataAttribute($attribute)
$validationRules[] = $validationRule;
}

return $this->attributeMetadataFactory->create()->setAttributeCode($attribute->getAttributeCode())
$attributeMetaData = $this->attributeMetadataFactory->create();

if ($attributeMetaData instanceof AttributeDefaultValueInterface) {
$attributeMetaData->setDefaultValue($attribute->getDefaultValue());
}

return $attributeMetaData->setAttributeCode($attribute->getAttributeCode())
->setFrontendInput($attribute->getFrontendInput())
->setInputFilter((string)$attribute->getInputFilter())
->setStoreLabel($attribute->getStoreLabel())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Magento\Framework\Exception\LocalizedException;

/**
* @deprecated
* Customer password attribute backend
*/
class Password extends \Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend
Expand Down Expand Up @@ -62,6 +63,7 @@ public function beforeSave($object)
}

/**
* @deprecated
* @param \Magento\Framework\DataObject $object
* @return bool
*/
Expand Down
19 changes: 18 additions & 1 deletion app/code/Magento/Customer/Model/Data/AttributeMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
* Customer attribute metadata class.
*/
class AttributeMetadata extends \Magento\Framework\Api\AbstractSimpleObject implements
\Magento\Customer\Api\Data\AttributeMetadataInterface
\Magento\Customer\Api\Data\AttributeMetadataInterface,
\Magento\Eav\Api\Data\AttributeDefaultValueInterface
{
/**
* {@inheritdoc}
Expand Down Expand Up @@ -400,4 +401,20 @@ public function setIsSearchableInGrid($isSearchableInGrid)
{
return $this->setData(self::IS_SEARCHABLE_IN_GRID, $isSearchableInGrid);
}

/**
* @inheritdoc
*/
public function getDefaultValue()
{
return $this->_get(self::DEFAULT_VALUE);
}

/**
* @inheritdoc
*/
public function setDefaultValue($defaultValue)
{
return $this->setData(self::DEFAULT_VALUE, $defaultValue);
}
}
45 changes: 18 additions & 27 deletions app/code/Magento/Customer/Model/EmailNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,23 @@ class EmailNotification implements EmailNotificationInterface
const XML_PATH_CONFIRM_EMAIL_TEMPLATE = 'customer/create_account/email_confirmation_template';

const XML_PATH_CONFIRMED_EMAIL_TEMPLATE = 'customer/create_account/email_confirmed_template';

/**
* self::NEW_ACCOUNT_EMAIL_REGISTERED welcome email, when confirmation is disabled
* and password is set
* self::NEW_ACCOUNT_EMAIL_REGISTERED_NO_PASSWORD welcome email, when confirmation is disabled
* and password is not set
* self::NEW_ACCOUNT_EMAIL_CONFIRMED welcome email, when confirmation is enabled
* and password is set
* self::NEW_ACCOUNT_EMAIL_CONFIRMATION email with confirmation link
*/
const TEMPLATE_TYPES = [
self::NEW_ACCOUNT_EMAIL_REGISTERED => self::XML_PATH_REGISTER_EMAIL_TEMPLATE,
self::NEW_ACCOUNT_EMAIL_REGISTERED_NO_PASSWORD => self::XML_PATH_REGISTER_NO_PASSWORD_EMAIL_TEMPLATE,
self::NEW_ACCOUNT_EMAIL_CONFIRMED => self::XML_PATH_CONFIRMED_EMAIL_TEMPLATE,
self::NEW_ACCOUNT_EMAIL_CONFIRMATION => self::XML_PATH_CONFIRM_EMAIL_TEMPLATE,
];

/**#@-*/

/**
Expand Down Expand Up @@ -339,7 +356,7 @@ public function newAccount(
$storeId = 0,
$sendemailStoreId = null
) {
$types = $this->getTemplateTypes();
$types = self::TEMPLATE_TYPES;

if (!isset($types[$type])) {
throw new LocalizedException(__('Please correct the transactional account email type.'));
Expand All @@ -361,30 +378,4 @@ public function newAccount(
$storeId
);
}

/**
* Get template types
*
* @return array
* @todo: consider eliminating method
*/
private function getTemplateTypes()
{
/**
* self::NEW_ACCOUNT_EMAIL_REGISTERED welcome email, when confirmation is disabled
* and password is set
* self::NEW_ACCOUNT_EMAIL_REGISTERED_NO_PASSWORD welcome email, when confirmation is disabled
* and password is not set
* self::NEW_ACCOUNT_EMAIL_CONFIRMED welcome email, when confirmation is enabled
* and password is set
* self::NEW_ACCOUNT_EMAIL_CONFIRMATION email with confirmation link
*/
$types = [
self::NEW_ACCOUNT_EMAIL_REGISTERED => self::XML_PATH_REGISTER_EMAIL_TEMPLATE,
self::NEW_ACCOUNT_EMAIL_REGISTERED_NO_PASSWORD => self::XML_PATH_REGISTER_NO_PASSWORD_EMAIL_TEMPLATE,
self::NEW_ACCOUNT_EMAIL_CONFIRMED => self::XML_PATH_CONFIRMED_EMAIL_TEMPLATE,
self::NEW_ACCOUNT_EMAIL_CONFIRMATION => self::XML_PATH_CONFIRM_EMAIL_TEMPLATE,
];
return $types;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
<?php
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Customer\Test\Unit\Model;

use Magento\Customer\Api\Data\OptionInterfaceFactory;
use Magento\Customer\Api\Data\ValidationRuleInterfaceFactory;
use Magento\Customer\Api\Data\AttributeMetadataInterfaceFactory;
use Magento\Customer\Model\AttributeMetadataConverter;

/**
* Class AttributeMetadataConverterTest
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
* @package Magento\Customer\Test\Unit\Model
*/
class AttributeMetadatConverterTest extends \PHPUnit_Framework_TestCase
{
/**
* @var OptionInterfaceFactory | \PHPUnit_Framework_MockObject_MockObject
*/
private $optionFactory;

/**
* @var ValidationRuleInterfaceFactory | \PHPUnit_Framework_MockObject_MockObject
*/
private $validationRuleFactory;

/**
* @var AttributeMetadataInterfaceFactory | \PHPUnit_Framework_MockObject_MockObject
*/
private $attributeMetadataFactory;

/**
* @var \Magento\Framework\Api\DataObjectHelper | \PHPUnit_Framework_MockObject_MockObject
*/
private $dataObjectHelper;

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

/** @var \Magento\Customer\Model\Attribute | \PHPUnit_Framework_MockObject_MockObject */
private $attribute;

public function setUp()
{
$this->optionFactory = $this->getMockBuilder(OptionInterfaceFactory::class)
->setMethods(['create'])
->disableOriginalConstructor()
->getMock();
$this->validationRuleFactory = $this->getMockBuilder(ValidationRuleInterfaceFactory::class)
->setMethods(['create'])
->disableOriginalConstructor()
->getMock();
$this->attributeMetadataFactory = $this->getMockBuilder(AttributeMetadataInterfaceFactory::class)
->setMethods(['create'])
->disableOriginalConstructor()
->getMock();
$this->dataObjectHelper = $this->getMockBuilder(\Magento\Framework\Api\DataObjectHelper::class)
->disableOriginalConstructor()
->getMock();
$this->attribute = $this->getMockBuilder(\Magento\Customer\Model\Attribute::class)
->disableOriginalConstructor()
->getMock();

$this->model = new AttributeMetadataConverter(
$this->optionFactory,
$this->validationRuleFactory,
$this->attributeMetadataFactory,
$this->dataObjectHelper
);
}

/**
* @return array
*/
private function prepareValidateRules()
{
return [
'one' => 'numeric',
'two' => 'alphanumeric'
];
}

/**
* @return array
*/
private function prepareOptions()
{
return [
[
'label' => 'few_values',
'value' => [
[1], [2]
]
],
[
'label' => 'one_value',
'value' => 1
]
];
}

public function testCreateAttributeMetadataTestWithSource()
{
$validatedRules = $this->prepareValidateRules();
$options = $this->prepareOptions();
$optionDataObjectForSimpleValue1 = $this->getMockBuilder(\Magento\Customer\Model\Data\Option::class)
->disableOriginalConstructor()
->getMock();
$optionDataObjectForSimpleValue2 = $this->getMockBuilder(\Magento\Customer\Model\Data\Option::class)
->disableOriginalConstructor()
->getMock();
$optionObject1 = $this->getMock(\Magento\Customer\Api\Data\OptionInterface::class);
$optionObject2 = $this->getMock(\Magento\Customer\Api\Data\OptionInterface::class);
$this->optionFactory->expects($this->exactly(4))
->method('create')
->will(
$this->onConsecutiveCalls(
$optionDataObjectForSimpleValue2,
$optionObject1,
$optionObject2,
$optionDataObjectForSimpleValue1
)
);
$source = $this->getMockBuilder(\Magento\Eav\Model\Entity\Attribute\Source\AbstractSource::class)
->disableOriginalConstructor()
->getMock();
$source->expects($this->once())
->method('getAllOptions')
->willReturn($options);
$this->attribute->expects($this->once())
->method('usesSource')
->willReturn(true);
$this->attribute->expects($this->once())
->method('getSource')
->willReturn($source);
$optionDataObjectForSimpleValue1->expects($this->once())
->method('setValue')
->with(1);
$optionDataObjectForSimpleValue2->expects($this->once())
->method('setLabel')
->with('few_values');
$optionDataObjectForSimpleValue1->expects($this->once())
->method('setLabel')
->with('one_value');
$this->dataObjectHelper->expects($this->exactly(2))
->method('populateWithArray')
->withConsecutive(
[$optionObject1, ['1'], \Magento\Customer\Api\Data\OptionInterface::class],
[$optionObject2, ['2'], \Magento\Customer\Api\Data\OptionInterface::class]
);
$validationRule1 = $this->getMock(\Magento\Customer\Api\Data\ValidationRuleInterface::class);
$validationRule2 = $this->getMock(\Magento\Customer\Api\Data\ValidationRuleInterface::class);
$this->validationRuleFactory->expects($this->exactly(2))
->method('create')
->will($this->onConsecutiveCalls($validationRule1, $validationRule2));
$validationRule1->expects($this->once())
->method('setValue')
->with('numeric');
$validationRule1->expects($this->once())
->method('setName')
->with('one')
->willReturnSelf();
$validationRule2->expects($this->once())
->method('setValue')
->with('alphanumeric');
$validationRule2->expects($this->once())
->method('setName')
->with('two')
->willReturnSelf();
$attributeMetaData = $this->getMockBuilder(\Magento\Customer\Model\Data\AttributeMetadata::class)
->disableOriginalConstructor()
->enableProxyingToOriginalMethods()
->getMock();
$this->attribute->expects($this->once())
->method('getValidateRules')
->willReturn($validatedRules);
$this->attributeMetadataFactory->expects($this->once())
->method('create')
->willReturn($attributeMetaData);
$frontend = $this->getMockBuilder(\Magento\Eav\Model\Entity\Attribute\Frontend\AbstractFrontend::class)
->disableOriginalConstructor()
->getMock();
$this->attribute->expects($this->once())
->method('getFrontend')
->willReturn($frontend);
$optionDataObjectForSimpleValue2->expects($this->once())
->method('setOptions')
->with([$optionObject1, $optionObject2]);
$this->model->createMetadataAttribute($this->attribute);
}
}
29 changes: 29 additions & 0 deletions app/code/Magento/Eav/Api/Data/AttributeDefaultValueInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
/**
*
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Eav\Api\Data;

/**
* Interface AttributeDefaultValueInterface
* Allows to manage attribute default value through interface
* @api
* @package Magento\Eav\Api\Data
*/
interface AttributeDefaultValueInterface
{
const DEFAULT_VALUE = "default_value";

/**
* @param string $defaultValue
* @return \Magento\Framework\Api\MetadataObjectInterface
*/
public function setDefaultValue($defaultValue);

/**
* @return string
*/
public function getDefaultValue();
}
1 change: 1 addition & 0 deletions app/code/Magento/Quote/Model/Quote.php
Original file line number Diff line number Diff line change
Expand Up @@ -841,6 +841,7 @@ public function beforeSave()
* Loading quote data by customer
*
* @param \Magento\Customer\Model\Customer|int $customer
* @deprecated
* @return $this
*/
public function loadByCustomer($customer)
Expand Down
Loading

0 comments on commit 25b6600

Please sign in to comment.