EditTest.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Config\Test\Unit\Block\System\Config;
  7. class EditTest extends \PHPUnit\Framework\TestCase
  8. {
  9. /**
  10. * @var \Magento\Config\Block\System\Config\Edit
  11. */
  12. protected $_object;
  13. /**
  14. * @var \PHPUnit_Framework_MockObject_MockObject
  15. */
  16. protected $_systemConfigMock;
  17. /**
  18. * @var \PHPUnit_Framework_MockObject_MockObject
  19. */
  20. protected $_requestMock;
  21. /**
  22. * @var \PHPUnit_Framework_MockObject_MockObject
  23. */
  24. protected $_layoutMock;
  25. /**
  26. * @var \PHPUnit_Framework_MockObject_MockObject
  27. */
  28. protected $_urlModelMock;
  29. /**
  30. * @var \PHPUnit_Framework_MockObject_MockObject
  31. */
  32. protected $_sectionMock;
  33. /**
  34. * @var \PHPUnit_Framework_MockObject_MockObject
  35. */
  36. protected $_jsonMock;
  37. protected function setUp()
  38. {
  39. $this->_systemConfigMock = $this->createMock(\Magento\Config\Model\Config\Structure::class);
  40. $this->_requestMock = $this->createMock(\Magento\Framework\App\RequestInterface::class);
  41. $this->_requestMock->expects(
  42. $this->any()
  43. )->method(
  44. 'getParam'
  45. )->with(
  46. 'section'
  47. )->will(
  48. $this->returnValue('test_section')
  49. );
  50. $this->_layoutMock = $this->createMock(\Magento\Framework\View\Layout::class);
  51. $this->_urlModelMock = $this->createMock(\Magento\Backend\Model\Url::class);
  52. $this->_sectionMock = $this->createMock(\Magento\Config\Model\Config\Structure\Element\Section::class);
  53. $this->_systemConfigMock->expects(
  54. $this->any()
  55. )->method(
  56. 'getElement'
  57. )->with(
  58. 'test_section'
  59. )->will(
  60. $this->returnValue($this->_sectionMock)
  61. );
  62. $this->_jsonMock = $this->createMock(\Magento\Framework\Serialize\Serializer\Json::class);
  63. $data = [
  64. 'data' => ['systemConfig' => $this->_systemConfigMock],
  65. 'request' => $this->_requestMock,
  66. 'layout' => $this->_layoutMock,
  67. 'urlBuilder' => $this->_urlModelMock,
  68. 'configStructure' => $this->_systemConfigMock,
  69. 'jsonSerializer' => $this->_jsonMock,
  70. ];
  71. $helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  72. $this->_object = $helper->getObject(\Magento\Config\Block\System\Config\Edit::class, $data);
  73. }
  74. public function testGetSaveButtonHtml()
  75. {
  76. $expected = 'element_html_code';
  77. $this->_layoutMock->expects(
  78. $this->once()
  79. )->method(
  80. 'getChildName'
  81. )->with(
  82. null,
  83. 'save_button'
  84. )->will(
  85. $this->returnValue('test_child_name')
  86. );
  87. $this->_layoutMock->expects(
  88. $this->once()
  89. )->method(
  90. 'renderElement'
  91. )->with(
  92. 'test_child_name'
  93. )->will(
  94. $this->returnValue('element_html_code')
  95. );
  96. $this->assertEquals($expected, $this->_object->getSaveButtonHtml());
  97. }
  98. public function testGetSaveUrl()
  99. {
  100. $expectedUrl = '*/system_config/save';
  101. $expectedParams = ['_current' => true];
  102. $this->_urlModelMock->expects(
  103. $this->once()
  104. )->method(
  105. 'getUrl'
  106. )->with(
  107. $expectedUrl,
  108. $expectedParams
  109. )->will(
  110. $this->returnArgument(0)
  111. );
  112. $this->assertEquals($expectedUrl, $this->_object->getSaveUrl());
  113. }
  114. public function testPrepareLayout()
  115. {
  116. $expectedHeader = 'Test Header';
  117. $expectedLabel = 'Test Label';
  118. $expectedBlock = 'Test Block';
  119. $blockMock = $this->getMockBuilder(\Magento\Framework\View\Element\Template::class)
  120. ->disableOriginalConstructor()
  121. ->getMock();
  122. $this->_sectionMock->expects($this->once())
  123. ->method('getFrontendModel')
  124. ->willReturn($expectedBlock);
  125. $this->_sectionMock->expects($this->once())
  126. ->method('getLabel')
  127. ->willReturn($expectedLabel);
  128. $this->_sectionMock->expects($this->once())
  129. ->method('getHeaderCss')
  130. ->willReturn($expectedHeader);
  131. $this->_layoutMock->expects($this->once())
  132. ->method('getBlock')
  133. ->with('page.actions.toolbar')
  134. ->willReturn($blockMock);
  135. $this->_layoutMock->expects($this->once())
  136. ->method('createBlock')
  137. ->with($expectedBlock)
  138. ->willReturn($blockMock);
  139. $blockMock->expects($this->once())
  140. ->method('getNameInLayout')
  141. ->willReturn($expectedBlock);
  142. $this->_layoutMock->expects($this->once())
  143. ->method('setChild')
  144. ->with($expectedBlock, $expectedBlock, 'form')
  145. ->willReturn($this->_layoutMock);
  146. $this->_object->setNameInLayout($expectedBlock);
  147. $this->_object->setLayout($this->_layoutMock);
  148. }
  149. /**
  150. * @param array $requestData
  151. * @param array $expected
  152. * @dataProvider getConfigSearchParamsJsonData
  153. */
  154. public function testGetConfigSearchParamsJson(array $requestData, array $expected)
  155. {
  156. $requestMock = $this->createMock(\Magento\Framework\App\RequestInterface::class);
  157. $requestMock->expects($this->any())
  158. ->method('getParam')
  159. ->will($this->returnValueMap($requestData));
  160. $this->_jsonMock->expects($this->once())
  161. ->method('serialize')
  162. ->with($expected);
  163. $data = [
  164. 'data' => ['systemConfig' => $this->_systemConfigMock],
  165. 'request' => $requestMock,
  166. 'layout' => $this->_layoutMock,
  167. 'urlBuilder' => $this->_urlModelMock,
  168. 'configStructure' => $this->_systemConfigMock,
  169. 'jsonSerializer' => $this->_jsonMock,
  170. ];
  171. $helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  172. $object = $helper->getObject(\Magento\Config\Block\System\Config\Edit::class, $data);
  173. $object->getConfigSearchParamsJson();
  174. }
  175. /**
  176. * @return array
  177. */
  178. public function getConfigSearchParamsJsonData()
  179. {
  180. return [
  181. [
  182. [
  183. ['section', null, null],
  184. ['group', null, null],
  185. ['field', null, null],
  186. ],
  187. [],
  188. ],
  189. [
  190. [
  191. ['section', null, 'section_code'],
  192. ['group', null, null],
  193. ['field', null, null],
  194. ],
  195. [
  196. 'section' => 'section_code',
  197. ],
  198. ],
  199. [
  200. [
  201. ['section', null, 'section_code'],
  202. ['group', null, 'group_code'],
  203. ['field', null, null],
  204. ],
  205. [
  206. 'section' => 'section_code',
  207. 'group' => 'group_code',
  208. ],
  209. ],
  210. [
  211. [
  212. ['section', null, 'section_code'],
  213. ['group', null, 'group_code'],
  214. ['field', null, 'field_code'],
  215. ],
  216. [
  217. 'section' => 'section_code',
  218. 'group' => 'group_code',
  219. 'field' => 'field_code',
  220. ],
  221. ],
  222. ];
  223. }
  224. }