diff --git a/src/Util/JsonLD.php b/src/Util/JsonLD.php index b4ff53fdb..878cd71e6 100644 --- a/src/Util/JsonLD.php +++ b/src/Util/JsonLD.php @@ -173,7 +173,7 @@ class JsonLD * * @return array fetched element */ - public static function fetchElementArray($array, $element, $key = '@id') + public static function fetchElementArray($array, $element, $key = null) { if (empty($array)) { return null; @@ -191,12 +191,10 @@ class JsonLD $elements = []; foreach ($array[$element] as $entry) { - if (!is_array($entry)) { + if (!is_array($entry) || (is_null($key) && is_array($entry))) { $elements[] = $entry; - } elseif (isset($entry[$key])) { + } elseif (!is_null($key) && isset($entry[$key])) { $elements[] = $entry[$key]; - } elseif (!empty($entry) || !is_array($entry)) { - $elements[] = $entry; } } diff --git a/tests/src/Util/JSonLDTest.php b/tests/src/Util/JSonLDTest.php new file mode 100644 index 000000000..94c7952b5 --- /dev/null +++ b/tests/src/Util/JSonLDTest.php @@ -0,0 +1,175 @@ +. + * + */ + +namespace Friendica\Test\src\Util; + +use Friendica\Util\JsonLD; +use PHPUnit\Framework\TestCase; + +/** + * JsonLD utility test class + */ +class JsonLDTest extends TestCase +{ + public function testFetchElementArrayNotFound() + { + $object = []; + + $data = JsonLD::fetchElementArray($object, 'field'); + $this->assertNull($data); + } + + public function testFetchElementArrayFoundEmptyArray() + { + $object = ['field' => []]; + + $data = JsonLD::fetchElementArray($object, 'field'); + $this->assertSame([[]], $data); + } + + public function testFetchElementArrayFoundID() + { + $object = ['field' => ['value1', ['@id' => 'value2'], ['@id' => 'value3']]]; + + $data = JsonLD::fetchElementArray($object, 'field', '@id'); + $this->assertSame(['value1', 'value2', 'value3'], $data); + } + + public function testFetchElementArrayFoundID2() + { + $object = ['field' => [['subfield11' => 'value11', 'subfield12' => 'value12'], + ['subfield21' => 'value21', 'subfield22' => 'value22'], + 'value3', ['@id' => 'value4', 'subfield42' => 'value42']]]; + + $data = JsonLD::fetchElementArray($object, 'field', '@id'); + $this->assertSame(['value3', 'value4'], $data); + } + public function testFetchElementArrayFoundArrays() + { + $object = ['field' => [['subfield11' => 'value11', 'subfield12' => 'value12'], + ['subfield21' => 'value21', 'subfield22' => 'value22']]]; + + $expect = [['subfield11' => 'value11', 'subfield12' => 'value12'], + ['subfield21' => 'value21', 'subfield22' => 'value22']]; + + $data = JsonLD::fetchElementArray($object, 'field'); + $this->assertSame($expect, $data); + } + + public function testFetchElementNotFound() + { + $object = []; + + $data = JsonLD::fetchElement($object, 'field'); + $this->assertNull($data); + } + + public function testFetchElementFound() + { + $object = ['field' => 'value']; + + $data = JsonLD::fetchElement($object, 'field'); + $this->assertSame('value', $data); + } + + public function testFetchElementFoundEmptyString() + { + $object = ['field' => '']; + + $data = JsonLD::fetchElement($object, 'field'); + $this->assertSame('', $data); + } + + public function testFetchElementKeyFoundEmptyArray() + { + $object = ['field' => ['content' => []]]; + + $data = JsonLD::fetchElement($object, 'field', 'content'); + $this->assertSame([], $data); + } + + public function testFetchElementFoundID() + { + $object = ['field' => ['field2' => 'value2', '@id' => 'value', 'field3' => 'value3']]; + + $data = JsonLD::fetchElement($object, 'field'); + $this->assertSame('value', $data); + } + + public function testFetchElementType() + { + $object = ['source' => ['content' => 'body', 'mediaType' => 'text/bbcode']]; + + $data = JsonLD::fetchElement($object, 'source', 'content', 'mediaType', 'text/bbcode'); + $this->assertSame('body', $data); + } + + public function testFetchElementTypeValueNotFound() + { + $object = ['source' => ['content' => 'body', 'mediaType' => 'text/html']]; + + $data = JsonLD::fetchElement($object, 'source', 'content', 'mediaType', 'text/bbcode'); + $this->assertNull($data); + } + + public function testFetchElementTypeNotFound() + { + $object = ['source' => ['content' => 'body', 'mediaType' => 'text/html']]; + + $data = JsonLD::fetchElement($object, 'source', 'content', 'mediaType2', 'text/html'); + $this->assertNull($data); + } + + public function testFetchElementKeyWithoutType() + { + $object = ['source' => ['content' => 'body', 'mediaType' => 'text/bbcode']]; + + $data = JsonLD::fetchElement($object, 'source', 'content'); + $this->assertSame('body', $data); + } + + public function testFetchElementTypeArray() + { + $object = ['source' => [['content' => 'body2', 'mediaType' => 'text/html'], + ['content' => 'body', 'mediaType' => 'text/bbcode']]]; + + $data = JsonLD::fetchElement($object, 'source', 'content', 'mediaType', 'text/bbcode'); + $this->assertSame('body', $data); + } + + public function testFetchElementTypeValueArrayNotFound() + { + $object = ['source' => [['content' => 'body2', 'mediaType' => 'text/html'], + ['content' => 'body', 'mediaType' => 'text/bbcode']]]; + + $data = JsonLD::fetchElement($object, 'source', 'content', 'mediaType', 'text/markdown'); + $this->assertNull($data); + } + + public function testFetchElementTypeArrayNotFound() + { + $object = ['source' => [['content' => 'body2', 'mediaType' => 'text/html'], + ['content' => 'body', 'mediaType' => 'text/bbcode']]]; + + $data = JsonLD::fetchElement($object, 'source', 'content', 'mediaType2', 'text/bbcode'); + $this->assertNull($data); + } +}