From 7ea218aa1cb635434f939ba033abcf711f871665 Mon Sep 17 00:00:00 2001 From: Steve Boyd Date: Wed, 16 Jan 2019 16:24:45 +1300 Subject: [PATCH 1/2] Ensure document is not falsey before attempting to clone it --- src/View/Parsers/HTMLValue.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/View/Parsers/HTMLValue.php b/src/View/Parsers/HTMLValue.php index d41ef3708..aa6a3f535 100644 --- a/src/View/Parsers/HTMLValue.php +++ b/src/View/Parsers/HTMLValue.php @@ -35,7 +35,11 @@ abstract class HTMLValue extends ViewableData */ public function getContent() { - $doc = clone $this->getDocument(); + $document = $this->getDocument(); + if (!$document) { + return ''; + } + $doc = clone $document; $xp = new DOMXPath($doc); // If there's no body, the content is empty string From d28552915d816036cf1bc80642776e58744f44a4 Mon Sep 17 00:00:00 2001 From: Steve Boyd Date: Fri, 25 Jan 2019 15:55:03 +1300 Subject: [PATCH 2/2] Add unit test for an invalid HTMLValue --- tests/php/View/Parsers/HTML4ValueTest.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tests/php/View/Parsers/HTML4ValueTest.php b/tests/php/View/Parsers/HTML4ValueTest.php index 5e6f9ed28..018f7b08d 100644 --- a/tests/php/View/Parsers/HTML4ValueTest.php +++ b/tests/php/View/Parsers/HTML4ValueTest.php @@ -80,4 +80,19 @@ class HTML4ValueTest extends SapphireTest $value->setContent(''); $this->assertEquals('', $value->getContent(), "'\"' character is escaped"); } + + public function testGetContent() + { + $value = new HTML4Value(); + + $value->setContent('

This is valid

'); + $this->assertEquals('

This is valid

', $value->getContent(), "Valid content is returned"); + + $value->setContent('valid is false + // for instance if a content editor saves something really weird in a LiteralField + // we can manually get to this state via ->setInvalid() + $value->setInvalid(); + $this->assertEquals('', $value->getContent(), "Blank string is returned when invalid"); + } }