14 class UrlTest extends \PHPUnit\Framework\TestCase
74 private $serializerMock;
80 protected function setUp()
82 $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
83 $this->_menuMock = $this->createPartialMock(
84 \
Magento\Backend\Model\Menu::class,
85 [
'getFirstAvailableChild',
'get',
'getFirstAvailable']
88 $this->_menuConfigMock = $this->createMock(\
Magento\Backend\Model\Menu\Config::class);
89 $this->_menuConfigMock->expects($this->any())->method(
'getMenu')->will($this->returnValue($this->_menuMock));
91 $this->_formKey = $this->createPartialMock(\
Magento\Framework\Data\Form\FormKey::class, [
'getFormKey']);
92 $this->_formKey->expects($this->any())->method(
'getFormKey')->will($this->returnValue(
'salt'));
94 $mockItem = $this->createMock(\
Magento\Backend\Model\Menu\Item::class);
95 $mockItem->expects($this->any())->method(
'isDisabled')->will($this->returnValue(
false));
96 $mockItem->expects($this->any())->method(
'isAllowed')->will($this->returnValue(
true));
102 $this->returnValue(
'Magento_Backend::system_acl_roles')
104 $mockItem->expects($this->any())->method(
'getAction')->will($this->returnValue(
'adminhtml/user_role'));
106 $this->_menuMock->expects(
111 $this->equalTo(
'Magento_Backend::system_acl_roles')
113 $this->returnValue($mockItem)
116 $helperMock = $this->createMock(\
Magento\Backend\Helper\Data::class);
117 $helperMock->expects(
122 $this->returnValue($this->_areaFrontName)
124 $this->_scopeConfigMock = $this->createMock(\
Magento\Framework\
App\Config\ScopeConfigInterface::class);
125 $this->_scopeConfigMock->expects(
130 \
Magento\Backend\Model\Url::XML_PATH_STARTUP_MENU_ITEM
132 $this->returnValue(
'Magento_Backend::system_acl_roles')
135 $this->_authSessionMock = $this->createMock(\
Magento\Backend\Model\Auth\Session::class);
136 $this->_encryptor = $this->createPartialMock(\
Magento\Framework\Encryption\Encryptor::class, [
'getHash']);
137 $this->_encryptor->expects($this->any())
139 ->willReturnArgument(0);
140 $routeParamsResolver = $this->createMock(\
Magento\Framework\Url\RouteParamsResolver::class);
141 $this->routeParamsResolverFactoryMock = $this->createMock(
142 \
Magento\Framework\Url\RouteParamsResolverFactory::class
144 $this->routeParamsResolverFactoryMock->expects($this->any())
146 ->willReturn($routeParamsResolver);
148 $hostCheckerMock = $this->createMock(HostChecker::class);
149 $this->serializerMock = $this->getMockBuilder(Json::class)
150 ->setMethods([
'serialize'])
151 ->disableOriginalConstructor()
154 $this->serializerMock->expects($this->any())
155 ->method(
'serialize')
157 $this->returnCallback(
159 return json_encode(
$value);
164 \
Magento\Backend\Model\Url::class,
166 'scopeConfig' => $this->_scopeConfigMock,
167 'backendHelper' => $helperMock,
168 'formKey' => $this->_formKey,
169 'menuConfig' => $this->_menuConfigMock,
170 'authSession' => $this->_authSessionMock,
171 'encryptor' => $this->_encryptor,
172 'routeParamsResolverFactory' => $this->routeParamsResolverFactoryMock,
173 'hostChecker' => $hostCheckerMock,
174 'serializer' => $this->serializerMock
177 $this->_requestMock = $this->createMock(\
Magento\Framework\
App\Request\Http::class);
178 $this->_model->setRequest($this->_requestMock);
183 $user = $this->createMock(\
Magento\User\Model\User::class);
184 $user->expects($this->once())->method(
'setHasAvailableResources')->with($this->equalTo(
false));
185 $mockSession = $this->createPartialMock(\
Magento\Backend\Model\Auth\Session::class, [
'getUser',
'isAllowed']);
187 $mockSession->expects($this->any())->method(
'getUser')->will($this->returnValue(
$user));
189 $this->_model->setSession($mockSession);
191 $this->_menuMock->expects($this->any())->method(
'getFirstAvailableChild')->will($this->returnValue(
null));
193 $this->assertEquals(
'*/*/denied', $this->_model->findFirstAvailableMenu());
198 $user = $this->createMock(\
Magento\User\Model\User::class);
199 $mockSession = $this->createPartialMock(\
Magento\Backend\Model\Auth\Session::class, [
'getUser',
'isAllowed']);
201 $mockSession->expects($this->any())->method(
'getUser')->will($this->returnValue(
$user));
203 $this->_model->setSession($mockSession);
205 $itemMock = $this->createMock(\
Magento\Backend\Model\Menu\Item::class);
206 $itemMock->expects($this->once())->method(
'getAction')->will($this->returnValue(
'adminhtml/user'));
207 $this->_menuMock->expects($this->any())->method(
'getFirstAvailable')->will($this->returnValue($itemMock));
209 $this->assertEquals(
'adminhtml/user', $this->_model->findFirstAvailableMenu());
214 $this->assertEquals(
'adminhtml/user_role', (
string)$this->_model->getStartupPageUrl());
219 $helperMock = $this->createMock(\
Magento\Backend\Helper\Data::class);
220 $helperMock->expects(
225 $this->returnValue($this->_areaFrontName)
228 $helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
229 $urlModel =
$helper->getObject(
230 \
Magento\Backend\Model\Url::class,
232 'backendHelper' => $helperMock,
233 'authSession' => $this->_authSessionMock,
234 'routeParamsResolverFactory' => $this->routeParamsResolverFactoryMock
237 $urlModel->getAreaFrontName();
246 $routeName =
'adminhtml';
247 $controllerName =
'catalog';
248 $actionName =
'index';
250 $keyWithRouteName = $this->_model->getSecretKey($routeName, $controllerName, $actionName);
251 $keyWithoutRouteName = $this->_model->getSecretKey(
null, $controllerName, $actionName);
252 $keyDummyRouteName = $this->_model->getSecretKey(
'dummy', $controllerName, $actionName);
254 $this->assertNotEquals($keyWithRouteName, $keyWithoutRouteName);
255 $this->assertNotEquals($keyWithRouteName, $keyDummyRouteName);
264 $routeName =
'adminhtml';
265 $controllerName =
'catalog';
266 $actionName =
'index';
268 $keyWithRouteName1 = $this->_model->getSecretKey($routeName, $controllerName, $actionName);
269 $keyWithRouteName2 = $this->_model->getSecretKey($routeName, $controllerName, $actionName);
271 $this->assertEquals($keyWithRouteName1, $keyWithRouteName2);
279 $routeName =
'adminhtml';
280 $controllerName =
'catalog';
281 $actionName =
'index';
283 $keyFromParams = $this->_model->getSecretKey($routeName, $controllerName, $actionName);
285 $this->_requestMock->expects(
288 'getBeforeForwardInfo' 290 $this->returnValue(
null)
292 $this->_requestMock->expects($this->once())->method(
'getRouteName')->will($this->returnValue($routeName));
293 $this->_requestMock->expects(
298 $this->returnValue($controllerName)
300 $this->_requestMock->expects($this->once())->method(
'getActionName')->will($this->returnValue($actionName));
301 $this->_model->setRequest($this->_requestMock);
303 $keyFromRequest = $this->_model->getSecretKey();
304 $this->assertEquals($keyFromParams, $keyFromRequest);
312 $routeName =
'adminhtml';
313 $controllerName =
'catalog';
314 $actionName =
'index';
316 $keyFromParams = $this->_model->getSecretKey($routeName, $controllerName, $actionName);
318 $this->_requestMock->expects(
321 'getBeforeForwardInfo' 325 $this->returnValue(
'adminhtml')
328 $this->_requestMock->expects(
331 'getBeforeForwardInfo' 335 $this->returnValue(
'adminhtml')
338 $this->_requestMock->expects(
341 'getBeforeForwardInfo' 345 $this->returnValue(
'catalog')
348 $this->_requestMock->expects(
351 'getBeforeForwardInfo' 355 $this->returnValue(
'catalog')
358 $this->_requestMock->expects(
361 'getBeforeForwardInfo' 365 $this->returnValue(
'index')
368 $this->_requestMock->expects(
371 'getBeforeForwardInfo' 375 $this->returnValue(
'index')
378 $this->_model->setRequest($this->_requestMock);
379 $keyFromRequest = $this->_model->getSecretKey();
380 $this->assertEquals($keyFromParams, $keyFromRequest);
385 $routePath =
'https://localhost/index.php/catalog/product/view/id/100/?foo=bar#anchor';
386 static::assertEquals($routePath, $this->_model->getUrl($routePath));
testGetSecretKeyGenerationWithRouteNameInRequest()
testGetSecretKeyGenerationWithRouteNameInForwardInfo()
testGetUrlWithUrlInRoutePath()
testFindFirstAvailableMenuDenied()
testGetSecretKeyGenerationWithRouteNameAsParamNotEquals()
testFindFirstAvailableMenu()
$routeParamsResolverFactoryMock
testGetSecretKeyGenerationWithRouteNameAsParamEquals()