|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\Customer\Test\Unit\Model; |
| 9 | + |
| 10 | +use Magento\Customer\Model\Attribute; |
| 11 | +use Magento\Customer\Model\AttributeMetadataResolver; |
| 12 | +use Magento\Customer\Model\Config\Share as ShareConfig; |
| 13 | +use Magento\Customer\Model\FileUploaderDataResolver; |
| 14 | +use Magento\Customer\Model\GroupManagement; |
| 15 | +use Magento\Customer\Model\ResourceModel\Address\Attribute\Source\CountryWithWebsites; |
| 16 | +use Magento\Eav\Model\Entity\Type; |
| 17 | +use Magento\Framework\View\Element\UiComponent\ContextInterface; |
| 18 | +use Magento\Ui\DataProvider\EavValidationRules; |
| 19 | +use PHPUnit\Framework\MockObject\MockObject; |
| 20 | +use PHPUnit\Framework\TestCase; |
| 21 | + |
| 22 | +/** |
| 23 | + * Class AttributeMetadataResolverTest |
| 24 | + * |
| 25 | + * Validate attributeMetadata contains correct values in meta data array |
| 26 | + */ |
| 27 | +class AttributeMetadataResolverTest extends TestCase |
| 28 | +{ |
| 29 | + /** |
| 30 | + * @var CountryWithWebsites|MockObject |
| 31 | + */ |
| 32 | + private $countryWithWebsiteSource; |
| 33 | + |
| 34 | + /** |
| 35 | + * @var EavValidationRules|MockObject |
| 36 | + */ |
| 37 | + private $eavValidationRules; |
| 38 | + |
| 39 | + /** |
| 40 | + * @var FileUploaderDataResolver|MockObject |
| 41 | + */ |
| 42 | + private $fileUploaderDataResolver; |
| 43 | + |
| 44 | + /** |
| 45 | + * @var ShareConfig|MockObject |
| 46 | + */ |
| 47 | + private $shareConfig; |
| 48 | + |
| 49 | + /** |
| 50 | + * @var GroupManagement|MockObject |
| 51 | + */ |
| 52 | + private $groupManagement; |
| 53 | + |
| 54 | + /** |
| 55 | + * @var ContextInterface|MockObject |
| 56 | + */ |
| 57 | + private $context; |
| 58 | + |
| 59 | + /** |
| 60 | + * @var AttributeMetadataResolver |
| 61 | + */ |
| 62 | + private $model; |
| 63 | + |
| 64 | + /** |
| 65 | + * @var Attribute|MockObject |
| 66 | + */ |
| 67 | + private $attribute; |
| 68 | + |
| 69 | + /** |
| 70 | + * @inheritdoc |
| 71 | + */ |
| 72 | + public function setUp() |
| 73 | + { |
| 74 | + $this->countryWithWebsiteSource = $this->getMockBuilder(CountryWithWebsites::class) |
| 75 | + ->setMethods(['getAllOptions']) |
| 76 | + ->disableOriginalConstructor() |
| 77 | + ->getMock(); |
| 78 | + $this->eavValidationRules = $this->getMockBuilder(EavValidationRules::class) |
| 79 | + ->setMethods(['build']) |
| 80 | + ->disableOriginalConstructor() |
| 81 | + ->getMock(); |
| 82 | + $this->fileUploaderDataResolver = $this->getMockBuilder(FileUploaderDataResolver::class) |
| 83 | + ->setMethods(['overrideFileUploaderMetadata']) |
| 84 | + ->disableOriginalConstructor() |
| 85 | + ->getMock(); |
| 86 | + $this->context = $this->getMockBuilder(ContextInterface::class) |
| 87 | + ->disableOriginalConstructor() |
| 88 | + ->getMock(); |
| 89 | + $this->shareConfig = $this->getMockBuilder(ShareConfig::class) |
| 90 | + ->disableOriginalConstructor() |
| 91 | + ->getMock(); |
| 92 | + $this->groupManagement = $this->getMockBuilder(GroupManagement::class) |
| 93 | + ->setMethods(['getId', 'getDefaultGroup']) |
| 94 | + ->disableOriginalConstructor() |
| 95 | + ->getMock(); |
| 96 | + $this->attribute = $this->getMockBuilder(Attribute::class) |
| 97 | + ->setMethods([ |
| 98 | + 'usesSource', |
| 99 | + 'getDataUsingMethod', |
| 100 | + 'getAttributeCode', |
| 101 | + 'getFrontendInput', |
| 102 | + 'getSource', |
| 103 | + 'setDataUsingMethod' |
| 104 | + ]) |
| 105 | + ->disableOriginalConstructor() |
| 106 | + ->getMock(); |
| 107 | + |
| 108 | + $this->model = new AttributeMetadataResolver( |
| 109 | + $this->countryWithWebsiteSource, |
| 110 | + $this->eavValidationRules, |
| 111 | + $this->fileUploaderDataResolver, |
| 112 | + $this->context, |
| 113 | + $this->shareConfig, |
| 114 | + $this->groupManagement |
| 115 | + ); |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Test to get meta data of the customer or customer address attribute |
| 120 | + * |
| 121 | + * @return void |
| 122 | + */ |
| 123 | + public function testGetAttributesMetaHasDefaultAttributeValue(): void |
| 124 | + { |
| 125 | + $rules = [ |
| 126 | + 'required-entry' => true |
| 127 | + ]; |
| 128 | + $defaultGroupId = '3'; |
| 129 | + $allowToShowHiddenAttributes = false; |
| 130 | + $usesSource = false; |
| 131 | + $entityType = $this->getMockBuilder(Type::class) |
| 132 | + ->disableOriginalConstructor() |
| 133 | + ->getMock(); |
| 134 | + $this->attribute->expects($this->once()) |
| 135 | + ->method('usesSource') |
| 136 | + ->willReturn($usesSource); |
| 137 | + $this->attribute->expects($this->once()) |
| 138 | + ->method('getAttributeCode') |
| 139 | + ->willReturn('group_id'); |
| 140 | + $this->groupManagement->expects($this->once()) |
| 141 | + ->method('getDefaultGroup') |
| 142 | + ->willReturnSelf(); |
| 143 | + $this->groupManagement->expects($this->once()) |
| 144 | + ->method('getId') |
| 145 | + ->willReturn($defaultGroupId); |
| 146 | + $this->attribute->expects($this->at(9)) |
| 147 | + ->method('getDataUsingMethod') |
| 148 | + ->with('default_value') |
| 149 | + ->willReturn($defaultGroupId); |
| 150 | + $this->attribute->expects($this->once()) |
| 151 | + ->method('setDataUsingMethod') |
| 152 | + ->willReturnSelf(); |
| 153 | + $this->eavValidationRules->expects($this->once()) |
| 154 | + ->method('build') |
| 155 | + ->with($this->attribute) |
| 156 | + ->willReturn($rules); |
| 157 | + $this->fileUploaderDataResolver->expects($this->once()) |
| 158 | + ->method('overrideFileUploaderMetadata') |
| 159 | + ->with($entityType, $this->attribute) |
| 160 | + ->willReturnSelf(); |
| 161 | + |
| 162 | + $meta = $this->model->getAttributesMeta($this->attribute, $entityType, $allowToShowHiddenAttributes); |
| 163 | + $this->assertArrayHasKey('default', $meta['arguments']['data']['config']); |
| 164 | + $this->assertEquals($defaultGroupId, $meta['arguments']['data']['config']['default']); |
| 165 | + } |
| 166 | +} |
0 commit comments