123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Captcha\Test\Unit\Model;
- /**
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- class DefaultTest extends \PHPUnit\Framework\TestCase
- {
- /**
- * Expiration frame
- */
- const EXPIRE_FRAME = 86400;
- /**
- * Captcha default config data
- * @var array
- */
- protected static $_defaultConfig = [
- 'type' => 'default',
- 'enable' => '1',
- 'font' => 'linlibertine',
- 'mode' => 'after_fail',
- 'forms' => 'user_forgotpassword,user_create',
- 'failed_attempts_login' => '3',
- 'failed_attempts_ip' => '1000',
- 'timeout' => '7',
- 'length' => '4-5',
- 'symbols' => 'ABCDEFGHJKMnpqrstuvwxyz23456789',
- 'case_sensitive' => '0',
- 'shown_to_logged_in_user' => ['contact_us' => 1],
- 'always_for' => [
- 'user_create',
- 'user_forgotpassword',
- 'contact_us',
- ],
- ];
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $_dirMock;
- /**
- * path to fonts
- * @var array
- */
- protected $_fontPath = [
- 'LinLibertine' => [
- 'label' => 'LinLibertine',
- 'path' => 'lib/internal/LinLibertineFont/LinLibertine_Bd-2.8.1.ttf',
- ],
- ];
- /**
- * @var \Magento\Captcha\Model\DefaultModel
- */
- protected $_object;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $_objectManager;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $_storeManager;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $session;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $_resLogFactory;
- /**
- * Sets up the fixture, for example, opens a network connection.
- * This method is called before a test is executed.
- */
- protected function setUp()
- {
- $this->session = $this->_getSessionStub();
- $this->_storeManager = $this->createPartialMock(\Magento\Store\Model\StoreManager::class, ['getStore']);
- $this->_storeManager->expects(
- $this->any()
- )->method(
- 'getStore'
- )->will(
- $this->returnValue($this->_getStoreStub())
- );
- // \Magento\Customer\Model\Session
- $this->_objectManager = $this->createMock(\Magento\Framework\ObjectManagerInterface::class);
- $this->_objectManager->expects(
- $this->any()
- )->method(
- 'get'
- )->will(
- $this->returnValueMap(
- [
- \Magento\Captcha\Helper\Data::class => $this->_getHelperStub(),
- \Magento\Customer\Model\Session::class => $this->session,
- ]
- )
- );
- $this->_resLogFactory = $this->createPartialMock(
- \Magento\Captcha\Model\ResourceModel\LogFactory::class,
- ['create']
- );
- $this->_resLogFactory->expects(
- $this->any()
- )->method(
- 'create'
- )->will(
- $this->returnValue($this->_getResourceModelStub())
- );
- $this->_object = new \Magento\Captcha\Model\DefaultModel(
- $this->session,
- $this->_getHelperStub(),
- $this->_resLogFactory,
- 'user_create'
- );
- }
- /**
- * @covers \Magento\Captcha\Model\DefaultModel::getBlockName
- */
- public function testGetBlockName()
- {
- $this->assertEquals($this->_object->getBlockName(), \Magento\Captcha\Block\Captcha\DefaultCaptcha::class);
- }
- /**
- * @covers \Magento\Captcha\Model\DefaultModel::isRequired
- */
- public function testIsRequired()
- {
- $this->assertTrue($this->_object->isRequired());
- }
- /**
- * @covers \Magento\Captcha\Model\DefaultModel::isCaseSensitive
- */
- public function testIsCaseSensitive()
- {
- self::$_defaultConfig['case_sensitive'] = '1';
- $this->assertEquals($this->_object->isCaseSensitive(), '1');
- self::$_defaultConfig['case_sensitive'] = '0';
- $this->assertEquals($this->_object->isCaseSensitive(), '0');
- }
- /**
- * @covers \Magento\Captcha\Model\DefaultModel::getFont
- */
- public function testGetFont()
- {
- $this->assertEquals($this->_object->getFont(), $this->_fontPath['LinLibertine']['path']);
- }
- /**
- * @covers \Magento\Captcha\Model\DefaultModel::getTimeout
- * @covers \Magento\Captcha\Model\DefaultModel::getExpiration
- */
- public function testGetTimeout()
- {
- $this->assertEquals($this->_object->getTimeout(), self::$_defaultConfig['timeout'] * 60);
- }
- /**
- * @covers \Magento\Captcha\Model\DefaultModel::isCorrect
- */
- public function testIsCorrect()
- {
- self::$_defaultConfig['case_sensitive'] = '1';
- $this->assertFalse($this->_object->isCorrect('abcdef5'));
- $sessionData = [
- 'user_create_word' => [
- 'data' => 'AbCdEf5',
- 'words' => 'AbCdEf5',
- 'expires' => time() + self::EXPIRE_FRAME
- ]
- ];
- $this->_object->getSession()->setData($sessionData);
- self::$_defaultConfig['case_sensitive'] = '0';
- $this->assertTrue($this->_object->isCorrect('abcdef5'));
- }
- /**
- * @covers \Magento\Captcha\Model\DefaultModel::getImgSrc
- */
- public function testGetImgSrc()
- {
- $this->assertEquals(
- $this->_object->getImgSrc(),
- 'http://localhost/pub/media/captcha/base/' . $this->_object->getId() . '.png'
- );
- }
- /**
- * @covers \Magento\Captcha\Model\DefaultModel::logAttempt
- */
- public function testLogAttempt()
- {
- $captcha = new \Magento\Captcha\Model\DefaultModel(
- $this->session,
- $this->_getHelperStub(),
- $this->_resLogFactory,
- 'user_create'
- );
- $captcha->logAttempt('admin');
- $this->assertEquals($captcha->getSession()->getData('user_create_show_captcha'), 1);
- }
- /**
- * @covers \Magento\Captcha\Model\DefaultModel::getWord
- */
- public function testGetWord()
- {
- $this->assertEquals($this->_object->getWord(), 'AbCdEf5');
- $this->_object->getSession()->setData(
- ['user_create_word' => ['data' => 'AbCdEf5', 'words' => 'AbCdEf5','expires' => time() - 360]]
- );
- $this->assertNull($this->_object->getWord());
- }
- /**
- * Create stub session object
- *
- * @return \Magento\Customer\Model\Session
- */
- protected function _getSessionStub()
- {
- $helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
- $sessionArgs = $helper->getConstructArguments(
- \Magento\Customer\Model\Session::class,
- ['storage' => new \Magento\Framework\Session\Storage()]
- );
- $session = $this->getMockBuilder(\Magento\Customer\Model\Session::class)
- ->setMethods(['isLoggedIn', 'getUserCreateWord'])
- ->setConstructorArgs($sessionArgs)
- ->getMock();
- $session->expects($this->any())->method('isLoggedIn')->will($this->returnValue(false));
- $session->setData([
- 'user_create_word' => [
- 'data' => 'AbCdEf5',
- 'words' => 'AbCdEf5',
- 'expires' => time() + self::EXPIRE_FRAME
- ]
- ]);
- return $session;
- }
- /**
- * Create helper stub
- * @return \Magento\Captcha\Helper\Data
- */
- protected function _getHelperStub()
- {
- $helper = $this->getMockBuilder(
- \Magento\Captcha\Helper\Data::class
- )->disableOriginalConstructor()->setMethods(
- ['getConfig', 'getFonts', '_getWebsiteCode', 'getImgUrl']
- )->getMock();
- $helper->expects(
- $this->any()
- )->method(
- 'getConfig'
- )->will(
- $this->returnCallback('Magento\Captcha\Test\Unit\Model\DefaultTest::getConfigNodeStub')
- );
- $helper->expects($this->any())->method('getFonts')->will($this->returnValue($this->_fontPath));
- $helper->expects($this->any())->method('_getWebsiteCode')->will($this->returnValue('base'));
- $helper->expects(
- $this->any()
- )->method(
- 'getImgUrl'
- )->will(
- $this->returnValue('http://localhost/pub/media/captcha/base/')
- );
- return $helper;
- }
- /**
- * Get stub for resource model
- * @return \Magento\Captcha\Model\ResourceModel\Log
- */
- protected function _getResourceModelStub()
- {
- $resourceModel = $this->createPartialMock(
- \Magento\Captcha\Model\ResourceModel\Log::class,
- ['countAttemptsByRemoteAddress', 'countAttemptsByUserLogin', 'logAttempt', '__wakeup']
- );
- $resourceModel->expects($this->any())->method('logAttempt');
- $resourceModel->expects($this->any())->method('countAttemptsByRemoteAddress')->will($this->returnValue(0));
- $resourceModel->expects($this->any())->method('countAttemptsByUserLogin')->will($this->returnValue(3));
- return $resourceModel;
- }
- /**
- * Mock get config method
- * @static
- * @return string
- * @throws \InvalidArgumentException
- */
- public static function getConfigNodeStub()
- {
- $args = func_get_args();
- $hashName = $args[0];
- if (array_key_exists($hashName, self::$_defaultConfig)) {
- return self::$_defaultConfig[$hashName];
- }
- throw new \InvalidArgumentException('Unknow id = ' . $hashName);
- }
- /**
- * Create store stub
- *
- * @return \Magento\Store\Model\Store
- */
- protected function _getStoreStub()
- {
- $store = $this->createPartialMock(\Magento\Store\Model\Store::class, ['isAdmin', 'getBaseUrl']);
- $store->expects($this->any())->method('getBaseUrl')->will($this->returnValue('http://localhost/pub/media/'));
- $store->expects($this->any())->method('isAdmin')->will($this->returnValue(false));
- return $store;
- }
- /**
- * @param boolean $expectedResult
- * @param string $formId
- * @dataProvider isShownToLoggedInUserDataProvider
- */
- public function testIsShownToLoggedInUser($expectedResult, $formId)
- {
- $captcha = new \Magento\Captcha\Model\DefaultModel(
- $this->session,
- $this->_getHelperStub(),
- $this->_resLogFactory,
- $formId
- );
- $this->assertEquals($expectedResult, $captcha->isShownToLoggedInUser());
- }
- /**
- * @return array
- */
- public function isShownToLoggedInUserDataProvider()
- {
- return [
- [true, 'contact_us'],
- [false, 'user_create'],
- [false, 'user_forgotpassword']
- ];
- }
- }
|