MassactionKeyTest.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Backend\Test\Unit\App\Action\Plugin;
  7. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  8. use Magento\Backend\App\AbstractAction;
  9. use Magento\Framework\App\RequestInterface;
  10. class MassactionKeyTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /**
  13. * @var \Magento\Backend\App\Action\Plugin\MassactionKey
  14. */
  15. protected $plugin;
  16. /**
  17. * @var \PHPUnit_Framework_MockObject_MockObject|RequestInterface
  18. */
  19. protected $requestMock;
  20. /**
  21. * @var \PHPUnit_Framework_MockObject_MockObject|AbstractAction
  22. */
  23. protected $subjectMock;
  24. protected function setUp()
  25. {
  26. $this->closureMock = function () {
  27. return 'Expected';
  28. };
  29. $this->subjectMock = $this->createMock(\Magento\Backend\App\AbstractAction::class);
  30. $this->requestMock = $this->getMockForAbstractClass(
  31. RequestInterface::class,
  32. [],
  33. '',
  34. false,
  35. false,
  36. true,
  37. ['getPost', 'setPostValue']
  38. );
  39. $objectManager = new ObjectManager($this);
  40. $this->plugin = $objectManager->getObject(
  41. \Magento\Backend\App\Action\Plugin\MassactionKey::class,
  42. [
  43. 'subject' => $this->subjectMock,
  44. 'request' => $this->requestMock,
  45. ]
  46. );
  47. }
  48. /**
  49. * @param $postData array|string
  50. * @param array $convertedData
  51. * @dataProvider beforeDispatchDataProvider
  52. */
  53. public function testBeforeDispatchWhenMassactionPrepareKeyRequestExists($postData, $convertedData)
  54. {
  55. $this->requestMock->expects($this->at(0))
  56. ->method('getPost')
  57. ->with('massaction_prepare_key')
  58. ->will($this->returnValue('key'));
  59. $this->requestMock->expects($this->at(1))
  60. ->method('getPost')
  61. ->with('key')
  62. ->will($this->returnValue($postData));
  63. $this->requestMock->expects($this->once())
  64. ->method('setPostValue')
  65. ->with('key', $convertedData);
  66. $this->plugin->beforeDispatch($this->subjectMock, $this->requestMock);
  67. }
  68. /**
  69. * @return array
  70. */
  71. public function beforeDispatchDataProvider()
  72. {
  73. return [
  74. 'post_data_is_array' => [['key'], ['key']],
  75. 'post_data_is_string' => ['key, key_two', ['key', ' key_two']]
  76. ];
  77. }
  78. public function testBeforeDispatchWhenMassactionPrepareKeyRequestNotExists()
  79. {
  80. $this->requestMock->expects($this->once())
  81. ->method('getPost')
  82. ->with('massaction_prepare_key')
  83. ->will($this->returnValue(false));
  84. $this->requestMock->expects($this->never())
  85. ->method('setPostValue');
  86. $this->plugin->beforeDispatch($this->subjectMock, $this->requestMock);
  87. }
  88. }