Inject form renderer into DisplayFormInWPContent
[MAILPOET-2665]
This commit is contained in:
committed by
Jack Kitterhing
parent
b38bd63bc5
commit
20926d56b7
@@ -15,9 +15,13 @@ class DisplayFormInWPContent {
|
|||||||
/** @var FormsRepository */
|
/** @var FormsRepository */
|
||||||
private $formsRepository;
|
private $formsRepository;
|
||||||
|
|
||||||
public function __construct(WPFunctions $wp, FormsRepository $formsRepository) {
|
/** @var Renderer */
|
||||||
|
private $formRenderer;
|
||||||
|
|
||||||
|
public function __construct(WPFunctions $wp, FormsRepository $formsRepository, Renderer $formRenderer) {
|
||||||
$this->wp = $wp;
|
$this->wp = $wp;
|
||||||
$this->formsRepository = $formsRepository;
|
$this->formsRepository = $formsRepository;
|
||||||
|
$this->formRenderer = $formRenderer;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function display(string $content): string {
|
public function display(string $content): string {
|
||||||
@@ -63,7 +67,7 @@ class DisplayFormInWPContent {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private function getContentBellow(FormEntity $form): string {
|
private function getContentBellow(FormEntity $form): string {
|
||||||
return Renderer::render([
|
return $this->formRenderer->render([
|
||||||
'body' => $form->getBody(),
|
'body' => $form->getBody(),
|
||||||
'styles' => $form->getStyles(),
|
'styles' => $form->getStyles(),
|
||||||
]);
|
]);
|
||||||
|
@@ -26,6 +26,12 @@ class Renderer {
|
|||||||
$this->blocksRenderer = $blocksRenderer;
|
$this->blocksRenderer = $blocksRenderer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function render(array $form = []): string {
|
||||||
|
$html = $this->renderStyles($form);
|
||||||
|
$html .= $this->renderHTML($form);
|
||||||
|
return $html;
|
||||||
|
}
|
||||||
|
|
||||||
public function renderStyles(array $form = [], string $prefix = null): string {
|
public function renderStyles(array $form = [], string $prefix = null): string {
|
||||||
$html = '<style type="text/css">';
|
$html = '<style type="text/css">';
|
||||||
$html .= '.mailpoet_hp_email_label{display:none;}'; // move honeypot field out of sight
|
$html .= '.mailpoet_hp_email_label{display:none;}'; // move honeypot field out of sight
|
||||||
|
@@ -5,15 +5,19 @@ namespace MailPoet\Form;
|
|||||||
use MailPoet\Entities\FormEntity;
|
use MailPoet\Entities\FormEntity;
|
||||||
use MailPoet\Settings\SettingsController;
|
use MailPoet\Settings\SettingsController;
|
||||||
use MailPoet\WP\Functions as WPFunctions;
|
use MailPoet\WP\Functions as WPFunctions;
|
||||||
|
use PHPUnit\Framework\MockObject\MockObject;
|
||||||
|
|
||||||
class DisplayFormInWPContentTest extends \MailPoetUnitTest {
|
class DisplayFormInWPContentTest extends \MailPoetUnitTest {
|
||||||
|
|
||||||
/** @var FormsRepository|\PHPUnit_Framework_MockObject_MockObject */
|
/** @var FormsRepository|MockObject */
|
||||||
private $repository;
|
private $repository;
|
||||||
|
|
||||||
/** @var WPFunctions|\PHPUnit_Framework_MockObject_MockObject */
|
/** @var WPFunctions|MockObject */
|
||||||
private $wp;
|
private $wp;
|
||||||
|
|
||||||
|
/** @var Renderer|MockObject */
|
||||||
|
private $renderer;
|
||||||
|
|
||||||
/** @var DisplayFormInWPContent */
|
/** @var DisplayFormInWPContent */
|
||||||
private $hook;
|
private $hook;
|
||||||
|
|
||||||
@@ -25,12 +29,15 @@ class DisplayFormInWPContentTest extends \MailPoetUnitTest {
|
|||||||
|
|
||||||
$this->repository = $this->createMock(FormsRepository::class);
|
$this->repository = $this->createMock(FormsRepository::class);
|
||||||
$this->wp = $this->createMock(WPFunctions::class);
|
$this->wp = $this->createMock(WPFunctions::class);
|
||||||
$this->hook = new DisplayFormInWPContent($this->wp, $this->repository);
|
$this->renderer = $this->createMock(Renderer::class);
|
||||||
|
$this->hook = new DisplayFormInWPContent($this->wp, $this->repository, $this->renderer);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testAppendsRenderedFormAfterPostContent() {
|
public function testAppendsRenderedFormAfterPostContent() {
|
||||||
|
$renderedForm = '<div class="form"></div>';
|
||||||
$this->wp->expects($this->once())->method('isSingle')->willReturn(true);
|
$this->wp->expects($this->once())->method('isSingle')->willReturn(true);
|
||||||
$this->wp->expects($this->any())->method('isSingular')->willReturn(true);
|
$this->wp->expects($this->any())->method('isSingular')->willReturn(true);
|
||||||
|
$this->renderer->expects($this->once())->method('render')->willReturn($renderedForm);
|
||||||
$form = new FormEntity('My Form');
|
$form = new FormEntity('My Form');
|
||||||
$form->setSettings([
|
$form->setSettings([
|
||||||
'segments' => ['3'],
|
'segments' => ['3'],
|
||||||
@@ -47,7 +54,7 @@ class DisplayFormInWPContentTest extends \MailPoetUnitTest {
|
|||||||
|
|
||||||
$result = $this->hook->display('content');
|
$result = $this->hook->display('content');
|
||||||
expect($result)->notEquals('content');
|
expect($result)->notEquals('content');
|
||||||
expect($result)->regExp('/content.*input type="submit"/is');
|
expect($result)->endsWith($renderedForm);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testDoesNotAppendFormIfDisabled() {
|
public function testDoesNotAppendFormIfDisabled() {
|
||||||
@@ -99,11 +106,15 @@ class DisplayFormInWPContentTest extends \MailPoetUnitTest {
|
|||||||
'id' => 'submit',
|
'id' => 'submit',
|
||||||
'name' => 'Submit',
|
'name' => 'Submit',
|
||||||
]]);
|
]]);
|
||||||
|
$renderedForm1 = '<div class="form1"></div>';
|
||||||
|
$renderedForm2 = '<div class="form2"></div>';
|
||||||
$this->repository->expects($this->once())->method('findAll')->willReturn([$form1, $form2]);
|
$this->repository->expects($this->once())->method('findAll')->willReturn([$form1, $form2]);
|
||||||
|
$this->renderer->expects($this->exactly(2))->method('render')->willReturnOnConsecutiveCalls($renderedForm1, $renderedForm2);
|
||||||
|
|
||||||
$result = $this->hook->display('content');
|
$result = $this->hook->display('content');
|
||||||
expect($result)->notEquals('content');
|
expect($result)->notEquals('content');
|
||||||
expect($result)->regExp('/content.*input.*value="Subscribe1".*input.*value="Subscribe2"/is');
|
expect($result)->contains($renderedForm1);
|
||||||
|
expect($result)->endsWith($renderedForm2);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testDoesNotAppendFormIfNotOnSinglePage() {
|
public function testDoesNotAppendFormIfNotOnSinglePage() {
|
||||||
@@ -137,6 +148,8 @@ class DisplayFormInWPContentTest extends \MailPoetUnitTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function testAppendsRenderedFormAfterPageContent() {
|
public function testAppendsRenderedFormAfterPageContent() {
|
||||||
|
$renderedForm = '<div class="form"></div>';
|
||||||
|
$this->renderer->expects($this->once())->method('render')->willReturn($renderedForm);
|
||||||
$this->wp->expects($this->once())->method('isSingle')->willReturn(true);
|
$this->wp->expects($this->once())->method('isSingle')->willReturn(true);
|
||||||
$this->wp->expects($this->any())->method('isPage')->willReturn(true);
|
$this->wp->expects($this->any())->method('isPage')->willReturn(true);
|
||||||
$this->wp
|
$this->wp
|
||||||
@@ -158,7 +171,7 @@ class DisplayFormInWPContentTest extends \MailPoetUnitTest {
|
|||||||
|
|
||||||
$result = $this->hook->display('content');
|
$result = $this->hook->display('content');
|
||||||
expect($result)->notEquals('content');
|
expect($result)->notEquals('content');
|
||||||
expect($result)->regExp('/content.*input type="submit"/is');
|
expect($result)->endsWith($renderedForm);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testSetsTransientToImprovePerformance() {
|
public function testSetsTransientToImprovePerformance() {
|
||||||
|
Reference in New Issue
Block a user