UrlTest.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Backend\Model;
  7. use Magento\Framework\App\RequestInterface;
  8. use Magento\Framework\Encryption\EncryptorInterface;
  9. use Magento\Framework\Escaper;
  10. use Magento\Framework\ObjectManagerInterface;
  11. use Magento\Framework\Session\SessionManagerInterface;
  12. use Magento\TestFramework\Helper\Bootstrap;
  13. /**
  14. * Test class for \Magento\Backend\Model\UrlInterface.
  15. *
  16. * @magentoAppArea adminhtml
  17. */
  18. class UrlTest extends \PHPUnit\Framework\TestCase
  19. {
  20. /**
  21. * @var RequestInterface
  22. */
  23. private $request;
  24. /**
  25. * @var UrlInterface
  26. */
  27. private $_model;
  28. /**
  29. * @var ObjectManagerInterface
  30. */
  31. private $objectManager;
  32. /**
  33. * @inheritdoc
  34. */
  35. protected function setUp()
  36. {
  37. $this->objectManager = Bootstrap::getObjectManager();
  38. $this->request = $this->objectManager->get(RequestInterface::class);
  39. $this->_model = $this->objectManager->create(UrlInterface::class);
  40. }
  41. /**
  42. * App isolation is enabled to protect next tests from polluted registry by getUrl().
  43. *
  44. * @param string $routePath
  45. * @param array $requestParams
  46. * @param string $expectedResult
  47. * @param array|null $routeParams
  48. * @return void
  49. *
  50. * @dataProvider getUrlDataProvider
  51. * @magentoAppIsolation enabled
  52. */
  53. public function testGetUrl(
  54. string $routePath,
  55. array $requestParams,
  56. string $expectedResult,
  57. $routeParams = null
  58. ): void {
  59. $this->request->setParams($requestParams);
  60. $url = $this->_model->getUrl($routePath, $routeParams);
  61. $this->assertContains($expectedResult, $url);
  62. }
  63. /**
  64. * Data provider for getUrl method.
  65. *
  66. * @return array
  67. */
  68. public function getUrlDataProvider(): array
  69. {
  70. /** @var $escaper Escaper */
  71. $escaper = Bootstrap::getObjectManager()->get(Escaper::class);
  72. return [
  73. [
  74. 'routePath' => 'adminhtml/auth/login',
  75. 'requestParams' => [],
  76. 'expectedResult'=> 'admin/auth/login/key/',
  77. ],
  78. [
  79. 'routePath' => 'adminhtml/auth/login',
  80. 'requestParams' => [],
  81. 'expectedResult'=> '/param1/a1==/',
  82. 'routeParams' => [
  83. '_escape_params' => false,
  84. 'param1' => 'a1==',
  85. ],
  86. ],
  87. [
  88. 'routePath' => 'adminhtml/auth/login',
  89. 'requestParams' => [],
  90. 'expectedResult'=> '/param1/a1==/',
  91. 'routeParams' => [
  92. '_escape_params' => false,
  93. 'param1' => 'a1==',
  94. ],
  95. ],
  96. [
  97. 'routePath' => 'adminhtml/auth/login',
  98. 'requestParams' => ['param2' => 'a2=='],
  99. 'expectedResult'=> '/param2/a2==/',
  100. 'routeParams' => [
  101. '_current' => true,
  102. '_escape_params' => false,
  103. ],
  104. ],
  105. [
  106. 'routePath' => 'adminhtml/auth/login',
  107. 'requestParams' => [],
  108. 'expectedResult' => '/param3/' . $escaper->encodeUrlParam('a3==') . '/',
  109. 'routeParams' => [
  110. '_escape_params' => true,
  111. 'param3' => 'a3==',
  112. ],
  113. ],
  114. [
  115. 'routePath' => 'adminhtml/auth/login',
  116. 'requestParams' => ['param4' => 'a4=='],
  117. 'expectedResult' => '/param4/' . $escaper->encodeUrlParam('a4==') . '/',
  118. 'routeParams' => [
  119. '_current' => true,
  120. '_escape_params' => true,
  121. ],
  122. ],
  123. [
  124. 'routePath' => 'route/controller/action/id/100',
  125. 'requestParams' => [],
  126. 'expectedResult' => 'id/100',
  127. ],
  128. ];
  129. }
  130. /**
  131. * @param string $routeName
  132. * @param string $controller
  133. * @param string $action
  134. * @param string $expectedHash
  135. * @return void
  136. *
  137. * @dataProvider getSecretKeyDataProvider
  138. * @magentoAppIsolation enabled
  139. */
  140. public function testGetSecretKey(string $routeName, string $controller, string $action, string $expectedHash): void
  141. {
  142. $this->request->setControllerName('default_controller')
  143. ->setActionName('default_action')
  144. ->setRouteName('default_router');
  145. $this->_model->setRequest($this->request);
  146. $this->objectManager->get(SessionManagerInterface::class)->setData('_form_key', 'salt');
  147. $this->assertEquals($expectedHash, $this->_model->getSecretKey($routeName, $controller, $action));
  148. }
  149. /**
  150. * @return array
  151. */
  152. public function getSecretKeyDataProvider(): array
  153. {
  154. /** @var $encryptor EncryptorInterface */
  155. $encryptor = Bootstrap::getObjectManager()->get(EncryptorInterface::class);
  156. return [
  157. [
  158. '',
  159. '',
  160. '',
  161. $encryptor->getHash('default_router' . 'default_controller' . 'default_action' . 'salt'),
  162. ],
  163. ['', '', 'action', $encryptor->getHash('default_router' . 'default_controller' . 'action' . 'salt')],
  164. [
  165. '',
  166. 'controller',
  167. '',
  168. $encryptor->getHash('default_router' . 'controller' . 'default_action' . 'salt'),
  169. ],
  170. [
  171. '',
  172. 'controller',
  173. 'action',
  174. $encryptor->getHash('default_router' . 'controller' . 'action' . 'salt'),
  175. ],
  176. [
  177. 'adminhtml',
  178. '',
  179. '',
  180. $encryptor->getHash('adminhtml' . 'default_controller' . 'default_action' . 'salt'),
  181. ],
  182. [
  183. 'adminhtml',
  184. '',
  185. 'action',
  186. $encryptor->getHash('adminhtml' . 'default_controller' . 'action' . 'salt'),
  187. ],
  188. [
  189. 'adminhtml',
  190. 'controller',
  191. '',
  192. $encryptor->getHash('adminhtml' . 'controller' . 'default_action' . 'salt'),
  193. ],
  194. [
  195. 'adminhtml',
  196. 'controller',
  197. 'action',
  198. $encryptor->getHash('adminhtml' . 'controller' . 'action' . 'salt'),
  199. ],
  200. ];
  201. }
  202. /**
  203. * @magentoAppIsolation enabled
  204. * @return void
  205. */
  206. public function testGetSecretKeyForwarded(): void
  207. {
  208. /** @var $encryptor EncryptorInterface */
  209. $encryptor = $this->objectManager->get(EncryptorInterface::class);
  210. $this->request->setControllerName('controller')->setActionName('action');
  211. $this->request->initForward()->setControllerName(uniqid())->setActionName(uniqid());
  212. $this->_model->setRequest($this->request);
  213. $this->objectManager->get(SessionManagerInterface::class)->setData('_form_key', 'salt');
  214. $this->assertEquals($encryptor->getHash('controller' . 'action' . 'salt'), $this->_model->getSecretKey());
  215. }
  216. /**
  217. * @return void
  218. */
  219. public function testUseSecretKey(): void
  220. {
  221. $this->_model->setNoSecret(true);
  222. $this->assertFalse($this->_model->useSecretKey());
  223. $this->_model->setNoSecret(false);
  224. $this->assertTrue($this->_model->useSecretKey());
  225. }
  226. }