UrlTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Backend\Test\Unit\Model;
  7. use Magento\Framework\Serialize\Serializer\Json;
  8. use Magento\Framework\Url\HostChecker;
  9. /**
  10. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  11. */
  12. class UrlTest extends \PHPUnit\Framework\TestCase
  13. {
  14. /**
  15. * @var \Magento\Backend\Model\Url
  16. */
  17. protected $_model;
  18. /**
  19. * @var string
  20. */
  21. protected $_areaFrontName = 'backendArea';
  22. /**
  23. * @var \PHPUnit_Framework_MockObject_MockObject
  24. */
  25. protected $_menuMock;
  26. /**
  27. * @var \PHPUnit_Framework_MockObject_MockObject
  28. */
  29. protected $_formKey;
  30. /**
  31. * @var \PHPUnit_Framework_MockObject_MockObject
  32. */
  33. protected $_scopeConfigMock;
  34. /**
  35. * @var \PHPUnit_Framework_MockObject_MockObject
  36. */
  37. protected $_menuConfigMock;
  38. /**
  39. * @var \PHPUnit_Framework_MockObject_MockObject
  40. */
  41. protected $_backendHelperMock;
  42. /**
  43. * @var \PHPUnit_Framework_MockObject_MockObject
  44. */
  45. protected $_requestMock;
  46. /**
  47. * @var \PHPUnit_Framework_MockObject_MockObject
  48. */
  49. protected $_authSessionMock;
  50. /**
  51. * @var \PHPUnit_Framework_MockObject_MockObject
  52. */
  53. protected $routeParamsResolverFactoryMock;
  54. /**
  55. * @var \Magento\Framework\Encryption\EncryptorInterface
  56. */
  57. protected $_encryptor;
  58. /**
  59. * @var Json|\PHPUnit_Framework_MockObject_MockObject
  60. */
  61. private $serializerMock;
  62. /**
  63. * @return void
  64. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  65. */
  66. protected function setUp()
  67. {
  68. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  69. $this->_menuMock = $this->createPartialMock(
  70. \Magento\Backend\Model\Menu::class,
  71. ['getFirstAvailableChild', 'get', 'getFirstAvailable']
  72. );
  73. $this->_menuConfigMock = $this->createMock(\Magento\Backend\Model\Menu\Config::class);
  74. $this->_menuConfigMock->expects($this->any())->method('getMenu')->will($this->returnValue($this->_menuMock));
  75. $this->_formKey = $this->createPartialMock(\Magento\Framework\Data\Form\FormKey::class, ['getFormKey']);
  76. $this->_formKey->expects($this->any())->method('getFormKey')->will($this->returnValue('salt'));
  77. $mockItem = $this->createMock(\Magento\Backend\Model\Menu\Item::class);
  78. $mockItem->expects($this->any())->method('isDisabled')->will($this->returnValue(false));
  79. $mockItem->expects($this->any())->method('isAllowed')->will($this->returnValue(true));
  80. $mockItem->expects(
  81. $this->any()
  82. )->method(
  83. 'getId'
  84. )->will(
  85. $this->returnValue('Magento_Backend::system_acl_roles')
  86. );
  87. $mockItem->expects($this->any())->method('getAction')->will($this->returnValue('adminhtml/user_role'));
  88. $this->_menuMock->expects(
  89. $this->any()
  90. )->method(
  91. 'get'
  92. )->with(
  93. $this->equalTo('Magento_Backend::system_acl_roles')
  94. )->will(
  95. $this->returnValue($mockItem)
  96. );
  97. $helperMock = $this->createMock(\Magento\Backend\Helper\Data::class);
  98. $helperMock->expects(
  99. $this->any()
  100. )->method(
  101. 'getAreaFrontName'
  102. )->will(
  103. $this->returnValue($this->_areaFrontName)
  104. );
  105. $this->_scopeConfigMock = $this->createMock(\Magento\Framework\App\Config\ScopeConfigInterface::class);
  106. $this->_scopeConfigMock->expects(
  107. $this->any()
  108. )->method(
  109. 'getValue'
  110. )->with(
  111. \Magento\Backend\Model\Url::XML_PATH_STARTUP_MENU_ITEM
  112. )->will(
  113. $this->returnValue('Magento_Backend::system_acl_roles')
  114. );
  115. $this->_authSessionMock = $this->createMock(\Magento\Backend\Model\Auth\Session::class);
  116. $this->_encryptor = $this->createPartialMock(\Magento\Framework\Encryption\Encryptor::class, ['getHash']);
  117. $this->_encryptor->expects($this->any())
  118. ->method('getHash')
  119. ->willReturnArgument(0);
  120. $routeParamsResolver = $this->createMock(\Magento\Framework\Url\RouteParamsResolver::class);
  121. $this->routeParamsResolverFactoryMock = $this->createMock(
  122. \Magento\Framework\Url\RouteParamsResolverFactory::class
  123. );
  124. $this->routeParamsResolverFactoryMock->expects($this->any())
  125. ->method('create')
  126. ->willReturn($routeParamsResolver);
  127. /** @var HostChecker|\PHPUnit_Framework_MockObject_MockObject $hostCheckerMock */
  128. $hostCheckerMock = $this->createMock(HostChecker::class);
  129. $this->serializerMock = $this->getMockBuilder(Json::class)
  130. ->setMethods(['serialize'])
  131. ->disableOriginalConstructor()
  132. ->getMock();
  133. $this->serializerMock->expects($this->any())
  134. ->method('serialize')
  135. ->will(
  136. $this->returnCallback(
  137. function ($value) {
  138. return json_encode($value);
  139. }
  140. )
  141. );
  142. $this->_model = $objectManager->getObject(
  143. \Magento\Backend\Model\Url::class,
  144. [
  145. 'scopeConfig' => $this->_scopeConfigMock,
  146. 'backendHelper' => $helperMock,
  147. 'formKey' => $this->_formKey,
  148. 'menuConfig' => $this->_menuConfigMock,
  149. 'authSession' => $this->_authSessionMock,
  150. 'encryptor' => $this->_encryptor,
  151. 'routeParamsResolverFactory' => $this->routeParamsResolverFactoryMock,
  152. 'hostChecker' => $hostCheckerMock,
  153. 'serializer' => $this->serializerMock
  154. ]
  155. );
  156. $this->_requestMock = $this->createMock(\Magento\Framework\App\Request\Http::class);
  157. $this->_model->setRequest($this->_requestMock);
  158. }
  159. public function testFindFirstAvailableMenuDenied()
  160. {
  161. $user = $this->createMock(\Magento\User\Model\User::class);
  162. $user->expects($this->once())->method('setHasAvailableResources')->with($this->equalTo(false));
  163. $mockSession = $this->createPartialMock(\Magento\Backend\Model\Auth\Session::class, ['getUser', 'isAllowed']);
  164. $mockSession->expects($this->any())->method('getUser')->will($this->returnValue($user));
  165. $this->_model->setSession($mockSession);
  166. $this->_menuMock->expects($this->any())->method('getFirstAvailableChild')->will($this->returnValue(null));
  167. $this->assertEquals('*/*/denied', $this->_model->findFirstAvailableMenu());
  168. }
  169. public function testFindFirstAvailableMenu()
  170. {
  171. $user = $this->createMock(\Magento\User\Model\User::class);
  172. $mockSession = $this->createPartialMock(\Magento\Backend\Model\Auth\Session::class, ['getUser', 'isAllowed']);
  173. $mockSession->expects($this->any())->method('getUser')->will($this->returnValue($user));
  174. $this->_model->setSession($mockSession);
  175. $itemMock = $this->createMock(\Magento\Backend\Model\Menu\Item::class);
  176. $itemMock->expects($this->once())->method('getAction')->will($this->returnValue('adminhtml/user'));
  177. $this->_menuMock->expects($this->any())->method('getFirstAvailable')->will($this->returnValue($itemMock));
  178. $this->assertEquals('adminhtml/user', $this->_model->findFirstAvailableMenu());
  179. }
  180. public function testGetStartupPageUrl()
  181. {
  182. $this->assertEquals('adminhtml/user_role', (string)$this->_model->getStartupPageUrl());
  183. }
  184. public function testGetAreaFrontName()
  185. {
  186. $helperMock = $this->createMock(\Magento\Backend\Helper\Data::class);
  187. $helperMock->expects(
  188. $this->once()
  189. )->method(
  190. 'getAreaFrontName'
  191. )->will(
  192. $this->returnValue($this->_areaFrontName)
  193. );
  194. $helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  195. $urlModel = $helper->getObject(
  196. \Magento\Backend\Model\Url::class,
  197. [
  198. 'backendHelper' => $helperMock,
  199. 'authSession' => $this->_authSessionMock,
  200. 'routeParamsResolverFactory' => $this->routeParamsResolverFactoryMock
  201. ]
  202. );
  203. $urlModel->getAreaFrontName();
  204. }
  205. /**
  206. * Check that secret key generation is based on usage of routeName passed as method param
  207. * Params are not equals
  208. */
  209. public function testGetSecretKeyGenerationWithRouteNameAsParamNotEquals()
  210. {
  211. $routeName = 'adminhtml';
  212. $controllerName = 'catalog';
  213. $actionName = 'index';
  214. $keyWithRouteName = $this->_model->getSecretKey($routeName, $controllerName, $actionName);
  215. $keyWithoutRouteName = $this->_model->getSecretKey(null, $controllerName, $actionName);
  216. $keyDummyRouteName = $this->_model->getSecretKey('dummy', $controllerName, $actionName);
  217. $this->assertNotEquals($keyWithRouteName, $keyWithoutRouteName);
  218. $this->assertNotEquals($keyWithRouteName, $keyDummyRouteName);
  219. }
  220. /**
  221. * Check that secret key generation is based on usage of routeName passed as method param
  222. * Params are equals
  223. */
  224. public function testGetSecretKeyGenerationWithRouteNameAsParamEquals()
  225. {
  226. $routeName = 'adminhtml';
  227. $controllerName = 'catalog';
  228. $actionName = 'index';
  229. $keyWithRouteName1 = $this->_model->getSecretKey($routeName, $controllerName, $actionName);
  230. $keyWithRouteName2 = $this->_model->getSecretKey($routeName, $controllerName, $actionName);
  231. $this->assertEquals($keyWithRouteName1, $keyWithRouteName2);
  232. }
  233. /**
  234. * Check that secret key generation is based on usage of routeName extracted from request
  235. */
  236. public function testGetSecretKeyGenerationWithRouteNameInRequest()
  237. {
  238. $routeName = 'adminhtml';
  239. $controllerName = 'catalog';
  240. $actionName = 'index';
  241. $keyFromParams = $this->_model->getSecretKey($routeName, $controllerName, $actionName);
  242. $this->_requestMock->expects(
  243. $this->exactly(3)
  244. )->method(
  245. 'getBeforeForwardInfo'
  246. )->will(
  247. $this->returnValue(null)
  248. );
  249. $this->_requestMock->expects($this->once())->method('getRouteName')->will($this->returnValue($routeName));
  250. $this->_requestMock->expects(
  251. $this->once()
  252. )->method(
  253. 'getControllerName'
  254. )->will(
  255. $this->returnValue($controllerName)
  256. );
  257. $this->_requestMock->expects($this->once())->method('getActionName')->will($this->returnValue($actionName));
  258. $this->_model->setRequest($this->_requestMock);
  259. $keyFromRequest = $this->_model->getSecretKey();
  260. $this->assertEquals($keyFromParams, $keyFromRequest);
  261. }
  262. /**
  263. * Check that secret key generation is based on usage of routeName extracted from request Forward info
  264. */
  265. public function testGetSecretKeyGenerationWithRouteNameInForwardInfo()
  266. {
  267. $routeName = 'adminhtml';
  268. $controllerName = 'catalog';
  269. $actionName = 'index';
  270. $keyFromParams = $this->_model->getSecretKey($routeName, $controllerName, $actionName);
  271. $this->_requestMock->expects(
  272. $this->at(0)
  273. )->method(
  274. 'getBeforeForwardInfo'
  275. )->with(
  276. 'route_name'
  277. )->will(
  278. $this->returnValue('adminhtml')
  279. );
  280. $this->_requestMock->expects(
  281. $this->at(1)
  282. )->method(
  283. 'getBeforeForwardInfo'
  284. )->with(
  285. 'route_name'
  286. )->will(
  287. $this->returnValue('adminhtml')
  288. );
  289. $this->_requestMock->expects(
  290. $this->at(2)
  291. )->method(
  292. 'getBeforeForwardInfo'
  293. )->with(
  294. 'controller_name'
  295. )->will(
  296. $this->returnValue('catalog')
  297. );
  298. $this->_requestMock->expects(
  299. $this->at(3)
  300. )->method(
  301. 'getBeforeForwardInfo'
  302. )->with(
  303. 'controller_name'
  304. )->will(
  305. $this->returnValue('catalog')
  306. );
  307. $this->_requestMock->expects(
  308. $this->at(4)
  309. )->method(
  310. 'getBeforeForwardInfo'
  311. )->with(
  312. 'action_name'
  313. )->will(
  314. $this->returnValue('index')
  315. );
  316. $this->_requestMock->expects(
  317. $this->at(5)
  318. )->method(
  319. 'getBeforeForwardInfo'
  320. )->with(
  321. 'action_name'
  322. )->will(
  323. $this->returnValue('index')
  324. );
  325. $this->_model->setRequest($this->_requestMock);
  326. $keyFromRequest = $this->_model->getSecretKey();
  327. $this->assertEquals($keyFromParams, $keyFromRequest);
  328. }
  329. public function testGetUrlWithUrlInRoutePath()
  330. {
  331. $routePath = 'https://localhost/index.php/catalog/product/view/id/100/?foo=bar#anchor';
  332. static::assertEquals($routePath, $this->_model->getUrl($routePath));
  333. }
  334. }