CaseCheckOnFrontendUnsuccessfulMessageWhenCaptchaFailedTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Captcha\Observer;
  7. use Magento\Framework\Data\Form\FormKey;
  8. use Magento\Framework\Message\MessageInterface;
  9. use Magento\TestFramework\Request;
  10. use Magento\TestFramework\TestCase\AbstractController;
  11. /**
  12. * Test captcha observer behavior
  13. *
  14. * @magentoAppArea frontend
  15. */
  16. class CaseCheckOnFrontendUnsuccessfulMessageWhenCaptchaFailedTest extends AbstractController
  17. {
  18. /**
  19. * Test incorrect captcha on customer login page
  20. *
  21. * @magentoDbIsolation enabled
  22. * @magentoAppIsolation enabled
  23. * @magentoConfigFixture default_store customer/captcha/enable 1
  24. * @magentoConfigFixture default_store customer/captcha/forms user_login
  25. * @magentoConfigFixture default_store customer/captcha/mode always
  26. */
  27. public function testLoginCheckUnsuccessfulMessageWhenCaptchaFailed()
  28. {
  29. /** @var FormKey $formKey */
  30. $formKey = $this->_objectManager->get(FormKey::class);
  31. $post = [
  32. 'login' => [
  33. 'username' => 'dummy@dummy.com',
  34. 'password' => 'dummy_password1',
  35. ],
  36. 'captcha' => ['user_login' => 'wrong_captcha'],
  37. 'form_key' => $formKey->getFormKey(),
  38. ];
  39. $this->prepareRequestData($post);
  40. $this->dispatch('customer/account/loginPost');
  41. $this->assertRedirect($this->stringContains('customer/account/login'));
  42. $this->assertSessionMessages(
  43. $this->equalTo(['Incorrect CAPTCHA']),
  44. MessageInterface::TYPE_ERROR
  45. );
  46. }
  47. /**
  48. * Test incorrect captcha on customer forgot password page
  49. *
  50. * @codingStandardsIgnoreStart
  51. * @magentoConfigFixture current_store customer/password/limit_password_reset_requests_method 0
  52. * @magentoConfigFixture default_store customer/captcha/enable 1
  53. * @magentoConfigFixture default_store customer/captcha/forms user_forgotpassword
  54. * @magentoConfigFixture default_store customer/captcha/mode always
  55. */
  56. public function testForgotPasswordCheckUnsuccessfulMessageWhenCaptchaFailed()
  57. {
  58. $post = ['email' => 'dummy@dummy.com'];
  59. $this->prepareRequestData($post);
  60. $this->dispatch('customer/account/forgotPasswordPost');
  61. $this->assertRedirect($this->stringContains('customer/account/forgotpassword'));
  62. $this->assertSessionMessages(
  63. $this->equalTo(['Incorrect CAPTCHA']),
  64. MessageInterface::TYPE_ERROR
  65. );
  66. }
  67. /**
  68. * Test incorrect captcha on customer create account page
  69. *
  70. * @codingStandardsIgnoreStart
  71. * @magentoConfigFixture current_store customer/password/limit_password_reset_requests_method 0
  72. * @magentoConfigFixture default_store customer/captcha/enable 1
  73. * @magentoConfigFixture default_store customer/captcha/forms user_create
  74. * @magentoConfigFixture default_store customer/captcha/mode always
  75. */
  76. public function testCreateAccountCheckUnsuccessfulMessageWhenCaptchaFailed()
  77. {
  78. /** @var FormKey $formKey */
  79. $formKey = $this->_objectManager->get(FormKey::class);
  80. $post = [
  81. 'firstname' => 'Firstname',
  82. 'lastname' => 'Lastname',
  83. 'email' => 'dummy@dummy.com',
  84. 'password' => 'TestPassword123',
  85. 'password_confirmation' => 'TestPassword123',
  86. 'captcha' => ['user_create' => 'wrong_captcha'],
  87. 'form_key' => $formKey->getFormKey(),
  88. ];
  89. $this->prepareRequestData($post);
  90. $this->dispatch('customer/account/createPost');
  91. $this->assertRedirect($this->stringContains('customer/account/create'));
  92. $this->assertSessionMessages(
  93. $this->equalTo(['Incorrect CAPTCHA']),
  94. MessageInterface::TYPE_ERROR
  95. );
  96. }
  97. /**
  98. * @param array $postData
  99. * @return void
  100. */
  101. private function prepareRequestData($postData)
  102. {
  103. $this->getRequest()->setMethod(Request::METHOD_POST);
  104. $this->getRequest()->setPostValue($postData);
  105. }
  106. }