forked from magento/magento2
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from magento/2.2-develop
2.2 develop-update
- Loading branch information
Showing
16 changed files
with
840 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
137 changes: 137 additions & 0 deletions
137
app/code/Magento/Captcha/Test/Unit/Observer/CheckUserLoginBackendObserverTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\Captcha\Test\Unit\Observer; | ||
|
||
use Magento\Captcha\Helper\Data; | ||
use Magento\Captcha\Model\DefaultModel; | ||
use Magento\Captcha\Observer\CaptchaStringResolver; | ||
use Magento\Captcha\Observer\CheckUserLoginBackendObserver; | ||
use Magento\Framework\App\RequestInterface; | ||
use Magento\Framework\Event; | ||
use Magento\Framework\Event\Observer; | ||
use Magento\Framework\Message\ManagerInterface; | ||
use PHPUnit\Framework\TestCase; | ||
use PHPUnit_Framework_MockObject_MockObject as MockObject; | ||
|
||
/** | ||
* Class CheckUserLoginBackendObserverTest | ||
*/ | ||
class CheckUserLoginBackendObserverTest extends TestCase | ||
{ | ||
/** | ||
* @var CheckUserLoginBackendObserver | ||
*/ | ||
private $observer; | ||
|
||
/** | ||
* @var ManagerInterface|MockObject | ||
*/ | ||
private $messageManagerMock; | ||
|
||
/** | ||
* @var CaptchaStringResolver|MockObject | ||
*/ | ||
private $captchaStringResolverMock; | ||
|
||
/** | ||
* @var RequestInterface|MockObject | ||
*/ | ||
private $requestMock; | ||
|
||
/** | ||
* @var Data|MockObject | ||
*/ | ||
private $helperMock; | ||
|
||
/** | ||
* Set Up | ||
* | ||
* @return void | ||
*/ | ||
protected function setUp() | ||
{ | ||
$this->helperMock = $this->createMock(Data::class); | ||
$this->messageManagerMock = $this->createMock(ManagerInterface::class); | ||
$this->captchaStringResolverMock = $this->createMock(CaptchaStringResolver::class); | ||
$this->requestMock = $this->createMock(RequestInterface::class); | ||
|
||
$this->observer = new CheckUserLoginBackendObserver( | ||
$this->helperMock, | ||
$this->captchaStringResolverMock, | ||
$this->requestMock | ||
); | ||
} | ||
|
||
/** | ||
* Test check user login in backend with correct captcha | ||
* | ||
* @dataProvider requiredCaptchaDataProvider | ||
* @param bool $isRequired | ||
* @return void | ||
*/ | ||
public function testCheckOnBackendLoginWithCorrectCaptcha(bool $isRequired) | ||
{ | ||
$formId = 'backend_login'; | ||
$login = 'admin'; | ||
$captchaValue = 'captcha-value'; | ||
|
||
/** @var Observer|MockObject $observerMock */ | ||
$observerMock = $this->createPartialMock(Observer::class, ['getEvent']); | ||
$eventMock = $this->createPartialMock(Event::class, ['getUsername']); | ||
$captcha = $this->createMock(DefaultModel::class); | ||
|
||
$eventMock->method('getUsername')->willReturn('admin'); | ||
$observerMock->method('getEvent')->willReturn($eventMock); | ||
$captcha->method('isRequired')->with($login)->willReturn($isRequired); | ||
$captcha->method('isCorrect')->with($captchaValue)->willReturn(true); | ||
$this->helperMock->method('getCaptcha')->with($formId)->willReturn($captcha); | ||
$this->captchaStringResolverMock->method('resolve')->with($this->requestMock, $formId) | ||
->willReturn($captchaValue); | ||
|
||
$this->observer->execute($observerMock); | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function requiredCaptchaDataProvider(): array | ||
{ | ||
return [ | ||
[true], | ||
[false] | ||
]; | ||
} | ||
|
||
/** | ||
* Test check user login in backend with wrong captcha | ||
* | ||
* @return void | ||
* @expectedException \Magento\Framework\Exception\Plugin\AuthenticationException | ||
*/ | ||
public function testCheckOnBackendLoginWithWrongCaptcha() | ||
{ | ||
$formId = 'backend_login'; | ||
$login = 'admin'; | ||
$captchaValue = 'captcha-value'; | ||
|
||
/** @var Observer|MockObject $observerMock */ | ||
$observerMock = $this->createPartialMock(Observer::class, ['getEvent']); | ||
$eventMock = $this->createPartialMock(Event::class, ['getUsername']); | ||
$captcha = $this->createMock(DefaultModel::class); | ||
|
||
$eventMock->method('getUsername')->willReturn($login); | ||
$observerMock->method('getEvent')->willReturn($eventMock); | ||
$captcha->method('isRequired')->with($login)->willReturn(true); | ||
$captcha->method('isCorrect')->with($captchaValue)->willReturn(false); | ||
$this->helperMock->method('getCaptcha')->with($formId)->willReturn($captcha); | ||
$this->captchaStringResolverMock->method('resolve')->with($this->requestMock, $formId) | ||
->willReturn($captchaValue); | ||
|
||
$this->observer->execute($observerMock); | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
app/code/Magento/ConfigurableProduct/Plugin/SalesRule/Model/Rule/Condition/Product.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\ConfigurableProduct\Plugin\SalesRule\Model\Rule\Condition; | ||
|
||
use Magento\ConfigurableProduct\Model\Product\Type\Configurable; | ||
|
||
/** | ||
* Class Product | ||
* | ||
* @package Magento\ConfigurableProduct\Plugin\SalesRule\Model\Rule\Condition | ||
*/ | ||
class Product | ||
{ | ||
/** | ||
* @param \Magento\SalesRule\Model\Rule\Condition\Product $subject | ||
* @param \Magento\Framework\Model\AbstractModel $model | ||
*/ | ||
public function beforeValidate( | ||
\Magento\SalesRule\Model\Rule\Condition\Product $subject, | ||
\Magento\Framework\Model\AbstractModel $model | ||
) { | ||
$product = $this->getProductToValidate($subject, $model); | ||
if ($model->getProduct() !== $product) { | ||
// We need to replace product only for validation and keep original product for all other cases. | ||
$clone = clone $model; | ||
$clone->setProduct($product); | ||
$model = $clone; | ||
} | ||
|
||
return [$model]; | ||
} | ||
|
||
/** | ||
* @param \Magento\SalesRule\Model\Rule\Condition\Product $subject | ||
* @param \Magento\Framework\Model\AbstractModel $model | ||
* | ||
* @return \Magento\Catalog\Api\Data\ProductInterface|\Magento\Catalog\Model\Product | ||
*/ | ||
private function getProductToValidate( | ||
\Magento\SalesRule\Model\Rule\Condition\Product $subject, | ||
\Magento\Framework\Model\AbstractModel $model | ||
) { | ||
/** @var \Magento\Catalog\Model\Product $product */ | ||
$product = $model->getProduct(); | ||
|
||
$attrCode = $subject->getAttribute(); | ||
|
||
/* Check for attributes which are not available for configurable products */ | ||
if ($product->getTypeId() == Configurable::TYPE_CODE && !$product->hasData($attrCode)) { | ||
/** @var \Magento\Catalog\Model\AbstractModel $childProduct */ | ||
$childProduct = current($model->getChildren())->getProduct(); | ||
if ($childProduct->hasData($attrCode)) { | ||
$product = $childProduct; | ||
} | ||
} | ||
|
||
return $product; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.