GridTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Backend\Block\Widget;
  7. /**
  8. * @magentoAppArea adminhtml
  9. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  10. */
  11. class GridTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /**
  14. * @var \Magento\Backend\Block\Widget\Grid\ColumnSet
  15. */
  16. protected $_block;
  17. /**
  18. * @var \Magento\Framework\View\LayoutInterface|\PHPUnit_Framework_MockObject_MockObject
  19. */
  20. protected $_layoutMock;
  21. /**
  22. * @var \Magento\Backend\Block\Widget\Grid\ColumnSet|\PHPUnit_Framework_MockObject_MockObject
  23. */
  24. protected $_columnSetMock;
  25. protected function setUp()
  26. {
  27. $this->_layoutMock = $this->createPartialMock(
  28. \Magento\Framework\View\Layout::class,
  29. ['getChildName', 'getBlock', 'createBlock', 'helper', 'renameElement', 'unsetChild', 'setChild']
  30. );
  31. $this->_columnSetMock = $this->_getColumnSetMock();
  32. $returnValueMap = [
  33. ['grid', 'grid.columnSet', 'grid.columnSet'],
  34. ['grid', 'reset_filter_button', 'reset_filter_button'],
  35. ['grid', 'search_button', 'search_button'],
  36. ];
  37. $this->_layoutMock->expects(
  38. $this->any()
  39. )->method(
  40. 'getChildName'
  41. )->will(
  42. $this->returnValueMap($returnValueMap)
  43. );
  44. $this->_layoutMock->expects(
  45. $this->any()
  46. )->method(
  47. 'getBlock'
  48. )->with(
  49. 'grid.columnSet'
  50. )->will(
  51. $this->returnValue($this->_columnSetMock)
  52. );
  53. $this->_layoutMock->expects(
  54. $this->any()
  55. )->method(
  56. 'createBlock'
  57. )->with(
  58. \Magento\Backend\Block\Widget\Button::class
  59. )->will(
  60. $this->returnValue(
  61. \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
  62. \Magento\Framework\View\LayoutInterface::class
  63. )->createBlock(
  64. \Magento\Backend\Block\Widget\Button::class
  65. )
  66. )
  67. );
  68. $this->_layoutMock->expects(
  69. $this->any()
  70. )->method(
  71. 'helper'
  72. )->with(
  73. \Magento\Framework\Json\Helper\Data::class
  74. )->will(
  75. $this->returnValue(
  76. \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
  77. \Magento\Framework\Json\Helper\Data::class
  78. )
  79. )
  80. );
  81. $this->_block = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
  82. \Magento\Framework\View\LayoutInterface::class
  83. )->createBlock(
  84. \Magento\Backend\Block\Widget\Grid::class
  85. );
  86. $this->_block->setLayout($this->_layoutMock);
  87. $this->_block->setNameInLayout('grid');
  88. }
  89. /**
  90. * Retrieve the mocked column set block instance
  91. *
  92. * @return \Magento\Backend\Block\Widget\Grid\ColumnSet|\PHPUnit_Framework_MockObject_MockObject
  93. */
  94. protected function _getColumnSetMock()
  95. {
  96. $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  97. $directoryList = $objectManager->create(
  98. \Magento\Framework\App\Filesystem\DirectoryList::class,
  99. ['root' => __DIR__]
  100. );
  101. return $this->getMockBuilder(\Magento\Backend\Block\Widget\Grid\ColumnSet::class)
  102. ->setConstructorArgs(
  103. [
  104. $objectManager->create(
  105. \Magento\Framework\View\Element\Template\Context::class,
  106. [
  107. 'filesystem' => $objectManager->create(
  108. \Magento\Framework\Filesystem::class,
  109. ['directoryList' => $directoryList]
  110. )
  111. ]
  112. ),
  113. $objectManager->create(\Magento\Backend\Model\Widget\Grid\Row\UrlGeneratorFactory::class),
  114. $objectManager->create(\Magento\Backend\Model\Widget\Grid\SubTotals::class),
  115. $objectManager->create(\Magento\Backend\Model\Widget\Grid\Totals::class)
  116. ]
  117. )
  118. ->getMock();
  119. }
  120. public function testToHtmlPreparesColumns()
  121. {
  122. $this->_columnSetMock->expects($this->once())->method('setRendererType');
  123. $this->_columnSetMock->expects($this->once())->method('setFilterType');
  124. $this->_columnSetMock->expects($this->once())->method('setSortable');
  125. $this->_block->setColumnRenderers(['filter' => 'Filter_Class']);
  126. $this->_block->setColumnFilters(['filter' => 'Filter_Class']);
  127. $this->_block->setSortable(false);
  128. $this->_block->toHtml();
  129. }
  130. public function testGetMainButtonsHtmlReturnsEmptyStringIfFiltersArentVisible()
  131. {
  132. $this->_columnSetMock->expects($this->once())->method('isFilterVisible')->will($this->returnValue(false));
  133. $this->_block->getMainButtonsHtml();
  134. }
  135. public function testGetMassactionBlock()
  136. {
  137. /** @var $layout \Magento\Framework\View\Layout */
  138. $layout = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
  139. \Magento\Framework\View\LayoutInterface::class
  140. );
  141. /** @var $block \Magento\Backend\Block\Widget\Grid */
  142. $block = $layout->createBlock(\Magento\Backend\Block\Widget\Grid\Extended::class, 'block');
  143. $child = $layout->addBlock(\Magento\Framework\View\Element\Template::class, 'massaction', 'block');
  144. $this->assertSame($child, $block->getMassactionBlock());
  145. }
  146. }