MassactionTest.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Backend\Block\Widget\Grid;
  7. use Magento\TestFramework\App\State;
  8. /**
  9. * @magentoAppArea adminhtml
  10. * @magentoComponentsDir Magento/Backend/Block/_files/design
  11. * @magentoDbIsolation enabled
  12. */
  13. class MassactionTest extends \PHPUnit\Framework\TestCase
  14. {
  15. /**
  16. * @var \Magento\Backend\Block\Widget\Grid\Massaction
  17. */
  18. protected $_block;
  19. /**
  20. * @var \Magento\Framework\View\LayoutInterface
  21. */
  22. protected $_layout;
  23. /**
  24. * @var \Magento\Framework\ObjectManagerInterface
  25. */
  26. private $objectManager;
  27. /**
  28. * @var string
  29. */
  30. private $mageMode;
  31. protected function setUp()
  32. {
  33. parent::setUp();
  34. $this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  35. $this->mageMode = $this->objectManager->get(State::class)->getMode();
  36. /** @var \Magento\Theme\Model\Theme\Registration $registration */
  37. $registration = $this->objectManager->get(\Magento\Theme\Model\Theme\Registration::class);
  38. $registration->register();
  39. $this->objectManager->get(\Magento\Framework\View\DesignInterface::class)
  40. ->setDesignTheme('BackendTest/test_default');
  41. }
  42. protected function tearDown()
  43. {
  44. $this->objectManager->get(State::class)->setMode($this->mageMode);
  45. }
  46. /**
  47. * @param string $mageMode
  48. */
  49. private function loadLayout($mageMode = State::MODE_DEVELOPER)
  50. {
  51. $this->objectManager->get(State::class)->setMode($mageMode);
  52. $this->_layout = $this->objectManager->create(
  53. \Magento\Framework\View\LayoutInterface::class,
  54. ['area' => 'adminhtml']
  55. );
  56. $this->_layout->getUpdate()->load('layout_test_grid_handle');
  57. $this->_layout->generateXml();
  58. $this->_layout->generateElements();
  59. $this->_block = $this->_layout->getBlock('admin.test.grid.massaction');
  60. $this->assertNotFalse($this->_block, 'Could not load the block for testing');
  61. }
  62. public function testMassactionDefaultValues()
  63. {
  64. $this->loadLayout();
  65. /** @var $blockEmpty \Magento\Backend\Block\Widget\Grid\Massaction */
  66. $blockEmpty = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
  67. ->get(\Magento\Framework\View\LayoutInterface::class)
  68. ->createBlock(\Magento\Backend\Block\Widget\Grid\Massaction::class);
  69. $this->assertEmpty($blockEmpty->getItems());
  70. $this->assertEquals(0, $blockEmpty->getCount());
  71. $this->assertSame('[]', $blockEmpty->getItemsJson());
  72. $this->assertFalse($blockEmpty->isAvailable());
  73. }
  74. public function testGetJavaScript()
  75. {
  76. $this->loadLayout();
  77. $javascript = $this->_block->getJavaScript();
  78. $expectedItemFirst = '#"option_id1":{"label":"Option One",' .
  79. '"url":"http:\\\/\\\/localhost\\\/index\.php\\\/(?:key\\\/([\w\d]+)\\\/)?",' .
  80. '"complete":"Test","id":"option_id1"}#';
  81. $this->assertRegExp($expectedItemFirst, $javascript);
  82. $expectedItemSecond = '#"option_id2":{"label":"Option Two",' .
  83. '"url":"http:\\\/\\\/localhost\\\/index\.php\\\/(?:key\\\/([\w\d]+)\\\/)?",' .
  84. '"confirm":"Are you sure\?","id":"option_id2"}#';
  85. $this->assertRegExp($expectedItemSecond, $javascript);
  86. }
  87. public function testGetJavaScriptWithAddedItem()
  88. {
  89. $this->loadLayout();
  90. $input = [
  91. 'id' => 'option_id3',
  92. 'label' => 'Option Three',
  93. 'url' => '*/*/option3',
  94. 'block_name' => 'admin.test.grid.massaction.option3',
  95. ];
  96. $expected = '#"option_id3":{"id":"option_id3","label":"Option Three",' .
  97. '"url":"http:\\\/\\\/localhost\\\/index\.php\\\/(?:key\\\/([\w\d]+)\\\/)?",' .
  98. '"block_name":"admin.test.grid.massaction.option3"}#';
  99. $this->_block->addItem($input['id'], $input);
  100. $this->assertRegExp($expected, $this->_block->getJavaScript());
  101. }
  102. /**
  103. * @param string $mageMode
  104. * @param int $expectedCount
  105. * @dataProvider getCountDataProvider
  106. */
  107. public function testGetCount($mageMode, $expectedCount)
  108. {
  109. $this->loadLayout($mageMode);
  110. $this->assertEquals($expectedCount, $this->_block->getCount());
  111. }
  112. /**
  113. * @return array
  114. */
  115. public function getCountDataProvider()
  116. {
  117. return [
  118. [
  119. 'mageMode' => State::MODE_DEVELOPER,
  120. 'expectedCount' => 3,
  121. ],
  122. [
  123. 'mageMode' => State::MODE_DEFAULT,
  124. 'expectedCount' => 3,
  125. ],
  126. [
  127. 'mageMode' => State::MODE_PRODUCTION,
  128. 'expectedCount' => 2,
  129. ],
  130. ];
  131. }
  132. /**
  133. * @param string $itemId
  134. * @param array $expectedItem
  135. * @dataProvider getItemsDataProvider
  136. */
  137. public function testGetItems($itemId, $expectedItem)
  138. {
  139. $this->loadLayout();
  140. $items = $this->_block->getItems();
  141. $this->assertCount(3, $items);
  142. $this->assertArrayHasKey($itemId, $items);
  143. $actualItem = $items[$itemId];
  144. $this->assertEquals($expectedItem['id'], $actualItem->getId());
  145. $this->assertEquals($expectedItem['label'], $actualItem->getLabel());
  146. $this->assertRegExp($expectedItem['url'], $actualItem->getUrl());
  147. $this->assertEquals($expectedItem['selected'], $actualItem->getSelected());
  148. $this->assertEquals($expectedItem['blockname'], $actualItem->getBlockName());
  149. }
  150. /**
  151. * @return array
  152. */
  153. public function getItemsDataProvider()
  154. {
  155. return [
  156. [
  157. 'option_id1',
  158. [
  159. 'id' => 'option_id1',
  160. 'label' => 'Option One',
  161. 'url' => '#http:\/\/localhost\/index\.php\/(?:key\/([\w\d]+)\/)?#',
  162. 'selected' => false,
  163. 'blockname' => ''
  164. ],
  165. ],
  166. [
  167. 'option_id2',
  168. [
  169. 'id' => 'option_id2',
  170. 'label' => 'Option Two',
  171. 'url' => '#http:\/\/localhost\/index\.php\/(?:key\/([\w\d]+)\/)?#',
  172. 'selected' => false,
  173. 'blockname' => ''
  174. ]
  175. ],
  176. [
  177. 'option_id3',
  178. [
  179. 'id' => 'option_id3',
  180. 'label' => 'Option Three',
  181. 'url' => '#http:\/\/localhost\/index\.php\/(?:key\/([\w\d]+)\/)?#',
  182. 'selected' => false,
  183. 'blockname' => ''
  184. ]
  185. ]
  186. ];
  187. }
  188. public function testGridContainsMassactionColumn()
  189. {
  190. $this->loadLayout();
  191. $this->_layout->getBlock('admin.test.grid')->toHtml();
  192. $gridMassactionColumn = $this->_layout->getBlock('admin.test.grid')
  193. ->getColumnSet()
  194. ->getChildBlock('massaction');
  195. $this->assertNotNull($gridMassactionColumn, 'Massaction column does not exist in the grid column set');
  196. $this->assertInstanceOf(
  197. \Magento\Backend\Block\Widget\Grid\Column::class,
  198. $gridMassactionColumn,
  199. 'Massaction column is not an instance of \Magento\Backend\Block\Widget\Column'
  200. );
  201. }
  202. }