From 95d997c79d3d004671883cb962ac368865d08150 Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 6 Jun 2020 12:53:36 +0000 Subject: [PATCH 01/10] Added tests for JsonLD class --- tests/src/Util/JSonLDTest.php | 80 +++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 tests/src/Util/JSonLDTest.php diff --git a/tests/src/Util/JSonLDTest.php b/tests/src/Util/JSonLDTest.php new file mode 100644 index 0000000000..1cb1bb1f9b --- /dev/null +++ b/tests/src/Util/JSonLDTest.php @@ -0,0 +1,80 @@ +. + * + */ + +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 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 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 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); + } +} From 699429ab08071cb2be3a107224f6398cab91b094 Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 6 Jun 2020 14:16:27 +0000 Subject: [PATCH 02/10] Added test for fetching arrays --- tests/src/Util/JSonLDTest.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/src/Util/JSonLDTest.php b/tests/src/Util/JSonLDTest.php index 1cb1bb1f9b..617e771f49 100644 --- a/tests/src/Util/JSonLDTest.php +++ b/tests/src/Util/JSonLDTest.php @@ -37,6 +37,15 @@ class JsonLDTest extends TestCase $this->assertNull($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 testFetchElementNotFound() { $object = []; From 0799b721c941a59c1d4b531a5181a4840cd51e0e Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 6 Jun 2020 14:59:28 +0000 Subject: [PATCH 03/10] Just some more tests added --- tests/src/Util/JSonLDTest.php | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/tests/src/Util/JSonLDTest.php b/tests/src/Util/JSonLDTest.php index 617e771f49..50f8506c83 100644 --- a/tests/src/Util/JSonLDTest.php +++ b/tests/src/Util/JSonLDTest.php @@ -37,6 +37,14 @@ class JsonLDTest extends TestCase $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']]]; @@ -45,6 +53,17 @@ class JsonLDTest extends TestCase $this->assertSame(['value1', 'value2', 'value3'], $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() { @@ -62,6 +81,14 @@ class JsonLDTest extends TestCase $this->assertSame('value', $data); } + public function testFetchElementFoundEmptyString() + { + $object = ['field' => '']; + + $data = JsonLD::fetchElement($object, 'field'); + $this->assertSame('', $data); + } + public function testFetchElementFoundID() { $object = ['field' => ['field2' => 'value2', '@id' => 'value', 'field3' => 'value3']]; From c38452d16e63e077880be804f71d22026837464e Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 6 Jun 2020 19:29:37 +0000 Subject: [PATCH 04/10] Avoiding unwanted side effects --- src/Util/JsonLD.php | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/Util/JsonLD.php b/src/Util/JsonLD.php index b4ff53fdb0..878cd71e6a 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; } } From 11404646db6265acce07096997feb277500b1147 Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 6 Jun 2020 19:53:26 +0000 Subject: [PATCH 05/10] Fix test --- tests/src/Util/JSonLDTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/src/Util/JSonLDTest.php b/tests/src/Util/JSonLDTest.php index 50f8506c83..969a04759c 100644 --- a/tests/src/Util/JSonLDTest.php +++ b/tests/src/Util/JSonLDTest.php @@ -42,7 +42,7 @@ class JsonLDTest extends TestCase $object = ['field' => []]; $data = JsonLD::fetchElementArray($object, 'field'); - $this->assertSame([], $data); + $this->assertSame([[]], $data); } public function testFetchElementArrayFoundID() From 775437cb1c8d469ea2e3be2d7e970f9d83f08577 Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 7 Jun 2020 08:15:04 +0000 Subject: [PATCH 06/10] Another test added --- tests/src/Util/JSonLDTest.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/src/Util/JSonLDTest.php b/tests/src/Util/JSonLDTest.php index 969a04759c..a8d4467f5f 100644 --- a/tests/src/Util/JSonLDTest.php +++ b/tests/src/Util/JSonLDTest.php @@ -53,6 +53,15 @@ class JsonLDTest extends TestCase $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'], From 1f36c655e13ed84cdd9c93f427f3c6d3c78891af Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 7 Jun 2020 08:24:08 +0000 Subject: [PATCH 07/10] Two more tests --- tests/src/Util/JSonLDTest.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tests/src/Util/JSonLDTest.php b/tests/src/Util/JSonLDTest.php index a8d4467f5f..1d9ebdc598 100644 --- a/tests/src/Util/JSonLDTest.php +++ b/tests/src/Util/JSonLDTest.php @@ -114,6 +114,22 @@ class JsonLDTest extends TestCase $this->assertSame('body', $data); } + public function testFetchElementTypeNotFound() + { + $object = ['source' => ['content' => 'body', 'mediaType' => 'text/html']]; + + $data = JsonLD::fetchElement($object, 'source', 'content', 'mediaType', 'text/bbcode'); + $this->assertNull($data); + } + + public function testFetchElementTypeWithoutType() + { + $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'], From bc616a142b5abd26be26cb826ec281b48b5339a2 Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 7 Jun 2020 08:29:19 +0000 Subject: [PATCH 08/10] There is always one last test ... --- tests/src/Util/JSonLDTest.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/tests/src/Util/JSonLDTest.php b/tests/src/Util/JSonLDTest.php index 1d9ebdc598..8fff9dee3d 100644 --- a/tests/src/Util/JSonLDTest.php +++ b/tests/src/Util/JSonLDTest.php @@ -114,7 +114,7 @@ class JsonLDTest extends TestCase $this->assertSame('body', $data); } - public function testFetchElementTypeNotFound() + public function testFetchElementTypeValueNotFound() { $object = ['source' => ['content' => 'body', 'mediaType' => 'text/html']]; @@ -122,6 +122,14 @@ class JsonLDTest extends TestCase $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 testFetchElementTypeWithoutType() { $object = ['source' => ['content' => 'body', 'mediaType' => 'text/bbcode']]; From 8868bc3b5590ae5516603f328e06d5e752e86920 Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 7 Jun 2020 08:39:00 +0000 Subject: [PATCH 09/10] The more tests the better? --- tests/src/Util/JSonLDTest.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tests/src/Util/JSonLDTest.php b/tests/src/Util/JSonLDTest.php index 8fff9dee3d..41fb93bbe3 100644 --- a/tests/src/Util/JSonLDTest.php +++ b/tests/src/Util/JSonLDTest.php @@ -146,4 +146,22 @@ class JsonLDTest extends TestCase $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); + } } From 8823cbf9c4062f5ba62a2512ccf82f043f4d310f Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 7 Jun 2020 08:49:53 +0000 Subject: [PATCH 10/10] Ensure that empty arrays are returned as empty arrays --- tests/src/Util/JSonLDTest.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/tests/src/Util/JSonLDTest.php b/tests/src/Util/JSonLDTest.php index 41fb93bbe3..94c7952b5c 100644 --- a/tests/src/Util/JSonLDTest.php +++ b/tests/src/Util/JSonLDTest.php @@ -98,6 +98,14 @@ class JsonLDTest extends TestCase $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']]; @@ -130,7 +138,7 @@ class JsonLDTest extends TestCase $this->assertNull($data); } - public function testFetchElementTypeWithoutType() + public function testFetchElementKeyWithoutType() { $object = ['source' => ['content' => 'body', 'mediaType' => 'text/bbcode']];