ColumnSetTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Backend\Test\Unit\Block\Widget\Grid;
  7. /**
  8. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  9. */
  10. class ColumnSetTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /**
  13. * @var \Magento\Backend\Block\Widget\Grid\ColumnSet
  14. */
  15. protected $_block;
  16. /**
  17. * @var \PHPUnit_Framework_MockObject_MockObject
  18. */
  19. protected $_layoutMock;
  20. /**
  21. * @var \PHPUnit_Framework_MockObject_MockObject
  22. */
  23. protected $_columnMock;
  24. /**
  25. * @var \PHPUnit_Framework_MockObject_MockObject
  26. */
  27. protected $_factoryMock;
  28. /**
  29. * @var \PHPUnit_Framework_MockObject_MockObject
  30. */
  31. protected $_subtotalsMock;
  32. /**
  33. * @var \PHPUnit_Framework_MockObject_MockObject
  34. */
  35. protected $_totalsMock;
  36. /**
  37. * @var \PHPUnit_Framework_MockObject_MockObject
  38. */
  39. protected $_gridMock;
  40. protected function setUp()
  41. {
  42. $this->_columnMock = $this->createPartialMock(
  43. \Magento\Backend\Block\Widget\Grid\Column::class,
  44. ['setSortable', 'setRendererType', 'setFilterType']
  45. );
  46. $this->_layoutMock = $this->createMock(\Magento\Framework\View\Layout::class);
  47. $this->_layoutMock->expects(
  48. $this->any()
  49. )->method(
  50. 'getChildBlocks'
  51. )->will(
  52. $this->returnValue(['column' => $this->_columnMock])
  53. );
  54. $this->_factoryMock = $this->createMock(\Magento\Backend\Model\Widget\Grid\Row\UrlGeneratorFactory::class);
  55. $this->_subtotalsMock = $this->createMock(\Magento\Backend\Model\Widget\Grid\SubTotals::class);
  56. $this->_totalsMock = $this->createMock(\Magento\Backend\Model\Widget\Grid\Totals::class);
  57. $arguments = [
  58. 'layout' => $this->_layoutMock,
  59. 'generatorFactory' => $this->_factoryMock,
  60. 'totals' => $this->_totalsMock,
  61. 'subtotals' => $this->_subtotalsMock,
  62. ];
  63. $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  64. $this->_block = $objectManagerHelper->getObject(
  65. \Magento\Backend\Block\Widget\Grid\ColumnSet::class,
  66. $arguments
  67. );
  68. $this->_block->setNameInLayout('grid.columnSet');
  69. }
  70. protected function tearDown()
  71. {
  72. unset($this->_block);
  73. unset($this->_layoutMock);
  74. unset($this->_columnMock);
  75. unset($this->_factoryMock);
  76. unset($this->_totalsMock);
  77. unset($this->_subtotalsMock);
  78. }
  79. public function testSetSortablePropagatesSortabilityToChildren()
  80. {
  81. $this->_columnMock->expects($this->once())->method('setSortable')->with(false);
  82. $this->_block->setSortable(false);
  83. }
  84. public function testSetSortablePropagatesSortabilityToChildrenOnlyIfSortabilityIsFalse()
  85. {
  86. $this->_columnMock->expects($this->never())->method('setSortable');
  87. $this->_block->setSortable(true);
  88. }
  89. public function testSetRendererTypePropagatesRendererTypeToColumns()
  90. {
  91. $this->_columnMock->expects($this->once())->method('setRendererType')->with('renderer', 'Renderer_Class');
  92. $this->_block->setRendererType('renderer', 'Renderer_Class');
  93. }
  94. public function testSetFilterTypePropagatesFilterTypeToColumns()
  95. {
  96. $this->_columnMock->expects($this->once())->method('setFilterType')->with('filter', 'Filter_Class');
  97. $this->_block->setFilterType('filter', 'Filter_Class');
  98. }
  99. public function testGetRowUrlIfUrlPathNotSet()
  100. {
  101. $this->assertEquals('#', $this->_block->getRowUrl(new \stdClass()));
  102. }
  103. public function testGetRowUrl()
  104. {
  105. $generatorClass = \Magento\Backend\Model\Widget\Grid\Row\UrlGenerator::class;
  106. $itemMock = $this->createMock(\Magento\Framework\DataObject::class);
  107. $rowUrlGenerator =
  108. $this->createPartialMock(\Magento\Backend\Model\Widget\Grid\Row\UrlGenerator::class, ['getUrl']);
  109. $rowUrlGenerator->expects(
  110. $this->once()
  111. )->method(
  112. 'getUrl'
  113. )->with(
  114. $this->equalTo($itemMock)
  115. )->will(
  116. $this->returnValue('http://localhost/mng/item/edit')
  117. );
  118. $factoryMock = $this->createPartialMock(
  119. \Magento\Backend\Model\Widget\Grid\Row\UrlGeneratorFactory::class,
  120. ['createUrlGenerator']
  121. );
  122. $factoryMock->expects(
  123. $this->once()
  124. )->method(
  125. 'createUrlGenerator'
  126. )->with(
  127. $this->equalTo($generatorClass),
  128. $this->equalTo(['args' => ['generatorClass' => $generatorClass]])
  129. )->will(
  130. $this->returnValue($rowUrlGenerator)
  131. );
  132. $arguments = [
  133. 'layout' => $this->_layoutMock,
  134. 'generatorFactory' => $factoryMock,
  135. 'data' => ['rowUrl' => ['generatorClass' => $generatorClass]],
  136. 'totals' => $this->_totalsMock,
  137. 'subtotals' => $this->_subtotalsMock,
  138. ];
  139. $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  140. /** @var $model \Magento\Backend\Block\Widget\Grid\ColumnSet */
  141. $model = $objectManagerHelper->getObject(\Magento\Backend\Block\Widget\Grid\ColumnSet::class, $arguments);
  142. $url = $model->getRowUrl($itemMock);
  143. $this->assertEquals('http://localhost/mng/item/edit', $url);
  144. }
  145. public function testItemHasMultipleRows()
  146. {
  147. $item = new \Magento\Framework\DataObject();
  148. // prepare sub-collection
  149. $subCollection = new \Magento\Framework\Data\Collection(
  150. $this->createMock(\Magento\Framework\Data\Collection\EntityFactory::class)
  151. );
  152. $subCollection->addItem(new \Magento\Framework\DataObject(['test4' => '1', 'test5' => '2']));
  153. $subCollection->addItem(new \Magento\Framework\DataObject(['test4' => '2', 'test5' => '2']));
  154. $item->setChildren($subCollection);
  155. $this->assertTrue($this->_block->hasMultipleRows($item));
  156. }
  157. public function testShouldRenderTotalWithNotEmptyCollection()
  158. {
  159. $this->_prepareLayoutWithGrid($this->_prepareGridMock($this->_getTestCollection()));
  160. $this->_block->setCountTotals(true);
  161. $this->assertTrue($this->_block->shouldRenderTotal());
  162. }
  163. public function testShouldRenderTotalWithEmptyCollection()
  164. {
  165. $this->_prepareLayoutWithGrid(
  166. $this->_prepareGridMock(
  167. new \Magento\Framework\Data\Collection(
  168. $this->createMock(\Magento\Framework\Data\Collection\EntityFactory::class)
  169. )
  170. )
  171. );
  172. $this->_block->setCountTotals(true);
  173. $this->assertFalse($this->_block->shouldRenderTotal());
  174. }
  175. public function testShouldRenderTotalWithFlagFalse()
  176. {
  177. $this->_block->setCountTotals(false);
  178. $this->assertFalse($this->_block->shouldRenderTotal());
  179. }
  180. public function testShouldRenderSubtotalWithFlagFalse()
  181. {
  182. $this->_block->setCountSubTotals(false);
  183. $this->assertFalse($this->_block->shouldRenderSubTotal(new \Magento\Framework\DataObject()));
  184. }
  185. public function testShouldRenderSubtotalWithEmptySubData()
  186. {
  187. $this->_block->setCountSubTotals(true);
  188. $this->assertFalse($this->_block->shouldRenderSubTotal(new \Magento\Framework\DataObject()));
  189. }
  190. public function testShouldRenderSubtotalWithNotEmptySubData()
  191. {
  192. $item = new \Magento\Framework\DataObject();
  193. // prepare sub-collection
  194. $subCollection = new \Magento\Framework\Data\Collection(
  195. $this->createMock(\Magento\Framework\Data\Collection\EntityFactory::class)
  196. );
  197. $subCollection->addItem(new \Magento\Framework\DataObject(['test4' => '1', 'test5' => '2']));
  198. $subCollection->addItem(new \Magento\Framework\DataObject(['test4' => '2', 'test5' => '2']));
  199. $item->setChildren($subCollection);
  200. $this->_block->setCountSubTotals(true);
  201. $this->assertTrue($this->_block->shouldRenderSubTotal($item));
  202. }
  203. public function testUpdateItemByFirstMultiRow()
  204. {
  205. $item = new \Magento\Framework\DataObject(['test1' => '1']);
  206. // prepare sub-collection
  207. $subCollection = new \Magento\Framework\Data\Collection(
  208. $this->createMock(\Magento\Framework\Data\Collection\EntityFactory::class)
  209. );
  210. $subCollection->addItem(new \Magento\Framework\DataObject(['test4' => '1', 'test5' => '2']));
  211. $subCollection->addItem(new \Magento\Framework\DataObject(['test4' => '2', 'test5' => '2']));
  212. $item->setChildren($subCollection);
  213. $expectedItem = new \Magento\Framework\DataObject(['test1' => '1']);
  214. $expectedItem->addData(['test4' => '1', 'test5' => '2']);
  215. $expectedItem->setChildren($subCollection);
  216. $this->_block->updateItemByFirstMultiRow($item);
  217. $this->assertEquals($expectedItem, $item);
  218. }
  219. public function testGetSubTotals()
  220. {
  221. // prepare sub-collection
  222. $subCollection = new \Magento\Framework\Data\Collection(
  223. $this->createMock(\Magento\Framework\Data\Collection\EntityFactory::class)
  224. );
  225. $subCollection->addItem(new \Magento\Framework\DataObject(['column' => '1']));
  226. $subCollection->addItem(new \Magento\Framework\DataObject(['column' => '1']));
  227. $this->_subtotalsMock->expects(
  228. $this->once()
  229. )->method(
  230. 'countTotals'
  231. )->with(
  232. $subCollection
  233. )->will(
  234. $this->returnValue(new \Magento\Framework\DataObject(['column' => '2']))
  235. );
  236. // prepare item
  237. $item = new \Magento\Framework\DataObject(['test1' => '1']);
  238. $item->setChildren($subCollection);
  239. $this->assertEquals(new \Magento\Framework\DataObject(['column' => '2']), $this->_block->getSubTotals($item));
  240. }
  241. public function testGetTotals()
  242. {
  243. $collection = $this->_getTestCollection();
  244. $this->_prepareLayoutWithGrid($this->_prepareGridMock($collection));
  245. $this->_totalsMock->expects(
  246. $this->once()
  247. )->method(
  248. 'countTotals'
  249. )->with(
  250. $collection
  251. )->will(
  252. $this->returnValue(new \Magento\Framework\DataObject(['test1' => '3', 'test2' => '2']))
  253. );
  254. $this->assertEquals(
  255. new \Magento\Framework\DataObject(['test1' => '3', 'test2' => '2']),
  256. $this->_block->getTotals()
  257. );
  258. }
  259. /**
  260. * Retrieve prepared mock for \Magento\Backend\Model\Widget\Grid with collection
  261. *
  262. * @param \Magento\Framework\Data\Collection $collection
  263. * @return \PHPUnit_Framework_MockObject_MockObject
  264. */
  265. protected function _prepareGridMock($collection)
  266. {
  267. // prepare block grid
  268. $gridMock = $this->createPartialMock(\Magento\Backend\Block\Widget\Grid::class, ['getCollection']);
  269. $gridMock->expects($this->any())->method('getCollection')->will($this->returnValue($collection));
  270. return $gridMock;
  271. }
  272. /**
  273. * Retrieve test collection
  274. *
  275. * @return \Magento\Framework\Data\Collection
  276. */
  277. protected function _getTestCollection()
  278. {
  279. $collection = new \Magento\Framework\Data\Collection(
  280. $this->createMock(\Magento\Framework\Data\Collection\EntityFactory::class)
  281. );
  282. $items = [
  283. new \Magento\Framework\DataObject(['test1' => '1', 'test2' => '2']),
  284. new \Magento\Framework\DataObject(['test1' => '1', 'test2' => '2']),
  285. new \Magento\Framework\DataObject(['test1' => '1', 'test2' => '2']),
  286. ];
  287. foreach ($items as $item) {
  288. $collection->addItem($item);
  289. }
  290. return $collection;
  291. }
  292. /**
  293. * Prepare layout for receiving grid block
  294. *
  295. * @param \PHPUnit_Framework_MockObject_MockObject $gridMock
  296. */
  297. protected function _prepareLayoutWithGrid($gridMock)
  298. {
  299. $this->_layoutMock->expects(
  300. $this->any()
  301. )->method(
  302. 'getParentName'
  303. )->with(
  304. 'grid.columnSet'
  305. )->will(
  306. $this->returnValue('grid')
  307. );
  308. $this->_layoutMock->expects(
  309. $this->any()
  310. )->method(
  311. 'getBlock'
  312. )->with(
  313. 'grid'
  314. )->will(
  315. $this->returnValue($gridMock)
  316. );
  317. }
  318. }