Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
UrlTest.php
Go to the documentation of this file.
1 <?php
7 
10 
14 class UrlTest extends \PHPUnit\Framework\TestCase
15 {
19  protected $_model;
20 
24  protected $_areaFrontName = 'backendArea';
25 
29  protected $_menuMock;
30 
34  protected $_formKey;
35 
39  protected $_scopeConfigMock;
40 
44  protected $_menuConfigMock;
45 
50 
54  protected $_requestMock;
55 
59  protected $_authSessionMock;
60 
65 
69  protected $_encryptor;
70 
74  private $serializerMock;
75 
80  protected function setUp()
81  {
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']
86  );
87 
88  $this->_menuConfigMock = $this->createMock(\Magento\Backend\Model\Menu\Config::class);
89  $this->_menuConfigMock->expects($this->any())->method('getMenu')->will($this->returnValue($this->_menuMock));
90 
91  $this->_formKey = $this->createPartialMock(\Magento\Framework\Data\Form\FormKey::class, ['getFormKey']);
92  $this->_formKey->expects($this->any())->method('getFormKey')->will($this->returnValue('salt'));
93 
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));
97  $mockItem->expects(
98  $this->any()
99  )->method(
100  'getId'
101  )->will(
102  $this->returnValue('Magento_Backend::system_acl_roles')
103  );
104  $mockItem->expects($this->any())->method('getAction')->will($this->returnValue('adminhtml/user_role'));
105 
106  $this->_menuMock->expects(
107  $this->any()
108  )->method(
109  'get'
110  )->with(
111  $this->equalTo('Magento_Backend::system_acl_roles')
112  )->will(
113  $this->returnValue($mockItem)
114  );
115 
116  $helperMock = $this->createMock(\Magento\Backend\Helper\Data::class);
117  $helperMock->expects(
118  $this->any()
119  )->method(
120  'getAreaFrontName'
121  )->will(
122  $this->returnValue($this->_areaFrontName)
123  );
124  $this->_scopeConfigMock = $this->createMock(\Magento\Framework\App\Config\ScopeConfigInterface::class);
125  $this->_scopeConfigMock->expects(
126  $this->any()
127  )->method(
128  'getValue'
129  )->with(
130  \Magento\Backend\Model\Url::XML_PATH_STARTUP_MENU_ITEM
131  )->will(
132  $this->returnValue('Magento_Backend::system_acl_roles')
133  );
134 
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())
138  ->method('getHash')
139  ->willReturnArgument(0);
140  $routeParamsResolver = $this->createMock(\Magento\Framework\Url\RouteParamsResolver::class);
141  $this->routeParamsResolverFactoryMock = $this->createMock(
142  \Magento\Framework\Url\RouteParamsResolverFactory::class
143  );
144  $this->routeParamsResolverFactoryMock->expects($this->any())
145  ->method('create')
146  ->willReturn($routeParamsResolver);
148  $hostCheckerMock = $this->createMock(HostChecker::class);
149  $this->serializerMock = $this->getMockBuilder(Json::class)
150  ->setMethods(['serialize'])
151  ->disableOriginalConstructor()
152  ->getMock();
153 
154  $this->serializerMock->expects($this->any())
155  ->method('serialize')
156  ->will(
157  $this->returnCallback(
158  function ($value) {
159  return json_encode($value);
160  }
161  )
162  );
163  $this->_model = $objectManager->getObject(
164  \Magento\Backend\Model\Url::class,
165  [
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
175  ]
176  );
177  $this->_requestMock = $this->createMock(\Magento\Framework\App\Request\Http::class);
178  $this->_model->setRequest($this->_requestMock);
179  }
180 
182  {
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']);
186 
187  $mockSession->expects($this->any())->method('getUser')->will($this->returnValue($user));
188 
189  $this->_model->setSession($mockSession);
190 
191  $this->_menuMock->expects($this->any())->method('getFirstAvailableChild')->will($this->returnValue(null));
192 
193  $this->assertEquals('*/*/denied', $this->_model->findFirstAvailableMenu());
194  }
195 
196  public function testFindFirstAvailableMenu()
197  {
198  $user = $this->createMock(\Magento\User\Model\User::class);
199  $mockSession = $this->createPartialMock(\Magento\Backend\Model\Auth\Session::class, ['getUser', 'isAllowed']);
200 
201  $mockSession->expects($this->any())->method('getUser')->will($this->returnValue($user));
202 
203  $this->_model->setSession($mockSession);
204 
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));
208 
209  $this->assertEquals('adminhtml/user', $this->_model->findFirstAvailableMenu());
210  }
211 
212  public function testGetStartupPageUrl()
213  {
214  $this->assertEquals('adminhtml/user_role', (string)$this->_model->getStartupPageUrl());
215  }
216 
217  public function testGetAreaFrontName()
218  {
219  $helperMock = $this->createMock(\Magento\Backend\Helper\Data::class);
220  $helperMock->expects(
221  $this->once()
222  )->method(
223  'getAreaFrontName'
224  )->will(
225  $this->returnValue($this->_areaFrontName)
226  );
227 
228  $helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
229  $urlModel = $helper->getObject(
230  \Magento\Backend\Model\Url::class,
231  [
232  'backendHelper' => $helperMock,
233  'authSession' => $this->_authSessionMock,
234  'routeParamsResolverFactory' => $this->routeParamsResolverFactoryMock
235  ]
236  );
237  $urlModel->getAreaFrontName();
238  }
239 
245  {
246  $routeName = 'adminhtml';
247  $controllerName = 'catalog';
248  $actionName = 'index';
249 
250  $keyWithRouteName = $this->_model->getSecretKey($routeName, $controllerName, $actionName);
251  $keyWithoutRouteName = $this->_model->getSecretKey(null, $controllerName, $actionName);
252  $keyDummyRouteName = $this->_model->getSecretKey('dummy', $controllerName, $actionName);
253 
254  $this->assertNotEquals($keyWithRouteName, $keyWithoutRouteName);
255  $this->assertNotEquals($keyWithRouteName, $keyDummyRouteName);
256  }
257 
263  {
264  $routeName = 'adminhtml';
265  $controllerName = 'catalog';
266  $actionName = 'index';
267 
268  $keyWithRouteName1 = $this->_model->getSecretKey($routeName, $controllerName, $actionName);
269  $keyWithRouteName2 = $this->_model->getSecretKey($routeName, $controllerName, $actionName);
270 
271  $this->assertEquals($keyWithRouteName1, $keyWithRouteName2);
272  }
273 
278  {
279  $routeName = 'adminhtml';
280  $controllerName = 'catalog';
281  $actionName = 'index';
282 
283  $keyFromParams = $this->_model->getSecretKey($routeName, $controllerName, $actionName);
284 
285  $this->_requestMock->expects(
286  $this->exactly(3)
287  )->method(
288  'getBeforeForwardInfo'
289  )->will(
290  $this->returnValue(null)
291  );
292  $this->_requestMock->expects($this->once())->method('getRouteName')->will($this->returnValue($routeName));
293  $this->_requestMock->expects(
294  $this->once()
295  )->method(
296  'getControllerName'
297  )->will(
298  $this->returnValue($controllerName)
299  );
300  $this->_requestMock->expects($this->once())->method('getActionName')->will($this->returnValue($actionName));
301  $this->_model->setRequest($this->_requestMock);
302 
303  $keyFromRequest = $this->_model->getSecretKey();
304  $this->assertEquals($keyFromParams, $keyFromRequest);
305  }
306 
311  {
312  $routeName = 'adminhtml';
313  $controllerName = 'catalog';
314  $actionName = 'index';
315 
316  $keyFromParams = $this->_model->getSecretKey($routeName, $controllerName, $actionName);
317 
318  $this->_requestMock->expects(
319  $this->at(0)
320  )->method(
321  'getBeforeForwardInfo'
322  )->with(
323  'route_name'
324  )->will(
325  $this->returnValue('adminhtml')
326  );
327 
328  $this->_requestMock->expects(
329  $this->at(1)
330  )->method(
331  'getBeforeForwardInfo'
332  )->with(
333  'route_name'
334  )->will(
335  $this->returnValue('adminhtml')
336  );
337 
338  $this->_requestMock->expects(
339  $this->at(2)
340  )->method(
341  'getBeforeForwardInfo'
342  )->with(
343  'controller_name'
344  )->will(
345  $this->returnValue('catalog')
346  );
347 
348  $this->_requestMock->expects(
349  $this->at(3)
350  )->method(
351  'getBeforeForwardInfo'
352  )->with(
353  'controller_name'
354  )->will(
355  $this->returnValue('catalog')
356  );
357 
358  $this->_requestMock->expects(
359  $this->at(4)
360  )->method(
361  'getBeforeForwardInfo'
362  )->with(
363  'action_name'
364  )->will(
365  $this->returnValue('index')
366  );
367 
368  $this->_requestMock->expects(
369  $this->at(5)
370  )->method(
371  'getBeforeForwardInfo'
372  )->with(
373  'action_name'
374  )->will(
375  $this->returnValue('index')
376  );
377 
378  $this->_model->setRequest($this->_requestMock);
379  $keyFromRequest = $this->_model->getSecretKey();
380  $this->assertEquals($keyFromParams, $keyFromRequest);
381  }
382 
384  {
385  $routePath = 'https://localhost/index.php/catalog/product/view/id/100/?foo=bar#anchor';
386  static::assertEquals($routePath, $this->_model->getUrl($routePath));
387  }
388 }
$helper
Definition: iframe.phtml:13
$objectManager
Definition: bootstrap.php:17
$value
Definition: gender.phtml:16
$user
Definition: dummy_user.php:13