Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AccountManagementCustomAttributesTest.php
Go to the documentation of this file.
1 <?php
7 
14 use Magento\TestFramework\Helper\Customer as CustomerHelper;
16 use Magento\Framework\Webapi\Exception as HTTPExceptionCodes;
17 
24 {
25  const SERVICE_VERSION = 'V1';
26  const SERVICE_NAME = 'customerAccountManagementV1';
27  const RESOURCE_PATH = '/V1/customers';
28 
32  const ATTRIBUTE_CODE = 'attribute_code';
33  const ATTRIBUTE_VALUE = 'attribute_value';
34 
38  private $accountManagement;
39 
43  private $customerHelper;
44 
48  private $currentCustomerId;
49 
53  private $dataObjectProcessor;
54 
58  private $imageFactory;
59 
63  private $fileSystem;
64 
68  public function setUp()
69  {
70  $this->accountManagement = Bootstrap::getObjectManager()->get(
71  \Magento\Customer\Api\AccountManagementInterface::class
72  );
73 
74  $this->customerHelper = new CustomerHelper();
75 
76  $this->dataObjectProcessor = Bootstrap::getObjectManager()->create(
77  \Magento\Framework\Reflection\DataObjectProcessor::class
78  );
79 
80  $this->imageFactory = Bootstrap::getObjectManager()->get(\Magento\Framework\Api\ImageContentFactory::class);
81 
82  $this->fileSystem = Bootstrap::getObjectManager()->get(\Magento\Framework\Filesystem::class);
83  }
84 
85  public function tearDown()
86  {
87  if (!empty($this->currentCustomerId)) {
88  foreach ($this->currentCustomerId as $customerId) {
89  $serviceInfo = [
90  'rest' => [
91  'resourcePath' => self::RESOURCE_PATH . '/' . $customerId,
93  ],
94  'soap' => [
96  'serviceVersion' => self::SERVICE_VERSION,
97  'operation' => CustomerRepositoryTest::SERVICE_NAME . 'DeleteById',
98  ],
99  ];
100 
101  $response = $this->_webApiCall($serviceInfo, ['customerId' => $customerId]);
102 
103  $this->assertTrue($response);
104  }
105  }
106  $this->accountManagement = null;
107  $mediaDirectory = $this->fileSystem->getDirectoryWrite(DirectoryList::MEDIA);
109  }
110 
115  {
116  $testImagePath = __DIR__ . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'test_image.jpg';
117  $imageData = base64_encode(file_get_contents($testImagePath));
118  $image = $this->imageFactory->create()
119  ->setType('image/jpeg')
120  ->setName('sample.jpeg')
121  ->setBase64EncodedData($imageData);
122 
123  $imageData = $this->dataObjectProcessor->buildOutputDataArray(
124  $image,
125  \Magento\Framework\Api\Data\ImageContentInterface::class
126  );
127  return $this->createCustomerWithImageAttribute($imageData);
128  }
129 
136  protected function createCustomerWithImageAttribute($imageData)
137  {
138  $serviceInfo = [
139  'rest' => [
140  'resourcePath' => self::RESOURCE_PATH,
142  ],
143  'soap' => [
144  'service' => self::SERVICE_NAME,
145  'serviceVersion' => self::SERVICE_VERSION,
146  'operation' => self::SERVICE_NAME . 'CreateAccount',
147  ],
148  ];
149 
150  $customerData = $this->customerHelper->createSampleCustomerDataObject();
151 
152  $customerDataArray = $this->dataObjectProcessor->buildOutputDataArray(
154  \Magento\Customer\Api\Data\CustomerInterface::class
155  );
156  $customerDataArray['custom_attributes'][] = [
157  'attribute_code' => 'customer_image',
158  'value' => $imageData,
159  ];
160  $requestData = [
161  'customer' => $customerDataArray,
163  ];
164  $customerData = $this->_webApiCall($serviceInfo, $requestData);
165 
166  return $customerData;
167  }
168 
169  protected function verifyImageAttribute($customAttributeArray, $expectedFileName)
170  {
171  $imageAttributeFound = false;
172  foreach ($customAttributeArray as $customAttribute) {
173  if ($customAttribute[AttributeValue::ATTRIBUTE_CODE] == 'customer_image') {
174  $this->assertContains($expectedFileName, $customAttribute[AttributeValue::VALUE]);
175  $mediaDirectory = $this->fileSystem->getDirectoryWrite(DirectoryList::MEDIA);
176  $customerMediaPath = $mediaDirectory->getAbsolutePath(CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER);
177  $imageAttributeFound = file_exists($customerMediaPath . $customAttribute[AttributeValue::VALUE]);
178  $this->assertTrue($imageAttributeFound, 'Expected file was not created');
179  }
180  }
181  if (!$imageAttributeFound) {
182  $this->fail('Expected image attribute missing.');
183  }
184  }
185 
190  {
192  $this->currentCustomerId[] = $customerData['id'];
194  }
195 
200  {
201  $image = $this->imageFactory->create()
202  ->setType('image/jpeg')
203  ->setName('sample.jpeg')
204  ->setBase64EncodedData('INVALID_IMAGE_DATA');
205 
206  $imageData = $this->dataObjectProcessor->buildOutputDataArray(
207  $image,
208  \Magento\Framework\Api\Data\ImageContentInterface::class
209  );
210  $expectedMessage = 'The image content must be valid base64 encoded data.';
211  try {
212  $this->createCustomerWithImageAttribute($imageData);
213  } catch (\SoapFault $e) {
214  $this->assertContains(
215  $expectedMessage,
216  $e->getMessage(),
217  "Exception message does not match"
218  );
219  } catch (\Exception $e) {
220  $errorObj = $this->processRestExceptionResult($e);
221  $this->assertEquals($expectedMessage, $errorObj['message']);
222  $this->assertEquals(HTTPExceptionCodes::HTTP_BAD_REQUEST, $e->getCode());
223  }
224  }
225 
230  {
231  $customerDataArray = $this->createCustomerWithDefaultImageAttribute();
232  $previousCustomerData = $customerDataArray;
233 
234  $testImagePath = __DIR__ . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'buttons.png';
235  $imageData = base64_encode(file_get_contents($testImagePath));
236  $image = $this->imageFactory->create()
237  ->setType('image/png')
238  ->setName('buttons.png')
239  ->setBase64EncodedData($imageData);
240  $imageData = $this->dataObjectProcessor->buildOutputDataArray(
241  $image,
242  \Magento\Framework\Api\Data\ImageContentInterface::class
243  );
244 
245  //Replace image attribute
246  $customerDataArray['custom_attributes'][1] = [
247  'attribute_code' => 'customer_image',
248  'value' => $imageData,
249  ];
250  $requestData = [
251  'customer' => $customerDataArray,
253  ];
254 
255  $serviceInfo = [
256  'rest' => [
257  'resourcePath' => self::RESOURCE_PATH . "/{$customerDataArray[CustomerInterface::ID]}",
259  ],
260  'soap' => [
261  'service' => 'customerCustomerRepositoryV1',
262  'serviceVersion' => self::SERVICE_VERSION,
263  'operation' => 'customerCustomerRepositoryV1Save',
264  ],
265  ];
266  $customerData = $this->_webApiCall($serviceInfo, $requestData);
268 
269  //Verify that the previous image is deleted
270  $mediaDirectory = $this->fileSystem->getDirectoryWrite(DirectoryList::MEDIA);
271  $customerMediaPath = $mediaDirectory->getAbsolutePath(CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER);
272  $previousImagePath =
274  $this->assertFalse(file_exists($customerMediaPath . $previousImagePath));
275  }
276 }
$response
Definition: 404.php:11
$customerData
_webApiCall( $serviceInfo, $arguments=[], $webApiAdapterCode=null, $storeCode=null, $integration=null)
defined('TESTS_BP')||define('TESTS_BP' __DIR__
Definition: _bootstrap.php:60
$mediaDirectory