Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
DefaultTest.php
Go to the documentation of this file.
1 <?php
7 
11 class DefaultTest extends \PHPUnit\Framework\TestCase
12 {
16  const EXPIRE_FRAME = 86400;
17 
22  protected static $_defaultConfig = [
23  'type' => 'default',
24  'enable' => '1',
25  'font' => 'linlibertine',
26  'mode' => 'after_fail',
27  'forms' => 'user_forgotpassword,user_create',
28  'failed_attempts_login' => '3',
29  'failed_attempts_ip' => '1000',
30  'timeout' => '7',
31  'length' => '4-5',
32  'symbols' => 'ABCDEFGHJKMnpqrstuvwxyz23456789',
33  'case_sensitive' => '0',
34  'shown_to_logged_in_user' => ['contact_us' => 1],
35  'always_for' => [
36  'user_create',
37  'user_forgotpassword',
38  'contact_us',
39  ],
40  ];
41 
45  protected $_dirMock;
46 
51  protected $_fontPath = [
52  'LinLibertine' => [
53  'label' => 'LinLibertine',
54  'path' => 'lib/internal/LinLibertineFont/LinLibertine_Bd-2.8.1.ttf',
55  ],
56  ];
57 
61  protected $_object;
62 
66  protected $_objectManager;
67 
71  protected $_storeManager;
72 
76  protected $session;
77 
81  protected $_resLogFactory;
82 
87  protected function setUp()
88  {
89  $this->session = $this->_getSessionStub();
90 
91  $this->_storeManager = $this->createPartialMock(\Magento\Store\Model\StoreManager::class, ['getStore']);
92  $this->_storeManager->expects(
93  $this->any()
94  )->method(
95  'getStore'
96  )->will(
97  $this->returnValue($this->_getStoreStub())
98  );
99 
100  // \Magento\Customer\Model\Session
101  $this->_objectManager = $this->createMock(\Magento\Framework\ObjectManagerInterface::class);
102  $this->_objectManager->expects(
103  $this->any()
104  )->method(
105  'get'
106  )->will(
107  $this->returnValueMap(
108  [
109  \Magento\Captcha\Helper\Data::class => $this->_getHelperStub(),
110  \Magento\Customer\Model\Session::class => $this->session,
111  ]
112  )
113  );
114 
115  $this->_resLogFactory = $this->createPartialMock(
116  \Magento\Captcha\Model\ResourceModel\LogFactory::class,
117  ['create']
118  );
119  $this->_resLogFactory->expects(
120  $this->any()
121  )->method(
122  'create'
123  )->will(
124  $this->returnValue($this->_getResourceModelStub())
125  );
126 
127  $this->_object = new \Magento\Captcha\Model\DefaultModel(
128  $this->session,
129  $this->_getHelperStub(),
130  $this->_resLogFactory,
131  'user_create'
132  );
133  }
134 
138  public function testGetBlockName()
139  {
140  $this->assertEquals($this->_object->getBlockName(), \Magento\Captcha\Block\Captcha\DefaultCaptcha::class);
141  }
142 
146  public function testIsRequired()
147  {
148  $this->assertTrue($this->_object->isRequired());
149  }
150 
154  public function testIsCaseSensitive()
155  {
156  self::$_defaultConfig['case_sensitive'] = '1';
157  $this->assertEquals($this->_object->isCaseSensitive(), '1');
158  self::$_defaultConfig['case_sensitive'] = '0';
159  $this->assertEquals($this->_object->isCaseSensitive(), '0');
160  }
161 
165  public function testGetFont()
166  {
167  $this->assertEquals($this->_object->getFont(), $this->_fontPath['LinLibertine']['path']);
168  }
169 
174  public function testGetTimeout()
175  {
176  $this->assertEquals($this->_object->getTimeout(), self::$_defaultConfig['timeout'] * 60);
177  }
178 
182  public function testIsCorrect()
183  {
184  self::$_defaultConfig['case_sensitive'] = '1';
185  $this->assertFalse($this->_object->isCorrect('abcdef5'));
186  $sessionData = ['user_create_word' => ['data' => 'AbCdEf5', 'expires' => time() + self::EXPIRE_FRAME]];
187  $this->_object->getSession()->setData($sessionData);
188  self::$_defaultConfig['case_sensitive'] = '0';
189  $this->assertTrue($this->_object->isCorrect('abcdef5'));
190  }
191 
195  public function testGetImgSrc()
196  {
197  $this->assertEquals(
198  $this->_object->getImgSrc(),
199  'http://localhost/pub/media/captcha/base/' . $this->_object->getId() . '.png'
200  );
201  }
202 
206  public function testLogAttempt()
207  {
208  $captcha = new \Magento\Captcha\Model\DefaultModel(
209  $this->session,
210  $this->_getHelperStub(),
211  $this->_resLogFactory,
212  'user_create'
213  );
214 
215  $captcha->logAttempt('admin');
216 
217  $this->assertEquals($captcha->getSession()->getData('user_create_show_captcha'), 1);
218  }
219 
223  public function testGetWord()
224  {
225  $this->assertEquals($this->_object->getWord(), 'AbCdEf5');
226  $this->_object->getSession()->setData(
227  ['user_create_word' => ['data' => 'AbCdEf5', 'expires' => time() - 360]]
228  );
229  $this->assertNull($this->_object->getWord());
230  }
231 
237  protected function _getSessionStub()
238  {
239  $helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
240  $sessionArgs = $helper->getConstructArguments(
241  \Magento\Customer\Model\Session::class,
242  ['storage' => new \Magento\Framework\Session\Storage()]
243  );
244  $session = $this->getMockBuilder(\Magento\Customer\Model\Session::class)
245  ->setMethods(['isLoggedIn', 'getUserCreateWord'])
246  ->setConstructorArgs($sessionArgs)
247  ->getMock();
248  $session->expects($this->any())->method('isLoggedIn')->will($this->returnValue(false));
249 
250  $session->setData(['user_create_word' => ['data' => 'AbCdEf5', 'expires' => time() + self::EXPIRE_FRAME]]);
251  return $session;
252  }
253 
258  protected function _getHelperStub()
259  {
260  $helper = $this->getMockBuilder(
261  \Magento\Captcha\Helper\Data::class
262  )->disableOriginalConstructor()->setMethods(
263  ['getConfig', 'getFonts', '_getWebsiteCode', 'getImgUrl']
264  )->getMock();
265 
266  $helper->expects(
267  $this->any()
268  )->method(
269  'getConfig'
270  )->will(
271  $this->returnCallback('Magento\Captcha\Test\Unit\Model\DefaultTest::getConfigNodeStub')
272  );
273 
274  $helper->expects($this->any())->method('getFonts')->will($this->returnValue($this->_fontPath));
275 
276  $helper->expects($this->any())->method('_getWebsiteCode')->will($this->returnValue('base'));
277 
278  $helper->expects(
279  $this->any()
280  )->method(
281  'getImgUrl'
282  )->will(
283  $this->returnValue('http://localhost/pub/media/captcha/base/')
284  );
285 
286  return $helper;
287  }
288 
293  protected function _getResourceModelStub()
294  {
295  $resourceModel = $this->createPartialMock(
296  \Magento\Captcha\Model\ResourceModel\Log::class,
297  ['countAttemptsByRemoteAddress', 'countAttemptsByUserLogin', 'logAttempt', '__wakeup']
298  );
299 
300  $resourceModel->expects($this->any())->method('logAttempt');
301 
302  $resourceModel->expects($this->any())->method('countAttemptsByRemoteAddress')->will($this->returnValue(0));
303 
304  $resourceModel->expects($this->any())->method('countAttemptsByUserLogin')->will($this->returnValue(3));
305  return $resourceModel;
306  }
307 
314  public static function getConfigNodeStub()
315  {
316  $args = func_get_args();
317  $hashName = $args[0];
318 
319  if (array_key_exists($hashName, self::$_defaultConfig)) {
320  return self::$_defaultConfig[$hashName];
321  }
322 
323  throw new \InvalidArgumentException('Unknow id = ' . $hashName);
324  }
325 
331  protected function _getStoreStub()
332  {
333  $store = $this->createPartialMock(\Magento\Store\Model\Store::class, ['isAdmin', 'getBaseUrl']);
334  $store->expects($this->any())->method('getBaseUrl')->will($this->returnValue('http://localhost/pub/media/'));
335  $store->expects($this->any())->method('isAdmin')->will($this->returnValue(false));
336  return $store;
337  }
338 
344  public function testIsShownToLoggedInUser($expectedResult, $formId)
345  {
346  $captcha = new \Magento\Captcha\Model\DefaultModel(
347  $this->session,
348  $this->_getHelperStub(),
349  $this->_resLogFactory,
350  $formId
351  );
352  $this->assertEquals($expectedResult, $captcha->isShownToLoggedInUser());
353  }
354 
359  {
360  return [
361  [true, 'contact_us'],
362  [false, 'user_create'],
363  [false, 'user_forgotpassword']
364  ];
365  }
366 }
$helper
Definition: iframe.phtml:13
testIsShownToLoggedInUser($expectedResult, $formId)
$resourceModel
Definition: tablerates.php:10
$captcha
Definition: default.phtml:12