Skip to content

[Backport 2.1] Added unit test for captcha string resolver #16067

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Captcha\Test\Unit\Observer;

use Magento\Captcha\Helper\Data as CaptchaDataHelper;
use Magento\Captcha\Observer\CaptchaStringResolver;
use Magento\Framework\App\Request\Http as HttpRequest;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;

class CaptchaStringResolverTest extends \PHPUnit_Framework_TestCase
{
/**
* @var ObjectManager
*/
private $objectManagerHelper;

/**
* @var CaptchaStringResolver
*/
private $captchaStringResolver;

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

protected function setUp()
{
$this->objectManagerHelper = new ObjectManager($this);
$this->requestMock = $this->getMock(HttpRequest::class, [], [], '', false);
$this->captchaStringResolver = $this->objectManagerHelper->getObject(CaptchaStringResolver::class);
}

public function testResolveWithFormIdSet()
{
$formId = 'contact_us';
$captchaValue = 'some-value';

$this->requestMock->expects($this->once())
->method('getPost')
->with(CaptchaDataHelper::INPUT_NAME_FIELD_VALUE)
->willReturn([$formId => $captchaValue]);

self::assertEquals(
$this->captchaStringResolver->resolve($this->requestMock, $formId),
$captchaValue
);
}

public function testResolveWithNoFormIdInRequest()
{
$formId = 'contact_us';

$this->requestMock->expects($this->once())
->method('getPost')
->with(CaptchaDataHelper::INPUT_NAME_FIELD_VALUE)
->willReturn([]);

self::assertEquals(
$this->captchaStringResolver->resolve($this->requestMock, $formId),
''
);
}
}