MassactionTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Test class for \Magento\Backend\Block\Widget\Grid\Massaction
  8. */
  9. namespace Magento\Backend\Test\Unit\Block\Widget\Grid;
  10. use Magento\Backend\Block\Widget\Grid\Massaction\VisibilityCheckerInterface as VisibilityChecker;
  11. use Magento\Framework\Authorization;
  12. class MassactionTest extends \PHPUnit\Framework\TestCase
  13. {
  14. /**
  15. * @var \Magento\Backend\Block\Widget\Grid\Massaction
  16. */
  17. protected $_block;
  18. /**
  19. * @var \Magento\Framework\View\Layout|\PHPUnit_Framework_MockObject_MockObject
  20. */
  21. protected $_layoutMock;
  22. /**
  23. * @var \Magento\Backend\Block\Widget\Grid|\PHPUnit_Framework_MockObject_MockObject
  24. */
  25. protected $_gridMock;
  26. /**
  27. * @var \PHPUnit_Framework_MockObject_MockObject
  28. */
  29. protected $_eventManagerMock;
  30. /**
  31. * @var \Magento\Backend\Model\Url|\PHPUnit_Framework_MockObject_MockObject
  32. */
  33. protected $_urlModelMock;
  34. /**
  35. * @var \Magento\Framework\App\Request\Http|\PHPUnit_Framework_MockObject_MockObject
  36. */
  37. protected $_requestMock;
  38. /**
  39. * @var Authorization|\PHPUnit_Framework_MockObject_MockObject
  40. */
  41. protected $_authorizationMock;
  42. /**
  43. * @var VisibilityChecker|\PHPUnit_Framework_MockObject_MockObject
  44. */
  45. private $visibilityCheckerMock;
  46. protected function setUp()
  47. {
  48. $this->_gridMock = $this->getMockBuilder(\Magento\Backend\Block\Widget\Grid::class)
  49. ->disableOriginalConstructor()
  50. ->disableOriginalClone()
  51. ->setMethods(['getId', 'getCollection'])
  52. ->getMock();
  53. $this->_gridMock->expects($this->any())
  54. ->method('getId')
  55. ->willReturn('test_grid');
  56. $this->_layoutMock = $this->getMockBuilder(\Magento\Framework\View\Layout::class)
  57. ->disableOriginalConstructor()
  58. ->disableOriginalClone()
  59. ->setMethods(['getParentName', 'getBlock', 'helper'])
  60. ->getMock();
  61. $this->_layoutMock->expects($this->any())
  62. ->method('getParentName')
  63. ->with('test_grid_massaction')
  64. ->willReturn('test_grid');
  65. $this->_layoutMock->expects($this->any())
  66. ->method('getBlock')
  67. ->with('test_grid')
  68. ->willReturn($this->_gridMock);
  69. $this->_requestMock = $this->getMockBuilder(\Magento\Framework\App\Request\Http::class)
  70. ->disableOriginalConstructor()
  71. ->disableOriginalClone()
  72. ->getMock();
  73. $this->_urlModelMock = $this->getMockBuilder(\Magento\Backend\Model\Url::class)
  74. ->disableOriginalConstructor()
  75. ->disableOriginalClone()
  76. ->getMock();
  77. $this->visibilityCheckerMock = $this->getMockBuilder(VisibilityChecker::class)
  78. ->getMockForAbstractClass();
  79. $this->_authorizationMock = $this->getMockBuilder(Authorization::class)
  80. ->disableOriginalConstructor()
  81. ->setMethods(['isAllowed'])
  82. ->getMock();
  83. $arguments = [
  84. 'layout' => $this->_layoutMock,
  85. 'request' => $this->_requestMock,
  86. 'urlBuilder' => $this->_urlModelMock,
  87. 'data' => ['massaction_id_field' => 'test_id', 'massaction_id_filter' => 'test_id'],
  88. 'authorization' => $this->_authorizationMock,
  89. ];
  90. $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  91. $this->_block = $objectManagerHelper->getObject(
  92. \Magento\Backend\Block\Widget\Grid\Massaction::class,
  93. $arguments
  94. );
  95. $this->_block->setNameInLayout('test_grid_massaction');
  96. }
  97. protected function tearDown()
  98. {
  99. unset($this->_layoutMock);
  100. unset($this->_eventManagerMock);
  101. unset($this->_gridMock);
  102. unset($this->_urlModelMock);
  103. unset($this->_block);
  104. }
  105. public function testMassactionDefaultValues()
  106. {
  107. $this->assertEquals(0, $this->_block->getCount());
  108. $this->assertFalse($this->_block->isAvailable());
  109. $this->assertEquals('massaction', $this->_block->getFormFieldName());
  110. $this->assertEquals('internal_massaction', $this->_block->getFormFieldNameInternal());
  111. $this->assertEquals('test_grid_massactionJsObject', $this->_block->getJsObjectName());
  112. $this->assertEquals('test_gridJsObject', $this->_block->getGridJsObjectName());
  113. $this->assertEquals('test_grid_massaction', $this->_block->getHtmlId());
  114. $this->assertTrue($this->_block->getUseSelectAll());
  115. }
  116. /**
  117. * @param string $itemId
  118. * @param \Magento\Framework\DataObject $item
  119. * @param $expectedItem \Magento\Framework\DataObject
  120. * @dataProvider itemsProcessingDataProvider
  121. */
  122. public function testItemsProcessing($itemId, $item, $expectedItem)
  123. {
  124. $this->_urlModelMock->expects($this->any())
  125. ->method('getBaseUrl')
  126. ->willReturn('http://localhost/index.php');
  127. $urlReturnValueMap = [
  128. ['*/*/test1', [], 'http://localhost/index.php/backend/admin/test/test1'],
  129. ['*/*/test2', [], 'http://localhost/index.php/backend/admin/test/test2'],
  130. ];
  131. $this->_urlModelMock->expects($this->any())
  132. ->method('getUrl')
  133. ->willReturnMap($urlReturnValueMap);
  134. $this->_authorizationMock->expects($this->any())
  135. ->method('isAllowed')
  136. ->willReturn(true);
  137. $this->_block->addItem($itemId, $item);
  138. $this->assertEquals(1, $this->_block->getCount());
  139. $actualItem = $this->_block->getItem($itemId);
  140. $this->assertInstanceOf(\Magento\Framework\DataObject::class, $actualItem);
  141. $this->assertEquals($expectedItem->getData(), $actualItem->getData());
  142. $this->_block->removeItem($itemId);
  143. $this->assertEquals(0, $this->_block->getCount());
  144. $this->assertNull($this->_block->getItem($itemId));
  145. }
  146. /**
  147. * @return array
  148. */
  149. public function itemsProcessingDataProvider()
  150. {
  151. return [
  152. [
  153. 'test_id1',
  154. ["label" => "Test Item One", "url" => "*/*/test1"],
  155. new \Magento\Framework\DataObject(
  156. [
  157. "label" => "Test Item One",
  158. "url" => "http://localhost/index.php/backend/admin/test/test1",
  159. "id" => 'test_id1',
  160. ]
  161. ),
  162. ],
  163. [
  164. 'test_id2',
  165. new \Magento\Framework\DataObject(["label" => "Test Item Two", "url" => "*/*/test2"]),
  166. new \Magento\Framework\DataObject(
  167. [
  168. "label" => "Test Item Two",
  169. "url" => "http://localhost/index.php/backend/admin/test/test2",
  170. "id" => 'test_id2',
  171. ]
  172. )
  173. ],
  174. [
  175. 'enabled',
  176. new \Magento\Framework\DataObject(["label" => "Test Item Enabled", "url" => "*/*/test2"]),
  177. new \Magento\Framework\DataObject(
  178. [
  179. "label" => "Test Item Enabled",
  180. "url" => "http://localhost/index.php/backend/admin/test/test2",
  181. "id" => 'enabled',
  182. ]
  183. )
  184. ],
  185. [
  186. 'refresh',
  187. new \Magento\Framework\DataObject(["label" => "Test Item Refresh", "url" => "*/*/test2"]),
  188. new \Magento\Framework\DataObject(
  189. [
  190. "label" => "Test Item Refresh",
  191. "url" => "http://localhost/index.php/backend/admin/test/test2",
  192. "id" => 'refresh',
  193. ]
  194. )
  195. ]
  196. ];
  197. }
  198. /**
  199. * @param string $param
  200. * @param string $expectedJson
  201. * @param array $expected
  202. * @dataProvider selectedDataProvider
  203. */
  204. public function testSelected($param, $expectedJson, $expected)
  205. {
  206. $this->_requestMock->expects($this->any())
  207. ->method('getParam')
  208. ->with($this->_block->getFormFieldNameInternal())
  209. ->willReturn($param);
  210. $this->assertEquals($expectedJson, $this->_block->getSelectedJson());
  211. $this->assertEquals($expected, $this->_block->getSelected());
  212. }
  213. /**
  214. * @return array
  215. */
  216. public function selectedDataProvider()
  217. {
  218. return [
  219. ['', '', []],
  220. ['test_id1,test_id2', 'test_id1,test_id2', ['test_id1', 'test_id2']]
  221. ];
  222. }
  223. public function testUseSelectAll()
  224. {
  225. $this->_block->setUseSelectAll(false);
  226. $this->assertFalse($this->_block->getUseSelectAll());
  227. $this->_block->setUseSelectAll(true);
  228. $this->assertTrue($this->_block->getUseSelectAll());
  229. }
  230. public function testGetGridIdsJsonWithoutUseSelectAll()
  231. {
  232. $this->_block->setUseSelectAll(false);
  233. $this->assertEmpty($this->_block->getGridIdsJson());
  234. }
  235. /**
  236. * @param array $items
  237. * @param string $result
  238. *
  239. * @dataProvider dataProviderGetGridIdsJsonWithUseSelectAll
  240. */
  241. public function testGetGridIdsJsonWithUseSelectAll(array $items, $result)
  242. {
  243. $this->_block->setUseSelectAll(true);
  244. if ($this->_block->getMassactionIdField()) {
  245. $massActionIdField = $this->_block->getMassactionIdField();
  246. } else {
  247. $massActionIdField = $this->_block->getParentBlock()->getMassactionIdField();
  248. }
  249. $collectionMock = $this->getMockBuilder(\Magento\Framework\Data\Collection::class)
  250. ->disableOriginalConstructor()
  251. ->getMock();
  252. $this->_gridMock->expects($this->once())
  253. ->method('getCollection')
  254. ->willReturn($collectionMock);
  255. $collectionMock->expects($this->once())
  256. ->method('setPageSize')
  257. ->with(0)
  258. ->willReturnSelf();
  259. $collectionMock->expects($this->once())
  260. ->method('getColumnValues')
  261. ->with($massActionIdField)
  262. ->willReturn($items);
  263. $this->assertEquals($result, $this->_block->getGridIdsJson());
  264. }
  265. /**
  266. * @return array
  267. */
  268. public function dataProviderGetGridIdsJsonWithUseSelectAll()
  269. {
  270. return [
  271. [
  272. [],
  273. '',
  274. ],
  275. [
  276. [1],
  277. '1',
  278. ],
  279. [
  280. [1, 2, 3],
  281. '1,2,3',
  282. ],
  283. ];
  284. }
  285. /**
  286. * @param string $itemId
  287. * @param array|\Magento\Framework\DataObject $item
  288. * @param int $count
  289. * @param bool $withVisibilityChecker
  290. * @param bool $isVisible
  291. * @param bool $isAllowed
  292. *
  293. * @dataProvider addItemDataProvider
  294. */
  295. public function testAddItem($itemId, $item, $count, $withVisibilityChecker, $isVisible, $isAllowed)
  296. {
  297. $this->visibilityCheckerMock->expects($this->any())
  298. ->method('isVisible')
  299. ->willReturn($isVisible);
  300. $this->_authorizationMock->expects($this->any())
  301. ->method('isAllowed')
  302. ->willReturn($isAllowed);
  303. if ($withVisibilityChecker) {
  304. $item['visible'] = $this->visibilityCheckerMock;
  305. }
  306. $urlReturnValueMap = [
  307. ['*/*/test1', [], 'http://localhost/index.php/backend/admin/test/test1'],
  308. ['*/*/test2', [], 'http://localhost/index.php/backend/admin/test/test2'],
  309. ];
  310. $this->_urlModelMock->expects($this->any())
  311. ->method('getUrl')
  312. ->willReturnMap($urlReturnValueMap);
  313. $this->_block->addItem($itemId, $item);
  314. $this->assertEquals($count, $this->_block->getCount(), $itemId);
  315. }
  316. /**
  317. * @return array
  318. */
  319. public function addItemDataProvider()
  320. {
  321. return [
  322. [
  323. 'itemId' => 'test1',
  324. 'item' => ['label' => 'Test 1', 'url' => '*/*/test1'],
  325. 'count' => 1,
  326. 'withVisibilityChecker' => false,
  327. 'isVisible' => false,
  328. 'isAllowed' => true,
  329. ],
  330. [
  331. 'itemId' => 'test2',
  332. 'item' => ['label' => 'Test 2', 'url' => '*/*/test2'],
  333. 'count' => 1,
  334. 'withVisibilityChecker' => false,
  335. 'isVisible' => true,
  336. 'isAllowed' => true,
  337. ],
  338. [
  339. 'itemId' => 'test3',
  340. 'item' => ['label' => 'Test 3. Hide', 'url' => '*/*/test3'],
  341. 'count' => 0,
  342. 'withVisibilityChecker' => true,
  343. 'isVisible' => false,
  344. 'isAllowed' => true,
  345. ],
  346. [
  347. 'itemId' => 'test4',
  348. 'item' => ['label' => 'Test 4. Does not hide', 'url' => '*/*/test4'],
  349. 'count' => 1,
  350. 'withVisibilityChecker' => true,
  351. 'isVisible' => true,
  352. 'isAllowed' => true,
  353. ],
  354. [
  355. 'itemId' => 'enable',
  356. 'item' => ['label' => 'Test 5. Not restricted', 'url' => '*/*/test5'],
  357. 'count' => 1,
  358. 'withVisibilityChecker' => true,
  359. 'isVisible' => true,
  360. 'isAllowed' => true,
  361. ],
  362. [
  363. 'itemId' => 'enable',
  364. 'item' => ['label' => 'Test 5. restricted', 'url' => '*/*/test5'],
  365. 'count' => 0,
  366. 'withVisibilityChecker' => true,
  367. 'isVisible' => true,
  368. 'isAllowed' => false,
  369. ],
  370. [
  371. 'itemId' => 'refresh',
  372. 'item' => ['label' => 'Test 6. Not Restricted', 'url' => '*/*/test6'],
  373. 'count' => 1,
  374. 'withVisibilityChecker' => true,
  375. 'isVisible' => true,
  376. 'isAllowed' => true,
  377. ],
  378. [
  379. 'itemId' => 'refresh',
  380. 'item' => ['label' => 'Test 6. Restricted', 'url' => '*/*/test6'],
  381. 'count' => 0,
  382. 'withVisibilityChecker' => true,
  383. 'isVisible' => true,
  384. 'isAllowed' => false,
  385. ],
  386. ];
  387. }
  388. }