Skip to content

Commit

Permalink
Merge pull request #13 from mautic-inc/fix-errors-if-plugin-not-insta…
Browse files Browse the repository at this point in the history
…lled-or-configured

Fix Mautic forms when the plugin is not installed or not configured
  • Loading branch information
KonstantinCodes authored Oct 2, 2018
2 parents ac6855b + df572d4 commit 6736156
Show file tree
Hide file tree
Showing 4 changed files with 223 additions and 87 deletions.
38 changes: 26 additions & 12 deletions EventListener/FormSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,8 @@
use MauticPlugin\MauticRecaptchaBundle\RecaptchaEvents;
use MauticPlugin\MauticRecaptchaBundle\Service\RecaptchaClient;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Mautic\PluginBundle\Integration\AbstractIntegration;

/**
* Class FormSubscriber.
*/
class FormSubscriber extends CommonSubscriber
{
const MODEL_NAME_KEY_LEAD = 'lead.lead';
Expand Down Expand Up @@ -55,8 +53,11 @@ class FormSubscriber extends CommonSubscriber
protected $secretKey;

/**
* FormSubscriber constructor.
*
* @var boolean
*/
private $recaptchaIsConfigured = false;

/**
* @param EventDispatcherInterface $eventDispatcher
* @param IntegrationHelper $integrationHelper
* @param ModelFactory $modelFactory
Expand All @@ -67,16 +68,21 @@ public function __construct(
IntegrationHelper $integrationHelper,
ModelFactory $modelFactory,
RecaptchaClient $recaptchaClient
){
) {
$this->eventDispatcher = $eventDispatcher;
$this->modelFactory = $modelFactory;
$this->recaptchaClient = $recaptchaClient;

$integrationObject = $integrationHelper->getIntegrationObject(RecaptchaIntegration::INTEGRATION_NAME);
$keys = $integrationObject->getKeys();

$this->siteKey = $keys['site_key'];
$this->secretKey = $keys['secret_key'];
$integrationObject = $integrationHelper->getIntegrationObject(RecaptchaIntegration::INTEGRATION_NAME);

if ($integrationObject instanceof AbstractIntegration) {
$keys = $integrationObject->getKeys();
$this->siteKey = isset($keys['site_key']) ? $keys['site_key'] : null;
$this->secretKey = isset($keys['secret_key']) ? $keys['secret_key'] : null;

if ($this->siteKey && $this->secretKey) {
$this->recaptchaIsConfigured = true;
}
}
}

/**
Expand All @@ -95,6 +101,10 @@ public static function getSubscribedEvents()
*/
public function onFormBuild(FormBuilderEvent $event)
{
if (!$this->recaptchaIsConfigured) {
return;
}

$event->addFormField('plugin.recaptcha', [
'label' => 'mautic.plugin.actions.recaptcha',
'formType' => 'recaptcha',
Expand All @@ -119,6 +129,10 @@ public function onFormBuild(FormBuilderEvent $event)
*/
public function onFormValidate(ValidationEvent $event)
{
if (!$this->recaptchaIsConfigured) {
return;
}

if ($this->recaptchaClient->verify($event->getValue())) {
return;
}
Expand Down
9 changes: 6 additions & 3 deletions Service/RecaptchaClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Mautic\CoreBundle\EventListener\CommonSubscriber;
use Mautic\PluginBundle\Helper\IntegrationHelper;
use MauticPlugin\MauticRecaptchaBundle\Integration\RecaptchaIntegration;
use Mautic\PluginBundle\Integration\AbstractIntegration;

class RecaptchaClient extends CommonSubscriber
{
Expand All @@ -36,9 +37,11 @@ public function __construct(IntegrationHelper $integrationHelper)
{
$integrationObject = $integrationHelper->getIntegrationObject(RecaptchaIntegration::INTEGRATION_NAME);

$keys = $integrationObject->getKeys();
$this->siteKey = $keys['site_key'];
$this->secretKey = $keys['secret_key'];
if ($integrationObject instanceof AbstractIntegration) {
$keys = $integrationObject->getKeys();
$this->siteKey = isset($keys['site_key']) ? $keys['site_key'] : null;
$this->secretKey = isset($keys['secret_key']) ? $keys['secret_key'] : null;
}
}

/**
Expand Down
193 changes: 121 additions & 72 deletions Tests/FormSubscriberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,122 +16,171 @@
use MauticPlugin\MauticRecaptchaBundle\Service\RecaptchaClient;
use PHPUnit_Framework_MockObject_MockBuilder;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Mautic\FormBundle\Event\FormBuilderEvent;

class FormSubscriberTest extends \PHPUnit_Framework_TestCase
{
/**
* @var RecaptchaIntegration
* @var PHPUnit_Framework_MockObject_MockBuilder|RecaptchaIntegration
*/
protected $integration;
private $integration;

/**
* @var IntegrationHelper
* @var PHPUnit_Framework_MockObject_MockBuilder|EventDispatcherInterface
*/
protected $integrationHelper;
private $eventDispatcher;

/**
* @var ModelFactory
* @var PHPUnit_Framework_MockObject_MockBuilder|IntegrationHelper
*/
protected $modelFactory;
private $integrationHelper;

/**
* @var EventDispatcherInterface
* @var PHPUnit_Framework_MockObject_MockBuilder|ModelFactory
*/
protected $eventDispatcher;
private $modelFactory;

/**
* @var PHPUnit_Framework_MockObject_MockBuilder|RecaptchaClient
*/
private $recaptchaClient;

/**
* @var PHPUnit_Framework_MockObject_MockBuilder|ValidationEvent
*/
private $validationEvent;

/**
* @var PHPUnit_Framework_MockObject_MockBuilder|FormBuilderEvent
*/
private $formBuildEvent;

protected function setUp()
{
parent::setUp();

$this->integration = $this->getMockBuilder(RecaptchaIntegration::class)
->disableOriginalConstructor()
->getMock();
$this->integration = $this->createMock(RecaptchaIntegration::class);
$this->eventDispatcher = $this->createMock(EventDispatcherInterface::class);
$this->integrationHelper = $this->createMock(IntegrationHelper::class);
$this->modelFactory = $this->createMock(ModelFactory::class);
$this->recaptchaClient = $this->createMock(RecaptchaClient::class);
$this->validationEvent = $this->createMock(ValidationEvent::class);
$this->formBuildEvent = $this->createMock(FormBuilderEvent::class);

$this->eventDispatcher
->method('addListener')
->willReturn(true);

$this->integration
->method('getKeys')
->willReturn(['site_key' => 'test', 'secret_key' => 'test']);
}

$this->eventDispatcher = $this->getMockBuilder(EventDispatcherInterface::class)
->disableOriginalConstructor()
->getMock();
$this->eventDispatcher
->method('addListener')
public function testOnFormValidateSuccessful()
{
$this->recaptchaClient->expects($this->once())
->method('verify')
->willReturn(true);

$this->integrationHelper = $this->getMockBuilder(IntegrationHelper::class)
->disableOriginalConstructor()
->getMock();

$this->integrationHelper
$this->integrationHelper->expects($this->once())
->method('getIntegrationObject')
->willReturn($this->integration);

$this->modelFactory = $this->getMockBuilder(ModelFactory::class)
->disableOriginalConstructor()
->getMock();
$this->createFormSubscriber()->onFormValidate($this->validationEvent);
}

public function testOnFormValidateSuccessful()
public function testOnFormValidateFailure()
{
/** @var RecaptchaClient $recaptchaClient */
$recaptchaClient = $this->getMockBuilder(RecaptchaClient::class)
->disableOriginalConstructor()
->getMock();
$recaptchaClient
$this->recaptchaClient->expects($this->once())
->method('verify')
->willReturn(true);

$formSubscriber = new FormSubscriber(
$this->eventDispatcher,
$this->integrationHelper,
$this->modelFactory,
$recaptchaClient
);

/** @var PHPUnit_Framework_MockObject_MockBuilder|ValidationEvent $validationEvent */
$validationEvent = $this->getMockBuilder(ValidationEvent::class)
->disableOriginalConstructor()
->getMock();
->willReturn(false);

$validationEvent
$this->validationEvent->expects($this->once())
->method('getValue')
->willReturn('any-value-should-work');
$validationEvent
->expects($this->never())
->method('failedValidation');

$formSubscriber->onFormValidate($validationEvent);
$this->integrationHelper->expects($this->once())
->method('getIntegrationObject')
->willReturn($this->integration);

$this->createFormSubscriber()->onFormValidate($this->validationEvent);
}

public function testOnFormValidateFailure()
public function testOnFormValidateWhenPluginIsNotInstalled()
{
/** @var RecaptchaClient $recaptchaClient */
$recaptchaClient = $this->getMockBuilder(RecaptchaClient::class)
->disableOriginalConstructor()
->getMock();
$recaptchaClient
->method('verify')
->willReturn(false);
$this->recaptchaClient->expects($this->never())
->method('verify');

$this->integrationHelper->expects($this->once())
->method('getIntegrationObject')
->willReturn(null);

$this->createFormSubscriber()->onFormValidate($this->validationEvent);
}

public function testOnFormValidateWhenPluginIsNotConfigured()
{
$this->recaptchaClient->expects($this->never())
->method('verify');

$this->integrationHelper->expects($this->once())
->method('getIntegrationObject')
->willReturn(['site_key' => '']);

$this->createFormSubscriber()->onFormValidate($this->validationEvent);
}

public function testOnFormBuildWhenPluginIsInstalledAndConfigured()
{
$this->formBuildEvent->expects($this->once())
->method('addFormField')
->with('plugin.recaptcha');

$this->formBuildEvent->expects($this->once())
->method('addValidator')
->with('plugin.recaptcha.validator');

$this->integrationHelper->expects($this->once())
->method('getIntegrationObject')
->willReturn($this->integration);

$this->createFormSubscriber()->onFormBuild($this->formBuildEvent);
}

public function testOnFormBuildWhenPluginIsNotInstalled()
{
$this->formBuildEvent->expects($this->never())
->method('addFormField');

$formSubscriber = new FormSubscriber(
$this->integrationHelper->expects($this->once())
->method('getIntegrationObject')
->willReturn(null);

$this->createFormSubscriber()->onFormBuild($this->formBuildEvent);
}

public function testOnFormBuildWhenPluginIsNotConfigured()
{
$this->formBuildEvent->expects($this->never())
->method('addFormField');

$this->integrationHelper->expects($this->once())
->method('getIntegrationObject')
->willReturn(['site_key' => '']);

$this->createFormSubscriber()->onFormBuild($this->formBuildEvent);
}

/**
* @return FormSubscriber
*/
private function createFormSubscriber()
{
return new FormSubscriber(
$this->eventDispatcher,
$this->integrationHelper,
$this->modelFactory,
$recaptchaClient
$this->recaptchaClient
);

/** @var PHPUnit_Framework_MockObject_MockBuilder|ValidationEvent $validationEvent */
$validationEvent = $this->getMockBuilder(ValidationEvent::class)
->disableOriginalConstructor()
->getMock();

$validationEvent
->method('getValue')
->willReturn('any-value-should-work');
$validationEvent
->expects($this->once())
->method('failedValidation');

$formSubscriber->onFormValidate($validationEvent);
}
}
Loading

0 comments on commit 6736156

Please sign in to comment.