3 * @copyright Copyright (C) 2010-2022, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Test\src\Util;
24 use Friendica\Util\JsonLD;
25 use PHPUnit\Framework\TestCase;
28 * JsonLD utility test class
30 class JsonLDTest extends TestCase
32 public function testFetchElementArrayNotFound()
36 $data = JsonLD::fetchElementArray($object, 'field');
37 self::assertNull($data);
40 public function testFetchElementArrayFoundEmptyArray()
42 $object = ['field' => []];
44 $data = JsonLD::fetchElementArray($object, 'field');
45 self::assertSame([[]], $data);
48 public function testFetchElementArrayFoundID()
50 $object = ['field' => ['value1', ['@id' => 'value2'], ['@id' => 'value3']]];
52 $data = JsonLD::fetchElementArray($object, 'field', '@id');
53 self::assertSame(['value1', 'value2', 'value3'], $data);
56 public function testFetchElementArrayFoundID2()
58 $object = ['field' => [['subfield11' => 'value11', 'subfield12' => 'value12'],
59 ['subfield21' => 'value21', 'subfield22' => 'value22'],
60 'value3', ['@id' => 'value4', 'subfield42' => 'value42']]];
62 $data = JsonLD::fetchElementArray($object, 'field', '@id');
63 self::assertSame(['value3', 'value4'], $data);
66 public function testFetchElementArrayFoundArrays()
68 $object = ['field' => [['subfield11' => 'value11', 'subfield12' => 'value12'],
69 ['subfield21' => 'value21', 'subfield22' => 'value22']]];
71 $expect = [['subfield11' => 'value11', 'subfield12' => 'value12'],
72 ['subfield21' => 'value21', 'subfield22' => 'value22']];
74 $data = JsonLD::fetchElementArray($object, 'field');
75 self::assertSame($expect, $data);
78 public function testFetchElementArrayTypeValue()
80 $object = ['field' => [['subfield11' => 'value11', 'subfield12' => 'value12'],
81 ['subfield21' => 'value21', 'subfield22' => 'value22']]];
83 $expect = [['subfield11' => 'value11', 'subfield12' => 'value12']];
85 $data = JsonLD::fetchElementArray($object, 'field', null, 'subfield11', 'value11');
86 self::assertSame($expect, $data);
89 public function testFetchElementNotFound()
93 $data = JsonLD::fetchElement($object, 'field');
94 self::assertNull($data);
97 public function testFetchElementFound()
99 $object = ['field' => 'value'];
101 $data = JsonLD::fetchElement($object, 'field');
102 self::assertSame('value', $data);
105 public function testFetchElementFoundEmptyString()
107 $object = ['field' => ''];
109 $data = JsonLD::fetchElement($object, 'field');
110 self::assertSame('', $data);
113 public function testFetchElementKeyFoundEmptyArray()
115 $object = ['field' => ['content' => []]];
117 $data = JsonLD::fetchElement($object, 'field', 'content');
118 self::assertSame([], $data);
121 public function testFetchElementFoundID()
123 $object = ['field' => ['field2' => 'value2', '@id' => 'value', 'field3' => 'value3']];
125 $data = JsonLD::fetchElement($object, 'field');
126 self::assertSame('value', $data);
129 public function testFetchElementType()
131 $object = ['source' => ['content' => 'body', 'mediaType' => 'text/bbcode']];
133 $data = JsonLD::fetchElement($object, 'source', 'content', 'mediaType', 'text/bbcode');
134 self::assertSame('body', $data);
137 public function testFetchElementTypeValueNotFound()
139 $object = ['source' => ['content' => 'body', 'mediaType' => 'text/html']];
141 $data = JsonLD::fetchElement($object, 'source', 'content', 'mediaType', 'text/bbcode');
142 self::assertNull($data);
145 public function testFetchElementTypeNotFound()
147 $object = ['source' => ['content' => 'body', 'mediaType' => 'text/html']];
149 $data = JsonLD::fetchElement($object, 'source', 'content', 'mediaType2', 'text/html');
150 self::assertNull($data);
153 public function testFetchElementKeyWithoutType()
155 $object = ['source' => ['content' => 'body', 'mediaType' => 'text/bbcode']];
157 $data = JsonLD::fetchElement($object, 'source', 'content');
158 self::assertSame('body', $data);
161 public function testFetchElementTypeArray()
163 $object = ['source' => [['content' => 'body2', 'mediaType' => 'text/html'],
164 ['content' => 'body', 'mediaType' => 'text/bbcode']]];
166 $data = JsonLD::fetchElement($object, 'source', 'content', 'mediaType', 'text/bbcode');
167 self::assertSame('body', $data);
170 public function testFetchElementTypeValueArrayNotFound()
172 $object = ['source' => [['content' => 'body2', 'mediaType' => 'text/html'],
173 ['content' => 'body', 'mediaType' => 'text/bbcode']]];
175 $data = JsonLD::fetchElement($object, 'source', 'content', 'mediaType', 'text/markdown');
176 self::assertNull($data);
179 public function testFetchElementTypeArrayNotFound()
181 $object = ['source' => [['content' => 'body2', 'mediaType' => 'text/html'],
182 ['content' => 'body', 'mediaType' => 'text/bbcode']]];
184 $data = JsonLD::fetchElement($object, 'source', 'content', 'mediaType2', 'text/bbcode');
185 self::assertNull($data);