123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Customer\Test\Unit\Model\Customer;
- use Magento\Customer\Api\CustomerMetadataInterface;
- use Magento\Customer\Model\Config\Share;
- use Magento\Customer\Model\ResourceModel\Address\Attribute\Source\CountryWithWebsites;
- use Magento\Customer\Model\ResourceModel\Customer\CollectionFactory;
- use Magento\Eav\Model\Config;
- use Magento\Eav\Model\Entity\Attribute\AbstractAttribute;
- use Magento\Eav\Model\Entity\Type;
- use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
- use Magento\Ui\Component\Form\Field;
- use Magento\Ui\DataProvider\EavValidationRules;
- /**
- * Class DataProviderTest
- *
- * Test for class \Magento\Customer\Model\Customer\DataProvider
- *
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- class DataProviderTest extends \PHPUnit\Framework\TestCase
- {
- const ATTRIBUTE_CODE = 'test-code';
- const OPTIONS_RESULT = 'test-options';
- /**
- * @var Config|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $eavConfigMock;
- /**
- * @var CollectionFactory|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $customerCollectionFactoryMock;
- /**
- * @var EavValidationRules|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $eavValidationRulesMock;
- /**
- * @var \Magento\Framework\Session\SessionManagerInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $sessionMock;
- /**
- * @var \Magento\Customer\Model\FileProcessor|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $fileProcessor;
- /**
- * @var \Magento\Customer\Model\FileUploaderDataResolver|\PHPUnit_Framework_MockObject_MockObject
- */
- private $fileUploaderDataResolver;
- /**
- * Set up
- *
- * @return void
- */
- protected function setUp()
- {
- $this->eavConfigMock = $this->getMockBuilder(\Magento\Eav\Model\Config::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->customerCollectionFactoryMock = $this->createPartialMock(
- \Magento\Customer\Model\ResourceModel\Customer\CollectionFactory::class,
- ['create']
- );
- $this->eavValidationRulesMock = $this
- ->getMockBuilder(\Magento\Ui\DataProvider\EavValidationRules::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->sessionMock = $this
- ->getMockBuilder(\Magento\Framework\Session\SessionManagerInterface::class)
- ->setMethods(['getCustomerFormData', 'unsCustomerFormData'])
- ->getMockForAbstractClass();
- $this->fileProcessor = $this->getMockBuilder(\Magento\Customer\Model\FileProcessor::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->fileUploaderDataResolver = $this->getMockBuilder(\Magento\Customer\Model\FileUploaderDataResolver::class)
- ->disableOriginalConstructor()
- ->setMethods(['overrideFileUploaderMetadata', 'overrideFileUploaderData'])
- ->getMock();
- }
- /**
- * Run test getAttributesMeta method
- *
- * @param array $expected
- * @return void
- *
- * @dataProvider getAttributesMetaDataProvider
- */
- public function testGetAttributesMetaWithOptions(array $expected)
- {
- $helper = new ObjectManager($this);
- /** @var \Magento\Customer\Model\Customer\DataProvider $dataProvider */
- $dataProvider = $helper->getObject(
- \Magento\Customer\Model\Customer\DataProvider::class,
- [
- 'name' => 'test-name',
- 'primaryFieldName' => 'primary-field-name',
- 'requestFieldName' => 'request-field-name',
- 'eavValidationRules' => $this->eavValidationRulesMock,
- 'customerCollectionFactory' => $this->getCustomerCollectionFactoryMock(),
- 'eavConfig' => $this->getEavConfigMock(),
- 'fileUploaderDataResolver' => $this->fileUploaderDataResolver
- ]
- );
- $meta = $dataProvider->getMeta();
- $this->assertNotEmpty($meta);
- $this->assertEquals($expected, $meta);
- }
- /**
- * Data provider for testGetAttributesMeta
- *
- * @return array
- * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
- */
- public function getAttributesMetaDataProvider()
- {
- return [
- [
- 'expected' => [
- 'customer' => [
- 'children' => [
- self::ATTRIBUTE_CODE => [
- 'arguments' => [
- 'data' => [
- 'config' => [
- 'dataType' => 'frontend_input',
- 'formElement' => 'frontend_input',
- 'options' => 'test-options',
- 'visible' => null,
- 'required' => 'is_required',
- 'label' => __('frontend_label'),
- 'sortOrder' => 'sort_order',
- 'notice' => 'note',
- 'default' => 'default_value',
- 'size' => 'multiline_count',
- 'componentType' => Field::NAME,
- ],
- ],
- ],
- ],
- 'test-code-boolean' => [
- 'arguments' => [
- 'data' => [
- 'config' => [
- 'dataType' => 'frontend_input',
- 'formElement' => 'frontend_input',
- 'visible' => null,
- 'required' => 'is_required',
- 'label' => __('frontend_label'),
- 'sortOrder' => 'sort_order',
- 'notice' => 'note',
- 'default' => 'default_value',
- 'size' => 'multiline_count',
- 'componentType' => Field::NAME,
- 'prefer' => 'toggle',
- 'valueMap' => [
- 'true' => 1,
- 'false' => 0,
- ],
- ],
- ],
- ],
- ],
- ],
- ],
- 'address' => [
- 'children' => [
- self::ATTRIBUTE_CODE => [
- 'arguments' => [
- 'data' => [
- 'config' => [
- 'dataType' => 'frontend_input',
- 'formElement' => 'frontend_input',
- 'options' => 'test-options',
- 'visible' => null,
- 'required' => 'is_required',
- 'label' => __('frontend_label'),
- 'sortOrder' => 'sort_order',
- 'notice' => 'note',
- 'default' => 'default_value',
- 'size' => 'multiline_count',
- 'componentType' => Field::NAME,
- ],
- ],
- ],
- ],
- 'test-code-boolean' => [
- 'arguments' => [
- 'data' => [
- 'config' => [
- 'dataType' => 'frontend_input',
- 'formElement' => 'frontend_input',
- 'visible' => null,
- 'required' => 'is_required',
- 'label' => 'frontend_label',
- 'sortOrder' => 'sort_order',
- 'notice' => 'note',
- 'default' => 'default_value',
- 'size' => 'multiline_count',
- 'componentType' => Field::NAME,
- 'prefer' => 'toggle',
- 'valueMap' => [
- 'true' => 1,
- 'false' => 0,
- ],
- ],
- ],
- ],
- ],
- 'country_id' => [
- 'arguments' => [
- 'data' => [
- 'config' => [
- 'dataType' => 'frontend_input',
- 'formElement' => 'frontend_input',
- 'options' => 'test-options',
- 'visible' => null,
- 'required' => 'is_required',
- 'label' => __('frontend_label'),
- 'sortOrder' => 'sort_order',
- 'notice' => 'note',
- 'default' => 'default_value',
- 'size' => 'multiline_count',
- 'componentType' => Field::NAME,
- 'filterBy' => [
- 'target' => '${ $.provider }:data.customer.website_id',
- 'field' => 'website_ids'
- ]
- ],
- ],
- ],
- ]
- ],
- ],
- ]
- ]
- ];
- }
- /**
- * @return CollectionFactory|\PHPUnit_Framework_MockObject_MockObject
- */
- protected function getCustomerCollectionFactoryMock()
- {
- $collectionMock = $this->getMockBuilder(\Magento\Customer\Model\ResourceModel\Customer\Collection::class)
- ->disableOriginalConstructor()
- ->getMock();
- $collectionMock->expects($this->once())
- ->method('addAttributeToSelect')
- ->with('*');
- $this->customerCollectionFactoryMock->expects($this->once())
- ->method('create')
- ->willReturn($collectionMock);
- return $this->customerCollectionFactoryMock;
- }
- /**
- * @return Config|\PHPUnit_Framework_MockObject_MockObject
- */
- protected function getEavConfigMock($customerAttributes = [])
- {
- $this->eavConfigMock->expects($this->at(0))
- ->method('getEntityType')
- ->with('customer')
- ->willReturn($this->getTypeCustomerMock($customerAttributes));
- $this->eavConfigMock->expects($this->at(1))
- ->method('getEntityType')
- ->with('customer_address')
- ->willReturn($this->getTypeAddressMock());
- return $this->eavConfigMock;
- }
- /**
- * @return Type|\PHPUnit_Framework_MockObject_MockObject
- */
- protected function getTypeCustomerMock($customerAttributes = [])
- {
- $typeCustomerMock = $this->getMockBuilder(\Magento\Eav\Model\Entity\Type::class)
- ->disableOriginalConstructor()
- ->getMock();
- $attributesCollection = !empty($customerAttributes) ? $customerAttributes : $this->getAttributeMock();
- $typeCustomerMock->expects($this->any())
- ->method('getEntityTypeCode')
- ->willReturn('customer');
- foreach ($attributesCollection as $attribute) {
- $attribute->expects($this->any())
- ->method('getEntityType')
- ->willReturn($typeCustomerMock);
- }
- $typeCustomerMock->expects($this->once())
- ->method('getAttributeCollection')
- ->willReturn($attributesCollection);
- return $typeCustomerMock;
- }
- /**
- * @return Type|\PHPUnit_Framework_MockObject_MockObject
- */
- protected function getTypeAddressMock()
- {
- $typeAddressMock = $this->getMockBuilder(\Magento\Eav\Model\Entity\Type::class)
- ->disableOriginalConstructor()
- ->getMock();
- $typeAddressMock->expects($this->once())
- ->method('getAttributeCollection')
- ->willReturn($this->getAttributeMock('address'));
- return $typeAddressMock;
- }
- /**
- * @param \PHPUnit_Framework_MockObject_MockObject $attributeMock
- * @param \PHPUnit_Framework_MockObject_MockObject $attributeBooleanMock
- * @param array $options
- */
- private function injectVisibilityProps(
- \PHPUnit_Framework_MockObject_MockObject $attributeMock,
- \PHPUnit_Framework_MockObject_MockObject $attributeBooleanMock,
- array $options = []
- ) {
- if (isset($options[self::ATTRIBUTE_CODE]['visible'])) {
- $attributeMock->expects($this->any())
- ->method('getIsVisible')
- ->willReturn($options[self::ATTRIBUTE_CODE]['visible']);
- }
- if (isset($options[self::ATTRIBUTE_CODE]['user_defined'])) {
- $attributeMock->expects($this->any())
- ->method('getIsUserDefined')
- ->willReturn($options[self::ATTRIBUTE_CODE]['user_defined']);
- }
- if (isset($options[self::ATTRIBUTE_CODE]['is_used_in_forms'])) {
- $attributeMock->expects($this->any())
- ->method('getUsedInForms')
- ->willReturn($options[self::ATTRIBUTE_CODE]['is_used_in_forms']);
- }
- if (isset($options['test-code-boolean']['visible'])) {
- $attributeBooleanMock->expects($this->any())
- ->method('getIsVisible')
- ->willReturn($options['test-code-boolean']['visible']);
- }
- if (isset($options['test-code-boolean']['user_defined'])) {
- $attributeBooleanMock->expects($this->any())
- ->method('getIsUserDefined')
- ->willReturn($options['test-code-boolean']['user_defined']);
- }
- if (isset($options['test-code-boolean']['is_used_in_forms'])) {
- $attributeBooleanMock->expects($this->any())
- ->method('getUsedInForms')
- ->willReturn($options['test-code-boolean']['is_used_in_forms']);
- }
- }
- /**
- * @return AbstractAttribute[]|\PHPUnit_Framework_MockObject_MockObject[]
- */
- protected function getAttributeMock($type = 'customer', $options = [])
- {
- $attributeMock = $this->getMockBuilder(\Magento\Eav\Model\Entity\Attribute\AbstractAttribute::class)
- ->setMethods(
- [
- 'getAttributeCode',
- 'getDataUsingMethod',
- 'usesSource',
- 'getFrontendInput',
- 'getIsVisible',
- 'getSource',
- 'getIsUserDefined',
- 'getUsedInForms',
- 'getEntityType',
- ]
- )
- ->disableOriginalConstructor()
- ->getMockForAbstractClass();
- $sourceMock = $this->getMockBuilder(\Magento\Eav\Model\Entity\Attribute\Source\AbstractSource::class)
- ->disableOriginalConstructor()
- ->getMockForAbstractClass();
- $attributeCode = self::ATTRIBUTE_CODE;
- if (isset($options[self::ATTRIBUTE_CODE]['specific_code_prefix'])) {
- $attributeCode = $attributeCode . $options[self::ATTRIBUTE_CODE]['specific_code_prefix'];
- }
- $attributeMock->expects($this->exactly(2))
- ->method('getAttributeCode')
- ->willReturn($attributeCode);
- $sourceMock->expects($this->any())
- ->method('getAllOptions')
- ->willReturn(self::OPTIONS_RESULT);
- $attributeMock->expects($this->any())
- ->method('getDataUsingMethod')
- ->willReturnCallback($this->attributeGetUsingMethodCallback());
- $attributeMock->expects($this->any())
- ->method('usesSource')
- ->willReturn(true);
- $attributeMock->expects($this->any())
- ->method('getSource')
- ->willReturn($sourceMock);
- $attributeBooleanMock = $this->getMockBuilder(\Magento\Eav\Model\Entity\Attribute\AbstractAttribute::class)
- ->setMethods(
- [
- 'getAttributeCode',
- 'getDataUsingMethod',
- 'usesSource',
- 'getFrontendInput',
- 'getIsVisible',
- 'getIsUserDefined',
- 'getUsedInForms',
- 'getSource',
- 'getEntityType',
- ]
- )
- ->disableOriginalConstructor()
- ->getMockForAbstractClass();
- $attributeBooleanMock->expects($this->any())
- ->method('getFrontendInput')
- ->willReturn('boolean');
- $attributeBooleanMock->expects($this->any())
- ->method('getDataUsingMethod')
- ->willReturnCallback($this->attributeGetUsingMethodCallback());
- $attributeBooleanMock->expects($this->once())
- ->method('usesSource')
- ->willReturn(false);
- $booleanAttributeCode = 'test-code-boolean';
- if (isset($options['test-code-boolean']['specific_code_prefix'])) {
- $booleanAttributeCode = $booleanAttributeCode . $options['test-code-boolean']['specific_code_prefix'];
- }
- $attributeBooleanMock->expects($this->exactly(2))
- ->method('getAttributeCode')
- ->willReturn($booleanAttributeCode);
- $this->eavValidationRulesMock->expects($this->any())
- ->method('build')
- ->willReturnMap([
- [$attributeMock, $this->logicalNot($this->isEmpty()), []],
- [$attributeBooleanMock, $this->logicalNot($this->isEmpty()), []],
- ]);
- $mocks = [$attributeMock, $attributeBooleanMock];
- $this->injectVisibilityProps($attributeMock, $attributeBooleanMock, $options);
- if ($type == "address") {
- $mocks[] = $this->getCountryAttrMock();
- }
- return $mocks;
- }
- /**
- * Callback for ::getDataUsingMethod
- *
- * @return \Closure
- */
- private function attributeGetUsingMethodCallback()
- {
- return function ($origName) {
- return $origName;
- };
- }
- /**
- * @return \PHPUnit_Framework_MockObject_MockObject
- */
- private function getCountryAttrMock()
- {
- $countryByWebsiteMock = $this->getMockBuilder(CountryWithWebsites::class)
- ->disableOriginalConstructor()
- ->getMock();
- $countryByWebsiteMock->expects($this->any())
- ->method('getAllOptions')
- ->willReturn('test-options');
- $shareMock = $this->getMockBuilder(Share::class)
- ->disableOriginalConstructor()
- ->getMock();
- $objectManagerMock = $this->createMock(\Magento\Framework\ObjectManagerInterface::class);
- $objectManagerMock->expects($this->any())
- ->method('get')
- ->willReturnMap([
- [CountryWithWebsites::class, $countryByWebsiteMock],
- [Share::class, $shareMock],
- ]);
- \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock);
- $countryAttrMock = $this->getMockBuilder(\Magento\Eav\Model\Entity\Attribute\AbstractAttribute::class)
- ->setMethods(['getAttributeCode', 'getDataUsingMethod', 'usesSource', 'getSource', 'getLabel'])
- ->disableOriginalConstructor()
- ->getMockForAbstractClass();
- $countryAttrMock->expects($this->exactly(2))
- ->method('getAttributeCode')
- ->willReturn('country_id');
- $countryAttrMock->expects($this->any())
- ->method('getDataUsingMethod')
- ->willReturnCallback(
- function ($origName) {
- return $origName;
- }
- );
- $countryAttrMock->expects($this->any())
- ->method('getLabel')
- ->willReturn(__('frontend_label'));
- $countryAttrMock->expects($this->any())
- ->method('usesSource')
- ->willReturn(true);
- $countryAttrMock->expects($this->any())
- ->method('getSource')
- ->willReturn(null);
- return $countryAttrMock;
- }
- /**
- * @return void
- * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
- */
- public function testGetData()
- {
- $customerData = [
- 'email' => 'test@test.ua',
- 'default_billing' => 2,
- 'default_shipping' => 2,
- 'password_hash' => 'password_hash',
- 'rp_token' => 'rp_token',
- 'confirmation' => 'confirmation',
- ];
- $addressData = [
- 'firstname' => 'firstname',
- 'lastname' => 'lastname',
- 'street' => "street\nstreet",
- ];
- $customer = $this->getMockBuilder(\Magento\Customer\Model\Customer::class)
- ->disableOriginalConstructor()
- ->getMock();
- $address = $this->getMockBuilder(\Magento\Customer\Model\Address::class)
- ->disableOriginalConstructor()
- ->getMock();
- $collectionMock = $this->getMockBuilder(\Magento\Customer\Model\ResourceModel\Customer\Collection::class)
- ->disableOriginalConstructor()
- ->getMock();
- $collectionMock->expects($this->once())
- ->method('addAttributeToSelect')
- ->with('*');
- $this->customerCollectionFactoryMock->expects($this->once())
- ->method('create')
- ->willReturn($collectionMock);
- $collectionMock->expects($this->once())
- ->method('getItems')
- ->willReturn([$customer]);
- $customer->expects($this->once())
- ->method('getData')
- ->willReturn($customerData);
- $customer->expects($this->once())
- ->method('getAddresses')
- ->willReturn([$address]);
- $address->expects($this->atLeastOnce())
- ->method('getId')
- ->willReturn(2);
- $address->expects($this->once())
- ->method('load')
- ->with(2)
- ->willReturnSelf();
- $address->expects($this->once())
- ->method('getData')
- ->willReturn($addressData);
- $helper = new ObjectManager($this);
- $dataProvider = $helper->getObject(
- \Magento\Customer\Model\Customer\DataProvider::class,
- [
- 'name' => 'test-name',
- 'primaryFieldName' => 'primary-field-name',
- 'requestFieldName' => 'request-field-name',
- 'eavValidationRules' => $this->eavValidationRulesMock,
- 'customerCollectionFactory' => $this->customerCollectionFactoryMock,
- 'eavConfig' => $this->getEavConfigMock(),
- 'fileUploaderDataResolver' => $this->fileUploaderDataResolver
- ]
- );
- $reflection = new \ReflectionClass(get_class($dataProvider));
- $reflectionProperty = $reflection->getProperty('session');
- $reflectionProperty->setAccessible(true);
- $reflectionProperty->setValue($dataProvider, $this->sessionMock);
- $this->sessionMock->expects($this->once())
- ->method('getCustomerFormData')
- ->willReturn(null);
- $this->assertEquals(
- [
- '' => [
- 'customer' => [
- 'email' => 'test@test.ua',
- 'default_billing' => 2,
- 'default_shipping' => 2,
- ],
- 'address' => [
- 2 => [
- 'firstname' => 'firstname',
- 'lastname' => 'lastname',
- // Won't be an array because it isn't defined as a multiline field in this test
- 'street' => "street\nstreet",
- 'default_billing' => 2,
- 'default_shipping' => 2,
- ]
- ]
- ]
- ],
- $dataProvider->getData()
- );
- }
- /**
- * @return void
- * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
- */
- public function testGetDataWithCustomerFormData()
- {
- $customerId = 11;
- $customerFormData = [
- 'customer' => [
- 'email' => 'test1@test1.ua',
- 'default_billing' => 3,
- 'default_shipping' => 3,
- 'entity_id' => $customerId,
- ],
- 'address' => [
- 3 => [
- 'firstname' => 'firstname1',
- 'lastname' => 'lastname1',
- 'street' => [
- 'street1',
- 'street2',
- ],
- 'default_billing' => 3,
- 'default_shipping' => 3,
- ],
- ],
- ];
- $customer = $this->getMockBuilder(\Magento\Customer\Model\Customer::class)
- ->disableOriginalConstructor()
- ->getMock();
- $address = $this->getMockBuilder(\Magento\Customer\Model\Address::class)
- ->disableOriginalConstructor()
- ->getMock();
- $collectionMock = $this->getMockBuilder(\Magento\Customer\Model\ResourceModel\Customer\Collection::class)
- ->disableOriginalConstructor()
- ->getMock();
- $collectionMock->expects($this->once())
- ->method('addAttributeToSelect')
- ->with('*');
- $this->customerCollectionFactoryMock->expects($this->once())
- ->method('create')
- ->willReturn($collectionMock);
- $collectionMock->expects($this->once())
- ->method('getItems')
- ->willReturn([$customer]);
- $customer->expects($this->once())
- ->method('getData')
- ->willReturn([
- 'email' => 'test@test.ua',
- 'default_billing' => 2,
- 'default_shipping' => 2,
- ]);
- $customer->expects($this->once())
- ->method('getId')
- ->willReturn($customerId);
- $customer->expects($this->once())
- ->method('getAddresses')
- ->willReturn([$address]);
- $address->expects($this->atLeastOnce())
- ->method('getId')
- ->willReturn(2);
- $address->expects($this->once())
- ->method('load')
- ->with(2)
- ->willReturnSelf();
- $address->expects($this->once())
- ->method('getData')
- ->willReturn([
- 'firstname' => 'firstname',
- 'lastname' => 'lastname',
- 'street' => "street\nstreet",
- ]);
- $helper = new ObjectManager($this);
- $dataProvider = $helper->getObject(
- \Magento\Customer\Model\Customer\DataProvider::class,
- [
- 'name' => 'test-name',
- 'primaryFieldName' => 'primary-field-name',
- 'requestFieldName' => 'request-field-name',
- 'eavValidationRules' => $this->eavValidationRulesMock,
- 'customerCollectionFactory' => $this->customerCollectionFactoryMock,
- 'eavConfig' => $this->getEavConfigMock(),
- 'fileUploaderDataResolver' => $this->fileUploaderDataResolver
- ]
- );
- $reflection = new \ReflectionClass(get_class($dataProvider));
- $reflectionProperty = $reflection->getProperty('session');
- $reflectionProperty->setAccessible(true);
- $reflectionProperty->setValue($dataProvider, $this->sessionMock);
- $this->sessionMock->expects($this->once())
- ->method('getCustomerFormData')
- ->willReturn($customerFormData);
- $this->sessionMock->expects($this->once())
- ->method('unsCustomerFormData');
- $this->assertEquals([$customerId => $customerFormData], $dataProvider->getData());
- }
- /**
- * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
- * @return void
- */
- public function testGetDataWithCustomAttributeImage()
- {
- $customerId = 1;
- $customerEmail = 'user1@example.com';
- $filename = '/filename.ext1';
- $customerMock = $this->getMockBuilder(\Magento\Customer\Model\Customer::class)
- ->disableOriginalConstructor()
- ->getMock();
- $customerMock->expects($this->once())
- ->method('getData')
- ->willReturn([
- 'email' => $customerEmail,
- 'img1' => $filename,
- ]);
- $customerMock->expects($this->once())
- ->method('getAddresses')
- ->willReturn([]);
- $customerMock->expects($this->once())
- ->method('getId')
- ->willReturn($customerId);
- $collectionMock = $this->getMockBuilder(\Magento\Customer\Model\ResourceModel\Customer\Collection::class)
- ->disableOriginalConstructor()
- ->getMock();
- $collectionMock->expects($this->once())
- ->method('getItems')
- ->willReturn([$customerMock]);
- $this->customerCollectionFactoryMock->expects($this->once())
- ->method('create')
- ->willReturn($collectionMock);
- $this->sessionMock->expects($this->once())
- ->method('getCustomerFormData')
- ->willReturn([]);
- $objectManager = new ObjectManager($this);
- $dataProvider = $objectManager->getObject(
- \Magento\Customer\Model\Customer\DataProvider::class,
- [
- 'name' => 'test-name',
- 'primaryFieldName' => 'primary-field-name',
- 'requestFieldName' => 'request-field-name',
- 'eavValidationRules' => $this->eavValidationRulesMock,
- 'customerCollectionFactory' => $this->customerCollectionFactoryMock,
- 'eavConfig' => $this->getEavConfigMock(),
- 'fileUploaderDataResolver' => $this->fileUploaderDataResolver
- ]
- );
- $objectManager->setBackwardCompatibleProperty(
- $dataProvider,
- 'session',
- $this->sessionMock
- );
- $this->fileUploaderDataResolver->expects($this->atLeastOnce())->method('overrideFileUploaderData')
- ->with(
- $customerMock,
- [
- 'email' => $customerEmail,
- 'img1' => $filename,
- ]
- );
- $dataProvider->getData();
- }
- /**
- * @return void
- * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
- */
- public function testGetAttributesMetaWithCustomAttributeImage()
- {
- $maxFileSize = 1000;
- $allowedExtension = 'ext1 ext2';
- $attributeCode = 'img1';
- $collectionMock = $this->getMockBuilder(\Magento\Customer\Model\ResourceModel\Customer\Collection::class)
- ->disableOriginalConstructor()
- ->getMock();
- $collectionMock->expects($this->once())
- ->method('addAttributeToSelect')
- ->with('*');
- $this->customerCollectionFactoryMock->expects($this->once())
- ->method('create')
- ->willReturn($collectionMock);
- $attributeMock = $this->getMockBuilder(\Magento\Eav\Model\Entity\Attribute\AbstractAttribute::class)
- ->setMethods([
- 'getAttributeCode',
- 'getFrontendInput',
- 'getDataUsingMethod',
- ])
- ->disableOriginalConstructor()
- ->getMockForAbstractClass();
- $attributeMock->expects($this->any())
- ->method('getAttributeCode')
- ->willReturn($attributeCode);
- $attributeMock->expects($this->any())
- ->method('getFrontendInput')
- ->willReturn('image');
- $attributeMock->expects($this->any())
- ->method('getDataUsingMethod')
- ->willReturnCallback(
- function ($origName) {
- return $origName;
- }
- );
- $typeCustomerMock = $this->getMockBuilder(\Magento\Eav\Model\Entity\Type::class)
- ->disableOriginalConstructor()
- ->getMock();
- $typeCustomerMock->expects($this->once())
- ->method('getAttributeCollection')
- ->willReturn([$attributeMock]);
- $typeCustomerMock->expects($this->once())
- ->method('getEntityTypeCode')
- ->willReturn(CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER);
- $typeAddressMock = $this->getMockBuilder(\Magento\Eav\Model\Entity\Type::class)
- ->disableOriginalConstructor()
- ->getMock();
- $typeAddressMock->expects($this->once())
- ->method('getAttributeCollection')
- ->willReturn([]);
- $this->eavConfigMock->expects($this->at(0))
- ->method('getEntityType')
- ->with('customer')
- ->willReturn($typeCustomerMock);
- $this->eavConfigMock->expects($this->at(1))
- ->method('getEntityType')
- ->with('customer_address')
- ->willReturn($typeAddressMock);
- $this->eavValidationRulesMock->expects($this->once())
- ->method('build')
- ->with($attributeMock, [
- 'dataType' => 'frontend_input',
- 'formElement' => 'frontend_input',
- 'visible' => 'is_visible',
- 'required' => 'is_required',
- 'sortOrder' => 'sort_order',
- 'notice' => 'note',
- 'default' => 'default_value',
- 'size' => 'multiline_count',
- 'label' => __('frontend_label'),
- ])
- ->willReturn([
- 'max_file_size' => $maxFileSize,
- 'file_extensions' => 'ext1, eXt2 ', // Added spaces and upper-cases
- ]);
- $objectManager = new ObjectManager($this);
- $dataProvider = $objectManager->getObject(
- \Magento\Customer\Model\Customer\DataProvider::class,
- [
- 'name' => 'test-name',
- 'primaryFieldName' => 'primary-field-name',
- 'requestFieldName' => 'request-field-name',
- 'eavValidationRules' => $this->eavValidationRulesMock,
- 'customerCollectionFactory' => $this->customerCollectionFactoryMock,
- 'eavConfig' => $this->eavConfigMock
- ]
- );
- $result = $dataProvider->getMeta();
- $this->assertNotEmpty($result);
- $expected = [
- 'customer' => [
- 'children' => [
- $attributeCode => [
- 'arguments' => [
- 'data' => [
- 'config' => [
- 'formElement' => 'fileUploader',
- 'componentType' => 'fileUploader',
- 'maxFileSize' => $maxFileSize,
- 'allowedExtensions' => $allowedExtension,
- 'uploaderConfig' => [
- 'url' => 'customer/file/customer_upload',
- ],
- 'sortOrder' => 'sort_order',
- 'required' => 'is_required',
- 'visible' => null,
- 'validation' => [
- 'max_file_size' => $maxFileSize,
- 'file_extensions' => 'ext1, eXt2 ',
- ],
- 'label' => __('frontend_label'),
- ],
- ],
- ],
- ],
- ],
- ],
- 'address' => [
- 'children' => [],
- ],
- ];
- $this->assertEquals($expected, $result);
- }
- /**
- * @return void
- */
- public function testGetDataWithVisibleAttributes()
- {
- $firstAttributesBundle = $this->getAttributeMock(
- 'customer',
- [
- self::ATTRIBUTE_CODE => [
- 'visible' => true,
- 'is_used_in_forms' => ['customer_account_edit'],
- 'user_defined' => true,
- 'specific_code_prefix' => "_1"
- ],
- 'test-code-boolean' => [
- 'visible' => true,
- 'is_used_in_forms' => ['customer_account_create'],
- 'user_defined' => true,
- 'specific_code_prefix' => "_1"
- ]
- ]
- );
- $secondAttributesBundle = $this->getAttributeMock(
- 'customer',
- [
- self::ATTRIBUTE_CODE => [
- 'visible' => true,
- 'is_used_in_forms' => ['customer_account_create'],
- 'user_defined' => false,
- 'specific_code_prefix' => "_2"
- ],
- 'test-code-boolean' => [
- 'visible' => true,
- 'is_used_in_forms' => ['customer_account_create'],
- 'user_defined' => true,
- 'specific_code_prefix' => "_2"
- ]
- ]
- );
- $helper = new ObjectManager($this);
- /** @var \Magento\Customer\Model\Customer\DataProvider $dataProvider */
- $dataProvider = $helper->getObject(
- \Magento\Customer\Model\Customer\DataProvider::class,
- [
- 'name' => 'test-name',
- 'primaryFieldName' => 'primary-field-name',
- 'requestFieldName' => 'request-field-name',
- 'eavValidationRules' => $this->eavValidationRulesMock,
- 'customerCollectionFactory' => $this->getCustomerCollectionFactoryMock(),
- 'eavConfig' => $this->getEavConfigMock(array_merge($firstAttributesBundle, $secondAttributesBundle)),
- 'fileUploaderDataResolver' => $this->fileUploaderDataResolver
- ]
- );
- $meta = $dataProvider->getMeta();
- $this->assertNotEmpty($meta);
- $this->assertEquals($this->getExpectationForVisibleAttributes(), $meta);
- }
- /**
- * @return void
- */
- public function testGetDataWithVisibleAttributesWithAccountEdit()
- {
- $firstAttributesBundle = $this->getAttributeMock(
- 'customer',
- [
- self::ATTRIBUTE_CODE => [
- 'visible' => true,
- 'is_used_in_forms' => ['customer_account_edit'],
- 'user_defined' => true,
- 'specific_code_prefix' => "_1"
- ],
- 'test-code-boolean' => [
- 'visible' => true,
- 'is_used_in_forms' => ['customer_account_create'],
- 'user_defined' => true,
- 'specific_code_prefix' => "_1"
- ]
- ]
- );
- $secondAttributesBundle = $this->getAttributeMock(
- 'customer',
- [
- self::ATTRIBUTE_CODE => [
- 'visible' => true,
- 'is_used_in_forms' => ['customer_account_create'],
- 'user_defined' => false,
- 'specific_code_prefix' => "_2"
- ],
- 'test-code-boolean' => [
- 'visible' => true,
- 'is_used_in_forms' => ['customer_account_create'],
- 'user_defined' => true,
- 'specific_code_prefix' => "_2"
- ]
- ]
- );
- $helper = new ObjectManager($this);
- $context = $this->getMockBuilder(\Magento\Framework\View\Element\UiComponent\ContextInterface::class)
- ->setMethods(['getRequestParam'])
- ->getMockForAbstractClass();
- $context->expects($this->any())
- ->method('getRequestParam')
- ->with('request-field-name')
- ->willReturn(1);
- /** @var \Magento\Customer\Model\Customer\DataProvider $dataProvider */
- $dataProvider = $helper->getObject(
- \Magento\Customer\Model\Customer\DataProvider::class,
- [
- 'name' => 'test-name',
- 'primaryFieldName' => 'primary-field-name',
- 'requestFieldName' => 'request-field-name',
- 'eavValidationRules' => $this->eavValidationRulesMock,
- 'customerCollectionFactory' => $this->getCustomerCollectionFactoryMock(),
- 'context' => $context,
- 'eavConfig' => $this->getEavConfigMock(array_merge($firstAttributesBundle, $secondAttributesBundle)),
- 'fileUploaderDataResolver' => $this->fileUploaderDataResolver
- ]
- );
- $meta = $dataProvider->getMeta();
- $this->assertNotEmpty($meta);
- $this->assertEquals($this->getExpectationForVisibleAttributes(false), $meta);
- }
- /**
- * Retrieve all customer variations of attributes with all variations of visibility
- *
- * @param bool $isRegistration
- * @return array
- */
- private function getCustomerAttributeExpectations($isRegistration)
- {
- return [
- self::ATTRIBUTE_CODE . "_1" => [
- 'arguments' => [
- 'data' => [
- 'config' => [
- 'dataType' => 'frontend_input',
- 'formElement' => 'frontend_input',
- 'options' => 'test-options',
- 'visible' => !$isRegistration,
- 'required' => 'is_required',
- 'label' => __('frontend_label'),
- 'sortOrder' => 'sort_order',
- 'notice' => 'note',
- 'default' => 'default_value',
- 'size' => 'multiline_count',
- 'componentType' => Field::NAME,
- ],
- ],
- ],
- ],
- self::ATTRIBUTE_CODE . "_2" => [
- 'arguments' => [
- 'data' => [
- 'config' => [
- 'dataType' => 'frontend_input',
- 'formElement' => 'frontend_input',
- 'options' => 'test-options',
- 'visible' => true,
- 'required' => 'is_required',
- 'label' => __('frontend_label'),
- 'sortOrder' => 'sort_order',
- 'notice' => 'note',
- 'default' => 'default_value',
- 'size' => 'multiline_count',
- 'componentType' => Field::NAME,
- ],
- ],
- ],
- ],
- 'test-code-boolean_1' => [
- 'arguments' => [
- 'data' => [
- 'config' => [
- 'dataType' => 'frontend_input',
- 'formElement' => 'frontend_input',
- 'visible' => $isRegistration,
- 'required' => 'is_required',
- 'label' => __('frontend_label'),
- 'sortOrder' => 'sort_order',
- 'notice' => 'note',
- 'default' => 'default_value',
- 'size' => 'multiline_count',
- 'componentType' => Field::NAME,
- 'prefer' => 'toggle',
- 'valueMap' => [
- 'true' => 1,
- 'false' => 0,
- ],
- ],
- ],
- ],
- ],
- 'test-code-boolean_2' => [
- 'arguments' => [
- 'data' => [
- 'config' => [
- 'dataType' => 'frontend_input',
- 'formElement' => 'frontend_input',
- 'visible' => $isRegistration,
- 'required' => 'is_required',
- 'label' => __('frontend_label'),
- 'sortOrder' => 'sort_order',
- 'notice' => 'note',
- 'default' => 'default_value',
- 'size' => 'multiline_count',
- 'componentType' => Field::NAME,
- 'prefer' => 'toggle',
- 'valueMap' => [
- 'true' => 1,
- 'false' => 0,
- ],
- ],
- ],
- ],
- ],
- ];
- }
- /**
- * Retrieve all variations of attributes with all variations of visibility
- *
- * @param bool $isRegistration
- * @return array
- */
- private function getExpectationForVisibleAttributes($isRegistration = true)
- {
- return [
- 'customer' => [
- 'children' => $this->getCustomerAttributeExpectations($isRegistration),
- ],
- 'address' => [
- 'children' => [
- self::ATTRIBUTE_CODE => [
- 'arguments' => [
- 'data' => [
- 'config' => [
- 'dataType' => 'frontend_input',
- 'formElement' => 'frontend_input',
- 'options' => 'test-options',
- 'visible' => null,
- 'required' => 'is_required',
- 'label' => __('frontend_label'),
- 'sortOrder' => 'sort_order',
- 'notice' => 'note',
- 'default' => 'default_value',
- 'size' => 'multiline_count',
- 'componentType' => Field::NAME,
- ],
- ],
- ],
- ],
- 'test-code-boolean' => [
- 'arguments' => [
- 'data' => [
- 'config' => [
- 'dataType' => 'frontend_input',
- 'formElement' => 'frontend_input',
- 'visible' => null,
- 'required' => 'is_required',
- 'label' => 'frontend_label',
- 'sortOrder' => 'sort_order',
- 'notice' => 'note',
- 'default' => 'default_value',
- 'size' => 'multiline_count',
- 'componentType' => Field::NAME,
- 'prefer' => 'toggle',
- 'valueMap' => [
- 'true' => 1,
- 'false' => 0,
- ],
- ],
- ],
- ],
- ],
- 'country_id' => [
- 'arguments' => [
- 'data' => [
- 'config' => [
- 'dataType' => 'frontend_input',
- 'formElement' => 'frontend_input',
- 'options' => 'test-options',
- 'visible' => null,
- 'required' => 'is_required',
- 'label' => __('frontend_label'),
- 'sortOrder' => 'sort_order',
- 'notice' => 'note',
- 'default' => 'default_value',
- 'size' => 'multiline_count',
- 'componentType' => Field::NAME,
- 'filterBy' => [
- 'target' => '${ $.provider }:data.customer.website_id',
- 'field' => 'website_ids'
- ]
- ],
- ],
- ],
- ]
- ],
- ],
- ];
- }
- }
|