Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
FileUploaderTest.php
Go to the documentation of this file.
1 <?php
7 
11 
12 class FileUploaderTest extends \PHPUnit\Framework\TestCase
13 {
17  private $customerMetadataService;
18 
22  private $addressMetadataService;
23 
27  private $elementFactory;
28 
32  private $fileProcessorFactory;
33 
37  private $attributeMetadata;
38 
39  protected function setUp()
40  {
41  $this->customerMetadataService = $this->getMockBuilder(\Magento\Customer\Api\CustomerMetadataInterface::class)
42  ->getMockForAbstractClass();
43 
44  $this->addressMetadataService = $this->getMockBuilder(\Magento\Customer\Api\AddressMetadataInterface::class)
45  ->getMockForAbstractClass();
46 
47  $this->elementFactory = $this->getMockBuilder(\Magento\Customer\Model\Metadata\ElementFactory::class)
48  ->disableOriginalConstructor()
49  ->getMock();
50 
51  $this->fileProcessorFactory = $this->getMockBuilder(\Magento\Customer\Model\FileProcessorFactory::class)
52  ->disableOriginalConstructor()
53  ->setMethods(['create'])
54  ->getMock();
55 
56  $this->attributeMetadata = $this->getMockBuilder(\Magento\Customer\Api\Data\AttributeMetadataInterface::class)
57  ->getMockForAbstractClass();
58  }
59 
60  protected function tearDown()
61  {
62  $_FILES = [];
63  }
64 
70  private function getModel($entityTypeCode, $scope)
71  {
72  $model = new FileUploader(
73  $this->customerMetadataService,
74  $this->addressMetadataService,
75  $this->elementFactory,
76  $this->fileProcessorFactory,
77  $this->attributeMetadata,
79  $scope
80  );
81  return $model;
82  }
83 
84  public function testValidate()
85  {
86  $attributeCode = 'attribute_code';
87 
88  $filename = 'filename.ext1';
89 
90  $_FILES = [
91  'customer' => [
92  'name' => [
93  $attributeCode => $filename,
94  ],
95  ],
96  ];
97 
98  $formElement = $this->getMockBuilder(\Magento\Customer\Model\Metadata\Form\Image::class)
99  ->disableOriginalConstructor()
100  ->getMock();
101  $formElement->expects($this->once())
102  ->method('validateValue')
103  ->with(['name' => $filename])
104  ->willReturn(true);
105 
106  $this->elementFactory->expects($this->once())
107  ->method('create')
108  ->with($this->attributeMetadata, null, CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER)
109  ->willReturn($formElement);
110 
111  $model = $this->getModel(CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER, 'customer');
112  $this->assertTrue($model->validate());
113  }
114 
115  public function testUpload()
116  {
117  $attributeCode = 'attribute_code';
118  $attributeFrontendInput = 'image';
119 
120  $resultFileName = '/filename.ext1';
121  $resultFilePath = 'filepath';
122  $resultFileUrl = 'viewFileUrl';
123 
124  $allowedExtensions = 'ext1,EXT2 , eXt3'; // Added spaces, commas and upper-cases
125  $expectedAllowedExtensions = [
126  'ext1',
127  'ext2',
128  'ext3',
129  ];
130 
131  $_FILES = [
132  'customer' => [
133  'name' => [
134  $attributeCode => 'filename',
135  ],
136  ],
137  ];
138 
139  $expectedResult = [
140  'name' => $resultFileName,
141  'file' => $resultFileName,
142  'path' => $resultFilePath,
143  'tmp_name' => ltrim($resultFileName, '/'),
144  'url' => $resultFileUrl,
145  ];
146 
147  $fileProcessor = $this->getMockBuilder(\Magento\Customer\Model\FileProcessor::class)
148  ->disableOriginalConstructor()
149  ->getMock();
150  $fileProcessor->expects($this->once())
151  ->method('saveTemporaryFile')
152  ->with('customer[' . $attributeCode . ']')
153  ->willReturn([
154  'name' => $resultFileName,
155  'path' => $resultFilePath,
156  'file' => $resultFileName,
157  ]);
158  $fileProcessor->expects($this->once())
159  ->method('getViewUrl')
160  ->with(FileProcessor::TMP_DIR . '/filename.ext1', $attributeFrontendInput)
161  ->willReturn($resultFileUrl);
162 
163  $this->fileProcessorFactory->expects($this->once())
164  ->method('create')
165  ->with([
167  'allowedExtensions' => $expectedAllowedExtensions,
168  ])
169  ->willReturn($fileProcessor);
170 
171  $validationRuleMock = $this->getMockBuilder(\Magento\Customer\Api\Data\ValidationRuleInterface::class)
172  ->getMockForAbstractClass();
173  $validationRuleMock->expects($this->once())
174  ->method('getName')
175  ->willReturn('file_extensions');
176  $validationRuleMock->expects($this->once())
177  ->method('getValue')
178  ->willReturn($allowedExtensions);
179 
180  $this->attributeMetadata->expects($this->once())
181  ->method('getFrontendInput')
182  ->willReturn($attributeFrontendInput);
183  $this->attributeMetadata->expects($this->once())
184  ->method('getValidationRules')
185  ->willReturn([$validationRuleMock]);
186 
187  $model = $this->getModel(CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER, 'customer');
188  $this->assertEquals($expectedResult, $model->upload());
189  }
190 }
$allowedExtensions
Definition: uploader.phtml:12
$attributeCode
Definition: extend.phtml:12