DefaultTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Captcha\Test\Unit\Model;
  7. /**
  8. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  9. */
  10. class DefaultTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /**
  13. * Expiration frame
  14. */
  15. const EXPIRE_FRAME = 86400;
  16. /**
  17. * Captcha default config data
  18. * @var array
  19. */
  20. protected static $_defaultConfig = [
  21. 'type' => 'default',
  22. 'enable' => '1',
  23. 'font' => 'linlibertine',
  24. 'mode' => 'after_fail',
  25. 'forms' => 'user_forgotpassword,user_create',
  26. 'failed_attempts_login' => '3',
  27. 'failed_attempts_ip' => '1000',
  28. 'timeout' => '7',
  29. 'length' => '4-5',
  30. 'symbols' => 'ABCDEFGHJKMnpqrstuvwxyz23456789',
  31. 'case_sensitive' => '0',
  32. 'shown_to_logged_in_user' => ['contact_us' => 1],
  33. 'always_for' => [
  34. 'user_create',
  35. 'user_forgotpassword',
  36. 'contact_us',
  37. ],
  38. ];
  39. /**
  40. * @var \PHPUnit_Framework_MockObject_MockObject
  41. */
  42. protected $_dirMock;
  43. /**
  44. * path to fonts
  45. * @var array
  46. */
  47. protected $_fontPath = [
  48. 'LinLibertine' => [
  49. 'label' => 'LinLibertine',
  50. 'path' => 'lib/internal/LinLibertineFont/LinLibertine_Bd-2.8.1.ttf',
  51. ],
  52. ];
  53. /**
  54. * @var \Magento\Captcha\Model\DefaultModel
  55. */
  56. protected $_object;
  57. /**
  58. * @var \PHPUnit_Framework_MockObject_MockObject
  59. */
  60. protected $_objectManager;
  61. /**
  62. * @var \PHPUnit_Framework_MockObject_MockObject
  63. */
  64. protected $_storeManager;
  65. /**
  66. * @var \PHPUnit_Framework_MockObject_MockObject
  67. */
  68. protected $session;
  69. /**
  70. * @var \PHPUnit_Framework_MockObject_MockObject
  71. */
  72. protected $_resLogFactory;
  73. /**
  74. * Sets up the fixture, for example, opens a network connection.
  75. * This method is called before a test is executed.
  76. */
  77. protected function setUp()
  78. {
  79. $this->session = $this->_getSessionStub();
  80. $this->_storeManager = $this->createPartialMock(\Magento\Store\Model\StoreManager::class, ['getStore']);
  81. $this->_storeManager->expects(
  82. $this->any()
  83. )->method(
  84. 'getStore'
  85. )->will(
  86. $this->returnValue($this->_getStoreStub())
  87. );
  88. // \Magento\Customer\Model\Session
  89. $this->_objectManager = $this->createMock(\Magento\Framework\ObjectManagerInterface::class);
  90. $this->_objectManager->expects(
  91. $this->any()
  92. )->method(
  93. 'get'
  94. )->will(
  95. $this->returnValueMap(
  96. [
  97. \Magento\Captcha\Helper\Data::class => $this->_getHelperStub(),
  98. \Magento\Customer\Model\Session::class => $this->session,
  99. ]
  100. )
  101. );
  102. $this->_resLogFactory = $this->createPartialMock(
  103. \Magento\Captcha\Model\ResourceModel\LogFactory::class,
  104. ['create']
  105. );
  106. $this->_resLogFactory->expects(
  107. $this->any()
  108. )->method(
  109. 'create'
  110. )->will(
  111. $this->returnValue($this->_getResourceModelStub())
  112. );
  113. $this->_object = new \Magento\Captcha\Model\DefaultModel(
  114. $this->session,
  115. $this->_getHelperStub(),
  116. $this->_resLogFactory,
  117. 'user_create'
  118. );
  119. }
  120. /**
  121. * @covers \Magento\Captcha\Model\DefaultModel::getBlockName
  122. */
  123. public function testGetBlockName()
  124. {
  125. $this->assertEquals($this->_object->getBlockName(), \Magento\Captcha\Block\Captcha\DefaultCaptcha::class);
  126. }
  127. /**
  128. * @covers \Magento\Captcha\Model\DefaultModel::isRequired
  129. */
  130. public function testIsRequired()
  131. {
  132. $this->assertTrue($this->_object->isRequired());
  133. }
  134. /**
  135. * @covers \Magento\Captcha\Model\DefaultModel::isCaseSensitive
  136. */
  137. public function testIsCaseSensitive()
  138. {
  139. self::$_defaultConfig['case_sensitive'] = '1';
  140. $this->assertEquals($this->_object->isCaseSensitive(), '1');
  141. self::$_defaultConfig['case_sensitive'] = '0';
  142. $this->assertEquals($this->_object->isCaseSensitive(), '0');
  143. }
  144. /**
  145. * @covers \Magento\Captcha\Model\DefaultModel::getFont
  146. */
  147. public function testGetFont()
  148. {
  149. $this->assertEquals($this->_object->getFont(), $this->_fontPath['LinLibertine']['path']);
  150. }
  151. /**
  152. * @covers \Magento\Captcha\Model\DefaultModel::getTimeout
  153. * @covers \Magento\Captcha\Model\DefaultModel::getExpiration
  154. */
  155. public function testGetTimeout()
  156. {
  157. $this->assertEquals($this->_object->getTimeout(), self::$_defaultConfig['timeout'] * 60);
  158. }
  159. /**
  160. * @covers \Magento\Captcha\Model\DefaultModel::isCorrect
  161. */
  162. public function testIsCorrect()
  163. {
  164. self::$_defaultConfig['case_sensitive'] = '1';
  165. $this->assertFalse($this->_object->isCorrect('abcdef5'));
  166. $sessionData = [
  167. 'user_create_word' => [
  168. 'data' => 'AbCdEf5',
  169. 'words' => 'AbCdEf5',
  170. 'expires' => time() + self::EXPIRE_FRAME
  171. ]
  172. ];
  173. $this->_object->getSession()->setData($sessionData);
  174. self::$_defaultConfig['case_sensitive'] = '0';
  175. $this->assertTrue($this->_object->isCorrect('abcdef5'));
  176. }
  177. /**
  178. * @covers \Magento\Captcha\Model\DefaultModel::getImgSrc
  179. */
  180. public function testGetImgSrc()
  181. {
  182. $this->assertEquals(
  183. $this->_object->getImgSrc(),
  184. 'http://localhost/pub/media/captcha/base/' . $this->_object->getId() . '.png'
  185. );
  186. }
  187. /**
  188. * @covers \Magento\Captcha\Model\DefaultModel::logAttempt
  189. */
  190. public function testLogAttempt()
  191. {
  192. $captcha = new \Magento\Captcha\Model\DefaultModel(
  193. $this->session,
  194. $this->_getHelperStub(),
  195. $this->_resLogFactory,
  196. 'user_create'
  197. );
  198. $captcha->logAttempt('admin');
  199. $this->assertEquals($captcha->getSession()->getData('user_create_show_captcha'), 1);
  200. }
  201. /**
  202. * @covers \Magento\Captcha\Model\DefaultModel::getWord
  203. */
  204. public function testGetWord()
  205. {
  206. $this->assertEquals($this->_object->getWord(), 'AbCdEf5');
  207. $this->_object->getSession()->setData(
  208. ['user_create_word' => ['data' => 'AbCdEf5', 'words' => 'AbCdEf5','expires' => time() - 360]]
  209. );
  210. $this->assertNull($this->_object->getWord());
  211. }
  212. /**
  213. * Create stub session object
  214. *
  215. * @return \Magento\Customer\Model\Session
  216. */
  217. protected function _getSessionStub()
  218. {
  219. $helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  220. $sessionArgs = $helper->getConstructArguments(
  221. \Magento\Customer\Model\Session::class,
  222. ['storage' => new \Magento\Framework\Session\Storage()]
  223. );
  224. $session = $this->getMockBuilder(\Magento\Customer\Model\Session::class)
  225. ->setMethods(['isLoggedIn', 'getUserCreateWord'])
  226. ->setConstructorArgs($sessionArgs)
  227. ->getMock();
  228. $session->expects($this->any())->method('isLoggedIn')->will($this->returnValue(false));
  229. $session->setData([
  230. 'user_create_word' => [
  231. 'data' => 'AbCdEf5',
  232. 'words' => 'AbCdEf5',
  233. 'expires' => time() + self::EXPIRE_FRAME
  234. ]
  235. ]);
  236. return $session;
  237. }
  238. /**
  239. * Create helper stub
  240. * @return \Magento\Captcha\Helper\Data
  241. */
  242. protected function _getHelperStub()
  243. {
  244. $helper = $this->getMockBuilder(
  245. \Magento\Captcha\Helper\Data::class
  246. )->disableOriginalConstructor()->setMethods(
  247. ['getConfig', 'getFonts', '_getWebsiteCode', 'getImgUrl']
  248. )->getMock();
  249. $helper->expects(
  250. $this->any()
  251. )->method(
  252. 'getConfig'
  253. )->will(
  254. $this->returnCallback('Magento\Captcha\Test\Unit\Model\DefaultTest::getConfigNodeStub')
  255. );
  256. $helper->expects($this->any())->method('getFonts')->will($this->returnValue($this->_fontPath));
  257. $helper->expects($this->any())->method('_getWebsiteCode')->will($this->returnValue('base'));
  258. $helper->expects(
  259. $this->any()
  260. )->method(
  261. 'getImgUrl'
  262. )->will(
  263. $this->returnValue('http://localhost/pub/media/captcha/base/')
  264. );
  265. return $helper;
  266. }
  267. /**
  268. * Get stub for resource model
  269. * @return \Magento\Captcha\Model\ResourceModel\Log
  270. */
  271. protected function _getResourceModelStub()
  272. {
  273. $resourceModel = $this->createPartialMock(
  274. \Magento\Captcha\Model\ResourceModel\Log::class,
  275. ['countAttemptsByRemoteAddress', 'countAttemptsByUserLogin', 'logAttempt', '__wakeup']
  276. );
  277. $resourceModel->expects($this->any())->method('logAttempt');
  278. $resourceModel->expects($this->any())->method('countAttemptsByRemoteAddress')->will($this->returnValue(0));
  279. $resourceModel->expects($this->any())->method('countAttemptsByUserLogin')->will($this->returnValue(3));
  280. return $resourceModel;
  281. }
  282. /**
  283. * Mock get config method
  284. * @static
  285. * @return string
  286. * @throws \InvalidArgumentException
  287. */
  288. public static function getConfigNodeStub()
  289. {
  290. $args = func_get_args();
  291. $hashName = $args[0];
  292. if (array_key_exists($hashName, self::$_defaultConfig)) {
  293. return self::$_defaultConfig[$hashName];
  294. }
  295. throw new \InvalidArgumentException('Unknow id = ' . $hashName);
  296. }
  297. /**
  298. * Create store stub
  299. *
  300. * @return \Magento\Store\Model\Store
  301. */
  302. protected function _getStoreStub()
  303. {
  304. $store = $this->createPartialMock(\Magento\Store\Model\Store::class, ['isAdmin', 'getBaseUrl']);
  305. $store->expects($this->any())->method('getBaseUrl')->will($this->returnValue('http://localhost/pub/media/'));
  306. $store->expects($this->any())->method('isAdmin')->will($this->returnValue(false));
  307. return $store;
  308. }
  309. /**
  310. * @param boolean $expectedResult
  311. * @param string $formId
  312. * @dataProvider isShownToLoggedInUserDataProvider
  313. */
  314. public function testIsShownToLoggedInUser($expectedResult, $formId)
  315. {
  316. $captcha = new \Magento\Captcha\Model\DefaultModel(
  317. $this->session,
  318. $this->_getHelperStub(),
  319. $this->_resLogFactory,
  320. $formId
  321. );
  322. $this->assertEquals($expectedResult, $captcha->isShownToLoggedInUser());
  323. }
  324. /**
  325. * @return array
  326. */
  327. public function isShownToLoggedInUserDataProvider()
  328. {
  329. return [
  330. [true, 'contact_us'],
  331. [false, 'user_create'],
  332. [false, 'user_forgotpassword']
  333. ];
  334. }
  335. }