diff --git a/lib/Form/Renderer.php b/lib/Form/Renderer.php index 310ac9b2ac..d07aedf33f 100644 --- a/lib/Form/Renderer.php +++ b/lib/Form/Renderer.php @@ -37,7 +37,10 @@ class Renderer { public function renderHTML(array $form = []): string { if (isset($form['body']) && !empty($form['body'])) { - return $this->renderBlocks($form['body']); + return $this->wrapForm( + $this->renderBlocks($form['body']), + $form['settings'] + ); } return ''; } @@ -88,4 +91,23 @@ class Renderer { '; } + + private function wrapForm(string $formBody, array $formSettings): string { + $styles = []; + + if (isset($formSettings['backgroundColor'])) { + $styles[] = 'background-color: ' . trim($formSettings['backgroundColor']); + } + + if (isset($formSettings['fontColor'])) { + $styles[] = 'color: ' . trim($formSettings['fontColor']); + } + + if (isset($formSettings['fontSize'])) { + $styles[] = 'font-size: ' . trim($formSettings['fontSize']) . 'px'; + } + + if (empty($styles)) return $formBody; + return '
' . $formBody . '
'; + } } diff --git a/tests/unit/Form/RendererTest.php b/tests/unit/Form/RendererTest.php index a29496e521..64763d7856 100644 --- a/tests/unit/Form/RendererTest.php +++ b/tests/unit/Form/RendererTest.php @@ -97,4 +97,73 @@ class RendererTest extends \MailPoetUnitTest { $recaptcha = $this->htmlParser->findByXpath($html, "//div[@class='mailpoet_recaptcha']"); expect($recaptcha->length)->equals(0); } + + public function testItShouldRenderBackgroundColour() { + $this->blocksRendererMock + ->expects($this->exactly(2)) + ->method('renderBlock') + ->willReturn('Dummy'); + $this->settingsMock + ->method('get') + ->with('captcha.type') + ->willReturn(Captcha::TYPE_DISABLED); + $formBody = Fixtures::get('simple_form_body'); + $html = $this->renderer->renderHTML([ + 'body' => $formBody, + 'settings' => ['backgroundColor' => 'red'], + ]); + $found = $this->htmlParser->findByXpath($html, "//div"); + expect($found->length)->equals(1); + $div = $found->item(0); + assert($div instanceof \DOMNode); + $source = $div->attributes->getNamedItem('style'); + assert($source instanceof \DOMAttr); + expect($source->value)->equals("background-color: red"); + } + + public function testItShouldRenderColour() { + $this->blocksRendererMock + ->expects($this->exactly(2)) + ->method('renderBlock') + ->willReturn('Dummy'); + $this->settingsMock + ->method('get') + ->with('captcha.type') + ->willReturn(Captcha::TYPE_DISABLED); + $formBody = Fixtures::get('simple_form_body'); + $html = $this->renderer->renderHTML([ + 'body' => $formBody, + 'settings' => ['fontColor' => 'red'], + ]); + $found = $this->htmlParser->findByXpath($html, "//div"); + expect($found->length)->equals(1); + $div = $found->item(0); + assert($div instanceof \DOMNode); + $source = $div->attributes->getNamedItem('style'); + assert($source instanceof \DOMAttr); + expect($source->value)->equals("color: red"); + } + + public function testItShouldRenderFontSize() { + $this->blocksRendererMock + ->expects($this->exactly(2)) + ->method('renderBlock') + ->willReturn('Dummy'); + $this->settingsMock + ->method('get') + ->with('captcha.type') + ->willReturn(Captcha::TYPE_DISABLED); + $formBody = Fixtures::get('simple_form_body'); + $html = $this->renderer->renderHTML([ + 'body' => $formBody, + 'settings' => ['fontSize' => '20'], + ]); + $found = $this->htmlParser->findByXpath($html, "//div"); + expect($found->length)->equals(1); + $div = $found->item(0); + assert($div instanceof \DOMNode); + $source = $div->attributes->getNamedItem('style'); + assert($source instanceof \DOMAttr); + expect($source->value)->equals("font-size: 20px"); + } }