BackendAuthenticationTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Rss\Test\Unit\App\Action\Plugin;
  7. class BackendAuthenticationTest extends \PHPUnit\Framework\TestCase
  8. {
  9. public function testAroundDispatch()
  10. {
  11. /** @var \Magento\Backend\App\AbstractAction|\PHPUnit_Framework_MockObject_MockObject $subject */
  12. $subject = $this->createMock(\Magento\Backend\App\AbstractAction::class);
  13. /** @var \Magento\Framework\App\ResponseInterface|\PHPUnit_Framework_MockObject_MockObject $response */
  14. $response = $this->createMock(\Magento\Framework\App\ResponseInterface::class);
  15. $proceed = function () use ($response) {
  16. return $response;
  17. };
  18. /** @var \Magento\Framework\App\Request\Http|\PHPUnit_Framework_MockObject_MockObject $request */
  19. $request = $this->createMock(\Magento\Framework\App\Request\Http::class);
  20. $request->expects($this->atLeastOnce())->method('getControllerName')->will($this->returnValue('feed'));
  21. $request->expects($this->atLeastOnce())->method('getActionName')->will($this->returnValue('index'));
  22. $request->expects($this->once())->method('getParam')->with('type')->will($this->returnValue('notifystock'));
  23. /** @var \Magento\Backend\Model\Auth\StorageInterface|\PHPUnit_Framework_MockObject_MockObject $session */
  24. $session = $this->createMock(\Magento\Backend\Model\Auth\StorageInterface::class);
  25. $session->expects($this->at(0))->method('isLoggedIn')->will($this->returnValue(false));
  26. $session->expects($this->at(1))->method('isLoggedIn')->will($this->returnValue(true));
  27. $username = 'admin';
  28. $password = '123123qa';
  29. $auth = $this->createMock(\Magento\Backend\Model\Auth::class);
  30. $auth->expects($this->once())->method('getAuthStorage')->will($this->returnValue($session));
  31. $auth->expects($this->once())->method('login')->with($username, $password);
  32. /** @var \Magento\Framework\HTTP\Authentication|\PHPUnit_Framework_MockObject_MockObject $httpAuthentication */
  33. $httpAuthentication = $this->createMock(\Magento\Framework\HTTP\Authentication::class);
  34. $httpAuthentication->expects($this->once())->method('getCredentials')
  35. ->will($this->returnValue([$username, $password]));
  36. $httpAuthentication->expects($this->once())->method('setAuthenticationFailed')->with('RSS Feeds');
  37. $authorization = $this->createMock(\Magento\Framework\AuthorizationInterface::class);
  38. $authorization->expects($this->at(0))->method('isAllowed')->with('Magento_Rss::rss')
  39. ->will($this->returnValue(true));
  40. $authorization->expects($this->at(1))->method('isAllowed')->with('Magento_Catalog::catalog_inventory')
  41. ->will($this->returnValue(false));
  42. $aclResources = [
  43. 'feed' => 'Magento_Rss::rss',
  44. 'notifystock' => 'Magento_Catalog::catalog_inventory',
  45. 'new_order' => 'Magento_Sales::actions_view',
  46. 'review' => 'Magento_Reports::review_product'
  47. ];
  48. /** @var \Magento\Rss\App\Action\Plugin\BackendAuthentication $plugin */
  49. $plugin = (new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this))
  50. ->getObject(
  51. \Magento\Rss\App\Action\Plugin\BackendAuthentication::class,
  52. [
  53. 'auth' => $auth,
  54. 'httpAuthentication' => $httpAuthentication,
  55. 'response' => $response,
  56. 'authorization' => $authorization,
  57. 'aclResources' => $aclResources
  58. ]
  59. );
  60. $this->assertSame(
  61. $response,
  62. $plugin->aroundDispatch($subject, $proceed, $request)
  63. );
  64. }
  65. }