123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Customer\Test\Unit\Model;
- use Magento\Customer\Api\CustomerMetadataInterface;
- use Magento\Customer\Model\FileProcessor;
- use Magento\Customer\Model\FileUploader;
- class FileUploaderTest extends \PHPUnit\Framework\TestCase
- {
- /**
- * @var CustomerMetadataInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- private $customerMetadataService;
- /**
- * @var \Magento\Customer\Api\AddressMetadataInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- private $addressMetadataService;
- /**
- * @var \Magento\Customer\Model\Metadata\ElementFactory|\PHPUnit_Framework_MockObject_MockObject
- */
- private $elementFactory;
- /**
- * @var \Magento\Customer\Model\FileProcessorFactory|\PHPUnit_Framework_MockObject_MockObject
- */
- private $fileProcessorFactory;
- /**
- * @var \Magento\Customer\Api\Data\AttributeMetadataInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- private $attributeMetadata;
- protected function setUp()
- {
- $this->customerMetadataService = $this->getMockBuilder(\Magento\Customer\Api\CustomerMetadataInterface::class)
- ->getMockForAbstractClass();
- $this->addressMetadataService = $this->getMockBuilder(\Magento\Customer\Api\AddressMetadataInterface::class)
- ->getMockForAbstractClass();
- $this->elementFactory = $this->getMockBuilder(\Magento\Customer\Model\Metadata\ElementFactory::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->fileProcessorFactory = $this->getMockBuilder(\Magento\Customer\Model\FileProcessorFactory::class)
- ->disableOriginalConstructor()
- ->setMethods(['create'])
- ->getMock();
- $this->attributeMetadata = $this->getMockBuilder(\Magento\Customer\Api\Data\AttributeMetadataInterface::class)
- ->getMockForAbstractClass();
- }
- protected function tearDown()
- {
- $_FILES = [];
- }
- /**
- * @param string $entityTypeCode
- * @param string $scope
- * @return FileUploader
- */
- private function getModel($entityTypeCode, $scope)
- {
- $model = new FileUploader(
- $this->customerMetadataService,
- $this->addressMetadataService,
- $this->elementFactory,
- $this->fileProcessorFactory,
- $this->attributeMetadata,
- $entityTypeCode,
- $scope
- );
- return $model;
- }
- public function testValidate()
- {
- $attributeCode = 'attribute_code';
- $filename = 'filename.ext1';
- $_FILES = [
- 'customer' => [
- 'name' => [
- $attributeCode => $filename,
- ],
- ],
- ];
- $formElement = $this->getMockBuilder(\Magento\Customer\Model\Metadata\Form\Image::class)
- ->disableOriginalConstructor()
- ->getMock();
- $formElement->expects($this->once())
- ->method('validateValue')
- ->with(['name' => $filename])
- ->willReturn(true);
- $this->elementFactory->expects($this->once())
- ->method('create')
- ->with($this->attributeMetadata, null, CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER)
- ->willReturn($formElement);
- $model = $this->getModel(CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER, 'customer');
- $this->assertTrue($model->validate());
- }
- public function testUpload()
- {
- $attributeCode = 'attribute_code';
- $attributeFrontendInput = 'image';
- $resultFileName = '/filename.ext1';
- $resultFilePath = 'filepath';
- $resultFileUrl = 'viewFileUrl';
- $allowedExtensions = 'ext1,EXT2 , eXt3'; // Added spaces, commas and upper-cases
- $expectedAllowedExtensions = [
- 'ext1',
- 'ext2',
- 'ext3',
- ];
- $_FILES = [
- 'customer' => [
- 'name' => [
- $attributeCode => 'filename',
- ],
- ],
- ];
- $expectedResult = [
- 'name' => $resultFileName,
- 'file' => $resultFileName,
- 'path' => $resultFilePath,
- 'tmp_name' => ltrim($resultFileName, '/'),
- 'url' => $resultFileUrl,
- ];
- $fileProcessor = $this->getMockBuilder(\Magento\Customer\Model\FileProcessor::class)
- ->disableOriginalConstructor()
- ->getMock();
- $fileProcessor->expects($this->once())
- ->method('saveTemporaryFile')
- ->with('customer[' . $attributeCode . ']')
- ->willReturn([
- 'name' => $resultFileName,
- 'path' => $resultFilePath,
- 'file' => $resultFileName,
- ]);
- $fileProcessor->expects($this->once())
- ->method('getViewUrl')
- ->with(FileProcessor::TMP_DIR . '/filename.ext1', $attributeFrontendInput)
- ->willReturn($resultFileUrl);
- $this->fileProcessorFactory->expects($this->once())
- ->method('create')
- ->with([
- 'entityTypeCode' => CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER,
- 'allowedExtensions' => $expectedAllowedExtensions,
- ])
- ->willReturn($fileProcessor);
- $validationRuleMock = $this->getMockBuilder(\Magento\Customer\Api\Data\ValidationRuleInterface::class)
- ->getMockForAbstractClass();
- $validationRuleMock->expects($this->once())
- ->method('getName')
- ->willReturn('file_extensions');
- $validationRuleMock->expects($this->once())
- ->method('getValue')
- ->willReturn($allowedExtensions);
- $this->attributeMetadata->expects($this->once())
- ->method('getFrontendInput')
- ->willReturn($attributeFrontendInput);
- $this->attributeMetadata->expects($this->once())
- ->method('getValidationRules')
- ->willReturn([$validationRuleMock]);
- $model = $this->getModel(CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER, 'customer');
- $this->assertEquals($expectedResult, $model->upload());
- }
- }
|