]> git.mxchange.org Git - friendica.git/blob - tests/src/Network/ProbeTest.php
Add test for public value of Depository\PermissionSet->selectOneById
[friendica.git] / tests / src / Network / ProbeTest.php
1 <?php
2
3 namespace Friendica\Test\src\Network;
4
5 use Friendica\Network\Probe;
6 use Friendica\Test\DiceHttpMockHandlerTrait;
7 use Friendica\Test\FixtureTest;
8 use GuzzleHttp\Middleware;
9
10 class ProbeTest extends FixtureTest
11 {
12         use DiceHttpMockHandlerTrait;
13
14         protected function setUp(): void
15         {
16                 parent::setUp();
17
18                 $this->setupHttpMockHandler();
19         }
20
21         const TEMPLATENOBASE = '
22 <!DOCTYPE html>
23 <html lang="en-us">
24 <head>
25     <title>Example Blog</title>
26     <link href="{{$link}}" rel="alternate" type="application/rss+xml" title="Example Blog" />
27         <link href="{{$link}}" rel="feed" type="application/rss+xml" title="Example Blog" />
28 </head>
29 <body>
30     <p>Hello World!</p>
31 </body>
32 </html>';
33
34         const TEMPLATEBASE = '
35 <!DOCTYPE html>
36 <html lang="en-us">
37 <head>
38     <title>Example Blog</title>
39     <link href="{{$link}}" rel="alternate" type="application/rss+xml" title="Example Blog" />
40         <link href="{{$link}}" rel="feed" type="application/rss+xml" title="Example Blog" />
41     <base href="{{$url}}">
42 </head>
43 <body>
44     <p>Hello World!</p>
45 </body>
46 </html>';
47
48         const EXPECTED = [
49                 'https://example.org/path/to/blog/index.php' => [
50                         'index.xml'               => 'https://example.org/path/to/blog/index.xml',
51                         './index.xml'             => 'https://example.org/path/to/blog/index.xml',
52                         '../index.xml'            => 'https://example.org/path/to/index.xml',
53                         '/index.xml'              => 'https://example.org/index.xml',
54                         '//example.com/index.xml' => 'https://example.com/index.xml',
55                 ],
56                 'https://example.org/path/to/blog/' => [
57                         'index.xml'               => 'https://example.org/path/to/blog/index.xml',
58                         './index.xml'             => 'https://example.org/path/to/blog/index.xml',
59                         '../index.xml'            => 'https://example.org/path/to/index.xml',
60                         '/index.xml'              => 'https://example.org/index.xml',
61                         '//example.com/index.xml' => 'https://example.com/index.xml',
62                 ],
63                 'https://example.org/blog/' => [
64                         'index.xml'               => 'https://example.org/blog/index.xml',
65                         './index.xml'             => 'https://example.org/blog/index.xml',
66                         '../index.xml'            => 'https://example.org/index.xml',
67                         '/index.xml'              => 'https://example.org/index.xml',
68                         '//example.com/index.xml' => 'https://example.com/index.xml',
69                 ],
70                 'https://example.org' => [
71                         'index.xml'               => 'https://example.org/index.xml',
72                         './index.xml'             => 'https://example.org/index.xml',
73                         '../index.xml'            => 'https://example.org/index.xml',
74                         '/index.xml'              => 'https://example.org/index.xml',
75                         '//example.com/index.xml' => 'https://example.com/index.xml',
76                 ],
77         ];
78
79         private function replaceMacros($template, $vars)
80         {
81                 foreach ($vars as $var => $value) {
82                         $template = str_replace('{{' . $var . '}}', $value, $template);
83                 }
84
85                 return $template;
86         }
87
88         /**
89          * @small
90          */
91         public function testGetFeedLinkNoBase()
92         {
93                 foreach (self::EXPECTED as $url => $hrefs) {
94                         foreach ($hrefs as $href => $expected) {
95                                 $body = $this->replaceMacros(self::TEMPLATENOBASE, ['$link' => $href]);
96
97                                 $feedLink = Probe::getFeedLink($url, $body);
98
99                                 self::assertEquals($expected, $feedLink, 'base url = ' . $url . ' | href = ' . $href);
100                         }
101                 }
102         }
103
104         /**
105          * @small
106          */
107         public function testGetFeedLinkBase()
108         {
109                 foreach (self::EXPECTED as $url => $hrefs) {
110                         foreach ($hrefs as $href => $expected) {
111                                 $body = $this->replaceMacros(self::TEMPLATEBASE, ['$url' => $url, '$link' => $href]);
112
113                                 $feedLink = Probe::getFeedLink('http://example.com', $body);
114
115                                 self::assertEquals($expected, $feedLink, 'base url = ' . $url . ' | href = ' . $href);
116                         }
117                 }
118         }
119
120         public function dataUri()
121         {
122                 return [
123                         '@-first' => [
124                                 'uri'         => '@Artists4Future_Muenchen@climatejustice.global',
125                                 'assertUri'   => 'Artists4Future_Muenchen@climatejustice.global',
126                                 'assertInfos' => [
127                                         'name'         => 'Artists4Future München',
128                                         'nick'         => 'Artists4Future_Muenchen',
129                                         'url'          => 'https://climatejustice.global/users/Artists4Future_Muenchen',
130                                         'alias'        => 'https://climatejustice.global/@Artists4Future_Muenchen',
131                                         'photo'        => 'https://cdn.masto.host/climatejusticeglobal/accounts/avatars/000/021/220/original/05ee9e827a5b47fc.jpg',
132                                         'header'       => 'https://cdn.masto.host/climatejusticeglobal/accounts/headers/000/021/220/original/9b98b75cf696cd11.jpg',
133                                         'account-type' => 0,
134                                         'about'        => 'Wir sind Künstler oder einfach gerne kreativ tätig und setzen uns unabhängig von politischen Parteien für den Klimaschutz ein. Die Bedingungen zu schaffen, die die [url=https://climatejustice.global/tags/Klimakrise]#Klimakrise[/url] verhindern/eindämmen (gemäß den Forderungen der [url=https://climatejustice.global/tags/Fridays4Future]#Fridays4Future[/url]) ist Aufgabe der Politik, muss aber gesamtgesellschaftlich getragen werden. Mit unseren künstlerischen Aktionen wollen wir einen anderen Zugang anbieten für wissenschaftlich rationale Argumente, speziell zur Erderwärmung und ihre Konsequenzen.',
135                                         'hide'         => 0,
136                                         'batch'        => 'https://climatejustice.global/inbox',
137                                         'notify'       => 'https://climatejustice.global/users/Artists4Future_Muenchen/inbox',
138                                         'poll'         => 'https://climatejustice.global/users/Artists4Future_Muenchen/outbox',
139                                         'subscribe'    => 'https://climatejustice.global/authorize_interaction?uri={uri}',
140                                         'following'    => 'https://climatejustice.global/users/Artists4Future_Muenchen/following',
141                                         'followers'    => 'https://climatejustice.global/users/Artists4Future_Muenchen/followers',
142                                         'inbox'        => 'https://climatejustice.global/users/Artists4Future_Muenchen/inbox',
143                                         'outbox'       => 'https://climatejustice.global/users/Artists4Future_Muenchen/outbox',
144                                         'sharedinbox'  => 'https://climatejustice.global/inbox',
145                                         'priority'     => 0,
146                                         'network'      => 'apub',
147                                         'pubkey'       => '-----BEGIN PUBLIC KEY-----
148 MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA6pYKPuDKb+rmBB869uPV
149 uLYFPosGxMUfenWqfWmFKzEqJ87rAft0IQDAL6dCoYE55ov/lEDNROhasTZLirZf
150 M5b7/1JmwMrAfEiaciuYqDWT3/yDpnekOIdzP5iSClg4zt7e6HRFuClqo4+b6hIE
151 DTMV4ksItvq/92MIu62pZ2SZr5ADPPZ/914lJ86hIH5BanbE8ZFzDS9vJA7V74rt
152 Vvkr5c/OiUyuODNYApSl87Ez8cuj8Edt89YWkDCajQn3EkmXGeJY/VRjEDfcyk6r
153 AvdUa0ArjXud3y3NkakVFZ0d7tmB20Vn9s/CfYHU8FXzbI1kFkov2BX899VVP5Ay
154 xQIDAQAB
155 -----END PUBLIC KEY-----',
156                                         'manually-approve' => 0,
157                                         'baseurl'          => 'https://climatejustice.global',
158                                 ]
159                         ]
160                 ];
161         }
162
163         /**
164          * @dataProvider dataUri
165          */
166         public function testCleanUri(string $uri, string $assertUri, array $assertInfos)
167         {
168                 self::markTestIncomplete('hard work due mocking 19 different http-requests');
169
170                 /**
171                  * Requests:
172                  *
173                  * GET : https://climatejustice.global/.well-known/webfinger?resource=acct:Artists4Future_Muenchen%40climatejustice.global
174                  * 200
175                  * GET : http://localhost/.well-known/nodeinfo
176                  * 200
177                  * GET : http://localhost/statistics.json
178                  * 404
179                  * GET : http://localhost
180                  * 200
181                  * GET : http://localhost/friendica/json
182                  * 404
183                  * GET : http://localhost/friendika/json
184                  * 404
185                  * GET : http://localhost/poco
186                  * 403
187                  * GET : http://localhost/api/v1/directory?limit=1
188                  * 200
189                  * GET : http://localhost/.well-known/x-social-relay
190                  * 200
191                  * GET : http://localhost/friendica
192                  * 404
193                  * GET : https://climatejustice.global/users/Artists4Future_Muenchen
194                  * 200
195                  * GET : https://climatejustice.global/users/Artists4Future_Muenchen/following
196                  * 200
197                  * GET : https://climatejustice.global/users/Artists4Future_Muenchen/followers
198                  * 200
199                  * GET : https://climatejustice.global/users/Artists4Future_Muenchen/outbox
200                  * 200
201                  * GET : https://climatejustice.global/.well-known/nodeinfo
202                  * 200
203                  * GET : https://climatejustice.global/nodeinfo/2.0
204                  * 200
205                  * GET : https://climatejustice.global/poco
206                  * 404
207                  * GET : https://climatejustice.global/api/v1/directory?limit=1
208                  * 200
209                  * GET : https://climatejustice.global/.well-known/webfinger?resource=acct%3AArtists4Future_Muenchen%40climatejustice.global
210                  * 200
211                  *
212                  */
213
214                 $container = [];
215                 $history   = Middleware::history($container);
216
217                 $this->httpRequestHandler->push($history);
218
219                 $cleaned = Probe::cleanURI($uri);
220                 self::assertEquals($assertUri, $cleaned);
221                 self::assertArraySubset($assertInfos, Probe::uri($cleaned, '', 0));
222
223
224                 // Iterate over the requests and responses
225                 foreach ($container as $transaction) {
226                         echo $transaction['request']->getMethod() . " : " . $transaction['request']->getUri() . PHP_EOL;
227                         //> GET, HEAD
228                         if ($transaction['response']) {
229                                 echo $transaction['response']->getStatusCode() . PHP_EOL;
230                         //> 200, 200
231                         } elseif ($transaction['error']) {
232                                 echo $transaction['error'];
233                                 //> exception
234                         }
235                 }
236         }
237 }