|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Elbformat\FieldHelperBundle\Tests\FieldHelper; |
| 6 | + |
| 7 | +use Elbformat\FieldHelperBundle\FieldHelper\RichtextFieldHelper; |
| 8 | +use eZ\Publish\API\Repository\Values\Content\Field; |
| 9 | +use eZ\Publish\Core\FieldType\Value; |
| 10 | +use eZ\Publish\Core\Helper\FieldHelper; |
| 11 | +use eZ\Publish\Core\Repository\Values\Content\Content; |
| 12 | +use eZ\Publish\Core\Repository\Values\Content\ContentUpdateStruct; |
| 13 | +use EzSystems\EzPlatformRichText\eZ\FieldType\RichText\Value as RichTextValue; |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | + |
| 16 | +/** |
| 17 | + * @author Ronny Gericke <[email protected]> |
| 18 | + */ |
| 19 | +class RichtextFieldHelperTest extends TestCase |
| 20 | +{ |
| 21 | + private const FIELD_NAME = 'richtext_field'; |
| 22 | + |
| 23 | + protected RichtextFieldHelper $richtextFieldHelper; |
| 24 | + |
| 25 | + public function setUp(): void |
| 26 | + { |
| 27 | + $fieldHelper = $this->createMock(FieldHelper::class); |
| 28 | + $fieldHelper->method('isFieldEmpty')->willReturnCallback([$this, 'isEmptyField']); |
| 29 | + |
| 30 | + $this->richtextFieldHelper = new RichtextFieldHelper($fieldHelper); |
| 31 | + } |
| 32 | + |
| 33 | + public function testGetName(): void |
| 34 | + { |
| 35 | + $this->assertSame('Elbformat\FieldHelperBundle\FieldHelper\RichtextFieldHelper', RichtextFieldHelper::getName()); |
| 36 | + } |
| 37 | + |
| 38 | + public function testGetXml(): void |
| 39 | + { |
| 40 | + $value = $this->getXmlDomFromString('some text'); |
| 41 | + $content = $this->createContentFromValue(new RichTextValue($value)); |
| 42 | + |
| 43 | + $this->assertSame( |
| 44 | + $value, |
| 45 | + $this->richtextFieldHelper->getXml($content, self::FIELD_NAME) |
| 46 | + ); |
| 47 | + } |
| 48 | + |
| 49 | + public function testIsEmpty(): void |
| 50 | + { |
| 51 | + $content = $this->createContentFromValue(new RichTextValue(null)); |
| 52 | + $this->assertTrue($this->richtextFieldHelper->isEmpty($content, self::FIELD_NAME)); |
| 53 | + } |
| 54 | + |
| 55 | + public function testIsNotEmpty(): void |
| 56 | + { |
| 57 | + $value = $this->getXmlDomFromString('not empty'); |
| 58 | + $content = $this->createContentFromValue(new RichTextValue($value)); |
| 59 | + $this->assertFalse($this->richtextFieldHelper->isEmpty($content, self::FIELD_NAME)); |
| 60 | + } |
| 61 | + |
| 62 | + public function isEmptyField(Content $content, string $fieldName): bool |
| 63 | + { |
| 64 | + $value = $content->getField($fieldName)->value; |
| 65 | + if (null === $value->xml) { |
| 66 | + return false; |
| 67 | + } |
| 68 | + |
| 69 | + return !$value->xml->documentElement->hasChildNodes(); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * @dataProvider getUpdateValues |
| 74 | + */ |
| 75 | + public function testUpdateXml(?\DOMDocument $newValue, bool $expectedResult): void |
| 76 | + { |
| 77 | + $struct = new ContentUpdateStruct(); |
| 78 | + $value = new RichTextValue( |
| 79 | + $this->getXmlDomFromString('initial value') |
| 80 | + ); |
| 81 | + $content = $this->createContentFromValue($value); |
| 82 | + |
| 83 | + $this->assertSame( |
| 84 | + $expectedResult, |
| 85 | + $this->richtextFieldHelper->updateXml($struct, self::FIELD_NAME, $newValue, $content) |
| 86 | + ); |
| 87 | + } |
| 88 | + |
| 89 | + public function getUpdateValues(): array |
| 90 | + { |
| 91 | + return [ |
| 92 | + [$this->getXmlDomFromString('initial value'), false], |
| 93 | + [$this->getXmlDomFromString('other value'), true], |
| 94 | + [null, true], |
| 95 | + ]; |
| 96 | + } |
| 97 | + |
| 98 | + protected function getXmlDomFromString(string $str): \DOMDocument |
| 99 | + { |
| 100 | + $xmlStr = sprintf( |
| 101 | + '<?xml version="1.0" encoding="UTF-8"?><section xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:ezxhtml="http://ez.no/xmlns/ezpublish/docbook/xhtml" xmlns:ezcustom="http://ez.no/xmlns/ezpublish/docbook/custom" version="5.0-variant ezpublish-1.0"><para>%s</para></section>', |
| 102 | + $str |
| 103 | + ); |
| 104 | + $dom = new \DOMDocument(); |
| 105 | + $dom->loadXML($xmlStr); |
| 106 | + |
| 107 | + return $dom; |
| 108 | + } |
| 109 | + |
| 110 | + protected function createContentFromValue(Value $value): Content |
| 111 | + { |
| 112 | + $field = new Field(['value' => $value]); |
| 113 | + |
| 114 | + $content = $this->createMock(Content::class); |
| 115 | + $content->method('getField')->with(self::FIELD_NAME)->willReturn($field); |
| 116 | + |
| 117 | + return $content; |
| 118 | + } |
| 119 | +} |
0 commit comments