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: 15 additions & 1 deletion app/code/Magento/Customer/Model/FileUploader.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ class FileUploader
*/
private $scope;

/**
* @var string[]
*/
private array $validInputTypes;

/**
* @param CustomerMetadataInterface $customerMetadataService
* @param AddressMetadataInterface $addressMetadataService
Expand All @@ -57,6 +62,7 @@ class FileUploader
* @param AttributeMetadataInterface $attributeMetadata
* @param string $entityTypeCode
* @param string $scope
* @param array|null $validInputTypes
*/
public function __construct(
CustomerMetadataInterface $customerMetadataService,
Expand All @@ -65,7 +71,8 @@ public function __construct(
FileProcessorFactory $fileProcessorFactory,
AttributeMetadataInterface $attributeMetadata,
$entityTypeCode,
$scope
$scope,
?array $validInputTypes = ['file', 'image']
) {
$this->customerMetadataService = $customerMetadataService;
$this->addressMetadataService = $addressMetadataService;
Expand All @@ -74,6 +81,7 @@ public function __construct(
$this->attributeMetadata = $attributeMetadata;
$this->entityTypeCode = $entityTypeCode;
$this->scope = $scope;
$this->validInputTypes = $validInputTypes;
}

/**
Expand All @@ -83,6 +91,12 @@ public function __construct(
*/
public function validate()
{
if (!in_array($this->attributeMetadata->getFrontendInput(), $this->validInputTypes)) {
return [
__('"%1" is not a valid input to accept file uploads.', $this->attributeMetadata->getFrontendInput())
];
}

$formElement = $this->elementFactory->create(
$this->attributeMetadata,
null,
Expand Down
30 changes: 30 additions & 0 deletions app/code/Magento/Customer/Test/Unit/Model/FileUploaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Magento\Customer\Model\FileUploader;
use Magento\Customer\Model\Metadata\ElementFactory;
use Magento\Customer\Model\Metadata\Form\Image;
use Magento\Customer\Model\Metadata\Form\Select;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -118,10 +119,39 @@ public function testValidate()
->with($this->attributeMetadata, null, CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER)
->willReturn($formElement);

$this->attributeMetadata->expects($this->once())
->method('getFrontendInput')
->willReturn('image');

$model = $this->getModel(CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER, 'customer');
$this->assertTrue($model->validate());
}

public function testValidateInvalidAttributeType()
{
$attributeType = 'select';
$attributeCode = 'attribute_code';
$filename = 'filename.ext1';

$_FILES = [
'customer' => [
'name' => [
$attributeCode => $filename,
],
],
];

$this->attributeMetadata->expects($this->exactly(2))
->method('getFrontendInput')
->willReturn($attributeType);

$model = $this->getModel(CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER, 'customer');
$expectedErrors = [
__('"%1" is not a valid input to accept file uploads.', $attributeType)
];
$this->assertEquals($expectedErrors, $model->validate());
}

public function testUpload()
{
$attributeCode = 'attribute_code';
Expand Down
8 changes: 8 additions & 0 deletions app/code/Magento/Customer/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -598,4 +598,12 @@
<argument name="indexerId" xsi:type="string">customer_grid</argument>
</arguments>
</virtualType>
<type name="Magento\Customer\Model\FileUploader">
<arguments>
<argument name="validInputTypes" xsi:type="array">
<item name="file" xsi:type="string">file</item>
<item name="image" xsi:type="string">image</item>
</argument>
</arguments>
</type>
</config>