Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ProductAttributeRepositoryTest.php
Go to the documentation of this file.
1 <?php
8 namespace Magento\Catalog\Api;
9 
10 use Magento\Framework\Webapi\Exception as HTTPExceptionCodes;
12 
14 {
15  const SERVICE_NAME = 'catalogProductAttributeRepositoryV1';
16  const SERVICE_VERSION = 'V1';
17  const RESOURCE_PATH = '/V1/products/attributes';
18 
22  private $createdAttributes = [];
23 
28  public function testGet()
29  {
30  $attributeCode = 'test_attribute_code_333';
32 
33  $this->assertTrue(is_array($attribute));
34  $this->assertArrayHasKey('attribute_id', $attribute);
35  $this->assertArrayHasKey('attribute_code', $attribute);
36  $this->assertEquals($attributeCode, $attribute['attribute_code']);
37  }
38 
42  public function testGetList()
43  {
44  $searchCriteria = [
45  'searchCriteria' => [
46  'filter_groups' => [
47  [
48  'filters' => [
49  [
50  'field' => 'frontend_input',
51  'value' => 'textarea',
52  'condition_type' => 'eq',
53  ],
54  ],
55  ],
56  ],
57  'current_page' => 1,
58  'page_size' => 2,
59  ],
61  ];
62 
63  $serviceInfo = [
64  'rest' => [
65  'resourcePath' => self::RESOURCE_PATH . '?' . http_build_query($searchCriteria),
67  ],
68  'soap' => [
69  'service' => self::SERVICE_NAME,
70  'serviceVersion' => self::SERVICE_VERSION,
71  'operation' => self::SERVICE_NAME . 'GetList',
72  ],
73  ];
74  $response = $this->_webApiCall($serviceInfo, $searchCriteria);
75 
76  $this->assertArrayHasKey('search_criteria', $response);
77  $this->assertArrayHasKey('total_count', $response);
78  $this->assertArrayHasKey('items', $response);
79 
80  $this->assertEquals($searchCriteria['searchCriteria'], $response['search_criteria']);
81  $this->assertTrue($response['total_count'] > 0);
82  $this->assertTrue(count($response['items']) > 0);
83 
84  $this->assertNotNull($response['items'][0]['default_frontend_label']);
85  $this->assertNotNull($response['items'][0]['attribute_id']);
86  }
87 
92  public function testCreate()
93  {
94  $attributeCode = uniqid('label_attr_code');
96 
97  $expectedData = [
98  'attribute_code' => $attributeCode,
99  'is_required' => true,
100  'entity_type_id' => "4",
101  "frontend_input" => "select",
102  "is_visible_on_front" => true,
103  "is_searchable" => true,
104  "is_visible_in_advanced_search" => true,
105  "is_filterable" => true,
106  "is_filterable_in_search" => true,
107  ];
108 
109  $this->assertEquals('default_label', $attribute['default_frontend_label']);
110  $this->assertEquals('front_lbl_store1', $attribute['frontend_labels'][0]['label']);
111  foreach ($expectedData as $key => $value) {
112  $this->assertEquals($value, $attribute[$key]);
113  }
114  //Validate options
115  //'Blue' should be first as it has sort_order = 0
116  $this->assertEquals('Default Blue', $attribute['options'][1]['label']);
117  $this->assertArrayHasKey('default_value', $attribute);
118  //'Blue' should be set as default
119  $this->assertEquals($attribute['default_value'], $attribute['options'][1]['value']);
120  $this->assertEquals('Default Red', $attribute['options'][2]['label']);
121  }
122 
128  {
129  $attributeCode = 'test_attribute_code_333';
130  try {
132  $this->fail("Expected exception");
133  } catch (\SoapFault $e) {
134  //Expects soap exception
135  } catch (\Exception $e) {
136  $this->assertEquals(HTTPExceptionCodes::HTTP_BAD_REQUEST, $e->getCode());
137  }
138  }
139 
144  public function testUpdate()
145  {
146  $attributeCode = uniqid('label_attr_code');
148 
149  //Make sure that 'Blue' is set as default
150  $this->assertEquals($attribute['default_value'], $attribute['options'][1]['value']);
151  $attributeData = [
152  'attribute' => [
153  'attribute_id' => $attribute['attribute_id'],
154  'attribute_code' => $attributeCode,
155  'entity_type_id' => 4,
156  'is_used_in_grid' => true,
157  //Update existing
158  'default_frontend_label' => 'default_label_new',
159  'frontend_labels' => [
160  //Update existing
161  ['store_id' => 0, 'label' => 'front_lbl_store0_new'],
162  ['store_id' => 1, 'label' => 'front_lbl_store1_new'],
163  ],
164  "options" => [
165  //Update existing
166  [
167  "value" => $attribute['options'][1]['value'],
168  "label" => "New Label",
169  "store_labels" => [
170  [
171  "store_id" => 1,
172  "label" => "Default Blue Updated"
173  ]
174  ]
175  ],
176  //Add new option
177  [
178  "label" => "Green",
179  "value" => "",
180  "sort_order" => 200,
181  "is_default" => true,
182  "store_labels" => [
183  [
184  "store_id" => 0,
185  "label" => "Admin Green"
186  ],
187  [
188  "store_id" => 1,
189  "label" => "Default Green"
190  ]
191  ]
192  ]
193  ],
194  'is_required' => false,
195  'frontend_input' => 'select',
196  ],
197  ];
199 
200  $this->assertEquals($attribute['attribute_id'], $result['attribute_id']);
201  $this->assertEquals(true, $result['is_used_in_grid']);
202  $this->assertEquals($attributeCode, $result['attribute_code']);
203  $this->assertEquals('default_label_new', $result['default_frontend_label']);
204  $this->assertEquals('front_lbl_store1_new', $result['frontend_labels'][0]['label']);
205  //New option set as default
206  $this->assertEquals($result['options'][3]['value'], $result['default_value']);
207  $this->assertEquals("Default Blue Updated", $result['options'][1]['label']);
208  }
209 
215  {
216  $attributeCode = uniqid('label_attr_code');
218 
219  $attributeData = [
220  'attribute' => [
221  'attribute_id' => $attribute['attribute_id'],
222  'attribute_code' => $attributeCode,
223  'entity_type_id' => 4,
224  'is_used_in_grid' => true,
225  'frontend_labels' => [
226  //Update existing
227  ['store_id' => 0, 'label' => 'front_lbl_store0_new'],
228  ['store_id' => 1, 'label' => 'front_lbl_store1_new'],
229  ],
230  'is_required' => false,
231  'frontend_input' => 'select',
232  ],
233  ];
235 
236  $this->assertEquals($attribute['attribute_id'], $result['attribute_id']);
237  $this->assertEquals(true, $result['is_used_in_grid']);
238  $this->assertEquals($attributeCode, $result['attribute_code']);
239  $this->assertEquals('front_lbl_store0_new', $result['default_frontend_label']);
240  $this->assertEquals('front_lbl_store1_new', $result['frontend_labels'][0]['label']);
241  }
242 
248  {
249  $attributeCode = uniqid('label_attr_code');
251 
252  $attributeData = [
253  'attribute' => [
254  'attribute_id' => $attribute['attribute_id'],
255  'attribute_code' => $attributeCode,
256  'entity_type_id' => 4,
257  'is_used_in_grid' => true,
258  'frontend_labels' => [
259  //Update existing
260  ['store_id' => 1, 'label' => 'front_lbl_store1_new'],
261  ],
262  'is_required' => false,
263  'frontend_input' => 'select',
264  ],
265  ];
267 
268  $this->assertEquals($attribute['attribute_id'], $result['attribute_id']);
269  $this->assertEquals(true, $result['is_used_in_grid']);
270  $this->assertEquals($attributeCode, $result['attribute_code']);
271  $this->assertEquals('default_label', $result['default_frontend_label']);
272  $this->assertEquals('front_lbl_store1_new', $result['frontend_labels'][0]['label']);
273  }
274 
279  public function testUpdateWithNewOption()
280  {
281  $attributeCode = uniqid('label_attr_code');
283 
284  $attributeData = [
285  'attribute' => [
286  'attribute_id' => $attribute['attribute_id'],
287  'attribute_code' => $attributeCode,
288  'entity_type_id' => 4,
289  'is_required' => true,
290  'frontend_labels' => [
291  ['store_id' => 0, 'label' => 'front_lbl_new'],
292  ],
293  "options" => [
294  [
295  "value" => 'option',
296  "label" => "New Label",
297  "store_labels" => [
298  [
299  "store_id" => 1,
300  "label" => "new label"
301  ]
302  ]
303  ],
304  ],
305  'frontend_input' => 'select',
306  ]
307  ];
308 
310  $this->assertEquals(4, count($output['options']));
311  }
312 
317  public function testDeleteById()
318  {
319  $attributeCode = 'test_attribute_code_333';
320  $this->assertTrue($this->deleteAttribute($attributeCode));
321  }
322 
327  {
328  $attributeCode = 'some_test_code';
329  $expectedMessage =
330  'The attribute with a "%1" attributeCode doesn\'t exist. Verify the attribute and try again.';
331 
332  $serviceInfo = [
333  'rest' => [
334  'resourcePath' => self::RESOURCE_PATH . '/' . $attributeCode,
336  ],
337  'soap' => [
338  'service' => self::SERVICE_NAME,
339  'serviceVersion' => self::SERVICE_VERSION,
340  'operation' => self::SERVICE_NAME . 'deleteById',
341  ],
342  ];
343 
344  try {
345  $this->_webApiCall($serviceInfo, ['attributeCode' => $attributeCode]);
346  $this->fail("Expected exception");
347  } catch (\SoapFault $e) {
348  $this->assertContains(
349  $expectedMessage,
350  $e->getMessage(),
351  "SoapFault does not contain expected message."
352  );
353  } catch (\Exception $e) {
354  $errorObj = $this->processRestExceptionResult($e);
355  $this->assertEquals($expectedMessage, $errorObj['message']);
356  $this->assertEquals([$attributeCode], $errorObj['parameters']);
357  $this->assertEquals(HTTPExceptionCodes::HTTP_NOT_FOUND, $e->getCode());
358  }
359  }
360 
365  protected function createAttribute($attributeCode)
366  {
367  $attributeData = [
368  'attribute' => [
369  'attribute_code' => $attributeCode,
370  'entity_type_id' => '4',
371  "default_frontend_label" => 'default_label',
372  'frontend_labels' => [
373  ['store_id' => 0, 'label' => 'front_lbl_store0'],
374  ['store_id' => 1, 'label' => 'front_lbl_store1'],
375  ],
376  'is_required' => true,
377  "default_value" => "",
378  "frontend_input" => "select",
379  "is_visible_on_front" => true,
380  "is_searchable" => true,
381  "is_visible_in_advanced_search" => true,
382  "is_filterable" => true,
383  "is_filterable_in_search" => true,
384  "options" => [
385  [
386  "label" => "Red",
387  "value" => "",
388  "sort_order" => 100,
389  "is_default" => false,
390  "store_labels" => [
391  [
392  "store_id" => 0,
393  "label" => "Admin Red"
394  ],
395  [
396  "store_id" => 1,
397  "label" => "Default Red"
398  ]
399  ]
400  ],
401  [
402  "label" => "Blue",
403  "value" => "",
404  "sort_order" => 0,
405  "is_default" => true,
406  "store_labels" => [
407  [
408  "store_id" => 0,
409  "label" => "Admin Blue"
410  ],
411  [
412  "store_id" => 1,
413  "label" => "Default Blue"
414  ]
415  ]
416  ]
417  ]
418  ],
419  ];
420 
421  $serviceInfo = [
422  'rest' => [
423  'resourcePath' => self::RESOURCE_PATH . '/',
425  ],
426  'soap' => [
427  'service' => self::SERVICE_NAME,
428  'serviceVersion' => self::SERVICE_VERSION,
429  'operation' => self::SERVICE_NAME . 'Save',
430  ],
431  ];
432  $attribute = $this->_webApiCall($serviceInfo, $attributeData);
433  if (isset($attribute['attribute_id']) && $attribute['attribute_id']) {
434  $this->createdAttributes[] = $attributeCode;
435  }
436  return $attribute;
437  }
438 
443  protected function getAttribute($attributeCode)
444  {
445  $serviceInfo = [
446  'rest' => [
447  'resourcePath' => self::RESOURCE_PATH . '/' . $attributeCode,
449  ],
450  'soap' => [
451  'service' => self::SERVICE_NAME,
452  'serviceVersion' => self::SERVICE_VERSION,
453  'operation' => self::SERVICE_NAME . 'Get',
454  ],
455  ];
456 
457  return $this->_webApiCall($serviceInfo, ['attributeCode' => $attributeCode]);
458  }
459 
466  protected function deleteAttribute($attributeCode)
467  {
468  $serviceInfo = [
469  'rest' => [
470  'resourcePath' => self::RESOURCE_PATH . '/' . $attributeCode,
472  ],
473  'soap' => [
474  'service' => self::SERVICE_NAME,
475  'serviceVersion' => self::SERVICE_VERSION,
476  'operation' => self::SERVICE_NAME . 'deleteById',
477  ],
478  ];
479  return $this->_webApiCall($serviceInfo, ['attributeCode' => $attributeCode]);
480  }
481 
489  {
490  if (TESTS_WEB_API_ADAPTER == self::ADAPTER_SOAP) {
491  $attributeData['attribute']['attributeCode'] = $attributeCode;
492  }
493  $serviceInfo = [
494  'rest' => [
495  'resourcePath' => self::RESOURCE_PATH . '/' . $attributeCode,
497  ],
498  'soap' => [
499  'service' => self::SERVICE_NAME,
500  'serviceVersion' => self::SERVICE_VERSION,
501  'operation' => self::SERVICE_NAME . 'Save',
502  ],
503  ];
504  return $this->_webApiCall($serviceInfo, $attributeData);
505  }
506 
510  protected function tearDown()
511  {
512  foreach ($this->createdAttributes as $attributeCode) {
513  $this->deleteAttribute($attributeCode);
514  }
515  }
516 }
$response
Definition: 404.php:11
_webApiCall( $serviceInfo, $arguments=[], $webApiAdapterCode=null, $storeCode=null, $integration=null)
$searchCriteria
$value
Definition: gender.phtml:16
$attributeCode
Definition: extend.phtml:12