Add unit test for form renderer
[MAILPOET-2665]
This commit is contained in:
committed by
Jack Kitterhing
parent
2e5fd69658
commit
b38bd63bc5
@ -64,7 +64,7 @@ class Renderer {
|
||||
}
|
||||
|
||||
private function renderHoneypot(): string {
|
||||
return '<label class="mailpoet_hp_email_label">' . __('Please leave this field empty', 'mailpoet') . '<input type="email" name="data[email]"></label>';
|
||||
return '<label class="mailpoet_hp_email_label">' . __('Please leave this field empty', 'mailpoet') . '<input type="email" name="data[email]"/></label>';
|
||||
}
|
||||
|
||||
private function renderReCaptcha(): string {
|
||||
|
12
tests/unit/Form/HtmlParser.php
Normal file
12
tests/unit/Form/HtmlParser.php
Normal file
@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
namespace MailPoet\Test\Form;
|
||||
|
||||
class HtmlParser extends \MailPoetUnitTest {
|
||||
public function findByXpath(string $html, string $xpath): \DOMNodeList {
|
||||
$dom = new \DOMDocument();
|
||||
$dom->loadHTML($html);
|
||||
$value = (new \DOMXPath($dom))->query($xpath);
|
||||
return $value ?: new \DOMNodeList();
|
||||
}
|
||||
}
|
100
tests/unit/Form/RendererTest.php
Normal file
100
tests/unit/Form/RendererTest.php
Normal file
@ -0,0 +1,100 @@
|
||||
<?php
|
||||
|
||||
namespace MailPoet\Test\Form;
|
||||
|
||||
use Codeception\Util\Fixtures;
|
||||
use MailPoet\Form\BlocksRenderer;
|
||||
use MailPoet\Form\Renderer;
|
||||
use MailPoet\Form\Util\Styles;
|
||||
use MailPoet\Settings\SettingsController;
|
||||
use MailPoet\Subscription\Captcha;
|
||||
use PHPUnit\Framework\MockObject\MockObject;
|
||||
|
||||
require_once __DIR__ . '/HtmlParser.php';
|
||||
|
||||
class RendererTest extends \MailPoetUnitTest {
|
||||
/** @var Renderer */
|
||||
private $renderer;
|
||||
|
||||
/** @var MockObject|Styles */
|
||||
private $stylesMock;
|
||||
|
||||
/** @var MockObject|SettingsController */
|
||||
private $settingsMock;
|
||||
|
||||
/** @var MockObject|BlocksRenderer */
|
||||
private $blocksRendererMock;
|
||||
|
||||
/** @var HtmlParser */
|
||||
private $htmlParser;
|
||||
|
||||
public function _before() {
|
||||
parent::_before();
|
||||
$this->stylesMock = $this->createMock(Styles::class);
|
||||
$this->settingsMock = $this->createMock(SettingsController::class);
|
||||
$this->blocksRendererMock = $this->createMock(BlocksRenderer::class);
|
||||
$this->renderer = new Renderer($this->stylesMock, $this->settingsMock, $this->blocksRendererMock);
|
||||
$this->htmlParser = new HtmlParser();
|
||||
}
|
||||
|
||||
public function testItShouldRenderBlocks() {
|
||||
$this->blocksRendererMock
|
||||
->expects($this->exactly(2))
|
||||
->method('renderBlock')
|
||||
->willReturn('<div class="block">Dummy</div>');
|
||||
$this->settingsMock
|
||||
->method('get')
|
||||
->with('captcha.type')
|
||||
->willReturn(Captcha::TYPE_DISABLED);
|
||||
$html = $this->renderer->renderBlocks(Fixtures::get('simple_form_body'));
|
||||
$blocks = $this->htmlParser->findByXpath($html, "//div[@class='block']");
|
||||
expect($blocks->count())->equals(2);
|
||||
}
|
||||
|
||||
public function testItShouldRenderHoneypot() {
|
||||
$this->blocksRendererMock->method('renderBlock')->willReturn('<div>Dummy</div>');
|
||||
$this->settingsMock
|
||||
->method('get')
|
||||
->with('captcha.type')
|
||||
->willReturn(Captcha::TYPE_DISABLED);
|
||||
$html = $this->renderer->renderBlocks(Fixtures::get('simple_form_body'));
|
||||
$hpLabel = $this->htmlParser->findByXpath($html, "//label[@class='mailpoet_hp_email_label']");
|
||||
expect($hpLabel->count())->equals(1);
|
||||
$hpInput = $this->htmlParser->findByXpath($html, "//input[@type='email']");
|
||||
expect($hpInput->count())->equals(1);
|
||||
}
|
||||
|
||||
public function testItShouldRenderReCaptcha() {
|
||||
$token = '123456';
|
||||
$this->blocksRendererMock->method('renderBlock')->willReturn('<div>Dummy</div>');
|
||||
$this->settingsMock
|
||||
->method('get')
|
||||
->will($this->returnValueMap([
|
||||
['captcha.type', null, Captcha::TYPE_RECAPTCHA],
|
||||
['captcha.recaptcha_site_token', null, $token],
|
||||
]));
|
||||
$html = $this->renderer->renderBlocks(Fixtures::get('simple_form_body'));
|
||||
$recaptcha = $this->htmlParser->findByXpath($html, "//div[@class='mailpoet_recaptcha']");
|
||||
expect($recaptcha->count())->equals(1);
|
||||
$recaptchaIframes = $this->htmlParser->findByXpath($html, "//iframe");
|
||||
expect($recaptchaIframes->count())->equals(1);
|
||||
$iframe = $recaptchaIframes->item(0);
|
||||
assert($iframe instanceof \DOMNode);
|
||||
$source = $iframe->attributes->getNamedItem('src');
|
||||
assert($source instanceof \DOMAttr);
|
||||
expect($source->value)->equals("https://www.google.com/recaptcha/api/fallback?k=$token");
|
||||
}
|
||||
|
||||
public function testItShouldNotRenderHoneypotAndRecaptcha() {
|
||||
$this->blocksRendererMock->method('renderBlock')->willReturn('<div>Dummy</div>');
|
||||
$this->settingsMock
|
||||
->method('get')
|
||||
->with('captcha.type')
|
||||
->willReturn(Captcha::TYPE_DISABLED);
|
||||
$html = $this->renderer->renderBlocks(Fixtures::get('simple_form_body'), false);
|
||||
$hpLabel = $this->htmlParser->findByXpath($html, "//label[@class='mailpoet_hp_email_label']");
|
||||
expect($hpLabel->count())->equals(0);
|
||||
$recaptcha = $this->htmlParser->findByXpath($html, "//div[@class='mailpoet_recaptcha']");
|
||||
expect($recaptcha->count())->equals(0);
|
||||
}
|
||||
}
|
@ -48,6 +48,36 @@ Fixtures::add(
|
||||
]
|
||||
);
|
||||
|
||||
Fixtures::add(
|
||||
'simple_form_body',
|
||||
[
|
||||
[
|
||||
'id' => 'email',
|
||||
'name' => 'Email',
|
||||
'position' => '1',
|
||||
'type' => 'text',
|
||||
'unique' => '0',
|
||||
'static' => '1',
|
||||
'params' => [
|
||||
'label' => 'Email',
|
||||
'label_within' => '1',
|
||||
'required' => '1',
|
||||
],
|
||||
],
|
||||
[
|
||||
'id' => 'submit',
|
||||
'name' => 'Submit',
|
||||
'position' => '2',
|
||||
'type' => 'submit',
|
||||
'unique' => '0',
|
||||
'static' => '1',
|
||||
'params' => [
|
||||
'label' => 'Subscribe!',
|
||||
],
|
||||
],
|
||||
]
|
||||
);
|
||||
|
||||
Fixtures::add(
|
||||
'form_body_template',
|
||||
[
|
||||
|
Reference in New Issue
Block a user