From e2780ba0a011d767e4bcce55c7c5567b765d7410 Mon Sep 17 00:00:00 2001 From: Aaron Parecki Date: Mon, 9 Jul 2018 07:31:13 -0700 Subject: [PATCH] when interpreting JSON, don't require `value` for html values --- lib/XRay/Formats/Mf2.php | 5 ++- tests/ParseTest.php | 94 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+), 2 deletions(-) diff --git a/lib/XRay/Formats/Mf2.php b/lib/XRay/Formats/Mf2.php index 01b2217..8463be8 100644 --- a/lib/XRay/Formats/Mf2.php +++ b/lib/XRay/Formats/Mf2.php @@ -229,7 +229,7 @@ class Mf2 extends Format { $content = $item['properties'][$property][0]; if(is_string($content)) { $textContent = $content; - } elseif(!is_string($content) && is_array($content) && array_key_exists('value', $content)) { + } elseif(!is_string($content) && is_array($content) && array_key_exists('html', $content)) { if(array_key_exists('html', $content)) { // Only allow images in the content if there is no photo property set if(isset($item['properties']['photo'])) @@ -241,7 +241,8 @@ class Mf2 extends Format { #$textContent = trim(str_replace(" ","\r",$content['value'])); $textContent = trim(self::stripHTML($htmlContent)); } else { - $textContent = trim($content['value']); + if(isset($content['value'])) + $textContent = trim($content['value']); } } diff --git a/tests/ParseTest.php b/tests/ParseTest.php index 9097739..f083817 100644 --- a/tests/ParseTest.php +++ b/tests/ParseTest.php @@ -74,6 +74,100 @@ class ParseTest extends PHPUnit_Framework_TestCase { $this->assertEquals('This page has a link to target.example.com and some formatted text.', $data->data->content->html); } + public function testContentFromJSONOnlyPlaintext() { + $mf2JSON = json_encode([ + 'items' => [[ + 'type' => ['h-entry'], + 'properties' => [ + 'content' => [ + 'plaintext' + ] + ] + ]] + ]); + $xray = new \p3k\XRay(); + $parsed = $xray->process(false, $mf2JSON); + $item = $parsed['data']; + $this->assertEquals('entry', $item['type']); + $this->assertEquals('plaintext', $item['content']['text']); + $this->assertArrayNotHasKey('html', $item['content']); + } + + public function testHTMLContentFromJSONNoPlaintext() { + $mf2JSON = json_encode([ + 'items' => [[ + 'type' => ['h-entry'], + 'properties' => [ + 'content' => [[ + 'html' => 'bold italic text' + ]] + ] + ]] + ]); + $xray = new \p3k\XRay(); + $parsed = $xray->process(false, $mf2JSON); + $item = $parsed['data']; + $this->assertEquals('entry', $item['type']); + $this->assertEquals('bold italic text', $item['content']['text']); + $this->assertEquals('bold italic text', $item['content']['html']); + } + + public function testHTMLContentFromJSONEmptyTags() { + $mf2JSON = json_encode([ + 'items' => [[ + 'type' => ['h-entry'], + 'properties' => [ + 'content' => [[ + 'html' => '' + ]] + ] + ]] + ]); + $xray = new \p3k\XRay(); + $parsed = $xray->process(false, $mf2JSON); + $item = $parsed['data']; + $this->assertEquals('entry', $item['type']); + $this->assertArrayNotHasKey('content', $item); + } + + public function testHTMLContentFromJSONContentMismatch() { + $mf2JSON = json_encode([ + 'items' => [[ + 'type' => ['h-entry'], + 'properties' => [ + 'content' => [[ + 'value' => 'foo', + 'html' => 'bar' + ]] + ] + ]] + ]); + $xray = new \p3k\XRay(); + $parsed = $xray->process(false, $mf2JSON); + $item = $parsed['data']; + $this->assertEquals('entry', $item['type']); + $this->assertEquals('bar', $item['content']['text']); + $this->assertEquals('bar', $item['content']['html']); + } + + public function testHTMLContentFromJSONNoHTML() { + $mf2JSON = json_encode([ + 'items' => [[ + 'type' => ['h-entry'], + 'properties' => [ + 'content' => [[ + 'value' => 'foo', + ]] + ] + ]] + ]); + $xray = new \p3k\XRay(); + $parsed = $xray->process(false, $mf2JSON); + $item = $parsed['data']; + $this->assertEquals('entry', $item['type']); + $this->assertArrayNotHasKey('content', $item); + } + public function testFindTargetLinkIsImage() { $url = 'http://source.example.com/link-is-img'; $response = $this->parse(['url' => $url, 'target' => 'http://target.example.com/photo.jpg']);