MetadataProviderTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Ui\Test\Unit\Model\Export;
  7. use Magento\Framework\Api\Search\DocumentInterface;
  8. use Magento\Framework\Locale\ResolverInterface;
  9. use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
  10. use Magento\Framework\View\Element\UiComponentInterface;
  11. use Magento\Ui\Component\Listing\Columns;
  12. use Magento\Ui\Component\Listing\Columns\Column;
  13. use Magento\Ui\Component\MassAction\Filter;
  14. use Magento\Ui\Model\Export\MetadataProvider;
  15. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  16. /**
  17. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  18. */
  19. class MetadataProviderTest extends \PHPUnit\Framework\TestCase
  20. {
  21. /**
  22. * @var MetadataProvider
  23. */
  24. private $model;
  25. /**
  26. * @var Filter | \PHPUnit_Framework_MockObject_MockObject
  27. */
  28. private $filter;
  29. /**
  30. * @var TimezoneInterface | \PHPUnit_Framework_MockObject_MockObject
  31. */
  32. private $localeDate;
  33. /**
  34. * @var ResolverInterface | \PHPUnit_Framework_MockObject_MockObject
  35. */
  36. private $localeResolver;
  37. /**
  38. * @inheritdoc
  39. */
  40. protected function setUp()
  41. {
  42. $this->filter = $this->getMockBuilder(\Magento\Ui\Component\MassAction\Filter::class)
  43. ->disableOriginalConstructor()
  44. ->getMock();
  45. $this->localeDate = $this->getMockBuilder(\Magento\Framework\Stdlib\DateTime\TimezoneInterface::class)
  46. ->disableOriginalConstructor()
  47. ->getMock();
  48. $this->localeResolver = $this->getMockBuilder(\Magento\Framework\Locale\ResolverInterface::class)
  49. ->disableOriginalConstructor()
  50. ->getMock();
  51. $this->localeResolver->expects($this->any())
  52. ->method('getLocale')
  53. ->willReturn(null);
  54. $objectManager = new ObjectManager($this);
  55. $this->model = $objectManager->getObject(
  56. \Magento\Ui\Model\Export\MetadataProvider::class,
  57. [
  58. 'filter' => $this->filter,
  59. 'localeDate' => $this->localeDate,
  60. 'localeResolver' => $this->localeResolver,
  61. 'data' => ['component_name' => ['field']],
  62. ]
  63. );
  64. }
  65. /**
  66. * @param array $columnLabels
  67. * @param array $expected
  68. * @return void
  69. * @dataProvider getColumnsDataProvider
  70. */
  71. public function testGetHeaders(array $columnLabels, array $expected): void
  72. {
  73. $componentName = 'component_name';
  74. $columnName = 'column_name';
  75. $component = $this->prepareColumns($componentName, $columnName, $columnLabels[0]);
  76. $result = $this->model->getHeaders($component);
  77. $this->assertTrue(is_array($result));
  78. $this->assertCount(1, $result);
  79. $this->assertEquals($expected, $result);
  80. }
  81. /**
  82. * @return array
  83. */
  84. public function getColumnsDataProvider(): array
  85. {
  86. return [
  87. [['ID'],['"ID"']],
  88. [['Name'],['Name']],
  89. [['Id'],['Id']],
  90. [['id'],['id']],
  91. [['IDTEST'],['"IDTEST"']],
  92. [['ID TEST'],['"ID TEST"']],
  93. ];
  94. }
  95. public function testGetFields()
  96. {
  97. $componentName = 'component_name';
  98. $columnName = 'column_name';
  99. $columnLabel = 'column_label';
  100. $component = $this->prepareColumns($componentName, $columnName, $columnLabel);
  101. $result = $this->model->getFields($component);
  102. $this->assertTrue(is_array($result));
  103. $this->assertCount(1, $result);
  104. $this->assertEquals($columnName, $result[0]);
  105. }
  106. /**
  107. * @param string $componentName
  108. * @param string $columnName
  109. * @param string $columnLabel
  110. * @param string $columnActionsName
  111. * @param string $columnActionsLabel
  112. * @return UiComponentInterface|\PHPUnit_Framework_MockObject_MockObject
  113. */
  114. protected function prepareColumns(
  115. $componentName,
  116. $columnName,
  117. $columnLabel,
  118. $columnActionsName = 'actions_name',
  119. $columnActionsLabel = 'actions_label'
  120. ) {
  121. /** @var UiComponentInterface|\PHPUnit_Framework_MockObject_MockObject $component */
  122. $component = $this->getMockBuilder(\Magento\Framework\View\Element\UiComponentInterface::class)
  123. ->getMockForAbstractClass();
  124. /** @var Columns|\PHPUnit_Framework_MockObject_MockObject $columns */
  125. $columns = $this->getMockBuilder(\Magento\Ui\Component\Listing\Columns::class)
  126. ->disableOriginalConstructor()
  127. ->getMock();
  128. /** @var Column|\PHPUnit_Framework_MockObject_MockObject $column */
  129. $column = $this->getMockBuilder(\Magento\Ui\Component\Listing\Columns\Column::class)
  130. ->disableOriginalConstructor()
  131. ->getMock();
  132. /** @var Column|\PHPUnit_Framework_MockObject_MockObject $columnActions */
  133. $columnActions = $this->getMockBuilder(\Magento\Ui\Component\Listing\Columns\Column::class)
  134. ->disableOriginalConstructor()
  135. ->getMock();
  136. $component->expects($this->any())
  137. ->method('getName')
  138. ->willReturn($componentName);
  139. $component->expects($this->once())
  140. ->method('getChildComponents')
  141. ->willReturn([$columns]);
  142. $columns->expects($this->once())
  143. ->method('getChildComponents')
  144. ->willReturn([$column, $columnActions]);
  145. $column->expects($this->any())
  146. ->method('getName')
  147. ->willReturn($columnName);
  148. $column->expects($this->any())
  149. ->method('getData')
  150. ->willReturnMap(
  151. [
  152. ['config/label', null, $columnLabel],
  153. ['config/dataType', null, 'data_type'],
  154. ]
  155. );
  156. $columnActions->expects($this->any())
  157. ->method('getName')
  158. ->willReturn($columnActionsName);
  159. $columnActions->expects($this->any())
  160. ->method('getData')
  161. ->willReturnMap(
  162. [
  163. ['config/label', null, $columnActionsLabel],
  164. ['config/dataType', null, 'actions'],
  165. ]
  166. );
  167. return $component;
  168. }
  169. /**
  170. * @param string $key
  171. * @param array $fields
  172. * @param array $options
  173. * @param array $expected
  174. * @dataProvider getRowDataProvider
  175. */
  176. public function testGetRowData($key, $fields, $options, $expected)
  177. {
  178. /** @var DocumentInterface|\PHPUnit_Framework_MockObject_MockObject $document */
  179. $document = $this->getMockBuilder(\Magento\Framework\Api\Search\DocumentInterface::class)
  180. ->getMockForAbstractClass();
  181. $attribute = $this->getMockBuilder(\Magento\Framework\Api\AttributeInterface::class)
  182. ->getMockForAbstractClass();
  183. $document->expects($this->once())
  184. ->method('getCustomAttribute')
  185. ->with($fields[0])
  186. ->willReturn($attribute);
  187. $attribute->expects($this->once())
  188. ->method('getValue')
  189. ->willReturn($key);
  190. $result = $this->model->getRowData($document, $fields, $options);
  191. $this->assertTrue(is_array($result));
  192. $this->assertCount(1, $result);
  193. $this->assertEquals($expected, $result);
  194. }
  195. /**
  196. * @return array
  197. */
  198. public function getRowDataProvider()
  199. {
  200. return [
  201. [
  202. 'key' => 'key_1',
  203. 'fields' => ['column'],
  204. 'options' => [
  205. 'column' => [
  206. 'key_1' => 'value_1',
  207. ],
  208. ],
  209. 'expected' => [
  210. 'value_1',
  211. ],
  212. ],
  213. [
  214. 'key' => 'key_2',
  215. 'fields' => ['column'],
  216. 'options' => [
  217. 'column' => [
  218. 'key_1' => 'value_1',
  219. ],
  220. ],
  221. 'expected' => [
  222. '',
  223. ],
  224. ],
  225. [
  226. 'key' => 'key_1',
  227. 'fields' => ['column'],
  228. 'options' => [],
  229. 'expected' => [
  230. 'key_1',
  231. ],
  232. ],
  233. ];
  234. }
  235. /**
  236. * @param string $filter
  237. * @param array $options
  238. * @param array $expected
  239. * @dataProvider getOptionsDataProvider
  240. */
  241. public function testGetOptions($filter, $options, $expected)
  242. {
  243. $component = $this->getMockBuilder(\Magento\Framework\View\Element\UiComponentInterface::class)
  244. ->getMockForAbstractClass();
  245. $childComponent = $this->getMockBuilder(\Magento\Framework\View\Element\UiComponentInterface::class)
  246. ->getMockForAbstractClass();
  247. $filters = $this->getMockBuilder(\Magento\Ui\Component\Filters::class)
  248. ->disableOriginalConstructor()
  249. ->getMock();
  250. $select = $this->getMockBuilder(\Magento\Ui\Component\Filters\Type\Select::class)
  251. ->disableOriginalConstructor()
  252. ->getMock();
  253. $this->filter->expects($this->once())
  254. ->method('getComponent')
  255. ->willReturn($component);
  256. $component->expects($this->once())
  257. ->method('getChildComponents')
  258. ->willReturn(['listing_top' => $childComponent]);
  259. $childComponent->expects($this->once())
  260. ->method('getChildComponents')
  261. ->willReturn([$filters]);
  262. $filters->expects($this->once())
  263. ->method('getChildComponents')
  264. ->willReturn([$select]);
  265. $select->expects($this->any())
  266. ->method('getName')
  267. ->willReturn($filter);
  268. $select->expects($this->any())
  269. ->method('getData')
  270. ->with('config/options')
  271. ->willReturn($options);
  272. $result = $this->model->getOptions();
  273. $this->assertTrue(is_array($result));
  274. $this->assertCount(1, $result);
  275. $this->assertEquals($expected, $result);
  276. }
  277. /**
  278. * @return array
  279. */
  280. public function getOptionsDataProvider()
  281. {
  282. return [
  283. [
  284. 'filter' => 'filter_name',
  285. 'options' => [
  286. [
  287. 'value' => 'value_1',
  288. 'label' => 'label_1',
  289. ]
  290. ],
  291. 'expected' => [
  292. 'filter_name' => [
  293. 'value_1' => 'label_1',
  294. ],
  295. ],
  296. ],
  297. [
  298. 'filter' => 'filter_name',
  299. 'options' => [
  300. [
  301. 'value' => [
  302. [
  303. 'value' => 'value_2',
  304. 'label' => 'label_2',
  305. ],
  306. ],
  307. 'label' => 'label_1',
  308. ]
  309. ],
  310. 'expected' => [
  311. 'filter_name' => [
  312. 'value_2' => 'label_1label_2',
  313. ],
  314. ],
  315. ],
  316. [
  317. 'filter' => 'filter_name',
  318. 'options' => [
  319. [
  320. 'value' => [
  321. [
  322. 'value' => [
  323. [
  324. 'value' => 'value_3',
  325. 'label' => 'label_3',
  326. ]
  327. ],
  328. 'label' => 'label_2',
  329. ],
  330. ],
  331. 'label' => 'label_1',
  332. ]
  333. ],
  334. 'expected' => [
  335. 'filter_name' => [
  336. 'value_3' => 'label_1label_2label_3',
  337. ],
  338. ],
  339. ],
  340. ];
  341. }
  342. /**
  343. * Test for convertDate function
  344. *
  345. * @param string $fieldValue
  346. * @param string $expected
  347. * @dataProvider convertDateProvider
  348. * @covers \Magento\Ui\Model\Export\MetadataProvider::convertDate()
  349. */
  350. public function testConvertDate($fieldValue, $expected)
  351. {
  352. $componentName = 'component_name';
  353. /** @var DocumentInterface|\PHPUnit_Framework_MockObject_MockObject $document */
  354. $document = $this->getMockBuilder(\Magento\Framework\DataObject::class)
  355. ->disableOriginalConstructor()
  356. ->getMock();
  357. $document->expects($this->once())
  358. ->method('getData')
  359. ->with('field')
  360. ->willReturn($fieldValue);
  361. $this->localeDate->expects($this->once())
  362. ->method('date')
  363. ->willReturn(new \DateTime($fieldValue, new \DateTimeZone('UTC')));
  364. $document->expects($this->once())
  365. ->method('setData')
  366. ->with('field', $expected);
  367. $this->model->convertDate($document, $componentName);
  368. }
  369. /**
  370. * Data provider for testConvertDate
  371. *
  372. * @return array
  373. */
  374. public function convertDateProvider()
  375. {
  376. return [
  377. [
  378. 'fieldValue' => '@1534505233',
  379. 'expected' => 'Aug 17, 2018 11:27:13 AM',
  380. ],
  381. [
  382. 'fieldValue' => '@1534530000',
  383. 'expected' => 'Aug 17, 2018 06:20:00 PM',
  384. ],
  385. ];
  386. }
  387. }