From 9238f3fd680f853322f5d1d41d3a035116b48c19 Mon Sep 17 00:00:00 2001 From: Rostislav Wolny Date: Mon, 10 Feb 2020 18:12:58 +0100 Subject: [PATCH] Add test for segments renderer [MAILPOET-2665] --- lib/Form/Block/Segment.php | 10 +++- tests/unit/Form/Block/SegmentTest.php | 75 +++++++++++++++++++++++++++ 2 files changed, 83 insertions(+), 2 deletions(-) create mode 100644 tests/unit/Form/Block/SegmentTest.php diff --git a/lib/Form/Block/Segment.php b/lib/Form/Block/Segment.php index 77fd49863e..37318b4475 100644 --- a/lib/Form/Block/Segment.php +++ b/lib/Form/Block/Segment.php @@ -2,13 +2,19 @@ namespace MailPoet\Form\Block; +use MailPoet\WP\Functions as WPFunctions; + class Segment { /** @var Base */ private $baseRenderer; - public function __construct(Base $baseRenderer) { + /** @var WPFunctions */ + private $wp; + + public function __construct(Base $baseRenderer, WPFunctions $wp) { $this->baseRenderer = $baseRenderer; + $this->wp = $wp; } public function render($block) { @@ -36,7 +42,7 @@ class Segment { $html .= 'name="' . $fieldName . '[]" '; $html .= 'value="' . $option['id'] . '" ' . $isChecked . ' '; $html .= $fieldValidation; - $html .= ' /> ' . esc_attr($option['name']); + $html .= ' /> ' . $this->wp->escAttr($option['name']); $html .= ''; } diff --git a/tests/unit/Form/Block/SegmentTest.php b/tests/unit/Form/Block/SegmentTest.php new file mode 100644 index 0000000000..32733baef1 --- /dev/null +++ b/tests/unit/Form/Block/SegmentTest.php @@ -0,0 +1,75 @@ + 'segment', + 'name' => 'Segments', + 'id' => 'segment', + 'unique' => '1', + 'static' => '0', + 'params' => [ + 'label' => 'Select lists', + 'values' => [[ + 'name' => 'List 1', + 'id' => '1', + 'is_checked' => '1', + ], [ + 'name' => 'List 2', + 'id' => '2', + ]], + ], + 'position' => '1', + ]; + + public function _before() { + parent::_before(); + $this->wpMock = $this->createMock(WPFunctions::class); + $this->wpMock->method('escAttr')->will($this->returnArgument(0)); + $this->baseMock = $this->createMock(Base::class); + $this->segment = new Segment($this->baseMock, $this->wpMock); + $this->htmlParser = new HtmlParser(); + } + + public function testItShouldRenderSegmets() { + $this->baseMock->expects($this->once())->method('renderLabel')->willReturn(''); + $this->baseMock->expects($this->once())->method('getInputValidation')->willReturn('validation="1"'); + $this->baseMock->expects($this->once())->method('getFieldName')->willReturn('Segments'); + + $html = $this->segment->render($this->block); + + $checkbox1 = $this->htmlParser->getElementByXpath($html, "//label[@class='mailpoet_checkbox_label']", 0); + $checkbox2 = $this->htmlParser->getElementByXpath($html, "//label[@class='mailpoet_checkbox_label']", 1); + expect($checkbox1->textContent)->equals(' List 1'); + expect($checkbox2->textContent)->equals(' List 2'); + + $checkbox1Input = $this->htmlParser->getChildElement($checkbox1, 'input'); + $checkbox2Input = $this->htmlParser->getChildElement($checkbox2, 'input'); + expect($this->htmlParser->getAttribute($checkbox1Input, 'value')->value)->equals(1); + expect($this->htmlParser->getAttribute($checkbox2Input, 'value')->value)->equals(2); + expect($this->htmlParser->getAttribute($checkbox1Input, 'name')->value)->equals('data[Segments][]'); + expect($this->htmlParser->getAttribute($checkbox2Input, 'name')->value)->equals('data[Segments][]'); + expect($this->htmlParser->getAttribute($checkbox1Input, 'checked')->value)->equals('checked'); + } +}