Skip to content

Commit

Permalink
Merge remote-tracking branch 'remotes/mainline/2.3-develop' into MAGE…
Browse files Browse the repository at this point in the history
…TWO-92773-Anchor-Category
  • Loading branch information
Alex Paliarush committed Jun 21, 2018
2 parents a60fd3e + be6a01f commit beb6398
Show file tree
Hide file tree
Showing 202 changed files with 2,968 additions and 1,386 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
<?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\Model\DefaultModel as CaptchaModel;
use Magento\Captcha\Observer\CheckRegisterCheckoutObserver;
use Magento\Captcha\Helper\Data as CaptchaDataHelper;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\ActionFlag;
use Magento\Captcha\Observer\CaptchaStringResolver;
use Magento\Checkout\Model\Type\Onepage;
use Magento\Framework\App\Request\Http;
use Magento\Framework\App\Response\Http as HttpResponse;
use Magento\Framework\Event\Observer;
use Magento\Framework\Json\Helper\Data as JsonHelper;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Magento\Quote\Model\Quote;

/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class CheckRegisterCheckoutObserverTest extends \PHPUnit\Framework\TestCase
{
const FORM_ID = 'register_during_checkout';

/**
* @var CheckRegisterCheckoutObserver
*/
private $checkRegisterCheckoutObserver;

/**
* @var ObjectManager
*/
private $objectManager;

/**
* @var Observer
*/
private $observer;

/**
* @var HttpResponse|\PHPUnit_Framework_MockObject_MockObject
*/
private $responseMock;

/**
* @var HttpResponse|\PHPUnit_Framework_MockObject_MockObject
*/
private $requestMock;

/**
* @var ActionFlag|\PHPUnit_Framework_MockObject_MockObject
*/
private $actionFlagMock;

/**
* @var CaptchaStringResolver|\PHPUnit_Framework_MockObject_MockObject
*/
private $captchaStringResolverMock;

/**
* @var JsonHelper|\PHPUnit_Framework_MockObject_MockObject
*/
private $jsonHelperMock;

/**
* @var CaptchaModel|\PHPUnit_Framework_MockObject_MockObject
*/
private $captchaModelMock;

/**
* @var Quote|\PHPUnit_Framework_MockObject_MockObject
*/
private $quoteModelMock;

/**
* @var Action|\PHPUnit_Framework_MockObject_MockObject
*/
private $controllerMock;

protected function setUp()
{
$onepageModelTypeMock = $this->createMock(Onepage::class);
$captchaHelperMock = $this->createMock(CaptchaDataHelper::class);
$this->objectManager = new ObjectManager($this);
$this->actionFlagMock = $this->createMock(ActionFlag::class);
$this->captchaStringResolverMock = $this->createMock(CaptchaStringResolver::class);
$this->captchaModelMock = $this->createMock(CaptchaModel::class);
$this->quoteModelMock = $this->createMock(Quote::class);
$this->controllerMock = $this->createMock(Action::class);
$this->requestMock = $this->createMock(Http::class);
$this->responseMock = $this->createMock(HttpResponse::class);
$this->observer = new Observer(['controller_action' => $this->controllerMock]);
$this->jsonHelperMock = $this->createMock(JsonHelper::class);

$this->checkRegisterCheckoutObserver = $this->objectManager->getObject(
CheckRegisterCheckoutObserver::class,
[
'helper' => $captchaHelperMock,
'actionFlag' => $this->actionFlagMock,
'captchaStringResolver' => $this->captchaStringResolverMock,
'typeOnepage' => $onepageModelTypeMock,
'jsonHelper' => $this->jsonHelperMock
]
);

$captchaHelperMock->expects($this->once())
->method('getCaptcha')
->with(self::FORM_ID)
->willReturn($this->captchaModelMock);
$onepageModelTypeMock->expects($this->once())
->method('getQuote')
->willReturn($this->quoteModelMock);
}

public function testCheckRegisterCheckoutForGuest()
{
$this->quoteModelMock->expects($this->once())
->method('getCheckoutMethod')
->willReturn(Onepage::METHOD_GUEST);
$this->captchaModelMock->expects($this->never())
->method('isRequired');

$this->checkRegisterCheckoutObserver->execute($this->observer);
}

public function testCheckRegisterCheckoutWithNoCaptchaRequired()
{
$this->quoteModelMock->expects($this->once())
->method('getCheckoutMethod')
->willReturn(Onepage::METHOD_REGISTER);
$this->captchaModelMock->expects($this->once())
->method('isRequired')
->willReturn(false);
$this->captchaModelMock->expects($this->never())
->method('isCorrect');

$this->checkRegisterCheckoutObserver->execute($this->observer);
}

public function testCheckRegisterCheckoutWithIncorrectCaptcha()
{
$captchaValue = 'some_word';
$encodedJsonValue = '{}';

$this->quoteModelMock->expects($this->once())
->method('getCheckoutMethod')
->willReturn(Onepage::METHOD_REGISTER);
$this->captchaModelMock->expects($this->once())
->method('isRequired')
->willReturn(true);
$this->controllerMock->expects($this->once())
->method('getRequest')
->willReturn($this->requestMock);
$this->controllerMock->expects($this->once())
->method('getResponse')
->willReturn($this->responseMock);
$this->controllerMock->expects($this->once())
->method('getResponse')
->willReturn($this->responseMock);
$this->captchaStringResolverMock->expects($this->once())
->method('resolve')
->with($this->requestMock, self::FORM_ID)
->willReturn($captchaValue);
$this->captchaModelMock->expects($this->once())
->method('isCorrect')
->with($captchaValue)
->willReturn(false);
$this->actionFlagMock->expects($this->once())
->method('set')
->with('', Action::FLAG_NO_DISPATCH, true);
$this->jsonHelperMock->expects($this->once())
->method('jsonEncode')
->willReturn($encodedJsonValue);
$this->responseMock->expects($this->once())
->method('representJson')
->with($encodedJsonValue);

$this->checkRegisterCheckoutObserver->execute($this->observer);
}

public function testCheckRegisterCheckoutWithCorrectCaptcha()
{
$this->quoteModelMock->expects($this->once())
->method('getCheckoutMethod')
->willReturn(Onepage::METHOD_REGISTER);
$this->captchaModelMock->expects($this->once())
->method('isRequired')
->willReturn(true);
$this->controllerMock->expects($this->once())
->method('getRequest')
->willReturn($this->requestMock);
$this->captchaStringResolverMock->expects($this->once())
->method('resolve')
->with($this->requestMock, self::FORM_ID)
->willReturn('some_word');
$this->captchaModelMock->expects($this->once())
->method('isCorrect')
->with('some_word')
->willReturn(true);
$this->actionFlagMock->expects($this->never())
->method('set');

$this->checkRegisterCheckoutObserver->execute($this->observer);
}
}
25 changes: 22 additions & 3 deletions app/code/Magento/Catalog/Controller/Adminhtml/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Catalog\Controller\Adminhtml;

use Magento\Store\Model\Store;

/**
* Catalog category controller
*/
Expand Down Expand Up @@ -44,7 +48,7 @@ public function __construct(
protected function _initCategory($getRootInstead = false)
{
$categoryId = $this->resolveCategoryId();
$storeId = (int)$this->getRequest()->getParam('store');
$storeId = $this->resolveStoreId();
$category = $this->_objectManager->create(\Magento\Catalog\Model\Category::class);
$category->setStoreId($storeId);

Expand All @@ -70,7 +74,7 @@ protected function _initCategory($getRootInstead = false)
$this->_objectManager->get(\Magento\Framework\Registry::class)->register('category', $category);
$this->_objectManager->get(\Magento\Framework\Registry::class)->register('current_category', $category);
$this->_objectManager->get(\Magento\Cms\Model\Wysiwyg\Config::class)
->setStoreId($this->getRequest()->getParam('store'));
->setStoreId($storeId);
return $category;
}

Expand All @@ -79,13 +83,28 @@ protected function _initCategory($getRootInstead = false)
*
* @return int
*/
private function resolveCategoryId()
private function resolveCategoryId() : int
{
$categoryId = (int)$this->getRequest()->getParam('id', false);

return $categoryId ?: (int)$this->getRequest()->getParam('entity_id', false);
}

/**
* Resolve store id
*
* Tries to take store id from store HTTP parameter
* @see Store
*
* @return int
*/
private function resolveStoreId() : int
{
$storeId = (int)$this->getRequest()->getParam('store', false);

return $storeId ?: (int)$this->getRequest()->getParam('store_id', Store::DEFAULT_STORE_ID);
}

/**
* Build response for ajax request
*
Expand Down
Loading

0 comments on commit beb6398

Please sign in to comment.