AccountManagementCustomAttributesTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Api;
  7. use Magento\Customer\Api\Data\CustomerInterface;
  8. use Magento\Customer\Model\AccountManagement;
  9. use Magento\Framework\Api\AttributeValue;
  10. use Magento\Framework\Api\CustomAttributesDataInterface;
  11. use Magento\Framework\App\Filesystem\DirectoryList;
  12. use Magento\TestFramework\Helper\Bootstrap;
  13. use Magento\TestFramework\Helper\Customer as CustomerHelper;
  14. use Magento\TestFramework\TestCase\WebapiAbstract;
  15. use Magento\Framework\Webapi\Exception as HTTPExceptionCodes;
  16. /**
  17. * Test class for Customer's custom attributes
  18. *
  19. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  20. */
  21. class AccountManagementCustomAttributesTest extends WebapiAbstract
  22. {
  23. const SERVICE_VERSION = 'V1';
  24. const SERVICE_NAME = 'customerAccountManagementV1';
  25. const RESOURCE_PATH = '/V1/customers';
  26. /**
  27. * Sample values for testing
  28. */
  29. const ATTRIBUTE_CODE = 'attribute_code';
  30. const ATTRIBUTE_VALUE = 'attribute_value';
  31. /**
  32. * @var AccountManagementInterface
  33. */
  34. private $accountManagement;
  35. /**
  36. * @var CustomerHelper
  37. */
  38. private $customerHelper;
  39. /**
  40. * @var array
  41. */
  42. private $currentCustomerId;
  43. /**
  44. * @var \Magento\Framework\Reflection\DataObjectProcessor
  45. */
  46. private $dataObjectProcessor;
  47. /**
  48. * @var \Magento\Framework\Api\Data\ImageFactory
  49. */
  50. private $imageFactory;
  51. /**
  52. * @var \Magento\Framework\Filesystem
  53. */
  54. private $fileSystem;
  55. /**
  56. * Execute per test initialization.
  57. */
  58. public function setUp()
  59. {
  60. $this->accountManagement = Bootstrap::getObjectManager()->get(
  61. \Magento\Customer\Api\AccountManagementInterface::class
  62. );
  63. $this->customerHelper = new CustomerHelper();
  64. $this->dataObjectProcessor = Bootstrap::getObjectManager()->create(
  65. \Magento\Framework\Reflection\DataObjectProcessor::class
  66. );
  67. $this->imageFactory = Bootstrap::getObjectManager()->get(\Magento\Framework\Api\ImageContentFactory::class);
  68. $this->fileSystem = Bootstrap::getObjectManager()->get(\Magento\Framework\Filesystem::class);
  69. }
  70. public function tearDown()
  71. {
  72. if (!empty($this->currentCustomerId)) {
  73. foreach ($this->currentCustomerId as $customerId) {
  74. $serviceInfo = [
  75. 'rest' => [
  76. 'resourcePath' => self::RESOURCE_PATH . '/' . $customerId,
  77. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_DELETE,
  78. ],
  79. 'soap' => [
  80. 'service' => CustomerRepositoryTest::SERVICE_NAME,
  81. 'serviceVersion' => self::SERVICE_VERSION,
  82. 'operation' => CustomerRepositoryTest::SERVICE_NAME . 'DeleteById',
  83. ],
  84. ];
  85. $response = $this->_webApiCall($serviceInfo, ['customerId' => $customerId]);
  86. $this->assertTrue($response);
  87. }
  88. }
  89. $this->accountManagement = null;
  90. $mediaDirectory = $this->fileSystem->getDirectoryWrite(DirectoryList::MEDIA);
  91. $mediaDirectory->delete(CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER);
  92. }
  93. /**
  94. * Create customer with a sample image file
  95. */
  96. protected function createCustomerWithDefaultImageAttribute()
  97. {
  98. $testImagePath = __DIR__ . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'test_image.jpg';
  99. $imageData = base64_encode(file_get_contents($testImagePath));
  100. $image = $this->imageFactory->create()
  101. ->setType('image/jpeg')
  102. ->setName('sample.jpeg')
  103. ->setBase64EncodedData($imageData);
  104. $imageData = $this->dataObjectProcessor->buildOutputDataArray(
  105. $image,
  106. \Magento\Framework\Api\Data\ImageContentInterface::class
  107. );
  108. return $this->createCustomerWithImageAttribute($imageData);
  109. }
  110. /**
  111. * Create customer with image attribute
  112. *
  113. * @param array $imageData
  114. * @return array Customer data as array
  115. */
  116. protected function createCustomerWithImageAttribute($imageData)
  117. {
  118. $serviceInfo = [
  119. 'rest' => [
  120. 'resourcePath' => self::RESOURCE_PATH,
  121. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST,
  122. ],
  123. 'soap' => [
  124. 'service' => self::SERVICE_NAME,
  125. 'serviceVersion' => self::SERVICE_VERSION,
  126. 'operation' => self::SERVICE_NAME . 'CreateAccount',
  127. ],
  128. ];
  129. $customerData = $this->customerHelper->createSampleCustomerDataObject();
  130. $customerDataArray = $this->dataObjectProcessor->buildOutputDataArray(
  131. $customerData,
  132. \Magento\Customer\Api\Data\CustomerInterface::class
  133. );
  134. $customerDataArray['custom_attributes'][] = [
  135. 'attribute_code' => 'customer_image',
  136. 'value' => $imageData,
  137. ];
  138. $requestData = [
  139. 'customer' => $customerDataArray,
  140. 'password' => \Magento\TestFramework\Helper\Customer::PASSWORD
  141. ];
  142. $customerData = $this->_webApiCall($serviceInfo, $requestData);
  143. return $customerData;
  144. }
  145. protected function verifyImageAttribute($customAttributeArray, $expectedFileName)
  146. {
  147. $imageAttributeFound = false;
  148. foreach ($customAttributeArray as $customAttribute) {
  149. if ($customAttribute[AttributeValue::ATTRIBUTE_CODE] == 'customer_image') {
  150. $this->assertContains($expectedFileName, $customAttribute[AttributeValue::VALUE]);
  151. $mediaDirectory = $this->fileSystem->getDirectoryWrite(DirectoryList::MEDIA);
  152. $customerMediaPath = $mediaDirectory->getAbsolutePath(CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER);
  153. $imageAttributeFound = file_exists($customerMediaPath . $customAttribute[AttributeValue::VALUE]);
  154. $this->assertTrue($imageAttributeFound, 'Expected file was not created');
  155. }
  156. }
  157. if (!$imageAttributeFound) {
  158. $this->fail('Expected image attribute missing.');
  159. }
  160. }
  161. /**
  162. * @magentoApiDataFixture Magento/Customer/_files/attribute_user_defined_custom_attribute.php
  163. */
  164. public function testCreateCustomerWithImageAttribute()
  165. {
  166. $customerData = $this->createCustomerWithDefaultImageAttribute();
  167. $this->currentCustomerId[] = $customerData['id'];
  168. $this->verifyImageAttribute($customerData[CustomAttributesDataInterface::CUSTOM_ATTRIBUTES], 'sample.jpeg');
  169. }
  170. /**
  171. * @magentoApiDataFixture Magento/Customer/_files/attribute_user_defined_custom_attribute.php
  172. */
  173. public function testCreateCustomerWithInvalidImageAttribute()
  174. {
  175. $image = $this->imageFactory->create()
  176. ->setType('image/jpeg')
  177. ->setName('sample.jpeg')
  178. ->setBase64EncodedData('INVALID_IMAGE_DATA');
  179. $imageData = $this->dataObjectProcessor->buildOutputDataArray(
  180. $image,
  181. \Magento\Framework\Api\Data\ImageContentInterface::class
  182. );
  183. $expectedMessage = 'The image content must be valid base64 encoded data.';
  184. try {
  185. $this->createCustomerWithImageAttribute($imageData);
  186. } catch (\SoapFault $e) {
  187. $this->assertContains(
  188. $expectedMessage,
  189. $e->getMessage(),
  190. "Exception message does not match"
  191. );
  192. } catch (\Exception $e) {
  193. $errorObj = $this->processRestExceptionResult($e);
  194. $this->assertEquals($expectedMessage, $errorObj['message']);
  195. $this->assertEquals(HTTPExceptionCodes::HTTP_BAD_REQUEST, $e->getCode());
  196. }
  197. }
  198. /**
  199. * @magentoApiDataFixture Magento/Customer/_files/attribute_user_defined_custom_attribute.php
  200. */
  201. public function testUpdateCustomerWithImageAttribute()
  202. {
  203. $customerDataArray = $this->createCustomerWithDefaultImageAttribute();
  204. $previousCustomerData = $customerDataArray;
  205. $testImagePath = __DIR__ . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'buttons.png';
  206. $imageData = base64_encode(file_get_contents($testImagePath));
  207. $image = $this->imageFactory->create()
  208. ->setType('image/png')
  209. ->setName('buttons.png')
  210. ->setBase64EncodedData($imageData);
  211. $imageData = $this->dataObjectProcessor->buildOutputDataArray(
  212. $image,
  213. \Magento\Framework\Api\Data\ImageContentInterface::class
  214. );
  215. //Replace image attribute
  216. $customerDataArray['custom_attributes'][1] = [
  217. 'attribute_code' => 'customer_image',
  218. 'value' => $imageData,
  219. ];
  220. $requestData = [
  221. 'customer' => $customerDataArray,
  222. 'password' => \Magento\TestFramework\Helper\Customer::PASSWORD
  223. ];
  224. $serviceInfo = [
  225. 'rest' => [
  226. 'resourcePath' => self::RESOURCE_PATH . "/{$customerDataArray[CustomerInterface::ID]}",
  227. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT,
  228. ],
  229. 'soap' => [
  230. 'service' => 'customerCustomerRepositoryV1',
  231. 'serviceVersion' => self::SERVICE_VERSION,
  232. 'operation' => 'customerCustomerRepositoryV1Save',
  233. ],
  234. ];
  235. $customerData = $this->_webApiCall($serviceInfo, $requestData);
  236. $this->verifyImageAttribute($customerData[CustomAttributesDataInterface::CUSTOM_ATTRIBUTES], 'buttons.png');
  237. //Verify that the previous image is deleted
  238. $mediaDirectory = $this->fileSystem->getDirectoryWrite(DirectoryList::MEDIA);
  239. $customerMediaPath = $mediaDirectory->getAbsolutePath(CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER);
  240. $previousImagePath =
  241. $previousCustomerData[CustomAttributesDataInterface::CUSTOM_ATTRIBUTES][0][AttributeValue::VALUE];
  242. $this->assertFalse(file_exists($customerMediaPath . $previousImagePath));
  243. }
  244. }