ConfigTest.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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;
  7. use Magento\Security\Model\ConfigInterface;
  8. /**
  9. * Test class for \Magento\Security\Model\Config testing
  10. */
  11. class ConfigTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /**
  14. * @var \Magento\Framework\App\Config\ScopeConfigInterface
  15. */
  16. protected $scopeConfigMock;
  17. /**
  18. * @var \Magento\Framework\Config\ScopeInterface
  19. */
  20. protected $scopeMock;
  21. /**
  22. * @var ConfigInterface
  23. */
  24. protected $model;
  25. /**
  26. * Init mocks for tests
  27. * @return void
  28. */
  29. protected function setUp()
  30. {
  31. $this->scopeConfigMock = $this->createPartialMock(
  32. \Magento\Framework\App\Config\ScopeConfigInterface::class,
  33. ['getValue', 'isSetFlag']
  34. );
  35. $this->scopeMock = $this->createMock(\Magento\Framework\Config\ScopeInterface::class);
  36. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  37. $this->model = $objectManager->getObject(
  38. \Magento\Security\Model\Config::class,
  39. [
  40. 'scopeConfig' => $this->scopeConfigMock,
  41. 'scope' => $this->scopeMock
  42. ]
  43. );
  44. }
  45. public function testGetLimitationTimePeriod()
  46. {
  47. $this->assertEquals(
  48. \Magento\Security\Model\Config::LIMITATION_TIME_PERIOD,
  49. $this->model->getLimitationTimePeriod()
  50. );
  51. }
  52. /**
  53. * Test get customer service email
  54. * @return void
  55. */
  56. public function testGetCustomerServiceEmail()
  57. {
  58. $email = 'test@example.com';
  59. $this->scopeConfigMock->expects($this->once())
  60. ->method('getValue')
  61. ->with(
  62. \Magento\Security\Model\Config::XML_PATH_EMAIL_RECIPIENT,
  63. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  64. )
  65. ->will(
  66. $this->returnValue($email)
  67. );
  68. $this->assertEquals($email, $this->model->getCustomerServiceEmail());
  69. }
  70. /**
  71. * Test get admin session lifetime
  72. * @return void
  73. */
  74. public function testGetAdminSessionLifetime()
  75. {
  76. $lifetime = 10;
  77. $this->scopeConfigMock->expects($this->once())
  78. ->method('getValue')
  79. ->with(\Magento\Backend\Model\Auth\Session::XML_PATH_SESSION_LIFETIME)
  80. ->will(
  81. $this->returnValue($lifetime)
  82. );
  83. $this->assertEquals($lifetime, $this->model->getAdminSessionLifetime());
  84. }
  85. /**
  86. * @param bool $isShared
  87. * @dataProvider dataProviderBoolValues
  88. */
  89. public function testIsAdminAccountSharingIsEnabled($isShared)
  90. {
  91. $this->scopeConfigMock->expects($this->once())
  92. ->method('isSetFlag')
  93. ->with(\Magento\Security\Model\Config::XML_PATH_ADMIN_ACCOUNT_SHARING)
  94. ->will(
  95. $this->returnValue($isShared)
  96. );
  97. $this->assertEquals($isShared, $this->model->isAdminAccountSharingEnabled());
  98. }
  99. /**
  100. * @return array
  101. */
  102. public function dataProviderBoolValues()
  103. {
  104. return [[true], [false]];
  105. }
  106. /**
  107. * @param int $resetMethod
  108. * @param int $scope
  109. * @dataProvider dataProviderResetMethodValues
  110. */
  111. public function testGetPasswordResetProtectionType($resetMethod, $scope)
  112. {
  113. $this->scopeConfigMock->expects($this->once())
  114. ->method('getValue')
  115. ->with(
  116. $this->getXmlPathPrefix($scope)
  117. . \Magento\Security\Model\Config::XML_PATH_PASSWORD_RESET_PROTECTION_TYPE
  118. )
  119. ->willReturn($resetMethod);
  120. $this->scopeMock->expects($this->once())
  121. ->method('getCurrentScope')
  122. ->willReturn($scope);
  123. $this->assertEquals($resetMethod, $this->model->getPasswordResetProtectionType($scope));
  124. }
  125. /**
  126. * @return array
  127. */
  128. public function dataProviderResetMethodValues()
  129. {
  130. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  131. $resetMethodSource = $objectManager->getObject(
  132. \Magento\Security\Model\Config\Source\ResetMethod::class
  133. );
  134. $optionKeys = array_keys($resetMethodSource->toArray());
  135. $data = [];
  136. foreach ($optionKeys as $key) {
  137. $data[] = [$key, \Magento\Framework\App\Area::AREA_ADMINHTML];
  138. $data[] = [$key, \Magento\Framework\App\Area::AREA_FRONTEND];
  139. }
  140. return $data;
  141. }
  142. /**
  143. * Get xml path by scope
  144. *
  145. * @param int $scope
  146. * @return string
  147. */
  148. protected function getXmlPathPrefix($scope)
  149. {
  150. if ($scope == \Magento\Framework\App\Area::AREA_ADMINHTML) {
  151. return \Magento\Security\Model\Config::XML_PATH_ADMIN_AREA;
  152. }
  153. return \Magento\Security\Model\Config::XML_PATH_FRONTEND_AREA;
  154. }
  155. /**
  156. * @param int $limitNumber
  157. * @param int $scope
  158. * @dataProvider dataProviderNumberValueWithScope
  159. */
  160. public function testGetMaxNumberPasswordResetRequests($limitNumber, $scope)
  161. {
  162. $this->scopeConfigMock->expects($this->once())
  163. ->method('getValue')
  164. ->with(
  165. $this->getXmlPathPrefix($scope)
  166. . \Magento\Security\Model\Config::XML_PATH_MAX_NUMBER_PASSWORD_RESET_REQUESTS
  167. )
  168. ->willReturn($limitNumber);
  169. $this->scopeMock->expects($this->once())
  170. ->method('getCurrentScope')
  171. ->willReturn($scope);
  172. $this->assertEquals($limitNumber, $this->model->getMaxNumberPasswordResetRequests());
  173. }
  174. /**
  175. * @param int $limitTime
  176. * @param int $scope
  177. * @dataProvider dataProviderNumberValueWithScope
  178. */
  179. public function testGetMinTimeBetweenPasswordResetRequests($limitTime, $scope)
  180. {
  181. $this->scopeConfigMock->expects($this->once())
  182. ->method('getValue')
  183. ->with(
  184. $this->getXmlPathPrefix($scope)
  185. . \Magento\Security\Model\Config::XML_PATH_MIN_TIME_BETWEEN_PASSWORD_RESET_REQUESTS
  186. )
  187. ->willReturn($limitTime);
  188. $this->scopeMock->expects($this->once())
  189. ->method('getCurrentScope')
  190. ->willReturn($scope);
  191. $this->assertEquals($limitTime * 60, $this->model->getMinTimeBetweenPasswordResetRequests());
  192. }
  193. /**
  194. * @return array
  195. */
  196. public function dataProviderNumberValueWithScope()
  197. {
  198. return [
  199. [5, \Magento\Framework\App\Area::AREA_ADMINHTML],
  200. [5, \Magento\Framework\App\Area::AREA_FRONTEND]
  201. ];
  202. }
  203. }