Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
MetadataProviderTest.php
Go to the documentation of this file.
1 <?php
7 
17 
21 class MetadataProviderTest extends \PHPUnit\Framework\TestCase
22 {
26  private $model;
27 
31  private $filter;
32 
36  private $localeDate;
37 
41  private $localeResolver;
42 
46  protected function setUp()
47  {
48  $this->filter = $this->getMockBuilder(\Magento\Ui\Component\MassAction\Filter::class)
49  ->disableOriginalConstructor()
50  ->getMock();
51 
52  $this->localeDate = $this->getMockBuilder(\Magento\Framework\Stdlib\DateTime\TimezoneInterface::class)
53  ->disableOriginalConstructor()
54  ->getMock();
55 
56  $this->localeResolver = $this->getMockBuilder(\Magento\Framework\Locale\ResolverInterface::class)
57  ->disableOriginalConstructor()
58  ->getMock();
59 
60  $this->localeResolver->expects($this->any())
61  ->method('getLocale')
62  ->willReturn(null);
63 
64  $objectManager = new ObjectManager($this);
65  $this->model = $objectManager->getObject(
66  \Magento\Ui\Model\Export\MetadataProvider::class,
67  [
68  'filter' => $this->filter,
69  'localeDate' => $this->localeDate,
70  'localeResolver' => $this->localeResolver,
71  'data' => ['component_name' => ['field']],
72  ]
73  );
74  }
75 
82  public function testGetHeaders(array $columnLabels, array $expected): void
83  {
84  $componentName = 'component_name';
85  $columnName = 'column_name';
86 
87  $component = $this->prepareColumns($componentName, $columnName, $columnLabels[0]);
88  $result = $this->model->getHeaders($component);
89  $this->assertTrue(is_array($result));
90  $this->assertCount(1, $result);
91  $this->assertEquals($expected, $result);
92  }
93 
97  public function getColumnsDataProvider(): array
98  {
99  return [
100  [['ID'],['"ID"']],
101  [['Name'],['Name']],
102  [['Id'],['Id']],
103  [['id'],['id']],
104  [['IDTEST'],['"IDTEST"']],
105  [['ID TEST'],['"ID TEST"']],
106  ];
107  }
108 
109  public function testGetFields()
110  {
111  $componentName = 'component_name';
112  $columnName = 'column_name';
113  $columnLabel = 'column_label';
114 
115  $component = $this->prepareColumns($componentName, $columnName, $columnLabel);
116 
117  $result = $this->model->getFields($component);
118  $this->assertTrue(is_array($result));
119  $this->assertCount(1, $result);
120  $this->assertEquals($columnName, $result[0]);
121  }
122 
131  protected function prepareColumns(
132  $componentName,
133  $columnName,
134  $columnLabel,
135  $columnActionsName = 'actions_name',
136  $columnActionsLabel = 'actions_label'
137  ) {
139  $component = $this->getMockBuilder(\Magento\Framework\View\Element\UiComponentInterface::class)
140  ->getMockForAbstractClass();
141 
143  $columns = $this->getMockBuilder(\Magento\Ui\Component\Listing\Columns::class)
144  ->disableOriginalConstructor()
145  ->getMock();
146 
148  $column = $this->getMockBuilder(\Magento\Ui\Component\Listing\Columns\Column::class)
149  ->disableOriginalConstructor()
150  ->getMock();
152  $columnActions = $this->getMockBuilder(\Magento\Ui\Component\Listing\Columns\Column::class)
153  ->disableOriginalConstructor()
154  ->getMock();
155 
156  $component->expects($this->any())
157  ->method('getName')
158  ->willReturn($componentName);
159  $component->expects($this->once())
160  ->method('getChildComponents')
161  ->willReturn([$columns]);
162 
163  $columns->expects($this->once())
164  ->method('getChildComponents')
165  ->willReturn([$column, $columnActions]);
166 
167  $column->expects($this->any())
168  ->method('getName')
169  ->willReturn($columnName);
170  $column->expects($this->any())
171  ->method('getData')
172  ->willReturnMap(
173  [
174  ['config/label', null, $columnLabel],
175  ['config/dataType', null, 'data_type'],
176  ]
177  );
178 
179  $columnActions->expects($this->any())
180  ->method('getName')
181  ->willReturn($columnActionsName);
182  $columnActions->expects($this->any())
183  ->method('getData')
184  ->willReturnMap(
185  [
186  ['config/label', null, $columnActionsLabel],
187  ['config/dataType', null, 'actions'],
188  ]
189  );
190 
191  return $component;
192  }
193 
201  public function testGetRowData($key, $fields, $options, $expected)
202  {
204  $document = $this->getMockBuilder(\Magento\Framework\Api\Search\DocumentInterface::class)
205  ->getMockForAbstractClass();
206 
207  $attribute = $this->getMockBuilder(\Magento\Framework\Api\AttributeInterface::class)
208  ->getMockForAbstractClass();
209 
210  $document->expects($this->once())
211  ->method('getCustomAttribute')
212  ->with($fields[0])
213  ->willReturn($attribute);
214 
215  $attribute->expects($this->once())
216  ->method('getValue')
217  ->willReturn($key);
218 
219  $result = $this->model->getRowData($document, $fields, $options);
220  $this->assertTrue(is_array($result));
221  $this->assertCount(1, $result);
222  $this->assertEquals($expected, $result);
223  }
224 
228  public function getRowDataProvider()
229  {
230  return [
231  [
232  'key' => 'key_1',
233  'fields' => ['column'],
234  'options' => [
235  'column' => [
236  'key_1' => 'value_1',
237  ],
238  ],
239  'expected' => [
240  'value_1',
241  ],
242  ],
243  [
244  'key' => 'key_2',
245  'fields' => ['column'],
246  'options' => [
247  'column' => [
248  'key_1' => 'value_1',
249  ],
250  ],
251  'expected' => [
252  '',
253  ],
254  ],
255  [
256  'key' => 'key_1',
257  'fields' => ['column'],
258  'options' => [],
259  'expected' => [
260  'key_1',
261  ],
262  ],
263  ];
264  }
265 
272  public function testGetOptions($filter, $options, $expected)
273  {
274  $component = $this->getMockBuilder(\Magento\Framework\View\Element\UiComponentInterface::class)
275  ->getMockForAbstractClass();
276 
277  $childComponent = $this->getMockBuilder(\Magento\Framework\View\Element\UiComponentInterface::class)
278  ->getMockForAbstractClass();
279 
280  $filters = $this->getMockBuilder(\Magento\Ui\Component\Filters::class)
281  ->disableOriginalConstructor()
282  ->getMock();
283 
284  $select = $this->getMockBuilder(\Magento\Ui\Component\Filters\Type\Select::class)
285  ->disableOriginalConstructor()
286  ->getMock();
287 
288  $this->filter->expects($this->once())
289  ->method('getComponent')
290  ->willReturn($component);
291 
292  $component->expects($this->once())
293  ->method('getChildComponents')
294  ->willReturn(['listing_top' => $childComponent]);
295 
296  $childComponent->expects($this->once())
297  ->method('getChildComponents')
298  ->willReturn([$filters]);
299 
300  $filters->expects($this->once())
301  ->method('getChildComponents')
302  ->willReturn([$select]);
303 
304  $select->expects($this->any())
305  ->method('getName')
306  ->willReturn($filter);
307  $select->expects($this->any())
308  ->method('getData')
309  ->with('config/options')
310  ->willReturn($options);
311 
312  $result = $this->model->getOptions();
313  $this->assertTrue(is_array($result));
314  $this->assertCount(1, $result);
315  $this->assertEquals($expected, $result);
316  }
317 
321  public function getOptionsDataProvider()
322  {
323  return [
324  [
325  'filter' => 'filter_name',
326  'options' => [
327  [
328  'value' => 'value_1',
329  'label' => 'label_1',
330  ]
331  ],
332  'expected' => [
333  'filter_name' => [
334  'value_1' => 'label_1',
335  ],
336  ],
337  ],
338  [
339  'filter' => 'filter_name',
340  'options' => [
341  [
342  'value' => [
343  [
344  'value' => 'value_2',
345  'label' => 'label_2',
346  ],
347  ],
348  'label' => 'label_1',
349  ]
350  ],
351  'expected' => [
352  'filter_name' => [
353  'value_2' => 'label_1label_2',
354  ],
355  ],
356  ],
357  [
358  'filter' => 'filter_name',
359  'options' => [
360  [
361  'value' => [
362  [
363  'value' => [
364  [
365  'value' => 'value_3',
366  'label' => 'label_3',
367  ]
368  ],
369  'label' => 'label_2',
370  ],
371  ],
372  'label' => 'label_1',
373  ]
374  ],
375  'expected' => [
376  'filter_name' => [
377  'value_3' => 'label_1label_2label_3',
378  ],
379  ],
380  ],
381  ];
382  }
383 
392  public function testConvertDate($fieldValue, $expected)
393  {
394  $componentName = 'component_name';
396  $document = $this->getMockBuilder(\Magento\Framework\DataObject::class)
397  ->disableOriginalConstructor()
398  ->getMock();
399 
400  $document->expects($this->once())
401  ->method('getData')
402  ->with('field')
403  ->willReturn($fieldValue);
404 
405  $this->localeDate->expects($this->once())
406  ->method('date')
407  ->willReturn(new \DateTime($fieldValue, new \DateTimeZone('UTC')));
408 
409  $document->expects($this->once())
410  ->method('setData')
411  ->with('field', $expected);
412 
413  $this->model->convertDate($document, $componentName);
414  }
415 
421  public function convertDateProvider()
422  {
423  return [
424  [
425  'fieldValue' => '@1534505233',
426  'expected' => 'Aug 17, 2018 11:27:13 AM',
427  ],
428  [
429  'fieldValue' => '@1534530000',
430  'expected' => 'Aug 17, 2018 06:20:00 PM',
431  ],
432  ];
433  }
434 }
$objectManager
Definition: bootstrap.php:17
$fields
Definition: details.phtml:14
$columns
Definition: default.phtml:15
testGetHeaders(array $columnLabels, array $expected)
$filters
Definition: uploader.phtml:11