AuthTest.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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\TestFramework\Unit\Helper\ObjectManager;
  8. /**
  9. * Class AuthTest
  10. */
  11. class AuthTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /**
  14. * @var \Magento\Backend\Model\Auth
  15. */
  16. protected $_model;
  17. /**
  18. * @var \PHPUnit_Framework_MockObject_MockObject
  19. */
  20. protected $_eventManagerMock;
  21. /**
  22. * @var \PHPUnit_Framework_MockObject_MockObject
  23. */
  24. protected $_credentialStorage;
  25. /**
  26. * @var \PHPUnit_Framework_MockObject_MockObject
  27. */
  28. protected $_modelFactoryMock;
  29. protected function setUp()
  30. {
  31. $this->_eventManagerMock = $this->createMock(\Magento\Framework\Event\ManagerInterface::class);
  32. $this->_credentialStorage = $this->getMockBuilder(
  33. \Magento\Backend\Model\Auth\Credential\StorageInterface::class
  34. )
  35. ->setMethods(['getId'])
  36. ->getMockForAbstractClass();
  37. $this->_modelFactoryMock = $this->createMock(\Magento\Framework\Data\Collection\ModelFactory::class);
  38. $objectManager = new ObjectManager($this);
  39. $this->_model = $objectManager->getObject(
  40. \Magento\Backend\Model\Auth::class,
  41. [
  42. 'eventManager' => $this->_eventManagerMock,
  43. 'credentialStorage' => $this->_credentialStorage,
  44. 'modelFactory' => $this->_modelFactoryMock
  45. ]
  46. );
  47. }
  48. /**
  49. * @expectedException \Magento\Framework\Exception\AuthenticationException
  50. */
  51. public function testLoginFailed()
  52. {
  53. $this->_modelFactoryMock
  54. ->expects($this->once())
  55. ->method('create')
  56. ->with(\Magento\Backend\Model\Auth\Credential\StorageInterface::class)
  57. ->will($this->returnValue($this->_credentialStorage));
  58. $exceptionMock = new \Magento\Framework\Exception\LocalizedException(
  59. __(
  60. 'The account sign-in was incorrect or your account is disabled temporarily. '
  61. . 'Please wait and try again later.'
  62. )
  63. );
  64. $this->_credentialStorage
  65. ->expects($this->once())
  66. ->method('login')
  67. ->with('username', 'password')
  68. ->will($this->throwException($exceptionMock));
  69. $this->_credentialStorage->expects($this->never())->method('getId');
  70. $this->_eventManagerMock->expects($this->once())->method('dispatch')->with('backend_auth_user_login_failed');
  71. $this->_model->login('username', 'password');
  72. $this->expectExceptionMessage(
  73. 'The account sign-in was incorrect or your account is disabled temporarily. '
  74. . 'Please wait and try again later.'
  75. );
  76. }
  77. }