AccountManagementTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Security\Test\Unit\Model\Plugin;
  7. use Magento\Customer\Model\AccountManagement;
  8. use Magento\Framework\App\Area;
  9. use Magento\Framework\Config\ScopeInterface;
  10. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  11. use Magento\Security\Model\PasswordResetRequestEvent;
  12. /**
  13. * Test class for \Magento\Security\Model\Plugin\AccountManagement testing
  14. */
  15. class AccountManagementTest extends \PHPUnit\Framework\TestCase
  16. {
  17. /**
  18. * @var \Magento\Security\Model\Plugin\AccountManagement
  19. */
  20. protected $model;
  21. /**
  22. * @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject
  23. */
  24. protected $request;
  25. /**
  26. * @var \Magento\Security\Model\SecurityManager|\PHPUnit_Framework_MockObject_MockObject
  27. */
  28. protected $securityManager;
  29. /**
  30. * @var AccountManagement|\PHPUnit_Framework_MockObject_MockObject
  31. */
  32. protected $accountManagement;
  33. /**
  34. * @var ScopeInterface|\PHPUnit_Framework_MockObject_MockObject
  35. */
  36. private $scope;
  37. /**
  38. * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
  39. */
  40. protected $objectManager;
  41. /**
  42. * Init mocks for tests
  43. * @return void
  44. */
  45. public function setUp()
  46. {
  47. $this->objectManager = new ObjectManager($this);
  48. $this->request = $this->createMock(\Magento\Framework\App\RequestInterface::class);
  49. $this->securityManager = $this->createPartialMock(
  50. \Magento\Security\Model\SecurityManager::class,
  51. ['performSecurityCheck']
  52. );
  53. $this->accountManagement = $this->createMock(AccountManagement::class);
  54. $this->scope = $this->createMock(ScopeInterface::class);
  55. }
  56. /**
  57. * @param $area
  58. * @param $passwordRequestEvent
  59. * @param $expectedTimes
  60. * @dataProvider beforeInitiatePasswordResetDataProvider
  61. */
  62. public function testBeforeInitiatePasswordReset($area, $passwordRequestEvent, $expectedTimes)
  63. {
  64. $email = 'test@example.com';
  65. $template = AccountManagement::EMAIL_RESET;
  66. $this->model = $this->objectManager->getObject(
  67. \Magento\Security\Model\Plugin\AccountManagement::class,
  68. [
  69. 'passwordRequestEvent' => $passwordRequestEvent,
  70. 'request' => $this->request,
  71. 'securityManager' => $this->securityManager,
  72. 'scope' => $this->scope
  73. ]
  74. );
  75. $this->scope->expects($this->once())
  76. ->method('getCurrentScope')
  77. ->willReturn($area);
  78. $this->securityManager->expects($this->exactly($expectedTimes))
  79. ->method('performSecurityCheck')
  80. ->with($passwordRequestEvent, $email)
  81. ->willReturnSelf();
  82. $this->model->beforeInitiatePasswordReset(
  83. $this->accountManagement,
  84. $email,
  85. $template
  86. );
  87. }
  88. /**
  89. * @return array
  90. */
  91. public function beforeInitiatePasswordResetDataProvider()
  92. {
  93. return [
  94. [Area::AREA_ADMINHTML, PasswordResetRequestEvent::CUSTOMER_PASSWORD_RESET_REQUEST, 0],
  95. [Area::AREA_ADMINHTML, PasswordResetRequestEvent::ADMIN_PASSWORD_RESET_REQUEST, 1],
  96. [Area::AREA_FRONTEND, PasswordResetRequestEvent::CUSTOMER_PASSWORD_RESET_REQUEST, 1],
  97. // This should never happen, but let's cover it with tests
  98. [Area::AREA_FRONTEND, PasswordResetRequestEvent::ADMIN_PASSWORD_RESET_REQUEST, 1],
  99. ];
  100. }
  101. }