FileUploaderTest.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Test\Unit\Model;
  7. use Magento\Customer\Api\CustomerMetadataInterface;
  8. use Magento\Customer\Model\FileProcessor;
  9. use Magento\Customer\Model\FileUploader;
  10. class FileUploaderTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /**
  13. * @var CustomerMetadataInterface|\PHPUnit_Framework_MockObject_MockObject
  14. */
  15. private $customerMetadataService;
  16. /**
  17. * @var \Magento\Customer\Api\AddressMetadataInterface|\PHPUnit_Framework_MockObject_MockObject
  18. */
  19. private $addressMetadataService;
  20. /**
  21. * @var \Magento\Customer\Model\Metadata\ElementFactory|\PHPUnit_Framework_MockObject_MockObject
  22. */
  23. private $elementFactory;
  24. /**
  25. * @var \Magento\Customer\Model\FileProcessorFactory|\PHPUnit_Framework_MockObject_MockObject
  26. */
  27. private $fileProcessorFactory;
  28. /**
  29. * @var \Magento\Customer\Api\Data\AttributeMetadataInterface|\PHPUnit_Framework_MockObject_MockObject
  30. */
  31. private $attributeMetadata;
  32. protected function setUp()
  33. {
  34. $this->customerMetadataService = $this->getMockBuilder(\Magento\Customer\Api\CustomerMetadataInterface::class)
  35. ->getMockForAbstractClass();
  36. $this->addressMetadataService = $this->getMockBuilder(\Magento\Customer\Api\AddressMetadataInterface::class)
  37. ->getMockForAbstractClass();
  38. $this->elementFactory = $this->getMockBuilder(\Magento\Customer\Model\Metadata\ElementFactory::class)
  39. ->disableOriginalConstructor()
  40. ->getMock();
  41. $this->fileProcessorFactory = $this->getMockBuilder(\Magento\Customer\Model\FileProcessorFactory::class)
  42. ->disableOriginalConstructor()
  43. ->setMethods(['create'])
  44. ->getMock();
  45. $this->attributeMetadata = $this->getMockBuilder(\Magento\Customer\Api\Data\AttributeMetadataInterface::class)
  46. ->getMockForAbstractClass();
  47. }
  48. protected function tearDown()
  49. {
  50. $_FILES = [];
  51. }
  52. /**
  53. * @param string $entityTypeCode
  54. * @param string $scope
  55. * @return FileUploader
  56. */
  57. private function getModel($entityTypeCode, $scope)
  58. {
  59. $model = new FileUploader(
  60. $this->customerMetadataService,
  61. $this->addressMetadataService,
  62. $this->elementFactory,
  63. $this->fileProcessorFactory,
  64. $this->attributeMetadata,
  65. $entityTypeCode,
  66. $scope
  67. );
  68. return $model;
  69. }
  70. public function testValidate()
  71. {
  72. $attributeCode = 'attribute_code';
  73. $filename = 'filename.ext1';
  74. $_FILES = [
  75. 'customer' => [
  76. 'name' => [
  77. $attributeCode => $filename,
  78. ],
  79. ],
  80. ];
  81. $formElement = $this->getMockBuilder(\Magento\Customer\Model\Metadata\Form\Image::class)
  82. ->disableOriginalConstructor()
  83. ->getMock();
  84. $formElement->expects($this->once())
  85. ->method('validateValue')
  86. ->with(['name' => $filename])
  87. ->willReturn(true);
  88. $this->elementFactory->expects($this->once())
  89. ->method('create')
  90. ->with($this->attributeMetadata, null, CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER)
  91. ->willReturn($formElement);
  92. $model = $this->getModel(CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER, 'customer');
  93. $this->assertTrue($model->validate());
  94. }
  95. public function testUpload()
  96. {
  97. $attributeCode = 'attribute_code';
  98. $attributeFrontendInput = 'image';
  99. $resultFileName = '/filename.ext1';
  100. $resultFilePath = 'filepath';
  101. $resultFileUrl = 'viewFileUrl';
  102. $allowedExtensions = 'ext1,EXT2 , eXt3'; // Added spaces, commas and upper-cases
  103. $expectedAllowedExtensions = [
  104. 'ext1',
  105. 'ext2',
  106. 'ext3',
  107. ];
  108. $_FILES = [
  109. 'customer' => [
  110. 'name' => [
  111. $attributeCode => 'filename',
  112. ],
  113. ],
  114. ];
  115. $expectedResult = [
  116. 'name' => $resultFileName,
  117. 'file' => $resultFileName,
  118. 'path' => $resultFilePath,
  119. 'tmp_name' => ltrim($resultFileName, '/'),
  120. 'url' => $resultFileUrl,
  121. ];
  122. $fileProcessor = $this->getMockBuilder(\Magento\Customer\Model\FileProcessor::class)
  123. ->disableOriginalConstructor()
  124. ->getMock();
  125. $fileProcessor->expects($this->once())
  126. ->method('saveTemporaryFile')
  127. ->with('customer[' . $attributeCode . ']')
  128. ->willReturn([
  129. 'name' => $resultFileName,
  130. 'path' => $resultFilePath,
  131. 'file' => $resultFileName,
  132. ]);
  133. $fileProcessor->expects($this->once())
  134. ->method('getViewUrl')
  135. ->with(FileProcessor::TMP_DIR . '/filename.ext1', $attributeFrontendInput)
  136. ->willReturn($resultFileUrl);
  137. $this->fileProcessorFactory->expects($this->once())
  138. ->method('create')
  139. ->with([
  140. 'entityTypeCode' => CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER,
  141. 'allowedExtensions' => $expectedAllowedExtensions,
  142. ])
  143. ->willReturn($fileProcessor);
  144. $validationRuleMock = $this->getMockBuilder(\Magento\Customer\Api\Data\ValidationRuleInterface::class)
  145. ->getMockForAbstractClass();
  146. $validationRuleMock->expects($this->once())
  147. ->method('getName')
  148. ->willReturn('file_extensions');
  149. $validationRuleMock->expects($this->once())
  150. ->method('getValue')
  151. ->willReturn($allowedExtensions);
  152. $this->attributeMetadata->expects($this->once())
  153. ->method('getFrontendInput')
  154. ->willReturn($attributeFrontendInput);
  155. $this->attributeMetadata->expects($this->once())
  156. ->method('getValidationRules')
  157. ->willReturn([$validationRuleMock]);
  158. $model = $this->getModel(CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER, 'customer');
  159. $this->assertEquals($expectedResult, $model->upload());
  160. }
  161. }