From 072c6e48a0b45ae60a0678d82965811c706247c8 Mon Sep 17 00:00:00 2001 From: Rostislav Wolny Date: Mon, 10 Feb 2020 17:03:31 +0100 Subject: [PATCH] Add basic checkbox test [MAILPOET-265] --- tests/unit/Form/Block/CheckboxTest.php | 65 ++++++++++++++++++++++++++ tests/unit/Form/HtmlParser.php | 19 ++++++++ 2 files changed, 84 insertions(+) create mode 100644 tests/unit/Form/Block/CheckboxTest.php diff --git a/tests/unit/Form/Block/CheckboxTest.php b/tests/unit/Form/Block/CheckboxTest.php new file mode 100644 index 0000000000..ba03b1a6b5 --- /dev/null +++ b/tests/unit/Form/Block/CheckboxTest.php @@ -0,0 +1,65 @@ + 'checkbox', + 'name' => 'Custom checkbox', + 'id' => '1', + 'unique' => '1', + 'static' => '0', + 'params' => [ + 'label' => 'Input label', + 'required' => '', + 'hide_label' => '', + 'values' => [[ + 'value' => 'Checkbox label', + 'is_checked' => '1', + ]], + ], + '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->checkbox = new Checkbox($this->baseMock, $this->wpMock); + $this->htmlParser = new HtmlParser(); + } + + public function testItShouldRenderCheckbox() { + $this->baseMock->expects($this->once())->method('renderLabel')->willReturn(''); + $this->baseMock->expects($this->once())->method('getFieldName')->willReturn('Field name'); + $this->baseMock->expects($this->once())->method('getInputValidation')->willReturn('validation="1"'); + $this->baseMock->expects($this->once())->method('getFieldValue')->willReturn('1'); + $html = $this->checkbox->render($this->block); + $checkboxLabel = $this->htmlParser->getElementByXpath($html, "//label[@class='mailpoet_checkbox_label']"); + expect($checkboxLabel->nodeValue)->equals(' Checkbox label'); + $checkbox = $this->htmlParser->getChildElement($checkboxLabel, 'input'); + $checked = $this->htmlParser->getAttribute($checkbox, 'checked'); + expect($checked->value)->equals('checked'); + } +} diff --git a/tests/unit/Form/HtmlParser.php b/tests/unit/Form/HtmlParser.php index b3cc09ff70..4b025e36ac 100644 --- a/tests/unit/Form/HtmlParser.php +++ b/tests/unit/Form/HtmlParser.php @@ -9,4 +9,23 @@ class HtmlParser extends \MailPoetUnitTest { $value = (new \DOMXPath($dom))->query($xpath); return $value ?: new \DOMNodeList(); } + + public function getElementByXpath(string $html, string $xpath, int $index = 0): \DOMElement { + $value = $this->findByXpath($html, $xpath); + $element = $value->item($index); + assert($element instanceof \DOMElement); + return $element; + } + + public function getChildElement(\DOMElement $element, string $tagName, int $index = 0): \DOMElement { + $result = $element->getElementsByTagName($tagName)->item($index); + assert($result instanceof \DOMElement); + return $result; + } + + public function getAttribute(\DOMElement $element, string $attrNam): \DOMAttr { + $attr = $element->attributes->getNamedItem($attrNam); + assert($attr instanceof \DOMAttr); + return $attr; + } }