Skip to content

Commit

Permalink
Magento Fork initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Yegor Shytikov committed Oct 13, 2021
1 parent c25f070 commit 4ac46e1
Show file tree
Hide file tree
Showing 25 changed files with 149 additions and 144 deletions.
38 changes: 29 additions & 9 deletions CustomerData/Captcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,50 @@
namespace Magento\Captcha\CustomerData;

use Magento\Customer\CustomerData\SectionSourceInterface;
use Magento\Customer\Model\Session as CustomerSession;
use Magento\Captcha\Model\DefaultModel;
use Magento\Captcha\Helper\Data as CaptchaHelper;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\DataObject;

/**
* Captcha section
* Captcha section.
*
* @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
*/
class Captcha extends \Magento\Framework\DataObject implements SectionSourceInterface
class Captcha extends DataObject implements SectionSourceInterface
{
/**
* @var array
*/
private $formIds;

/**
* @var \Magento\Captcha\Helper\Data
* @var CaptchaHelper
*/
private $helper;

/**
* @param \Magento\Captcha\Helper\Data $helper
* @var CustomerSession
*/
private $customerSession;

/**
* @param CaptchaHelper $helper
* @param array $formIds
* @param array $data
* @codeCoverageIgnore
* @param CustomerSession|null $customerSession
*/
public function __construct(
\Magento\Captcha\Helper\Data $helper,
CaptchaHelper $helper,
array $formIds,
array $data = []
array $data = [],
?CustomerSession $customerSession = null
) {
parent::__construct($data);
$this->helper = $helper;
$this->formIds = $formIds;
parent::__construct($data);
$this->customerSession = $customerSession ?? ObjectManager::getInstance()->get(CustomerSession::class);
}

/**
Expand All @@ -49,9 +63,15 @@ public function getSectionData() :array
$data = [];

foreach ($this->formIds as $formId) {
/** @var DefaultModel $captchaModel */
$captchaModel = $this->helper->getCaptcha($formId);
$login = '';
if ($this->customerSession->isLoggedIn()) {
$login = $this->customerSession->getCustomerData()->getEmail();
}
$required = $captchaModel->isRequired($login);
$data[$formId] = [
'isRequired' => $captchaModel->isRequired(),
'isRequired' => $required,
'timestamp' => time()
];
}
Expand Down
35 changes: 17 additions & 18 deletions Model/DefaultModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Captcha\Model;

use Magento\Captcha\Helper\Data;
use Magento\Framework\Math\Random;

/**
* Implementation of \Zend\Captcha\Image
*
* @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
*
* @api
* @since 100.0.2
*/
Expand Down Expand Up @@ -83,24 +88,32 @@ class DefaultModel extends \Zend\Captcha\Image implements \Magento\Captcha\Model
*/
private $words;

/**
* @var Random
*/
private $randomMath;

/**
* @param \Magento\Framework\Session\SessionManagerInterface $session
* @param \Magento\Captcha\Helper\Data $captchaData
* @param ResourceModel\LogFactory $resLogFactory
* @param string $formId
* @param Random $randomMath
* @throws \Zend\Captcha\Exception\ExtensionNotLoadedException
*/
public function __construct(
\Magento\Framework\Session\SessionManagerInterface $session,
\Magento\Captcha\Helper\Data $captchaData,
\Magento\Captcha\Model\ResourceModel\LogFactory $resLogFactory,
$formId
$formId,
Random $randomMath = null
) {
parent::__construct();
$this->session = $session;
$this->captchaData = $captchaData;
$this->resLogFactory = $resLogFactory;
$this->formId = $formId;
$this->randomMath = $randomMath ?? \Magento\Framework\App\ObjectManager::getInstance()->get(Random::class);
}

/**
Expand Down Expand Up @@ -382,23 +395,9 @@ public function setShowCaptchaInSession($value = true)
*/
protected function generateWord()
{
$word = '';
$symbols = $this->getSymbols();
$symbols = (string)$this->captchaData->getConfig('symbols');
$wordLen = $this->getWordLen();
for ($i = 0; $i < $wordLen; $i++) {
$word .= $symbols[array_rand($symbols)];
}
return $word;
}

/**
* Get symbols array to use for word generation
*
* @return array
*/
private function getSymbols()
{
return str_split((string)$this->captchaData->getConfig('symbols'));
return $this->randomMath->getRandomString($wordLen, $symbols);
}

/**
Expand Down Expand Up @@ -562,7 +561,7 @@ protected function randomSize()
*/
protected function gc()
{
//do nothing
return; // required for static testing to pass
}

/**
Expand Down
18 changes: 15 additions & 3 deletions Observer/CaptchaStringResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,31 @@
*/
namespace Magento\Captcha\Observer;

use Magento\Framework\App\RequestInterface;
use Magento\Framework\App\Request\Http as HttpRequest;

/**
* Extract given captcha word.
*/
class CaptchaStringResolver
{
/**
* Get Captcha String
*
* @param \Magento\Framework\App\RequestInterface $request
* @param \Magento\Framework\App\RequestInterface|HttpRequest $request
* @param string $formId
* @return string
*/
public function resolve(\Magento\Framework\App\RequestInterface $request, $formId)
public function resolve(RequestInterface $request, $formId)
{
$captchaParams = $request->getPost(\Magento\Captcha\Helper\Data::INPUT_NAME_FIELD_VALUE);
if (!empty($captchaParams) && !empty($captchaParams[$formId])) {
$value = $captchaParams[$formId];
} else {
//For Web APIs
$value = $request->getHeader('X-Captcha');
}

return $captchaParams[$formId] ?? '';
return $value;
}
}
6 changes: 5 additions & 1 deletion Observer/CheckUserLoginObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Captcha\Observer;

use Magento\Customer\Model\AuthenticationInterface;
Expand All @@ -11,7 +12,10 @@
use Magento\Customer\Api\CustomerRepositoryInterface;

/**
* Check captcha on user login page observer.
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
* @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
*/
class CheckUserLoginObserver implements ObserverInterface
{
Expand Down Expand Up @@ -140,7 +144,7 @@ public function execute(\Magento\Framework\Event\Observer $observer)
$customer = $this->getCustomerRepository()->get($login);
$this->getAuthentication()->processAuthenticationFailure($customer->getId());
} catch (NoSuchEntityException $e) {
//do nothing as customer existance is validated later in authenticate method
//do nothing as customer existence is validated later in authenticate method
}
$this->messageManager->addError(__('Incorrect CAPTCHA'));
$this->_actionFlag->set('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true);
Expand Down
10 changes: 3 additions & 7 deletions Test/Mftf/ActionGroup/AdminLoginWithCaptchaActionGroup.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,10 @@

<actionGroups xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:mftf:Test/etc/actionGroupSchema.xsd">
<actionGroup name="AdminLoginWithCaptchaActionGroup" extends="AdminLoginActionGroup">
<annotations>
<description>EXTENDS: LoginAsAdmin. Fills in the Captcha field on the Backend Admin Login page.</description>
</annotations>
<actionGroup name="AdminLoginWithCaptchaActionGroup" extends="LoginAsAdmin">
<arguments>
<argument name="captcha" type="string"/>
<argument name="captcha" type="string" />
</arguments>

<fillField stepKey="fillCaptchaField" after="fillPassword" userInput="{{captcha}}" selector="{{AdminLoginFormSection.captchaField}}"/>
<fillField stepKey="fillCaptchaField" after="fillPassword" userInput="{{captcha}}" selector="{{AdminLoginFormSection.captchaField}}" />
</actionGroup>
</actionGroups>
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,12 @@
<actionGroups xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:mftf:Test/etc/actionGroupSchema.xsd">
<actionGroup name="AssertCaptchaNotVisibleOnCustomerLoginFormActionGroup">
<annotations>
<description>Validate that the Captcha is NOT present on the Backend Admin Login page.</description>
</annotations>

<waitForPageLoad stepKey="waitForPageLoaded"/>
<waitForPageLoad stepKey="waitForPageLoaded" />
<dontSee selector="{{StorefrontCustomerSignInFormSection.captchaField}}" stepKey="dontSeeCaptchaField"/>
<dontSee selector="{{StorefrontCustomerSignInFormSection.captchaImg}}" stepKey="dontSeeCaptchaImage"/>
<dontSee selector="{{StorefrontCustomerSignInFormSection.captchaReload}}" stepKey="dontSeeCaptchaReloadButton"/>
<reloadPage stepKey="refreshPage"/>
<waitForPageLoad stepKey="waitForPageReloaded"/>
<waitForPageLoad stepKey="waitForPageReloaded" />
<dontSee selector="{{StorefrontCustomerSignInFormSection.captchaField}}" stepKey="dontSeeCaptchaFieldAfterPageReload"/>
<dontSee selector="{{StorefrontCustomerSignInFormSection.captchaImg}}" stepKey="dontSeeCaptchaImageAfterPageReload"/>
<dontSee selector="{{StorefrontCustomerSignInFormSection.captchaReload}}" stepKey="dontSeeCaptchaReloadButtonAfterPageReload"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,11 @@
<actionGroups xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:mftf:Test/etc/actionGroupSchema.xsd">
<actionGroup name="AssertCaptchaVisibleOnAdminLoginFormActionGroup">
<annotations>
<description>Validate that the Captcha IS present on the Backend Admin Login page.</description>
</annotations>

<waitForElementVisible selector="{{AdminLoginFormSection.captchaField}}" stepKey="seeCaptchaField"/>
<waitForElementVisible selector="{{AdminLoginFormSection.captchaImg}}" stepKey="seeCaptchaImage"/>
<waitForElementVisible selector="{{AdminLoginFormSection.captchaReload}}" stepKey="seeCaptchaReloadButton"/>
<reloadPage stepKey="refreshPage"/>
<waitForPageLoad stepKey="waitForPageReloaded"/>
<waitForPageLoad stepKey="waitForPageReloaded" />
<waitForElementVisible selector="{{AdminLoginFormSection.captchaField}}" stepKey="seeCaptchaFieldAfterPageReload"/>
<waitForElementVisible selector="{{AdminLoginFormSection.captchaImg}}" stepKey="seeCaptchaImageAfterPageReload"/>
<waitForElementVisible selector="{{AdminLoginFormSection.captchaReload}}" stepKey="seeCaptchaReloadButtonAfterPageReload"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,11 @@
<actionGroups xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:mftf:Test/etc/actionGroupSchema.xsd">
<actionGroup name="AssertCaptchaVisibleOnContactUsFormActionGroup">
<annotations>
<description>Validate that the Captcha IS present on the Storefront Contact Us page.</description>
</annotations>

<waitForElementVisible selector="{{StorefrontContactUsFormSection.captchaField}}" stepKey="waitToSeeCaptchaField"/>
<waitForElementVisible selector="{{StorefrontContactUsFormSection.captchaImg}}" stepKey="waitToSeeCaptchaImage"/>
<waitForElementVisible selector="{{StorefrontContactUsFormSection.captchaReload}}" stepKey="waitToSeeCaptchaReloadButton"/>
<reloadPage stepKey="refreshPage"/>
<waitForPageLoad stepKey="waitForPageReloaded"/>
<waitForPageLoad stepKey="waitForPageReloaded" />
<waitForElementVisible selector="{{StorefrontContactUsFormSection.captchaField}}" stepKey="waitToSeeCaptchaFieldAfterPageReload"/>
<waitForElementVisible selector="{{StorefrontContactUsFormSection.captchaImg}}" stepKey="waitToSeeCaptchaImageAfterPageReload"/>
<waitForElementVisible selector="{{StorefrontContactUsFormSection.captchaReload}}" stepKey="waitToSeeCaptchaReloadButtonAfterPageReload"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@
<actionGroups xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:mftf:Test/etc/actionGroupSchema.xsd">
<actionGroup name="AssertCaptchaVisibleOnCustomerAccountCreatePageActionGroup">
<annotations>
<description>Validate that the Captcha IS present on the Storefront Create Customer page.</description>
</annotations>

<waitForElementVisible selector="{{StorefrontCustomerCreateFormSection.captchaField}}" stepKey="waitForCaptchaField"/>
<waitForElementVisible selector="{{StorefrontCustomerCreateFormSection.captchaImg}}" stepKey="waitForCaptchaImage"/>
<waitForElementVisible selector="{{StorefrontCustomerCreateFormSection.captchaReload}}" stepKey="waitForCaptchaReloadButton"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,13 @@
<actionGroups xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:mftf:Test/etc/actionGroupSchema.xsd">
<actionGroup name="AssertCaptchaVisibleOnCustomerAccountInfoActionGroup">
<annotations>
<description>Validate that the Captcha IS present on the Storefront Customer Account Information page.</description>
</annotations>

<checkOption selector="{{StorefrontCustomerAccountInformationSection.changeEmail}}" stepKey="clickChangeEmailCheckbox"/>
<checkOption selector="{{StorefrontCustomerAccountInformationSection.changeEmail}}" stepKey="clickChangeEmailCheckbox" />
<waitForElementVisible selector="{{StorefrontCustomerAccountInformationSection.captchaField}}" stepKey="seeCaptchaField"/>
<waitForElementVisible selector="{{StorefrontCustomerAccountInformationSection.captchaImg}}" stepKey="seeCaptchaImage"/>
<waitForElementVisible selector="{{StorefrontCustomerAccountInformationSection.captchaReload}}" stepKey="seeCaptchaReloadButton"/>
<reloadPage stepKey="refreshPage"/>
<waitForPageLoad stepKey="waitForPageReloaded"/>
<checkOption selector="{{StorefrontCustomerAccountInformationSection.changeEmail}}" stepKey="clickChangeEmailCheckboxAfterPageReload"/>
<waitForPageLoad stepKey="waitForPageReloaded" />
<checkOption selector="{{StorefrontCustomerAccountInformationSection.changeEmail}}" stepKey="clickChangeEmailCheckboxAfterPageReload" />
<waitForElementVisible selector="{{StorefrontCustomerAccountInformationSection.captchaField}}" stepKey="seeCaptchaFieldAfterPageReload"/>
<waitForElementVisible selector="{{StorefrontCustomerAccountInformationSection.captchaImg}}" stepKey="seeCaptchaImageAfterPageReload"/>
<waitForElementVisible selector="{{StorefrontCustomerAccountInformationSection.captchaReload}}" stepKey="seeCaptchaReloadButtonAfterPageReload"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,11 @@
<actionGroups xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:mftf:Test/etc/actionGroupSchema.xsd">
<actionGroup name="AssertCaptchaVisibleOnCustomerLoginFormActionGroup">
<annotations>
<description>Validate that the Captcha IS present on the Storefront Customer Login page.</description>
</annotations>

<waitForElementVisible selector="{{StorefrontCustomerSignInFormSection.captchaField}}" stepKey="waitToSeeCaptchaField"/>
<waitForElementVisible selector="{{StorefrontCustomerSignInFormSection.captchaImg}}" stepKey="waitToSeeCaptchaImage"/>
<waitForElementVisible selector="{{StorefrontCustomerSignInFormSection.captchaReload}}" stepKey="waitToSeeCaptchaReloadButton"/>
<reloadPage stepKey="refreshPage"/>
<waitForPageLoad stepKey="waitForPageReloaded"/>
<waitForPageLoad stepKey="waitForPageReloaded" />
<waitForElementVisible selector="{{StorefrontCustomerSignInFormSection.captchaField}}" stepKey="waitToSeeCaptchaFieldAfterPageReload"/>
<waitForElementVisible selector="{{StorefrontCustomerSignInFormSection.captchaImg}}" stepKey="waitToSeeCaptchaImageAfterPageReload"/>
<waitForElementVisible selector="{{StorefrontCustomerSignInFormSection.captchaReload}}" stepKey="waitToSeeCaptchaReloadButtonAfterPageReload"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
-->

<actionGroups xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../../../../../../vendor/magento/magento2-functional-testing-framework/src/Magento/FunctionalTestingFramework/Test/etc/actionGroupSchema.xsd">
xsi:noNamespaceSchemaLocation="urn:magento:mftf:Test/etc/actionGroupSchema.xsd">
<actionGroup name="CaptchaFormsDisplayingActionGroup">
<click selector="{{CaptchaFormsDisplayingSection.store}}" stepKey="ClickToGoStores"/>
<waitForPageLoad stepKey="waitForStoresLoaded"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,10 @@
<actionGroups xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:mftf:Test/etc/actionGroupSchema.xsd">
<actionGroup name="StorefrontCustomerChangeEmailWithCaptchaActionGroup" extends="StorefrontCustomerChangeEmailActionGroup">
<annotations>
<description>EXTENDS: StorefrontCustomerChangeEmailActionGroup. Fills in the Captcha field on the Storefront Customer Information page.</description>
</annotations>
<arguments>
<argument name="captcha" type="string"/>
<argument name="captcha" type="string" />
</arguments>

<fillField selector="{{StorefrontCustomerAccountInformationSection.captchaField}}" userInput="{{captcha}}" stepKey="fillCaptchaField" after="fillCurrentPassword"/>
<fillField selector="{{StorefrontCustomerAccountInformationSection.captchaField}}" userInput="{{captcha}}" stepKey="fillCaptchaField" after="fillCurrentPassword" />
</actionGroup>
</actionGroups>
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,9 @@
<actionGroups xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:mftf:Test/etc/actionGroupSchema.xsd">
<actionGroup name="StorefrontFillContactUsFormWithCaptchaActionGroup" extends="StorefrontFillContactUsFormActionGroup">
<annotations>
<description>EXTENDS: StorefrontFillContactUsFormActionGroup. Fills in the Captcha field on the Storefront Contact Us page.</description>
</annotations>
<arguments>
<argument name="captcha" type="string"/>
<argument name="captcha" type="string" />
</arguments>

<fillField stepKey="fillCaptchaField" after="fillComment" userInput="{{captcha}}" selector="{{StorefrontContactUsFormSection.captchaField}}"/>
<fillField stepKey="fillCaptchaField" after="fillComment" userInput="{{captcha}}" selector="{{StorefrontContactUsFormSection.captchaField}}" />
</actionGroup>
</actionGroups>
Loading

0 comments on commit 4ac46e1

Please sign in to comment.