]> git.mxchange.org Git - friendica.git/blob - tests/src/Network/ProbeTest.php
Merge pull request #9416 from nupplaphil/task/revert_guzzlehttp
[friendica.git] / tests / src / Network / ProbeTest.php
1 <?php
2
3 namespace Friendica\Test\src\Network;
4
5 use Friendica\Network\Probe;
6 use PHPUnit\Framework\TestCase;
7
8 class ProbeTest extends TestCase
9 {
10         const TEMPLATENOBASE = '
11 <!DOCTYPE html>
12 <html lang="en-us">
13 <head>
14     <title>Example Blog</title>
15     <link href="{{$link}}" rel="alternate" type="application/rss+xml" title="Example Blog" />
16         <link href="{{$link}}" rel="feed" type="application/rss+xml" title="Example Blog" />
17 </head>
18 <body>
19     <p>Hello World!</p>
20 </body>
21 </html>';
22
23         const TEMPLATEBASE = '
24 <!DOCTYPE html>
25 <html lang="en-us">
26 <head>
27     <title>Example Blog</title>
28     <link href="{{$link}}" rel="alternate" type="application/rss+xml" title="Example Blog" />
29         <link href="{{$link}}" rel="feed" type="application/rss+xml" title="Example Blog" />
30     <base href="{{$url}}">
31 </head>
32 <body>
33     <p>Hello World!</p>
34 </body>
35 </html>';
36
37         const EXPECTED = [
38                 'https://example.org/path/to/blog/index.php' => [
39                         'index.xml'               => 'https://example.org/path/to/blog/index.xml',
40                         './index.xml'             => 'https://example.org/path/to/blog/index.xml',
41                         '../index.xml'            => 'https://example.org/path/to/index.xml',
42                         '/index.xml'              => 'https://example.org/index.xml',
43                         '//example.com/index.xml' => 'https://example.com/index.xml',
44                 ],
45                 'https://example.org/path/to/blog/' => [
46                         'index.xml'               => 'https://example.org/path/to/blog/index.xml',
47                         './index.xml'             => 'https://example.org/path/to/blog/index.xml',
48                         '../index.xml'            => 'https://example.org/path/to/index.xml',
49                         '/index.xml'              => 'https://example.org/index.xml',
50                         '//example.com/index.xml' => 'https://example.com/index.xml',
51                 ],
52                 'https://example.org/blog/' => [
53                         'index.xml'               => 'https://example.org/blog/index.xml',
54                         './index.xml'             => 'https://example.org/blog/index.xml',
55                         '../index.xml'            => 'https://example.org/index.xml',
56                         '/index.xml'              => 'https://example.org/index.xml',
57                         '//example.com/index.xml' => 'https://example.com/index.xml',
58                 ],
59                 'https://example.org' => [
60                         'index.xml'               => 'https://example.org/index.xml',
61                         './index.xml'             => 'https://example.org/index.xml',
62                         '../index.xml'            => 'https://example.org/index.xml',
63                         '/index.xml'              => 'https://example.org/index.xml',
64                         '//example.com/index.xml' => 'https://example.com/index.xml',
65                 ],
66         ];
67
68         private function replaceMacros($template, $vars)
69         {
70                 foreach ($vars as $var => $value) {
71                         $template = str_replace('{{' . $var . '}}', $value, $template);
72                 }
73
74                 return $template;
75         }
76
77         /**
78          * @small
79          */
80         public function testGetFeedLinkNoBase()
81         {
82                 foreach (self::EXPECTED as $url => $hrefs) {
83                         foreach ($hrefs as $href => $expected) {
84                                 $body = $this->replaceMacros(self::TEMPLATENOBASE, ['$link' => $href]);
85
86                                 $feedLink = Probe::getFeedLink($url, $body);
87
88                                 $this->assertEquals($expected, $feedLink, 'base url = ' . $url . ' | href = ' . $href);
89                         }
90                 }
91         }
92
93         /**
94          * @small
95          */
96         public function testGetFeedLinkBase()
97         {
98                 foreach (self::EXPECTED as $url => $hrefs) {
99                         foreach ($hrefs as $href => $expected) {
100                                 $body = $this->replaceMacros(self::TEMPLATEBASE, ['$url' => $url, '$link' => $href]);
101
102                                 $feedLink = Probe::getFeedLink('http://example.com', $body);
103
104                                 $this->assertEquals($expected, $feedLink, 'base url = ' . $url . ' | href = ' . $href);
105                         }
106                 }
107         }
108 }