]> git.mxchange.org Git - friendica.git/blob - tests/src/Util/JsonLDTest.php
Merge pull request #9530 from annando/fatal
[friendica.git] / tests / src / Util / JsonLDTest.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 namespace Friendica\Test\src\Util;
23
24 use Friendica\Util\JsonLD;
25 use PHPUnit\Framework\TestCase;
26
27 /**
28  * JsonLD utility test class
29  */
30 class JsonLDTest extends TestCase
31 {       
32         public function testFetchElementArrayNotFound()
33         {
34                 $object = [];
35
36                 $data = JsonLD::fetchElementArray($object, 'field');
37                 self::assertNull($data);
38         }
39
40         public function testFetchElementArrayFoundEmptyArray()
41         {
42                 $object = ['field' => []];
43
44                 $data = JsonLD::fetchElementArray($object, 'field');
45                 self::assertSame([[]], $data);
46         }
47
48         public function testFetchElementArrayFoundID()
49         {
50                 $object = ['field' => ['value1', ['@id' => 'value2'], ['@id' => 'value3']]];
51
52                 $data = JsonLD::fetchElementArray($object, 'field', '@id');
53                 self::assertSame(['value1', 'value2', 'value3'], $data);
54         }
55
56         public function testFetchElementArrayFoundID2()
57         {
58                 $object = ['field' => [['subfield11' => 'value11', 'subfield12' => 'value12'],
59                         ['subfield21' => 'value21', 'subfield22' => 'value22'],
60                         'value3', ['@id' => 'value4', 'subfield42' => 'value42']]];
61
62                 $data = JsonLD::fetchElementArray($object, 'field', '@id');
63                 self::assertSame(['value3', 'value4'], $data);
64         }
65
66         public function testFetchElementArrayFoundArrays()
67         {
68                 $object = ['field' => [['subfield11' => 'value11', 'subfield12' => 'value12'],
69                         ['subfield21' => 'value21', 'subfield22' => 'value22']]];
70
71                 $expect = [['subfield11' => 'value11', 'subfield12' => 'value12'],
72                         ['subfield21' => 'value21', 'subfield22' => 'value22']];
73
74                 $data = JsonLD::fetchElementArray($object, 'field');
75                 self::assertSame($expect, $data);
76         }
77
78         public function testFetchElementArrayTypeValue()
79         {
80                 $object = ['field' => [['subfield11' => 'value11', 'subfield12' => 'value12'],
81                         ['subfield21' => 'value21', 'subfield22' => 'value22']]];
82
83                 $expect = [['subfield11' => 'value11', 'subfield12' => 'value12']];
84
85                 $data = JsonLD::fetchElementArray($object, 'field', null, 'subfield11', 'value11');
86                 self::assertSame($expect, $data);
87         }
88
89         public function testFetchElementNotFound()
90         {
91                 $object = [];
92
93                 $data = JsonLD::fetchElement($object, 'field');
94                 self::assertNull($data);
95         }
96
97         public function testFetchElementFound()
98         {
99                 $object = ['field' => 'value'];
100
101                 $data = JsonLD::fetchElement($object, 'field');
102                 self::assertSame('value', $data);
103         }
104
105         public function testFetchElementFoundEmptyString()
106         {
107                 $object = ['field' => ''];
108
109                 $data = JsonLD::fetchElement($object, 'field');
110                 self::assertSame('', $data);
111         }
112
113         public function testFetchElementKeyFoundEmptyArray()
114         {
115                 $object = ['field' => ['content' => []]];
116
117                 $data = JsonLD::fetchElement($object, 'field', 'content');
118                 self::assertSame([], $data);
119         }
120
121         public function testFetchElementFoundID()
122         {
123                 $object = ['field' => ['field2' => 'value2', '@id' => 'value', 'field3' => 'value3']];
124
125                 $data = JsonLD::fetchElement($object, 'field');
126                 self::assertSame('value', $data);
127         }
128
129         public function testFetchElementType()
130         {
131                 $object = ['source' => ['content' => 'body', 'mediaType' => 'text/bbcode']];
132
133                 $data = JsonLD::fetchElement($object, 'source', 'content', 'mediaType', 'text/bbcode');
134                 self::assertSame('body', $data);
135         }
136
137         public function testFetchElementTypeValueNotFound()
138         {
139                 $object = ['source' => ['content' => 'body', 'mediaType' => 'text/html']];
140
141                 $data = JsonLD::fetchElement($object, 'source', 'content', 'mediaType', 'text/bbcode');
142                 self::assertNull($data);
143         }
144
145         public function testFetchElementTypeNotFound()
146         {
147                 $object = ['source' => ['content' => 'body', 'mediaType' => 'text/html']];
148
149                 $data = JsonLD::fetchElement($object, 'source', 'content', 'mediaType2', 'text/html');
150                 self::assertNull($data);
151         }
152
153         public function testFetchElementKeyWithoutType()
154         {
155                 $object = ['source' => ['content' => 'body', 'mediaType' => 'text/bbcode']];
156
157                 $data = JsonLD::fetchElement($object, 'source', 'content');
158                 self::assertSame('body', $data);
159         }
160
161         public function testFetchElementTypeArray()
162         {
163                 $object = ['source' => [['content' => 'body2', 'mediaType' => 'text/html'],
164                         ['content' => 'body', 'mediaType' => 'text/bbcode']]];
165
166                 $data = JsonLD::fetchElement($object, 'source', 'content', 'mediaType', 'text/bbcode');
167                 self::assertSame('body', $data);
168         }
169
170         public function testFetchElementTypeValueArrayNotFound()
171         {
172                 $object = ['source' => [['content' => 'body2', 'mediaType' => 'text/html'],
173                         ['content' => 'body', 'mediaType' => 'text/bbcode']]];
174
175                 $data = JsonLD::fetchElement($object, 'source', 'content', 'mediaType', 'text/markdown');
176                 self::assertNull($data);
177         }
178
179         public function testFetchElementTypeArrayNotFound()
180         {
181                 $object = ['source' => [['content' => 'body2', 'mediaType' => 'text/html'],
182                         ['content' => 'body', 'mediaType' => 'text/bbcode']]];
183
184                 $data = JsonLD::fetchElement($object, 'source', 'content', 'mediaType2', 'text/bbcode');
185                 self::assertNull($data);
186         }
187 }