Add test for segments renderer
[MAILPOET-2665]
This commit is contained in:
committed by
Jack Kitterhing
parent
778576e674
commit
9238f3fd68
@@ -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 .= '</label>';
|
||||
}
|
||||
|
||||
|
75
tests/unit/Form/Block/SegmentTest.php
Normal file
75
tests/unit/Form/Block/SegmentTest.php
Normal file
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
|
||||
namespace MailPoet\Test\Form\Block;
|
||||
|
||||
use MailPoet\Form\Block\Base;
|
||||
use MailPoet\Form\Block\Segment;
|
||||
use MailPoet\Test\Form\HtmlParser;
|
||||
use MailPoet\WP\Functions as WPFunctions;
|
||||
use PHPUnit\Framework\MockObject\MockObject;
|
||||
|
||||
require_once __DIR__ . '/../HtmlParser.php';
|
||||
|
||||
class SegmentTest extends \MailPoetUnitTest {
|
||||
/** @var Segment */
|
||||
private $segment;
|
||||
|
||||
/** @var MockObject|WPFunctions */
|
||||
private $wpMock;
|
||||
|
||||
/** @var MockObject|Base */
|
||||
private $baseMock;
|
||||
|
||||
/** @var HtmlParser */
|
||||
private $htmlParser;
|
||||
|
||||
private $block = [
|
||||
'type' => '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('<label></label>');
|
||||
$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');
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user