|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +namespace Magento\User\Test\Unit\Model\Backend\Config; |
| 10 | + |
| 11 | +use Magento\User\Model\Backend\Config\ObserverConfig; |
| 12 | +use Magento\Backend\App\ConfigInterface; |
| 13 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper; |
| 14 | + |
| 15 | +/** |
| 16 | + * Unit Test for \Magento\User\Model\Backend\Config\ObserverConfig class |
| 17 | + * |
| 18 | + * Class \Magento\User\Test\Unit\Model\Backend\Config\ObserverConfigTest |
| 19 | + */ |
| 20 | +class ObserverConfigTest extends \PHPUnit\Framework\TestCase |
| 21 | +{ |
| 22 | + /** |
| 23 | + * Config path for lockout threshold |
| 24 | + */ |
| 25 | + private const XML_ADMIN_SECURITY_LOCKOUT_THRESHOLD = 'admin/security/lockout_threshold'; |
| 26 | + |
| 27 | + /** |
| 28 | + * Config path for password change is forced or not |
| 29 | + */ |
| 30 | + private const XML_ADMIN_SECURITY_PASSWORD_IS_FORCED = 'admin/security/password_is_forced'; |
| 31 | + |
| 32 | + /** |
| 33 | + * Config path for password lifetime |
| 34 | + */ |
| 35 | + private const XML_ADMIN_SECURITY_PASSWORD_LIFETIME = 'admin/security/password_lifetime'; |
| 36 | + |
| 37 | + /** |
| 38 | + * Config path for maximum lockout failures |
| 39 | + */ |
| 40 | + private const XML_ADMIN_SECURITY_LOCKOUT_FAILURES = 'admin/security/lockout_failures'; |
| 41 | + |
| 42 | + /** @var ObserverConfig */ |
| 43 | + private $model; |
| 44 | + |
| 45 | + /** |
| 46 | + * @var \PHPUnit_Framework_MockObject_MockObject|ConfigInterface |
| 47 | + */ |
| 48 | + private $backendConfigMock; |
| 49 | + |
| 50 | + /** |
| 51 | + * Set environment for test |
| 52 | + */ |
| 53 | + protected function setUp() |
| 54 | + { |
| 55 | + $this->backendConfigMock = $this->createMock(ConfigInterface::class); |
| 56 | + |
| 57 | + $objectManager = new ObjectManagerHelper($this); |
| 58 | + $this->model = $objectManager->getObject( |
| 59 | + ObserverConfig::class, |
| 60 | + [ |
| 61 | + 'backendConfig' => $this->backendConfigMock |
| 62 | + ] |
| 63 | + ); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Test when admin password lifetime = 0 days |
| 68 | + */ |
| 69 | + public function testIsLatestPasswordExpiredWhenNoAdminLifeTime() |
| 70 | + { |
| 71 | + $this->backendConfigMock->expects(self::any())->method('getValue') |
| 72 | + ->with(self::XML_ADMIN_SECURITY_PASSWORD_LIFETIME) |
| 73 | + ->willReturn('0'); |
| 74 | + $this->assertEquals(false, $this->model->_isLatestPasswordExpired([])); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Test when admin password lifetime = 2 days |
| 79 | + */ |
| 80 | + public function testIsLatestPasswordExpiredWhenHasAdminLifeTime() |
| 81 | + { |
| 82 | + $this->backendConfigMock->expects(self::any())->method('getValue') |
| 83 | + ->with(self::XML_ADMIN_SECURITY_PASSWORD_LIFETIME) |
| 84 | + ->willReturn('2'); |
| 85 | + $this->assertEquals(true, $this->model->_isLatestPasswordExpired(['last_updated' => 1571428052])); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Test when security lockout threshold = 100 minutes |
| 90 | + */ |
| 91 | + public function testGetAdminLockThreshold() |
| 92 | + { |
| 93 | + $this->backendConfigMock->expects(self::any())->method('getValue') |
| 94 | + ->with(self::XML_ADMIN_SECURITY_LOCKOUT_THRESHOLD) |
| 95 | + ->willReturn('100'); |
| 96 | + $this->assertEquals(6000, $this->model->getAdminLockThreshold()); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Test when password change force is true |
| 101 | + */ |
| 102 | + public function testIsPasswordChangeForcedTrue() |
| 103 | + { |
| 104 | + $this->backendConfigMock->expects(self::any())->method('getValue') |
| 105 | + ->with(self::XML_ADMIN_SECURITY_PASSWORD_IS_FORCED) |
| 106 | + ->willReturn('1'); |
| 107 | + $this->assertEquals(true, $this->model->isPasswordChangeForced()); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Test when password change force is false |
| 112 | + */ |
| 113 | + public function testIsPasswordChangeForcedFalse() |
| 114 | + { |
| 115 | + $this->backendConfigMock->expects(self::any())->method('getValue') |
| 116 | + ->with(self::XML_ADMIN_SECURITY_PASSWORD_IS_FORCED) |
| 117 | + ->willReturn('0'); |
| 118 | + $this->assertEquals(false, $this->model->isPasswordChangeForced()); |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Test when admin password lifetime = 2 days |
| 123 | + */ |
| 124 | + public function testGetAdminPasswordLifetime() |
| 125 | + { |
| 126 | + $this->backendConfigMock->expects(self::any())->method('getValue') |
| 127 | + ->with(self::XML_ADMIN_SECURITY_PASSWORD_LIFETIME) |
| 128 | + ->willReturn('2'); |
| 129 | + $this->assertEquals(172800, $this->model->getAdminPasswordLifetime()); |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Test when max failures = 5 (times) |
| 134 | + */ |
| 135 | + public function testGetMaxFailures() |
| 136 | + { |
| 137 | + $this->backendConfigMock->expects(self::any())->method('getValue') |
| 138 | + ->with(self::XML_ADMIN_SECURITY_LOCKOUT_FAILURES) |
| 139 | + ->willReturn('5'); |
| 140 | + $this->assertEquals(5, $this->model->getMaxFailures()); |
| 141 | + } |
| 142 | +} |
0 commit comments