]> git.mxchange.org Git - friendica.git/commitdiff
Add tests for Network\Probe::getFeedLink
authorHypolite Petovan <hypolite@mrpetovan.com>
Thu, 21 May 2020 04:24:46 +0000 (00:24 -0400)
committerHypolite Petovan <hypolite@mrpetovan.com>
Thu, 21 May 2020 04:25:48 +0000 (00:25 -0400)
tests/src/Network/ProbeTest.php [new file with mode: 0644]

diff --git a/tests/src/Network/ProbeTest.php b/tests/src/Network/ProbeTest.php
new file mode 100644 (file)
index 0000000..2dfc0e2
--- /dev/null
@@ -0,0 +1,108 @@
+<?php
+
+namespace Friendica\Test\src\Network;
+
+use Friendica\Network\Probe;
+use PHPUnit\Framework\TestCase;
+
+class ProbeTest extends TestCase
+{
+       const TEMPLATENOBASE = '
+<!DOCTYPE html>
+<html lang="en-us">
+<head>
+    <title>Example Blog</title>
+    <link href="{{$link}}" rel="alternate" type="application/rss+xml" title="Example Blog" />
+       <link href="{{$link}}" rel="feed" type="application/rss+xml" title="Example Blog" />
+</head>
+<body>
+    <p>Hello World!</p>
+</body>
+</html>';
+
+       const TEMPLATEBASE = '
+<!DOCTYPE html>
+<html lang="en-us">
+<head>
+    <title>Example Blog</title>
+    <link href="{{$link}}" rel="alternate" type="application/rss+xml" title="Example Blog" />
+       <link href="{{$link}}" rel="feed" type="application/rss+xml" title="Example Blog" />
+    <base href="{{$url}}">
+</head>
+<body>
+    <p>Hello World!</p>
+</body>
+</html>';
+
+       const EXPECTED = [
+               'https://example.org/path/to/blog/index.php' => [
+                       'index.xml'               => 'https://example.org/path/to/blog/index.xml',
+                       './index.xml'             => 'https://example.org/path/to/blog/index.xml',
+                       '../index.xml'            => 'https://example.org/path/to/index.xml',
+                       '/index.xml'              => 'https://example.org/index.xml',
+                       '//example.com/index.xml' => 'https://example.com/index.xml',
+               ],
+               'https://example.org/path/to/blog/' => [
+                       'index.xml'               => 'https://example.org/path/to/blog/index.xml',
+                       './index.xml'             => 'https://example.org/path/to/blog/index.xml',
+                       '../index.xml'            => 'https://example.org/path/to/index.xml',
+                       '/index.xml'              => 'https://example.org/index.xml',
+                       '//example.com/index.xml' => 'https://example.com/index.xml',
+               ],
+               'https://example.org/blog/' => [
+                       'index.xml'               => 'https://example.org/blog/index.xml',
+                       './index.xml'             => 'https://example.org/blog/index.xml',
+                       '../index.xml'            => 'https://example.org/index.xml',
+                       '/index.xml'              => 'https://example.org/index.xml',
+                       '//example.com/index.xml' => 'https://example.com/index.xml',
+               ],
+               'https://example.org' => [
+                       'index.xml'               => 'https://example.org/index.xml',
+                       './index.xml'             => 'https://example.org/index.xml',
+                       '../index.xml'            => 'https://example.org/index.xml',
+                       '/index.xml'              => 'https://example.org/index.xml',
+                       '//example.com/index.xml' => 'https://example.com/index.xml',
+               ],
+       ];
+
+       private function replaceMacros($template, $vars)
+       {
+               foreach ($vars as $var => $value) {
+                       $template = str_replace('{{' . $var . '}}', $value, $template);
+               }
+
+               return $template;
+       }
+
+       /**
+        * @small
+        */
+       public function testGetFeedLinkNoBase()
+       {
+               foreach (self::EXPECTED as $url => $hrefs) {
+                       foreach ($hrefs as $href => $expected) {
+                               $body = $this->replaceMacros(self::TEMPLATENOBASE, ['$link' => $href]);
+
+                               $feedLink = Probe::getFeedLink($url, $body);
+
+                               $this->assertEquals($expected, $feedLink, 'base url = ' . $url . ' | href = ' . $href);
+                       }
+               }
+       }
+
+       /**
+        * @small
+        */
+       public function testGetFeedLinkBase()
+       {
+               foreach (self::EXPECTED as $url => $hrefs) {
+                       foreach ($hrefs as $href => $expected) {
+                               $body = $this->replaceMacros(self::TEMPLATEBASE, ['$url' => $url, '$link' => $href]);
+
+                               $feedLink = Probe::getFeedLink('http://example.com', $body);
+
+                               $this->assertEquals($expected, $feedLink, 'base url = ' . $url . ' | href = ' . $href);
+                       }
+               }
+       }
+}