Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CaptchaLockoutCustomerTest.php
Go to the documentation of this file.
1 <?php
8 
11 use Magento\Customer\Test\Page\Adminhtml\CustomerIndexEdit;
12 use Magento\Customer\Test\Page\CustomerAccountLogin;
13 use Magento\Mtf\Fixture\FixtureFactory;
14 use Magento\Mtf\Fixture\FixtureInterface;
15 use Magento\Mtf\TestCase\Injectable;
16 use Magento\Mtf\TestStep\TestStepFactory;
17 
34 class CaptchaLockoutCustomerTest extends Injectable
35 {
41  private $stepFactory;
42 
48  private $fixtureFactory;
49 
55  private $customerAccountLogin;
56 
62  private $customerIndexEdit;
63 
69  private $configData;
70 
80  public function __inject(
81  TestStepFactory $stepFactory,
82  FixtureFactory $fixtureFactory,
83  CustomerAccountLogin $customerAccountLogin,
84  CustomerIndexEdit $customerIndexEdit
85  ) {
86  $this->stepFactory = $stepFactory;
87  $this->fixtureFactory = $fixtureFactory;
88  $this->customerAccountLogin = $customerAccountLogin;
89  $this->customerIndexEdit = $customerIndexEdit;
90  }
91 
102  public function test(
104  $configData,
105  $captcha,
106  $incorrectPassword,
107  $attempts
108  ) {
109  $this->configData = $configData;
110 
111  // Preconditions
112  $this->stepFactory->create(
113  \Magento\Config\Test\TestStep\SetupConfigurationStep::class,
114  ['configData' => $this->configData]
115  )->run();
116  $customer->persist();
117 
118  $correctData = $customer->getData();
119  $correctData['captcha'] = $captcha;
120  $correctData['group_id'] = [
121  'customerGroup' => $customer->getDataFieldConfig('group_id')['source']->getCustomerGroup()
122  ];
123 
124  $newData = $customer->getData();
125  $newData['captcha'] = $captcha;
126  $newData['group_id'] = [
127  'customerGroup' => $customer->getDataFieldConfig('group_id')['source']->getCustomerGroup()
128  ];
129  $newData['password'] = $incorrectPassword;
130 
131  $incorrectCustomer = $this->fixtureFactory->createByCode('customer', ['data' => $newData]);
132 
133  // Steps
134  $this->customerAccountLogin->open();
135 
136  // Fill incorrect password $attempts times.
137  $this->customerLogin($incorrectCustomer, $this->customerAccountLogin->getLoginBlock(), $attempts);
138 
139  // Fill correct captcha and incorrect password $attempts times.
140  $this->customerLogin($incorrectCustomer, $this->customerAccountLogin->getLoginBlockWithCaptcha(), $attempts);
141 
142  // Log in customer with correct captcha and correct password.
143  $customer = $this->fixtureFactory->createByCode('customer', ['data' => $correctData]);
144  $this->customerLogin($customer, $this->customerAccountLogin->getLoginBlockWithCaptcha(), 1);
145  }
146 
155  private function customerLogin(FixtureInterface $customer, Login $loginForm, $attempts)
156  {
157  for ($i = 0; $i < $attempts; $i++) {
158  $loginForm->fill($customer);
159  $loginForm->submit();
160  }
161  }
162 
168  public function tearDown()
169  {
170  $this->stepFactory->create(
171  \Magento\Config\Test\TestStep\SetupConfigurationStep::class,
172  ['configData' => $this->configData, 'rollback' => true]
173  )->run();
174  }
175 }
$customer
Definition: customers.php:11
$captcha
Definition: default.phtml:12
__inject(TestStepFactory $stepFactory, FixtureFactory $fixtureFactory, CustomerAccountLogin $customerAccountLogin, CustomerIndexEdit $customerIndexEdit)
$i
Definition: gallery.phtml:31
test(Customer $customer, $configData, $captcha, $incorrectPassword, $attempts)